blob: 917cdc267e4d245ba8d01a8c60a0f8154f3b7b3f [file] [log] [blame]
Praveen Chandran080ae572021-08-12 07:58:27 -07001/*
2 *
Evgeny Margolis15ac9172022-09-22 11:41:42 -07003 * Copyright (c) 2021-2022 Project CHIP Authors
Praveen Chandran080ae572021-08-12 07:58:27 -07004 * Copyright (c) 2020 Nest Labs, Inc.
5 * All rights reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20/**
21 * @file
22 * Provides an implementation of the BLEManager singleton object
23 * for the PSoC6 platform.
24 */
25
26/* this file behaves like a config.h, comes first */
27#include <platform/internal/CHIPDeviceLayerInternal.h>
28
29#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
30
31#include <ble/CHIPBleServiceData.h>
Evgeny Margolis15ac9172022-09-22 11:41:42 -070032#include <lib/support/CHIPMemString.h>
Zang MingJie53dd5832021-09-03 03:05:16 +080033#include <lib/support/CodeUtils.h>
34#include <lib/support/logging/CHIPLogging.h>
Praveen Chandran080ae572021-08-12 07:58:27 -070035#include <platform/internal/BLEManager.h>
Praveen Chandran080ae572021-08-12 07:58:27 -070036
37extern "C" {
38#include "app_platform_cfg.h"
39#include "cycfg_bt_settings.h"
40#include "cycfg_gatt_db.h"
41}
42
43#include "cy_utils.h"
44#include "wiced_bt_stack.h"
45
Praveen Chandrana15e2b02022-07-18 16:54:00 -070046#include "wiced_memory.h"
Praveen Chandran080ae572021-08-12 07:58:27 -070047#include <wiced_bt_ble.h>
48#include <wiced_bt_gatt.h>
49
50using namespace ::chip;
51using namespace ::chip::Ble;
52
Praveen Chandran7b28de42022-01-17 12:00:07 -080053#define BLE_SERVICE_DATA_SIZE 10
Praveen Chandrana15e2b02022-07-18 16:54:00 -070054#define BT_STACK_HEAP_SIZE (1024 * 6)
55typedef void (*pfn_free_buffer_t)(uint8_t *);
56wiced_bt_heap_t * p_heap = NULL;
57static bool heap_allocated = false;
Praveen Chandran7b28de42022-01-17 12:00:07 -080058
Praveen Chandran080ae572021-08-12 07:58:27 -070059namespace chip {
60namespace DeviceLayer {
61namespace Internal {
62
63namespace {
64const ChipBleUUID chipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
65 0x9D, 0x11 } };
66
67const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
68 0x9D, 0x12 } };
69
70} // unnamed namespace
71
72BLEManagerImpl BLEManagerImpl::sInstance;
73
74wiced_bt_gatt_status_t app_gatts_callback(wiced_bt_gatt_evt_t event, wiced_bt_gatt_event_data_t * p_data);
75
76wiced_result_t BLEManagerImpl::BLEManagerCallback(wiced_bt_management_evt_t event, wiced_bt_management_evt_data_t * p_event_data)
77{
78 switch (event)
79 {
80 case BTM_ENABLED_EVT:
81 // Post a event to _OnPlatformEvent.
82 {
83 // Register with stack to receive GATT callback
84 wiced_bt_gatt_register(app_gatts_callback);
85
86 // Inform the stack to use this app GATT database
87 wiced_bt_gatt_db_init(gatt_database, gatt_database_len, NULL);
88
89 ChipDeviceEvent bleEvent;
90 bleEvent.Type = DeviceEventType::kP6BLEEnabledEvt;
Kevin Schoedel6fd38112021-09-14 13:22:09 -040091 if (PlatformMgr().PostEvent(&bleEvent) != CHIP_NO_ERROR)
92 {
93 return WICED_BT_ERROR;
94 }
Praveen Chandran080ae572021-08-12 07:58:27 -070095 }
96 break;
97 }
98
99 return WICED_BT_SUCCESS;
100}
101
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700102uint8_t * BLEManagerImpl::gatt_alloc_buffer(uint16_t len)
103{
104 uint8_t * p = (uint8_t *) wiced_bt_get_buffer(len);
105 return p;
106}
107
108void BLEManagerImpl::gatt_free_buffer(uint8_t * p_data)
109{
110 wiced_bt_free_buffer(p_data);
111}
112
113static void gatt_free_buffer_cb(uint8_t * p_data)
114{
115 BLEManagerImpl::sInstance.gatt_free_buffer(p_data);
116}
117
Praveen Chandran080ae572021-08-12 07:58:27 -0700118CHIP_ERROR BLEManagerImpl::_Init()
119{
120 CHIP_ERROR err;
121
122 // Initialize the CHIP BleLayer.
Kevin Schoedel86eb0402021-09-10 08:37:38 -0400123 err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
Praveen Chandran080ae572021-08-12 07:58:27 -0700124 SuccessOrExit(err);
125
126 // Configure platform specific settings for Bluetooth
127 cybt_platform_config_init(&bt_platform_cfg_settings);
128
129 // Initialize the Bluetooth stack with a callback function and stack
130 // configuration structure */
131 if (WICED_SUCCESS != wiced_bt_stack_init(BLEManagerCallback, &wiced_bt_cfg_settings))
132 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700133 ChipLogError(DeviceLayer, "Error initializing BT stack\n");
Praveen Chandran080ae572021-08-12 07:58:27 -0700134 CY_ASSERT(0);
135 }
136
137 mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;
138 if (CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART)
139 {
140 mFlags.Set(Flags::kFlag_AdvertisingEnabled, true);
141 }
142 else
143 {
144 mFlags.Set(Flags::kFlag_AdvertisingEnabled, false);
145 }
146 mNumCons = 0;
147 memset(mCons, 0, sizeof(mCons));
148 memset(mDeviceName, 0, sizeof(mDeviceName));
149
150 ChipLogProgress(DeviceLayer, "BLEManagerImpl::Init() complete");
151
152 PlatformMgr().ScheduleWork(DriveBLEState, 0);
153
154exit:
155 return err;
156}
157
Praveen Chandran080ae572021-08-12 07:58:27 -0700158bool BLEManagerImpl::_IsAdvertisingEnabled(void)
159{
160 return mFlags.Has(Flags::kFlag_AdvertisingEnabled);
161}
162
163CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
164{
165 CHIP_ERROR err = CHIP_NO_ERROR;
166
167 VerifyOrExit(mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported, err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE);
168
169 if (mFlags.Has(Flags::kFlag_AdvertisingEnabled) != val)
170 {
171 mFlags.Set(Flags::kFlag_AdvertisingEnabled, val);
172 PlatformMgr().ScheduleWork(DriveBLEState, 0);
173 }
174
175exit:
176 return err;
177}
178
Praveen Chandran080ae572021-08-12 07:58:27 -0700179CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
180{
181 (void) (mode);
182
183 return CHIP_ERROR_NOT_IMPLEMENTED;
184}
185
186CHIP_ERROR BLEManagerImpl::_GetDeviceName(char * buf, size_t bufSize)
187{
188 if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_NotSupported)
189 {
190 return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
191 }
192 if (strlen(mDeviceName) >= bufSize)
193 {
194 return CHIP_ERROR_BUFFER_TOO_SMALL;
195 }
196 strcpy(buf, mDeviceName);
197 ChipLogProgress(DeviceLayer, "Getting device name to : \"%s\"", mDeviceName);
198 return CHIP_NO_ERROR;
199}
200
201CHIP_ERROR BLEManagerImpl::_SetDeviceName(const char * deviceName)
202{
203 if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_NotSupported)
204 {
205 return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
206 }
207 if (deviceName != NULL && deviceName[0] != 0)
208 {
209 if (strlen(deviceName) >= kMaxDeviceNameLength)
210 {
211 return CHIP_ERROR_INVALID_ARGUMENT;
212 }
Evgeny Margolis15ac9172022-09-22 11:41:42 -0700213 Platform::CopyString(mDeviceName, deviceName);
Praveen Chandran080ae572021-08-12 07:58:27 -0700214 mFlags.Set(Flags::kFlag_DeviceNameSet, true);
215 ChipLogProgress(DeviceLayer, "Setting device name to : \"%s\"", deviceName);
216 }
217 else
218 {
219 wiced_bt_cfg_settings.device_name[0] = 0;
220 mDeviceName[0] = 0;
221 mFlags.Set(Flags::kFlag_DeviceNameSet, false);
222 }
223
224 return CHIP_NO_ERROR;
225}
226
227uint16_t BLEManagerImpl::_NumConnections(void)
228{
229 return mNumCons;
230}
231
232void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
233{
234 switch (event->Type)
235 {
236
237 case DeviceEventType::kP6BLEEnabledEvt:
238 mFlags.Set(Flags::kFlag_StackInitialized, true);
239 PlatformMgr().ScheduleWork(DriveBLEState, 0);
240 break;
241
242 case DeviceEventType::kCHIPoBLESubscribe:
243 HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &CHIP_BLE_SVC_ID, &ChipUUID_CHIPoBLEChar_TX);
244 {
245 ChipDeviceEvent _event;
246 _event.Type = DeviceEventType::kCHIPoBLEConnectionEstablished;
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400247 PlatformMgr().PostEventOrDie(&_event);
Praveen Chandran080ae572021-08-12 07:58:27 -0700248 }
249 break;
250
251 case DeviceEventType::kCHIPoBLEUnsubscribe:
252 HandleUnsubscribeReceived(event->CHIPoBLEUnsubscribe.ConId, &CHIP_BLE_SVC_ID, &ChipUUID_CHIPoBLEChar_TX);
253 break;
254
255 case DeviceEventType::kCHIPoBLEWriteReceived:
256 HandleWriteReceived(event->CHIPoBLEWriteReceived.ConId, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_RX,
257 PacketBufferHandle::Adopt(event->CHIPoBLEWriteReceived.Data));
258 break;
259
Praveen Chandrand0596802022-08-09 17:56:44 -0700260 case DeviceEventType::kCHIPoBLEIndicateConfirm:
261 HandleIndicationConfirmation(event->CHIPoBLEIndicateConfirm.ConId, &CHIP_BLE_SVC_ID, &ChipUUID_CHIPoBLEChar_TX);
Praveen Chandran080ae572021-08-12 07:58:27 -0700262 break;
263
264 case DeviceEventType::kCHIPoBLEConnectionError:
265 HandleConnectionError(event->CHIPoBLEConnectionError.ConId, event->CHIPoBLEConnectionError.Reason);
266 break;
267
Praveen Chandran080ae572021-08-12 07:58:27 -0700268 case DeviceEventType::kServiceProvisioningChange:
Praveen Chandran080ae572021-08-12 07:58:27 -0700269 // Force the advertising state to be refreshed to reflect new provisioning state.
270 mFlags.Set(Flags::kFlag_AdvertisingRefreshNeeded, true);
271
272 DriveBLEState();
273 break;
274
275 default:
276 break;
277 }
278}
279
280bool BLEManagerImpl::SubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId)
281{
282 ChipLogProgress(DeviceLayer, "BLEManagerImpl::SubscribeCharacteristic() not supported");
283 return false;
284}
285
286bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId)
287{
288 ChipLogProgress(DeviceLayer, "BLEManagerImpl::UnsubscribeCharacteristic() not supported");
289 return false;
290}
291
292bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId)
293{
294 ChipLogProgress(DeviceLayer, "Closing BLE GATT connection (con %u)", conId);
295
296 // Initiate a GAP disconnect.
297 wiced_bt_gatt_status_t gatt_err = wiced_bt_gatt_disconnect((uint16_t) conId);
298 if (gatt_err != WICED_BT_GATT_SUCCESS)
299 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700300 ChipLogError(DeviceLayer, "wiced_bt_gatt_disconnect() failed: %d", gatt_err);
Praveen Chandran080ae572021-08-12 07:58:27 -0700301 }
302
303 return (gatt_err == WICED_BT_GATT_SUCCESS);
304}
305
306uint16_t BLEManagerImpl::GetMTU(BLE_CONNECTION_OBJECT conId) const
307{
308 CHIPoBLEConState * p_conn;
309
310 /* Check if target connection state exists. */
311 p_conn = BLEManagerImpl::sInstance.GetConnectionState(conId);
312
313 if (!p_conn)
314 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700315 return wiced_bt_cfg_settings.p_ble_cfg->ble_max_rx_pdu_size;
Praveen Chandran080ae572021-08-12 07:58:27 -0700316 }
317 else
318 {
319 return p_conn->Mtu;
320 }
321}
322
323bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
324 PacketBufferHandle data)
325{
326 CHIP_ERROR err = CHIP_NO_ERROR;
327 uint16_t dataLen = data->DataLength();
328 wiced_bt_gatt_status_t gatt_err = WICED_BT_GATT_SUCCESS;
329 CHIPoBLEConState * conState = GetConnectionState(conId);
330
331 VerifyOrExit(conState != NULL, err = CHIP_ERROR_INVALID_ARGUMENT);
332
333#ifdef BLE_DEBUG
Praveen Chandrand0596802022-08-09 17:56:44 -0700334 ChipLogDetail(DeviceLayer, "Sending indication for CHIPoBLE TX characteristic (con %u, len %u)", conId, dataLen);
Praveen Chandran080ae572021-08-12 07:58:27 -0700335#endif
336
Praveen Chandrand0596802022-08-09 17:56:44 -0700337 // Send a indication for the CHIPoBLE TX characteristic to the client containing the supplied data.
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700338 gatt_err =
Praveen Chandrand0596802022-08-09 17:56:44 -0700339 wiced_bt_gatt_server_send_indication((uint16_t) conId, HDLC_CHIP_SERVICE_CHAR_C2_VALUE, dataLen, data->Start(), NULL);
Praveen Chandran080ae572021-08-12 07:58:27 -0700340
341exit:
342 if (gatt_err != WICED_BT_GATT_SUCCESS)
343 {
Praveen Chandrand0596802022-08-09 17:56:44 -0700344 ChipLogError(DeviceLayer, "BLEManagerImpl::SendIndication() failed: %d", gatt_err);
Praveen Chandran080ae572021-08-12 07:58:27 -0700345 return false;
346 }
347 return err == CHIP_NO_ERROR;
348}
349
350bool BLEManagerImpl::SendWriteRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
351 PacketBufferHandle data)
352
353{
354 ChipLogError(DeviceLayer, "BLEManagerImpl::SendWriteRequest() not supported");
355 return false;
356}
357
358bool BLEManagerImpl::SendReadRequest(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId,
359 PacketBufferHandle data)
360{
361 ChipLogError(DeviceLayer, "BLEManagerImpl::SendReadRequest() not supported");
362 return false;
363}
364
365bool BLEManagerImpl::SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQUEST_CONTEXT requestContext,
366 const ChipBleUUID * svcId, const ChipBleUUID * charId)
367{
368 ChipLogError(DeviceLayer, "BLEManagerImpl::SendReadResponse() not supported");
369 return false;
370}
371
372void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) {}
373
374void BLEManagerImpl::DriveBLEState(void)
375{
376 CHIP_ERROR err = CHIP_NO_ERROR;
377
378 // Exit if Stack not initialized
379 VerifyOrExit(mFlags.Has(Flags::kFlag_StackInitialized), /* */);
380
381 // Perform any initialization actions that must occur after the CHIP task is running.
382 if (!mFlags.Has(Flags::kFlag_AsyncInitCompleted))
383 {
384 mFlags.Set(Flags::kFlag_AsyncInitCompleted, true);
Praveen Chandran080ae572021-08-12 07:58:27 -0700385 }
386
387 // If the application has enabled CHIPoBLE and BLE advertising...
388 if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled &&
389 mFlags.Has(Flags::kFlag_AdvertisingEnabled)
390#if CHIP_DEVICE_CONFIG_CHIPOBLE_SINGLE_CONNECTION
391 // and no connections are active...
392 && (mNumCons == 0)
393#endif
394 )
395 {
396 // Start/re-start SoftDevice advertising if not already advertising, or if the
397 // advertising state of the SoftDevice needs to be refreshed.
398 if (!mFlags.Has(Flags::kFlag_Advertising) || mFlags.Has(Flags::kFlag_AdvertisingRefreshNeeded))
399 {
400 ChipLogProgress(DeviceLayer, "CHIPoBLE advertising started");
401
402 mFlags.Set(Flags::kFlag_Advertising, true);
403 mFlags.Set(Flags::kFlag_AdvertisingRefreshNeeded, false);
404
405 SetAdvertisingData();
406
407 wiced_bt_start_advertisements(BTM_BLE_ADVERT_UNDIRECTED_HIGH, BLE_ADDR_PUBLIC, NULL);
408
409 // Post a CHIPoBLEAdvertisingChange(Started) event.
410 {
411 ChipDeviceEvent advChange;
412 advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange;
413 advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Started;
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400414 err = PlatformMgr().PostEvent(&advChange);
Praveen Chandran080ae572021-08-12 07:58:27 -0700415 }
416 }
417 }
418
419 // Otherwise, stop advertising if currently active.
420 else
421 {
422 if (mFlags.Has(Flags::kFlag_Advertising))
423 {
424 mFlags.Set(Flags::kFlag_Advertising, false);
425
426 ChipLogProgress(DeviceLayer, "CHIPoBLE stop advertising");
427 wiced_bt_start_advertisements(BTM_BLE_ADVERT_OFF, BLE_ADDR_PUBLIC, NULL);
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700428
Arkadiusz Bokowybd6c4ab2024-02-06 08:57:29 +0100429 /* Delete the heap allocated during BLE Advertisement Stop */
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700430 if (p_heap)
431 {
432 wiced_bt_delete_heap(p_heap);
433 heap_allocated = false;
434 }
Praveen Chandran080ae572021-08-12 07:58:27 -0700435 }
436 }
437
438exit:
439 if (err != CHIP_NO_ERROR)
440 {
441 ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %s", ErrorStr(err));
442 mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled;
443 }
444}
445
446/*
447 * This function searches through the GATT DB to point to the attribute
448 * corresponding to the given handle
449 */
450gatt_db_lookup_table_t * BLEManagerImpl::GetGattAttr(uint16_t handle)
451{
452 /* Search for the given handle in the GATT DB and return the pointer to the
453 correct attribute */
454 uint8_t array_index = 0;
455
456 for (array_index = 0; array_index < app_gatt_db_ext_attr_tbl_size; array_index++)
457 {
458 if (app_gatt_db_ext_attr_tbl[array_index].handle == handle)
459 {
460 return (&app_gatt_db_ext_attr_tbl[array_index]);
461 }
462 }
463 return NULL;
464}
465
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700466wiced_bt_gatt_status_t BLEManagerImpl::HandleGattServiceRead(uint16_t conn_id, wiced_bt_gatt_opcode_t opcode,
467 wiced_bt_gatt_read_t * p_read_req, uint16_t len_requested)
Praveen Chandran080ae572021-08-12 07:58:27 -0700468{
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700469 gatt_db_lookup_table_t * p_attribute;
470 uint8_t * from;
Praveen Chandran080ae572021-08-12 07:58:27 -0700471
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700472 if ((p_attribute = GetGattAttr(p_read_req->handle)) == NULL)
Praveen Chandran080ae572021-08-12 07:58:27 -0700473 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700474 ChipLogError(DeviceLayer, "[%s] attr not found handle: 0x%04x\n", __FUNCTION__, p_read_req->handle);
475 wiced_bt_gatt_server_send_error_rsp(conn_id, opcode, p_read_req->handle, WICED_BT_GATT_INVALID_HANDLE);
Praveen Chandran080ae572021-08-12 07:58:27 -0700476 return WICED_BT_GATT_INVALID_HANDLE;
477 }
478
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700479 if (p_read_req->offset >= p_attribute->cur_len)
Praveen Chandran080ae572021-08-12 07:58:27 -0700480 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700481 ChipLogError(DeviceLayer, "[%s] offset:%d larger than attribute length:%d\n", __FUNCTION__, p_read_req->offset,
482 p_attribute->cur_len);
483
484 wiced_bt_gatt_server_send_error_rsp(conn_id, opcode, p_read_req->handle, WICED_BT_GATT_INVALID_OFFSET);
485 return (WICED_BT_GATT_INVALID_OFFSET);
486 }
487 else if (len_requested + p_read_req->offset > p_attribute->cur_len)
488 {
489 len_requested = p_attribute->cur_len - p_read_req->offset;
Praveen Chandran080ae572021-08-12 07:58:27 -0700490 }
491
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700492 from = ((uint8_t *) p_attribute->p_data) + p_read_req->offset;
Praveen Chandran080ae572021-08-12 07:58:27 -0700493
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700494 wiced_bt_gatt_server_send_read_handle_rsp(conn_id, opcode, len_requested, from, NULL);
495
496 return WICED_BT_GATT_SUCCESS;
497}
498/*
499 * Currently there is no reason to pass Read Req by type handler to CHIP. Only process request for
500 * attributes in the GATT DB attribute table
501 */
502wiced_bt_gatt_status_t BLEManagerImpl::HandleGattServiceReadByTypeHandler(uint16_t conn_id, wiced_bt_gatt_opcode_t opcode,
503 wiced_bt_gatt_read_by_type_t * p_read_req,
504 uint16_t len_requested)
505{
506 gatt_db_lookup_table_t * puAttribute;
507 uint16_t attr_handle = p_read_req->s_handle;
508 uint8_t * p_rsp = NULL;
509 uint8_t pair_len = 0;
510 int used = 0;
511
512 if (heap_allocated == false)
513 {
514 p_heap = wiced_bt_create_heap("default_heap", NULL, BT_STACK_HEAP_SIZE, NULL, WICED_TRUE);
515 heap_allocated = true;
516 }
517
518 /* Allocate buffer for GATT Read */
519 p_rsp = gatt_alloc_buffer(len_requested);
520 if (p_rsp == NULL)
521 {
522 ChipLogError(DeviceLayer, "[%s] no memory len_requested: %d!!\n", __FUNCTION__, len_requested);
523 wiced_bt_gatt_server_send_error_rsp(conn_id, opcode, attr_handle, WICED_BT_GATT_INSUF_RESOURCE);
524 return WICED_BT_GATT_INSUF_RESOURCE;
525 }
526
527 /* Read by type returns all attributes of the specified type, between the start and end handles */
528 while (WICED_TRUE)
529 {
530 attr_handle = wiced_bt_gatt_find_handle_by_type(attr_handle, p_read_req->e_handle, &p_read_req->uuid);
531
532 if (attr_handle == 0)
533 break;
534
535 if ((puAttribute = GetGattAttr(attr_handle)) == NULL)
Praveen Chandran080ae572021-08-12 07:58:27 -0700536 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700537 ChipLogError(DeviceLayer, "[%s] found type but no attribute ??\n", __FUNCTION__);
538 wiced_bt_gatt_server_send_error_rsp(conn_id, opcode, p_read_req->s_handle, WICED_BT_GATT_ERR_UNLIKELY);
539 gatt_free_buffer(p_rsp);
540 return WICED_BT_GATT_INVALID_HANDLE;
Praveen Chandran080ae572021-08-12 07:58:27 -0700541 }
542
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700543 {
544 int filled = wiced_bt_gatt_put_read_by_type_rsp_in_stream(p_rsp + used, len_requested - used, &pair_len, attr_handle,
545 puAttribute->cur_len, puAttribute->p_data);
546 if (filled == 0)
547 {
548 break;
549 }
550 used += filled;
551 }
Praveen Chandran080ae572021-08-12 07:58:27 -0700552
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700553 /* Increment starting handle for next search to one past current */
554 attr_handle++;
Praveen Chandran080ae572021-08-12 07:58:27 -0700555 }
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700556
557 if (used == 0)
558 {
559 ChipLogError(DeviceLayer, "[%s] attr not found start_handle: 0x%04x end_handle: 0x%04x Type: 0x%04x\n", __FUNCTION__,
560 p_read_req->s_handle, p_read_req->e_handle, p_read_req->uuid.uu.uuid16);
561 wiced_bt_gatt_server_send_error_rsp(conn_id, opcode, p_read_req->s_handle, WICED_BT_GATT_INVALID_HANDLE);
562 gatt_free_buffer(p_rsp);
563 return WICED_BT_GATT_INVALID_HANDLE;
564 }
565
566 /* Send the response */
567 wiced_bt_gatt_server_send_read_by_type_rsp(conn_id, opcode, pair_len, used, p_rsp,
568 (wiced_bt_gatt_app_context_t) gatt_free_buffer_cb);
569
Praveen Chandran080ae572021-08-12 07:58:27 -0700570 return WICED_BT_GATT_SUCCESS;
571}
572
573/*
574 * If Attribute is for CHIP, pass it through. Otherwise process request for
575 * attributes in the GATT DB attribute table.
576 */
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700577wiced_bt_gatt_status_t BLEManagerImpl::HandleGattServiceWrite(uint16_t conn_id, wiced_bt_gatt_write_req_t * p_data)
Praveen Chandran080ae572021-08-12 07:58:27 -0700578{
579 wiced_bt_gatt_status_t result = WICED_BT_GATT_SUCCESS;
580 gatt_db_lookup_table_t * puAttribute;
581 const uint16_t valLen = p_data->val_len;
Praveen Chandran080ae572021-08-12 07:58:27 -0700582 // special handling for CHIP RX path
583 if (p_data->handle == HDLC_CHIP_SERVICE_CHAR_C1_VALUE)
584 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700585 System::PacketBufferHandle buf;
Praveen Chandran080ae572021-08-12 07:58:27 -0700586
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700587 buf = System::PacketBufferHandle::NewWithData(p_data->p_val, valLen, 0, 0);
588 if (!buf.IsNull())
Praveen Chandran080ae572021-08-12 07:58:27 -0700589 {
Praveen Chandran080ae572021-08-12 07:58:27 -0700590#ifdef BLE_DEBUG
591 ChipLogDetail(DeviceLayer, "Write received for CHIPoBLE RX characteristic con %04x len %d", conn_id, valLen);
592#endif
593 // Post an event to the CHIP queue to deliver the data into the CHIP stack.
594 {
595 ChipDeviceEvent event;
596 event.Type = DeviceEventType::kCHIPoBLEWriteReceived;
597 event.CHIPoBLEWriteReceived.ConId = conn_id;
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700598 event.CHIPoBLEWriteReceived.Data = std::move(buf).UnsafeRelease();
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400599 CHIP_ERROR status = PlatformMgr().PostEvent(&event);
600 if (status != CHIP_NO_ERROR)
601 {
602 result = WICED_BT_GATT_INTERNAL_ERROR;
603 }
Praveen Chandran080ae572021-08-12 07:58:27 -0700604 buf = NULL;
605 }
606 }
607 else
608 {
609 ChipLogError(DeviceLayer, "BLEManagerImpl: Out of buffers during CHIPoBLE RX");
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400610 result = WICED_BT_GATT_NO_RESOURCES;
Praveen Chandran080ae572021-08-12 07:58:27 -0700611 }
612 }
613 else
614 {
615 ChipLogDetail(DeviceLayer, "Write received for CHIPoBLE RX characteristic con:%04x handle:%04x len:%d", conn_id,
616 p_data->handle, valLen);
617
618 /* Get the right address for the handle in Gatt DB */
619 if (NULL == (puAttribute = GetGattAttr(p_data->handle)))
620 {
621 ChipLogError(DeviceLayer, "BLEManagerImpl: Write wrong handle:%04x", p_data->handle);
622 return WICED_BT_GATT_INVALID_HANDLE;
623 }
624 puAttribute->cur_len = valLen > puAttribute->max_len ? puAttribute->max_len : valLen;
625 memcpy(puAttribute->p_data, p_data->p_val, puAttribute->cur_len);
626
627 // Post an event to the Chip queue to process either a CHIPoBLE Subscribe or Unsubscribe based on
628 // whether the client is enabling or disabling indications.
629 if (p_data->handle == HDLD_CHIP_SERVICE_RX_CLIENT_CHAR_CONFIG)
630 {
631 ChipDeviceEvent event;
632 event.Type = (app_chip_service_char_tx_client_char_config[0] != 0) ? DeviceEventType::kCHIPoBLESubscribe
633 : DeviceEventType::kCHIPoBLEUnsubscribe;
634 event.CHIPoBLESubscribe.ConId = conn_id;
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400635 if (PlatformMgr().PostEvent(&event) != CHIP_NO_ERROR)
636 {
637 return WICED_BT_GATT_INTERNAL_ERROR;
638 }
Praveen Chandran080ae572021-08-12 07:58:27 -0700639 }
640
641 ChipLogProgress(DeviceLayer, "CHIPoBLE %s received",
642 app_chip_service_char_tx_client_char_config[0] != 0 ? "subscribe" : "unsubscribe");
643 }
644 return result;
645}
646
647/*
648 * Process MTU request received from the GATT client
649 */
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700650wiced_bt_gatt_status_t BLEManagerImpl::HandleGattServiceMtuReq(uint16_t conn_id, uint16_t mtu)
Praveen Chandran080ae572021-08-12 07:58:27 -0700651{
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700652 wiced_bt_gatt_server_send_mtu_rsp(conn_id, mtu, wiced_bt_cfg_settings.p_ble_cfg->ble_max_rx_pdu_size);
Praveen Chandran080ae572021-08-12 07:58:27 -0700653 return WICED_BT_GATT_SUCCESS;
654}
655
656/*
Praveen Chandrand0596802022-08-09 17:56:44 -0700657 * Process GATT Indication Confirm from the client
658 */
659wiced_bt_gatt_status_t BLEManagerImpl::HandleGattServiceIndCfm(uint16_t conn_id, uint16_t handle)
660{
661#ifdef BLE_DEBUG
662 ChipLogDetail(DeviceLayer, "GATT Ind Cfm received con:%04x handle:%d", conn_id, handle);
663#endif
664 if (handle == HDLC_CHIP_SERVICE_CHAR_C2_VALUE)
665 {
666 ChipDeviceEvent event;
667 event.Type = DeviceEventType::kCHIPoBLEIndicateConfirm;
668 event.CHIPoBLEIndicateConfirm.ConId = conn_id;
669 if (PlatformMgr().PostEvent(&event) != CHIP_NO_ERROR)
670 {
671 return WICED_BT_GATT_INTERNAL_ERROR;
672 }
673 }
674 return WICED_BT_GATT_SUCCESS;
675}
676
677/*
Praveen Chandran080ae572021-08-12 07:58:27 -0700678 * Process GATT attribute requests
679 */
680wiced_bt_gatt_status_t BLEManagerImpl::HandleGattServiceRequestEvent(wiced_bt_gatt_attribute_request_t * p_request,
681 CHIPoBLEConState * p_conn)
682{
683 wiced_bt_gatt_status_t result = WICED_BT_GATT_INVALID_PDU;
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700684 switch (p_request->opcode)
Praveen Chandran080ae572021-08-12 07:58:27 -0700685 {
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700686 case GATT_REQ_READ:
687 case GATT_REQ_READ_BLOB:
688 result =
689 HandleGattServiceRead(p_request->conn_id, p_request->opcode, &(p_request->data.read_req), p_request->len_requested);
Praveen Chandran080ae572021-08-12 07:58:27 -0700690 break;
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700691 case GATT_REQ_READ_BY_TYPE:
692 result = HandleGattServiceReadByTypeHandler(p_request->conn_id, p_request->opcode, &p_request->data.read_by_type,
693 p_request->len_requested);
694 break;
695 case GATT_REQ_WRITE:
696 case GATT_CMD_WRITE:
Praveen Chandran080ae572021-08-12 07:58:27 -0700697 result = HandleGattServiceWrite(p_request->conn_id, &(p_request->data.write_req));
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700698 if ((p_request->opcode == GATT_REQ_WRITE) && (result == WICED_BT_GATT_SUCCESS))
699 {
700 wiced_bt_gatt_write_req_t * p_write_request = &p_request->data.write_req;
701 wiced_bt_gatt_server_send_write_rsp(p_request->conn_id, p_request->opcode, p_write_request->handle);
702 }
Praveen Chandran080ae572021-08-12 07:58:27 -0700703 break;
704
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700705 case GATT_REQ_MTU:
706 result = HandleGattServiceMtuReq(p_request->conn_id, p_request->data.remote_mtu);
Praveen Chandran080ae572021-08-12 07:58:27 -0700707 break;
708
Praveen Chandrand0596802022-08-09 17:56:44 -0700709 case GATT_HANDLE_VALUE_CONF:
710 result = HandleGattServiceIndCfm(p_request->conn_id, p_request->data.confirm.handle);
711 break;
712
Praveen Chandran080ae572021-08-12 07:58:27 -0700713 default:
714 break;
715 }
716
717 return result;
718}
719
720/*
721 * Handle GATT connection events from the stack
722 */
723wiced_bt_gatt_status_t BLEManagerImpl::HandleGattConnectEvent(wiced_bt_gatt_connection_status_t * p_conn_status,
724 CHIPoBLEConState * p_conn)
725{
726 if (p_conn_status->connected)
727 {
728 /* Device got connected */
729 p_conn->connected = true;
730 ChipLogProgress(DeviceLayer, "BLE GATT connection up (con %u)", p_conn_status->conn_id);
731 }
732 else /* Device got disconnected */
733 {
734 ChipDeviceEvent event;
735 event.Type = DeviceEventType::kCHIPoBLEConnectionError;
736 event.CHIPoBLEConnectionError.ConId = p_conn_status->conn_id;
737
738 switch (p_conn_status->reason)
739 {
740 case GATT_CONN_TERMINATE_PEER_USER:
741 event.CHIPoBLEConnectionError.Reason = BLE_ERROR_REMOTE_DEVICE_DISCONNECTED;
742 break;
743
744 case GATT_CONN_TERMINATE_LOCAL_HOST:
745 event.CHIPoBLEConnectionError.Reason = BLE_ERROR_APP_CLOSED_CONNECTION;
746 break;
747
748 default:
749 event.CHIPoBLEConnectionError.Reason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT;
750 break;
751 }
752
753 ChipLogProgress(DeviceLayer, "BLE GATT connection closed (con %u, reason %u)", p_conn_status->conn_id,
754 p_conn_status->reason);
755
Kevin Schoedel6fd38112021-09-14 13:22:09 -0400756 if (PlatformMgr().PostEvent(&event) != CHIP_NO_ERROR)
757 {
758 return WICED_BT_GATT_INTERNAL_ERROR;
759 }
Praveen Chandran080ae572021-08-12 07:58:27 -0700760
761 // Arrange to re-enable connectable advertising in case it was disabled due to the
762 // maximum connection limit being reached.
763 mFlags.Set(Flags::kFlag_Advertising, false);
764 PlatformMgr().ScheduleWork(DriveBLEState, 0);
765
766 ReleaseConnectionState(p_conn->ConId);
767 }
768 return WICED_BT_GATT_SUCCESS;
769}
770
771/*
772 * Process GATT requests. Callback is received in the BT stack thread context.
773 *
774 */
775wiced_bt_gatt_status_t app_gatts_callback(wiced_bt_gatt_evt_t event, wiced_bt_gatt_event_data_t * p_data)
776{
777 uint16_t conn_id;
778 BLEManagerImpl::CHIPoBLEConState * p_conn;
779
780 /* Check parameter. */
781 if (!p_data)
782 {
783 return WICED_BT_GATT_ILLEGAL_PARAMETER;
784 }
785
786 /* Check if target connection state exists. */
787 switch (event)
788 {
789 case GATT_CONNECTION_STATUS_EVT:
790 conn_id = p_data->connection_status.conn_id;
791 break;
792
793 case GATT_OPERATION_CPLT_EVT:
794 conn_id = p_data->operation_complete.conn_id;
795 break;
796
797 case GATT_DISCOVERY_RESULT_EVT:
798 conn_id = p_data->discovery_result.conn_id;
799 break;
800
801 case GATT_DISCOVERY_CPLT_EVT:
802 conn_id = p_data->discovery_complete.conn_id;
803 break;
804
805 case GATT_ATTRIBUTE_REQUEST_EVT:
806 conn_id = p_data->attribute_request.conn_id;
807 break;
808
809 case GATT_CONGESTION_EVT:
810 conn_id = p_data->congestion.conn_id;
811 break;
812
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700813 case GATT_GET_RESPONSE_BUFFER_EVT:
814 if (heap_allocated == false)
815 {
816 p_heap = wiced_bt_create_heap("default_heap", NULL, BT_STACK_HEAP_SIZE, NULL, WICED_TRUE);
817 heap_allocated = true;
818 }
819 p_data->buffer_request.buffer.p_app_rsp_buffer =
820 BLEManagerImpl::sInstance.gatt_alloc_buffer(p_data->buffer_request.len_requested);
821 p_data->buffer_request.buffer.p_app_ctxt = (wiced_bt_gatt_app_context_t) gatt_free_buffer_cb;
822 return WICED_BT_GATT_SUCCESS;
823 break;
824
825 case GATT_APP_BUFFER_TRANSMITTED_EVT: {
826 pfn_free_buffer_t pfn_free = (pfn_free_buffer_t) p_data->buffer_xmitted.p_app_ctxt;
827 if (pfn_free)
828 {
829 pfn_free(p_data->buffer_xmitted.p_app_data);
830 }
831 }
832 return WICED_BT_GATT_SUCCESS;
833 break;
834
Praveen Chandran080ae572021-08-12 07:58:27 -0700835 default:
836 return WICED_BT_GATT_ILLEGAL_PARAMETER;
837 }
838
839 p_conn = BLEManagerImpl::sInstance.GetConnectionState(conn_id);
840
841 /* Allocate connection state if no exist. */
842 if (!p_conn)
843 {
844 p_conn = BLEManagerImpl::sInstance.AllocConnectionState(conn_id);
845
846 if (!p_conn)
847 {
848 return WICED_BT_GATT_INSUF_RESOURCE;
849 }
850 }
851
852 switch (event)
853 {
854 case GATT_CONNECTION_STATUS_EVT:
855 return BLEManagerImpl::sInstance.HandleGattConnectEvent(&p_data->connection_status, p_conn);
856
857 case GATT_ATTRIBUTE_REQUEST_EVT:
858 return BLEManagerImpl::sInstance.HandleGattServiceRequestEvent(&p_data->attribute_request, p_conn);
859
860 default:
861 break;
862 }
863
864 return WICED_BT_GATT_ILLEGAL_PARAMETER;
865}
866
867void BLEManagerImpl::SetAdvertisingData(void)
868{
869 CHIP_ERROR err;
870 wiced_bt_ble_advert_elem_t adv_elem[4];
871 uint8_t num_elem = 0;
872 uint8_t flag = BTM_BLE_GENERAL_DISCOVERABLE_FLAG | BTM_BLE_BREDR_NOT_SUPPORTED;
873 uint8_t chip_service_uuid[2] = { BIT16_TO_8(__UUID16_CHIPoBLEService) };
874 ChipBLEDeviceIdentificationInfo mDeviceIdInfo;
875 uint16_t deviceDiscriminator = 0;
876 uint8_t localDeviceNameLen;
Praveen Chandran7b28de42022-01-17 12:00:07 -0800877 uint8_t service_data[BLE_SERVICE_DATA_SIZE];
Praveen Chandran080ae572021-08-12 07:58:27 -0700878 uint8_t * p = service_data;
879
Praveen Chandran7b28de42022-01-17 12:00:07 -0800880 static_assert(BLE_SERVICE_DATA_SIZE == sizeof(ChipBLEDeviceIdentificationInfo) + 2, "BLE Service Data Size is incorrect");
881
Praveen Chandran080ae572021-08-12 07:58:27 -0700882 // Initialize the CHIP BLE Device Identification Information block that will be sent as payload
883 // within the BLE service advertisement data.
884 err = ConfigurationMgr().GetBLEDeviceIdentificationInfo(mDeviceIdInfo);
885 SuccessOrExit(err);
886
887 // Get device discriminator
888 deviceDiscriminator = mDeviceIdInfo.GetDeviceDiscriminator();
889
890 // Verify device name was not already set
891 if (!sInstance.mFlags.Has(sInstance.Flags::kFlag_DeviceNameSet))
892 {
893 /* Default device name is CHIP-<DISCRIMINATOR> */
894 memset(sInstance.mDeviceName, 0, kMaxDeviceNameLength);
895 snprintf(sInstance.mDeviceName, kMaxDeviceNameLength, "%s%04u", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX,
896 deviceDiscriminator);
897 localDeviceNameLen = strlen(sInstance.mDeviceName);
898
Evgeny Margolis15ac9172022-09-22 11:41:42 -0700899 Platform::CopyString((char *) app_gap_device_name, sizeof(app_gap_device_name), sInstance.mDeviceName);
Praveen Chandran080ae572021-08-12 07:58:27 -0700900 app_gatt_db_ext_attr_tbl[0].cur_len = app_gatt_db_ext_attr_tbl[0].max_len < strlen(sInstance.mDeviceName)
901 ? app_gatt_db_ext_attr_tbl[0].max_len
902 : strlen(sInstance.mDeviceName);
903
904 ChipLogProgress(DeviceLayer, "SetAdvertisingData: device name set: %s", sInstance.mDeviceName);
905 }
906 else
907 {
908 localDeviceNameLen = strlen(sInstance.mDeviceName);
909 }
910
Martin Turond24eff12021-12-17 06:21:23 -0800911 /* First element is the advertisement flags */
Praveen Chandran080ae572021-08-12 07:58:27 -0700912 adv_elem[num_elem].advert_type = BTM_BLE_ADVERT_TYPE_FLAG;
913 adv_elem[num_elem].len = sizeof(uint8_t);
914 adv_elem[num_elem].p_data = &flag;
915 num_elem++;
916
917 /* Second element is the service data for CHIP service */
918 adv_elem[num_elem].advert_type = BTM_BLE_ADVERT_TYPE_SERVICE_DATA;
919 adv_elem[num_elem].len = sizeof(service_data);
920 adv_elem[num_elem].p_data = service_data;
921 num_elem++;
922 UINT8_TO_STREAM(p, chip_service_uuid[0]);
923 UINT8_TO_STREAM(p, chip_service_uuid[1]);
924 UINT8_TO_STREAM(p, 0); // CHIP BLE Opcode == 0x00 (Uncommissioned)
925 UINT16_TO_STREAM(p, deviceDiscriminator);
926 UINT8_TO_STREAM(p, mDeviceIdInfo.DeviceVendorId[0]);
927 UINT8_TO_STREAM(p, mDeviceIdInfo.DeviceVendorId[1]);
928 UINT8_TO_STREAM(p, mDeviceIdInfo.DeviceProductId[0]);
929 UINT8_TO_STREAM(p, mDeviceIdInfo.DeviceProductId[1]);
Praveen Chandran7b28de42022-01-17 12:00:07 -0800930 UINT8_TO_STREAM(p, 0); // Additional Data Flag
Praveen Chandran080ae572021-08-12 07:58:27 -0700931
932 adv_elem[num_elem].advert_type = BTM_BLE_ADVERT_TYPE_NAME_COMPLETE;
933 adv_elem[num_elem].len = localDeviceNameLen;
934 adv_elem[num_elem].p_data = (uint8_t *) sInstance.mDeviceName;
935 num_elem++;
936
937 wiced_bt_ble_set_raw_advertisement_data(num_elem, adv_elem);
938
939 /* Configure Scan Response data */
940 num_elem = 0;
941 adv_elem[num_elem].advert_type = BTM_BLE_ADVERT_TYPE_NAME_COMPLETE;
942 adv_elem[num_elem].len = localDeviceNameLen;
943 adv_elem[num_elem].p_data = (uint8_t *) sInstance.mDeviceName;
944 num_elem++;
945
946 wiced_bt_ble_set_raw_scan_response_data(num_elem, adv_elem);
947
948exit:
949 ChipLogProgress(DeviceLayer, "BLEManagerImpl::SetAdvertisingData err:%s", ErrorStr(err));
950}
951
952BLEManagerImpl::CHIPoBLEConState * BLEManagerImpl::AllocConnectionState(uint16_t conId)
953{
954 for (uint16_t i = 0; i < kMaxConnections; i++)
955 {
956 if (mCons[i].connected == false)
957 {
958 mCons[i].ConId = conId;
Praveen Chandrana15e2b02022-07-18 16:54:00 -0700959 mCons[i].Mtu = wiced_bt_cfg_settings.p_ble_cfg->ble_max_rx_pdu_size;
Praveen Chandran080ae572021-08-12 07:58:27 -0700960 mCons[i].connected = false;
961
962 mNumCons++;
963
964 return &mCons[i];
965 }
966 }
967 ChipLogError(DeviceLayer, "Failed to allocate CHIPoBLEConState");
968 return NULL;
969}
970
971BLEManagerImpl::CHIPoBLEConState * BLEManagerImpl::GetConnectionState(uint16_t conId)
972{
973 for (uint16_t i = 0; i < kMaxConnections; i++)
974 {
975 if (mCons[i].ConId == conId)
976 {
977 return &mCons[i];
978 }
979 }
980 ChipLogError(DeviceLayer, "Failed to find CHIPoBLEConState");
981 return NULL;
982}
983
984bool BLEManagerImpl::ReleaseConnectionState(uint16_t conId)
985{
986 for (uint16_t i = 0; i < kMaxConnections; i++)
987 {
988 if (mCons[i].ConId == conId)
989 {
990 memset(&mCons[i], 0, sizeof(CHIPoBLEConState));
991 mNumCons--;
992 return true;
993 }
994 }
995 ChipLogError(DeviceLayer, "Failed to delete CHIPoBLEConState");
996 return false;
997}
998
999void BLEManagerImpl::DriveBLEState(intptr_t arg)
1000{
1001 sInstance.DriveBLEState();
1002}
1003
1004} // namespace Internal
1005} // namespace DeviceLayer
1006} // namespace chip
1007
1008#endif