Refactoring of BLE service discovery : it is now implemented into the classes of the services.

This commit is contained in:
JF
2020-10-29 16:06:01 +01:00
parent f90f2254f5
commit 29f8074fcb
10 changed files with 254 additions and 227 deletions

View File

@@ -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({&currentTimeClient, &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()) {}
@@ -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:
@@ -271,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 && currentTimeClient.IsCharacteristicDiscovered()) {
NRF_LOG_INFO("CTS characteristic Discovery complete");
auto res = ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this);
return res;
}
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);
}