From 3028105ffddd110da23481a6cbef50640707687c Mon Sep 17 00:00:00 2001 From: weicheng Date: Fri, 2 Jun 2023 04:25:19 +0800 Subject: [PATCH] [ASR] update BLE implementation (#27000) --- src/platform/ASR/ASRFactoryDataProvider.cpp | 20 ++- src/platform/ASR/BLEAppSvc.cpp | 63 ++++++- src/platform/ASR/BLEAppSvc.h | 13 +- src/platform/ASR/BLEManagerImpl.cpp | 185 +++++++++++++++----- src/platform/ASR/BLEManagerImpl.h | 14 +- third_party/asr/asr582x/asr_sdk | 2 +- third_party/asr/asr595x/asr_sdk | 2 +- 7 files changed, 236 insertions(+), 63 deletions(-) diff --git a/src/platform/ASR/ASRFactoryDataProvider.cpp b/src/platform/ASR/ASRFactoryDataProvider.cpp index 382b4abdd8c898..d21202af43a1d7 100644 --- a/src/platform/ASR/ASRFactoryDataProvider.cpp +++ b/src/platform/ASR/ASRFactoryDataProvider.cpp @@ -16,6 +16,7 @@ */ #include #include +#include #include #include #include @@ -463,9 +464,11 @@ CHIP_ERROR ASRFactoryDataProvider::GetHardwareVersionString(char * buf, size_t b CHIP_ERROR ASRFactoryDataProvider::GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) { ChipError err = CHIP_ERROR_WRONG_KEY_TYPE; -#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) +#if CHIP_ENABLE_ROTATING_DEVICE_ID static_assert(ConfigurationManager::kRotatingDeviceIDUniqueIDLength >= ConfigurationManager::kMinRotatingDeviceIDUniqueIDLength, "Length of unique ID for rotating device ID is smaller than minimum."); +#if !CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER +#ifdef CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID constexpr uint8_t uniqueId[] = CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID; ReturnErrorCodeIf(sizeof(uniqueId) > uniqueIdSpan.size(), CHIP_ERROR_BUFFER_TOO_SMALL); @@ -473,7 +476,20 @@ CHIP_ERROR ASRFactoryDataProvider::GetRotatingDeviceIdUniqueId(MutableByteSpan & memcpy(uniqueIdSpan.data(), uniqueId, sizeof(uniqueId)); uniqueIdSpan.reduce_size(sizeof(uniqueId)); return CHIP_NO_ERROR; -#endif +#endif // CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID +#else // CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER +#define ROTATING_UNIQUE_ID_STRING_LEN ConfigurationManager::kRotatingDeviceIDUniqueIDLength * 2 + uint8_t buffer[ROTATING_UNIQUE_ID_STRING_LEN] = { 0 }; + size_t buffer_len = ROTATING_UNIQUE_ID_STRING_LEN; + ReturnErrorCodeIf(ConfigurationManager::kRotatingDeviceIDUniqueIDLength > uniqueIdSpan.size(), CHIP_ERROR_BUFFER_TOO_SMALL); + ReturnErrorOnFailure(ASRConfig::ReadFactoryConfigValue(ASR_ROTATING_UNIQUE_ID_PARTITION, buffer, buffer_len, buffer_len)); + size_t bytesLen = + chip::Encoding::HexToBytes(Uint8::to_char(buffer), ROTATING_UNIQUE_ID_STRING_LEN, uniqueIdSpan.data(), uniqueIdSpan.size()); + ReturnErrorCodeIf(bytesLen != ConfigurationManager::kRotatingDeviceIDUniqueIDLength, CHIP_ERROR_INVALID_STRING_LENGTH); + uniqueIdSpan.reduce_size(bytesLen); + return CHIP_NO_ERROR; +#endif // CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER +#endif // CHIP_ENABLE_ROTATING_DEVICE_ID return err; } diff --git a/src/platform/ASR/BLEAppSvc.cpp b/src/platform/ASR/BLEAppSvc.cpp index 8c40aa3741f10b..0b9735ba60afd4 100644 --- a/src/platform/ASR/BLEAppSvc.cpp +++ b/src/platform/ASR/BLEAppSvc.cpp @@ -80,6 +80,14 @@ ble_gatt_att_reg_t matter_csvc_atts[CSVC_IDX_NB] = { SONATA_PERM(UUID_LEN, UUID_128) }, { 0, 0 } }, [CSVC_IDX_TX_CFG] = { { { 0X02, 0X29, 0 }, PRD_NA | PWR_NA, 2, PRI }, { matter_tx_CCCD_write_cb, matter_tx_CCCD_read_cb } }, +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + [CSVC_IDX_C3_CHAR] = { { { 0X03, 0X28, 0 }, PRD_NA, 0, 0 }, { 0, 0 } }, + [CSVC_IDX_C3_VAL] = { { { 0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64 }, + PRD_NA, + 512, + PRI | SONATA_PERM(UUID_LEN, UUID_128) }, + { 0, matter_c3_char_read_cb } }, +#endif }; /* * FUNCTION DEFINITIONS @@ -87,19 +95,41 @@ ble_gatt_att_reg_t matter_csvc_atts[CSVC_IDX_NB] = { */ void matter_ble_stack_open(void) { - ChipLogProgress(DeviceLayer, "matter_ble_stack_open\r\n"); + ChipLogProgress(DeviceLayer, "matter_ble_stack_open"); app_ble_stack_start(USER_MATTER_MODULE_ID); } -void matter_ble_start_adv(void) +void matter_ble_start_adv(bool fast) { ble_adv_data_t data; ble_scan_data_t scan_data; + sonata_gap_directed_adv_create_param_t param = { 0 }; + memset(&data, 0, sizeof(ble_adv_data_t)); memset(&scan_data, 0, sizeof(ble_scan_data_t)); BLEMgrImpl().SetAdvertisingData((uint8_t *) &data.ble_advdata, (uint8_t *) &data.ble_advdataLen); BLEMgrImpl().SetScanRspData((uint8_t *) &scan_data.ble_respdata, (uint8_t *) &scan_data.ble_respdataLen); - app_ble_advertising_start(APP_MATTER_ADV_IDX, &data, &scan_data); + + param.disc_mode = SONATA_GAP_ADV_MODE_GEN_DISC; + param.prop = SONATA_GAP_ADV_PROP_UNDIR_CONN_MASK; + param.max_tx_pwr = 0xE2; + param.filter_pol = SONATA_ADV_ALLOW_SCAN_ANY_CON_ANY; + param.addr_type = SONATA_GAP_STATIC_ADDR; + param.chnl_map = 0x07; + param.phy = SONATA_GAP_PHY_LE_1MBPS; + if (fast) + { + param.adv_intv_min = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN; + param.adv_intv_max = CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX; + ChipLogProgress(DeviceLayer, "fast advertising"); + } + else + { + param.adv_intv_min = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN; + param.adv_intv_max = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX; + ChipLogProgress(DeviceLayer, "slow advertising"); + } + app_ble_start_advertising_with_param(APP_MATTER_ADV_IDX, SONATA_GAP_STATIC_ADDR, ¶m, &data, &scan_data); } void matter_ble_stop_adv(void) @@ -112,31 +142,43 @@ void matter_ble_add_service() ret = app_ble_gatt_add_svc_helper(&service_handle, sizeof(matter_csvc_atts) / sizeof(ble_gatt_att_reg_t), 1, matter_csvc_atts); if (ret != 0) { - ChipLogError(DeviceLayer, "matter_ble_add_service add service failed\r\n"); + ChipLogError(DeviceLayer, "matter_ble_add_service add service failed"); } else { - ChipLogProgress(DeviceLayer, "matter_ble_add_service add service service_handle=%d\r\n", service_handle); + ChipLogProgress(DeviceLayer, "matter_ble_add_service add service service_handle=%d", service_handle); } } void matter_set_connection_id(uint8_t conId) { - ChipLogProgress(DeviceLayer, "matter_set_connection_id conId=%d\r\n", conId); + ChipLogProgress(DeviceLayer, "matter_set_connection_id conId=%d", conId); BLEMgrImpl().AllocConnectionState(conId); current_connect_id = conId; } +void matter_close_connection(uint8_t conId) +{ + if (current_connect_id != SONATA_ADDR_NONE && conId != current_connect_id) + { + ChipLogError(DeviceLayer, "wrong connection id"); + } + sonata_ble_gap_disconnect(conId, 0); +} + uint16_t matter_ble_complete_event_handler(int opt_id, uint8_t status, uint16_t param, uint32_t dwparam) { uint16_t ret = MATTER_EVENT_DONE_CONTINUE; switch (opt_id) { + case SONATA_GAP_CMP_BLE_ON: + matter_ble_add_service(); + BLEMgrImpl().SetStackInit(); + break; case SONATA_GAP_CMP_ADVERTISING_START: // 0x0F06 if (is_matter_activity(param)) { BLEMgrImpl().SetAdvStartFlag(); - matter_ble_add_service(); } break; case SONATA_GAP_CMP_ADVERTISING_STOP: @@ -189,7 +231,12 @@ void matter_tx_CCCD_read_cb(uint8_t * data, uint16_t * size) { BLEMgrImpl().HandleTXCharCCCDRead(current_connect_id, size, data); } - +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +void matter_c3_char_read_cb(uint8_t * data, uint16_t * size) +{ + BLEMgrImpl().HandleC3CharRead(current_connect_id, size, data); +} +#endif void matter_tx_char_send_indication(uint8_t conId, uint16_t size, uint8_t * data) { ChipLogProgress(DeviceLayer, "matter_tx_char_send_indication conId=%d size=%d data=%p service_handle=%d", conId, size, data, diff --git a/src/platform/ASR/BLEAppSvc.h b/src/platform/ASR/BLEAppSvc.h index 1e063e4b6b2a80..169c59a6bc07de 100644 --- a/src/platform/ASR/BLEAppSvc.h +++ b/src/platform/ASR/BLEAppSvc.h @@ -27,6 +27,7 @@ * INCLUDE FILES **************************************************************************************** */ +#include #ifdef __cplusplus extern "C" { #endif @@ -51,6 +52,10 @@ enum csvc_att_db_handles CSVC_IDX_TX_CHAR, CSVC_IDX_TX_VAL, CSVC_IDX_TX_CFG, +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + CSVC_IDX_C3_CHAR, + CSVC_IDX_C3_VAL, +#endif CSVC_IDX_NB, }; @@ -75,14 +80,16 @@ uint16_t matter_ble_complete_event_handler(int opt_id, uint8_t status, uint16_t void matter_ble_stop_adv(); -void matter_ble_start_adv(); +void matter_ble_start_adv(bool fast); void matter_set_connection_id(uint8_t conId); - +void matter_close_connection(uint8_t conId); void matter_tx_CCCD_write_cb(uint8_t * data, uint16_t size); void matter_rx_char_write_cb(uint8_t * data, uint16_t size); void matter_tx_CCCD_read_cb(uint8_t * data, uint16_t * size); - +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +void matter_c3_char_read_cb(uint8_t * data, uint16_t * size); +#endif void matter_tx_char_send_indication(uint8_t conId, uint16_t size, uint8_t * data); void matter_init_callback(void); diff --git a/src/platform/ASR/BLEManagerImpl.cpp b/src/platform/ASR/BLEManagerImpl.cpp index 791f36a745204c..51bd920b832dc7 100644 --- a/src/platform/ASR/BLEManagerImpl.cpp +++ b/src/platform/ASR/BLEManagerImpl.cpp @@ -26,7 +26,11 @@ #include #include #include +#include #include +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +#include +#endif #include "app.h" @@ -63,7 +67,13 @@ const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0 const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, 0x12 } }; - +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +const ChipBleUUID ChipUUID_CHIPoBLEChar_C3 = { { 0x64, 0x63, 0x02, 0x38, 0x87, 0x72, 0x45, 0xF2, 0xB8, 0x7D, 0x74, 0x8A, 0x83, 0x21, + 0x8F, 0x04 } }; +#endif +static constexpr System::Clock::Timeout kFastAdvertiseTimeout = + System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME); +System::Clock::Timestamp mAdvertiseStartTime; } // unnamed namespace BLEManagerImpl BLEManagerImpl::sInstance; @@ -77,7 +87,6 @@ extern "C" { #include "sonata_utils_api.h" int init_ble_task(void); int ble_close(void); -// uint16_t app_ble_stop_adv(uint8_t adv_idx); extern uint8_t ble_open; #ifdef __cplusplus } @@ -91,20 +100,21 @@ CHIP_ERROR BLEManagerImpl::_Init() // Initialize the CHIP BleLayer. err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer()); SuccessOrExit(err); + mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled; log_i("%s:%s:%d err=%s ble_open=%d\r\n", "BLEManagerImpl", __func__, __LINE__, ErrorStr(err), (int) ble_open); matter_init_callback(); if (ble_open == 0) { log_i("%s:%s:%d err=%s ble_open=%d\r\n", "BLEManagerImpl", __func__, __LINE__, ErrorStr(err), (int) ble_open); - // init_ble_task(); matter_ble_stack_open(); } else { + mFlags.Set(Flags::kFlag_StackInitialized, true); log_i("ble is alread open!\n"); } log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); - mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled; + if (CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART) { mFlags.Set(Flags::kFlag_AdvertisingEnabled, true); @@ -115,6 +125,7 @@ CHIP_ERROR BLEManagerImpl::_Init() mFlags.Set(Flags::kFlag_AdvertisingEnabled, false); log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); } + mFlags.Set(Flags::kFlag_FastAdvertisingEnabled); log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); mNumCons = 0; memset(mCons, 0, sizeof(mCons)); @@ -122,8 +133,10 @@ CHIP_ERROR BLEManagerImpl::_Init() ChipLogProgress(DeviceLayer, "BLEManagerImpl::Init() complete"); log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); - PlatformMgr().ScheduleWork(DriveBLEState, 0); - + if (mFlags.Has(Flags::kFlag_StackInitialized)) + { + PlatformMgr().ScheduleWork(DriveBLEState, 0); + } exit: log_i("%s:%s:%d err=%s\r\n", "BLEManagerImpl", __func__, __LINE__, ErrorStr(err)); return err; @@ -135,30 +148,74 @@ bool BLEManagerImpl::_IsAdvertisingEnabled(void) return mFlags.Has(Flags::kFlag_AdvertisingEnabled); } +void BLEManagerImpl::HandleFastAdvertisementTimer() +{ + System::Clock::Timestamp currentTimestamp = System::SystemClock().GetMonotonicTimestamp(); + + if (currentTimestamp - mAdvertiseStartTime >= kFastAdvertiseTimeout) + { + mFlags.Set(Flags::kFlag_FastAdvertisingEnabled, 0); + mFlags.Set(Flags::kFlag_AdvertisingRestarted, 1); + PlatformMgr().ScheduleWork(DriveBLEState, 0); + } +} + +void BLEManagerImpl::HandleFastAdvertisementTimer(System::Layer * systemLayer, void * context) +{ + static_cast(context)->HandleFastAdvertisementTimer(); +} + CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val) { CHIP_ERROR err = CHIP_NO_ERROR; log_i("%s:%s:%d val=%d\r\n", "BLEManagerImpl", __func__, __LINE__, val); VerifyOrExit(mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported, err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); + if (val) + { + mAdvertiseStartTime = System::SystemClock().GetMonotonicTimestamp(); + ReturnErrorOnFailure(DeviceLayer::SystemLayer().StartTimer(kFastAdvertiseTimeout, HandleFastAdvertisementTimer, this)); + } + if (mFlags.Has(Flags::kFlag_AdvertisingEnabled) != val) { mFlags.Set(Flags::kFlag_AdvertisingEnabled, val); + mFlags.Set(Flags::kFlag_FastAdvertisingEnabled, val); PlatformMgr().ScheduleWork(DriveBLEState, 0); } - exit: return err; } -/* - * TODO - */ CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode) { - (void) (mode); log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); - return CHIP_ERROR_NOT_IMPLEMENTED; + bool modeChange = false; + switch (mode) + { + case BLEAdvertisingMode::kFastAdvertising: + if (!mFlags.Has(Flags::kFlag_FastAdvertisingEnabled)) + { + mFlags.Set(Flags::kFlag_FastAdvertisingEnabled, true); + modeChange = true; + } + break; + case BLEAdvertisingMode::kSlowAdvertising: + if (!mFlags.Has(Flags::kFlag_FastAdvertisingEnabled)) + { + mFlags.Set(Flags::kFlag_FastAdvertisingEnabled, false); + modeChange = false; + } + break; + default: + return CHIP_ERROR_INVALID_ARGUMENT; + } + if (modeChange && mFlags.Has(Flags::kFlag_Advertising)) + { + mFlags.Set(Flags::kFlag_AdvertisingRestarted); + } + PlatformMgr().ScheduleWork(DriveBLEState, 0); + return CHIP_NO_ERROR; } CHIP_ERROR BLEManagerImpl::_GetDeviceName(char * buf, size_t bufSize) @@ -191,7 +248,6 @@ uint16_t BLEManagerImpl::_NumConnections(void) void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) { - // log_i("\r\n%s:%s:%d event->Type=%d\r\n","BLEManagerImpl",__func__,__LINE__,event->Type); switch (event->Type) { case DeviceEventType::kCHIPoBLESubscribe: @@ -226,14 +282,9 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) case DeviceEventType::kServiceProvisioningChange: case DeviceEventType::kWiFiConnectivityChange: - // Force the advertising configuration to be refreshed to reflect new provisioning state. ChipLogProgress(DeviceLayer, "Updating advertising data"); - // mFlags.Clear(Flags::kAdvertisingConfigured); - // mFlags.Set(Flags::kAdvertisingRefreshNeeded); - DriveBLEState(); - default: break; } @@ -256,6 +307,7 @@ bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, cons bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId) { log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); + matter_close_connection(conId); return true; } @@ -284,9 +336,6 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUU CHIP_ERROR err = CHIP_NO_ERROR; CHIPoBLEConState * conState = GetConnectionState(conId); - // log_i( "%s:%s:%d Sending indication for CHIPoBLE TX characteristic (con %u, len %u)\r\n", - // "BLEManagerImpl",__func__,__LINE__,conId, data->DataLength()); log_i("character - // id=%02x-%02x-%02x\r\n",charId->bytes[0],charId->bytes[1],charId->bytes[2]); VerifyOrExit(conState != NULL, err = CHIP_ERROR_INVALID_ARGUMENT); matter_tx_char_send_indication(conId, data->DataLength(), data->Start()); @@ -332,12 +381,13 @@ void BLEManagerImpl::DriveBLEState(void) log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); // Perform any initialization actions that must occur after the CHIP task is running. - if (!mFlags.Has(Flags::kFlag_AsyncInitCompleted)) + if (!mFlags.Has(Flags::kFlag_StackInitialized)) { - mFlags.Set(Flags::kFlag_AsyncInitCompleted, true); + ChipLogError(DeviceLayer, "stack not initialized"); + return; } - ChipLogProgress(DeviceLayer, "%s:%s:%d kFlag_AdvertisingEnabled=%d \r\n", "BLEManagerImpl", __func__, __LINE__, - mFlags.Has(Flags::kFlag_AdvertisingEnabled)); + ChipLogProgress(DeviceLayer, "%s:%s:%d kFlag_AdvertisingEnabled=%d kFlag_FastAdvertisingEnabled=%d", "BLEManagerImpl", __func__, + __LINE__, mFlags.Has(Flags::kFlag_AdvertisingEnabled), mFlags.Has(Flags::kFlag_FastAdvertisingEnabled)); // If the application has enabled CHIPoBLE and BLE advertising... if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && @@ -351,13 +401,17 @@ void BLEManagerImpl::DriveBLEState(void) log_i("%s:%s:%d\r\n", "BLEManagerImpl", __func__, __LINE__); // Start/re-start SoftDevice advertising if not already advertising, or if the // advertising state of the SoftDevice needs to be refreshed. - if (!mFlags.Has(Flags::kFlag_Advertising) || mFlags.Has(Flags::kFlag_AdvertisingRefreshNeeded)) + if (!mFlags.Has(Flags::kFlag_Advertising)) { ChipLogProgress(DeviceLayer, "CHIPoBLE advertising started"); - matter_ble_start_adv(); + matter_ble_start_adv(mFlags.Has(Flags::kFlag_FastAdvertisingEnabled)); + } + else if (mFlags.Has(Flags::kFlag_AdvertisingRestarted) && mFlags.Has(Flags::kFlag_Advertising)) + { + ChipLogProgress(DeviceLayer, "CHIPoBLE stop advertising to restart"); + matter_ble_stop_adv(); } } - // Otherwise, stop advertising if currently active. else { @@ -365,7 +419,6 @@ void BLEManagerImpl::DriveBLEState(void) if (mFlags.Has(Flags::kFlag_Advertising)) { ChipLogProgress(DeviceLayer, "CHIPoBLE stop advertising"); - // sonata_ble_stop_advertising(); matter_ble_stop_adv(); } } @@ -400,15 +453,11 @@ void BLEManagerImpl::SetAdvertisingData(uint8_t * data, uint8_t * len) return; } - // if (!mFlags.Has(Flags::kUseCustom DeviceName)) - { - snprintf(mDeviceName, sizeof(mDeviceName), "%s%04u", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, discriminator); - mDeviceName[32] = 0; - ChipLogError(DeviceLayer, "%s:%s:%d mDeviceName=%s\r\n", "BLEManagerImpl", __func__, __LINE__, mDeviceName); - } + snprintf(mDeviceName, sizeof(mDeviceName), "%s%04u", CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX, discriminator); + mDeviceName[32] = 0; + ChipLogError(DeviceLayer, "%s:%s:%d mDeviceName=%s", "BLEManagerImpl", __func__, __LINE__, mDeviceName); memset(advData, 0, sizeof(advData)); - advData[index++] = 0x0B; // length advData[index++] = SONATA_GAP_AD_TYPE_SERVICE_16_BIT_DATA; // AD type: (Service Data - 16-bit UUID) advData[index++] = ShortUUID_CHIPoBLEService[0]; // AD value @@ -416,15 +465,18 @@ void BLEManagerImpl::SetAdvertisingData(uint8_t * data, uint8_t * len) if (index + sizeof(deviceIdInfo) > CHIP_MAX_ADV_DATA_LEN) { - ChipLogError(DeviceLayer, "SetAdvertisingData advdata extend len=%d\r\n", index); + ChipLogError(DeviceLayer, "SetAdvertisingData advdata extend len=%d", index); return; } +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + deviceIdInfo.SetAdditionalDataFlag(true); +#endif memcpy(&advData[index], &deviceIdInfo, sizeof(deviceIdInfo)); index = static_cast(index + sizeof(deviceIdInfo)); if (index + strlen(mDeviceName) + 2 > CHIP_MAX_ADV_DATA_LEN) { - ChipLogError(DeviceLayer, "SetAdvertisingData advdata extend len=%d\r\n", index); + ChipLogError(DeviceLayer, "SetAdvertisingData advdata extend len=%d", index); return; } advData[index++] = strlen(mDeviceName) + 1; @@ -447,7 +499,7 @@ void BLEManagerImpl::SetScanRspData(uint8_t * data, uint8_t * len) advData[index++] = SONATA_GAP_AD_TYPE_COMPLETE_NAME; if (index + strlen(mDeviceName) > CHIP_MAX_ADV_DATA_LEN) { - ChipLogError(DeviceLayer, "advdata extend len=%d\r\n", index); + ChipLogError(DeviceLayer, "advdata extend len=%d", index); return; } memcpy(&advData[index], mDeviceName, strlen(mDeviceName)); @@ -463,7 +515,7 @@ void BLEManagerImpl::SetAdvStartFlag(void) CHIP_ERROR err; mFlags.Set(Flags::kFlag_Advertising, true); - mFlags.Set(Flags::kFlag_AdvertisingRefreshNeeded, false); + mFlags.Set(Flags::kFlag_AdvertisingRestarted, false); ChipDeviceEvent advChange; advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; @@ -491,6 +543,16 @@ void BLEManagerImpl::SetAdvEndFlag(void) { ChipLogError(DeviceLayer, "SetAdvEndFlag to error: %s", ErrorStr(err)); } + if (mFlags.Has(Flags::kFlag_AdvertisingRestarted)) + { + PlatformMgr().ScheduleWork(DriveBLEState, 0); + } +} + +void BLEManagerImpl::SetStackInit(void) +{ + mFlags.Set(Flags::kFlag_StackInitialized, true); + PlatformMgr().ScheduleWork(DriveBLEState, 0); } bool BLEManagerImpl::HandleRXCharWrite(uint8_t connection_id, uint16_t length, uint8_t * value) @@ -570,6 +632,47 @@ bool BLEManagerImpl::HandleTXCharCCCDWrite(uint8_t connection_id, uint16_t lengt return true; } +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +void BLEManagerImpl::HandleC3CharRead(uint8_t connection_id, uint16_t * p_len, uint8_t * p_value) +{ + CHIP_ERROR err = CHIP_NO_ERROR; + CHIPoBLEConState * conState; + PacketBufferHandle bufferHandle; + BitFlags additionalDataFields; + AdditionalDataPayloadGeneratorParams additionalDataPayloadParams; + + conState = GetConnectionState(connection_id); + if (conState == NULL) + { + log_i("%s:%d,exit", __func__, __LINE__); + return; + } +#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) + uint8_t rotatingDeviceIdUniqueId[ConfigurationManager::kRotatingDeviceIDUniqueIDLength] = {}; + MutableByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId); + err = DeviceLayer::GetDeviceInstanceInfoProvider()->GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan); + SuccessOrExit(err); + err = ConfigurationMgr().GetLifetimeCounter(additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter); + SuccessOrExit(err); + additionalDataPayloadParams.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueIdSpan; + additionalDataFields.Set(AdditionalDataFields::RotatingDeviceId); +#endif /* CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID) */ + err = AdditionalDataPayloadGenerator().generateAdditionalDataPayload(additionalDataPayloadParams, bufferHandle, + additionalDataFields); + SuccessOrExit(err); + *p_len = bufferHandle->DataLength(); + memcpy(p_value, bufferHandle->Start(), *p_len); + + log_i("%s:%s:%d length=%d\r\n", "BLEManagerImpl", __func__, __LINE__, *p_len); +exit: + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to generate TLV encoded Additional Data (%s)", __func__); + } + return; +} +#endif /* CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING */ + void BLEManagerImpl::SendIndicationConfirm(uint16_t conId) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -577,7 +680,6 @@ void BLEManagerImpl::SendIndicationConfirm(uint16_t conId) CHIPoBLEConState * pConnection = GetConnectionState(conId); if (pConnection) { - // log_i(" SendIndicationConfirm conId=%d\r\n",conId); event.Type = DeviceEventType::kCHIPoBLENotifyConfirm; event.CHIPoBLENotifyConfirm.ConId = conId; err = PlatformMgr().PostEvent(&event); @@ -622,7 +724,6 @@ BLEManagerImpl::CHIPoBLEConState * BLEManagerImpl::AllocConnectionState(uint16_t BLEManagerImpl::CHIPoBLEConState * BLEManagerImpl::GetConnectionState(uint16_t conId) { - // log_i("%s:%s:%d conId=%d\r\n","BLEManagerImpl",__func__,__LINE__,conId); for (uint16_t i = 0; i < kMaxConnections; i++) { if (mCons[i].ConId == conId) diff --git a/src/platform/ASR/BLEManagerImpl.h b/src/platform/ASR/BLEManagerImpl.h index e29f4d0a671852..82844d44a98e70 100644 --- a/src/platform/ASR/BLEManagerImpl.h +++ b/src/platform/ASR/BLEManagerImpl.h @@ -83,14 +83,12 @@ class BLEManagerImpl final : public BLEManager, // ===== Private members reserved for use by this class only. enum class Flags : uint16_t { - kFlag_AsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */ + kFlag_StackInitialized = 0x0001, /**< One-time asynchronous initialization actions have been performed. */ kFlag_AdvertisingEnabled = 0x0002, /**< The application has enabled CHIPoBLE advertising. */ kFlag_FastAdvertisingEnabled = 0x0004, /**< The application has enabled fast advertising. */ kFlag_Advertising = 0x0008, /**< The system is currently CHIPoBLE advertising. */ - kFlag_AdvertisingRefreshNeeded = + kFlag_AdvertisingRestarted = 0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */ - kFlag_DeviceNameSet = 0x0020, - kFlag_StackInitialized = 0x0040, }; enum @@ -118,17 +116,21 @@ class BLEManagerImpl final : public BLEManager, void SetScanRspData(uint8_t * data, uint8_t * len); void SetAdvStartFlag(void); void SetAdvEndFlag(void); + void SetStackInit(void); CHIPoBLEConState * AllocConnectionState(uint16_t conId); CHIPoBLEConState * GetConnectionState(uint16_t conId); bool ReleaseConnectionState(uint16_t conId); void SetConnectionMtu(uint16_t conId, uint16_t mtu); - static void DriveBLEState(intptr_t arg); - void HandleTXCharCCCDRead(uint8_t connection_id, uint16_t * length, uint8_t * value); bool HandleRXCharWrite(uint8_t connection_id, uint16_t length, uint8_t * value); bool HandleTXCharCCCDWrite(uint8_t connection_id, uint16_t length, uint8_t * value); void SendIndicationConfirm(uint16_t conId); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + void HandleC3CharRead(uint8_t connection_id, uint16_t * p_len, uint8_t * p_value); +#endif + static void HandleFastAdvertisementTimer(System::Layer * systemLayer, void * context); + void HandleFastAdvertisementTimer(); }; /** diff --git a/third_party/asr/asr582x/asr_sdk b/third_party/asr/asr582x/asr_sdk index 3c26f86d72abbb..66ab48a1ab851f 160000 --- a/third_party/asr/asr582x/asr_sdk +++ b/third_party/asr/asr582x/asr_sdk @@ -1 +1 @@ -Subproject commit 3c26f86d72abbb167b6d6bed3d712a4790f8d023 +Subproject commit 66ab48a1ab851f77117307ac410aca8f175bc4b1 diff --git a/third_party/asr/asr595x/asr_sdk b/third_party/asr/asr595x/asr_sdk index 7f5547c5439121..d85ca3cb552e8e 160000 --- a/third_party/asr/asr595x/asr_sdk +++ b/third_party/asr/asr595x/asr_sdk @@ -1 +1 @@ -Subproject commit 7f5547c5439121ec81623be99fa40b37ffaf9f4c +Subproject commit d85ca3cb552e8e7a72b2f8b04bf62e12c9a58835