@@ -80,32 +80,43 @@ using namespace ::chip;
80
80
using namespace ::chip::Ble;
81
81
using namespace ::chip::DeviceLayer::Internal;
82
82
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 {
91
86
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 {
95
88
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
100
93
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)
107
109
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)
109
120
{
110
121
switch (inEvent.eventType )
111
122
{
@@ -154,7 +165,17 @@ void ProcessEvent(BleEvent_t inEvent)
154
165
}
155
166
}
156
167
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)
158
179
{
159
180
sl_status_t status;
160
181
BleEvent_t bleEvent;
@@ -163,15 +184,15 @@ void sl_ble_event_handling_task(void * args)
163
184
osSemaphoreAcquire (sl_rs_ble_init_sem, osWaitForever);
164
185
165
186
// This function initialize BLE and start BLE advertisement.
166
- sl_ble_init ();
187
+ sInstance . sl_ble_init ();
167
188
168
189
// Application event map
169
190
while (1 )
170
191
{
171
- status = osMessageQueueGet (GetBleEventQueue () , &bleEvent, NULL , osWaitForever);
192
+ status = osMessageQueueGet (sInstance . sBleEventQueue , &bleEvent, NULL , osWaitForever);
172
193
if (status == osOK)
173
194
{
174
- ProcessEvent (bleEvent);
195
+ sInstance . ProcessEvent (bleEvent);
175
196
}
176
197
else
177
198
{
@@ -180,48 +201,41 @@ void sl_ble_event_handling_task(void * args)
180
201
}
181
202
}
182
203
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 ;
205
212
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 );
209
216
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 );
213
221
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 );
215
228
216
- BLEManagerImpl BLEManagerImpl::sInstance ;
229
+ chip::DeviceLayer::Internal::BLEMgrImpl ().HandleBootEvent ();
230
+ }
217
231
218
232
CHIP_ERROR BLEManagerImpl::_Init ()
219
233
{
220
234
CHIP_ERROR err;
221
235
222
236
sl_rs_ble_init_sem = osSemaphoreNew (1 , 0 , NULL );
223
237
224
- sBleThread = osThreadNew (sl_ble_event_handling_task, NULL , &kBleTaskAttr );
238
+ sBleThread = osThreadNew (sInstance . sl_ble_event_handling_task , NULL , &kBleTaskAttr );
225
239
226
240
VerifyOrReturnError (sBleThread != nullptr , CHIP_ERROR_INCORRECT_STATE);
227
241
0 commit comments