Skip to content

Commit 68f0437

Browse files
[Silabs] Fixing the BLE Advertisement for the NCP and 917SoC devices (#36189) (#36219)
* ble fixes for the pointer * removing the global structure * addressing the review comments * removing the static keyword * modifying the assignment * restyling the PR * addressing review comments * restyling the PR * removing the TODOs * addressing the review comment * restyling the PR
1 parent d3e4148 commit 68f0437

File tree

4 files changed

+132
-153
lines changed

4 files changed

+132
-153
lines changed

src/platform/silabs/BLEManagerImpl.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
5858
#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
5959
// Used for posting the event in the BLE queue
6060
void BlePostEvent(SilabsBleWrapper::BleEvent_t * event);
61-
void HandleConnectEvent(SilabsBleWrapper::sl_wfx_msg_t * evt);
62-
void HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t * evt);
63-
void HandleWriteEvent(SilabsBleWrapper::sl_wfx_msg_t * evt);
64-
void UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt);
61+
void HandleConnectEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt);
62+
void HandleConnectionCloseEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt);
63+
void HandleWriteEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt);
64+
void UpdateMtu(const SilabsBleWrapper::sl_wfx_msg_t & evt);
6565
void HandleTxConfirmationEvent(BLE_CONNECTION_OBJECT conId);
66-
void HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt);
66+
void HandleTXCharCCCDWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt);
6767
void HandleSoftTimerEvent(void);
6868
int32_t SendBLEAdvertisementCommand(void);
6969
#else
@@ -194,7 +194,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
194194
#endif
195195

196196
#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
197-
void HandleRXCharWrite(SilabsBleWrapper::sl_wfx_msg_t * evt);
197+
void HandleRXCharWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt);
198198
#else
199199
void HandleRXCharWrite(volatile sl_bt_msg_t * evt);
200200
#endif

src/platform/silabs/rs911x/BLEManagerImpl.cpp

+107-49
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,7 @@ extern "C" {
5757
#define BLE_TIMEOUT_MS 400
5858
#define BLE_SEND_INDICATION_TIMER_PERIOD_MS (5000)
5959

60-
// Used to send the Indication Confirmation
61-
uint8_t dev_address[RSI_DEV_ADDR_LEN];
62-
uint16_t ble_measurement_hndl;
63-
6460
osSemaphoreId_t sl_rs_ble_init_sem;
65-
osTimerId_t sbleAdvTimeoutTimer;
66-
67-
static osThreadId_t sBleThread;
68-
constexpr uint32_t kBleTaskSize = 2048;
69-
static uint8_t bleStack[kBleTaskSize];
70-
static osThread_t sBleTaskControlBlock;
71-
constexpr osThreadAttr_t kBleTaskAttr = { .name = "rsi_ble",
72-
.attr_bits = osThreadDetached,
73-
.cb_mem = &sBleTaskControlBlock,
74-
.cb_size = osThreadCbSize,
75-
.stack_mem = bleStack,
76-
.stack_size = kBleTaskSize,
77-
.priority = osPriorityHigh };
7861

7962
using namespace ::chip;
8063
using namespace ::chip::Ble;
@@ -111,6 +94,84 @@ const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00
11194
0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 };
11295
const uint8_t ShortUUID_CHIPoBLEService[] = { 0xF6, 0xFF };
11396

97+
// Used to send the Indication Confirmation
98+
uint8_t dev_address[RSI_DEV_ADDR_LEN];
99+
uint16_t rsi_ble_measurement_hndl;
100+
uint16_t rsi_ble_gatt_server_client_config_hndl;
101+
102+
osTimerId_t sbleAdvTimeoutTimer;
103+
104+
osThreadId_t sBleThread;
105+
constexpr uint32_t kBleTaskSize = 2560;
106+
uint8_t bleStack[kBleTaskSize];
107+
osThread_t sBleTaskControlBlock;
108+
constexpr osThreadAttr_t kBleTaskAttr = { .name = "rsi_ble",
109+
.attr_bits = osThreadDetached,
110+
.cb_mem = &sBleTaskControlBlock,
111+
.cb_size = osThreadCbSize,
112+
.stack_mem = bleStack,
113+
.stack_size = kBleTaskSize,
114+
.priority = osPriorityHigh };
115+
116+
void rsi_ble_add_matter_service(void)
117+
{
118+
constexpr uuid_t custom_service = { .size = RSI_BLE_MATTER_CUSTOM_SERVICE_SIZE,
119+
.val = { .val16 = RSI_BLE_MATTER_CUSTOM_SERVICE_VALUE_16 } };
120+
uint8_t data[RSI_BLE_MATTER_CUSTOM_SERVICE_DATA_LENGTH] = { RSI_BLE_MATTER_CUSTOM_SERVICE_DATA };
121+
122+
constexpr uuid_t custom_characteristic_RX = { .size = RSI_BLE_CUSTOM_CHARACTERISTIC_RX_SIZE,
123+
.reserved = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_RESERVED },
124+
.val = { .val128 = {
125+
.data1 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_1 },
126+
.data2 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_2 },
127+
.data3 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_3 },
128+
.data4 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_4 } } } };
129+
130+
rsi_ble_resp_add_serv_t new_serv_resp = { 0 };
131+
rsi_ble_add_service(custom_service, &new_serv_resp);
132+
133+
// Adding custom characteristic declaration to the custom service
134+
SilabsBleWrapper::rsi_ble_add_char_serv_att(
135+
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_ATTRIBUTE_HANDLE_LOCATION,
136+
RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ, // Set read, write, write without response
137+
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_VALUE_HANDLE_LOCATION, custom_characteristic_RX);
138+
139+
// Adding characteristic value attribute to the service
140+
SilabsBleWrapper::rsi_ble_add_char_val_att(
141+
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_VALUE_HANDLE_LOCATION,
142+
custom_characteristic_RX,
143+
RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ, // Set read, write, write without response
144+
data, sizeof(data), ATT_REC_IN_HOST);
145+
146+
constexpr uuid_t custom_characteristic_TX = { .size = RSI_BLE_CUSTOM_CHARACTERISTIC_TX_SIZE,
147+
.reserved = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_RESERVED },
148+
.val = { .val128 = {
149+
.data1 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_1 },
150+
.data2 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_2 },
151+
.data3 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_3 },
152+
.data4 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_4 } } } };
153+
154+
// Adding custom characteristic declaration to the custom service
155+
SilabsBleWrapper::rsi_ble_add_char_serv_att(
156+
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_ATTRIBUTE_HANDLE_LOCATION,
157+
RSI_BLE_ATT_PROPERTY_WRITE_NO_RESPONSE | RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ |
158+
RSI_BLE_ATT_PROPERTY_NOTIFY | RSI_BLE_ATT_PROPERTY_INDICATE, // Set read, write, write without response
159+
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_MEASUREMENT_HANDLE_LOCATION, custom_characteristic_TX);
160+
161+
// Adding characteristic value attribute to the service
162+
rsi_ble_measurement_hndl = new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_MEASUREMENT_HANDLE_LOCATION;
163+
164+
// Adding characteristic value attribute to the service
165+
rsi_ble_gatt_server_client_config_hndl =
166+
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_GATT_SERVER_CLIENT_HANDLE_LOCATION;
167+
168+
SilabsBleWrapper::rsi_ble_add_char_val_att(new_serv_resp.serv_handler, rsi_ble_measurement_hndl, custom_characteristic_TX,
169+
RSI_BLE_ATT_PROPERTY_WRITE_NO_RESPONSE | RSI_BLE_ATT_PROPERTY_WRITE |
170+
RSI_BLE_ATT_PROPERTY_READ | RSI_BLE_ATT_PROPERTY_NOTIFY |
171+
RSI_BLE_ATT_PROPERTY_INDICATE, // Set read, write, write without response
172+
data, sizeof(data), ATT_REC_MAINTAIN_IN_HOST);
173+
}
174+
114175
} // namespace
115176

116177
BLEManagerImpl BLEManagerImpl::sInstance;
@@ -120,15 +181,14 @@ void BLEManagerImpl::ProcessEvent(SilabsBleWrapper::BleEvent_t inEvent)
120181
switch (inEvent.eventType)
121182
{
122183
case SilabsBleWrapper::BleEventType::RSI_BLE_CONN_EVENT: {
123-
BLEMgrImpl().HandleConnectEvent((inEvent.eventData));
184+
BLEMgrImpl().HandleConnectEvent(inEvent.eventData);
124185
// Requests the connection parameters change with the remote device
125-
rsi_ble_conn_params_update(inEvent.eventData->resp_enh_conn.dev_addr, BLE_MIN_CONNECTION_INTERVAL_MS,
186+
rsi_ble_conn_params_update(inEvent.eventData.resp_enh_conn.dev_addr, BLE_MIN_CONNECTION_INTERVAL_MS,
126187
BLE_MAX_CONNECTION_INTERVAL_MS, BLE_SLAVE_LATENCY_MS, BLE_TIMEOUT_MS);
127-
rsi_ble_set_data_len(inEvent.eventData->resp_enh_conn.dev_addr, RSI_BLE_TX_OCTETS, RSI_BLE_TX_TIME);
188+
rsi_ble_set_data_len(inEvent.eventData.resp_enh_conn.dev_addr, RSI_BLE_TX_OCTETS, RSI_BLE_TX_TIME);
128189

129190
// Used to send the Indication confirmation
130-
memcpy(dev_address, inEvent.eventData->resp_enh_conn.dev_addr, RSI_DEV_ADDR_LEN);
131-
ble_measurement_hndl = inEvent.eventData->rsi_ble_measurement_hndl;
191+
memcpy(dev_address, inEvent.eventData.resp_enh_conn.dev_addr, RSI_DEV_ADDR_LEN);
132192
}
133193
break;
134194
case SilabsBleWrapper::BleEventType::RSI_BLE_DISCONN_EVENT: {
@@ -143,7 +203,7 @@ void BLEManagerImpl::ProcessEvent(SilabsBleWrapper::BleEvent_t inEvent)
143203
break;
144204
case SilabsBleWrapper::BleEventType::RSI_BLE_EVENT_GATT_RD: {
145205
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
146-
if (inEvent.eventData->rsi_ble_read_req->type == 0)
206+
if (inEvent.eventData.rsi_ble_read_req->type == 0)
147207
{
148208
BLEMgrImpl().HandleC3ReadRequest(inEvent.eventData);
149209
}
@@ -220,10 +280,10 @@ void BLEManagerImpl::sl_ble_init()
220280
SilabsBleWrapper::rsi_ble_on_event_indication_confirmation, NULL);
221281

222282
// Exchange of GATT info with BLE stack
223-
SilabsBleWrapper::rsi_ble_add_matter_service();
283+
rsi_ble_add_matter_service();
224284
rsi_ble_set_random_address_with_value(randomAddrBLE);
225285

226-
sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(WfxEvent_t), NULL);
286+
sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(SilabsBleWrapper::BleEvent_t), NULL);
227287
VerifyOrDie(sInstance.sBleEventQueue != nullptr);
228288

229289
chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
@@ -440,7 +500,7 @@ CHIP_ERROR BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const Chi
440500
PacketBufferHandle data)
441501
{
442502
int32_t status = 0;
443-
status = rsi_ble_indicate_value(dev_address, ble_measurement_hndl, data->DataLength(), data->Start());
503+
status = rsi_ble_indicate_value(dev_address, rsi_ble_measurement_hndl, data->DataLength(), data->Start());
444504
if (status != RSI_SUCCESS)
445505
{
446506
ChipLogProgress(DeviceLayer, "indication failed with error code %lx ", status);
@@ -734,9 +794,9 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
734794
return err;
735795
}
736796

737-
void BLEManagerImpl::UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt)
797+
void BLEManagerImpl::UpdateMtu(const SilabsBleWrapper::sl_wfx_msg_t & evt)
738798
{
739-
CHIPoBLEConState * bleConnState = GetConnectionState(evt->connectionHandle);
799+
CHIPoBLEConState * bleConnState = GetConnectionState(evt.connectionHandle);
740800
if (bleConnState != NULL)
741801
{
742802
// bleConnState->MTU is a 10-bit field inside a uint16_t. We're
@@ -748,10 +808,10 @@ void BLEManagerImpl::UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt)
748808
// TODO: https://github.com/project-chip/connectedhomeip/issues/2569
749809
// tracks making this safe with a check or explaining why no check
750810
// is needed.
751-
ChipLogProgress(DeviceLayer, "DriveBLEState UpdateMtu %d", evt->rsi_ble_mtu.mtu_size);
811+
ChipLogProgress(DeviceLayer, "DriveBLEState UpdateMtu %d", evt.rsi_ble_mtu.mtu_size);
752812
#pragma GCC diagnostic push
753813
#pragma GCC diagnostic ignored "-Wconversion"
754-
bleConnState->mtu = evt->rsi_ble_mtu.mtu_size;
814+
bleConnState->mtu = evt.rsi_ble_mtu.mtu_size;
755815
#pragma GCC diagnostic pop
756816
;
757817
}
@@ -763,14 +823,13 @@ void BLEManagerImpl::HandleBootEvent(void)
763823
PlatformMgr().ScheduleWork(DriveBLEState, 0);
764824
}
765825

766-
void BLEManagerImpl::HandleConnectEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
826+
void BLEManagerImpl::HandleConnectEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
767827
{
768-
AddConnection(evt->connectionHandle, evt->bondingHandle);
828+
AddConnection(evt.connectionHandle, evt.bondingHandle);
769829
PlatformMgr().ScheduleWork(DriveBLEState, 0);
770830
}
771831

772-
// TODO:: Implementation need to be done.
773-
void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
832+
void BLEManagerImpl::HandleConnectionCloseEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
774833
{
775834
uint8_t connHandle = 1;
776835

@@ -780,7 +839,7 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
780839
event.Type = DeviceEventType::kCHIPoBLEConnectionError;
781840
event.CHIPoBLEConnectionError.ConId = connHandle;
782841

783-
switch (evt->reason)
842+
switch (evt.reason)
784843
{
785844

786845
case RSI_BT_CTRL_REMOTE_USER_TERMINATED:
@@ -792,7 +851,7 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
792851
event.CHIPoBLEConnectionError.Reason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT;
793852
}
794853

795-
ChipLogProgress(DeviceLayer, "BLE GATT connection closed (con %u, reason %x)", connHandle, evt->reason);
854+
ChipLogProgress(DeviceLayer, "BLE GATT connection closed (con %u, reason %x)", connHandle, evt.reason);
796855

797856
PlatformMgr().PostEventOrDie(&event);
798857

@@ -804,11 +863,11 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
804863
}
805864
}
806865

807-
void BLEManagerImpl::HandleWriteEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
866+
void BLEManagerImpl::HandleWriteEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
808867
{
809-
ChipLogProgress(DeviceLayer, "Char Write Req, packet type %d", evt->rsi_ble_write.pkt_type);
868+
ChipLogProgress(DeviceLayer, "Char Write Req, packet type %d", evt.rsi_ble_write.pkt_type);
810869

811-
if (evt->rsi_ble_write.handle[0] == (uint8_t) evt->rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
870+
if (evt.rsi_ble_write.handle[0] == (uint8_t) rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
812871
{
813872
HandleTXCharCCCDWrite(evt);
814873
}
@@ -818,23 +877,22 @@ void BLEManagerImpl::HandleWriteEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
818877
}
819878
}
820879

821-
// TODO:: Need to implement this
822-
void BLEManagerImpl::HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
880+
void BLEManagerImpl::HandleTXCharCCCDWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt)
823881
{
824882
CHIP_ERROR err = CHIP_NO_ERROR;
825883
bool isIndicationEnabled = false;
826884
ChipDeviceEvent event;
827885
CHIPoBLEConState * bleConnState;
828886

829-
bleConnState = GetConnectionState(evt->connectionHandle);
887+
bleConnState = GetConnectionState(evt.connectionHandle);
830888
VerifyOrExit(bleConnState != NULL, err = CHIP_ERROR_NO_MEMORY);
831889

832890
// Determine if the client is enabling or disabling notification/indication.
833-
if (evt->rsi_ble_write.att_value[0] != 0)
891+
if (evt.rsi_ble_write.att_value[0] != 0)
834892
{
835893
isIndicationEnabled = true;
836894
}
837-
ChipLogProgress(DeviceLayer, "HandleTXcharCCCDWrite - Config Flags value : %d", evt->rsi_ble_write.att_value[0]);
895+
ChipLogProgress(DeviceLayer, "HandleTXcharCCCDWrite - Config Flags value : %d", evt.rsi_ble_write.att_value[0]);
838896
ChipLogProgress(DeviceLayer, "CHIPoBLE %s received", isIndicationEnabled ? "subscribe" : "unsubscribe");
839897

840898
if (isIndicationEnabled)
@@ -864,13 +922,13 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
864922
}
865923
}
866924

867-
void BLEManagerImpl::HandleRXCharWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
925+
void BLEManagerImpl::HandleRXCharWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt)
868926
{
869927
uint8_t conId = 1;
870928
CHIP_ERROR err = CHIP_NO_ERROR;
871929
System::PacketBufferHandle buf;
872-
uint16_t writeLen = evt->rsi_ble_write.length;
873-
uint8_t * data = (uint8_t *) evt->rsi_ble_write.att_value;
930+
uint16_t writeLen = evt.rsi_ble_write.length;
931+
uint8_t * data = (uint8_t *) evt.rsi_ble_write.att_value;
874932

875933
// Copy the data to a packet buffer.
876934
buf = System::PacketBufferHandle::NewWithData(data, writeLen, 0, 0);
@@ -997,9 +1055,9 @@ CHIP_ERROR BLEManagerImpl::EncodeAdditionalDataTlv()
9971055
return err;
9981056
}
9991057

1000-
void BLEManagerImpl::HandleC3ReadRequest(SilabsBleWrapper::sl_wfx_msg_t * evt)
1058+
void BLEManagerImpl::HandleC3ReadRequest(const SilabsBleWrapper::sl_wfx_msg_t & evt)
10011059
{
1002-
sl_status_t ret = rsi_ble_gatt_read_response(evt->rsi_ble_read_req->dev_addr, GATT_READ_RESP, evt->rsi_ble_read_req->handle,
1060+
sl_status_t ret = rsi_ble_gatt_read_response(evt.rsi_ble_read_req->dev_addr, GATT_READ_RESP, evt.rsi_ble_read_req->handle,
10031061
GATT_READ_ZERO_OFFSET, sInstance.c3AdditionalDataBufferHandle->DataLength(),
10041062
sInstance.c3AdditionalDataBufferHandle->Start());
10051063
if (ret != SL_STATUS_OK)

0 commit comments

Comments
 (0)