diff options
author | JF002 <JF002@users.noreply.github.com> | 2020-10-30 14:16:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-30 14:16:04 +0100 |
commit | 0e97db1c30748d9893291a27504bb55723f58e7d (patch) | |
tree | 9b410aee92af82099619c56a4d25c9c6b7c99e68 /src/components/ble/NimbleController.cpp | |
parent | cb9e8815d8bc6ce71fd8e97f3e3dae402658ce1f (diff) | |
parent | 29f8074fcb844cf9668a5bf071e9cffa47299c99 (diff) |
Merge pull request #118 from JF002/improve-ble-connection
Improve ble connection
Diffstat (limited to 'src/components/ble/NimbleController.cpp')
-rw-r--r-- | src/components/ble/NimbleController.cpp | 112 |
1 files changed, 6 insertions, 106 deletions
diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 022cc510..af7f4029 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -1,10 +1,7 @@ - #include "components/datetime/DateTimeController.h" - #include <systemtask/SystemTask.h> #include "components/ble/NotificationManager.h" #include <hal/nrf_rtc.h> - #include "NimbleController.h" #include "MusicService.h" #include <services/gatt/ble_svc_gatt.h> @@ -14,14 +11,8 @@ #include <host/ble_hs.h> #include <host/ble_gap.h> - - using namespace Pinetime::Controllers; -// TODO I'm not satisfied by how this code looks like (AlertNotificationClient and CurrentTimeClient must -// expose too much data, too many callbacks -> NimbleController -> CTS/ANS client. -// Let's try to improve this code (and keep it working!) - NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController, DateTime& dateTimeController, @@ -40,8 +31,8 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, currentTimeService{dateTimeController}, musicService{systemTask}, batteryInformationService{batteryController}, - immediateAlertService{systemTask, notificationManager} { - + immediateAlertService{systemTask, notificationManager}, + serviceDiscovery({¤tTimeClient, &alertNotificationClient}) { } int GAPEventCallback(struct ble_gap_event *event, void *arg) { @@ -49,33 +40,6 @@ int GAPEventCallback(struct ble_gap_event *event, void *arg) { return nimbleController->OnGAPEvent(event); } -int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast<NimbleController*>(arg); - return client->OnCTSCharacteristicDiscoveryEvent(conn_handle, error, chr); -} - -int AlertNotificationCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - const struct ble_gatt_chr *chr, void *arg) { - auto client = static_cast<NimbleController*>(arg); - return client->OnANSCharacteristicDiscoveryEvent(conn_handle, error, chr); -} - -int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error *error, - struct ble_gatt_attr *attr, void *arg) { - auto client = static_cast<NimbleController*>(arg); - return client->OnCurrentTimeReadResult(conn_handle, error, attr); -} - -int AlertNotificationDescriptorDiscoveryEventCallback(uint16_t conn_handle, - const struct ble_gatt_error *error, - uint16_t chr_val_handle, - const struct ble_gatt_dsc *dsc, - void *arg) { - auto client = static_cast<NimbleController*>(arg); - return client->OnANSDescriptorDiscoveryEventCallback(conn_handle, error, chr_val_handle, dsc); -} - void NimbleController::Init() { while (!ble_hs_synced()) {} @@ -108,7 +72,7 @@ void NimbleController::Init() { } void NimbleController::StartAdvertising() { - if(ble_gap_adv_active()) return; + if(bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active()) return; ble_svc_gap_device_name_set(deviceName); @@ -158,15 +122,6 @@ void NimbleController::StartAdvertising() { // the application has been woken up, for example. } -int OnAllSvrDisco(uint16_t conn_handle, - const struct ble_gatt_error *error, - const struct ble_gatt_svc *service, - void *arg) { - auto nimbleController = static_cast<NimbleController*>(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: @@ -197,6 +152,8 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) { NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason); /* Connection terminated; resume advertising. */ + currentTimeClient.Reset(); + alertNotificationClient.Reset(); connectionHandle = BLE_HS_CONN_HANDLE_NONE; bleController.Disconnect(); StartAdvertising(); @@ -269,65 +226,8 @@ 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) { - if(service == nullptr && error->status == BLE_HS_EDONE) { - NRF_LOG_INFO("Service Discovery complete"); - if(currentTimeClient.IsDiscovered()) { - ble_gattc_disc_all_chrs(connectionHandle, currentTimeClient.StartHandle(), currentTimeClient.EndHandle(), - CurrentTimeCharacteristicDiscoveredCallback, this); - - } else if(alertNotificationClient.IsDiscovered()) { - ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), alertNotificationClient.EndHandle(), - AlertNotificationCharacteristicDiscoveredCallback, this); - } - } - - alertNotificationClient.OnDiscoveryEvent(i, error, service); - currentTimeClient.OnDiscoveryEvent(i, error, service); - return 0; -} - -int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic) { - if(characteristic == nullptr && error->status == BLE_HS_EDONE) { - NRF_LOG_INFO("CTS characteristic Discovery complete"); - ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this); - return 0; - } - return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic); -} - -int NimbleController::OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, - const ble_gatt_chr *characteristic) { - if(characteristic == nullptr && error->status == BLE_HS_EDONE) { - NRF_LOG_INFO("ANS characteristic Discovery complete"); - ble_gattc_disc_all_dscs(connectionHandle, - alertNotificationClient.NewAlerthandle(), alertNotificationClient.EndHandle(), - AlertNotificationDescriptorDiscoveryEventCallback, this); - return 0; - } - return alertNotificationClient.OnCharacteristicsDiscoveryEvent(connectionHandle, error, characteristic); -} - -int NimbleController::OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute) { - currentTimeClient.OnCurrentTimeReadResult(connectionHandle, error, attribute); - - if (alertNotificationClient.IsDiscovered()) { - ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), - alertNotificationClient.EndHandle(), - AlertNotificationCharacteristicDiscoveredCallback, this); - } - return 0; -} - -int NimbleController::OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error, - uint16_t characteristicValueHandle, - const ble_gatt_dsc *descriptor) { - return alertNotificationClient.OnDescriptorDiscoveryEventCallback(connectionHandle, error, characteristicValueHandle, descriptor); -} - void NimbleController::StartDiscovery() { - ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this); + serviceDiscovery.StartDiscovery(connectionHandle); } |