Skip to content

Commit 225acd0

Browse files
committed
Avoid using uninitialized value in ImmediateAlertClient.
1 parent 99f7a39 commit 225acd0

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/components/ble/ImmediateAlertClient.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ void ImmediateAlertClient::Init() {
3131

3232
bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) {
3333
if (service == nullptr && error->status == BLE_HS_EDONE) {
34-
if (isDiscovered) {
34+
if (iasHandles.has_value()) {
3535
NRF_LOG_INFO("[IAS] service found, starting characteristics discovery");
3636

37-
ble_gattc_disc_all_chrs(connectionHandle, iasStartHandle, iasEndHandle, OnImmediateAlertCharacteristicDiscoveredCallback, this);
37+
ble_gattc_disc_all_chrs(connectionHandle,
38+
iasHandles->startHandle,
39+
iasHandles->endHandle,
40+
OnImmediateAlertCharacteristicDiscoveredCallback,
41+
this);
3842
} else {
3943
NRF_LOG_INFO("[IAS] service not found");
4044
onServiceDiscovered(connectionHandle);
@@ -44,9 +48,10 @@ bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble
4448

4549
if (service != nullptr && ble_uuid_cmp(&immediateAlertClientUuid.u, &service->uuid.u) == 0) {
4650
NRF_LOG_INFO("[IAS] discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
47-
isDiscovered = true;
48-
iasStartHandle = service->start_handle;
49-
iasEndHandle = service->end_handle;
51+
iasHandles.emplace(HandleRange {
52+
.startHandle = service->start_handle,
53+
.endHandle = service->end_handle,
54+
});
5055
}
5156
return false;
5257
}
@@ -62,8 +67,8 @@ int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
6267
}
6368

6469
if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
65-
if (!isCharacteristicDiscovered) {
66-
NRF_LOG_INFO("[IAS] Characteristic discovery unsuccessful");
70+
if (!alertLevelHandle.has_value()) {
71+
NRF_LOG_INFO("[IAS] Alert level characteristic not found.");
6772
onServiceDiscovered(conn_handle);
6873
}
6974

@@ -72,7 +77,6 @@ int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
7277

7378
if (characteristic != nullptr && ble_uuid_cmp(&alertLevelCharacteristicUuid.u, &characteristic->uuid.u) == 0) {
7479
NRF_LOG_INFO("[IAS] Characteristic discovered : 0x%x", characteristic->val_handle);
75-
isCharacteristicDiscovered = true;
7680
alertLevelHandle = characteristic->val_handle;
7781
}
7882
return 0;
@@ -93,7 +97,10 @@ bool ImmediateAlertClient::SendImmediateAlert(ImmediateAlertClient::Levels level
9397
if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) {
9498
return false;
9599
}
100+
if (!alertLevelHandle.has_value()) {
101+
return false;
102+
}
96103

97-
ble_gattc_write_no_rsp(connectionHandle, alertLevelHandle, om);
104+
ble_gattc_write_no_rsp(connectionHandle, *alertLevelHandle, om);
98105
return true;
99106
}

src/components/ble/ImmediateAlertClient.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ namespace Pinetime {
4646
static constexpr ble_uuid16_t immediateAlertClientUuid {.u {.type = BLE_UUID_TYPE_16}, .value = immediateAlertClientId};
4747
static constexpr ble_uuid16_t alertLevelCharacteristicUuid {.u {.type = BLE_UUID_TYPE_16}, .value = alertLevelId};
4848

49-
bool isDiscovered = false;
50-
uint16_t iasStartHandle;
51-
uint16_t iasEndHandle;
52-
bool isCharacteristicDiscovered = false;
49+
struct HandleRange {
50+
uint16_t startHandle;
51+
uint16_t endHandle;
52+
};
5353

54-
uint16_t alertLevelHandle;
54+
std::optional<HandleRange> iasHandles;
55+
std::optional<uint16_t> alertLevelHandle;
5556
std::function<void(uint16_t)> onServiceDiscovered;
5657
};
5758
}

0 commit comments

Comments
 (0)