Skip to content

Commit 1487495

Browse files
tx2rxpull[bot]
authored andcommitted
[ASR] update BLE implementation (#27000)
1 parent 6407f40 commit 1487495

File tree

7 files changed

+236
-63
lines changed

7 files changed

+236
-63
lines changed

src/platform/ASR/ASRFactoryDataProvider.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
#include <crypto/CHIPCryptoPAL.h>
1818
#include <lib/support/Base64.h>
19+
#include <lib/support/BytesToHex.h>
1920
#include <platform/ASR/ASRConfig.h>
2021
#include <platform/ASR/ASRFactoryDataProvider.h>
2122
#include <platform/CHIPDeviceConfig.h>
@@ -463,17 +464,32 @@ CHIP_ERROR ASRFactoryDataProvider::GetHardwareVersionString(char * buf, size_t b
463464
CHIP_ERROR ASRFactoryDataProvider::GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan)
464465
{
465466
ChipError err = CHIP_ERROR_WRONG_KEY_TYPE;
466-
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
467+
#if CHIP_ENABLE_ROTATING_DEVICE_ID
467468
static_assert(ConfigurationManager::kRotatingDeviceIDUniqueIDLength >= ConfigurationManager::kMinRotatingDeviceIDUniqueIDLength,
468469
"Length of unique ID for rotating device ID is smaller than minimum.");
470+
#if !CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER
471+
#ifdef CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID
469472
constexpr uint8_t uniqueId[] = CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID;
470473

471474
ReturnErrorCodeIf(sizeof(uniqueId) > uniqueIdSpan.size(), CHIP_ERROR_BUFFER_TOO_SMALL);
472475
ReturnErrorCodeIf(sizeof(uniqueId) != ConfigurationManager::kRotatingDeviceIDUniqueIDLength, CHIP_ERROR_BUFFER_TOO_SMALL);
473476
memcpy(uniqueIdSpan.data(), uniqueId, sizeof(uniqueId));
474477
uniqueIdSpan.reduce_size(sizeof(uniqueId));
475478
return CHIP_NO_ERROR;
476-
#endif
479+
#endif // CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID
480+
#else // CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER
481+
#define ROTATING_UNIQUE_ID_STRING_LEN ConfigurationManager::kRotatingDeviceIDUniqueIDLength * 2
482+
uint8_t buffer[ROTATING_UNIQUE_ID_STRING_LEN] = { 0 };
483+
size_t buffer_len = ROTATING_UNIQUE_ID_STRING_LEN;
484+
ReturnErrorCodeIf(ConfigurationManager::kRotatingDeviceIDUniqueIDLength > uniqueIdSpan.size(), CHIP_ERROR_BUFFER_TOO_SMALL);
485+
ReturnErrorOnFailure(ASRConfig::ReadFactoryConfigValue(ASR_ROTATING_UNIQUE_ID_PARTITION, buffer, buffer_len, buffer_len));
486+
size_t bytesLen =
487+
chip::Encoding::HexToBytes(Uint8::to_char(buffer), ROTATING_UNIQUE_ID_STRING_LEN, uniqueIdSpan.data(), uniqueIdSpan.size());
488+
ReturnErrorCodeIf(bytesLen != ConfigurationManager::kRotatingDeviceIDUniqueIDLength, CHIP_ERROR_INVALID_STRING_LENGTH);
489+
uniqueIdSpan.reduce_size(bytesLen);
490+
return CHIP_NO_ERROR;
491+
#endif // CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER
492+
#endif // CHIP_ENABLE_ROTATING_DEVICE_ID
477493
return err;
478494
}
479495

src/platform/ASR/BLEAppSvc.cpp

+55-8
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,56 @@ ble_gatt_att_reg_t matter_csvc_atts[CSVC_IDX_NB] = {
8080
SONATA_PERM(UUID_LEN, UUID_128) },
8181
{ 0, 0 } },
8282
[CSVC_IDX_TX_CFG] = { { { 0X02, 0X29, 0 }, PRD_NA | PWR_NA, 2, PRI }, { matter_tx_CCCD_write_cb, matter_tx_CCCD_read_cb } },
83+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
84+
[CSVC_IDX_C3_CHAR] = { { { 0X03, 0X28, 0 }, PRD_NA, 0, 0 }, { 0, 0 } },
85+
[CSVC_IDX_C3_VAL] = { { { 0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64 },
86+
PRD_NA,
87+
512,
88+
PRI | SONATA_PERM(UUID_LEN, UUID_128) },
89+
{ 0, matter_c3_char_read_cb } },
90+
#endif
8391
};
8492
/*
8593
* FUNCTION DEFINITIONS
8694
****************************************************************************************
8795
*/
8896
void matter_ble_stack_open(void)
8997
{
90-
ChipLogProgress(DeviceLayer, "matter_ble_stack_open\r\n");
98+
ChipLogProgress(DeviceLayer, "matter_ble_stack_open");
9199
app_ble_stack_start(USER_MATTER_MODULE_ID);
92100
}
93101

94-
void matter_ble_start_adv(void)
102+
void matter_ble_start_adv(bool fast)
95103
{
96104
ble_adv_data_t data;
97105
ble_scan_data_t scan_data;
106+
sonata_gap_directed_adv_create_param_t param = { 0 };
107+
98108
memset(&data, 0, sizeof(ble_adv_data_t));
99109
memset(&scan_data, 0, sizeof(ble_scan_data_t));
100110
BLEMgrImpl().SetAdvertisingData((uint8_t *) &data.ble_advdata, (uint8_t *) &data.ble_advdataLen);
101111
BLEMgrImpl().SetScanRspData((uint8_t *) &scan_data.ble_respdata, (uint8_t *) &scan_data.ble_respdataLen);
102-
app_ble_advertising_start(APP_MATTER_ADV_IDX, &data, &scan_data);
112+
113+
param.disc_mode = SONATA_GAP_ADV_MODE_GEN_DISC;
114+
param.prop = SONATA_GAP_ADV_PROP_UNDIR_CONN_MASK;
115+
param.max_tx_pwr = 0xE2;
116+
param.filter_pol = SONATA_ADV_ALLOW_SCAN_ANY_CON_ANY;
117+
param.addr_type = SONATA_GAP_STATIC_ADDR;
118+
param.chnl_map = 0x07;
119+
param.phy = SONATA_GAP_PHY_LE_1MBPS;
120+
if (fast)
121+
{
122+
param.adv_intv_min = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN;
123+
param.adv_intv_max = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX;
124+
ChipLogProgress(DeviceLayer, "fast advertising");
125+
}
126+
else
127+
{
128+
param.adv_intv_min = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN;
129+
param.adv_intv_max = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX;
130+
ChipLogProgress(DeviceLayer, "slow advertising");
131+
}
132+
app_ble_start_advertising_with_param(APP_MATTER_ADV_IDX, SONATA_GAP_STATIC_ADDR, &param, &data, &scan_data);
103133
}
104134

105135
void matter_ble_stop_adv(void)
@@ -112,31 +142,43 @@ void matter_ble_add_service()
112142
ret = app_ble_gatt_add_svc_helper(&service_handle, sizeof(matter_csvc_atts) / sizeof(ble_gatt_att_reg_t), 1, matter_csvc_atts);
113143
if (ret != 0)
114144
{
115-
ChipLogError(DeviceLayer, "matter_ble_add_service add service failed\r\n");
145+
ChipLogError(DeviceLayer, "matter_ble_add_service add service failed");
116146
}
117147
else
118148
{
119-
ChipLogProgress(DeviceLayer, "matter_ble_add_service add service service_handle=%d\r\n", service_handle);
149+
ChipLogProgress(DeviceLayer, "matter_ble_add_service add service service_handle=%d", service_handle);
120150
}
121151
}
122152
void matter_set_connection_id(uint8_t conId)
123153
{
124-
ChipLogProgress(DeviceLayer, "matter_set_connection_id conId=%d\r\n", conId);
154+
ChipLogProgress(DeviceLayer, "matter_set_connection_id conId=%d", conId);
125155
BLEMgrImpl().AllocConnectionState(conId);
126156
current_connect_id = conId;
127157
}
128158

159+
void matter_close_connection(uint8_t conId)
160+
{
161+
if (current_connect_id != SONATA_ADDR_NONE && conId != current_connect_id)
162+
{
163+
ChipLogError(DeviceLayer, "wrong connection id");
164+
}
165+
sonata_ble_gap_disconnect(conId, 0);
166+
}
167+
129168
uint16_t matter_ble_complete_event_handler(int opt_id, uint8_t status, uint16_t param, uint32_t dwparam)
130169
{
131170
uint16_t ret = MATTER_EVENT_DONE_CONTINUE;
132171

133172
switch (opt_id)
134173
{
174+
case SONATA_GAP_CMP_BLE_ON:
175+
matter_ble_add_service();
176+
BLEMgrImpl().SetStackInit();
177+
break;
135178
case SONATA_GAP_CMP_ADVERTISING_START: // 0x0F06
136179
if (is_matter_activity(param))
137180
{
138181
BLEMgrImpl().SetAdvStartFlag();
139-
matter_ble_add_service();
140182
}
141183
break;
142184
case SONATA_GAP_CMP_ADVERTISING_STOP:
@@ -189,7 +231,12 @@ void matter_tx_CCCD_read_cb(uint8_t * data, uint16_t * size)
189231
{
190232
BLEMgrImpl().HandleTXCharCCCDRead(current_connect_id, size, data);
191233
}
192-
234+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
235+
void matter_c3_char_read_cb(uint8_t * data, uint16_t * size)
236+
{
237+
BLEMgrImpl().HandleC3CharRead(current_connect_id, size, data);
238+
}
239+
#endif
193240
void matter_tx_char_send_indication(uint8_t conId, uint16_t size, uint8_t * data)
194241
{
195242
ChipLogProgress(DeviceLayer, "matter_tx_char_send_indication conId=%d size=%d data=%p service_handle=%d", conId, size, data,

src/platform/ASR/BLEAppSvc.h

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* INCLUDE FILES
2828
****************************************************************************************
2929
*/
30+
#include <platform/DeviceInstanceInfoProvider.h>
3031
#ifdef __cplusplus
3132
extern "C" {
3233
#endif
@@ -51,6 +52,10 @@ enum csvc_att_db_handles
5152
CSVC_IDX_TX_CHAR,
5253
CSVC_IDX_TX_VAL,
5354
CSVC_IDX_TX_CFG,
55+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
56+
CSVC_IDX_C3_CHAR,
57+
CSVC_IDX_C3_VAL,
58+
#endif
5459
CSVC_IDX_NB,
5560
};
5661

@@ -75,14 +80,16 @@ uint16_t matter_ble_complete_event_handler(int opt_id, uint8_t status, uint16_t
7580

7681
void matter_ble_stop_adv();
7782

78-
void matter_ble_start_adv();
83+
void matter_ble_start_adv(bool fast);
7984

8085
void matter_set_connection_id(uint8_t conId);
81-
86+
void matter_close_connection(uint8_t conId);
8287
void matter_tx_CCCD_write_cb(uint8_t * data, uint16_t size);
8388
void matter_rx_char_write_cb(uint8_t * data, uint16_t size);
8489
void matter_tx_CCCD_read_cb(uint8_t * data, uint16_t * size);
85-
90+
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
91+
void matter_c3_char_read_cb(uint8_t * data, uint16_t * size);
92+
#endif
8693
void matter_tx_char_send_indication(uint8_t conId, uint16_t size, uint8_t * data);
8794

8895
void matter_init_callback(void);

0 commit comments

Comments
 (0)