Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/BluetoothCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const uint8_t FROMRADIO_UUID_16[16u] = {0x02, 0x00, 0x12, 0xac, 0x42, 0x02, 0x78
0xed, 0x11, 0x93, 0x49, 0x9e, 0xe6, 0x55, 0x2c};
const uint8_t FROMNUM_UUID_16[16u] = {0x53, 0x44, 0xe3, 0x47, 0x75, 0xaa, 0x70, 0xa6,
0x66, 0x4f, 0x00, 0xa8, 0x8c, 0xa1, 0x9d, 0xed};
const uint8_t FROMRADIOSYNC_UUID_16[16u] = {0x5d, 0x16, 0x69, 0x37, 0x92, 0xc7, 0x63, 0x99,
0xdb, 0x45, 0x2d, 0x98, 0xc3, 0x50, 0x8a, 0x88};
const uint8_t LEGACY_LOGRADIO_UUID_16[16u] = {0xe2, 0xf2, 0x1e, 0xbe, 0xc5, 0x15, 0xcf, 0xaa,
0x6b, 0x43, 0xfa, 0x78, 0x38, 0xd2, 0x6f, 0x6c};
const uint8_t LOGRADIO_UUID_16[16u] = {0x47, 0x95, 0xDF, 0x8C, 0xDE, 0xE9, 0x44, 0x99,
Expand Down
4 changes: 3 additions & 1 deletion src/BluetoothCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#define TORADIO_UUID "f75c76d2-129e-4dad-a1dd-7866124401e7"
#define FROMRADIO_UUID "2c55e69e-4993-11ed-b878-0242ac120002"
#define FROMNUM_UUID "ed9da18c-a800-4f66-a670-aa7547e34453"
#define FROMRADIOSYNC_UUID "888a50c3-982d-45db-9963-c7923769165d"
#define LEGACY_LOGRADIO_UUID "6c6fd238-78fa-436b-aacf-15c5be1ef2e2"
#define LOGRADIO_UUID "5a3d6e49-06e6-4423-9944-e9de8cdf9547"

// NRF52 wants these constants as byte arrays
// Generated here https://yupana-engineering.com/online-uuid-to-c-array-converter - but in REVERSE BYTE ORDER
extern const uint8_t MESH_SERVICE_UUID_16[], TORADIO_UUID_16[16u], FROMRADIO_UUID_16[], FROMNUM_UUID_16[], LOGRADIO_UUID_16[];
extern const uint8_t MESH_SERVICE_UUID_16[], TORADIO_UUID_16[16u], FROMRADIO_UUID_16[], FROMNUM_UUID_16[],
FROMRADIOSYNC_UUID_16[], LOGRADIO_UUID_16[];

/// Given a level between 0-100, update the BLE attribute
void updateBatteryLevel(uint8_t level);
Expand Down
23 changes: 23 additions & 0 deletions src/nimble/NimbleBluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ constexpr uint16_t kPreferredBleTxTimeUs = (kPreferredBleTxOctets + 14) * 8;
#define NIMBLE_BLUETOOTH_FROM_PHONE_QUEUE_SIZE 3

NimBLECharacteristic *fromNumCharacteristic;
NimBLECharacteristic *fromRadioSyncCharacteristic;
NimBLECharacteristic *BatteryCharacteristic;
NimBLECharacteristic *logRadioCharacteristic;
NimBLEServer *bleServer;
Expand Down Expand Up @@ -313,6 +314,23 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread
{
PhoneAPI::onNowHasData(fromRadioNum);

// Check if FromRadioSync is subscribed (indications enabled)
// If so, we push a single packet via indication to keep overhead low.
if (fromRadioSyncCharacteristic->getSubscribedCount() > 0) {
uint8_t fromRadioBytes[meshtastic_FromRadio_size];
size_t numBytes = getFromRadio(fromRadioBytes);
if (numBytes > 0) {
fromRadioSyncCharacteristic->setValue(fromRadioBytes, numBytes);
#ifdef NIMBLE_TWO
fromRadioSyncCharacteristic->indicate(fromRadioBytes, numBytes, BLE_HS_CONN_HANDLE_NONE);
#else
fromRadioSyncCharacteristic->indicate();
#endif
}
// If we sent via Sync, we don't notify legacy FromNum to avoid double-processing or confusion.
return;
}

#ifdef DEBUG_NIMBLE_NOTIFY

int currentNotifyCount = notifyCount.fetch_add(1);
Expand Down Expand Up @@ -871,6 +889,8 @@ void NimbleBluetooth::setupService()
// Allow notifications so phones can stream FromRadio without polling.
FromRadioCharacteristic = bleService->createCharacteristic(FROMRADIO_UUID, NIMBLE_PROPERTY::READ);
fromNumCharacteristic = bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ);
fromRadioSyncCharacteristic =
bleService->createCharacteristic(FROMRADIOSYNC_UUID, NIMBLE_PROPERTY::INDICATE | NIMBLE_PROPERTY::READ);
logRadioCharacteristic =
bleService->createCharacteristic(LOGRADIO_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ, 512U);
} else {
Expand All @@ -881,6 +901,9 @@ void NimbleBluetooth::setupService()
fromNumCharacteristic =
bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC);
fromRadioSyncCharacteristic =
bleService->createCharacteristic(FROMRADIOSYNC_UUID, NIMBLE_PROPERTY::INDICATE | NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC);
logRadioCharacteristic = bleService->createCharacteristic(
LOGRADIO_UUID,
NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC, 512U);
Expand Down
17 changes: 16 additions & 1 deletion src/platform/nrf52/NRF52Bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <utility/bonding.h>
static BLEService meshBleService = BLEService(BLEUuid(MESH_SERVICE_UUID_16));
static BLECharacteristic fromNum = BLECharacteristic(BLEUuid(FROMNUM_UUID_16));
static BLECharacteristic fromRadioSync = BLECharacteristic(BLEUuid(FROMRADIOSYNC_UUID_16));
static BLECharacteristic fromRadio = BLECharacteristic(BLEUuid(FROMRADIO_UUID_16));
static BLECharacteristic toRadio = BLECharacteristic(BLEUuid(TORADIO_UUID_16));
static BLECharacteristic logRadio = BLECharacteristic(BLEUuid(LOGRADIO_UUID_16));
Expand Down Expand Up @@ -42,6 +43,14 @@ class BluetoothPhoneAPI : public PhoneAPI
{
PhoneAPI::onNowHasData(fromRadioNum);

if (fromRadioSync.indicateEnabled()) {
size_t len = getFromRadio(fromRadioBytes);
if (len > 0) {
fromRadioSync.indicate(fromRadioBytes, len);
}
return;
}

LOG_INFO("BLE notify fromNum");
fromNum.notify32(fromRadioNum);
}
Expand Down Expand Up @@ -97,7 +106,7 @@ void onCccd(uint16_t conn_hdl, BLECharacteristic *chr, uint16_t cccd_value)
// According to the GATT spec: cccd value = 0x0001 means notifications are enabled
// and cccd value = 0x0002 means indications are enabled

if (chr->uuid == fromNum.uuid || chr->uuid == logRadio.uuid) {
if (chr->uuid == fromNum.uuid || chr->uuid == logRadio.uuid || chr->uuid == fromRadioSync.uuid) {
auto result = cccd_value == 2 ? chr->indicateEnabled(conn_hdl) : chr->notifyEnabled(conn_hdl);
if (result) {
LOG_INFO("Notify/Indicate enabled");
Expand Down Expand Up @@ -188,6 +197,12 @@ void setupMeshService(void)
fromNum.write32(0); // Provide default fromNum of 0
fromNum.begin();

fromRadioSync.setProperties(CHR_PROPS_INDICATE | CHR_PROPS_READ);
fromRadioSync.setPermission(secMode, SECMODE_NO_ACCESS);
fromRadioSync.setMaxLen(sizeof(fromRadioBytes)); // Reuse size
fromRadioSync.setCccdWriteCallback(onCccd);
fromRadioSync.begin();

fromRadio.setProperties(CHR_PROPS_READ);
fromRadio.setPermission(secMode, SECMODE_NO_ACCESS);
fromRadio.setMaxLen(sizeof(fromRadioBytes));
Expand Down
Loading