Skip to content

Commit

Permalink
[Silabs] Fixing the BLE Advertisement for the NCP and 917SoC devices (#…
Browse files Browse the repository at this point in the history
…36189)

* 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
  • Loading branch information
chirag-silabs authored Oct 23, 2024
1 parent b99a978 commit 7026061
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 153 deletions.
12 changes: 6 additions & 6 deletions src/platform/silabs/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
// Used for posting the event in the BLE queue
void BlePostEvent(SilabsBleWrapper::BleEvent_t * event);
void HandleConnectEvent(SilabsBleWrapper::sl_wfx_msg_t * evt);
void HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t * evt);
void HandleWriteEvent(SilabsBleWrapper::sl_wfx_msg_t * evt);
void UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt);
void HandleConnectEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt);
void HandleConnectionCloseEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt);
void HandleWriteEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt);
void UpdateMtu(const SilabsBleWrapper::sl_wfx_msg_t & evt);
void HandleTxConfirmationEvent(BLE_CONNECTION_OBJECT conId);
void HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt);
void HandleTXCharCCCDWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt);
void HandleSoftTimerEvent(void);
int32_t SendBLEAdvertisementCommand(void);
#else
Expand Down Expand Up @@ -194,7 +194,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla
#endif

#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
void HandleRXCharWrite(SilabsBleWrapper::sl_wfx_msg_t * evt);
void HandleRXCharWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt);
#else
void HandleRXCharWrite(volatile sl_bt_msg_t * evt);
#endif
Expand Down
156 changes: 107 additions & 49 deletions src/platform/silabs/rs911x/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,7 @@ extern "C" {
#define BLE_TIMEOUT_MS 400
#define BLE_SEND_INDICATION_TIMER_PERIOD_MS (5000)

// Used to send the Indication Confirmation
uint8_t dev_address[RSI_DEV_ADDR_LEN];
uint16_t ble_measurement_hndl;

osSemaphoreId_t sl_rs_ble_init_sem;
osTimerId_t sbleAdvTimeoutTimer;

static osThreadId_t sBleThread;
constexpr uint32_t kBleTaskSize = 2048;
static uint8_t bleStack[kBleTaskSize];
static osThread_t sBleTaskControlBlock;
constexpr osThreadAttr_t kBleTaskAttr = { .name = "rsi_ble",
.attr_bits = osThreadDetached,
.cb_mem = &sBleTaskControlBlock,
.cb_size = osThreadCbSize,
.stack_mem = bleStack,
.stack_size = kBleTaskSize,
.priority = osPriorityHigh };

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

// Used to send the Indication Confirmation
uint8_t dev_address[RSI_DEV_ADDR_LEN];
uint16_t rsi_ble_measurement_hndl;
uint16_t rsi_ble_gatt_server_client_config_hndl;

osTimerId_t sbleAdvTimeoutTimer;

osThreadId_t sBleThread;
constexpr uint32_t kBleTaskSize = 2560;
uint8_t bleStack[kBleTaskSize];
osThread_t sBleTaskControlBlock;
constexpr osThreadAttr_t kBleTaskAttr = { .name = "rsi_ble",
.attr_bits = osThreadDetached,
.cb_mem = &sBleTaskControlBlock,
.cb_size = osThreadCbSize,
.stack_mem = bleStack,
.stack_size = kBleTaskSize,
.priority = osPriorityHigh };

void rsi_ble_add_matter_service(void)
{
constexpr uuid_t custom_service = { .size = RSI_BLE_MATTER_CUSTOM_SERVICE_SIZE,
.val = { .val16 = RSI_BLE_MATTER_CUSTOM_SERVICE_VALUE_16 } };
uint8_t data[RSI_BLE_MATTER_CUSTOM_SERVICE_DATA_LENGTH] = { RSI_BLE_MATTER_CUSTOM_SERVICE_DATA };

constexpr uuid_t custom_characteristic_RX = { .size = RSI_BLE_CUSTOM_CHARACTERISTIC_RX_SIZE,
.reserved = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_RESERVED },
.val = { .val128 = {
.data1 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_1 },
.data2 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_2 },
.data3 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_3 },
.data4 = { RSI_BLE_CUSTOM_CHARACTERISTIC_RX_VALUE_128_DATA_4 } } } };

rsi_ble_resp_add_serv_t new_serv_resp = { 0 };
rsi_ble_add_service(custom_service, &new_serv_resp);

// Adding custom characteristic declaration to the custom service
SilabsBleWrapper::rsi_ble_add_char_serv_att(
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_ATTRIBUTE_HANDLE_LOCATION,
RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ, // Set read, write, write without response
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_VALUE_HANDLE_LOCATION, custom_characteristic_RX);

// Adding characteristic value attribute to the service
SilabsBleWrapper::rsi_ble_add_char_val_att(
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_RX_VALUE_HANDLE_LOCATION,
custom_characteristic_RX,
RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ, // Set read, write, write without response
data, sizeof(data), ATT_REC_IN_HOST);

constexpr uuid_t custom_characteristic_TX = { .size = RSI_BLE_CUSTOM_CHARACTERISTIC_TX_SIZE,
.reserved = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_RESERVED },
.val = { .val128 = {
.data1 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_1 },
.data2 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_2 },
.data3 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_3 },
.data4 = { RSI_BLE_CUSTOM_CHARACTERISTIC_TX_VALUE_128_DATA_4 } } } };

// Adding custom characteristic declaration to the custom service
SilabsBleWrapper::rsi_ble_add_char_serv_att(
new_serv_resp.serv_handler, new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_ATTRIBUTE_HANDLE_LOCATION,
RSI_BLE_ATT_PROPERTY_WRITE_NO_RESPONSE | RSI_BLE_ATT_PROPERTY_WRITE | RSI_BLE_ATT_PROPERTY_READ |
RSI_BLE_ATT_PROPERTY_NOTIFY | RSI_BLE_ATT_PROPERTY_INDICATE, // Set read, write, write without response
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_MEASUREMENT_HANDLE_LOCATION, custom_characteristic_TX);

// Adding characteristic value attribute to the service
rsi_ble_measurement_hndl = new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_MEASUREMENT_HANDLE_LOCATION;

// Adding characteristic value attribute to the service
rsi_ble_gatt_server_client_config_hndl =
new_serv_resp.start_handle + RSI_BLE_CHARACTERISTIC_TX_GATT_SERVER_CLIENT_HANDLE_LOCATION;

SilabsBleWrapper::rsi_ble_add_char_val_att(new_serv_resp.serv_handler, rsi_ble_measurement_hndl, custom_characteristic_TX,
RSI_BLE_ATT_PROPERTY_WRITE_NO_RESPONSE | RSI_BLE_ATT_PROPERTY_WRITE |
RSI_BLE_ATT_PROPERTY_READ | RSI_BLE_ATT_PROPERTY_NOTIFY |
RSI_BLE_ATT_PROPERTY_INDICATE, // Set read, write, write without response
data, sizeof(data), ATT_REC_MAINTAIN_IN_HOST);
}

} // namespace

BLEManagerImpl BLEManagerImpl::sInstance;
Expand All @@ -120,15 +181,14 @@ void BLEManagerImpl::ProcessEvent(SilabsBleWrapper::BleEvent_t inEvent)
switch (inEvent.eventType)
{
case SilabsBleWrapper::BleEventType::RSI_BLE_CONN_EVENT: {
BLEMgrImpl().HandleConnectEvent((inEvent.eventData));
BLEMgrImpl().HandleConnectEvent(inEvent.eventData);
// Requests the connection parameters change with the remote device
rsi_ble_conn_params_update(inEvent.eventData->resp_enh_conn.dev_addr, BLE_MIN_CONNECTION_INTERVAL_MS,
rsi_ble_conn_params_update(inEvent.eventData.resp_enh_conn.dev_addr, BLE_MIN_CONNECTION_INTERVAL_MS,
BLE_MAX_CONNECTION_INTERVAL_MS, BLE_SLAVE_LATENCY_MS, BLE_TIMEOUT_MS);
rsi_ble_set_data_len(inEvent.eventData->resp_enh_conn.dev_addr, RSI_BLE_TX_OCTETS, RSI_BLE_TX_TIME);
rsi_ble_set_data_len(inEvent.eventData.resp_enh_conn.dev_addr, RSI_BLE_TX_OCTETS, RSI_BLE_TX_TIME);

// Used to send the Indication confirmation
memcpy(dev_address, inEvent.eventData->resp_enh_conn.dev_addr, RSI_DEV_ADDR_LEN);
ble_measurement_hndl = inEvent.eventData->rsi_ble_measurement_hndl;
memcpy(dev_address, inEvent.eventData.resp_enh_conn.dev_addr, RSI_DEV_ADDR_LEN);
}
break;
case SilabsBleWrapper::BleEventType::RSI_BLE_DISCONN_EVENT: {
Expand All @@ -143,7 +203,7 @@ void BLEManagerImpl::ProcessEvent(SilabsBleWrapper::BleEvent_t inEvent)
break;
case SilabsBleWrapper::BleEventType::RSI_BLE_EVENT_GATT_RD: {
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
if (inEvent.eventData->rsi_ble_read_req->type == 0)
if (inEvent.eventData.rsi_ble_read_req->type == 0)
{
BLEMgrImpl().HandleC3ReadRequest(inEvent.eventData);
}
Expand Down Expand Up @@ -220,10 +280,10 @@ void BLEManagerImpl::sl_ble_init()
SilabsBleWrapper::rsi_ble_on_event_indication_confirmation, NULL);

// Exchange of GATT info with BLE stack
SilabsBleWrapper::rsi_ble_add_matter_service();
rsi_ble_add_matter_service();
rsi_ble_set_random_address_with_value(randomAddrBLE);

sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(WfxEvent_t), NULL);
sInstance.sBleEventQueue = osMessageQueueNew(WFX_QUEUE_SIZE, sizeof(SilabsBleWrapper::BleEvent_t), NULL);
VerifyOrDie(sInstance.sBleEventQueue != nullptr);

chip::DeviceLayer::Internal::BLEMgrImpl().HandleBootEvent();
Expand Down Expand Up @@ -440,7 +500,7 @@ CHIP_ERROR BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const Chi
PacketBufferHandle data)
{
int32_t status = 0;
status = rsi_ble_indicate_value(dev_address, ble_measurement_hndl, data->DataLength(), data->Start());
status = rsi_ble_indicate_value(dev_address, rsi_ble_measurement_hndl, data->DataLength(), data->Start());
if (status != RSI_SUCCESS)
{
ChipLogProgress(DeviceLayer, "indication failed with error code %lx ", status);
Expand Down Expand Up @@ -734,9 +794,9 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
return err;
}

void BLEManagerImpl::UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt)
void BLEManagerImpl::UpdateMtu(const SilabsBleWrapper::sl_wfx_msg_t & evt)
{
CHIPoBLEConState * bleConnState = GetConnectionState(evt->connectionHandle);
CHIPoBLEConState * bleConnState = GetConnectionState(evt.connectionHandle);
if (bleConnState != NULL)
{
// bleConnState->MTU is a 10-bit field inside a uint16_t. We're
Expand All @@ -748,10 +808,10 @@ void BLEManagerImpl::UpdateMtu(SilabsBleWrapper::sl_wfx_msg_t * evt)
// TODO: https://github.com/project-chip/connectedhomeip/issues/2569
// tracks making this safe with a check or explaining why no check
// is needed.
ChipLogProgress(DeviceLayer, "DriveBLEState UpdateMtu %d", evt->rsi_ble_mtu.mtu_size);
ChipLogProgress(DeviceLayer, "DriveBLEState UpdateMtu %d", evt.rsi_ble_mtu.mtu_size);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
bleConnState->mtu = evt->rsi_ble_mtu.mtu_size;
bleConnState->mtu = evt.rsi_ble_mtu.mtu_size;
#pragma GCC diagnostic pop
;
}
Expand All @@ -763,14 +823,13 @@ void BLEManagerImpl::HandleBootEvent(void)
PlatformMgr().ScheduleWork(DriveBLEState, 0);
}

void BLEManagerImpl::HandleConnectEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
void BLEManagerImpl::HandleConnectEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
{
AddConnection(evt->connectionHandle, evt->bondingHandle);
AddConnection(evt.connectionHandle, evt.bondingHandle);
PlatformMgr().ScheduleWork(DriveBLEState, 0);
}

// TODO:: Implementation need to be done.
void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
void BLEManagerImpl::HandleConnectionCloseEvent(const SilabsBleWrapper::sl_wfx_msg_t & evt)
{
uint8_t connHandle = 1;

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

switch (evt->reason)
switch (evt.reason)
{

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

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

PlatformMgr().PostEventOrDie(&event);

Expand All @@ -804,11 +863,11 @@ void BLEManagerImpl::HandleConnectionCloseEvent(SilabsBleWrapper::sl_wfx_msg_t *
}
}

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

if (evt->rsi_ble_write.handle[0] == (uint8_t) evt->rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
if (evt.rsi_ble_write.handle[0] == (uint8_t) rsi_ble_gatt_server_client_config_hndl) // TODO:: compare the handle exactly
{
HandleTXCharCCCDWrite(evt);
}
Expand All @@ -818,23 +877,22 @@ void BLEManagerImpl::HandleWriteEvent(SilabsBleWrapper::sl_wfx_msg_t * evt)
}
}

// TODO:: Need to implement this
void BLEManagerImpl::HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
void BLEManagerImpl::HandleTXCharCCCDWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt)
{
CHIP_ERROR err = CHIP_NO_ERROR;
bool isIndicationEnabled = false;
ChipDeviceEvent event;
CHIPoBLEConState * bleConnState;

bleConnState = GetConnectionState(evt->connectionHandle);
bleConnState = GetConnectionState(evt.connectionHandle);
VerifyOrExit(bleConnState != NULL, err = CHIP_ERROR_NO_MEMORY);

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

if (isIndicationEnabled)
Expand Down Expand Up @@ -864,13 +922,13 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
}
}

void BLEManagerImpl::HandleRXCharWrite(SilabsBleWrapper::sl_wfx_msg_t * evt)
void BLEManagerImpl::HandleRXCharWrite(const SilabsBleWrapper::sl_wfx_msg_t & evt)
{
uint8_t conId = 1;
CHIP_ERROR err = CHIP_NO_ERROR;
System::PacketBufferHandle buf;
uint16_t writeLen = evt->rsi_ble_write.length;
uint8_t * data = (uint8_t *) evt->rsi_ble_write.att_value;
uint16_t writeLen = evt.rsi_ble_write.length;
uint8_t * data = (uint8_t *) evt.rsi_ble_write.att_value;

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

void BLEManagerImpl::HandleC3ReadRequest(SilabsBleWrapper::sl_wfx_msg_t * evt)
void BLEManagerImpl::HandleC3ReadRequest(const SilabsBleWrapper::sl_wfx_msg_t & evt)
{
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,
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,
GATT_READ_ZERO_OFFSET, sInstance.c3AdditionalDataBufferHandle->DataLength(),
sInstance.c3AdditionalDataBufferHandle->Start());
if (ret != SL_STATUS_OK)
Expand Down
Loading

0 comments on commit 7026061

Please sign in to comment.