summaryrefslogtreecommitdiff
path: root/src/Components/Ble/NimbleController.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Components/Ble/NimbleController.cpp')
-rw-r--r--src/Components/Ble/NimbleController.cpp86
1 files changed, 83 insertions, 3 deletions
diff --git a/src/Components/Ble/NimbleController.cpp b/src/Components/Ble/NimbleController.cpp
index 5e3d58eb..7894ff43 100644
--- a/src/Components/Ble/NimbleController.cpp
+++ b/src/Components/Ble/NimbleController.cpp
@@ -17,8 +17,9 @@
using namespace Pinetime::Controllers;
-// TODO c++ify the following code
-// - cts should be in it own class
+// TODO I'm not satisfied by how this code looks like (AlertNotificationClient and CurrentTimeClient must
+// expose too much data, too many callbacks -> NimbleController -> CTS/ANS client.
+// Let's try to improve this code (and keep it working!)
NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
DateTime& dateTimeController,
@@ -36,6 +37,33 @@ int GAPEventCallback(struct ble_gap_event *event, void *arg) {
return nimbleController->OnGAPEvent(event);
}
+int CurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle, const struct ble_gatt_error *error,
+ const struct ble_gatt_chr *chr, void *arg) {
+ auto client = static_cast<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()) {}
@@ -203,11 +231,63 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
}
int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error, const ble_gatt_svc *service) {
+ if(service == nullptr && error->status == BLE_HS_EDONE) {
+ NRF_LOG_INFO("Service Discovery complete");
+ if(currentTimeClient.IsDiscovered()) {
+ ble_gattc_disc_all_chrs(connectionHandle, currentTimeClient.StartHandle(), currentTimeClient.EndHandle(),
+ CurrentTimeCharacteristicDiscoveredCallback, this);
+
+ } else if(alertNotificationClient.IsDiscovered()) {
+ ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(), alertNotificationClient.EndHandle(),
+ AlertNotificationCharacteristicDiscoveredCallback, this);
+ }
+ return 0;
+ }
+
alertNotificationClient.OnDiscoveryEvent(i, error, service);
-// currentTimeClient.OnDiscoveryEvent(i, error, service);
+ currentTimeClient.OnDiscoveryEvent(i, error, service);
+ return 0;
+}
+
+int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error,
+ const ble_gatt_chr *characteristic) {
+ if(characteristic == nullptr && error->status == BLE_HS_EDONE) {
+ NRF_LOG_INFO("CTS characteristic Discovery complete");
+ ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this);
+ return 0;
+ }
+ return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic);
+}
+
+int NimbleController::OnANSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error,
+ const ble_gatt_chr *characteristic) {
+ if(characteristic == nullptr && error->status == BLE_HS_EDONE) {
+ NRF_LOG_INFO("ANS characteristic Discovery complete");
+ ble_gattc_disc_all_dscs(connectionHandle,
+ alertNotificationClient.NewAlerthandle(), alertNotificationClient.EndHandle(),
+ AlertNotificationDescriptorDiscoveryEventCallback, this);
+ return 0;
+ }
+ return alertNotificationClient.OnCharacteristicsDiscoveryEvent(connectionHandle, error, characteristic);
+}
+
+int NimbleController::OnCurrentTimeReadResult(uint16_t connectionHandle, const ble_gatt_error *error, ble_gatt_attr *attribute) {
+ currentTimeClient.OnCurrentTimeReadResult(connectionHandle, error, attribute);
+
+ if (alertNotificationClient.IsDiscovered()) {
+ ble_gattc_disc_all_chrs(connectionHandle, alertNotificationClient.StartHandle(),
+ alertNotificationClient.EndHandle(),
+ AlertNotificationCharacteristicDiscoveredCallback, this);
+ }
return 0;
}
+int NimbleController::OnANSDescriptorDiscoveryEventCallback(uint16_t connectionHandle, const ble_gatt_error *error,
+ uint16_t characteristicValueHandle,
+ const ble_gatt_dsc *descriptor) {
+ return alertNotificationClient.OnDescriptorDiscoveryEventCallback(connectionHandle, error, characteristicValueHandle, descriptor);
+}
+