Skip to content

Commit

Permalink
Part of theheraldproject#113. Compiles with Herald Beacon libs, but u…
Browse files Browse the repository at this point in the history
…ses 40kb SRAM.

Signed off by: Adam Fowler <[email protected]>
  • Loading branch information
adamfowleruk committed Apr 13, 2022
1 parent 6ac0ff7 commit 7ef1fdb
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 39 deletions.
4 changes: 2 additions & 2 deletions config/zephyr/base-mesh.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Zephyr STD C++ API support - Required by the Herald API
CONFIG_CPLUSPLUS=y
# CONFIG_NEWLIB_LIBC=y
# CONFIG_LIB_CPLUSPLUS=y
CONFIG_NEWLIB_LIBC=y
CONFIG_LIB_CPLUSPLUS=y

# Bug in zephyr kernel includes - must enable USB if board forces it - see KConfig
# The below is no longer required in base as of NCS1.8/Zephyr2.7
Expand Down
60 changes: 30 additions & 30 deletions herald-mesh-relay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,35 @@ set(BASE_CONFIG ../config/zephyr)
set(OVERLAY_CONFIG
${BASE_CONFIG}/base-mesh.conf
${BASE_CONFIG}/usb.conf
# ${BASE_CONFIG}/receiver.conf
# ${BASE_CONFIG}/transmitter.conf
${BASE_CONFIG}/receiver.conf
${BASE_CONFIG}/transmitter.conf
)

# if($ENV{BOARD} MATCHES .*nrf5340.*)
# set(OVERLAY_CONFIG
# ${OVERLAY_CONFIG}
# ${BASE_CONFIG}/nrf5340.conf
# )
# add_definitions(-DHERALD_MEMORYARENA_MAX=4096)
# elseif($ENV{BOARD} MATCHES .*nrf52840.*)
# set(OVERLAY_CONFIG
# ${OVERLAY_CONFIG}
# ${BASE_CONFIG}/nrf52840.conf
# )
# add_definitions(-DHERALD_MEMORYARENA_MAX=4096)
# elseif($ENV{BOARD} MATCHES .*nrf52832.*)
# set(OVERLAY_CONFIG
# ${OVERLAY_CONFIG}
# ${BASE_CONFIG}/nrf52832.conf
# )
# add_definitions(-DHERALD_MEMORYARENA_MAX=512)
# elseif($ENV{BOARD} MATCHES .*nrf52833.*)
# set(OVERLAY_CONFIG
# ${OVERLAY_CONFIG}
# ${BASE_CONFIG}/nrf52833.conf
# )
# add_definitions(-DHERALD_MEMORYARENA_MAX=4096)
# endif()
if($ENV{BOARD} MATCHES .*nrf5340.*)
set(OVERLAY_CONFIG
${OVERLAY_CONFIG}
${BASE_CONFIG}/nrf5340.conf
)
add_definitions(-DHERALD_MEMORYARENA_MAX=4096)
elseif($ENV{BOARD} MATCHES .*nrf52840.*)
set(OVERLAY_CONFIG
${OVERLAY_CONFIG}
${BASE_CONFIG}/nrf52840.conf
)
add_definitions(-DHERALD_MEMORYARENA_MAX=4096)
elseif($ENV{BOARD} MATCHES .*nrf52832.*)
set(OVERLAY_CONFIG
${OVERLAY_CONFIG}
${BASE_CONFIG}/nrf52832.conf
)
add_definitions(-DHERALD_MEMORYARENA_MAX=512)
elseif($ENV{BOARD} MATCHES .*nrf52833.*)
set(OVERLAY_CONFIG
${OVERLAY_CONFIG}
${BASE_CONFIG}/nrf52833.conf
)
add_definitions(-DHERALD_MEMORYARENA_MAX=4096)
endif()
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(OVERLAY_CONFIG
${OVERLAY_CONFIG}
Expand Down Expand Up @@ -106,9 +106,9 @@ endif()

message("HERALD SOURCES MESH: ${HERALD_SOURCES_MESH}")
target_sources(app PRIVATE
# ${HERALD_SOURCES}
# ${HERALD_SOURCES_ZEPHYR}
# ${HERALD_SOURCES_TINYCRYPT}
${HERALD_SOURCES}
${HERALD_SOURCES_ZEPHYR}
${HERALD_SOURCES_TINYCRYPT}
#${HERALD_SOURCES_MBEDTLS}
${HERALD_SOURCES_MESH}
src/main.cpp
Expand Down
206 changes: 199 additions & 7 deletions herald-mesh-relay/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
/*
* Copyright 2020-2021 Herald Project Contributors
* Copyright 2020-2022 Herald Project Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// #include "../../herald/herald.h" // This is convenient, but leads to large binaries!
#include "../../herald/include/herald/sensor_array.h"
#include "../../herald/include/herald/sensor.h"
#include "../../herald/include/herald/datatype/target_identifier.h"
#include "../../herald/include/herald/datatype/proximity.h"
#include "../../herald/include/herald/datatype/date.h"
#include "../../herald/include/herald/datatype/payload_data.h"
#include "../../herald/include/herald/datatype/sensor_type.h"
#include "../../herald/include/herald/datatype/immediate_send_data.h"
#include "../../herald/include/herald/datatype/location.h"
#include "../../herald/include/herald/datatype/sensor_state.h"
#include "../../herald/include/herald/zephyr_context.h"
#include "../../herald/include/herald/sensor_delegate.h"
#include "../../herald/include/herald/ble/zephyr/concrete_ble_receiver.h"
#include "../../herald/include/herald/ble/zephyr/concrete_ble_transmitter.h"
#include "../../herald/include/herald/ble/zephyr/nordic_uart/nordic_uart_sensor_delegate.h"
#include "../../herald/include/herald/payload/beacon/beacon_payload_data_supplier.h"
#include "../../herald/include/herald/payload/extended/extended_data.h"
#include "../../herald/include/herald/payload/fixed/fixed_payload_data_supplier.h"
#include "../../herald/include/herald/ble/ble_sensor_configuration.h"

#include <bluetooth/bluetooth.h>
#include <bluetooth/mesh/models.h>
#include <bluetooth/mesh/dk_prov.h>
Expand All @@ -15,12 +36,10 @@
#include "herald/mesh/location_services_srv.h"

#include <logging/log.h>
// namespace applogging {
LOG_MODULE_REGISTER(app, CONFIG_APP_LOG_LEVEL);
#define APP_DBG(_msg,...) LOG_DBG(_msg,##__VA_ARGS__);
#define APP_INF(_msg,...) LOG_INF(_msg,##__VA_ARGS__);
#define APP_ERR(_msg,...) LOG_ERR(_msg,##__VA_ARGS__);
// }
LOG_MODULE_REGISTER(app, CONFIG_APP_LOG_LEVEL);
#define APP_DBG(_msg,...) LOG_DBG(_msg,##__VA_ARGS__);
#define APP_INF(_msg,...) LOG_INF(_msg,##__VA_ARGS__);
#define APP_ERR(_msg,...) LOG_ERR(_msg,##__VA_ARGS__);

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS 1000
Expand Down Expand Up @@ -75,6 +94,171 @@ static void bt_ready(int err)
lbs_handler_init();
}

struct k_thread herald_thread;
constexpr int stackMaxSize =
#ifdef CONFIG_BT_MAX_CONN
2048 + (CONFIG_BT_MAX_CONN * 512)
// Was 12288 + (CONFIG_BT_MAX_CONN * 512), but this starved newlibc of HEAP (used in handling BLE connections/devices)
#else
9192
#endif
// Since v2.1 - MEMORY ARENA extra stack reservation - See herald/datatype/data.h
#ifdef HERALD_MEMORYARENA_MAX
+ HERALD_MEMORYARENA_MAX
#else
+ 8192
#endif
;
K_THREAD_STACK_DEFINE(herald_stack,
stackMaxSize); // Was 9192 for nRF5340 (10 conns), 2048 for nRF52832 (3 conns)

struct DummyDelegate
{
};

using MYUINT32 = unsigned long;

struct basic_venue
{
std::uint16_t country;
std::uint16_t state;
MYUINT32 code; // C++ linker may balk, confusing unsigned int with unsigned long
std::string name;
};

static struct basic_venue joesPizza = {
.country = 826,
.state = 4,
.code = 12345,
.name = "Joe's Pizza"};

static struct basic_venue adamsFishShop = {
.country = 826,
.state = 3,
.code = 22334,
.name = "Adam's Fish Shop"};

static struct basic_venue maxsFineDining = {
.country = 832,
.state = 1,
.code = 55566,
.name = "Max's Fine Dining"};

static struct basic_venue erinsStakehouse = {
.country = 826,
.state = 4,
.code = 123123,
.name = "Erin's Stakehouse"};

// TODO replace the below with sub-venue extended data, with same venue code
static struct basic_venue adamsKitchen = {
.country = 826,
.state = 4,
.code = 1234,
.name = "Adam's Kitchen"};
static struct basic_venue adamsOffice = {
.country = 826,
.state = 4,
.code = 2345,
.name = "Adam's Office"};
static struct basic_venue adamsBedroom = {
.country = 826,
.state = 4,
.code = 3456,
.name = "Adam's Bedroom"};
static struct basic_venue adamsLanding = {
.country = 826,
.state = 4,
.code = 5678,
.name = "Adam's Landing"};
static struct basic_venue adamsPond = {
.country = 826,
.state = 4,
.code = 6789,
.name = "Adam's Pond"};
static struct basic_venue adamsLounge = {
.country = 826,
.state = 4,
.code = 7890,
.name = "Adam's Lounge"};

void herald_entry()
{
APP_DBG("Herald entry");
k_sleep(K_MSEC(10000)); // pause so we have time to see Herald initialisation log messages. Don't do this in production!
APP_DBG("Herald setup begins");

using namespace herald;
using namespace herald::payload;
using namespace herald::payload::beacon;
using namespace herald::payload::extended;

// Create Herald sensor array
ZephyrContextProvider zcp;
Context ctx(zcp, zcp.getLoggingSink(), zcp.getBluetoothStateManager());
// using CT = Context<ZephyrContextProvider,ZephyrLoggingSink,BluetoothStateManager>;

// Disable receiver / scanning mode - we're just transmitting our value
BLESensorConfiguration config = ctx.getSensorConfiguration(); // copy ctor
config.scanningEnabled = false;
// config.advertisingEnabled = true; // default
ctx.setSensorConfiguration(config);

ConcreteExtendedDataV1 extendedData;
extendedData.addSection(ExtendedDataSegmentCodesV1::TextPremises, erinsStakehouse.name);

payload::beacon::ConcreteBeaconPayloadDataSupplierV1 pds(
erinsStakehouse.country,
erinsStakehouse.state,
erinsStakehouse.code,
extendedData);

herald::ble::nordic_uart::NordicUartSensorDelegate nus(ctx);

// this is unusual, but required. Really we should log activity to serial BLE or similar
DummyDelegate appDelegate;
SensorDelegateSet sensorDelegates(appDelegate, nus);

ConcreteBLESensor ble(ctx, ctx.getBluetoothStateManager(), pds, sensorDelegates);
SensorArray sa(ctx, pds, ble);

// Start array (and thus start advertising)
sa.start();

int iter = 0;
// APP_DBG("got iter!");
// k_sleep(K_SECONDS(2));
Date last;
// APP_DBG("got last!");
// k_sleep(K_SECONDS(2));
int delay = 250; // KEEP THIS SMALL!!! This is how often we check to see if anything needs to happen over a connection.

APP_DBG("Entering herald iteration loop");
k_sleep(K_SECONDS(2));
while (1)
{
k_sleep(K_MSEC(delay));
Date now;
if (iter > 40 /* && iter < 44 */)
{ // some delay to allow us to see advertising output
// You could only do first 3 iterations so we can see the older log messages without continually scrolling through log messages
APP_DBG("Calling Sensor Array iteration");
// k_sleep(K_SECONDS(2));
sa.iteration(now - last);
}

if (0 == iter % (5000 / delay))
{
APP_DBG("herald thread still running. Iteration: %d", iter);
// runner.run(Date()); // Note: You may want to do this less or more regularly depending on your requirements
APP_ERR("Memory pages free in Data Arena: %d", herald::datatype::Data::getArena().pagesFree());
}

last = now;
++iter;
}
}

void main(void)
{
k_sleep(K_SECONDS(6));
Expand Down Expand Up @@ -102,6 +286,14 @@ void main(void)
APP_DBG("Bluetooth init failed (err %d)", err);
}

// Start herald entry on a new thread in case of errors, or needing to do something on the main thread
[[maybe_unused]]
k_tid_t herald_pid = k_thread_create(&herald_thread, herald_stack, stackMaxSize,
(k_thread_entry_t)herald_entry, NULL, NULL, NULL,
-1, K_USER,
K_NO_WAIT);


uint8_t dummyMac[6] = {0,1,2,3,4,5};

// Regular debug output to show the app is still running
Expand Down
2 changes: 2 additions & 0 deletions herald/include/herald/ble/ble_concrete_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ble_database.h"
#include "ble_receiver.h"
#include "ble_device.h"
#include "ble_sensor.h"
#include "ble_transmitter.h"
#include "ble_concrete.h"
Expand All @@ -20,6 +21,7 @@
#include "ble_sensor_configuration.h"
#include "ble_coordinator.h"
#include "../datatype/bluetooth_state.h"
#include "../datatype/target_identifier.h"

#include <array>
#include <algorithm>
Expand Down

0 comments on commit 7ef1fdb

Please sign in to comment.