From 2c9ce1cfc7d4c733b1b35f51a1f6f5da332cf3fa Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 19 Apr 2020 21:26:09 +0200 Subject: Encapsulate nimble code into NimbleController. Handle all GAP events. --- src/Components/Ble/NimbleController.cpp | 148 ++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/Components/Ble/NimbleController.cpp (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp new file mode 100644 index 00000000..eaad9077 --- /dev/null +++ b/src/Components/Ble/NimbleController.cpp @@ -0,0 +1,148 @@ +#include "NimbleController.h" +#include +#include +#include +#include +#include +#include + + +using namespace Pinetime::Controllers; + +int GAPEventCallback(struct ble_gap_event *event, void *arg) { + auto nimbleController = static_cast(arg); + return nimbleController->OnGAPEvent(event); +} + +void NimbleController::Init() { + while (!ble_hs_synced()) {} + + ble_svc_gap_init(); + ble_svc_gatt_init(); + + int res; + res = ble_hs_util_ensure_addr(0); + res = ble_hs_id_infer_auto(0, &addrType); + res = ble_svc_gap_device_name_set(deviceName); +} + +void NimbleController::StartAdvertising() { + ble_svc_gap_device_name_set("Pinetime-JF"); + + /* set adv parameters */ + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + /* advertising payload is split into advertising data and advertising + response, because all data cannot fit into single packet; name of device + is sent as response to scan request */ + struct ble_hs_adv_fields rsp_fields; + + /* fill all fields and parameters with zeros */ + memset(&adv_params, 0, sizeof(adv_params)); + memset(&fields, 0, sizeof(fields)); + memset(&rsp_fields, 0, sizeof(rsp_fields)); + + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + + fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; +// fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE( +// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, +// 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff)); + fields.num_uuids128 = 0; + fields.uuids128_is_complete = 0;; + fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + + rsp_fields.name = (uint8_t *)"Pinetime-JF"; + rsp_fields.name_len = strlen("Pinetime-JF"); + rsp_fields.name_is_complete = 1; + + int res; + res = ble_gap_adv_set_fields(&fields); + assert(res == 0); + + res = ble_gap_adv_rsp_set_fields(&rsp_fields); + + res = ble_gap_adv_start(addrType, NULL, 10000, + &adv_params, GAPEventCallback, this); + + +} + +int NimbleController::OnGAPEvent(ble_gap_event *event) { + switch (event->type) { + case BLE_GAP_EVENT_ADV_COMPLETE: + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE"); + NRF_LOG_INFO("advertise complete; reason=%d", event->adv_complete.reason); + StartAdvertising(); + break; + case BLE_GAP_EVENT_CONNECT: { + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONNECT"); + + /* A new connection was established or a connection attempt failed. */ + NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", + event->connect.status); + + if (event->connect.status != 0) { + /* Connection failed; resume advertising. */ + StartAdvertising(); + } + } + break; + case BLE_GAP_EVENT_DISCONNECT: + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT"); + NRF_LOG_INFO("disconnect; reason=%d ", event->disconnect.reason); + + /* Connection terminated; resume advertising. */ + StartAdvertising(); + break; + case BLE_GAP_EVENT_CONN_UPDATE: + NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_CONN_UPDATE"); + /* The central has updated the connection parameters. */ + NRF_LOG_INFO("connection updated; status=%d ", event->conn_update.status); + break; + case BLE_GAP_EVENT_ENC_CHANGE: + /* Encryption has been enabled or disabled for this connection. */ + NRF_LOG_INFO("encryption change event; status=%d ", event->enc_change.status); + return 0; + case BLE_GAP_EVENT_SUBSCRIBE: + NRF_LOG_INFO("subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=???\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate); + return 0; + case BLE_GAP_EVENT_MTU: + NRF_LOG_INFO("mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + return 0; + + case BLE_GAP_EVENT_REPEAT_PAIRING: { + /* We already have a bond with the peer, but it is attempting to + * establish a new secure link. This app sacrifices security for + * convenience: just throw away the old bond and accept the new link. + */ + + /* Delete the old bond. */ + struct ble_gap_conn_desc desc; + ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc); + ble_store_util_delete_peer(&desc.peer_id_addr); + + /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should + * continue with the pairing operation. + */ + } + return BLE_GAP_REPEAT_PAIRING_RETRY; + default: + NRF_LOG_INFO("Advertising event : %d", event->type); + break; + } + return 0; +} + -- cgit v1.2.3 From a9254ee90e835b6a187d1fcd6f8850decca5bc89 Mon Sep 17 00:00:00 2001 From: JF Date: Wed, 22 Apr 2020 20:19:36 +0200 Subject: NimbleController : support CTS --- src/Components/Ble/NimbleController.cpp | 79 ++++++++++++++++++++++++++++++++- src/Components/Ble/NimbleController.h | 29 +++++++++++- src/SystemTask/SystemTask.cpp | 2 +- 3 files changed, 107 insertions(+), 3 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index eaad9077..f3add36d 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -1,3 +1,5 @@ +#include + #include "NimbleController.h" #include #include @@ -5,15 +7,41 @@ #include #include #include - +#include using namespace Pinetime::Controllers; +NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} { + ctsUuid.u.type = BLE_UUID_TYPE_16; + ctsUuid.value = BleGatServiceCts; + + ctsCurrentTimeUuid.u.type = BLE_UUID_TYPE_16; + ctsCurrentTimeUuid.value = bleGattCharacteristicCurrentTime; +} + int GAPEventCallback(struct ble_gap_event *event, void *arg) { auto nimbleController = static_cast(arg); return nimbleController->OnGAPEvent(event); } +int DiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg) { + auto nimbleController = static_cast(arg); + return nimbleController->OnDiscoveryEvent(conn_handle, error, service); +} + +int CharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto nimbleController = static_cast(arg); + return nimbleController->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); +} + +static int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg) { + auto nimbleController = static_cast(arg); + return nimbleController->OnCurrentTimeReadResult(conn_handle, error, attr); +} + void NimbleController::Init() { while (!ble_hs_synced()) {} @@ -87,6 +115,10 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { if (event->connect.status != 0) { /* Connection failed; resume advertising. */ StartAdvertising(); + } else { + connectionHandle = event->connect.conn_handle; + + ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsUuid), DiscoveryEventCallback, this); } } break; @@ -146,3 +178,48 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { return 0; } +int NimbleController::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) + NRF_LOG_INFO("Discovery complete"); + + if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsUuid), &service->uuid.u) == 0) { + NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); + ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)&ctsCurrentTimeUuid), CharacteristicDiscoveredCallback, this); + } + + return 0; +} + +int NimbleController::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) + NRF_LOG_INFO("Characteristic discovery complete"); + + if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsCurrentTimeUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); + + ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this); + } + return 0; +} + +int NimbleController::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) { + if(error->status == 0) { + // TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent + CtsData result; + os_mbuf_copydata(attribute->om, 0, sizeof(CtsData), &result); + NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, + result.month, result.dayofmonth, + result.hour, result.minute, result.second); + dateTimeController.SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + } else { + NRF_LOG_INFO("Error retrieving current time: %d", error->status); + } + return 0; +} + + + + + diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 1901b14e..f44f26d4 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -5,15 +5,42 @@ namespace Pinetime { namespace Controllers { - + class DateTime; class NimbleController { public: + NimbleController(DateTime& dateTimeController); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event *event); + int OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); + int + OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); private: static constexpr char* deviceName = "Pinetime-JF"; + static constexpr uint16_t BleGatServiceCts = 0x1805; + + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; + + DateTime& dateTimeController; + + ble_uuid16_t ctsUuid; + + static constexpr uint16_t bleGattCharacteristicCurrentTime = 0x2a2b; + ble_uuid16_t ctsCurrentTimeUuid; + uint8_t addrType; + uint16_t connectionHandle; }; } } diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index ae031a1e..ab30d5aa 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -22,7 +22,7 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::C Pinetime::Controllers::NotificationManager& notificationManager) : spi{spi}, lcd{lcd}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, - watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager} { + watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, nimbleController({dateTimeController}) { systemTaksMsgQueue = xQueueCreate(10, 1); } -- cgit v1.2.3 From 14d6954466b44c4eee72c68ee91ab1cf7f45dab3 Mon Sep 17 00:00:00 2001 From: JF Date: Wed, 22 Apr 2020 20:53:43 +0200 Subject: NimbleController : Implement device info service --- src/Components/Ble/NimbleController.cpp | 127 +++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 2 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index f3add36d..d9d4bbdb 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -9,14 +9,134 @@ #include #include + using namespace Pinetime::Controllers; +// TODO c++ify the following code +// - device info should be in its own class +// - cts should be in it own class + +#define BLE_GATT_SVC_DEVINFO (0x180a) /**< device info */ +#define BLE_GATT_CHAR_MANUFACTURER_NAME (0x2a29) /**< manufacturer name */ +#define BLE_GATT_CHAR_MODEL_NUMBER_STR (0x2a24) /**< model number */ +#define BLE_GATT_CHAR_SERIAL_NUMBER_STR (0x2a25) /**< serial number */ +#define BLE_GATT_CHAR_FW_REV_STR (0x2a26) /**< firmware revision */ +#define BLE_GATT_CHAR_HW_REV_STR (0x2a27) /**< hardware revision */ + +static int _devinfo_handler(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) { + const char *str; + + switch (ble_uuid_u16(ctxt->chr->uuid)) { + case BLE_GATT_CHAR_MANUFACTURER_NAME: + str = "Codingfield"; + break; + case BLE_GATT_CHAR_MODEL_NUMBER_STR: + str = "1"; + break; + case BLE_GATT_CHAR_SERIAL_NUMBER_STR: + str = "1.2.3.4.5.6"; + break; + case BLE_GATT_CHAR_FW_REV_STR: + str = "0.5.0"; + break; + case BLE_GATT_CHAR_HW_REV_STR: + str = "1.0"; + break; + default: + return BLE_ATT_ERR_UNLIKELY; + } + + int res = os_mbuf_append(ctxt->om, str, strlen(str)); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + +ble_uuid16_t deviceInfoUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = BLE_GATT_SVC_DEVINFO +}; + +ble_uuid16_t manufacturerNameUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = BLE_GATT_CHAR_MANUFACTURER_NAME +}; + +ble_uuid16_t modelNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = BLE_GATT_CHAR_MODEL_NUMBER_STR +}; + +ble_uuid16_t serialNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = BLE_GATT_CHAR_SERIAL_NUMBER_STR +}; + +ble_uuid16_t fwRevisionUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = BLE_GATT_CHAR_FW_REV_STR +}; + +ble_uuid16_t hwRevisionUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = BLE_GATT_CHAR_HW_REV_STR +}; + + +static const struct ble_gatt_svc_def gatt_svr_svcs[] = { + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*)&deviceInfoUuid, + .characteristics = (struct ble_gatt_chr_def[]) { + { + .uuid = (ble_uuid_t*)&manufacturerNameUuid, + .access_cb = _devinfo_handler, + .arg = nullptr, + .flags = BLE_GATT_CHR_F_READ, + + }, + { + .uuid = (ble_uuid_t*)&modelNumberUuid, + .access_cb = _devinfo_handler, + .arg = nullptr, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*)&serialNumberUuid, + .access_cb = _devinfo_handler, + .arg = nullptr, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*)&fwRevisionUuid, + .access_cb = _devinfo_handler, + .arg = nullptr, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t*)&hwRevisionUuid, + .access_cb = _devinfo_handler, + .arg = nullptr, + .flags = BLE_GATT_CHR_F_READ, + }, + { + 0, /* no more characteristics in this service */ + }, + } + }, + { + 0, /* no more services */ + }, +}; + + NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} { ctsUuid.u.type = BLE_UUID_TYPE_16; ctsUuid.value = BleGatServiceCts; ctsCurrentTimeUuid.u.type = BLE_UUID_TYPE_16; ctsCurrentTimeUuid.value = bleGattCharacteristicCurrentTime; + } int GAPEventCallback(struct ble_gap_event *event, void *arg) { @@ -48,10 +168,13 @@ void NimbleController::Init() { ble_svc_gap_init(); ble_svc_gatt_init(); + ble_gatts_count_cfg(gatt_svr_svcs); + ble_gatts_add_svcs(gatt_svr_svcs); int res; res = ble_hs_util_ensure_addr(0); res = ble_hs_id_infer_auto(0, &addrType); res = ble_svc_gap_device_name_set(deviceName); + ble_gatts_start(); } void NimbleController::StartAdvertising() { @@ -90,9 +213,9 @@ void NimbleController::StartAdvertising() { res = ble_gap_adv_set_fields(&fields); assert(res == 0); - res = ble_gap_adv_rsp_set_fields(&rsp_fields); + ble_gap_adv_rsp_set_fields(&rsp_fields); - res = ble_gap_adv_start(addrType, NULL, 10000, + ble_gap_adv_start(addrType, NULL, 10000, &adv_params, GAPEventCallback, this); -- cgit v1.2.3 From 24a7b6e39758386f9f33df266a9419dd3408f862 Mon Sep 17 00:00:00 2001 From: JF Date: Thu, 23 Apr 2020 20:34:38 +0200 Subject: NimbleController : Encapsulate device info service in its own class. --- src/CMakeLists.txt | 2 + src/Components/Ble/BleController.h | 2 +- src/Components/Ble/DeviceInformationService.cpp | 101 ++++++++++++++++++++ src/Components/Ble/DeviceInformationService.h | 67 +++++++++++++ src/Components/Ble/NimbleController.cpp | 119 +----------------------- src/Components/Ble/NimbleController.h | 3 + 6 files changed, 175 insertions(+), 119 deletions(-) create mode 100644 src/Components/Ble/DeviceInformationService.cpp create mode 100644 src/Components/Ble/DeviceInformationService.h (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f16ee0c0..653151dc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -317,6 +317,7 @@ list(APPEND SOURCE_FILES Components/DateTime/DateTimeController.cpp Components/Brightness/BrightnessController.cpp Components/Ble/NimbleController.cpp + Components/Ble/DeviceInformationService.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c @@ -362,6 +363,7 @@ set(INCLUDE_FILES Components/DateTime/DateTimeController.h Components/Brightness/BrightnessController.h Components/Ble/NimbleController.h + Components/Ble/DeviceInformationService.h drivers/Cst816s.h FreeRTOS/portmacro.h FreeRTOS/portmacro_cmsis.h diff --git a/src/Components/Ble/BleController.h b/src/Components/Ble/BleController.h index 31d66986..f2bd77e0 100644 --- a/src/Components/Ble/BleController.h +++ b/src/Components/Ble/BleController.h @@ -1,6 +1,6 @@ #pragma once -#include > +#include #include namespace Pinetime { diff --git a/src/Components/Ble/DeviceInformationService.cpp b/src/Components/Ble/DeviceInformationService.cpp new file mode 100644 index 00000000..3099b1bf --- /dev/null +++ b/src/Components/Ble/DeviceInformationService.cpp @@ -0,0 +1,101 @@ +#include "DeviceInformationService.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t DeviceInformationService::manufacturerNameUuid; +constexpr ble_uuid16_t DeviceInformationService::modelNumberUuid; +constexpr ble_uuid16_t DeviceInformationService::serialNumberUuid; +constexpr ble_uuid16_t DeviceInformationService::fwRevisionUuid; +constexpr ble_uuid16_t DeviceInformationService::deviceInfoUuid; +constexpr ble_uuid16_t DeviceInformationService::hwRevisionUuid; + +int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto deviceInformationService = static_cast(arg); + return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt); +} + +void DeviceInformationService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + const char *str; + + switch (ble_uuid_u16(ctxt->chr->uuid)) { + case manufacturerNameId: + str = manufacturerName; + break; + case modelNumberId: + str = modelNumber; + break; + case serialNumberId: + str = serialNumber; + break; + case fwRevisionId: + str = fwRevision; + break; + case hwRevisionId: + str = hwRevision; + break; + default: + return BLE_ATT_ERR_UNLIKELY; + } + + int res = os_mbuf_append(ctxt->om, str, strlen(str)); + return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + +DeviceInformationService::DeviceInformationService() : + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &manufacturerNameUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &modelNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &serialNumberUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &fwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + .uuid = (ble_uuid_t *) &hwRevisionUuid, + .access_cb = DeviceInformationCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_READ, + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &deviceInfoUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + diff --git a/src/Components/Ble/DeviceInformationService.h b/src/Components/Ble/DeviceInformationService.h new file mode 100644 index 00000000..6249893d --- /dev/null +++ b/src/Components/Ble/DeviceInformationService.h @@ -0,0 +1,67 @@ +#pragma once +#include +#include + +#include + +namespace Pinetime { + namespace Controllers { + class DeviceInformationService { + public: + DeviceInformationService(); + void Init(); + + int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + private: + static constexpr uint16_t deviceInfoId {0x180a}; + static constexpr uint16_t manufacturerNameId {0x2a29}; + static constexpr uint16_t modelNumberId {0x2a24}; + static constexpr uint16_t serialNumberId {0x2a25}; + static constexpr uint16_t fwRevisionId {0x2a26}; + static constexpr uint16_t hwRevisionId {0x2a27}; + + static constexpr char* manufacturerName = "Codingfield"; + static constexpr char* modelNumber = "1"; + static constexpr char* serialNumber = "9.8.7.6.5.4"; + static constexpr char* fwRevision = "0.5.0"; + static constexpr char* hwRevision = "1.0.0"; + + static constexpr ble_uuid16_t deviceInfoUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = deviceInfoId + }; + + static constexpr ble_uuid16_t manufacturerNameUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = manufacturerNameId + }; + + static constexpr ble_uuid16_t modelNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = modelNumberId + }; + + static constexpr ble_uuid16_t serialNumberUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = serialNumberId + }; + + static constexpr ble_uuid16_t fwRevisionUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = fwRevisionId + }; + + static constexpr ble_uuid16_t hwRevisionUuid { + .u {.type = BLE_UUID_TYPE_16}, + .value = hwRevisionId + }; + + struct ble_gatt_chr_def characteristicDefinition[6]; + struct ble_gatt_svc_def serviceDefinition[2]; + + + }; + } +} \ No newline at end of file diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index d9d4bbdb..acee6651 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -9,127 +9,11 @@ #include #include - using namespace Pinetime::Controllers; // TODO c++ify the following code -// - device info should be in its own class // - cts should be in it own class -#define BLE_GATT_SVC_DEVINFO (0x180a) /**< device info */ -#define BLE_GATT_CHAR_MANUFACTURER_NAME (0x2a29) /**< manufacturer name */ -#define BLE_GATT_CHAR_MODEL_NUMBER_STR (0x2a24) /**< model number */ -#define BLE_GATT_CHAR_SERIAL_NUMBER_STR (0x2a25) /**< serial number */ -#define BLE_GATT_CHAR_FW_REV_STR (0x2a26) /**< firmware revision */ -#define BLE_GATT_CHAR_HW_REV_STR (0x2a27) /**< hardware revision */ - -static int _devinfo_handler(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt, void *arg) { - const char *str; - - switch (ble_uuid_u16(ctxt->chr->uuid)) { - case BLE_GATT_CHAR_MANUFACTURER_NAME: - str = "Codingfield"; - break; - case BLE_GATT_CHAR_MODEL_NUMBER_STR: - str = "1"; - break; - case BLE_GATT_CHAR_SERIAL_NUMBER_STR: - str = "1.2.3.4.5.6"; - break; - case BLE_GATT_CHAR_FW_REV_STR: - str = "0.5.0"; - break; - case BLE_GATT_CHAR_HW_REV_STR: - str = "1.0"; - break; - default: - return BLE_ATT_ERR_UNLIKELY; - } - - int res = os_mbuf_append(ctxt->om, str, strlen(str)); - return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; -} - -ble_uuid16_t deviceInfoUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = BLE_GATT_SVC_DEVINFO -}; - -ble_uuid16_t manufacturerNameUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = BLE_GATT_CHAR_MANUFACTURER_NAME -}; - -ble_uuid16_t modelNumberUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = BLE_GATT_CHAR_MODEL_NUMBER_STR -}; - -ble_uuid16_t serialNumberUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = BLE_GATT_CHAR_SERIAL_NUMBER_STR -}; - -ble_uuid16_t fwRevisionUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = BLE_GATT_CHAR_FW_REV_STR -}; - -ble_uuid16_t hwRevisionUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = BLE_GATT_CHAR_HW_REV_STR -}; - - -static const struct ble_gatt_svc_def gatt_svr_svcs[] = { - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t*)&deviceInfoUuid, - .characteristics = (struct ble_gatt_chr_def[]) { - { - .uuid = (ble_uuid_t*)&manufacturerNameUuid, - .access_cb = _devinfo_handler, - .arg = nullptr, - .flags = BLE_GATT_CHR_F_READ, - - }, - { - .uuid = (ble_uuid_t*)&modelNumberUuid, - .access_cb = _devinfo_handler, - .arg = nullptr, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t*)&serialNumberUuid, - .access_cb = _devinfo_handler, - .arg = nullptr, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t*)&fwRevisionUuid, - .access_cb = _devinfo_handler, - .arg = nullptr, - .flags = BLE_GATT_CHR_F_READ, - }, - { - .uuid = (ble_uuid_t*)&hwRevisionUuid, - .access_cb = _devinfo_handler, - .arg = nullptr, - .flags = BLE_GATT_CHR_F_READ, - }, - { - 0, /* no more characteristics in this service */ - }, - } - }, - { - 0, /* no more services */ - }, -}; - - NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} { ctsUuid.u.type = BLE_UUID_TYPE_16; ctsUuid.value = BleGatServiceCts; @@ -168,8 +52,7 @@ void NimbleController::Init() { ble_svc_gap_init(); ble_svc_gatt_init(); - ble_gatts_count_cfg(gatt_svr_svcs); - ble_gatts_add_svcs(gatt_svr_svcs); + deviceInformationService.Init(); int res; res = ble_hs_util_ensure_addr(0); res = ble_hs_id_infer_auto(0, &addrType); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index f44f26d4..ce51527e 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -1,6 +1,8 @@ #pragma once #include + +#include "DeviceInformationService.h" #include namespace Pinetime { @@ -33,6 +35,7 @@ namespace Pinetime { } CtsData; DateTime& dateTimeController; + DeviceInformationService deviceInformationService; ble_uuid16_t ctsUuid; -- cgit v1.2.3 From 89ccdd00032dbd6f97ce5cff57f588b6bd88ef2a Mon Sep 17 00:00:00 2001 From: JF Date: Thu, 23 Apr 2020 20:57:53 +0200 Subject: NimbleController : Encapsulate CTS client in its own class. --- src/CMakeLists.txt | 2 + src/Components/Ble/CurrentTimeClient.cpp | 78 ++++++++++++++++++++++++++++++++ src/Components/Ble/CurrentTimeClient.h | 54 ++++++++++++++++++++++ src/Components/Ble/NimbleController.cpp | 72 ++--------------------------- src/Components/Ble/NimbleController.h | 27 ++--------- 5 files changed, 141 insertions(+), 92 deletions(-) create mode 100644 src/Components/Ble/CurrentTimeClient.cpp create mode 100644 src/Components/Ble/CurrentTimeClient.h (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 653151dc..fc22c9a9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -318,6 +318,7 @@ list(APPEND SOURCE_FILES Components/Brightness/BrightnessController.cpp Components/Ble/NimbleController.cpp Components/Ble/DeviceInformationService.cpp + Components/Ble/CurrentTimeClient.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c @@ -364,6 +365,7 @@ set(INCLUDE_FILES Components/Brightness/BrightnessController.h Components/Ble/NimbleController.h Components/Ble/DeviceInformationService.h + Components/Ble/CurrentTimeClient.h drivers/Cst816s.h FreeRTOS/portmacro.h FreeRTOS/portmacro_cmsis.h diff --git a/src/Components/Ble/CurrentTimeClient.cpp b/src/Components/Ble/CurrentTimeClient.cpp new file mode 100644 index 00000000..524c37d5 --- /dev/null +++ b/src/Components/Ble/CurrentTimeClient.cpp @@ -0,0 +1,78 @@ +#include +#include "CurrentTimeClient.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid; +constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid; + +int Pinetime::Controllers::CurrentTimeDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg) { + auto client = static_cast(arg); + return client->OnDiscoveryEvent(conn_handle, error, service); +} + +int Pinetime::Controllers::CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto client = static_cast(arg); + return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); +} + +int Pinetime::Controllers::CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg) { + auto client = static_cast(arg); + return client->OnCurrentTimeReadResult(conn_handle, error, attr); +} + + +CurrentTimeClient::CurrentTimeClient(DateTime& dateTimeController) : dateTimeController{dateTimeController} { + +} + +void CurrentTimeClient::Init() { + +} + +void CurrentTimeClient::StartDiscovery(uint16_t connectionHandle) { + ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsServiceUuid), CurrentTimeDiscoveryEventCallback, this); +} + +int CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) + NRF_LOG_INFO("Discovery complete"); + + if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsServiceUuid), &service->uuid.u) == 0) { + NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); + ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)¤tTimeCharacteristicUuid), CurrentTimeCharacteristicDiscoveredCallback, this); + } + return 0; +} + +int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) + NRF_LOG_INFO("Characteristic discovery complete"); + + if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); + + ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this); + } + return 0; +} + +int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) { + if(error->status == 0) { + // TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent + CtsData result; + os_mbuf_copydata(attribute->om, 0, sizeof(CtsData), &result); + NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, + result.month, result.dayofmonth, + result.hour, result.minute, result.second); + dateTimeController.SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + } else { + NRF_LOG_INFO("Error retrieving current time: %d", error->status); + } + return 0; +} diff --git a/src/Components/Ble/CurrentTimeClient.h b/src/Components/Ble/CurrentTimeClient.h new file mode 100644 index 00000000..94892cef --- /dev/null +++ b/src/Components/Ble/CurrentTimeClient.h @@ -0,0 +1,54 @@ +#pragma once +#include +#include +#include +#include + +namespace Pinetime { + namespace Controllers { + int CurrentTimeDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg); + int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg); + int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg); + + class CurrentTimeClient { + public: + explicit CurrentTimeClient(DateTime& dateTimeController); + void Init(); + int OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); + int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); + + + void StartDiscovery(uint16_t connectionHandle); + private: + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; + + static constexpr uint16_t ctsServiceId {0x1805}; + static constexpr uint16_t currentTimeCharacteristicId {0x2a2b}; + + static constexpr ble_uuid16_t ctsServiceUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ctsServiceId + }; + static constexpr ble_uuid16_t currentTimeCharacteristicUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = currentTimeCharacteristicId + }; + + DateTime& dateTimeController; + }; + } +} \ No newline at end of file diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index acee6651..cedc9f60 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -14,12 +14,8 @@ using namespace Pinetime::Controllers; // TODO c++ify the following code // - cts should be in it own class -NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} { - ctsUuid.u.type = BLE_UUID_TYPE_16; - ctsUuid.value = BleGatServiceCts; - - ctsCurrentTimeUuid.u.type = BLE_UUID_TYPE_16; - ctsCurrentTimeUuid.value = bleGattCharacteristicCurrentTime; +NimbleController::NimbleController(DateTime &datetimeController) : dateTimeController{datetimeController}, + currentTimeClient{datetimeController} { } @@ -28,24 +24,6 @@ int GAPEventCallback(struct ble_gap_event *event, void *arg) { return nimbleController->OnGAPEvent(event); } -int DiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg) { - auto nimbleController = static_cast(arg); - return nimbleController->OnDiscoveryEvent(conn_handle, error, service); -} - -int CharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto nimbleController = static_cast(arg); - return nimbleController->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); -} - -static int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, void *arg) { - auto nimbleController = static_cast(arg); - return nimbleController->OnCurrentTimeReadResult(conn_handle, error, attr); -} - void NimbleController::Init() { while (!ble_hs_synced()) {} @@ -53,6 +31,7 @@ void NimbleController::Init() { ble_svc_gatt_init(); deviceInformationService.Init(); + currentTimeClient.Init(); int res; res = ble_hs_util_ensure_addr(0); res = ble_hs_id_infer_auto(0, &addrType); @@ -123,8 +102,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { StartAdvertising(); } else { connectionHandle = event->connect.conn_handle; - - ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsUuid), DiscoveryEventCallback, this); + currentTimeClient.StartDiscovery(connectionHandle); } } break; @@ -184,48 +162,6 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { return 0; } -int NimbleController::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { - if(service == nullptr && error->status == BLE_HS_EDONE) - NRF_LOG_INFO("Discovery complete"); - - if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsUuid), &service->uuid.u) == 0) { - NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); - ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)&ctsCurrentTimeUuid), CharacteristicDiscoveredCallback, this); - } - - return 0; -} - -int NimbleController::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic) { - if(characteristic == nullptr && error->status == BLE_HS_EDONE) - NRF_LOG_INFO("Characteristic discovery complete"); - - if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsCurrentTimeUuid), &characteristic->uuid.u) == 0) { - NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); - - ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this); - } - return 0; -} - -int NimbleController::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) { - if(error->status == 0) { - // TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent - CtsData result; - os_mbuf_copydata(attribute->om, 0, sizeof(CtsData), &result); - NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, - result.month, result.dayofmonth, - result.hour, result.minute, result.second); - dateTimeController.SetTime(result.year, result.month, result.dayofmonth, - 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); - } else { - NRF_LOG_INFO("Error retrieving current time: %d", error->status); - } - return 0; -} - - diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index ce51527e..2a396949 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -3,6 +3,7 @@ #include #include "DeviceInformationService.h" +#include "CurrentTimeClient.h" #include namespace Pinetime { @@ -14,34 +15,12 @@ namespace Pinetime { void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event *event); - int OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); - int - OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic); - int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); + private: static constexpr char* deviceName = "Pinetime-JF"; - static constexpr uint16_t BleGatServiceCts = 0x1805; - - typedef struct __attribute__((packed)) { - uint16_t year; - uint8_t month; - uint8_t dayofmonth; - uint8_t hour; - uint8_t minute; - uint8_t second; - uint8_t millis; - uint8_t reason; - } CtsData; - DateTime& dateTimeController; DeviceInformationService deviceInformationService; - - ble_uuid16_t ctsUuid; - - static constexpr uint16_t bleGattCharacteristicCurrentTime = 0x2a2b; - ble_uuid16_t ctsCurrentTimeUuid; - + CurrentTimeClient currentTimeClient; uint8_t addrType; uint16_t connectionHandle; }; -- cgit v1.2.3 From 5fcb90a14951ec70a8ec41a656f6f58358b9986b Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 25 Apr 2020 13:09:47 +0200 Subject: NimbleController : CTS & ANS working but not at the same time (conflict during discovery) --- src/CMakeLists.txt | 2 + src/Components/Ble/AlertNotificationClient.cpp | 149 +++++++++++++++++++++++++ src/Components/Ble/AlertNotificationClient.h | 88 +++++++++++++++ src/Components/Ble/CurrentTimeClient.cpp | 14 ++- src/Components/Ble/CurrentTimeClient.h | 2 +- src/Components/Ble/NimbleController.cpp | 54 ++++++++- src/Components/Ble/NimbleController.h | 9 +- src/SystemTask/SystemTask.cpp | 2 +- 8 files changed, 306 insertions(+), 14 deletions(-) create mode 100644 src/Components/Ble/AlertNotificationClient.cpp create mode 100644 src/Components/Ble/AlertNotificationClient.h (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fc22c9a9..93878e85 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -319,6 +319,7 @@ list(APPEND SOURCE_FILES Components/Ble/NimbleController.cpp Components/Ble/DeviceInformationService.cpp Components/Ble/CurrentTimeClient.cpp + Components/Ble/AlertNotificationClient.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c @@ -366,6 +367,7 @@ set(INCLUDE_FILES Components/Ble/NimbleController.h Components/Ble/DeviceInformationService.h Components/Ble/CurrentTimeClient.h + Components/Ble/AlertNotificationClient.h drivers/Cst816s.h FreeRTOS/portmacro.h FreeRTOS/portmacro_cmsis.h diff --git a/src/Components/Ble/AlertNotificationClient.cpp b/src/Components/Ble/AlertNotificationClient.cpp new file mode 100644 index 00000000..e6683e99 --- /dev/null +++ b/src/Components/Ble/AlertNotificationClient.cpp @@ -0,0 +1,149 @@ +#include +#include "NotificationManager.h" + +#include "AlertNotificationClient.h" + + +using namespace Pinetime::Controllers; +constexpr ble_uuid16_t AlertNotificationClient::ansServiceUuid; + +constexpr ble_uuid16_t AlertNotificationClient::supportedNewAlertCategoryUuid; +constexpr ble_uuid16_t AlertNotificationClient::supportedUnreadAlertCategoryUuid ; +constexpr ble_uuid16_t AlertNotificationClient::newAlertUuid; +constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid; +constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid; + +int Pinetime::Controllers::AlertNotificationDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg) { + auto client = static_cast(arg); + return client->OnDiscoveryEvent(conn_handle, error, service); +} + +int Pinetime::Controllers::AlertNotificationCharacteristicsDiscoveryEventCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto client = static_cast(arg); + return client->OnCharacteristicsDiscoveryEvent(conn_handle, error, chr); +} + +int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg) { + auto client = static_cast(arg); + return client->OnNewAlertSubcribe(conn_handle, error, attr); +} + +int Pinetime::Controllers::AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t chr_val_handle, + const struct ble_gatt_dsc *dsc, + void *arg) { + NRF_LOG_INFO("ANS VCS"); + auto client = static_cast(arg); + return client->OnDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); +} + +AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NotificationManager& notificationManager) : + systemTask{systemTask}, notificationManager{notificationManager}{ + +} + +void AlertNotificationClient::StartDiscovery(uint16_t connectionHandle) { + ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ansServiceUuid), AlertNotificationDiscoveryEventCallback, this); +} + +bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("ANS Discovery complete"); + ble_gattc_disc_all_dscs(connectionHandle, newAlertHandle, ansEndHandle, AlertNotificationDescriptorDiscoveryEventCallback, this); + return true; + } + + if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ansServiceUuid), &service->uuid.u) == 0) { + NRF_LOG_INFO("ANS discovered : 0x%x", service->start_handle); + ble_gattc_disc_all_chrs(connectionHandle, service->start_handle, service->end_handle, AlertNotificationCharacteristicsDiscoveryEventCallback, this); + ansEndHandle = service->end_handle; + } + return false; +} + +void AlertNotificationClient::Init() { + +} + +int AlertNotificationClient::OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(error->status != 0 && error->status != BLE_HS_EDONE) { + NRF_LOG_INFO("ANS Characteristic discovery ERROR"); + return 0; + } + + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("ANS Characteristic discovery complete"); + } else { + if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&supportedNewAlertCategoryUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : supportedNewAlertCategoryUuid"); + supportedNewAlertCategoryHandle = characteristic->val_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&supportedUnreadAlertCategoryUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : supportedUnreadAlertCategoryUuid"); + supportedUnreadAlertCategoryHandle = characteristic->val_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&newAlertUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : newAlertUuid"); + newAlertHandle = characteristic->val_handle; + newAlertDefHandle = characteristic->def_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&unreadAlertStatusUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : unreadAlertStatusUuid"); + unreadAlertStatusHandle = characteristic->val_handle; + } else if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&controlPointUuid), &characteristic->uuid.u) == 0) { + NRF_LOG_INFO("ANS Characteristic discovered : controlPointUuid"); + controlPointHandle = characteristic->val_handle; + }else + NRF_LOG_INFO("ANS Characteristic discovered : 0x%x", characteristic->val_handle); + } + return 0; +} + +int AlertNotificationClient::OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error, + ble_gatt_attr *attribute) { + if(error->status == 0) { + NRF_LOG_INFO("ANS New alert subscribe OK"); + } else { + NRF_LOG_INFO("ANS New alert subscribe ERROR"); + } + + return 0; +} + +int AlertNotificationClient::OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, + const ble_gatt_dsc *descriptor) { + if(error->status == 0) { + if(characteristicValueHandle == newAlertHandle && ble_uuid_cmp(((ble_uuid_t*)&newAlertUuid), &descriptor->uuid.u)) { + if(newAlertDescriptorHandle == 0) { + NRF_LOG_INFO("ANS Descriptor discovered : %d", descriptor->handle); + newAlertDescriptorHandle = descriptor->handle; + uint8_t value[2]; + value[0] = 1; + value[1] = 0; + ble_gattc_write_flat(connectionHandle, newAlertDescriptorHandle, value, sizeof(value), NewAlertSubcribeCallback, this); + } + } + } + return 0; +} + +void AlertNotificationClient::OnNotification(ble_gap_event *event) { + if(event->notify_rx.attr_handle == newAlertHandle) { + size_t notifSize = OS_MBUF_PKTLEN(event->notify_rx.om); + uint8_t data[notifSize + 1]; + data[notifSize] = '\0'; + os_mbuf_copydata(event->notify_rx.om, 0, notifSize, data); + char *s = (char *) &data[2]; + NRF_LOG_INFO("DATA : %s", s); + + notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, s, notifSize + 1); + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); + } +} diff --git a/src/Components/Ble/AlertNotificationClient.h b/src/Components/Ble/AlertNotificationClient.h new file mode 100644 index 00000000..595dbe37 --- /dev/null +++ b/src/Components/Ble/AlertNotificationClient.h @@ -0,0 +1,88 @@ +#pragma once +#include +#include +#include + + +namespace Pinetime { + namespace Controllers { + int AlertNotificationDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, void *arg); + int AlertNotificationCharacteristicsDiscoveryEventCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg); + int NewAlertSubcribeCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg); + + int AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t chr_val_handle, + const struct ble_gatt_dsc *dsc, + void *arg); + + class AlertNotificationClient { + public: + explicit AlertNotificationClient(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); + void Init(); + + + void StartDiscovery(uint16_t connectionHandle); + bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); + int OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnNewAlertSubcribe(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute); + int OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); + void OnNotification(ble_gap_event *event); + private: + static constexpr uint16_t ansServiceId {0x1811}; + static constexpr uint16_t supportedNewAlertCategoryId = 0x2a47; + static constexpr uint16_t supportedUnreadAlertCategoryId = 0x2a48; + static constexpr uint16_t newAlertId = 0x2a46; + static constexpr uint16_t unreadAlertStatusId = 0x2a45; + static constexpr uint16_t controlPointId = 0x2a44; + + static constexpr ble_uuid16_t ansServiceUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ansServiceId + }; + static constexpr ble_uuid16_t supportedNewAlertCategoryUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = supportedNewAlertCategoryId + }; + static constexpr ble_uuid16_t supportedUnreadAlertCategoryUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = supportedUnreadAlertCategoryId + }; + static constexpr ble_uuid16_t newAlertUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = newAlertId + }; + static constexpr ble_uuid16_t unreadAlertStatusUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = unreadAlertStatusId + }; + static constexpr ble_uuid16_t controlPointUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = controlPointId + }; + + uint16_t ansEndHandle; + uint16_t supportedNewAlertCategoryHandle; + uint16_t supportedUnreadAlertCategoryHandle; + uint16_t newAlertHandle; + uint16_t newAlertDescriptorHandle = 0; + uint16_t newAlertDefHandle; + uint16_t unreadAlertStatusHandle; + uint16_t controlPointHandle; + bool discoveryDone = false; + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::NotificationManager& notificationManager; + + }; + + + } +} \ No newline at end of file diff --git a/src/Components/Ble/CurrentTimeClient.cpp b/src/Components/Ble/CurrentTimeClient.cpp index 524c37d5..44065bce 100644 --- a/src/Components/Ble/CurrentTimeClient.cpp +++ b/src/Components/Ble/CurrentTimeClient.cpp @@ -37,21 +37,23 @@ void CurrentTimeClient::StartDiscovery(uint16_t connectionHandle) { ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsServiceUuid), CurrentTimeDiscoveryEventCallback, this); } -int CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { - if(service == nullptr && error->status == BLE_HS_EDONE) - NRF_LOG_INFO("Discovery complete"); +bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("CTS Discovery complete"); + return true; + } if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsServiceUuid), &service->uuid.u) == 0) { - NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); + NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)¤tTimeCharacteristicUuid), CurrentTimeCharacteristicDiscoveredCallback, this); } - return 0; + return false; } int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_chr *characteristic) { if(characteristic == nullptr && error->status == BLE_HS_EDONE) - NRF_LOG_INFO("Characteristic discovery complete"); + NRF_LOG_INFO("CTS Characteristic discovery complete"); if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); diff --git a/src/Components/Ble/CurrentTimeClient.h b/src/Components/Ble/CurrentTimeClient.h index 94892cef..43deadcc 100644 --- a/src/Components/Ble/CurrentTimeClient.h +++ b/src/Components/Ble/CurrentTimeClient.h @@ -17,7 +17,7 @@ namespace Pinetime { public: explicit CurrentTimeClient(DateTime& dateTimeController); void Init(); - int OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); + bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_chr *characteristic); int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index cedc9f60..5e3d58eb 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -1,5 +1,10 @@ + #include +#include +#include +#include + #include "NimbleController.h" #include #include @@ -7,15 +12,22 @@ #include #include #include -#include + + using namespace Pinetime::Controllers; // TODO c++ify the following code // - cts should be in it own class -NimbleController::NimbleController(DateTime &datetimeController) : dateTimeController{datetimeController}, - currentTimeClient{datetimeController} { +NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, + DateTime& dateTimeController, + Pinetime::Controllers::NotificationManager& notificationManager) : + systemTask{systemTask}, + dateTimeController{dateTimeController}, + notificationManager{notificationManager}, + currentTimeClient{dateTimeController}, + alertNotificationClient{systemTask, notificationManager} { } @@ -83,6 +95,15 @@ void NimbleController::StartAdvertising() { } +int OnAllSvrDisco(uint16_t conn_handle, + const struct ble_gatt_error *error, + const struct ble_gatt_svc *service, + void *arg) { + auto nimbleController = static_cast(arg); + return nimbleController->OnDiscoveryEvent(conn_handle, error, service); + return 0; +} + int NimbleController::OnGAPEvent(ble_gap_event *event) { switch (event->type) { case BLE_GAP_EVENT_ADV_COMPLETE: @@ -102,7 +123,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { StartAdvertising(); } else { connectionHandle = event->connect.conn_handle; - currentTimeClient.StartDiscovery(connectionHandle); + ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this); } } break; @@ -155,6 +176,25 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { */ } return BLE_GAP_REPEAT_PAIRING_RETRY; + + case BLE_GAP_EVENT_NOTIFY_RX: { + /* Peer sent us a notification or indication. */ + size_t notifSize = OS_MBUF_PKTLEN(event->notify_rx.om); + + NRF_LOG_INFO("received %s; conn_handle=%d attr_handle=%d " + "attr_len=%d", + event->notify_rx.indication ? + "indication" : + "notification", + event->notify_rx.conn_handle, + event->notify_rx.attr_handle, + notifSize); + + alertNotificationClient.OnNotification(event); + return 0; + } + /* Attribute data is contained in event->notify_rx.attr_data. */ + default: NRF_LOG_INFO("Advertising event : %d", event->type); break; @@ -162,6 +202,12 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { return 0; } +int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error, const ble_gatt_svc *service) { + alertNotificationClient.OnDiscoveryEvent(i, error, service); +// currentTimeClient.OnDiscoveryEvent(i, error, service); + return 0; +} + diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 2a396949..c4eab52b 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -1,7 +1,7 @@ #pragma once #include - +#include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" #include @@ -11,16 +11,21 @@ namespace Pinetime { class DateTime; class NimbleController { public: - NimbleController(DateTime& dateTimeController); + NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event *event); + int OnDiscoveryEvent(uint16_t i, const ble_gatt_error *pError, const ble_gatt_svc *pSvc); private: static constexpr char* deviceName = "Pinetime-JF"; + Pinetime::System::SystemTask& systemTask; DateTime& dateTimeController; + Pinetime::Controllers::NotificationManager& notificationManager; + DeviceInformationService deviceInformationService; CurrentTimeClient currentTimeClient; + AlertNotificationClient alertNotificationClient; uint8_t addrType; uint16_t connectionHandle; }; diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index ab30d5aa..5dceb89b 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -22,7 +22,7 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::C Pinetime::Controllers::NotificationManager& notificationManager) : spi{spi}, lcd{lcd}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, - watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, nimbleController({dateTimeController}) { + watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, nimbleController(*this, dateTimeController, notificationManager) { systemTaksMsgQueue = xQueueCreate(10, 1); } -- cgit v1.2.3 From 032fad094c6411ad3ff4321ad61ceed95d7dc4ff Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 25 Apr 2020 15:52:00 +0200 Subject: NimbleController : CTS & ANS are now working together (even if the code is not as good as I would like). --- src/Components/Ble/AlertNotificationClient.cpp | 49 ++++++--------- src/Components/Ble/AlertNotificationClient.h | 66 +++++++++----------- src/Components/Ble/CurrentTimeClient.cpp | 51 +++++++-------- src/Components/Ble/CurrentTimeClient.h | 19 +++--- src/Components/Ble/NimbleController.cpp | 86 +++++++++++++++++++++++++- src/Components/Ble/NimbleController.h | 8 +++ 6 files changed, 174 insertions(+), 105 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/Components/Ble/AlertNotificationClient.cpp b/src/Components/Ble/AlertNotificationClient.cpp index e6683e99..6e096353 100644 --- a/src/Components/Ble/AlertNotificationClient.cpp +++ b/src/Components/Ble/AlertNotificationClient.cpp @@ -13,19 +13,6 @@ constexpr ble_uuid16_t AlertNotificationClient::newAlertUuid; constexpr ble_uuid16_t AlertNotificationClient::unreadAlertStatusUuid; constexpr ble_uuid16_t AlertNotificationClient::controlPointUuid; -int Pinetime::Controllers::AlertNotificationDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg) { - auto client = static_cast(arg); - return client->OnDiscoveryEvent(conn_handle, error, service); -} - -int Pinetime::Controllers::AlertNotificationCharacteristicsDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast(arg); - return client->OnCharacteristicsDiscoveryEvent(conn_handle, error, chr); -} - int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, @@ -34,37 +21,23 @@ int Pinetime::Controllers::NewAlertSubcribeCallback(uint16_t conn_handle, return client->OnNewAlertSubcribe(conn_handle, error, attr); } -int Pinetime::Controllers::AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - uint16_t chr_val_handle, - const struct ble_gatt_dsc *dsc, - void *arg) { - NRF_LOG_INFO("ANS VCS"); - auto client = static_cast(arg); - return client->OnDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); -} - AlertNotificationClient::AlertNotificationClient(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager) : systemTask{systemTask}, notificationManager{notificationManager}{ } -void AlertNotificationClient::StartDiscovery(uint16_t connectionHandle) { - ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ansServiceUuid), AlertNotificationDiscoveryEventCallback, this); -} - bool AlertNotificationClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { if(service == nullptr && error->status == BLE_HS_EDONE) { NRF_LOG_INFO("ANS Discovery complete"); - ble_gattc_disc_all_dscs(connectionHandle, newAlertHandle, ansEndHandle, AlertNotificationDescriptorDiscoveryEventCallback, this); return true; } if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ansServiceUuid), &service->uuid.u) == 0) { NRF_LOG_INFO("ANS discovered : 0x%x", service->start_handle); - ble_gattc_disc_all_chrs(connectionHandle, service->start_handle, service->end_handle, AlertNotificationCharacteristicsDiscoveryEventCallback, this); - ansEndHandle = service->end_handle; + ansStartHandle = service->start_handle; + ansEndHandle = service->end_handle; + isDiscovered = true; } return false; } @@ -147,3 +120,19 @@ void AlertNotificationClient::OnNotification(ble_gap_event *event) { systemTask.PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); } } + +bool AlertNotificationClient::IsDiscovered() const { + return isDiscovered; +} + +uint16_t AlertNotificationClient::StartHandle() const { + return ansStartHandle; +} + +uint16_t AlertNotificationClient::EndHandle() const { + return ansEndHandle; +} + +uint16_t AlertNotificationClient::NewAlerthandle() const { + return newAlertHandle; +} diff --git a/src/Components/Ble/AlertNotificationClient.h b/src/Components/Ble/AlertNotificationClient.h index 595dbe37..7a085b7e 100644 --- a/src/Components/Ble/AlertNotificationClient.h +++ b/src/Components/Ble/AlertNotificationClient.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -6,29 +7,17 @@ namespace Pinetime { namespace Controllers { - int AlertNotificationDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg); - int AlertNotificationCharacteristicsDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg); int NewAlertSubcribeCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, - void *arg); - - int AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - uint16_t chr_val_handle, - const struct ble_gatt_dsc *dsc, - void *arg); + const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, + void *arg); class AlertNotificationClient { public: - explicit AlertNotificationClient(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); + explicit AlertNotificationClient(Pinetime::System::SystemTask &systemTask, + Pinetime::Controllers::NotificationManager ¬ificationManager); void Init(); - - void StartDiscovery(uint16_t connectionHandle); bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service); int OnCharacteristicsDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_chr *characteristic); @@ -36,39 +25,47 @@ namespace Pinetime { int OnDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); void OnNotification(ble_gap_event *event); + bool IsDiscovered() const; + uint16_t StartHandle() const; + uint16_t EndHandle() const; + + static constexpr const ble_uuid16_t &Uuid() { return ansServiceUuid; } + + uint16_t NewAlerthandle() const; private: - static constexpr uint16_t ansServiceId {0x1811}; + static constexpr uint16_t ansServiceId{0x1811}; static constexpr uint16_t supportedNewAlertCategoryId = 0x2a47; static constexpr uint16_t supportedUnreadAlertCategoryId = 0x2a48; static constexpr uint16_t newAlertId = 0x2a46; static constexpr uint16_t unreadAlertStatusId = 0x2a45; static constexpr uint16_t controlPointId = 0x2a44; - static constexpr ble_uuid16_t ansServiceUuid { - .u { .type = BLE_UUID_TYPE_16 }, + static constexpr ble_uuid16_t ansServiceUuid{ + .u {.type = BLE_UUID_TYPE_16}, .value = ansServiceId }; - static constexpr ble_uuid16_t supportedNewAlertCategoryUuid { - .u { .type = BLE_UUID_TYPE_16 }, + static constexpr ble_uuid16_t supportedNewAlertCategoryUuid{ + .u {.type = BLE_UUID_TYPE_16}, .value = supportedNewAlertCategoryId }; - static constexpr ble_uuid16_t supportedUnreadAlertCategoryUuid { - .u { .type = BLE_UUID_TYPE_16 }, + static constexpr ble_uuid16_t supportedUnreadAlertCategoryUuid{ + .u {.type = BLE_UUID_TYPE_16}, .value = supportedUnreadAlertCategoryId }; - static constexpr ble_uuid16_t newAlertUuid { - .u { .type = BLE_UUID_TYPE_16 }, + static constexpr ble_uuid16_t newAlertUuid{ + .u {.type = BLE_UUID_TYPE_16}, .value = newAlertId }; - static constexpr ble_uuid16_t unreadAlertStatusUuid { - .u { .type = BLE_UUID_TYPE_16 }, + static constexpr ble_uuid16_t unreadAlertStatusUuid{ + .u {.type = BLE_UUID_TYPE_16}, .value = unreadAlertStatusId }; - static constexpr ble_uuid16_t controlPointUuid { - .u { .type = BLE_UUID_TYPE_16 }, + static constexpr ble_uuid16_t controlPointUuid{ + .u {.type = BLE_UUID_TYPE_16}, .value = controlPointId }; + uint16_t ansStartHandle; uint16_t ansEndHandle; uint16_t supportedNewAlertCategoryHandle; uint16_t supportedUnreadAlertCategoryHandle; @@ -77,12 +74,9 @@ namespace Pinetime { uint16_t newAlertDefHandle; uint16_t unreadAlertStatusHandle; uint16_t controlPointHandle; - bool discoveryDone = false; - Pinetime::System::SystemTask& systemTask; - Pinetime::Controllers::NotificationManager& notificationManager; - + bool isDiscovered = false; + Pinetime::System::SystemTask &systemTask; + Pinetime::Controllers::NotificationManager ¬ificationManager; }; - - } } \ No newline at end of file diff --git a/src/Components/Ble/CurrentTimeClient.cpp b/src/Components/Ble/CurrentTimeClient.cpp index 44065bce..fdebc084 100644 --- a/src/Components/Ble/CurrentTimeClient.cpp +++ b/src/Components/Ble/CurrentTimeClient.cpp @@ -6,25 +6,6 @@ using namespace Pinetime::Controllers; constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid; constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid; -int Pinetime::Controllers::CurrentTimeDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg) { - auto client = static_cast(arg); - return client->OnDiscoveryEvent(conn_handle, error, service); -} - -int Pinetime::Controllers::CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast(arg); - return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); -} - -int Pinetime::Controllers::CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, void *arg) { - auto client = static_cast(arg); - return client->OnCurrentTimeReadResult(conn_handle, error, attr); -} - - CurrentTimeClient::CurrentTimeClient(DateTime& dateTimeController) : dateTimeController{dateTimeController} { } @@ -33,10 +14,6 @@ void CurrentTimeClient::Init() { } -void CurrentTimeClient::StartDiscovery(uint16_t connectionHandle) { - ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsServiceUuid), CurrentTimeDiscoveryEventCallback, this); -} - bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) { if(service == nullptr && error->status == BLE_HS_EDONE) { NRF_LOG_INFO("CTS Discovery complete"); @@ -45,20 +22,24 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsServiceUuid), &service->uuid.u) == 0) { NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle); - ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)¤tTimeCharacteristicUuid), CurrentTimeCharacteristicDiscoveredCallback, this); + isDiscovered = true; + ctsStartHandle = service->start_handle; + ctsEndHandle = service->end_handle; + return false; } return false; } int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_chr *characteristic) { - if(characteristic == nullptr && error->status == BLE_HS_EDONE) + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { NRF_LOG_INFO("CTS Characteristic discovery complete"); + return 0; + } if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); - - ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this); + currentTimeHandle = characteristic->val_handle; } return 0; } @@ -78,3 +59,19 @@ int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_g } return 0; } + +bool CurrentTimeClient::IsDiscovered() const { + return isDiscovered; +} + +uint16_t CurrentTimeClient::StartHandle() const { + return ctsStartHandle; +} + +uint16_t CurrentTimeClient::EndHandle() const { + return ctsEndHandle; +} + +uint16_t CurrentTimeClient::CurrentTimeHandle() const { + return currentTimeHandle; +} diff --git a/src/Components/Ble/CurrentTimeClient.h b/src/Components/Ble/CurrentTimeClient.h index 43deadcc..2278ef15 100644 --- a/src/Components/Ble/CurrentTimeClient.h +++ b/src/Components/Ble/CurrentTimeClient.h @@ -6,12 +6,6 @@ namespace Pinetime { namespace Controllers { - int CurrentTimeDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, void *arg); - int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg); - int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, void *arg); class CurrentTimeClient { public: @@ -21,9 +15,12 @@ namespace Pinetime { int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_chr *characteristic); int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); - - - void StartDiscovery(uint16_t connectionHandle); + bool IsDiscovered() const; + uint16_t StartHandle() const; + uint16_t EndHandle() const; + uint16_t CurrentTimeHandle() const; + static constexpr const ble_uuid16_t* Uuid() { return &CurrentTimeClient::ctsServiceUuid; } + static constexpr const ble_uuid16_t* CurrentTimeCharacteristicUuid() { return &CurrentTimeClient::currentTimeCharacteristicUuid; } private: typedef struct __attribute__((packed)) { uint16_t year; @@ -48,7 +45,11 @@ namespace Pinetime { .value = currentTimeCharacteristicId }; + uint16_t currentTimeHandle; DateTime& dateTimeController; + bool isDiscovered = false; + uint16_t ctsStartHandle; + uint16_t ctsEndHandle; }; } } \ No newline at end of file diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 5e3d58eb..7894ff43 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -17,8 +17,9 @@ using namespace Pinetime::Controllers; -// TODO c++ify the following code -// - cts should be in it own class +// TODO I'm not satisfied by how this code looks like (AlertNotificationClient and CurrentTimeClient must +// expose too much data, too many callbacks -> NimbleController -> CTS/ANS client. +// Let's try to improve this code (and keep it working!) NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, @@ -36,6 +37,33 @@ int GAPEventCallback(struct ble_gap_event *event, void *arg) { return nimbleController->OnGAPEvent(event); } +int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto client = static_cast(arg); + return client->OnCTSCharacteristicDiscoveryEvent(conn_handle, error, chr); +} + +int AlertNotificationCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + const struct ble_gatt_chr *chr, void *arg) { + auto client = static_cast(arg); + return client->OnANSCharacteristicDiscoveryEvent(conn_handle, error, chr); +} + +int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, + struct ble_gatt_attr *attr, void *arg) { + auto client = static_cast(arg); + return client->OnCurrentTimeReadResult(conn_handle, error, attr); +} + +int AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, + const struct ble_gatt_error *error, + uint16_t chr_val_handle, + const struct ble_gatt_dsc *dsc, + void *arg) { + auto client = static_cast(arg); + return client->OnANSDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); +} + void NimbleController::Init() { while (!ble_hs_synced()) {} @@ -203,11 +231,63 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { } int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error, const ble_gatt_svc *service) { + if(service == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("Service Discovery complete"); + if(currentTimeClient.IsDiscovered()) { + ble_gattc_disc_all_chrs(connectionHandle, currentTimeClient.StartHandle(), currentTimeClient.EndHandle(), + CurrentTimeCharacteristicDiscoveredCallback, this); + + } else if(alertNotificationClient.IsDiscovered()) { + ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), alertNotificationClient.EndHandle(), + AlertNotificationCharacteristicDiscoveredCallback, this); + } + return 0; + } + alertNotificationClient.OnDiscoveryEvent(i, error, service); -// currentTimeClient.OnDiscoveryEvent(i, error, service); + currentTimeClient.OnDiscoveryEvent(i, error, service); + return 0; +} + +int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("CTS characteristic Discovery complete"); + ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this); + return 0; + } + return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic); +} + +int NimbleController::OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic) { + if(characteristic == nullptr && error->status == BLE_HS_EDONE) { + NRF_LOG_INFO("ANS characteristic Discovery complete"); + ble_gattc_disc_all_dscs(connectionHandle, + alertNotificationClient.NewAlerthandle(), alertNotificationClient.EndHandle(), + AlertNotificationDescriptorDiscoveryEventCallback, this); + return 0; + } + return alertNotificationClient.OnCharacteristicsDiscoveryEvent(connectionHandle, error, characteristic); +} + +int NimbleController::OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute) { + currentTimeClient.OnCurrentTimeReadResult(connectionHandle, error, attribute); + + if (alertNotificationClient.IsDiscovered()) { + ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), + alertNotificationClient.EndHandle(), + AlertNotificationCharacteristicDiscoveredCallback, this); + } return 0; } +int NimbleController::OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, + const ble_gatt_dsc *descriptor) { + return alertNotificationClient.OnDescriptorDiscoveryEventCallback(connectionHandle, error, characteristicValueHandle, descriptor); +} + diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index c4eab52b..7a7a94c9 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -10,6 +10,7 @@ namespace Pinetime { namespace Controllers { class DateTime; class NimbleController { + public: NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); void Init(); @@ -17,6 +18,13 @@ namespace Pinetime { int OnGAPEvent(ble_gap_event *event); int OnDiscoveryEvent(uint16_t i, const ble_gatt_error *pError, const ble_gatt_svc *pSvc); + int OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, + const ble_gatt_chr *characteristic); + int OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute); + int OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, + uint16_t characteristicValueHandle, const ble_gatt_dsc *descriptor); private: static constexpr char* deviceName = "Pinetime-JF"; Pinetime::System::SystemTask& systemTask; -- cgit v1.2.3 From 746c164c593d2b9aa63a0f9a1c1ad032de9627a0 Mon Sep 17 00:00:00 2001 From: JF Date: Mon, 27 Apr 2020 20:13:27 +0200 Subject: BLE : Display the actual status of the connection on the screen. --- src/CMakeLists.txt | 1 - src/Components/Ble/NimbleController.cpp | 5 +++++ src/Components/Ble/NimbleController.h | 3 ++- src/SystemTask/SystemTask.cpp | 3 ++- src/main.cpp | 39 --------------------------------- 5 files changed, 9 insertions(+), 42 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ad83caee..bd9f3b42 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -357,7 +357,6 @@ set(INCLUDE_FILES drivers/SpiMaster.h drivers/Watchdog.h drivers/DebugPins.h - BLE/BleManager.h Components/Battery/BatteryController.h Components/Ble/BleController.h Components/Ble/NotificationManager.h diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 7894ff43..02f99180 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -22,9 +22,11 @@ using namespace Pinetime::Controllers; // Let's try to improve this code (and keep it working!) NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager) : systemTask{systemTask}, + bleController{bleController}, dateTimeController{dateTimeController}, notificationManager{notificationManager}, currentTimeClient{dateTimeController}, @@ -149,7 +151,9 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { if (event->connect.status != 0) { /* Connection failed; resume advertising. */ StartAdvertising(); + bleController.Disconnect(); } else { + bleController.Connect(); connectionHandle = event->connect.conn_handle; ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this); } @@ -160,6 +164,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { NRF_LOG_INFO("disconnect; reason=%d ", event->disconnect.reason); /* Connection terminated; resume advertising. */ + bleController.Disconnect(); StartAdvertising(); break; case BLE_GAP_EVENT_CONN_UPDATE: diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 7a7a94c9..99e0c811 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -12,7 +12,7 @@ namespace Pinetime { class NimbleController { public: - NimbleController(Pinetime::System::SystemTask& systemTask, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); + NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, Pinetime::Controllers::NotificationManager& notificationManager); void Init(); void StartAdvertising(); int OnGAPEvent(ble_gap_event *event); @@ -28,6 +28,7 @@ namespace Pinetime { private: static constexpr char* deviceName = "Pinetime-JF"; Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::Ble& bleController; DateTime& dateTimeController; Pinetime::Controllers::NotificationManager& notificationManager; diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index a003163c..6516f68b 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -21,7 +21,8 @@ SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::C Pinetime::Controllers::NotificationManager& notificationManager) : spi{spi}, lcd{lcd}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, bleController{bleController}, dateTimeController{dateTimeController}, - watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, nimbleController(*this, dateTimeController, notificationManager) { + watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, + nimbleController(*this, bleController,dateTimeController, notificationManager) { systemTaksMsgQueue = xQueueCreate(10, 1); } diff --git a/src/main.cpp b/src/main.cpp index 9748f533..7c5eaf7c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,7 +7,6 @@ #include #include #include -//#include #include #include "Components/Battery/BatteryController.h" #include "Components/Ble/BleController.h" @@ -90,34 +89,6 @@ void DebounceTimerCallback(TimerHandle_t xTimer) { systemTask->OnButtonPushed(); } -void OnBleConnection() { - bleController.Connect(); -} - -void OnBleDisconnection() { - bleController.Disconnect(); -} - -void OnNewNotification(const char* message, uint8_t size) { - notificationManager.Push(Pinetime::Controllers::NotificationManager::Categories::SimpleAlert, message, size); - systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnNewNotification); -} - -//void OnNewTime(current_time_char_t* currentTime) { -// auto dayOfWeek = currentTime->exact_time_256.day_date_time.day_of_week; -// auto year = currentTime->exact_time_256.day_date_time.date_time.year; -// auto month = currentTime->exact_time_256.day_date_time.date_time.month; -// auto day = currentTime->exact_time_256.day_date_time.date_time.day; -// auto hour = currentTime->exact_time_256.day_date_time.date_time.hours; -// auto minute = currentTime->exact_time_256.day_date_time.date_time.minutes; -// auto second = currentTime->exact_time_256.day_date_time.date_time.seconds; -// -// dateTimeController.SetTime(year, month, day, -// dayOfWeek, hour, minute, second, nrf_rtc_counter_get(portNRF_RTC_REG)); -// -// systemTask->PushMessage(Pinetime::System::SystemTask::Messages::OnNewTime); -//} - void SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler(void) { if(((NRF_SPIM0->INTENSET & (1<<6)) != 0) && NRF_SPIM0->EVENTS_END == 1) { NRF_SPIM0->EVENTS_END = 0; @@ -218,8 +189,6 @@ void nimble_port_init(void) { ble_ll_init(); ble_hci_ram_init(); nimble_port_freertos_init(BleHost); - - } void nimble_port_ll_task_func(void *args) { @@ -238,16 +207,8 @@ int main(void) { systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, touchPanel, lvgl, batteryController, bleController, dateTimeController, notificationManager)); systemTask->Start(); - nimble_port_init(); - -// ble_manager_init(); -// ble_manager_set_new_time_callback(OnNewTime); -// ble_manager_set_ble_connection_callback(OnBleConnection); -// ble_manager_set_ble_disconnection_callback(OnBleDisconnection); -// ble_manager_set_new_notification_callback(OnNewNotification); - vTaskStartScheduler(); for (;;) { -- cgit v1.2.3 From ed168716b5055d3efec9b30b1b1a3c7ef6c5b17d Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 2 May 2020 17:42:26 +0200 Subject: Add Asssert & debug messages. --- src/Components/Ble/NimbleController.cpp | 21 +++++++++++++-------- src/main.cpp | 7 +++++-- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 02f99180..64ec379d 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -76,9 +76,14 @@ void NimbleController::Init() { currentTimeClient.Init(); int res; res = ble_hs_util_ensure_addr(0); + ASSERT(res == 0); res = ble_hs_id_infer_auto(0, &addrType); + ASSERT(res == 0); res = ble_svc_gap_device_name_set(deviceName); - ble_gatts_start(); + + ASSERT(res == 0); + res = ble_gatts_start(); + ASSERT(res == 0); } void NimbleController::StartAdvertising() { @@ -115,14 +120,14 @@ void NimbleController::StartAdvertising() { int res; res = ble_gap_adv_set_fields(&fields); - assert(res == 0); + ASSERT(res == 0); - ble_gap_adv_rsp_set_fields(&rsp_fields); + res = ble_gap_adv_rsp_set_fields(&rsp_fields); + ASSERT(res == 0); - ble_gap_adv_start(addrType, NULL, 10000, + res = ble_gap_adv_start(addrType, NULL, 10000, &adv_params, GAPEventCallback, this); - - + ASSERT(res == 0); } int OnAllSvrDisco(uint16_t conn_handle, @@ -138,7 +143,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { switch (event->type) { case BLE_GAP_EVENT_ADV_COMPLETE: NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE"); - NRF_LOG_INFO("advertise complete; reason=%d", event->adv_complete.reason); + NRF_LOG_INFO("advertise complete; reason=%dn status=%d", event->adv_complete.reason, event->connect.status); StartAdvertising(); break; case BLE_GAP_EVENT_CONNECT: { @@ -161,7 +166,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { break; case BLE_GAP_EVENT_DISCONNECT: NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_DISCONNECT"); - NRF_LOG_INFO("disconnect; reason=%d ", event->disconnect.reason); + NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason); /* Connection terminated; resume advertising. */ bleController.Disconnect(); diff --git a/src/main.cpp b/src/main.cpp index 7c5eaf7c..e0e9b65e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -184,8 +184,11 @@ void nimble_port_init(void) { ble_hs_init(); ble_store_ram_init(); - hal_timer_init(5, NULL); - os_cputime_init(32768); + int res; + res = hal_timer_init(5, NULL); + ASSERT(res == 0); + res = os_cputime_init(32768); + ASSERT(res == 0); ble_ll_init(); ble_hci_ram_init(); nimble_port_freertos_init(BleHost); -- cgit v1.2.3 From b760b3f98ca786bbe0d586e30fed2847525b0ae4 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Tue, 28 Apr 2020 11:21:35 +0100 Subject: Add a simple service to allow setting the time from a controlling application --- src/CMakeLists.txt | 1 + src/Components/Ble/NimbleController.cpp | 3 ++ src/Components/Ble/NimbleController.h | 2 + src/Components/Ble/PinetimeService.cpp | 73 +++++++++++++++++++++++++++++++++ src/Components/Ble/PinetimeService.h | 50 ++++++++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 src/Components/Ble/PinetimeService.cpp create mode 100644 src/Components/Ble/PinetimeService.h (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bd9f3b42..2e14c02b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -319,6 +319,7 @@ list(APPEND SOURCE_FILES Components/Ble/DeviceInformationService.cpp Components/Ble/CurrentTimeClient.cpp Components/Ble/AlertNotificationClient.cpp + Components/Ble/PinetimeService.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 02f99180..77bab5de 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -74,6 +74,9 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); + pinetimeService.Init(); + pinetimeService.setDateTimeController(&dateTimeController); + int res; res = ble_hs_util_ensure_addr(0); res = ble_hs_id_infer_auto(0, &addrType); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 99e0c811..d315e97b 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -4,6 +4,7 @@ #include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" +#include "PinetimeService.h" #include namespace Pinetime { @@ -35,6 +36,7 @@ namespace Pinetime { DeviceInformationService deviceInformationService; CurrentTimeClient currentTimeClient; AlertNotificationClient alertNotificationClient; + PinetimeService pinetimeService; uint8_t addrType; uint16_t connectionHandle; }; diff --git a/src/Components/Ble/PinetimeService.cpp b/src/Components/Ble/PinetimeService.cpp new file mode 100644 index 00000000..5bcb36d1 --- /dev/null +++ b/src/Components/Ble/PinetimeService.cpp @@ -0,0 +1,73 @@ +#include "PinetimeService.h" +#include + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t PinetimeService::pinetimeUuid; +constexpr ble_uuid16_t PinetimeService::timeUuid; + + +int PinetimeTimeCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto pinetimeService = static_cast(arg); + return pinetimeService->OnTimeAccessed(conn_handle, attr_handle, ctxt); +} + +void PinetimeService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int PinetimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + + NRF_LOG_INFO("Setting time..."); + + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + if (m_dateTimeController) { + CtsData result; + os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); + + NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, + result.month, result.dayofmonth, + result.hour, result.minute, result.second); + + m_dateTimeController->SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + } + } + return 0; +} + +PinetimeService::PinetimeService() : + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &timeUuid, + .access_cb = PinetimeTimeCallback, + + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &pinetimeUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + +void PinetimeService::setDateTimeController(DateTime *dateTimeController) +{ + m_dateTimeController = dateTimeController; +} diff --git a/src/Components/Ble/PinetimeService.h b/src/Components/Ble/PinetimeService.h new file mode 100644 index 00000000..d4f8ee2b --- /dev/null +++ b/src/Components/Ble/PinetimeService.h @@ -0,0 +1,50 @@ +#pragma once +#include +#include +#include +#include + +namespace Pinetime { + namespace Controllers { + class PinetimeService { + public: + PinetimeService(); + void Init(); + + int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + void setDateTimeController(DateTime *dateTimeController); + + private: + static constexpr uint16_t pinetimeId {0x6666}; + static constexpr uint16_t timeCharId {0x6667}; + + static constexpr ble_uuid16_t pinetimeUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = pinetimeId + }; + + static constexpr ble_uuid16_t timeUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = timeCharId + }; + + struct ble_gatt_chr_def characteristicDefinition[2]; + struct ble_gatt_svc_def serviceDefinition[2]; + + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; + + DateTime *m_dateTimeController = nullptr; + }; + } +} -- cgit v1.2.3 From d33be52bc9cec10e530e1390f1cf812c12cd830e Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Tue, 28 Apr 2020 18:39:26 +0100 Subject: Run the alert notification service and simplify trhe pinetime service initialization --- src/CMakeLists.txt | 1 + src/Components/Ble/NimbleController.cpp | 7 +++++-- src/Components/Ble/NimbleController.h | 3 +++ src/Components/Ble/PinetimeService.cpp | 23 +++++++++-------------- src/Components/Ble/PinetimeService.h | 4 ++-- 5 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2e14c02b..777faefc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -320,6 +320,7 @@ list(APPEND SOURCE_FILES Components/Ble/CurrentTimeClient.cpp Components/Ble/AlertNotificationClient.cpp Components/Ble/PinetimeService.cpp + Components/Ble/AlertNotificationService.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 77bab5de..eb765b80 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -30,7 +30,9 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, dateTimeController{dateTimeController}, notificationManager{notificationManager}, currentTimeClient{dateTimeController}, - alertNotificationClient{systemTask, notificationManager} { + alertNotificationClient{systemTask, notificationManager}, + anService{systemTask, notificationManager}, + pinetimeService{dateTimeController} { } @@ -75,7 +77,8 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); pinetimeService.Init(); - pinetimeService.setDateTimeController(&dateTimeController); + + anService.Init(); int res; res = ble_hs_util_ensure_addr(0); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index d315e97b..820b1c77 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -1,6 +1,7 @@ #pragma once #include +#include "AlertNotificationService.h" #include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" @@ -35,8 +36,10 @@ namespace Pinetime { DeviceInformationService deviceInformationService; CurrentTimeClient currentTimeClient; + AlertNotificationService anService; AlertNotificationClient alertNotificationClient; PinetimeService pinetimeService; + uint8_t addrType; uint16_t connectionHandle; }; diff --git a/src/Components/Ble/PinetimeService.cpp b/src/Components/Ble/PinetimeService.cpp index 5bcb36d1..e18d78aa 100644 --- a/src/Components/Ble/PinetimeService.cpp +++ b/src/Components/Ble/PinetimeService.cpp @@ -24,22 +24,21 @@ int PinetimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, NRF_LOG_INFO("Setting time..."); if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { - if (m_dateTimeController) { - CtsData result; - os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); + CtsData result; + os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); - NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, - result.month, result.dayofmonth, - result.hour, result.minute, result.second); + NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, + result.month, result.dayofmonth, + result.hour, result.minute, result.second); + + m_dateTimeController.SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); - m_dateTimeController->SetTime(result.year, result.month, result.dayofmonth, - 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); - } } return 0; } -PinetimeService::PinetimeService() : +PinetimeService::PinetimeService(DateTime &dateTimeController) : m_dateTimeController{dateTimeController}, characteristicDefinition{ { .uuid = (ble_uuid_t *) &timeUuid, @@ -67,7 +66,3 @@ PinetimeService::PinetimeService() : } -void PinetimeService::setDateTimeController(DateTime *dateTimeController) -{ - m_dateTimeController = dateTimeController; -} diff --git a/src/Components/Ble/PinetimeService.h b/src/Components/Ble/PinetimeService.h index d4f8ee2b..0cae8345 100644 --- a/src/Components/Ble/PinetimeService.h +++ b/src/Components/Ble/PinetimeService.h @@ -8,7 +8,7 @@ namespace Pinetime { namespace Controllers { class PinetimeService { public: - PinetimeService(); + PinetimeService(DateTime &dateTimeController); void Init(); int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, @@ -44,7 +44,7 @@ namespace Pinetime { uint8_t reason; } CtsData; - DateTime *m_dateTimeController = nullptr; + DateTime &m_dateTimeController; }; } } -- cgit v1.2.3 From 206bdbf5eb94ac967284862b5d7a270ed50195db Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Mon, 4 May 2020 21:43:51 +0100 Subject: Move PinetimeService to CurrentTimeService witha view to implement CTS for time handling --- src/CMakeLists.txt | 2 +- src/Components/Ble/CurrentTimeService.cpp | 69 +++++++++++++++++++++++++++++++ src/Components/Ble/CurrentTimeService.h | 48 +++++++++++++++++++++ src/Components/Ble/NimbleController.cpp | 4 +- src/Components/Ble/NimbleController.h | 4 +- src/Components/Ble/PinetimeService.cpp | 68 ------------------------------ src/Components/Ble/PinetimeService.h | 50 ---------------------- 7 files changed, 122 insertions(+), 123 deletions(-) create mode 100644 src/Components/Ble/CurrentTimeService.cpp create mode 100644 src/Components/Ble/CurrentTimeService.h delete mode 100644 src/Components/Ble/PinetimeService.cpp delete mode 100644 src/Components/Ble/PinetimeService.h (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 777faefc..4b1f7c16 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -319,7 +319,7 @@ list(APPEND SOURCE_FILES Components/Ble/DeviceInformationService.cpp Components/Ble/CurrentTimeClient.cpp Components/Ble/AlertNotificationClient.cpp - Components/Ble/PinetimeService.cpp + Components/Ble/CurrentTimeService.cpp Components/Ble/AlertNotificationService.cpp drivers/Cst816s.cpp FreeRTOS/port.c diff --git a/src/Components/Ble/CurrentTimeService.cpp b/src/Components/Ble/CurrentTimeService.cpp new file mode 100644 index 00000000..0af20dd3 --- /dev/null +++ b/src/Components/Ble/CurrentTimeService.cpp @@ -0,0 +1,69 @@ +#include "CurrentTimeService.h" +#include + +using namespace Pinetime::Controllers; + +constexpr ble_uuid16_t CurrentTimeService::ctsUuid; +constexpr ble_uuid16_t CurrentTimeService::ctChrUuid; + + +int CTSCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { + auto cts = static_cast(arg); + return cts->OnTimeAccessed(conn_handle, attr_handle, ctxt); +} + +void CurrentTimeService::Init() { + ble_gatts_count_cfg(serviceDefinition); + ble_gatts_add_svcs(serviceDefinition); +} + + +int CurrentTimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt) { + + NRF_LOG_INFO("Setting time..."); + + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + CtsData result; + os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); + + NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, + result.month, result.dayofmonth, + result.hour, result.minute, result.second); + + m_dateTimeController.SetTime(result.year, result.month, result.dayofmonth, + 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); + + } + //!TODO need to support reading the time. + return 0; +} + +CurrentTimeService::CurrentTimeService(DateTime &dateTimeController) : m_dateTimeController{dateTimeController}, + characteristicDefinition{ + { + .uuid = (ble_uuid_t *) &ctChrUuid, + .access_cb = CTSCallback, + + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ + }, + { + 0 + } + }, + serviceDefinition{ + { + /* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t *) &ctsUuid, + .characteristics = characteristicDefinition + }, + { + 0 + }, + } + { + +} + diff --git a/src/Components/Ble/CurrentTimeService.h b/src/Components/Ble/CurrentTimeService.h new file mode 100644 index 00000000..58bc5ba6 --- /dev/null +++ b/src/Components/Ble/CurrentTimeService.h @@ -0,0 +1,48 @@ +#pragma once +#include +#include +#include +#include + +namespace Pinetime { + namespace Controllers { + class CurrentTimeService { + public: + CurrentTimeService(DateTime &dateTimeController); + void Init(); + + int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt); + + private: + static constexpr uint16_t ctsId {0x1805}; + static constexpr uint16_t ctsCharId {0x2a2b}; + + static constexpr ble_uuid16_t ctsUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ctsId + }; + + static constexpr ble_uuid16_t ctChrUuid { + .u { .type = BLE_UUID_TYPE_16 }, + .value = ctsCharId + }; + + struct ble_gatt_chr_def characteristicDefinition[2]; + struct ble_gatt_svc_def serviceDefinition[2]; + + typedef struct __attribute__((packed)) { + uint16_t year; + uint8_t month; + uint8_t dayofmonth; + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t millis; + uint8_t reason; + } CtsData; + + DateTime &m_dateTimeController; + }; + } +} diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index eb765b80..826d255b 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -32,7 +32,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, currentTimeClient{dateTimeController}, alertNotificationClient{systemTask, notificationManager}, anService{systemTask, notificationManager}, - pinetimeService{dateTimeController} { + currentTimeService{dateTimeController} { } @@ -76,7 +76,7 @@ void NimbleController::Init() { deviceInformationService.Init(); currentTimeClient.Init(); - pinetimeService.Init(); + currentTimeService.Init(); anService.Init(); diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h index 820b1c77..44fbbe2c 100644 --- a/src/Components/Ble/NimbleController.h +++ b/src/Components/Ble/NimbleController.h @@ -5,7 +5,7 @@ #include "AlertNotificationClient.h" #include "DeviceInformationService.h" #include "CurrentTimeClient.h" -#include "PinetimeService.h" +#include "CurrentTimeService.h" #include namespace Pinetime { @@ -38,7 +38,7 @@ namespace Pinetime { CurrentTimeClient currentTimeClient; AlertNotificationService anService; AlertNotificationClient alertNotificationClient; - PinetimeService pinetimeService; + CurrentTimeService currentTimeService; uint8_t addrType; uint16_t connectionHandle; diff --git a/src/Components/Ble/PinetimeService.cpp b/src/Components/Ble/PinetimeService.cpp deleted file mode 100644 index e18d78aa..00000000 --- a/src/Components/Ble/PinetimeService.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include "PinetimeService.h" -#include - -using namespace Pinetime::Controllers; - -constexpr ble_uuid16_t PinetimeService::pinetimeUuid; -constexpr ble_uuid16_t PinetimeService::timeUuid; - - -int PinetimeTimeCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { - auto pinetimeService = static_cast(arg); - return pinetimeService->OnTimeAccessed(conn_handle, attr_handle, ctxt); -} - -void PinetimeService::Init() { - ble_gatts_count_cfg(serviceDefinition); - ble_gatts_add_svcs(serviceDefinition); -} - - -int PinetimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt) { - - NRF_LOG_INFO("Setting time..."); - - if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { - CtsData result; - os_mbuf_copydata(ctxt->om, 0, sizeof(CtsData), &result); - - NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year, - result.month, result.dayofmonth, - result.hour, result.minute, result.second); - - m_dateTimeController.SetTime(result.year, result.month, result.dayofmonth, - 0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG)); - - } - return 0; -} - -PinetimeService::PinetimeService(DateTime &dateTimeController) : m_dateTimeController{dateTimeController}, - characteristicDefinition{ - { - .uuid = (ble_uuid_t *) &timeUuid, - .access_cb = PinetimeTimeCallback, - - .arg = this, - .flags = BLE_GATT_CHR_F_WRITE - }, - { - 0 - } - }, - serviceDefinition{ - { - /* Device Information Service */ - .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t *) &pinetimeUuid, - .characteristics = characteristicDefinition - }, - { - 0 - }, - } - { - -} - diff --git a/src/Components/Ble/PinetimeService.h b/src/Components/Ble/PinetimeService.h deleted file mode 100644 index 0cae8345..00000000 --- a/src/Components/Ble/PinetimeService.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace Pinetime { - namespace Controllers { - class PinetimeService { - public: - PinetimeService(DateTime &dateTimeController); - void Init(); - - int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle, - struct ble_gatt_access_ctxt *ctxt); - - void setDateTimeController(DateTime *dateTimeController); - - private: - static constexpr uint16_t pinetimeId {0x6666}; - static constexpr uint16_t timeCharId {0x6667}; - - static constexpr ble_uuid16_t pinetimeUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = pinetimeId - }; - - static constexpr ble_uuid16_t timeUuid { - .u { .type = BLE_UUID_TYPE_16 }, - .value = timeCharId - }; - - struct ble_gatt_chr_def characteristicDefinition[2]; - struct ble_gatt_svc_def serviceDefinition[2]; - - typedef struct __attribute__((packed)) { - uint16_t year; - uint8_t month; - uint8_t dayofmonth; - uint8_t hour; - uint8_t minute; - uint8_t second; - uint8_t millis; - uint8_t reason; - } CtsData; - - DateTime &m_dateTimeController; - }; - } -} -- cgit v1.2.3 From d6c6ac4cf5801e17caf7bfc0878423703ed0413b Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 16 May 2020 16:13:22 +0200 Subject: Remove reference to NRF Softdevice in CMake and documentation. Update documentation. Remove Asserts when starting advertising to prevent crash (known bug). Set version 0.5.0. --- CMakeLists.txt | 2 +- README.md | 26 ++----- cmake-nRF5x/CMake_nRF5x.cmake | 124 +------------------------------- src/Components/Ble/NimbleController.cpp | 13 +++- src/main.cpp | 1 - 5 files changed, 19 insertions(+), 147 deletions(-) (limited to 'src/Components/Ble/NimbleController.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 5af971f0..f803cecc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(pinetime VERSION 0.4.0 LANGUAGES C CXX ASM) +project(pinetime VERSION 0.5.0 LANGUAGES C CXX ASM) set(NRF_TARGET "nrf52") diff --git a/README.md b/README.md index 790e3de1..4c970f8e 100644 --- a/README.md +++ b/README.md @@ -27,14 +27,16 @@ I've tested this project on the actual PineTime hardware. * Project builds and runs on the Pinetime; * Logs available via JLink RTT; * SPI (DMA & IRQ based) LCD driver; - * BLE advertising, connection and bonding; + * Open source BLE stack : [NimBLE](https://github.com/apache/mynewt-nimble); + * BLE advertising and connection connection; * BLE CTS client (retrieves the time from the connected device if it implements a CTS server); * Push button to go to disable screen (and go to low power mode) / enable screen (and wake-up) and UI navigation * Touch panel support; * Rich user interface (using [LittleVGL](https://littlevgl.com/)) via display, touchpanel and push button. * Digital watch face and 4 demo applications (spinning meter, analog gauche, push button and message box); * Watchdog (automatic reset in case of firmware crash) and reset support (push and hold the button for 7 - 10s); - * BLE Notification support (still Work-In-Progress, [companion app](https://github.com/JF002/gobbledegook) needed). + * BLE Notification support (still Work-In-Progress, [companion app](https://github.com/JF002/gobbledegook) needed); + * Supported by companion app [Amazfish](https://openrepos.net/content/piggz/amazfish) (time synchronization and notifications are integrated). ## Documentation @@ -77,10 +79,6 @@ See [this page](./doc/PinetimeStubWithNrf52DK.md) - -DOPENOCD_BIN_PATH=[path to openocd] - * Optionally, you can define MERGEHEX with the path to the ```mergehex``` tool from [NRF5X Command Line Tools](https://infocenter.nordicsemi.com/index.jsp?topic=%2Fug_nrf5x_cltools%2FUG%2Fcltools%2Fnrf5x_command_line_tools_lpage.html&cp=6_1) to be able to merge the application and softdevice into one HEX file. In this case the merged file is generated in src/pinetime-app-full.hex - - - -DMERGEHEX=[Path to the mergehex executable] - JLINK ``` $ mkdir build @@ -116,18 +114,6 @@ $ make -j pinetime-app $ make FLASH_ERASE ``` -* Flash softdevice & application - -``` -$ make FLASH_SOFTDEVICE -$ make FLASH_pinetime-app -``` - -Or, with ```mergehex``` - -``` -$ make FLASH_MERGED_pinetime-app -``` * For your information : list make targets : @@ -208,7 +194,7 @@ $ JLinkRTTClient - https://github.com/eliotstock/memory : display the memory usage (FLASH/RAM) using the .map file from GCC. -## BLE connection, bonding and time synchronization +## BLE connection and time synchronization At runtime, BLE advertising is started. You can then use a smartphone or computer to connect and bond to your Pinetime. As soon as a device is bonded, Pinetime will look for a **CTS** server (**C**urrent **T**ime **S**ervice) on the connected device. @@ -223,7 +209,7 @@ Here is how to do it with an Android smartphone running NRFConnect: - Select server configuration "Current Time Service" and tap OK * Go back to the main screen and scan for BLE devices. A device called "PineTime" should appear * Tap the button "Connect" next to the PineTime device. It should connect to the PineTime and switch to a new tab. -* On this tab, on the top right, there is a 3 dots button. Tap on it and select Bond. The bonding process begins, and if it is sucessful, the PineTime should update its time and display it on the screen. +* If a CTS server is found, the Pinetime should update its time with the time provided by the server. ### Using Linux and bluetoothctl * Ensure that your bluetooth controller is enabled and working fine. I've tested this on a x86 Debian computer and on a RaspberryPi 3. diff --git a/cmake-nRF5x/CMake_nRF5x.cmake b/cmake-nRF5x/CMake_nRF5x.cmake index ec4b47db..c1785d36 100755 --- a/cmake-nRF5x/CMake_nRF5x.cmake +++ b/cmake-nRF5x/CMake_nRF5x.cmake @@ -5,10 +5,6 @@ if (NOT NRF5_SDK_PATH) message(FATAL_ERROR "The path to the nRF5 SDK (NRF5_SDK_PATH) must be set.") endif () -#if (NOT NRFJPROG) -# message(FATAL_ERROR "The path to the nrfjprog utility (NRFJPROG) must be set.") -#endif () - # convert toolchain path to bin path if(DEFINED ARM_NONE_EABI_TOOLCHAIN_PATH) set(ARM_NONE_EABI_TOOLCHAIN_BIN_PATH ${ARM_NONE_EABI_TOOLCHAIN_PATH}/bin) @@ -70,23 +66,15 @@ macro(nRF5x_setup) endif() set(CPU_FLAGS "-mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16") add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 -DNRF52_PAN_12 -DNRF52_PAN_58 -DNRF52_PAN_54 -DNRF52_PAN_31 -DNRF52_PAN_51 -DNRF52_PAN_36 -DNRF52_PAN_15 -DNRF52_PAN_20 -DNRF52_PAN_55 -DBOARD_PCA10040) -# add_definitions(-DSOFTDEVICE_PRESENT -DS132 -DSWI_DISABLE0 -DBLE_STACK_SUPPORT_REQD -DNRF_SD_BLE_API_VERSION=6) add_definitions(-DFREERTOS) add_definitions(-DDEBUG_NRF_USER) -# add_definitions(-D__STARTUP_CLEAR_BSS) -# add_definitions(-D__HEAP_SIZE=8192) -# add_definitions(-D__STACK_SIZE=2048) - include_directories( -# "${NRF5_SDK_PATH}/components/softdevice/s132/headers" -# "${NRF5_SDK_PATH}/components/softdevice/s132/headers/nrf52" "${NRF5_SDK_PATH}/components/drivers_nrf/nrf_soc_nosd" ) list(APPEND SDK_SOURCE_FILES "${NRF5_SDK_PATH}/modules/nrfx/mdk/system_nrf52.c" "${NRF5_SDK_PATH}/modules/nrfx/mdk/gcc_startup_nrf52.S" ) -# set(SOFTDEVICE_PATH "${NRF5_SDK_PATH}/components/softdevice/s132/hex/s132_nrf52_6.1.1_softdevice.hex") endif () set(COMMON_FLAGS "-MP -MD -mthumb -mabi=aapcs -Wall -g3 -ffunction-sections -fdata-sections -fno-strict-aliasing -fno-builtin --short-enums ${CPU_FLAGS} -Wreturn-type -Werror=return-type") @@ -243,7 +231,6 @@ macro(nRF5x_setup) # Other external include_directories( "${NRF5_SDK_PATH}/external/fprintf/" -# "${NRF5_SDK_PATH}/external/utf_converter/" ) list(APPEND SDK_SOURCE_FILES @@ -255,103 +242,25 @@ macro(nRF5x_setup) # LCD/GFX include_directories( "${NRF5_SDK_PATH}/external/thedotfactory_fonts" -# "${NRF5_SDK_PATH}/components/ble/ble_db_discovery" ) - list(APPEND SDK_SOURCE_FILES -# "${NRF5_SDK_PATH}/components/ble/ble_db_discovery/ble_db_discovery.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_cts_c/ble_cts_c.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_ans_c/ble_ans_c.c" -# "${NRF5_SDK_PATH}/external/thedotfactory_fonts/orkney24pts.c" - ) - - #BLE S132 -# include_directories( -# "${NRF5_SDK_PATH}/components/ble/common" -# "${NRF5_SDK_PATH}/components/ble/ble_advertising" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt" -# "${NRF5_SDK_PATH}/components/libraries/sensorsim" -# "${NRF5_SDK_PATH}/components/ble/peer_manager" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr" -# ) - - LIST(APPEND SDK_SOURCE_FILES -# "${NRF5_SDK_PATH}//components/ble/common/ble_srv_common.c" -# "${NRF5_SDK_PATH}/components/ble/ble_advertising/ble_advertising.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_advdata.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_bas/ble_bas.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_hrs/ble_hrs.c" -# "${NRF5_SDK_PATH}/components/ble/ble_services/ble_dis/ble_dis.c" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_gatt/nrf_ble_gatt.c" -# "${NRF5_SDK_PATH}/components/libraries/sensorsim/sensorsim.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" -# "${NRF5_SDK_PATH}/components/ble/nrf_ble_qwr/nrf_ble_qwr.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/auth_status_tracker.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/gatt_cache_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/gatts_cache_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/id_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_data_storage.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_database.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_id.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/peer_manager_handler.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/pm_buffer.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/security_dispatcher.c" -# "${NRF5_SDK_PATH}/components/ble/peer_manager/security_manager.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_params.c" -# "${NRF5_SDK_PATH}/components/ble/common/ble_conn_state.c" -# "${NRF5_SDK_PATH}/components/libraries/atomic_flags/nrf_atflags.c" -# "${NRF5_SDK_PATH}/components/libraries/fds/fds.c" -# "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage.c" -# "${NRF5_SDK_PATH}/components/libraries/fstorage/nrf_fstorage_sd.c" -# "${NRF5_SDK_PATH}/components/libraries/atomic_fifo/nrf_atfifo.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_ble.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_freertos.c" -# "${NRF5_SDK_PATH}/components/softdevice/common/nrf_sdh_soc.c" -# "${NRF5_SDK_PATH}/components/libraries/experimental_section_vars/nrf_section_iter.c" -# "${NRF5_SDK_PATH}/components/libraries/bsp/bsp_btn_ble.c" -# "${NRF5_SDK_PATH}/components/libraries/hardfault/hardfault_implementation.c" -# "${NRF5_SDK_PATH}/components/libraries/hardfault/nrf52/handler/hardfault_handler_gcc.c" - ) - LIST(APPEND SDK_SOURCE_FILES "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_twi.c" ) - # adds target for erasing and flashing the board with a softdevice + # adds target for erasing if(USE_JLINK) - add_custom_target(FLASH_SOFTDEVICE - COMMAND ${NRFJPROG} --program ${SOFTDEVICE_PATH} -f ${NRF_TARGET} --sectorerase - COMMAND sleep 0.5s - COMMAND ${NRFJPROG} --reset -f ${NRF_TARGET} - COMMENT "flashing SoftDevice" - ) - add_custom_target(FLASH_ERASE COMMAND ${NRFJPROG} --eraseall -f ${NRF_TARGET} COMMENT "erasing flashing" ) elseif(USE_GDB_CLIENT) - add_custom_target(FLASH_SOFTDEVICE - COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'load' -ex 'kill' ${SOFTDEVICE_PATH} - COMMENT "flashing SoftDevice" - ) add_custom_target(FLASH_ERASE COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'mon erase_mass' COMMENT "erasing flashing" ) elseif(USE_OPENOCD) - add_custom_target(FLASH_SOFTDEVICE - COMMAND ${OPENOCD_BIN_PATH} -c "tcl_port disabled" -c "gdb_port 3333" -c "telnet_port 4444" -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c "program \"${SOFTDEVICE_PATH}\"" -c reset -c shutdown - COMMENT "flashing SoftDevice" - ) - add_custom_target(FLASH_ERASE + add_custom_target(FLASH_ERASE COMMAND ${OPENOCD_BIN_PATH} -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c init -c halt -c 'nrf5 mass_erase' -c reset -c shutdown COMMENT "erasing flashing" ) @@ -392,35 +301,6 @@ macro(nRF5x_addExecutable EXECUTABLE_NAME SOURCE_FILES) COMMAND ${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_NAME}.out "${EXECUTABLE_NAME}.hex" COMMENT "post build steps for ${EXECUTABLE_NAME}") - if(MERGEHEX) - add_custom_command(TARGET ${EXECUTABLE_NAME} - POST_BUILD - COMMAND ${MERGEHEX} --merge ${EXECUTABLE_NAME}.hex ${NRF5_SDK_PATH}/components/softdevice/s132/hex/s132_nrf52_6.1.1_softdevice.hex --output ${EXECUTABLE_NAME}-full.hex - COMMENT "merging HEX files") - - if(USE_JLINK) - add_custom_target("FLASH_MERGED_${EXECUTABLE_NAME}" - DEPENDS ${EXECUTABLE_NAME} - COMMAND ${NRFJPROG} --program ${EXECUTABLE_NAME}-full.hex -f ${NRF_TARGET} --sectorerase - COMMAND sleep 0.5s - COMMAND ${NRFJPROG} --reset -f ${NRF_TARGET} - COMMENT "flashing ${EXECUTABLE_NAME}-full.hex" - ) - elseif(USE_GDB_CLIENT) - add_custom_target("FLASH_MERGED_${EXECUTABLE_NAME}" - DEPENDS ${EXECUTABLE_NAME} - COMMAND ${GDB_CLIENT_BIN_PATH} -nx --batch -ex 'target extended-remote ${GDB_CLIENT_TARGET_REMOTE}' -ex 'monitor swdp_scan' -ex 'attach 1' -ex 'load' -ex 'kill' ${EXECUTABLE_NAME}-full.hex - COMMENT "flashing ${EXECUTABLE_NAME}-full.hex" - ) - elseif(USE_OPENOCD) - add_custom_target("FLASH_MERGED_${EXECUTABLE_NAME}" - DEPENDS ${EXECUTABLE_NAME} - COMMAND ${OPENOCD_BIN_PATH} -c "tcl_port disabled" -c "gdb_port 3333" -c "telnet_port 4444" -f interface/stlink.cfg -c 'transport select hla_swd' -f target/nrf52.cfg -c "program \"${EXECUTABLE_NAME}-full.hex\"" -c reset -c shutdown - COMMENT "flashing ${EXECUTABLE_NAME}-full.hex" - ) - endif() - endif() - # custom target for flashing the board if(USE_JLINK) add_custom_target("FLASH_${EXECUTABLE_NAME}" diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp index 172bcb9f..2fe03571 100644 --- a/src/Components/Ble/NimbleController.cpp +++ b/src/Components/Ble/NimbleController.cpp @@ -126,14 +126,21 @@ void NimbleController::StartAdvertising() { int res; res = ble_gap_adv_set_fields(&fields); - ASSERT(res == 0); + //ASSERT(res == 0); res = ble_gap_adv_rsp_set_fields(&rsp_fields); - ASSERT(res == 0); + //ASSERT(res == 0); res = ble_gap_adv_start(addrType, NULL, 10000, &adv_params, GAPEventCallback, this); - ASSERT(res == 0); + //ASSERT(res == 0); + + // TODO I've disabled these ASSERT as they sometime asserts and reset the mcu. + // For now, the advertising is restarted as soon as it ends. There may be a race condition + // that prevent the advertising from restarting reliably. + // I remove the assert to prevent this uncesseray crash, but in the long term, the management of + // the advertising should be improve (better error handling, and advertise for 3 minutes after + // the application has been woken up, for example. } int OnAllSvrDisco(uint16_t conn_handle, diff --git a/src/main.cpp b/src/main.cpp index e0e9b65e..797495bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -195,7 +195,6 @@ void nimble_port_init(void) { } void nimble_port_ll_task_func(void *args) { -// extern void ble_ll_task(void *arg); ble_ll_task(args); } } -- cgit v1.2.3