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/CMakeLists.txt | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) (limited to 'src/CMakeLists.txt') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e92e3998..59448be8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,11 +23,100 @@ nRF5x_addAppGpiote() add_definitions(-DCONFIG_GPIO_AS_PINRESET) add_definitions(-DDEBUG) - +add_definitions(-DNIMBLE_CFG_CONTROLLER) +add_definitions(-DOS_CPUTIME_FREQ) include_directories(.) include_directories(libs/) +set(TINYCRYPT_SRC + libs/mynewt-nimble/ext/tinycrypt/src/aes_encrypt.c + libs/mynewt-nimble/ext/tinycrypt/src/utils.c + ) + +set(NIMBLE_SRC + libs/mynewt-nimble/porting/npl/freertos/src/nimble_port_freertos.c + libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c + + + + libs/mynewt-nimble/nimble/host/src/ble_hs.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci_evt.c + libs/mynewt-nimble/nimble/host/src/ble_l2cap_sig_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_l2cap_sig.c + libs/mynewt-nimble/nimble/host/src/ble_l2cap.c + libs/mynewt-nimble/nimble/host/src/ble_hs_mbuf.c + libs/mynewt-nimble/nimble/host/src/ble_sm.c + libs/mynewt-nimble/nimble/host/src/ble_gap.c + libs/mynewt-nimble/nimble/host/src/ble_gatts.c + libs/mynewt-nimble/nimble/host/src/ble_gattc.c + 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_hs_pvcy.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci.c + libs/mynewt-nimble/nimble/host/src/ble_hs_log.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci_util.c + libs/mynewt-nimble/nimble/host/src/ble_hs_hci_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_hs_cfg.c + libs/mynewt-nimble/nimble/host/src/ble_uuid.c + libs/mynewt-nimble/nimble/host/src/ble_hs_id.c + libs/mynewt-nimble/nimble/host/src/ble_hs_misc.c + libs/mynewt-nimble/nimble/host/src/ble_att.c + libs/mynewt-nimble/nimble/host/src/ble_att_clt.c + libs/mynewt-nimble/nimble/host/src/ble_att_svr.c + libs/mynewt-nimble/nimble/host/src/ble_att_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_hs_atomic.c + libs/mynewt-nimble/nimble/host/src/ble_hs_adv.c + libs/mynewt-nimble/nimble/host/src/ble_hs_flow.c + libs/mynewt-nimble/nimble/host/src/ble_sm.c + libs/mynewt-nimble/nimble/host/src/ble_sm_cmd.c + libs/mynewt-nimble/nimble/host/src/ble_sm_lgcy.c + libs/mynewt-nimble/nimble/host/src/ble_sm_alg.c + libs/mynewt-nimble/nimble/host/src/ble_hs_mqueue.c + libs/mynewt-nimble/nimble/host/src/ble_hs_stop.c + libs/mynewt-nimble/nimble/host/src/ble_hs_startup.c + libs/mynewt-nimble/nimble/host/store/ram/src/ble_store_ram.c + + libs/mynewt-nimble/nimble/transport/ram/src/ble_hci_ram.c + + + + libs/mynewt-nimble/nimble/controller/src/ble_ll.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_rand.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_conn.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_ctrl.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_hci.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_conn_hci.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_utils.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_scan.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_whitelist.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_adv.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_sched.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_supp_cmd.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_hci_ev.c + libs/mynewt-nimble/nimble/controller/src/ble_ll_rfmgmt.c + + + + libs/mynewt-nimble/porting/nimble/src/os_cputime.c + libs/mynewt-nimble/porting/nimble/src/os_cputime_pwr2.c + libs/mynewt-nimble/porting/nimble/src/os_mbuf.c + libs/mynewt-nimble/porting/nimble/src/os_mempool.c + libs/mynewt-nimble/porting/nimble/src/hal_timer.c + libs/mynewt-nimble/porting/nimble/src/mem.c + libs/mynewt-nimble/porting/nimble/src/endian.c + libs/mynewt-nimble/porting/nimble/src/os_msys_init.c + + libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_hw.c + libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c + + libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c + libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c + + libs/mynewt-nimble/nimble/host/util/src/addr.c + ) + set(LVGL_SRC libs/lv_conf.h libs/lvgl/lvgl.h @@ -230,6 +319,8 @@ list(APPEND SOURCE_FILES FreeRTOS/port.c FreeRTOS/port_cmsis_systick.c FreeRTOS/port_cmsis.c + ${TINYCRYPT_SRC} + ${NIMBLE_SRC} ${LVGL_SRC} ${IMAGE_FILES} @@ -288,6 +379,18 @@ set(INCLUDE_FILES include_directories( FreeRTOS/ libs/date/includes + libs/mynewt-nimble/porting/npl/freertos/include + libs/mynewt-nimble/nimble/include + libs/mynewt-nimble/porting/nimble/include + libs/mynewt-nimble/nimble/host/include + libs/mynewt-nimble/nimble/controller/include + libs/mynewt-nimble/nimble/transport/ram/include + libs/mynewt-nimble/nimble/drivers/nrf52/include + libs/mynewt-nimble/ext/tinycrypt/include + libs/mynewt-nimble/nimble/host/services/gap/include + libs/mynewt-nimble/nimble/host/services/gatt/include + libs/mynewt-nimble/nimble/host/util/include + libs/mynewt-nimble/nimble/host/store/ram/include ) link_directories( -- cgit v1.2.3 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/CMakeLists.txt') 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 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/CMakeLists.txt') 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/CMakeLists.txt') 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/CMakeLists.txt') 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 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/CMakeLists.txt') 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 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/CMakeLists.txt') 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 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/CMakeLists.txt') 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/CMakeLists.txt') 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/CMakeLists.txt') 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