From dd6aecbf6b343e40f75808f5e26a077eb22a2ed2 Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 19 Apr 2020 20:44:59 +0200 Subject: Integration of nimble, work in progress. Advertising is working. --- src/SystemTask/SystemTask.cpp | 90 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 6 deletions(-) (limited to 'src/SystemTask') diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index e65abb61..59f0a92f 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -4,9 +4,14 @@ #include #include #include -#include #include +#include +#include +#include #include "SystemTask.h" +#include +#include +#include #include "../main.h" using namespace Pinetime::System; @@ -32,14 +37,87 @@ void SystemTask::Process(void *instance) { app->Work(); } +static int _gap_event_cb(struct ble_gap_event *event, void *arg) +{ + return 0; +} + +static int +adv_event(struct ble_gap_event *event, void *arg) +{ + switch (event->type) { + case BLE_GAP_EVENT_ADV_COMPLETE: + return 0; + case BLE_GAP_EVENT_CONNECT: + return 0; + case BLE_GAP_EVENT_DISCONNECT: + return 0; + default: + return 0; + } +} + void SystemTask::Work() { - watchdog.Setup(7); - watchdog.Start(); +// watchdog.Setup(7); +// watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); - bool erase_bonds=true; - ble_manager_init_peer_manager(); - nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); +// bool erase_bonds=true; +// ble_manager_init_peer_manager(); +// nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); +/* BLE */ + while (!ble_hs_synced()) {} + + int res; + res = ble_hs_util_ensure_addr(0); + assert(res == 0); + uint8_t addrType; + res = ble_hs_id_infer_auto(0, &addrType); + assert(res == 0); + + res = ble_svc_gap_device_name_set("Pinetime-JF"); + assert(res == 0); + + + /* 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; + + 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, 36000, + &adv_params, adv_event, NULL); + assert(res == 0); + +/* /BLE*/ spi.Init(); lcd.Init(); -- cgit v1.2.3-70-g09d2 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/CMakeLists.txt | 3 + src/Components/Ble/NimbleController.cpp | 148 ++++++++++++++++++++++++++++++++ src/Components/Ble/NimbleController.h | 19 ++++ src/SystemTask/SystemTask.cpp | 79 +---------------- src/SystemTask/SystemTask.h | 2 + src/main.cpp | 4 +- 6 files changed, 177 insertions(+), 78 deletions(-) create mode 100644 src/Components/Ble/NimbleController.cpp create mode 100644 src/Components/Ble/NimbleController.h (limited to 'src/SystemTask') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 59448be8..f16ee0c0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -53,6 +53,7 @@ set(NIMBLE_SRC libs/mynewt-nimble/nimble/host/src/ble_hs_conn.c libs/mynewt-nimble/nimble/host/src/ble_att_svr.c libs/mynewt-nimble/nimble/host/src/ble_store.c + libs/mynewt-nimble/nimble/host/src/ble_store_util.c libs/mynewt-nimble/nimble/host/src/ble_hs_pvcy.c libs/mynewt-nimble/nimble/host/src/ble_hs_hci.c libs/mynewt-nimble/nimble/host/src/ble_hs_log.c @@ -315,6 +316,7 @@ list(APPEND SOURCE_FILES Components/Ble/NotificationManager.cpp Components/DateTime/DateTimeController.cpp Components/Brightness/BrightnessController.cpp + Components/Ble/NimbleController.cpp drivers/Cst816s.cpp FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c @@ -359,6 +361,7 @@ set(INCLUDE_FILES Components/Ble/NotificationManager.h Components/DateTime/DateTimeController.h Components/Brightness/BrightnessController.h + Components/Ble/NimbleController.h drivers/Cst816s.h FreeRTOS/portmacro.h FreeRTOS/portmacro_cmsis.h 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; +} + diff --git a/src/Components/Ble/NimbleController.h b/src/Components/Ble/NimbleController.h new file mode 100644 index 00000000..1901b14e --- /dev/null +++ b/src/Components/Ble/NimbleController.h @@ -0,0 +1,19 @@ +#pragma once + +#include +#include + +namespace Pinetime { + namespace Controllers { + + class NimbleController { + public: + void Init(); + void StartAdvertising(); + int OnGAPEvent(ble_gap_event *event); + private: + static constexpr char* deviceName = "Pinetime-JF"; + uint8_t addrType; + }; + } +} diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 59f0a92f..ae031a1e 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include "SystemTask.h" @@ -13,6 +12,7 @@ #include #include #include "../main.h" + using namespace Pinetime::System; SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, Drivers::Cst816S &touchPanel, @@ -37,86 +37,15 @@ void SystemTask::Process(void *instance) { app->Work(); } -static int _gap_event_cb(struct ble_gap_event *event, void *arg) -{ - return 0; -} - -static int -adv_event(struct ble_gap_event *event, void *arg) -{ - switch (event->type) { - case BLE_GAP_EVENT_ADV_COMPLETE: - return 0; - case BLE_GAP_EVENT_CONNECT: - return 0; - case BLE_GAP_EVENT_DISCONNECT: - return 0; - default: - return 0; - } -} - void SystemTask::Work() { // watchdog.Setup(7); // watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); -// bool erase_bonds=true; -// ble_manager_init_peer_manager(); -// nrf_sdh_freertos_init(ble_manager_start_advertising, &erase_bonds); -/* BLE */ - while (!ble_hs_synced()) {} - - int res; - res = ble_hs_util_ensure_addr(0); - assert(res == 0); - uint8_t addrType; - res = ble_hs_id_infer_auto(0, &addrType); - assert(res == 0); - - res = ble_svc_gap_device_name_set("Pinetime-JF"); - assert(res == 0); - - - /* 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; - - 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, 36000, - &adv_params, adv_event, NULL); - assert(res == 0); +/* BLE */ + nimbleController.Init(); + nimbleController.StartAdvertising(); /* /BLE*/ spi.Init(); diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index a1ba277a..5eba391b 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace Pinetime { namespace System { @@ -44,6 +45,7 @@ namespace Pinetime { Pinetime::Drivers::Watchdog watchdog; Pinetime::Drivers::WatchdogView watchdogView; Pinetime::Controllers::NotificationManager& notificationManager; + Pinetime::Controllers::NimbleController nimbleController; static constexpr uint8_t pinSpiSck = 2; diff --git a/src/main.cpp b/src/main.cpp index f4c5c60a..e3226929 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #if NRF_LOG_ENABLED @@ -242,8 +241,7 @@ int main(void) { systemTask->Start(); nimble_port_init(); - ble_svc_gap_init(); - ble_svc_gatt_init(); + // ble_manager_init(); // ble_manager_set_new_time_callback(OnNewTime); -- cgit v1.2.3-70-g09d2 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/SystemTask') 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-70-g09d2 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/SystemTask') 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-70-g09d2 From be67b5297d63dfd0d0c8d732c3e2bce40af4e547 Mon Sep 17 00:00:00 2001 From: Adam Pigg Date: Mon, 27 Apr 2020 14:41:08 +0100 Subject: Remove references to BLE Manager --- src/CMakeLists.txt | 1 - src/SystemTask/SystemTask.cpp | 1 - src/main.cpp | 1 - 3 files changed, 3 deletions(-) (limited to 'src/SystemTask') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 93878e85..ad83caee 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -310,7 +310,6 @@ list(APPEND SOURCE_FILES drivers/SpiMaster.cpp drivers/Watchdog.cpp drivers/DebugPins.cpp - BLE/BleManager.c Components/Battery/BatteryController.cpp Components/Ble/BleController.cpp Components/Ble/NotificationManager.cpp diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 5dceb89b..a003163c 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include diff --git a/src/main.cpp b/src/main.cpp index e3226929..9748f533 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,7 +9,6 @@ #include //#include #include -#include "BLE/BleManager.h" #include "Components/Battery/BatteryController.h" #include "Components/Ble/BleController.h" #include -- cgit v1.2.3-70-g09d2 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/SystemTask') 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-70-g09d2 From 833c53424a211f34774fa210be3eb96ad4f35210 Mon Sep 17 00:00:00 2001 From: JF Date: Mon, 27 Apr 2020 20:46:25 +0200 Subject: Re-enable watchdog --- src/SystemTask/SystemTask.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/SystemTask') diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 6516f68b..fc37ecb2 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -38,8 +38,8 @@ void SystemTask::Process(void *instance) { } void SystemTask::Work() { -// watchdog.Setup(7); -// watchdog.Start(); + watchdog.Setup(7); + watchdog.Start(); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); APP_GPIOTE_INIT(2); -- cgit v1.2.3-70-g09d2