Skip to content

Commit 6fdb641

Browse files
committed
cherry-pick of the BLE Fix PR 36189
1 parent 9101fb4 commit 6fdb641

File tree

4 files changed

+134
-151
lines changed

4 files changed

+134
-151
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

+108-47
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,85 @@ 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+
uuid_t custom_service = { RSI_BLE_MATTER_CUSTOM_SERVICE_UUID };
119+
custom_service.size = RSI_BLE_MATTER_CUSTOM_SERVICE_SIZE;
120+
custom_service.val.val16 = RSI_BLE_MATTER_CUSTOM_SERVICE_VALUE_16;
121+
uint8_t data[RSI_BLE_MATTER_CUSTOM_SERVICE_DATA_LENGTH] = { RSI_BLE_MATTER_CUSTOM_SERVICE_DATA };
122+
123+
const uuid_t custom_characteristic_RX = { .size = RSI_BLE_CUSTOM_CHARACTERISTIC_RX_SIZE,
124+
.reserved = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_RESERVED },
125+
.val = {
126+
.val128 = { .data1 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_1 },
127+
.data2 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_2 },
128+
.data3 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_3 },
129+
.data4 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_4 } } } };
130+
131+
rsi_ble_resp_add_serv_t new_serv_resp = { 0 };
132+
rsi_ble_add_service(custom_service, &new_serv_resp);
133+
134+
// Adding custom characteristic declaration to the custom service
135+
SilabsBleWrapper::rsi_ble_add_char_serv_att(
136+
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_ATTRIBUTE_HANDLE_LOCATION,
137+
RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ, // Set read, write, write without response
138+
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_VALUE_HANDLE_LOCATION, custom_characteristic_RX);
139+
140+
// Adding characteristic value attribute to the service
141+
SilabsBleWrapper::rsi_ble_add_char_val_att(
142+
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_VALUE_HANDLE_LOCATION,
143+
custom_characteristic_RX,
144+
RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ, // Set read, write, write without response
145+
data, sizeof(data), ATT_REC_IN_HOST);
146+
147+
const uuid_t custom_characteristic_TX = { .size = RSI_BLE_CUSTOM_CHARACTERISTIC_TX_SIZE,
148+
.reserved = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_RESERVED },
149+
.val = {
150+
.val128 = { .data1 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_1 },
151+
.data2 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_2 },
152+
.data3 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_3 },
153+
.data4 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_4 } } } };
154+
155+
// Adding custom characteristic declaration to the custom service
156+
SilabsBleWrapper::rsi_ble_add_char_serv_att(
157+
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_ATTRIBUTE_HANDLE_LOCATION,
158+
RSI_BLE_ATT_PROPERTY_WRITE_NO_RESPONSE | RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ |
159+
RSI_BLE_ATT_PROPERTY_NOTIFY | RSI_BLE_ATT_PROPERTY_INDICATE, // Set read, write, write without response
160+
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_MEASUREMENT_HANDLE_LOCATION, custom_characteristic_TX);
161+
162+
// Adding characteristic value attribute to the service
163+
rsi_ble_measurement_hndl = new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_MEASUREMENT_HANDLE_LOCATION;
164+
165+
// Adding characteristic value attribute to the service
166+
rsi_ble_gatt_server_client_config_hndl =
167+
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_GATT_SERVER_CLIENT_HANDLE_LOCATION;
168+
169+
SilabsBleWrapper::rsi_ble_add_char_val_att(new_serv_resp.serv_handler, rsi_ble_measurement_hndl, custom_characteristic_TX,
170+
RSI_BLE_ATT_PROPERTY_WRITE_NO_RESPONSE | RSI_BLE_ATT_PROPERTY_WRITE |
171+
RSI_BLE_ATT_PROPERTY_READ | RSI_BLE_ATT_PROPERTY_NOTIFY |
172+
RSI_BLE_ATT_PROPERTY_INDICATE, // Set read, write, write without response
173+
data, sizeof(data), ATT_REC_MAINTAIN_IN_HOST);
174+
}
175+
114176
} // namespace
115177

116178
BLEManagerImpl BLEManagerImpl::sInstance;
@@ -120,15 +182,14 @@ void BLEManagerImpl::ProcessEvent(SilabsBleWrapper::BleEvent_t inEvent)
120182
switch (inEvent.eventType)
121183
{
122184
case SilabsBleWrapper::BleEventType::RSI_BLE_CONN_EVENT: {
123-
BLEMgrImpl().HandleConnectEvent((inEvent.eventData));
185+
BLEMgrImpl().HandleConnectEvent(inEvent.eventData);
124186
// 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,
187+
rsi_ble_conn_params_update(inEvent.eventData.resp_enh_conn.dev_addr, BLE_MIN_CONNECTION_INTERVAL_MS,
126188
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);
189+
rsi_ble_set_data_len(inEvent.eventData.resp_enh_conn.dev_addr, RSI_BLE_TX_OCTETS, RSI_BLE_TX_TIME);
128190

129191
// 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;
192+
memcpy(dev_address, inEvent.eventData.resp_enh_conn.dev_addr, RSI_DEV_ADDR_LEN);
132193
}
133194
break;
134195
case SilabsBleWrapper::BleEventType::RSI_BLE_DISCONN_EVENT: {
@@ -143,7 +204,7 @@ void BLEManagerImpl::ProcessEvent(SilabsBleWrapper::BleEvent_t inEvent)
143204
break;
144205
case SilabsBleWrapper::BleEventType::RSI_BLE_EVENT_GATT_RD: {
145206
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
146-
if (inEvent.eventData->rsi_ble_read_req->type == 0)
207+
if (inEvent.eventData.rsi_ble_read_req->type == 0)
147208
{
148209
BLEMgrImpl().HandleC3ReadRequest(inEvent.eventData);
149210
}
@@ -220,10 +281,10 @@ void BLEManagerImpl::sl_ble_init()
220281
SilabsBleWrapper::rsi_ble_on_event_indication_confirmation, NULL);
221282

222283
// Exchange of GATT info with BLE stack
223-
SilabsBleWrapper::rsi_ble_add_matter_service();
284+
rsi_ble_add_matter_service();
224285
rsi_ble_set_random_address_with_value(randomAddrBLE);
225286

226-
sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(WfxEvent_t), NULL);
287+
sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(SilabsBleWrapper::BleEvent_t), NULL);
227288
VerifyOrDie(sInstance.sBleEventQueue != nullptr);
228289

229290
chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
@@ -440,7 +501,7 @@ CHIP_ERROR BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const Chi
440501
PacketBufferHandle data)
441502
{
442503
int32_t status = 0;
443-
status = rsi_ble_indicate_value(dev_address, ble_measurement_hndl, data->DataLength(), data->Start());
504+
status = rsi_ble_indicate_value(dev_address, rsi_ble_measurement_hndl, data->DataLength(), data->Start());
444505
if (status != RSI_SUCCESS)
445506
{
446507
ChipLogProgress(DeviceLayer, "indication failed with error code %lx ", status);
@@ -734,9 +795,9 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
734795
return err;
735796
}
736797

737-
void BLEManagerImpl::UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt)
798+
void BLEManagerImpl::UpdateMtu(const SilabsBleWrapper::sl_wfx_msg_t & evt)
738799
{
739-
CHIPoBLEConState * bleConnState = GetConnectionState(evt->connectionHandle);
800+
CHIPoBLEConState * bleConnState = GetConnectionState(evt.connectionHandle);
740801
if (bleConnState != NULL)
741802
{
742803
// bleConnState->MTU is a 10-bit field inside a uint16_t. We're
@@ -748,10 +809,10 @@ void BLEManagerImpl::UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt)
748809
// TODO: https://github.com/project-chip/connectedhomeip/issues/2569
749810
// tracks making this safe with a check or explaining why no check
750811
// is needed.
751-
ChipLogProgress(DeviceLayer, "DriveBLEState UpdateMtu %d", evt->rsi_ble_mtu.mtu_size);
812+
ChipLogProgress(DeviceLayer, "DriveBLEState UpdateMtu %d", evt.rsi_ble_mtu.mtu_size);
752813
#pragma GCC diagnostic push
753814
#pragma GCC diagnostic ignored "-Wconversion"
754-
bleConnState->mtu = evt->rsi_ble_mtu.mtu_size;
815+
bleConnState->mtu = evt.rsi_ble_mtu.mtu_size;
755816
#pragma GCC diagnostic pop
756817
;
757818
}
@@ -763,14 +824,14 @@ void BLEManagerImpl::HandleBootEvent(void)
763824
PlatformMgr().ScheduleWork(DriveBLEState, 0);
764825
}
765826

766-
void BLEManagerImpl::HandleConnectEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
827+
void BLEManagerImpl::HandleConnectEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
767828
{
768-
AddConnection(evt->connectionHandle, evt->bondingHandle);
829+
AddConnection(evt.connectionHandle, evt.bondingHandle);
769830
PlatformMgr().ScheduleWork(DriveBLEState, 0);
770831
}
771832

772833
// TODO:: Implementation need to be done.
773-
void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
834+
void BLEManagerImpl::HandleConnectionCloseEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
774835
{
775836
uint8_t connHandle = 1;
776837

@@ -780,7 +841,7 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
780841
event.Type = DeviceEventType::kCHIPoBLEConnectionError;
781842
event.CHIPoBLEConnectionError.ConId = connHandle;
782843

783-
switch (evt->reason)
844+
switch (evt.reason)
784845
{
785846

786847
case RSI_BT_CTRL_REMOTE_USER_TERMINATED:
@@ -792,7 +853,7 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
792853
event.CHIPoBLEConnectionError.Reason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT;
793854
}
794855

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

797858
PlatformMgr().PostEventOrDie(&event);
798859

@@ -804,11 +865,11 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
804865
}
805866
}
806867

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

811-
if (evt->rsi_ble_write.handle[0] == (uint8_t) evt->rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
872+
if (evt.rsi_ble_write.handle[0] == (uint8_t) rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
812873
{
813874
HandleTXCharCCCDWrite(evt);
814875
}
@@ -819,22 +880,22 @@ void BLEManagerImpl::HandleWriteEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
819880
}
820881

821882
// TODO:: Need to implement this
822-
void BLEManagerImpl::HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
883+
void BLEManagerImpl::HandleTXCharCCCDWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt)
823884
{
824885
CHIP_ERROR err = CHIP_NO_ERROR;
825886
bool isIndicationEnabled = false;
826887
ChipDeviceEvent event;
827888
CHIPoBLEConState * bleConnState;
828889

829-
bleConnState = GetConnectionState(evt->connectionHandle);
890+
bleConnState = GetConnectionState(evt.connectionHandle);
830891
VerifyOrExit(bleConnState != NULL, err = CHIP_ERROR_NO_MEMORY);
831892

832893
// Determine if the client is enabling or disabling notification/indication.
833-
if (evt->rsi_ble_write.att_value[0] != 0)
894+
if (evt.rsi_ble_write.att_value[0] != 0)
834895
{
835896
isIndicationEnabled = true;
836897
}
837-
ChipLogProgress(DeviceLayer, "HandleTXcharCCCDWrite - Config Flags value : %d", evt->rsi_ble_write.att_value[0]);
898+
ChipLogProgress(DeviceLayer, "HandleTXcharCCCDWrite - Config Flags value : %d", evt.rsi_ble_write.att_value[0]);
838899
ChipLogProgress(DeviceLayer, "CHIPoBLE %s received", isIndicationEnabled ? "subscribe" : "unsubscribe");
839900

840901
if (isIndicationEnabled)
@@ -864,13 +925,13 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
864925
}
865926
}
866927

867-
void BLEManagerImpl::HandleRXCharWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
928+
void BLEManagerImpl::HandleRXCharWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt)
868929
{
869930
uint8_t conId = 1;
870931
CHIP_ERROR err = CHIP_NO_ERROR;
871932
System::PacketBufferHandle buf;
872-
uint16_t writeLen = evt->rsi_ble_write.length;
873-
uint8_t * data = (uint8_t *) evt->rsi_ble_write.att_value;
933+
uint16_t writeLen = evt.rsi_ble_write.length;
934+
uint8_t * data = (uint8_t *) evt.rsi_ble_write.att_value;
874935

875936
// Copy the data to a packet buffer.
876937
buf = System::PacketBufferHandle::NewWithData(data, writeLen, 0, 0);
@@ -997,9 +1058,9 @@ CHIP_ERROR BLEManagerImpl::EncodeAdditionalDataTlv()
9971058
return err;
9981059
}
9991060

1000-
void BLEManagerImpl::HandleC3ReadRequest(SilabsBleWrapper::sl_wfx_msg_t * evt)
1061+
void BLEManagerImpl::HandleC3ReadRequest(const SilabsBleWrapper::sl_wfx_msg_t & evt)
10011062
{
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,
1063+
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,
10031064
GATT_READ_ZERO_OFFSET, sInstance.c3AdditionalDataBufferHandle->DataLength(),
10041065
sInstance.c3AdditionalDataBufferHandle->Start());
10051066
if (ret != SL_STATUS_OK)

0 commit comments

Comments
 (0)