Reset BLE services on disconnect, do not start advertising if a connection is already established.

This commit is contained in:
JF 2020-10-28 18:23:09 +01:00
parent cb9e8815d8
commit f90f2254f5
5 changed files with 54 additions and 23 deletions

View File

@ -139,3 +139,16 @@ uint16_t AlertNotificationClient::EndHandle() const {
uint16_t AlertNotificationClient::NewAlerthandle() const { uint16_t AlertNotificationClient::NewAlerthandle() const {
return newAlertHandle; return newAlertHandle;
} }
void AlertNotificationClient::Reset() {
ansStartHandle = 0;
ansEndHandle = 0;
supportedNewAlertCategoryHandle = 0;
supportedUnreadAlertCategoryHandle = 0;
newAlertHandle = 0;
newAlertDescriptorHandle = 0;
newAlertDefHandle = 0;
unreadAlertStatusHandle = 0;
controlPointHandle = 0;
isDiscovered = false;
}

View File

@ -27,6 +27,7 @@ namespace Pinetime {
bool IsDiscovered() const; bool IsDiscovered() const;
uint16_t StartHandle() const; uint16_t StartHandle() const;
uint16_t EndHandle() const; uint16_t EndHandle() const;
void Reset();
static constexpr const ble_uuid16_t &Uuid() { return ansServiceUuid; } static constexpr const ble_uuid16_t &Uuid() { return ansServiceUuid; }
@ -64,15 +65,15 @@ namespace Pinetime {
.value = controlPointId .value = controlPointId
}; };
uint16_t ansStartHandle; uint16_t ansStartHandle = 0;
uint16_t ansEndHandle; uint16_t ansEndHandle = 0;
uint16_t supportedNewAlertCategoryHandle; uint16_t supportedNewAlertCategoryHandle = 0;
uint16_t supportedUnreadAlertCategoryHandle; uint16_t supportedUnreadAlertCategoryHandle = 0;
uint16_t newAlertHandle; uint16_t newAlertHandle = 0;
uint16_t newAlertDescriptorHandle = 0; uint16_t newAlertDescriptorHandle = 0;
uint16_t newAlertDefHandle; uint16_t newAlertDefHandle = 0;
uint16_t unreadAlertStatusHandle; uint16_t unreadAlertStatusHandle = 0;
uint16_t controlPointHandle; uint16_t controlPointHandle = 0;
bool isDiscovered = false; bool isDiscovered = false;
Pinetime::System::SystemTask &systemTask; Pinetime::System::SystemTask &systemTask;
Pinetime::Controllers::NotificationManager &notificationManager; Pinetime::Controllers::NotificationManager &notificationManager;

View File

@ -32,13 +32,14 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga
int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error, int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
const ble_gatt_chr *characteristic) { const ble_gatt_chr *characteristic) {
if(characteristic == nullptr && error->status == BLE_HS_EDONE) { if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
NRF_LOG_INFO("CTS Characteristic discovery complete"); NRF_LOG_INFO("CTS Characteristic discovery complete");
return 0; return 0;
} }
if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) { if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t *) &currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle); NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
isCharacteristicDiscovered = true;
currentTimeHandle = characteristic->val_handle; currentTimeHandle = characteristic->val_handle;
} }
return 0; return 0;
@ -75,3 +76,12 @@ uint16_t CurrentTimeClient::EndHandle() const {
uint16_t CurrentTimeClient::CurrentTimeHandle() const { uint16_t CurrentTimeClient::CurrentTimeHandle() const {
return currentTimeHandle; return currentTimeHandle;
} }
void CurrentTimeClient::Reset() {
isDiscovered = false;
isCharacteristicDiscovered = false;
}
bool CurrentTimeClient::IsCharacteristicDiscovered() const {
return isCharacteristicDiscovered;
}

View File

@ -12,11 +12,13 @@ namespace Pinetime {
public: public:
explicit CurrentTimeClient(DateTime& dateTimeController); explicit CurrentTimeClient(DateTime& dateTimeController);
void Init(); void Init();
void Reset();
bool 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, int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
const ble_gatt_chr *characteristic); const ble_gatt_chr *characteristic);
int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute); int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute);
bool IsDiscovered() const; bool IsDiscovered() const;
bool IsCharacteristicDiscovered() const;
uint16_t StartHandle() const; uint16_t StartHandle() const;
uint16_t EndHandle() const; uint16_t EndHandle() const;
uint16_t CurrentTimeHandle() const; uint16_t CurrentTimeHandle() const;
@ -46,11 +48,14 @@ namespace Pinetime {
.value = currentTimeCharacteristicId .value = currentTimeCharacteristicId
}; };
uint16_t currentTimeHandle;
DateTime& dateTimeController; DateTime& dateTimeController;
bool isDiscovered = false; bool isDiscovered = false;
uint16_t ctsStartHandle; uint16_t ctsStartHandle;
uint16_t ctsEndHandle; uint16_t ctsEndHandle;
bool isCharacteristicDiscovered = false;
uint16_t currentTimeHandle;
}; };
} }
} }

View File

@ -108,7 +108,7 @@ void NimbleController::Init() {
} }
void NimbleController::StartAdvertising() { 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); ble_svc_gap_device_name_set(deviceName);
@ -197,6 +197,8 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason); NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason);
/* Connection terminated; resume advertising. */ /* Connection terminated; resume advertising. */
currentTimeClient.Reset();
alertNotificationClient.Reset();
connectionHandle = BLE_HS_CONN_HANDLE_NONE; connectionHandle = BLE_HS_CONN_HANDLE_NONE;
bleController.Disconnect(); bleController.Disconnect();
StartAdvertising(); StartAdvertising();
@ -289,10 +291,10 @@ int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error,
int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error,
const ble_gatt_chr *characteristic) { const ble_gatt_chr *characteristic) {
if(characteristic == nullptr && error->status == BLE_HS_EDONE) { if(characteristic == nullptr && error->status == BLE_HS_EDONE && currentTimeClient.IsCharacteristicDiscovered()) {
NRF_LOG_INFO("CTS characteristic Discovery complete"); NRF_LOG_INFO("CTS characteristic Discovery complete");
ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this); auto res = ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this);
return 0; return res;
} }
return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic); return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic);
} }