Skip to content

Commit f90f225

Browse files
committed
Reset BLE services on disconnect, do not start advertising if a connection is already established.
1 parent cb9e881 commit f90f225

5 files changed

Lines changed: 54 additions & 23 deletions

File tree

src/components/ble/AlertNotificationClient.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,16 @@ uint16_t AlertNotificationClient::EndHandle() const {
139139
uint16_t AlertNotificationClient::NewAlerthandle() const {
140140
return newAlertHandle;
141141
}
142+
143+
void AlertNotificationClient::Reset() {
144+
ansStartHandle = 0;
145+
ansEndHandle = 0;
146+
supportedNewAlertCategoryHandle = 0;
147+
supportedUnreadAlertCategoryHandle = 0;
148+
newAlertHandle = 0;
149+
newAlertDescriptorHandle = 0;
150+
newAlertDefHandle = 0;
151+
unreadAlertStatusHandle = 0;
152+
controlPointHandle = 0;
153+
isDiscovered = false;
154+
}

src/components/ble/AlertNotificationClient.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace Pinetime {
2727
bool IsDiscovered() const;
2828
uint16_t StartHandle() const;
2929
uint16_t EndHandle() const;
30+
void Reset();
3031

3132
static constexpr const ble_uuid16_t &Uuid() { return ansServiceUuid; }
3233

@@ -64,15 +65,15 @@ namespace Pinetime {
6465
.value = controlPointId
6566
};
6667

67-
uint16_t ansStartHandle;
68-
uint16_t ansEndHandle;
69-
uint16_t supportedNewAlertCategoryHandle;
70-
uint16_t supportedUnreadAlertCategoryHandle;
71-
uint16_t newAlertHandle;
68+
uint16_t ansStartHandle = 0;
69+
uint16_t ansEndHandle = 0;
70+
uint16_t supportedNewAlertCategoryHandle = 0;
71+
uint16_t supportedUnreadAlertCategoryHandle = 0;
72+
uint16_t newAlertHandle = 0;
7273
uint16_t newAlertDescriptorHandle = 0;
73-
uint16_t newAlertDefHandle;
74-
uint16_t unreadAlertStatusHandle;
75-
uint16_t controlPointHandle;
74+
uint16_t newAlertDefHandle = 0;
75+
uint16_t unreadAlertStatusHandle = 0;
76+
uint16_t controlPointHandle = 0;
7677
bool isDiscovered = false;
7778
Pinetime::System::SystemTask &systemTask;
7879
Pinetime::Controllers::NotificationManager &notificationManager;

src/components/ble/CurrentTimeClient.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_ga
3232

3333
int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
3434
const ble_gatt_chr *characteristic) {
35-
if(characteristic == nullptr && error->status == BLE_HS_EDONE) {
36-
NRF_LOG_INFO("CTS Characteristic discovery complete");
37-
return 0;
38-
}
39-
40-
if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
41-
NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
42-
currentTimeHandle = characteristic->val_handle;
43-
}
35+
if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
36+
NRF_LOG_INFO("CTS Characteristic discovery complete");
4437
return 0;
38+
}
39+
40+
if (characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t *) &currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
41+
NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
42+
isCharacteristicDiscovered = true;
43+
currentTimeHandle = characteristic->val_handle;
44+
}
45+
return 0;
4546
}
4647

4748
int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute) {
@@ -74,4 +75,13 @@ uint16_t CurrentTimeClient::EndHandle() const {
7475

7576
uint16_t CurrentTimeClient::CurrentTimeHandle() const {
7677
return currentTimeHandle;
77-
}
78+
}
79+
80+
void CurrentTimeClient::Reset() {
81+
isDiscovered = false;
82+
isCharacteristicDiscovered = false;
83+
}
84+
85+
bool CurrentTimeClient::IsCharacteristicDiscovered() const {
86+
return isCharacteristicDiscovered;
87+
}

src/components/ble/CurrentTimeClient.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ namespace Pinetime {
1212
public:
1313
explicit CurrentTimeClient(DateTime& dateTimeController);
1414
void Init();
15+
void Reset();
1516
bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service);
1617
int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
1718
const ble_gatt_chr *characteristic);
1819
int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error, const ble_gatt_attr *attribute);
1920
bool IsDiscovered() const;
21+
bool IsCharacteristicDiscovered() const;
2022
uint16_t StartHandle() const;
2123
uint16_t EndHandle() const;
2224
uint16_t CurrentTimeHandle() const;
@@ -46,11 +48,14 @@ namespace Pinetime {
4648
.value = currentTimeCharacteristicId
4749
};
4850

49-
uint16_t currentTimeHandle;
5051
DateTime& dateTimeController;
5152
bool isDiscovered = false;
5253
uint16_t ctsStartHandle;
5354
uint16_t ctsEndHandle;
55+
56+
bool isCharacteristicDiscovered = false;
57+
uint16_t currentTimeHandle;
58+
5459
};
5560
}
5661
}

src/components/ble/NimbleController.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void NimbleController::Init() {
108108
}
109109

110110
void NimbleController::StartAdvertising() {
111-
if(ble_gap_adv_active()) return;
111+
if(bleController.IsConnected() || ble_gap_conn_active() || ble_gap_adv_active()) return;
112112

113113
ble_svc_gap_device_name_set(deviceName);
114114

@@ -197,6 +197,8 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
197197
NRF_LOG_INFO("disconnect; reason=%d", event->disconnect.reason);
198198

199199
/* Connection terminated; resume advertising. */
200+
currentTimeClient.Reset();
201+
alertNotificationClient.Reset();
200202
connectionHandle = BLE_HS_CONN_HANDLE_NONE;
201203
bleController.Disconnect();
202204
StartAdvertising();
@@ -289,10 +291,10 @@ int NimbleController::OnDiscoveryEvent(uint16_t i, const ble_gatt_error *error,
289291

290292
int NimbleController::OnCTSCharacteristicDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error,
291293
const ble_gatt_chr *characteristic) {
292-
if(characteristic == nullptr && error->status == BLE_HS_EDONE) {
294+
if(characteristic == nullptr && error->status == BLE_HS_EDONE && currentTimeClient.IsCharacteristicDiscovered()) {
293295
NRF_LOG_INFO("CTS characteristic Discovery complete");
294-
ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this);
295-
return 0;
296+
auto res = ble_gattc_read(connectionHandle, currentTimeClient.CurrentTimeHandle(), CurrentTimeReadCallback, this);
297+
return res;
296298
}
297299
return currentTimeClient.OnCharacteristicDiscoveryEvent(connectionHandle, error, characteristic);
298300
}

0 commit comments

Comments
 (0)