2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/NimbleController.h"
|
2021-10-30 14:02:39 -04:00
|
|
|
#include <cstring>
|
|
|
|
|
2020-11-15 09:05:51 -05:00
|
|
|
#include <hal/nrf_rtc.h>
|
|
|
|
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
|
|
|
#define max
|
2020-04-19 15:26:09 -04:00
|
|
|
#include <host/ble_gap.h>
|
2020-11-15 09:05:51 -05:00
|
|
|
#include <host/ble_hs.h>
|
|
|
|
#include <host/ble_hs_id.h>
|
|
|
|
#include <host/util/util.h>
|
2021-10-30 14:02:39 -04:00
|
|
|
#include <controller/ble_ll.h>
|
2021-12-04 15:49:49 -05:00
|
|
|
#include <controller/ble_hw.h>
|
2020-11-15 09:05:51 -05:00
|
|
|
#undef max
|
|
|
|
#undef min
|
|
|
|
#include <services/gap/ble_svc_gap.h>
|
|
|
|
#include <services/gatt/ble_svc_gatt.h>
|
|
|
|
#include "components/ble/BleController.h"
|
|
|
|
#include "components/ble/NotificationManager.h"
|
|
|
|
#include "components/datetime/DateTimeController.h"
|
2021-12-04 15:49:49 -05:00
|
|
|
#include "components/fs/FS.h"
|
2020-11-15 09:05:51 -05:00
|
|
|
#include "systemtask/SystemTask.h"
|
2020-04-25 07:09:47 -04:00
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2020-04-25 07:09:47 -04:00
|
|
|
NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
|
2020-04-27 14:13:27 -04:00
|
|
|
Pinetime::Controllers::Ble& bleController,
|
2021-04-18 13:28:14 -04:00
|
|
|
DateTime& dateTimeController,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager,
|
|
|
|
Controllers::Battery& batteryController,
|
|
|
|
Pinetime::Drivers::SpiNorFlash& spiNorFlash,
|
2021-10-17 02:23:44 -04:00
|
|
|
Controllers::HeartRateController& heartRateController,
|
2021-12-04 15:49:49 -05:00
|
|
|
Controllers::MotionController& motionController,
|
|
|
|
Pinetime::Controllers::FS& fs)
|
2021-04-18 13:28:14 -04:00
|
|
|
: systemTask {systemTask},
|
|
|
|
bleController {bleController},
|
|
|
|
dateTimeController {dateTimeController},
|
|
|
|
notificationManager {notificationManager},
|
|
|
|
spiNorFlash {spiNorFlash},
|
|
|
|
dfuService {systemTask, bleController, spiNorFlash},
|
|
|
|
currentTimeClient {dateTimeController},
|
|
|
|
anService {systemTask, notificationManager},
|
|
|
|
alertNotificationClient {systemTask, notificationManager},
|
|
|
|
currentTimeService {dateTimeController},
|
|
|
|
musicService {systemTask},
|
|
|
|
navService {systemTask},
|
|
|
|
batteryInformationService {batteryController},
|
|
|
|
immediateAlertService {systemTask, notificationManager},
|
|
|
|
heartRateService {systemTask, heartRateController},
|
2021-12-04 15:49:49 -05:00
|
|
|
fs {fs},
|
|
|
|
motionService {systemTask, motionController},
|
2021-04-18 13:28:14 -04:00
|
|
|
serviceDiscovery({¤tTimeClient, &alertNotificationClient}) {
|
2020-04-22 14:19:36 -04:00
|
|
|
}
|
|
|
|
|
2021-08-29 16:50:04 -04:00
|
|
|
void nimble_on_reset(int reason) {
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Nimble lost sync, resetting state; reason=%d", reason);
|
2021-08-29 16:50:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nimble_on_sync(void) {
|
2021-10-30 14:02:39 -04:00
|
|
|
int rc;
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Nimble is synced");
|
|
|
|
|
|
|
|
rc = ble_hs_util_ensure_addr(0);
|
|
|
|
ASSERT(rc == 0);
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2021-10-30 14:02:39 -04:00
|
|
|
nptr->StartAdvertising();
|
2021-08-29 16:50:04 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int GAPEventCallback(struct ble_gap_event* event, void* arg) {
|
2020-04-19 15:26:09 -04:00
|
|
|
auto nimbleController = static_cast<NimbleController*>(arg);
|
|
|
|
return nimbleController->OnGAPEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NimbleController::Init() {
|
2021-04-18 13:28:14 -04:00
|
|
|
while (!ble_hs_synced()) {
|
|
|
|
}
|
2020-04-19 15:26:09 -04:00
|
|
|
|
2021-08-29 16:50:04 -04:00
|
|
|
nptr = this;
|
|
|
|
ble_hs_cfg.reset_cb = nimble_on_reset;
|
|
|
|
ble_hs_cfg.sync_cb = nimble_on_sync;
|
2021-10-30 14:02:39 -04:00
|
|
|
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
ble_svc_gap_init();
|
|
|
|
ble_svc_gatt_init();
|
|
|
|
|
2020-04-23 14:34:38 -04:00
|
|
|
deviceInformationService.Init();
|
2020-04-23 14:57:53 -04:00
|
|
|
currentTimeClient.Init();
|
2020-05-04 16:43:51 -04:00
|
|
|
currentTimeService.Init();
|
2020-07-11 16:37:28 -04:00
|
|
|
musicService.Init();
|
2021-01-20 15:34:09 -05:00
|
|
|
navService.Init();
|
2020-04-28 13:39:26 -04:00
|
|
|
anService.Init();
|
2020-04-27 14:16:03 -04:00
|
|
|
dfuService.Init();
|
2020-09-27 14:02:47 -04:00
|
|
|
batteryInformationService.Init();
|
2020-09-27 14:59:06 -04:00
|
|
|
immediateAlertService.Init();
|
2021-01-17 10:34:14 -05:00
|
|
|
heartRateService.Init();
|
2021-10-17 02:23:44 -04:00
|
|
|
motionService.Init();
|
2021-08-29 16:50:04 -04:00
|
|
|
|
|
|
|
int rc;
|
|
|
|
rc = ble_hs_util_ensure_addr(0);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
rc = ble_hs_id_infer_auto(0, &addrType);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
rc = ble_svc_gap_device_name_set(deviceName);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
rc = ble_svc_gap_device_appearance_set(0xC2);
|
|
|
|
ASSERT(rc == 0);
|
2020-06-08 15:51:34 -04:00
|
|
|
Pinetime::Controllers::Ble::BleAddress address;
|
2021-08-29 16:50:04 -04:00
|
|
|
rc = ble_hs_id_copy_addr(addrType, address.data(), nullptr);
|
|
|
|
ASSERT(rc == 0);
|
2021-10-30 14:02:39 -04:00
|
|
|
|
2020-06-08 15:51:34 -04:00
|
|
|
bleController.Address(std::move(address));
|
2021-10-30 14:02:39 -04:00
|
|
|
switch (addrType) {
|
|
|
|
case BLE_OWN_ADDR_PUBLIC:
|
|
|
|
bleController.AddressType(Ble::AddressTypes::Public);
|
|
|
|
break;
|
|
|
|
case BLE_OWN_ADDR_RANDOM:
|
|
|
|
bleController.AddressType(Ble::AddressTypes::Random);
|
|
|
|
break;
|
|
|
|
case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT:
|
|
|
|
bleController.AddressType(Ble::AddressTypes::RPA_Public);
|
|
|
|
break;
|
|
|
|
case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:
|
|
|
|
bleController.AddressType(Ble::AddressTypes::RPA_Random);
|
|
|
|
break;
|
|
|
|
}
|
2020-06-08 15:51:34 -04:00
|
|
|
|
2021-08-29 16:50:04 -04:00
|
|
|
rc = ble_gatts_start();
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
|
2021-12-04 15:49:49 -05:00
|
|
|
RestoreBond();
|
|
|
|
|
2021-08-29 16:50:04 -04:00
|
|
|
if (!ble_gap_adv_active() && !bleController.IsConnected())
|
|
|
|
StartAdvertising();
|
2020-04-19 15:26:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void NimbleController::StartAdvertising() {
|
|
|
|
struct ble_gap_adv_params adv_params;
|
|
|
|
struct ble_hs_adv_fields fields;
|
|
|
|
struct ble_hs_adv_fields rsp_fields;
|
|
|
|
|
|
|
|
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;
|
2021-09-05 16:53:20 -04:00
|
|
|
/* fast advertise for 30 sec */
|
|
|
|
if (fastAdvCount < 15) {
|
|
|
|
adv_params.itvl_min = 32;
|
|
|
|
adv_params.itvl_max = 47;
|
|
|
|
fastAdvCount++;
|
|
|
|
} else {
|
|
|
|
adv_params.itvl_min = 1636;
|
|
|
|
adv_params.itvl_max = 1651;
|
|
|
|
}
|
2020-04-19 15:26:09 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP;
|
2020-04-27 14:16:03 -04:00
|
|
|
fields.uuids128 = &dfuServiceUuid;
|
|
|
|
fields.num_uuids128 = 1;
|
|
|
|
fields.uuids128_is_complete = 1;
|
2020-04-19 15:26:09 -04:00
|
|
|
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
|
|
|
|
|
2021-10-30 14:02:39 -04:00
|
|
|
rsp_fields.name = reinterpret_cast<const uint8_t*>(deviceName);
|
2020-09-20 05:09:50 -04:00
|
|
|
rsp_fields.name_len = strlen(deviceName);
|
2020-04-19 15:26:09 -04:00
|
|
|
rsp_fields.name_is_complete = 1;
|
|
|
|
|
2021-10-30 14:02:39 -04:00
|
|
|
int rc;
|
2021-08-29 16:50:04 -04:00
|
|
|
rc = ble_gap_adv_set_fields(&fields);
|
|
|
|
ASSERT(rc == 0);
|
2020-04-19 15:26:09 -04:00
|
|
|
|
2021-08-29 16:50:04 -04:00
|
|
|
rc = ble_gap_adv_rsp_set_fields(&rsp_fields);
|
|
|
|
ASSERT(rc == 0);
|
2020-04-19 15:26:09 -04:00
|
|
|
|
2021-09-05 16:53:20 -04:00
|
|
|
rc = ble_gap_adv_start(addrType, NULL, 2000, &adv_params, GAPEventCallback, this);
|
2021-08-29 16:50:04 -04:00
|
|
|
ASSERT(rc == 0);
|
2020-04-19 15:26:09 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int NimbleController::OnGAPEvent(ble_gap_event* event) {
|
2020-04-19 15:26:09 -04:00
|
|
|
switch (event->type) {
|
|
|
|
case BLE_GAP_EVENT_ADV_COMPLETE:
|
|
|
|
NRF_LOG_INFO("Advertising event : BLE_GAP_EVENT_ADV_COMPLETE");
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("reason=%d; status=%0X", event->adv_complete.reason, event->connect.status);
|
2021-08-29 16:50:04 -04:00
|
|
|
StartAdvertising();
|
2020-04-19 15:26:09 -04:00
|
|
|
break;
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2021-09-01 23:50:56 -04:00
|
|
|
case BLE_GAP_EVENT_CONNECT:
|
2020-04-19 15:26:09 -04:00
|
|
|
/* A new connection was established or a connection attempt failed. */
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Connect event : BLE_GAP_EVENT_CONNECT");
|
|
|
|
NRF_LOG_INFO("connection %s; status=%0X ", event->connect.status == 0 ? "established" : "failed", event->connect.status);
|
2020-04-19 15:26:09 -04:00
|
|
|
|
|
|
|
if (event->connect.status != 0) {
|
|
|
|
/* Connection failed; resume advertising. */
|
2021-08-31 00:17:16 -04:00
|
|
|
currentTimeClient.Reset();
|
|
|
|
alertNotificationClient.Reset();
|
|
|
|
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
|
2020-04-27 14:13:27 -04:00
|
|
|
bleController.Disconnect();
|
2021-09-05 16:53:20 -04:00
|
|
|
fastAdvCount = 0;
|
2021-08-29 16:50:04 -04:00
|
|
|
StartAdvertising();
|
2020-04-22 14:19:36 -04:00
|
|
|
} else {
|
2021-08-29 16:50:04 -04:00
|
|
|
connectionHandle = event->connect.conn_handle;
|
2020-04-27 14:13:27 -04:00
|
|
|
bleController.Connect();
|
2021-06-06 09:56:03 -04:00
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::BleConnected);
|
2021-08-29 16:50:04 -04:00
|
|
|
// Service discovery is deferred via systemtask
|
2020-04-19 15:26:09 -04:00
|
|
|
}
|
2021-09-01 23:50:56 -04:00
|
|
|
break;
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
case BLE_GAP_EVENT_DISCONNECT:
|
|
|
|
/* Connection terminated; resume advertising. */
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Disconnect event : BLE_GAP_EVENT_DISCONNECT");
|
|
|
|
NRF_LOG_INFO("disconnect reason=%d", event->disconnect.reason);
|
2021-12-04 15:49:49 -05:00
|
|
|
|
|
|
|
if (event->disconnect.conn.sec_state.bonded)
|
|
|
|
PersistBond(event->disconnect.conn);
|
|
|
|
|
2020-10-28 13:23:09 -04:00
|
|
|
currentTimeClient.Reset();
|
|
|
|
alertNotificationClient.Reset();
|
2020-05-01 15:58:31 -04:00
|
|
|
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
|
2020-04-27 14:13:27 -04:00
|
|
|
bleController.Disconnect();
|
2021-09-05 16:53:20 -04:00
|
|
|
fastAdvCount = 0;
|
2020-04-19 15:26:09 -04:00
|
|
|
StartAdvertising();
|
|
|
|
break;
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
case BLE_GAP_EVENT_CONN_UPDATE:
|
|
|
|
/* The central has updated the connection parameters. */
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Update event : BLE_GAP_EVENT_CONN_UPDATE");
|
|
|
|
NRF_LOG_INFO("update status=%0X ", event->conn_update.status);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_GAP_EVENT_CONN_UPDATE_REQ:
|
|
|
|
/* The central has requested updated connection parameters */
|
|
|
|
NRF_LOG_INFO("Update event : BLE_GAP_EVENT_CONN_UPDATE_REQ");
|
|
|
|
NRF_LOG_INFO("update request : itvl_min=%d itvl_max=%d latency=%d supervision=%d",
|
|
|
|
event->conn_update_req.peer_params->itvl_min,
|
|
|
|
event->conn_update_req.peer_params->itvl_max,
|
|
|
|
event->conn_update_req.peer_params->latency,
|
|
|
|
event->conn_update_req.peer_params->supervision_timeout);
|
2020-04-19 15:26:09 -04:00
|
|
|
break;
|
2021-08-29 16:50:04 -04:00
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
case BLE_GAP_EVENT_ENC_CHANGE:
|
|
|
|
/* Encryption has been enabled or disabled for this connection. */
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Security event : BLE_GAP_EVENT_ENC_CHANGE");
|
|
|
|
NRF_LOG_INFO("encryption change event; status=%0X ", event->enc_change.status);
|
2021-12-04 15:49:49 -05:00
|
|
|
|
|
|
|
if (event->enc_change.status == 0) {
|
|
|
|
struct ble_gap_conn_desc desc;
|
|
|
|
ble_gap_conn_find(event->enc_change.conn_handle, &desc);
|
|
|
|
if (desc.sec_state.bonded)
|
|
|
|
PersistBond(desc);
|
|
|
|
|
|
|
|
NRF_LOG_INFO("new state: encrypted=%d authenticated=%d bonded=%d key_size=%d",
|
|
|
|
desc.sec_state.encrypted,
|
|
|
|
desc.sec_state.authenticated,
|
|
|
|
desc.sec_state.bonded,
|
|
|
|
desc.sec_state.key_size);
|
|
|
|
}
|
2021-10-30 14:02:39 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_GAP_EVENT_PASSKEY_ACTION:
|
|
|
|
/* Authentication has been requested for this connection.
|
|
|
|
* Standards insist that the rand() PRNG be deterministic.
|
|
|
|
* Use the nimble TRNG since rand() is predictable.
|
|
|
|
*/
|
|
|
|
NRF_LOG_INFO("Security event : BLE_GAP_EVENT_PASSKEY_ACTION");
|
|
|
|
if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
|
|
|
|
struct ble_sm_io pkey = {0};
|
|
|
|
pkey.action = event->passkey.params.action;
|
|
|
|
pkey.passkey = ble_ll_rand() % 1000000;
|
|
|
|
bleController.SetPairingKey(pkey.passkey);
|
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::OnPairing);
|
|
|
|
ble_sm_inject_io(event->passkey.conn_handle, &pkey);
|
|
|
|
}
|
2021-08-29 16:50:04 -04:00
|
|
|
break;
|
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
case BLE_GAP_EVENT_SUBSCRIBE:
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Subscribe event; conn_handle=%d attr_handle=%d "
|
2021-04-18 13:28:14 -04:00
|
|
|
"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);
|
2021-10-17 02:23:01 -04:00
|
|
|
|
2021-12-04 15:49:49 -05:00
|
|
|
if (event->subscribe.reason == BLE_GAP_SUBSCRIBE_REASON_TERM) {
|
2021-10-17 02:23:01 -04:00
|
|
|
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
2021-10-17 02:42:49 -04:00
|
|
|
motionService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
2021-12-04 15:49:49 -05:00
|
|
|
} else if (event->subscribe.prev_notify == 0 && event->subscribe.cur_notify == 1) {
|
2021-10-17 02:23:01 -04:00
|
|
|
heartRateService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
2021-10-17 02:42:49 -04:00
|
|
|
motionService.SubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
2021-12-04 15:49:49 -05:00
|
|
|
} else if (event->subscribe.prev_notify == 1 && event->subscribe.cur_notify == 0) {
|
2021-10-17 02:23:01 -04:00
|
|
|
heartRateService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
2021-10-17 02:42:49 -04:00
|
|
|
motionService.UnsubscribeNotification(event->subscribe.conn_handle, event->subscribe.attr_handle);
|
2021-10-17 02:23:01 -04:00
|
|
|
}
|
2021-08-29 16:50:04 -04:00
|
|
|
break;
|
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
case BLE_GAP_EVENT_MTU:
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("MTU Update event; conn_handle=%d cid=%d mtu=%d", event->mtu.conn_handle, event->mtu.channel_id, event->mtu.value);
|
2021-08-29 16:50:04 -04:00
|
|
|
break;
|
2020-04-19 15:26:09 -04:00
|
|
|
|
|
|
|
case BLE_GAP_EVENT_REPEAT_PAIRING: {
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("Pairing event : BLE_GAP_EVENT_REPEAT_PAIRING");
|
2020-04-19 15:26:09 -04:00
|
|
|
/* 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;
|
2020-04-25 07:09:47 -04:00
|
|
|
|
|
|
|
case BLE_GAP_EVENT_NOTIFY_RX: {
|
|
|
|
/* Peer sent us a notification or indication. */
|
2021-10-30 14:02:39 -04:00
|
|
|
/* Attribute data is contained in event->notify_rx.attr_data. */
|
|
|
|
NRF_LOG_INFO("Notify event : BLE_GAP_EVENT_NOTIFY_RX");
|
2020-04-25 07:09:47 -04:00
|
|
|
size_t notifSize = OS_MBUF_PKTLEN(event->notify_rx.om);
|
|
|
|
|
|
|
|
NRF_LOG_INFO("received %s; conn_handle=%d attr_handle=%d "
|
|
|
|
"attr_len=%d",
|
2021-04-18 13:28:14 -04:00
|
|
|
event->notify_rx.indication ? "indication" : "notification",
|
2020-04-25 07:09:47 -04:00
|
|
|
event->notify_rx.conn_handle,
|
|
|
|
event->notify_rx.attr_handle,
|
|
|
|
notifSize);
|
|
|
|
|
|
|
|
alertNotificationClient.OnNotification(event);
|
2021-08-29 16:50:04 -04:00
|
|
|
} break;
|
2021-10-30 14:02:39 -04:00
|
|
|
|
|
|
|
case BLE_GAP_EVENT_NOTIFY_TX:
|
|
|
|
NRF_LOG_INFO("Notify event : BLE_GAP_EVENT_NOTIFY_TX");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_GAP_EVENT_IDENTITY_RESOLVED:
|
|
|
|
NRF_LOG_INFO("Identity event : BLE_GAP_EVENT_IDENTITY_RESOLVED");
|
|
|
|
break;
|
2020-04-25 07:09:47 -04:00
|
|
|
|
2020-04-19 15:26:09 -04:00
|
|
|
default:
|
2021-10-30 14:02:39 -04:00
|
|
|
NRF_LOG_INFO("UNHANDLED GAP event : %d", event->type);
|
2020-04-19 15:26:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:58:31 -04:00
|
|
|
void NimbleController::StartDiscovery() {
|
2021-09-01 23:48:01 -04:00
|
|
|
if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
|
|
|
|
serviceDiscovery.StartDiscovery(connectionHandle);
|
|
|
|
}
|
2020-05-01 15:58:31 -04:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:28:21 -04:00
|
|
|
uint16_t NimbleController::connHandle() {
|
2021-04-18 13:28:14 -04:00
|
|
|
return connectionHandle;
|
2020-07-20 16:28:21 -04:00
|
|
|
}
|
2021-07-11 10:55:06 -04:00
|
|
|
|
|
|
|
void NimbleController::NotifyBatteryLevel(uint8_t level) {
|
2021-09-01 23:50:56 -04:00
|
|
|
if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) {
|
2021-07-11 10:55:06 -04:00
|
|
|
batteryInformationService.NotifyBatteryLevel(connectionHandle, level);
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 15:49:49 -05:00
|
|
|
|
|
|
|
void NimbleController::PersistBond(struct ble_gap_conn_desc& desc) {
|
|
|
|
union ble_store_key key;
|
|
|
|
union ble_store_value our_sec, peer_sec, peer_cccd_set[MYNEWT_VAL(BLE_STORE_MAX_CCCDS)] = {0};
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
memset(&key, 0, sizeof key);
|
|
|
|
memset(&our_sec, 0, sizeof our_sec);
|
|
|
|
key.sec.peer_addr = desc.peer_id_addr;
|
|
|
|
rc = ble_store_read_our_sec(&key.sec, &our_sec.sec);
|
|
|
|
|
|
|
|
if (memcmp(&our_sec.sec, &bondId, sizeof bondId) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(&bondId, &our_sec.sec, sizeof bondId);
|
|
|
|
|
|
|
|
memset(&key, 0, sizeof key);
|
|
|
|
memset(&peer_sec, 0, sizeof peer_sec);
|
|
|
|
key.sec.peer_addr = desc.peer_id_addr;
|
|
|
|
rc += ble_store_read_peer_sec(&key.sec, &peer_sec.sec);
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
memset(&key, 0, sizeof key);
|
|
|
|
key.cccd.peer_addr = desc.peer_id_addr;
|
|
|
|
int peer_count = 0;
|
|
|
|
ble_store_util_count(BLE_STORE_OBJ_TYPE_CCCD, &peer_count);
|
|
|
|
for (int i = 0; i < peer_count; i++) {
|
|
|
|
key.cccd.idx = peer_count;
|
|
|
|
ble_store_read_cccd(&key.cccd, &peer_cccd_set[i].cccd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wakeup Spi and SpiNorFlash before accessing the file system
|
|
|
|
* This should be fixed in the FS driver
|
|
|
|
*/
|
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::GoToRunning);
|
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::DisableSleeping);
|
|
|
|
vTaskDelay(10);
|
|
|
|
|
|
|
|
lfs_file_t file_p;
|
|
|
|
|
|
|
|
rc = fs.FileOpen(&file_p, "/bond.dat", LFS_O_WRONLY | LFS_O_CREAT);
|
|
|
|
if (rc == 0) {
|
|
|
|
fs.FileWrite(&file_p, reinterpret_cast<uint8_t*>(&our_sec.sec), sizeof our_sec);
|
|
|
|
fs.FileWrite(&file_p, reinterpret_cast<uint8_t*>(&peer_sec.sec), sizeof peer_sec);
|
|
|
|
fs.FileWrite(&file_p, reinterpret_cast<const uint8_t*>(&peer_count), 1);
|
|
|
|
for (int i = 0; i < peer_count; i++) {
|
|
|
|
fs.FileWrite(&file_p, reinterpret_cast<uint8_t*>(&peer_cccd_set[i].cccd), sizeof(struct ble_store_value_cccd));
|
|
|
|
}
|
|
|
|
fs.FileClose(&file_p);
|
|
|
|
}
|
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::EnableSleeping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NimbleController::RestoreBond() {
|
|
|
|
lfs_file_t file_p;
|
|
|
|
union ble_store_value sec, cccd;
|
|
|
|
uint8_t peer_count = 0;
|
|
|
|
|
|
|
|
if (fs.FileOpen(&file_p, "/bond.dat", LFS_O_RDONLY) == 0) {
|
|
|
|
memset(&sec, 0, sizeof sec);
|
|
|
|
fs.FileRead(&file_p, reinterpret_cast<uint8_t*>(&sec.sec), sizeof sec);
|
|
|
|
ble_store_write_our_sec(&sec.sec);
|
|
|
|
|
|
|
|
memset(&sec, 0, sizeof sec);
|
|
|
|
fs.FileRead(&file_p, reinterpret_cast<uint8_t*>(&sec.sec), sizeof sec);
|
|
|
|
ble_store_write_peer_sec(&sec.sec);
|
|
|
|
|
|
|
|
fs.FileRead(&file_p, &peer_count, 1);
|
|
|
|
for (int i = 0; i < peer_count; i++) {
|
|
|
|
fs.FileRead(&file_p, reinterpret_cast<uint8_t*>(&cccd.cccd), sizeof(struct ble_store_value_cccd));
|
|
|
|
ble_store_write_cccd(&cccd.cccd);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.FileClose(&file_p);
|
|
|
|
fs.FileDelete("/bond.dat");
|
|
|
|
}
|
|
|
|
}
|