Skip to content

Commit 7ddd0df

Browse files
committed
addressing review comments
1 parent 9f81638 commit 7ddd0df

File tree

3 files changed

+90
-87
lines changed

3 files changed

+90
-87
lines changed

src/platform/silabs/BLEManagerImpl.h

+10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
5959
void HandleBootEvent(void);
6060

6161
#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
62+
// Used for posting the event in the BLE queue
63+
void BlePostEvent(BleEvent_t * event);
6264
void HandleConnectEvent(sl_wfx_msg_t * evt);
6365
void HandleConnectionCloseEvent(sl_wfx_msg_t * evt);
6466
void HandleWriteEvent(sl_wfx_msg_t * evt);
@@ -94,6 +96,14 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
9496
// the implementation methods provided by this class.
9597
friend BLEManager;
9698

99+
#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
100+
// rs91x BLE task handling
101+
osMessageQueueId_t sBleEventQueue = NULL;
102+
static void sl_ble_event_handling_task(void * args);
103+
void sl_ble_init();
104+
void ProcessEvent(BleEvent_t inEvent);
105+
#endif
106+
97107
// ===== Members that implement the BLEManager internal interface.
98108

99109
CHIP_ERROR _Init(void);

src/platform/silabs/rs911x/BLEManagerImpl.cpp

+71-57
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,43 @@ using namespace ::chip;
8080
using namespace ::chip::Ble;
8181
using namespace ::chip::DeviceLayer::Internal;
8282

83-
void sl_ble_init()
84-
{
85-
uint8_t randomAddrBLE[RSI_BLE_ADDR_LENGTH] = { 0 };
86-
uint64_t randomAddr = chip::Crypto::GetRandU64();
87-
memcpy(randomAddrBLE, &randomAddr, RSI_BLE_ADDR_LENGTH);
88-
// Set the two least significant bits as the first 2 bits of the address has to be '11' to ensure the address is a random
89-
// non-resolvable private address
90-
randomAddrBLE[(RSI_BLE_ADDR_LENGTH - 1)] |= 0xC0;
83+
namespace chip {
84+
namespace DeviceLayer {
85+
namespace Internal {
9186

92-
// registering the GAP callback functions
93-
rsi_ble_gap_register_callbacks(NULL, NULL, rsi_ble_on_disconnect_event, NULL, NULL, NULL, rsi_ble_on_enhance_conn_status_event,
94-
NULL, NULL, NULL);
87+
namespace {
9588

96-
// registering the GATT call back functions
97-
rsi_ble_gatt_register_callbacks(NULL, NULL, NULL, NULL, NULL, NULL, NULL, rsi_ble_on_gatt_write_event, NULL, NULL,
98-
rsi_ble_on_read_req_event, rsi_ble_on_mtu_event, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
99-
NULL, rsi_ble_on_event_indication_confirmation, NULL);
89+
#define CHIP_ADV_DATA_TYPE_FLAGS 0x01
90+
#define CHIP_ADV_DATA_TYPE_UUID 0x03
91+
#define CHIP_ADV_DATA_TYPE_NAME 0x09
92+
#define CHIP_ADV_DATA_TYPE_SERVICE_DATA 0x16
10093

101-
// Exchange of GATT info with BLE stack
102-
rsi_ble_add_matter_service();
103-
rsi_ble_set_random_address_with_value(randomAddrBLE);
104-
InitBleEventQueue();
105-
chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
106-
}
94+
#define CHIP_ADV_DATA_FLAGS 0x06
95+
96+
#define CHIP_ADV_DATA 0
97+
#define CHIP_ADV_SCAN_RESPONSE_DATA 1
98+
#define CHIP_ADV_SHORT_UUID_LEN 2
99+
100+
#define MAX_RESPONSE_DATA_LEN 31
101+
#define MAX_ADV_DATA_LEN 31
102+
103+
// Timer Frequency used.
104+
#define TIMER_CLK_FREQ ((uint32_t) 32768)
105+
106+
// Convert msec to timer ticks.
107+
#define TIMER_MS_2_TIMERTICK(ms) ((TIMER_CLK_FREQ * ms) / 1000)
108+
#define TIMER_S_2_TIMERTICK(s) (TIMER_CLK_FREQ * s)
107109

108-
void ProcessEvent(BleEvent_t inEvent)
110+
const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
111+
0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 };
112+
const uint8_t ShortUUID_CHIPoBLEService[] = { 0xF6, 0xFF };
113+
114+
} // namespace
115+
116+
BLEManagerImpl BLEManagerImpl::sInstance;
117+
118+
119+
void BLEManagerImpl::ProcessEvent(BleEvent_t inEvent)
109120
{
110121
switch (inEvent.eventType)
111122
{
@@ -154,7 +165,17 @@ void ProcessEvent(BleEvent_t inEvent)
154165
}
155166
}
156167

157-
void sl_ble_event_handling_task(void * args)
168+
void BLEManagerImpl::BlePostEvent(BleEvent_t * event)
169+
{
170+
sl_status_t status = osMessageQueuePut(sInstance.sBleEventQueue, event, 0, 0);
171+
if (status != osOK)
172+
{
173+
ChipLogError(DeviceLayer, "BlePostEvent: failed to post event: 0x%lx", status);
174+
// TODO: Handle error, requeue event depending on queue size or notify relevant task, Chipdie, etc.
175+
}
176+
}
177+
178+
void BLEManagerImpl::sl_ble_event_handling_task(void * args)
158179
{
159180
sl_status_t status;
160181
BleEvent_t bleEvent;
@@ -163,15 +184,15 @@ void sl_ble_event_handling_task(void * args)
163184
osSemaphoreAcquire(sl_rs_ble_init_sem, osWaitForever);
164185

165186
// This function initialize BLE and start BLE advertisement.
166-
sl_ble_init();
187+
sInstance.sl_ble_init();
167188

168189
// Application event map
169190
while (1)
170191
{
171-
status = osMessageQueueGet(GetBleEventQueue(), &bleEvent, NULL, osWaitForever);
192+
status = osMessageQueueGet(sInstance.sBleEventQueue, &bleEvent, NULL, osWaitForever);
172193
if (status == osOK)
173194
{
174-
ProcessEvent(bleEvent);
195+
sInstance.ProcessEvent(bleEvent);
175196
}
176197
else
177198
{
@@ -180,48 +201,41 @@ void sl_ble_event_handling_task(void * args)
180201
}
181202
}
182203

183-
namespace chip {
184-
namespace DeviceLayer {
185-
namespace Internal {
186-
187-
namespace {
188-
189-
#define CHIP_ADV_DATA_TYPE_FLAGS 0x01
190-
#define CHIP_ADV_DATA_TYPE_UUID 0x03
191-
#define CHIP_ADV_DATA_TYPE_NAME 0x09
192-
#define CHIP_ADV_DATA_TYPE_SERVICE_DATA 0x16
193-
194-
#define CHIP_ADV_DATA_FLAGS 0x06
195-
196-
#define CHIP_ADV_DATA 0
197-
#define CHIP_ADV_SCAN_RESPONSE_DATA 1
198-
#define CHIP_ADV_SHORT_UUID_LEN 2
199-
200-
#define MAX_RESPONSE_DATA_LEN 31
201-
#define MAX_ADV_DATA_LEN 31
202-
203-
// Timer Frequency used.
204-
#define TIMER_CLK_FREQ ((uint32_t) 32768)
204+
void BLEManagerImpl::sl_ble_init()
205+
{
206+
uint8_t randomAddrBLE[RSI_BLE_ADDR_LENGTH] = { 0 };
207+
uint64_t randomAddr = chip::Crypto::GetRandU64();
208+
memcpy(randomAddrBLE, &randomAddr, RSI_BLE_ADDR_LENGTH);
209+
// Set the two least significant bits as the first 2 bits of the address has to be '11' to ensure the address is a random
210+
// non-resolvable private address
211+
randomAddrBLE[(RSI_BLE_ADDR_LENGTH - 1)] |= 0xC0;
205212

206-
// Convert msec to timer ticks.
207-
#define TIMER_MS_2_TIMERTICK(ms) ((TIMER_CLK_FREQ * ms) / 1000)
208-
#define TIMER_S_2_TIMERTICK(s) (TIMER_CLK_FREQ * s)
213+
// registering the GAP callback functions
214+
rsi_ble_gap_register_callbacks(NULL, NULL, rsi_ble_on_disconnect_event, NULL, NULL, NULL, rsi_ble_on_enhance_conn_status_event,
215+
NULL, NULL, NULL);
209216

210-
const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
211-
0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 };
212-
const uint8_t ShortUUID_CHIPoBLEService[] = { 0xF6, 0xFF };
217+
// registering the GATT call back functions
218+
rsi_ble_gatt_register_callbacks(NULL, NULL, NULL, NULL, NULL, NULL, NULL, rsi_ble_on_gatt_write_event, NULL, NULL,
219+
rsi_ble_on_read_req_event, rsi_ble_on_mtu_event, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
220+
NULL, rsi_ble_on_event_indication_confirmation, NULL);
213221

214-
} // namespace
222+
// Exchange of GATT info with BLE stack
223+
rsi_ble_add_matter_service();
224+
rsi_ble_set_random_address_with_value(randomAddrBLE);
225+
226+
sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(WfxEvent_t), NULL);
227+
VerifyOrDie(sInstance.sBleEventQueue != nullptr);
215228

216-
BLEManagerImpl BLEManagerImpl::sInstance;
229+
chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
230+
}
217231

218232
CHIP_ERROR BLEManagerImpl::_Init()
219233
{
220234
CHIP_ERROR err;
221235

222236
sl_rs_ble_init_sem = osSemaphoreNew(1, 0, NULL);
223237

224-
sBleThread = osThreadNew(sl_ble_event_handling_task, NULL, &kBleTaskAttr);
238+
sBleThread = osThreadNew(sInstance.sl_ble_event_handling_task, NULL, &kBleTaskAttr);
225239

226240
VerifyOrReturnError(sBleThread != nullptr, CHIP_ERROR_INCORRECT_STATE);
227241

src/platform/silabs/rs911x/wfx_sl_ble_init.cpp

+9-30
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
#include <lib/support/logging/CHIPLogging.h>
2424
#include <platform/CHIPDeviceLayer.h>
25+
// #include <platform/internal/BLEManager.h>
26+
#include <platform/silabs/BLEManagerImpl.h>
2527

2628
#ifdef __cplusplus
2729
extern "C" {
@@ -32,30 +34,7 @@ extern "C" {
3234
#endif
3335

3436
// Global Variables
35-
rsi_ble_t att_list;
3637
BleEvent_t bleEvent;
37-
static osMessageQueueId_t sBleEventQueue = NULL;
38-
39-
void InitBleEventQueue()
40-
{
41-
sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(WfxEvent_t), NULL);
42-
VerifyOrDie(sBleEventQueue != nullptr);
43-
}
44-
45-
osMessageQueueId_t GetBleEventQueue()
46-
{
47-
return sBleEventQueue;
48-
}
49-
50-
void BlePostEvent(BleEvent_t * event)
51-
{
52-
sl_status_t status = osMessageQueuePut(sBleEventQueue, event, 0, 0);
53-
if (status != osOK)
54-
{
55-
ChipLogError(DeviceLayer, "BlePostEvent: failed to post event: 0x%lx", status);
56-
// TODO: Handle error, requeue event depending on queue size or notify relevant task, Chipdie, etc.
57-
}
58-
}
5938

6039
/*==============================================*/
6140
/**
@@ -70,7 +49,7 @@ void rsi_ble_on_mtu_event(rsi_ble_event_mtu_t * rsi_ble_mtu)
7049
{
7150
bleEvent.eventType = RSI_BLE_MTU_EVENT;
7251
memcpy(&bleEvent.eventData->rsi_ble_mtu, rsi_ble_mtu, sizeof(rsi_ble_event_mtu_t));
73-
BlePostEvent(&bleEvent);
52+
chip::DeviceLayer::Internal::BLEMgrImpl().BlePostEvent(&bleEvent);
7453
}
7554

7655
/*==============================================*/
@@ -88,7 +67,7 @@ void rsi_ble_on_gatt_write_event(uint16_t event_id, rsi_ble_event_write_t * rsi_
8867
bleEvent.eventType = RSI_BLE_GATT_WRITE_EVENT;
8968
bleEvent.eventData->event_id = event_id;
9069
memcpy(&bleEvent.eventData->rsi_ble_write, rsi_ble_write, sizeof(rsi_ble_event_write_t));
91-
BlePostEvent(&bleEvent);
70+
chip::DeviceLayer::Internal::BLEMgrImpl().BlePostEvent(&bleEvent);
9271
}
9372

9473
/*==============================================*/
@@ -106,7 +85,7 @@ void rsi_ble_on_enhance_conn_status_event(rsi_ble_event_enhance_conn_status_t *
10685
bleEvent.eventData->connectionHandle = 1;
10786
bleEvent.eventData->bondingHandle = 255;
10887
memcpy(bleEvent.eventData->resp_enh_conn.dev_addr, resp_enh_conn->dev_addr, RSI_DEV_ADDR_LEN);
109-
BlePostEvent(&bleEvent);
88+
chip::DeviceLayer::Internal::BLEMgrImpl().BlePostEvent(&bleEvent);
11089
}
11190

11291
/*==============================================*/
@@ -123,7 +102,7 @@ void rsi_ble_on_disconnect_event(rsi_ble_event_disconnect_t * resp_disconnect, u
123102
{
124103
bleEvent.eventType = RSI_BLE_DISCONN_EVENT;
125104
bleEvent.eventData->reason = reason;
126-
BlePostEvent(&bleEvent);
105+
chip::DeviceLayer::Internal::BLEMgrImpl().BlePostEvent(&bleEvent);
127106
}
128107

129108
/*==============================================*/
@@ -140,7 +119,7 @@ void rsi_ble_on_event_indication_confirmation(uint16_t resp_status, rsi_ble_set_
140119
bleEvent.eventType = RSI_BLE_GATT_INDICATION_CONFIRMATION;
141120
bleEvent.eventData->resp_status = resp_status;
142121
memcpy(&bleEvent.eventData->rsi_ble_event_set_att_rsp, rsi_ble_event_set_att_rsp, sizeof(rsi_ble_set_att_resp_t));
143-
BlePostEvent(&bleEvent);
122+
chip::DeviceLayer::Internal::BLEMgrImpl().BlePostEvent(&bleEvent);
144123
}
145124

146125
/*==============================================*/
@@ -158,7 +137,7 @@ void rsi_ble_on_read_req_event(uint16_t event_id, rsi_ble_read_req_t * rsi_ble_r
158137
bleEvent.eventType = RSI_BLE_EVENT_GATT_RD;
159138
bleEvent.eventData->event_id = event_id;
160139
memcpy(&bleEvent.eventData->rsi_ble_read_req, rsi_ble_read_req, sizeof(rsi_ble_read_req_t));
161-
BlePostEvent(&bleEvent);
140+
chip::DeviceLayer::Internal::BLEMgrImpl().BlePostEvent(&bleEvent);
162141
}
163142

164143
/*==============================================*/
@@ -260,7 +239,7 @@ void rsi_ble_add_char_val_att(void * serv_handler, uint16_t handle, uuid_t att_t
260239
uint8_t data_len, uint8_t auth_read)
261240
{
262241
rsi_ble_req_add_att_t new_att = { 0 };
263-
242+
rsi_ble_t att_list;
264243
memset(&new_att, 0, sizeof(rsi_ble_req_add_att_t));
265244
//! preparing the attributes
266245
new_att.serv_handler = serv_handler;

0 commit comments

Comments
 (0)