From 46023e86ea0250d51270be38091eb7d5914c90c2 Mon Sep 17 00:00:00 2001 From: jczhang777 <101778393+jczhang777@users.noreply.github.com> Date: Tue, 27 Sep 2022 01:15:51 +0800 Subject: [PATCH] Update flash tool, add mac os flash tool,add ipv6 get addr hook,update readme ,add board link,update ble impl, fix support phone commission (#22798) * change properties of C2 Characteristic of BLE GATT to indication * change properties of C2 Characteristic of BLE GATT to indication for BL702 * update supported boards * [BL602] 1.Update flash tool, 2.Add ipv6 get ip hook * [BL602] Fix board led onoff * update README.md * correct spell * update workflow build * [BL602] Update flash tool name * Restyled by prettier-markdown * [BL602] Remove not use code * Restyled by clang-format * [BL616] Restyled * Sync ZCL State to LightingManager State setting * Restyled by clang-format * fix license conflict * fix license conflict-1 * fix license conflict-2 * specifiy bfbl_iot_tool version * [BL602] Delete not use code Co-authored-by: wyhong Co-authored-by: Restyled.io Co-authored-by: wyhong <30567533+wy-hh@users.noreply.github.com> Co-authored-by: shchen-Lab Co-authored-by: shchen-Lab <75290921+shchen-Lab@users.noreply.github.com> --- .github/workflows/examples-bouffalolab.yaml | 8 +- .../lighting-app/bouffalolab/bl602/BUILD.gn | 3 + .../lighting-app/bouffalolab/bl602/README.md | 5 +- .../bouffalolab/bl602/include/AppTask.h | 1 + .../bl602/include/CHIPDeviceManager.h | 4 +- .../bouffalolab/bl602/src/AppTask.cpp | 15 + .../bouffalolab/bl602/src/DeviceCallbacks.cpp | 13 +- .../lighting-app/bouffalolab/bl702/README.md | 62 +-- .../bl602/route_hook/bl_route_hook.c | 206 +++++++++ .../bl602/route_hook/bl_route_hook.h | 10 + .../bl602/route_hook/bl_route_table.c | 174 ++++++++ .../bl602/route_hook/bl_route_table.h | 76 ++++ scripts/build/build/targets.py | 6 +- scripts/build/builders/bouffalolab.py | 7 +- .../testdata/all_targets_except_host.txt | 6 +- .../build/testdata/build_all_except_host.txt | 28 +- .../glob_star_targets_except_host.txt | 6 +- scripts/examples/gn_bouffalolab_example.sh | 2 +- scripts/requirements.bouffalolab.txt | 2 +- .../bouffalolab/BL602/BLEManagerImpl.cpp | 411 ++++++++++-------- .../bouffalolab/BL602/BLEManagerImpl.h | 33 +- .../BL602/CHIPDevicePlatformEvent.h | 19 +- .../BL602/ConnectivityManagerImpl.cpp | 10 + .../BL602/ConnectivityManagerImpl.h | 1 + src/platform/bouffalolab/BL602/DnssdImpl.cpp | 12 - .../bouffalolab/BL602/PlatformManagerImpl.cpp | 5 + .../bouffalolab/BL702/BLEManagerImpl.cpp | 173 +++++--- .../bouffalolab/BL702/BLEManagerImpl.h | 28 +- src/platform/bouffalolab/BL702/BUILD.gn | 3 +- .../bouffalolab/BL702/BlePlatformConfig.h | 5 +- .../BL702/CHIPDevicePlatformConfig.h | 5 +- .../BL702/CHIPDevicePlatformEvent.h | 20 +- .../bouffalolab/BL702/CHIPPlatformConfig.h | 13 +- src/platform/bouffalolab/BL702/CHIPmemory.cpp | 10 +- .../BL702/ConfigurationManagerImpl.cpp | 13 +- .../BL702/ConfigurationManagerImpl.h | 11 +- .../BL702/ConnectivityManagerImpl.cpp | 11 +- .../BL702/ConnectivityManagerImpl.h | 10 +- .../DeviceNetworkProvisioningDelegateImpl.cpp | 4 +- .../DeviceNetworkProvisioningDelegateImpl.h | 4 +- .../BL702/DiagnosticDataProviderImpl.cpp | 10 +- .../BL702/DiagnosticDataProviderImpl.h | 9 +- .../bouffalolab/BL702/InetPlatformConfig.h | 12 +- .../BL702/KeyValueStoreManagerImpl.cpp | 8 +- .../BL702/KeyValueStoreManagerImpl.h | 8 +- src/platform/bouffalolab/BL702/Logging.cpp | 17 +- .../BL702/OTAImageProcessorImpl.cpp | 3 +- .../bouffalolab/BL702/OTAImageProcessorImpl.h | 3 +- .../bouffalolab/BL702/PlatformManagerImpl.cpp | 11 +- .../bouffalolab/BL702/PlatformManagerImpl.h | 10 +- .../bouffalolab/BL702/SystemPlatformConfig.h | 12 +- .../BL702/ThreadStackManagerImpl.cpp | 13 +- .../BL702/ThreadStackManagerImpl.h | 13 +- .../bouffalolab/BL702/bl702Config.cpp | 10 +- src/platform/bouffalolab/BL702/bl702Config.h | 10 +- third_party/bouffalolab/bl602/bl602_sdk.gni | 5 + third_party/bouffalolab/repo | 2 +- 57 files changed, 1023 insertions(+), 568 deletions(-) create mode 100644 examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.c create mode 100644 examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.h create mode 100644 examples/platform/bouffalolab/bl602/route_hook/bl_route_table.c create mode 100644 examples/platform/bouffalolab/bl602/route_hook/bl_route_table.h diff --git a/.github/workflows/examples-bouffalolab.yaml b/.github/workflows/examples-bouffalolab.yaml index b50a0bb5d30d3c..4f32c97aaf424e 100644 --- a/.github/workflows/examples-bouffalolab.yaml +++ b/.github/workflows/examples-bouffalolab.yaml @@ -56,17 +56,17 @@ jobs: timeout-minutes: 30 run: | ./scripts/run_in_build_env.sh \ - "./scripts/build/build_examples.py --target bouffalolab-BL706-IoT-DVK-BL706C-22-light build" + "./scripts/build/build_examples.py --target bouffalolab-BL706-IoT-DVK-light build" .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py bl702 bl702 lighting-app \ - out/bouffalolab-BL706-IoT-DVK-BL706C-22-light/chip-bl702-lighting-example.out /tmp/bloat_reports/ + out/bouffalolab-BL706-IoT-DVK-light/chip-bl702-lighting-example.out /tmp/bloat_reports/ - name: Build example BL702 Lighting App with RPCs timeout-minutes: 30 run: | ./scripts/run_in_build_env.sh \ - "./scripts/build/build_examples.py --target bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc build" + "./scripts/build/build_examples.py --target bouffalolab-BL706-IoT-DVK-light-rpc build" .environment/pigweed-venv/bin/python3 scripts/tools/memory/gh_sizes.py bl702 bl702+rpc lighting-app \ - out/bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc/chip-bl702-lighting-example.out /tmp/bloat_reports/ + out/bouffalolab-BL706-IoT-DVK-light-rpc/chip-bl702-lighting-example.out /tmp/bloat_reports/ - name: Uploading Size Reports uses: actions/upload-artifact@v2 diff --git a/examples/lighting-app/bouffalolab/bl602/BUILD.gn b/examples/lighting-app/bouffalolab/bl602/BUILD.gn index b63a569da40842..a00a5a47503269 100644 --- a/examples/lighting-app/bouffalolab/bl602/BUILD.gn +++ b/examples/lighting-app/bouffalolab/bl602/BUILD.gn @@ -81,6 +81,8 @@ bl602_executable("lighting_app") { sources = [ #"${bl602_project_dir}/include/CHIPProjectConfig.h", "${examples_plat_dir}/InitPlatform.cpp", + "${examples_plat_dir}/route_hook/bl_route_hook.c", + "${examples_plat_dir}/route_hook/bl_route_table.c", "include/CHIPProjectConfig.h", "src/AppTask.cpp", "src/CHIPDeviceManager.cpp", @@ -162,6 +164,7 @@ bl602_executable("lighting_app") { include_dirs += [ "${chip_root}/examples/common", "${chip_root}/examples/common/pigweed/bouffalolab/bl602", + "${examples_plat_dir}/route_hook", ] } diff --git a/examples/lighting-app/bouffalolab/bl602/README.md b/examples/lighting-app/bouffalolab/bl602/README.md index d82959afd7a19d..1c66c73ecc5c83 100644 --- a/examples/lighting-app/bouffalolab/bl602/README.md +++ b/examples/lighting-app/bouffalolab/bl602/README.md @@ -3,7 +3,8 @@ This example functions as a wifi light bulb device type, with on/off capabilities. The steps were verified on BL602-IoT-Matter-V1 board. -BL602-IoT-Matter-V1 board: +BL602-IoT-Matter-V1 board and +[purchase link](https://www.amazon.com/dp/B0B9ZVGXD8): ## Initial setup @@ -66,7 +67,7 @@ The steps in this document were validated on Ubuntu 18.04 and 20.04. ``` cd third_party/bouffalolab/repo/tools/flash_tool - ./bflb_iot_tool --chipname=BL602 --baudrate=115200 --port=/dev/ttyACM0 --pt=chips/bl602/partition/partition_cfg_4M.toml --dts=chips/bl602/device_tree/bl_factory_params_IoTKitA_40M.dts --firmware=../../../../../out/bl602-light/chip-bl602-lighting-example.bin + ./bflb_iot_tool-ubuntu --chipname=BL602 --baudrate=115200 --port=/dev/ttyACM0 --pt=chips/bl602/partition/partition_cfg_4M.toml --dts=chips/bl602/device_tree/bl_factory_params_IoTKitA_40M.dts --firmware=../../../../../out/bl602-light/chip-bl602-lighting-example.bin ``` ``` diff --git a/examples/lighting-app/bouffalolab/bl602/include/AppTask.h b/examples/lighting-app/bouffalolab/bl602/include/AppTask.h index 097d040c55702c..b88d3b628311f0 100644 --- a/examples/lighting-app/bouffalolab/bl602/include/AppTask.h +++ b/examples/lighting-app/bouffalolab/bl602/include/AppTask.h @@ -49,6 +49,7 @@ class AppTask void PostEvent(const AppEvent * event); void ButtonEventHandler(uint8_t btnIdx, uint8_t btnAction); void LightStateUpdateEventHandler(void); + void LightStateInit(void); private: friend AppTask & GetAppTask(void); diff --git a/examples/lighting-app/bouffalolab/bl602/include/CHIPDeviceManager.h b/examples/lighting-app/bouffalolab/bl602/include/CHIPDeviceManager.h index 641d3e0394deb2..6fb4b28d3bc8fc 100644 --- a/examples/lighting-app/bouffalolab/bl602/include/CHIPDeviceManager.h +++ b/examples/lighting-app/bouffalolab/bl602/include/CHIPDeviceManager.h @@ -70,8 +70,8 @@ class DLL_EXPORT CHIPDeviceManagerCallbacks * @param size size of the attribute * @param value pointer to the new value */ - void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, - uint8_t type, uint16_t size, uint8_t * value) + virtual void PostAttributeChangeCallback(chip::EndpointId endpoint, chip::ClusterId clusterId, chip::AttributeId attributeId, + uint8_t type, uint16_t size, uint8_t * value) {} // virtual ~CHIPDeviceManagerCallbacks(); }; diff --git a/examples/lighting-app/bouffalolab/bl602/src/AppTask.cpp b/examples/lighting-app/bouffalolab/bl602/src/AppTask.cpp index 62bb09cb5b928b..cd865f515b85e4 100644 --- a/examples/lighting-app/bouffalolab/bl602/src/AppTask.cpp +++ b/examples/lighting-app/bouffalolab/bl602/src/AppTask.cpp @@ -153,6 +153,7 @@ CHIP_ERROR AppTask::Init() PrintOnboardingCodes(chip::RendezvousInformationFlag(chip::RendezvousInformationFlag::kBLE)); InitButtons(); + #if PW_RPC_ENABLED chip::rpc::Init(); #endif @@ -576,10 +577,24 @@ void AppTask::LightStateUpdateEventHandler(void) { statusLED.SetBrightness(0); statusLED.Set(0); + PostLightActionRequest(AppEvent::kEventType_Light, LightingManager::OFF_ACTION); } else { statusLED.SetBrightness(level); + PostLightActionRequest(AppEvent::kEventType_Light, LightingManager::ON_ACTION); } } while (0); } + +void AppTask::LightStateInit(void) +{ + uint8_t onoff = 1; + uint8_t level = 254; + EndpointId endpoint = 1; + + emberAfWriteAttribute(endpoint, ZCL_LEVEL_CONTROL_CLUSTER_ID, ZCL_CURRENT_LEVEL_ATTRIBUTE_ID, (uint8_t *) &level, + ZCL_INT8U_ATTRIBUTE_TYPE); + + emberAfWriteAttribute(endpoint, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, (uint8_t *) &onoff, ZCL_BOOLEAN_ATTRIBUTE_TYPE); +} diff --git a/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp b/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp index 87d3cd73c767a8..79440c9dba1089 100644 --- a/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp +++ b/examples/lighting-app/bouffalolab/bl602/src/DeviceCallbacks.cpp @@ -37,6 +37,7 @@ #include #include #include +#include using namespace ::chip; using namespace ::chip::Inet; @@ -47,7 +48,6 @@ uint32_t identifyTimerCount; constexpr uint32_t kIdentifyTimerDelayMS = 250; static LEDWidget statusLED1; -// static LEDWidget statusLED2; void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { @@ -67,6 +67,7 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ case DeviceEventType::kCommissioningComplete: log_info("Commissioning complete\r\n"); + GetAppTask().LightStateInit(); break; case DeviceEventType::kWiFiConnectivityChange: @@ -85,6 +86,12 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ // newly selected address. chip::app::DnssdServer::Instance().StartServer(); } + + if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned) + { + ChipLogProgress(DeviceLayer, "Initializing route hook..."); + bl_route_hook_init(); + } break; } } @@ -125,15 +132,11 @@ void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established) { log_info("IPv4 Server ready...\r\n"); - // TODO - // wifiLED.Set(true); chip::app::DnssdServer::Instance().StartServer(); } else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost) { log_info("Lost IPv4 connectivity...\r\n"); - // TODO - // wifiLED.Set(false); } if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established) { diff --git a/examples/lighting-app/bouffalolab/bl702/README.md b/examples/lighting-app/bouffalolab/bl702/README.md index 0339fcedf3052d..f02b1b84758f86 100644 --- a/examples/lighting-app/bouffalolab/bl702/README.md +++ b/examples/lighting-app/bouffalolab/bl702/README.md @@ -1,14 +1,14 @@ -# CHIP BL702 Lighting App Example +# CHIP BL70X Lighting App Example -## Supported Hardware +BL70X is highly integrated BLE and IEEE 802.15.4 combo chip for IoT +applications, and BL702 is a general name for BL70X family. -Current supported develop boards: +This example is powered by BL706 and functions as a Thread light bulb device +type, with on/off, level and color capabilities. The steps were verified with +following boards: -- BL702-IoT-DVK -- BL706-IoT-DVK -- BL706-NIGHT-LIGHT - > Note, please make sure both of flash and `PSRAM` shipped with develop - > board/device are at lease 2MB. + - BL706-IoT-DVK + - BL706-NIGHT-LIGHT ## Build @@ -49,29 +49,19 @@ Current supported develop boards: ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL706-IoT-DVK ``` - - Build lighting-app for board BL702-IOT-DVK module_type is - `BL706C-22` by default. Please execute the following command to - build lighting-app for BL702-IoT-DVK with module `BL702` - - ```shell - ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL702-IoT-DVK module_type="BL702" - ``` - - Build lighting-app for board BL706-NIGHT-LIGHT - ```shell - ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL706-NIGHT-LIGHT module_type="BL702" - ``` - - > Note, please check which module is used on the board. + ```shell + ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL706-NIGHT-LIGHT + ``` - - With UART shell command support: + - With UART shell command enabled: ```shell ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL706-IoT-DVK chip_build_libshell=true ``` - - With pigweed RPC support: + - With pigweed RPC enabled: ```shell ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL706-IoT-DVK 'import("//with_pw_rpc.gni")' ``` @@ -79,21 +69,37 @@ Current supported develop boards: - Build with `build_examples.py` - - Build for BL702-IoT-DVK, BL706-IoT-DVK and BL706-NIGHT-LIGHT as - following commands. + - Build for BL706-IoT-DVK and BL706-NIGHT-LIGHT as following commands. ```shell - ./scripts/build/build_examples.py --target bouffalolab-BL702-IoT-DVK-light build ./scripts/build/build_examples.py --target bouffalolab-BL706-IoT-DVK-BL706C-22-light build ./scripts/build/build_examples.py --target bouffalolab-BL706-NIGHT-LIGHT-light build ``` - - Build with pigweed RPC support as following commands. + - Build with pigweed RPC enabled as following commands. ```shell - ./scripts/build/build_examples.py --target bouffalolab-BL702-IoT-DVK-light-rpc build ./scripts/build/build_examples.py --target bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc build ``` +- Build options + + - Build options list There are some build options for function and debug + purpose as below. + - `chip_build_libshell`, whether to enable shell command line. It is + set to false by default. + - `baudrate`, UART baudrate for log output or shell command line. + - `enable_psram`, whether to enable `PSRAM`. It is set to true by + default. + - How to use Before using these build options, please check whether they + are available to configure in BUILD.gn file under example application + folder. + - build with `build_examples.py` Modify value of build options in + BUILD.gn file under example application folder. + - build with gn_bouffalolab_example.sh Input build options, such as + ``` + ./scripts/examples/gn_bouffalolab_example.sh lighting-app out/debug BL706-IoT-DVK chip_build_libshell=true + ``` + - Download image - Using script `chip-bl702-lighting-example.flash.py`. diff --git a/examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.c b/examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.c new file mode 100644 index 00000000000000..230cccadd52657 --- /dev/null +++ b/examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.c @@ -0,0 +1,206 @@ +#include +#include + +#include "bl_route_hook.h" +#include "bl_route_table.h" +#include "utils_log.h" + +#include "lwip/icmp6.h" +#include "lwip/mld6.h" +#include "lwip/netif.h" +#include "lwip/prot/icmp6.h" +#include "lwip/prot/ip6.h" +#include "lwip/prot/nd6.h" +#include "lwip/raw.h" + +typedef struct bl_route_hook_t +{ + struct netif * netif; + struct raw_pcb * pcb; + struct bl_route_hook_t * next; +} bl_route_hook_t; + +PACK_STRUCT_BEGIN +struct rio_header_t +{ + PACK_STRUCT_FLD_8(u8_t type); + PACK_STRUCT_FLD_8(u8_t length); + PACK_STRUCT_FLD_8(u8_t prefix_length); + PACK_STRUCT_FLD_8(u8_t preference); + PACK_STRUCT_FIELD(u32_t route_lifetime); +} PACK_STRUCT_STRUCT; +PACK_STRUCT_END + +typedef struct rio_header_t rio_header_t; + +static bl_route_hook_t * s_hooks; + +static bool is_self_address(struct netif * netif, const ip6_addr_t * addr) +{ + for (size_t i = 0; i < LWIP_ARRAYSIZE(netif->ip6_addr); i++) + { + if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i)) && + memcmp(addr->addr, netif_ip6_addr(netif, i)->addr, sizeof(addr->addr)) == 0) + { + return true; + } + } + return false; +} + +static void ra_recv_handler(struct netif * netif, const uint8_t * icmp_payload, uint16_t payload_len, const ip6_addr_t * src_addr) +{ + if (payload_len < sizeof(struct ra_header)) + { + return; + } + icmp_payload += sizeof(struct ra_header); + payload_len -= sizeof(struct ra_header); + + while (payload_len >= 2) + { + uint8_t opt_type = icmp_payload[0]; + uint8_t opt_len = icmp_payload[1] << 3; + + if (opt_type == ND6_OPTION_TYPE_ROUTE_INFO && opt_len >= sizeof(rio_header_t) && !is_self_address(netif, src_addr) && + payload_len >= opt_len) + { + rio_header_t rio_header; + memcpy(&rio_header, icmp_payload, sizeof(rio_header)); + + // skip if prefix is longer than IPv6 address. + if (rio_header.prefix_length > 128) + { + break; + } + uint8_t prefix_len_bytes = (rio_header.prefix_length + 7) / 8; + int8_t preference = -2 * ((rio_header.preference >> 4) & 1) + (((rio_header.preference) >> 3) & 1); + const uint8_t * rio_data = &icmp_payload[sizeof(rio_header_t)]; + uint8_t rio_data_len = opt_len - sizeof(rio_header_t); + + log_info("Received RIO\n"); + if (rio_data_len >= prefix_len_bytes) + { + ip6_addr_t prefix; + bl_route_entry_t route; + + memset(&prefix, 0, sizeof(prefix)); + memcpy(&prefix.addr, rio_data, prefix_len_bytes); + route.netif = netif; + route.gateway = *src_addr; + route.prefix_length = rio_header.prefix_length; + route.prefix = prefix; + route.preference = preference; + route.lifetime_seconds = lwip_ntohl(rio_header.route_lifetime); + log_info("prefix %s lifetime %u\n", ip6addr_ntoa(&prefix), route.lifetime_seconds); + if (bl_route_table_add_route_entry(&route) == NULL) + { + log_error("Failed to add route table entry\n"); + } + else + { + log_info("Added entry to route table\n"); + } + } + } + icmp_payload += opt_len; + payload_len -= opt_len; + } +} + +static uint8_t icmp6_raw_recv_handler(void * arg, struct raw_pcb * pcb, struct pbuf * p, const ip_addr_t * addr) +{ + uint8_t * icmp_payload = NULL; + uint16_t icmp_payload_len; + struct ip6_hdr * ip6_header = (struct ip6_hdr *) p->payload; + struct icmp6_hdr * icmp6_header; + ip6_addr_t src; + ip6_addr_t dest; + bl_route_hook_t * hook = (bl_route_hook_t *) arg; + + memcpy(src.addr, ip6_header->src.addr, sizeof(src.addr)); + memcpy(dest.addr, ip6_header->dest.addr, sizeof(dest.addr)); +#if LWIP_IPV6_SCOPES + src.zone = 0; +#endif + + if (p->tot_len != p->len) + { + log_error("Ignore segmented ICMP packet\n"); + return 0; + } + if (p->tot_len <= sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) + { + log_error("Ignore invalid ICMP packet\n"); + return 0; + } + if (!ip6_addr_islinklocal(&dest) && !ip6_addr_isallnodes_linklocal(&dest) && !ip6_addr_isallrouters_linklocal(&dest)) + { + return 0; + } + + icmp_payload_len = p->tot_len - sizeof(struct ip6_hdr); + icmp_payload = p->payload + sizeof(struct ip6_hdr); + + icmp6_header = (struct icmp6_hdr *) icmp_payload; + if (icmp6_header->type == ICMP6_TYPE_RA) + { + ra_recv_handler(hook->netif, icmp_payload, icmp_payload_len, &src); + } + return 0; +} + +int8_t bl_route_hook_init() +{ + struct netif * lwip_netif = wifi_mgmr_sta_netif_get(); + ip_addr_t router_group = IPADDR6_INIT_HOST(0xFF020000, 0, 0, 0x02); + bl_route_hook_t * hook = NULL; + uint8_t ret = 0; + + if (lwip_netif == NULL) + { + log_error("Invalid network interface\n"); + return -1; + } + + for (bl_route_hook_t * iter = s_hooks; iter != NULL; iter++) + { + if (iter->netif == lwip_netif) + { + log_error("Hook already installed on netif, skip...\n"); + return 0; + } + } + + hook = (bl_route_hook_t *) malloc(sizeof(bl_route_hook_t)); + if (hook == NULL) + { + log_error("Cannot allocate hook\n"); + return -1; + } + + if (mld6_joingroup_netif(lwip_netif, ip_2_ip6(&router_group)) != ERR_OK) + { + log_error("Failed to join multicast group\n"); + ret = -1; + goto exit; + } + + hook->netif = lwip_netif; + hook->pcb = raw_new_ip_type(IPADDR_TYPE_V6, IP6_NEXTH_ICMP6); + hook->pcb->flags |= RAW_FLAGS_MULTICAST_LOOP; + hook->pcb->chksum_reqd = 1; + // The ICMPv6 header checksum offset + hook->pcb->chksum_offset = 2; + raw_bind_netif(hook->pcb, lwip_netif); + raw_recv(hook->pcb, icmp6_raw_recv_handler, hook); + hook->next = s_hooks; + s_hooks = hook; + +exit: + if (ret != 0 && hook != NULL) + { + free(hook); + } + return ret; +} diff --git a/examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.h b/examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.h new file mode 100644 index 00000000000000..80c8bfec06a56a --- /dev/null +++ b/examples/platform/bouffalolab/bl602/route_hook/bl_route_hook.h @@ -0,0 +1,10 @@ +#include +#ifdef __cplusplus +extern "C" { +#endif + +int8_t bl_route_hook_init(void); + +#ifdef __cplusplus +} +#endif diff --git a/examples/platform/bouffalolab/bl602/route_hook/bl_route_table.c b/examples/platform/bouffalolab/bl602/route_hook/bl_route_table.c new file mode 100644 index 00000000000000..4d2c90129712bf --- /dev/null +++ b/examples/platform/bouffalolab/bl602/route_hook/bl_route_table.c @@ -0,0 +1,174 @@ +#include "bl_route_table.h" +#include +#include + +#include "lwip/ip6_addr.h" +#include "lwip/netif.h" +#include "lwip/timeouts.h" + +#define MAX_RIO_ROUTE 20 +#define MAX_RIO_TIMEOUT UINT32_MAX / (1000 * 4) // lwIP defined reasonable timeout value + +static bl_route_entry_t s_route_entries[MAX_RIO_ROUTE]; + +static bl_route_entry_t * find_route_entry(const bl_route_entry_t * route_entry) +{ + for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + { + if (s_route_entries[i].netif == NULL) + { + break; + } + if (s_route_entries[i].netif == route_entry->netif && s_route_entries[i].prefix_length == route_entry->prefix_length && + memcmp(s_route_entries[i].gateway.addr, route_entry->gateway.addr, sizeof(route_entry->gateway.addr)) == 0 && + memcmp(s_route_entries[i].prefix.addr, route_entry->prefix.addr, route_entry->prefix_length / 8) == 0) + { + return &s_route_entries[i]; + } + } + return NULL; +} + +static bl_route_entry_t * find_empty_route_entry(void) +{ + for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + { + if (s_route_entries[i].netif == NULL) + { + return &s_route_entries[i]; + } + } + return NULL; +} + +static void route_timeout_handler(void * arg) +{ + bl_route_entry_t * route = (bl_route_entry_t *) arg; + + bl_route_table_remove_route_entry(route); +} + +bl_route_entry_t * bl_route_table_add_route_entry(const bl_route_entry_t * route_entry) +{ + if (route_entry == NULL || (route_entry->lifetime_seconds > MAX_RIO_TIMEOUT && route_entry->lifetime_seconds != UINT32_MAX)) + { + return NULL; + } + + bl_route_entry_t * entry = find_route_entry(route_entry); + + if (entry == NULL) + { + entry = find_empty_route_entry(); + if (entry == NULL) + { + return NULL; + } + entry->netif = route_entry->netif; + entry->gateway = route_entry->gateway; + ip6_addr_assign_zone(&entry->gateway, IP6_UNICAST, entry->netif); + entry->prefix = route_entry->prefix; + entry->prefix_length = route_entry->prefix_length; + } + else + { + sys_untimeout(route_timeout_handler, entry); + } + entry->preference = route_entry->preference; + entry->lifetime_seconds = route_entry->lifetime_seconds; + if (entry->lifetime_seconds != UINT32_MAX) + { + sys_timeout(entry->lifetime_seconds * 1000, route_timeout_handler, entry); + } + return entry; +} + +int8_t bl_route_table_remove_route_entry(bl_route_entry_t * route_entry) +{ + if (route_entry < &s_route_entries[0] || route_entry > &s_route_entries[LWIP_ARRAYSIZE(s_route_entries)]) + { + return -1; + } + route_entry->netif = NULL; + for (bl_route_entry_t * moved = route_entry; moved < &s_route_entries[LWIP_ARRAYSIZE(s_route_entries) - 1]; moved++) + { + *moved = *(moved + 1); + if (moved->netif == NULL) + { + break; + } + } + return 0; +} + +static inline bool is_better_route(const bl_route_entry_t * lhs, const bl_route_entry_t * rhs) +{ + if (rhs == NULL) + { + return true; + } + if (lhs == NULL) + { + return false; + } + return (lhs->prefix_length > rhs->prefix_length) || + (lhs->prefix_length == rhs->prefix_length && lhs->preference > rhs->preference); +} + +static inline bool route_match(const bl_route_entry_t * route, const ip6_addr_t * dest) +{ + return memcmp(dest, route->prefix.addr, route->prefix_length / 8) == 0; +} + +struct netif * lwip_hook_ip6_route(const ip6_addr_t * src, const ip6_addr_t * dest) +{ + bl_route_entry_t * route = NULL; + + for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + { + if (s_route_entries[i].netif == NULL) + { + break; + } + if (route_match(&s_route_entries[i], dest) && is_better_route(&s_route_entries[i], route)) + { + route = &s_route_entries[i]; + } + } + + if (route) + { + return route->netif; + } + else + { + return NULL; + } +} + +const ip6_addr_t * lwip_hook_nd6_get_gw(struct netif * netif, const ip6_addr_t * dest) +{ + bl_route_entry_t * route = NULL; + + for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + { + if (s_route_entries[i].netif == NULL) + { + break; + } + if (s_route_entries[i].netif == netif && route_match(&s_route_entries[i], dest) && + is_better_route(&s_route_entries[i], route)) + { + route = &s_route_entries[i]; + } + } + + if (route) + { + return &route->gateway; + } + else + { + return NULL; + } +} diff --git a/examples/platform/bouffalolab/bl602/route_hook/bl_route_table.h b/examples/platform/bouffalolab/bl602/route_hook/bl_route_table.h new file mode 100644 index 00000000000000..eaf0f4f8c5ec63 --- /dev/null +++ b/examples/platform/bouffalolab/bl602/route_hook/bl_route_table.h @@ -0,0 +1,76 @@ +#include + +#include "lwip/ip6_addr.h" +#include "lwip/netif.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Route table entry + * + */ +typedef struct +{ + ip6_addr_t prefix; + uint8_t prefix_length; + ip6_addr_t gateway; + int8_t preference; + uint32_t lifetime_seconds; + struct netif * netif; +} bl_route_entry_t; + +/** + * @brief Adds an entry to the route table + * + * @param[in] route_entry The route entry to be added + * + * @return + * - The pointer to the added route entry on success + * - NULL on failure + * + */ +bl_route_entry_t * bl_route_table_add_route_entry(const bl_route_entry_t * route_entry); + +/** + * @brief Removes an entry from the route table + * + * @param[in] route_entry The route entry to be removed + * + * @return + * - 0 + * - -1 The provided route_entry is not in the route table. + * + */ +int8_t bl_route_table_remove_route_entry(bl_route_entry_t * route_entry); + +/** + * @brief The lwIP ip6 route hook, called by the lwIP function ip6_route when sending packets. + * + * @param[in] src The source address + * @param[in] dest The destination address + * + * @return + * - The target interface when route found + * - NULL when route not found + * + */ +struct netif * lwip_hook_ip6_route(const ip6_addr_t * src, const ip6_addr_t * dest); + +/** + * @brief The lwIP gateway hook, called by the lwIP when deciding next hop. + * + * @param[in] netif The output network interface + * @param[in] dest The destination address + * + * @return + * - The gateway address when route found + * - NULL when route not found + * + */ +const ip6_addr_t * lwip_hook_nd6_get_gw(struct netif * netif, const ip6_addr_t * dest); + +#ifdef __cplusplus +} +#endif diff --git a/scripts/build/build/targets.py b/scripts/build/build/targets.py index 02ef6b7c369bae..648b053c6bff54 100755 --- a/scripts/build/build/targets.py +++ b/scripts/build/build/targets.py @@ -621,10 +621,8 @@ def Bl602Targets(): def BouffalolabTargets(): target = Target('bouffalolab', BouffalolabBuilder) - yield target.Extend('BL706-IoT-DVK-BL706C-22-light', board=BouffalolabBoard.BL706_IoT_DVK, app=BouffalolabApp.LIGHT, enable_rpcs=False, module_type="BL706C-22") - yield target.Extend('BL702-IoT-DVK-light', board=BouffalolabBoard.BL702_IoT_DVK, app=BouffalolabApp.LIGHT, enable_rpcs=False, module_type="BL702") - yield target.Extend('BL706-IoT-DVK-BL706C-22-light-rpc', board=BouffalolabBoard.BL706_IoT_DVK, app=BouffalolabApp.LIGHT, enable_rpcs=True, module_type="BL706C-22") - yield target.Extend('BL702-IoT-DVK-light-rpc', board=BouffalolabBoard.BL702_IoT_DVK, app=BouffalolabApp.LIGHT, enable_rpcs=True, module_type="BL702") + yield target.Extend('BL706-IoT-DVK-light', board=BouffalolabBoard.BL706_IoT_DVK, app=BouffalolabApp.LIGHT, enable_rpcs=False, module_type="BL706C-22") + yield target.Extend('BL706-IoT-DVK-light-rpc', board=BouffalolabBoard.BL706_IoT_DVK, app=BouffalolabApp.LIGHT, enable_rpcs=True, module_type="BL706C-22") yield target.Extend('BL706-NIGHT-LIGHT-light', board=BouffalolabBoard.BL706_NIGHT_LIGHT, app=BouffalolabApp.LIGHT, enable_rpcs=False, module_type="BL702") diff --git a/scripts/build/builders/bouffalolab.py b/scripts/build/builders/bouffalolab.py index 8e7fdeedfb93ce..b391251093653f 100644 --- a/scripts/build/builders/bouffalolab.py +++ b/scripts/build/builders/bouffalolab.py @@ -43,14 +43,11 @@ def FlashBundleName(self): class BouffalolabBoard(Enum): BL706_IoT_DVK = 1 - BL702_IoT_DVK = 2 - BL706_NIGHT_LIGHT = 3 + BL706_NIGHT_LIGHT = 2 def GnArgName(self): if self == BouffalolabBoard.BL706_IoT_DVK: return 'BL706-IoT-DVK' - elif self == BouffalolabBoard.BL702_IoT_DVK: - return 'BL702-IoT-DVK' elif self == BouffalolabBoard.BL706_NIGHT_LIGHT: return 'BL706-NIGHT-LIGHT' else: @@ -63,7 +60,7 @@ def __init__(self, root, runner, app: BouffalolabApp = BouffalolabApp.LIGHT, - board: BouffalolabBoard = BouffalolabBoard.BL702_IoT_DVK, + board: BouffalolabBoard = BouffalolabBoard.BL706_IoT_DVK, enable_rpcs: bool = False, module_type: str = "BL706C-22", ): diff --git a/scripts/build/testdata/all_targets_except_host.txt b/scripts/build/testdata/all_targets_except_host.txt index 1252a0dae7f059..c30e44fc41b5e6 100644 --- a/scripts/build/testdata/all_targets_except_host.txt +++ b/scripts/build/testdata/all_targets_except_host.txt @@ -18,10 +18,8 @@ android-x64-tv-server android-x86-chip-tool android-x86-tv-server bl602-light -bouffalolab-BL702-IoT-DVK-light -bouffalolab-BL702-IoT-DVK-light-rpc -bouffalolab-BL706-IoT-DVK-BL706C-22-light -bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc +bouffalolab-BL706-IoT-DVK-light +bouffalolab-BL706-IoT-DVK-light-rpc bouffalolab-BL706-NIGHT-LIGHT-light cc13x2x7_26x2x7-all-clusters cc13x2x7_26x2x7-all-clusters-minimal diff --git a/scripts/build/testdata/build_all_except_host.txt b/scripts/build/testdata/build_all_except_host.txt index ebfb4ad3070bae..d6548b7e44ffa6 100644 --- a/scripts/build/testdata/build_all_except_host.txt +++ b/scripts/build/testdata/build_all_except_host.txt @@ -151,17 +151,11 @@ bash -c 'yes | TEST_ANDROID_HOME/tools/bin/sdkmanager --licenses >/dev/null' # Generating bl602-light gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl602 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" bl602_board="BL-HWC-G1"' {out}/bl602-light -# Generating bouffalolab-BL702-IoT-DVK-light -gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL702-IoT-DVK" module_type="BL702"' {out}/bouffalolab-BL702-IoT-DVK-light +# Generating bouffalolab-BL706-IoT-DVK-light +gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL706-IoT-DVK" module_type="BL706C-22"' {out}/bouffalolab-BL706-IoT-DVK-light -# Generating bouffalolab-BL702-IoT-DVK-light-rpc -gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL702-IoT-DVK" module_type="BL702" import("//with_pw_rpc.gni")' {out}/bouffalolab-BL702-IoT-DVK-light-rpc - -# Generating bouffalolab-BL706-IoT-DVK-BL706C-22-light -gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL706-IoT-DVK" module_type="BL706C-22"' {out}/bouffalolab-BL706-IoT-DVK-BL706C-22-light - -# Generating bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc -gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL706-IoT-DVK" module_type="BL706C-22" import("//with_pw_rpc.gni")' {out}/bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc +# Generating bouffalolab-BL706-IoT-DVK-light-rpc +gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL706-IoT-DVK" module_type="BL706C-22" import("//with_pw_rpc.gni")' {out}/bouffalolab-BL706-IoT-DVK-light-rpc # Generating bouffalolab-BL706-NIGHT-LIGHT-light gn gen --check --fail-on-unused-args --export-compile-commands --root={root}/examples/lighting-app/bouffalolab/bl702 '--args=custom_toolchain="{root}/../../examples/platform/bouffalolab/common/toolchain:riscv_gcc" board="BL706-NIGHT-LIGHT" module_type="BL702"' {out}/bouffalolab-BL706-NIGHT-LIGHT-light @@ -1559,17 +1553,11 @@ cp {out}/android-x86-tv-server/lib/TvApp.jar {root}/examples/tv-app/android/App/ # Building bl602-light ninja -C {out}/bl602-light -# Building bouffalolab-BL702-IoT-DVK-light -ninja -C {out}/bouffalolab-BL702-IoT-DVK-light - -# Building bouffalolab-BL702-IoT-DVK-light-rpc -ninja -C {out}/bouffalolab-BL702-IoT-DVK-light-rpc - -# Building bouffalolab-BL706-IoT-DVK-BL706C-22-light -ninja -C {out}/bouffalolab-BL706-IoT-DVK-BL706C-22-light +# Building bouffalolab-BL706-IoT-DVK-light +ninja -C {out}/bouffalolab-BL706-IoT-DVK-light -# Building bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc -ninja -C {out}/bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc +# Building bouffalolab-BL706-IoT-DVK-light-rpc +ninja -C {out}/bouffalolab-BL706-IoT-DVK-light-rpc # Building bouffalolab-BL706-NIGHT-LIGHT-light ninja -C {out}/bouffalolab-BL706-NIGHT-LIGHT-light diff --git a/scripts/build/testdata/glob_star_targets_except_host.txt b/scripts/build/testdata/glob_star_targets_except_host.txt index 04111a9ba7b770..ecf21d637c058a 100644 --- a/scripts/build/testdata/glob_star_targets_except_host.txt +++ b/scripts/build/testdata/glob_star_targets_except_host.txt @@ -18,10 +18,8 @@ android-x64-tv-server android-x86-chip-tool android-x86-tv-server bl602-light -bouffalolab-BL702-IoT-DVK-light -bouffalolab-BL702-IoT-DVK-light-rpc -bouffalolab-BL706-IoT-DVK-BL706C-22-light -bouffalolab-BL706-IoT-DVK-BL706C-22-light-rpc +bouffalolab-BL706-IoT-DVK-light +bouffalolab-BL706-IoT-DVK-light-rpc bouffalolab-BL706-NIGHT-LIGHT-light cc13x2x7_26x2x7-all-clusters cc13x2x7_26x2x7-all-clusters-minimal diff --git a/scripts/examples/gn_bouffalolab_example.sh b/scripts/examples/gn_bouffalolab_example.sh index 3dd3a0c34da6cd..f04307607ba30b 100755 --- a/scripts/examples/gn_bouffalolab_example.sh +++ b/scripts/examples/gn_bouffalolab_example.sh @@ -31,7 +31,7 @@ source "$MATTER_ROOT/scripts/activate.sh" # export PATH="$BL_IOT_SDK_PATH/toolchain/riscv/Darwin/bin:$PATH" # fi -bl702_boards=("BL702-IoT-DVK" "BL706-IoT-DVK" "BL706-NIGHT-LIGHT") +bl702_boards=("BL706-IoT-DVK" "BL706-NIGHT-LIGHT") bl702_modules=("BL702" "BL706C-22") bl702_module_type="BL706C-22" diff --git a/scripts/requirements.bouffalolab.txt b/scripts/requirements.bouffalolab.txt index 20ecc8ba62dba7..ceda55f866c282 100644 --- a/scripts/requirements.bouffalolab.txt +++ b/scripts/requirements.bouffalolab.txt @@ -1 +1 @@ -bflb-iot-tool \ No newline at end of file +bflb-iot-tool>=1.8.0 ; platform_machine != 'aarch64' and sys_platform == 'linux' \ No newline at end of file diff --git a/src/platform/bouffalolab/BL602/BLEManagerImpl.cpp b/src/platform/bouffalolab/BL602/BLEManagerImpl.cpp index cf00d6a00095ce..825a28f4cdb1e1 100644 --- a/src/platform/bouffalolab/BL602/BLEManagerImpl.cpp +++ b/src/platform/bouffalolab/BL602/BLEManagerImpl.cpp @@ -15,18 +15,11 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the BLEManager singleton object - * for the BL602 platform. - */ - #define __ZEPHYR__ 1 #include #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE -// #include #include "BLEManagerImpl.h" #include @@ -34,27 +27,16 @@ #include #include #include +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +#include +#endif extern "C" { -// #include #include - -#if 0 -// BaseType_t xPortIsInsideInterrupt(void) __attribute__((weak, alias("_xPortIsInsideInterrupt"))); -// BaseType_t _xPortIsInsideInterrupt(void) -BaseType_t xPortIsInsideInterrupt(void) -{ - return TrapNetCounter ? 1 : 0; -} -#endif } #include #include -// #include -// #include -// #include -// #include using namespace ::chip; using namespace ::chip::Ble; @@ -64,132 +46,98 @@ namespace chip { namespace DeviceLayer { namespace Internal { -namespace { - const bt_uuid_128 UUID128_CHIPoBLEChar_RX = BT_UUID_INIT_128(0x11, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18); const bt_uuid_128 UUID128_CHIPoBLEChar_TX = BT_UUID_INIT_128(0x12, 0x9D, 0x9F, 0x42, 0x9C, 0x4F, 0x9F, 0x95, 0x59, 0x45, 0x3D, 0x26, 0xF5, 0x2E, 0xEE, 0x18); bt_uuid_16 UUID16_CHIPoBLEService = BT_UUID_INIT_16(0xFFF6); -// #define UUID128_CHIPoBLEChar_RX BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0x119d9f42, 0x9c4f, 0x9f95, 0x5945, 0x3d26f52eee18)) -// #define UUID128_CHIPoBLEChar_TX BT_UUID_DECLARE_128(BT_UUID_128_ENCODE(0x129d9f42, 0x9c4f, 0x9f95, 0x5945, 0x3d26f52eee18)) -// #define UUID16_CHIPoBLEService BT_UUID_DECLARE_16(0xFFF6) - const ChipBleUUID chipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, 0x11 } }; 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 bt_uuid_128 UUID128_CHIPoBLEChar_C3 = + BT_UUID_INIT_128(0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64); +#endif _bt_gatt_ccc CHIPoBLEChar_TX_CCC = BT_GATT_CCC_INITIALIZER(nullptr, BLEManagerImpl::HandleTXCCCWrite, nullptr); -// clang-format off - -BT_GATT_SERVICE_DEFINE(_3_CHIPoBLE_Service, +struct bt_gatt_attr sChipoBleAttributes[] = { BT_GATT_PRIMARY_SERVICE(&UUID16_CHIPoBLEService.uuid), - BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_RX.uuid, - BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP, - BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, - nullptr, BLEManagerImpl::HandleRXWrite, nullptr), - BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_TX.uuid, - BT_GATT_CHRC_NOTIFY, - BT_GATT_PERM_NONE, - nullptr, nullptr, nullptr), - BT_GATT_CCC_MANAGED(&CHIPoBLEChar_TX_CCC, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE) -); - -// static struct bt_gatt_attr attr = BT_GATT_PRIMARY_SERVICE(UUID16_CHIPoBLEService); + BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_RX.uuid, BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP, + BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, nullptr, BLEManagerImpl::HandleRXWrite, nullptr), + BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_TX.uuid, BT_GATT_CHRC_INDICATE, BT_GATT_PERM_NONE, nullptr, nullptr, nullptr), + BT_GATT_CCC_MANAGED(&CHIPoBLEChar_TX_CCC, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_C3.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, BLEManagerImpl::HandleC3Read, + nullptr, nullptr), +#endif +}; -// clang-format on +struct bt_gatt_service sChipoBleService = { + .attrs = sChipoBleAttributes, + .attr_count = sizeof(sChipoBleAttributes) / sizeof(sChipoBleAttributes[0]), +}; -// Index of the CCC descriptor in the CHIPoBLE_Service array of attributes. -// This value should be adjusted accordingly if the service declaration changes. -constexpr int kCHIPoBLE_CCC_AttributeIndex = 3; +static const int kCHIPoBLE_CCC_AttributeIndex = 3; -void InitRandomStaticAddress() +CHIP_ERROR InitRandomStaticAddress() { -#if !CONFIG_BT_PRIVACY // When the BT privacy feature is disabled, generate a random static address once per boot. // This must be done before bt_enable() has been called. bt_addr_le_t addr; int error = bt_addr_le_create_static(&addr); - VerifyOrReturn(error == 0, ChipLogError(DeviceLayer, "Failed to create BLE address: %d", error)); + if (error) + { + ChipLogError(DeviceLayer, "Failed to create BLE address: %d", error); + return System::MapErrorZephyr(error); + } error = bt_id_create(&addr, nullptr); - VerifyOrReturn(error == 0, ChipLogError(DeviceLayer, "Failed to create BLE identity: %d", error)); - - ChipLogProgress(DeviceLayer, "BLE address was set to %02X:%02X:%02X:%02X:%02X:%02X", addr.a.val[5], addr.a.val[4], - addr.a.val[3], addr.a.val[2], addr.a.val[1], addr.a.val[0]); -#endif -} -} // unnamed namespace - -BLEManagerImpl BLEManagerImpl::sInstance; - -static void bt_enable_cb(int err) -{ - log_info("err: %d\r\n", err); - if (!err) + if (error < 0) { - log_info("trace\r\n"); - // ble_cli_register(); - // log_info("trace\r\n"); + ChipLogError(DeviceLayer, "Failed to create BLE identity: %d", error); + return System::MapErrorZephyr(error); } -} -// static extern "C" void ble_stack_start(void) -static void ble_stack_start(void) -{ - log_info("trace\r\n"); - // Initialize BLE controller - ble_controller_init(configMAX_PRIORITIES - 1); - log_info("trace\r\n"); - // Initialize BLE Host stack - hci_driver_init(); - log_info("trace\r\n"); - bt_enable(bt_enable_cb); - log_info("trace\r\n"); + ChipLogProgress(DeviceLayer, "BLE address: %02X:%02X:%02X:%02X:%02X:%02X", addr.a.val[5], addr.a.val[4], addr.a.val[3], + addr.a.val[2], addr.a.val[1], addr.a.val[0]); + return CHIP_NO_ERROR; } +BLEManagerImpl BLEManagerImpl::sInstance; + CHIP_ERROR BLEManagerImpl::_Init() { - CHIP_ERROR err; - log_info("trace\r\n"); mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled; mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART); mFlags.Set(Flags::kFastAdvertisingEnabled, true); mGAPConns = 0; - log_info("trace\r\n"); memset(mSubscribedConns, 0, sizeof(mSubscribedConns)); - InitRandomStaticAddress(); - log_info("trace\r\n"); - // err = bt_enable(NULL); - // log_info("err: %d\r\n", err); - // VerifyOrExit(err == CHIP_NO_ERROR, err = MapErrorZephyr(err)); - ble_stack_start(); + ReturnErrorOnFailure(InitRandomStaticAddress()); + + ble_controller_init(configMAX_PRIORITIES - 1); + hci_driver_init(); + bt_enable(NULL); memset(&mConnCallbacks, 0, sizeof(mConnCallbacks)); mConnCallbacks.connected = HandleConnect; mConnCallbacks.disconnected = HandleDisconnect; - log_info("trace\r\n"); bt_conn_cb_register(&mConnCallbacks); - log_info("trace\r\n"); // Initialize the CHIP BleLayer. - err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer()); - log_info("err: %d\r\n", err); + ReturnErrorOnFailure(BleLayer::Init(this, this, &DeviceLayer::SystemLayer())); PlatformMgr().ScheduleWork(DriveBLEState, 0); - log_info("trace\r\n"); -exit: - return err; + return CHIP_NO_ERROR; } void BLEManagerImpl::DriveBLEState(intptr_t arg) @@ -201,8 +149,6 @@ void BLEManagerImpl::DriveBLEState() { CHIP_ERROR err = CHIP_NO_ERROR; - ChipLogDetail(DeviceLayer, "In DriveBLEState"); - // Perform any initialization actions that must occur after the CHIP task is running. if (!mFlags.Has(Flags::kAsyncInitCompleted)) { @@ -227,11 +173,27 @@ void BLEManagerImpl::DriveBLEState() SuccessOrExit(err); } } - // Otherwise, stop advertising if currently active. else { - err = StopAdvertising(); - SuccessOrExit(err); + if (mFlags.Has(Flags::kAdvertising)) + { + err = StopAdvertising(); + SuccessOrExit(err); + } + + // If no connections are active unregister also CHIPoBLE GATT service + if (NumConnections() == 0 && mFlags.Has(Flags::kChipoBleGattServiceRegister)) + { + // Unregister CHIPoBLE service to not allow discovering it when pairing is disabled. + if (bt_gatt_service_unregister(&sChipoBleService) != 0) + { + ChipLogError(DeviceLayer, "Failed to unregister CHIPoBLE GATT service"); + } + else + { + mFlags.Clear(Flags::kChipoBleGattServiceRegister); + } + } } exit: @@ -251,35 +213,54 @@ struct BLEManagerImpl::ServiceData CHIP_ERROR BLEManagerImpl::StartAdvertising(void) { int err = 0; - const char * deviceName = bt_get_name(); - const uint8_t advFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR; const bool isAdvertisingRerun = mFlags.Has(Flags::kAdvertising); // At first run always select fast advertising, on the next attempt slow down interval. - const uint16_t intervalMin = mFlags.Has(Flags::kFastAdvertisingEnabled) ? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN + const uint32_t intervalMin = mFlags.Has(Flags::kFastAdvertisingEnabled) ? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN : CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN; - const uint16_t intervalMax = mFlags.Has(Flags::kFastAdvertisingEnabled) ? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX + const uint32_t intervalMax = mFlags.Has(Flags::kFastAdvertisingEnabled) ? CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX : CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX; - // bt_le_adv_param advParams = - // BT_LE_ADV_PARAM_INIT(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME, intervalMin, intervalMax, nullptr); bt_le_adv_param advParams; advParams.id = BT_ID_DEFAULT; advParams.options = BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME; advParams.interval_min = intervalMin; advParams.interval_max = intervalMax; - // Define advertising data + // Define advertising and, if BLE device name is set, scan response data ServiceData serviceData; - bt_data ad[] = { BT_DATA(BT_DATA_FLAGS, &advFlags, sizeof(advFlags)), - BT_DATA(BT_DATA_SVC_DATA16, &serviceData, sizeof(serviceData)), - BT_DATA(BT_DATA_NAME_COMPLETE, deviceName, static_cast(strlen(deviceName))) }; + const uint8_t advFlags = BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR; + const bt_data advertisingData[] = { BT_DATA(BT_DATA_FLAGS, &advFlags, sizeof(advFlags)), + BT_DATA(BT_DATA_SVC_DATA16, &serviceData, sizeof(serviceData)) }; + + const char * deviceName = bt_get_name(); + const uint8_t deviceNameSize = static_cast(strlen(deviceName)); + const bt_data scanResponseData[] = { BT_DATA(BT_DATA_NAME_COMPLETE, deviceName, deviceNameSize) }; + const bt_data * scanResponseDataPtr = deviceNameSize > 0 ? scanResponseData : nullptr; + const size_t scanResponseDataLen = deviceNameSize > 0 ? sizeof(scanResponseData) / sizeof(scanResponseData[0]) : 0u; + + // Register dynamically CHIPoBLE GATT service + if (!mFlags.Has(Flags::kChipoBleGattServiceRegister)) + { + err = bt_gatt_service_register(&sChipoBleService); + + if (err != 0) + ChipLogError(DeviceLayer, "Failed to register CHIPoBLE GATT service"); + + VerifyOrReturnError(err == 0, MapErrorZephyr(err)); + + mFlags.Set(Flags::kChipoBleGattServiceRegister); + } // Initialize service data static_assert(sizeof(serviceData) == 10, "Size of BLE advertisement data changed! Was that intentional?"); chip::Encoding::LittleEndian::Put16(serviceData.uuid, UUID16_CHIPoBLEService.val); ReturnErrorOnFailure(ConfigurationMgr().GetBLEDeviceIdentificationInfo(serviceData.deviceIdInfo)); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + ReturnErrorOnFailure(PrepareC3CharData()); +#endif + if (!isAdvertisingRerun) { #if CONFIG_BT_PRIVACY @@ -296,7 +277,8 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) err = bt_le_adv_stop(); VerifyOrReturnError(err == 0, MapErrorZephyr(err)); - err = bt_le_adv_start(&advParams, ad, ARRAY_SIZE(ad), nullptr, 0u); + err = bt_le_adv_start(&advParams, advertisingData, sizeof(advertisingData) / sizeof(advertisingData[0]), scanResponseDataPtr, + scanResponseDataLen); VerifyOrReturnError(err == 0, MapErrorZephyr(err)); // Transition to the Advertising state... @@ -311,7 +293,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) ChipDeviceEvent advChange; advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Started; - PlatformMgr().PostEvent(&advChange); + ReturnErrorOnFailure(PlatformMgr().PostEvent(&advChange)); } if (mFlags.Has(Flags::kFastAdvertisingEnabled)) @@ -344,7 +326,7 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void) ChipDeviceEvent advChange; advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped; - PlatformMgr().PostEvent(&advChange); + ReturnErrorOnFailure(PlatformMgr().PostEvent(&advChange)); } // Cancel timer event changing CHIPoBLE advertisement interval @@ -356,20 +338,18 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void) CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val) { - CHIP_ERROR err = CHIP_NO_ERROR; - - VerifyOrExit(mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported, err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); + VerifyOrReturnError(mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported, + CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE); if (mFlags.Has(Flags::kAdvertisingEnabled) != val) { - ChipLogDetail(DeviceLayer, "SetAdvertisingEnabled(%s)", val ? "true" : "false"); + ChipLogDetail(DeviceLayer, "CHIPoBLE advertising set to %s", val ? "on" : "off"); mFlags.Set(Flags::kAdvertisingEnabled, val); PlatformMgr().ScheduleWork(DriveBLEState, 0); } -exit: - return err; + return CHIP_NO_ERROR; } CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode) @@ -399,7 +379,7 @@ CHIP_ERROR BLEManagerImpl::_GetDeviceName(char * buf, size_t bufSize) CHIP_ERROR BLEManagerImpl::_SetDeviceName(const char * deviceName) { - if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_NotSupported) + if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_NotSupported) { return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; } @@ -419,7 +399,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPConnect(const ChipDeviceEvent * event) } else { - ChipLogProgress(DeviceLayer, "BLE connection failed (reason: 0x%02x)", connEvent->HciResult); + ChipLogError(DeviceLayer, "BLE connection failed (reason: 0x%02x)", connEvent->HciResult); } ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", NumConnections(), CONFIG_BT_MAX_CONN); @@ -440,7 +420,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event) mGAPConns--; - // If notifications were enabled for this connection, record that they are now disabled and + // If indications were enabled for this connection, record that they are now disabled and // notify the BLE Layer of a disconnect. if (UnsetSubscribed(connEvent->BtConn)) { @@ -468,6 +448,10 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event) ChipLogProgress(DeviceLayer, "Current number of connections: %u/%u", NumConnections(), CONFIG_BT_MAX_CONN); + ChipDeviceEvent disconnectEvent; + disconnectEvent.Type = DeviceEventType::kCHIPoBLEConnectionClosed; + ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent)); + // Force a reconfiguration of advertising in case we switched to non-connectable mode when // the BLE connection was established. mFlags.Set(Flags::kAdvertisingRefreshNeeded); @@ -482,8 +466,8 @@ CHIP_ERROR BLEManagerImpl::HandleTXCharCCCDWrite(const ChipDeviceEvent * event) ChipLogDetail(DeviceLayer, "ConnId: 0x%02x, New CCCD value: 0x%04x", bt_conn_index(writeEvent->BtConn), writeEvent->Value); - // If the client has requested to enable notifications and if it is not yet subscribed - if (writeEvent->Value != 0 && SetSubscribed(writeEvent->BtConn)) + // If the client has requested to enable indications and if it is not yet subscribed + if (writeEvent->Value == BT_GATT_CCC_INDICATE && SetSubscribed(writeEvent->BtConn)) { // Alert the BLE layer that CHIPoBLE "subscribe" has been received and increment the bt_conn reference counter. HandleSubscribeReceived(writeEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); @@ -495,7 +479,7 @@ CHIP_ERROR BLEManagerImpl::HandleTXCharCCCDWrite(const ChipDeviceEvent * event) { ChipDeviceEvent conEstEvent; conEstEvent.Type = DeviceEventType::kCHIPoBLEConnectionEstablished; - PlatformMgr().PostEvent(&conEstEvent); + ReturnErrorOnFailure(PlatformMgr().PostEvent(&conEstEvent)); } } else @@ -513,30 +497,64 @@ CHIP_ERROR BLEManagerImpl::HandleTXCharCCCDWrite(const ChipDeviceEvent * event) CHIP_ERROR BLEManagerImpl::HandleRXCharWrite(const ChipDeviceEvent * event) { - const BleRXWriteEventType * writeEvent = &event->Platform.BleRXWriteEvent; + const BleC1WriteEventType * c1WriteEvent = &event->Platform.BleC1WriteEvent; - ChipLogDetail(DeviceLayer, "Write request received for CHIPoBLE RX (ConnId 0x%02x)", bt_conn_index(writeEvent->BtConn)); + ChipLogDetail(DeviceLayer, "Write request received for CHIPoBLE RX characteristic (ConnId 0x%02x)", + bt_conn_index(c1WriteEvent->BtConn)); - HandleWriteReceived(writeEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_RX, - PacketBufferHandle::Adopt(writeEvent->Data)); - bt_conn_unref(writeEvent->BtConn); + HandleWriteReceived(c1WriteEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_RX, + PacketBufferHandle::Adopt(c1WriteEvent->Data)); + bt_conn_unref(c1WriteEvent->BtConn); return CHIP_NO_ERROR; } CHIP_ERROR BLEManagerImpl::HandleTXCharComplete(const ChipDeviceEvent * event) { - const BleTXCompleteEventType * completeEvent = &event->Platform.BleTXCompleteEvent; + const BleC2IndDoneEventType * c2IndDoneEvent = &event->Platform.BleC2IndDoneEvent; - ChipLogDetail(DeviceLayer, "Notification for CHIPoBLE TX done (ConnId 0x%02x)", bt_conn_index(completeEvent->BtConn)); + ChipLogDetail(DeviceLayer, "Indication for CHIPoBLE TX characteristic done (ConnId 0x%02x, result 0x%02x)", + bt_conn_index(c2IndDoneEvent->BtConn), c2IndDoneEvent->Result); - // Signal the BLE Layer that the outstanding notification is complete. - HandleIndicationConfirmation(completeEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); - bt_conn_unref(completeEvent->BtConn); + // Signal the BLE Layer that the outstanding indication is complete. + HandleIndicationConfirmation(c2IndDoneEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); + bt_conn_unref(c2IndDoneEvent->BtConn); return CHIP_NO_ERROR; } +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +CHIP_ERROR BLEManagerImpl::PrepareC3CharData() +{ + CHIP_ERROR err = CHIP_NO_ERROR; + BitFlags additionalDataFields; + AdditionalDataPayloadGeneratorParams additionalDataPayloadParams; + +#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, c3CharDataBufferHandle, + additionalDataFields); + +exit: + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to generate TLV encoded Additional Data (%s)", __func__); + } + + return err; +} +#endif + void BLEManagerImpl::HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param) { BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising); @@ -561,29 +579,21 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) err = HandleTXCharCCCDWrite(event); break; - case DeviceEventType::kPlatformZephyrBleRXWrite: + case DeviceEventType::kPlatformZephyrBleC1WriteEvent: err = HandleRXCharWrite(event); break; - case DeviceEventType::kPlatformZephyrBleTXComplete: + case DeviceEventType::kPlatformZephyrBleC2IndDoneEvent: err = HandleTXCharComplete(event); break; - case DeviceEventType::kServiceProvisioningChange: - // Force the advertising state to be refreshed to reflect new provisioning state. - mFlags.Set(Flags::kAdvertisingRefreshNeeded); - - DriveBLEState(); - - break; - default: break; } if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %s", ErrorStr(err)); + ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format()); mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled; PlatformMgr().ScheduleWork(DriveBLEState, 0); } @@ -597,7 +607,7 @@ uint16_t BLEManagerImpl::_NumConnections(void) bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId) { ChipLogProgress(DeviceLayer, "Closing BLE GATT connection (ConnId %02x)", bt_conn_index((bt_conn *) conId)); - return bt_conn_disconnect(conId, BT_HCI_ERR_REMOTE_USER_TERM_CONN) == 0; + return bt_conn_disconnect((bt_conn *) conId, BT_HCI_ERR_REMOTE_USER_TERM_CONN) == 0; } uint16_t BLEManagerImpl::GetMTU(BLE_CONNECTION_OBJECT conId) const @@ -620,30 +630,30 @@ bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, cons bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId, PacketBufferHandle pBuf) { - log_info("\r\n"); - CHIP_ERROR err = CHIP_NO_ERROR; - int status = 0; - uint8_t index = bt_conn_index((bt_conn *) conId); - bt_gatt_notify_params * params = &mNotifyParams[index]; + CHIP_ERROR err = CHIP_NO_ERROR; + int status = 0; + uint8_t index = bt_conn_index(conId); + bt_gatt_indicate_params * params = &mIndicateParams[index]; - VerifyOrExit(IsSubscribed((bt_conn *) conId) == true, err = CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrExit(IsSubscribed(conId) == true, err = CHIP_ERROR_INVALID_ARGUMENT); - ChipLogDetail(DeviceLayer, "Sending notification for CHIPoBLE TX (ConnId %02x, len %u)", index, pBuf->DataLength()); + ChipLogDetail(DeviceLayer, "Sending indication for CHIPoBLE TX characteristic (ConnId %02x, len %u)", index, + pBuf->DataLength()); - params->uuid = nullptr; - params->attr = &_3_CHIPoBLE_Service.attrs[kCHIPoBLE_CCC_AttributeIndex]; - params->data = pBuf->Start(); - params->len = pBuf->DataLength(); - params->func = HandleTXCompleted; - params->user_data = nullptr; + params->uuid = nullptr; + params->attr = &sChipoBleAttributes[kCHIPoBLE_CCC_AttributeIndex]; + params->func = HandleTXIndicated; + params->data = pBuf->Start(); + params->len = pBuf->DataLength(); - status = bt_gatt_notify_cb(conId, params); + status = bt_gatt_indicate(conId, params); VerifyOrExit(status == 0, err = MapErrorZephyr(status)); exit: if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "Sending notification for CHIPoBLE TX failed: %s", ErrorStr(err)); + ChipLogError(DeviceLayer, "BLEManagerImpl::SendIndication() failed: %." CHIP_ERROR_FORMAT, err.Format()); + ChipLogError(DeviceLayer, "BLEManagerImpl::SendIndication() failed: %d", status); } return err == CHIP_NO_ERROR; @@ -672,7 +682,7 @@ bool BLEManagerImpl::SendReadResponse(BLE_CONNECTION_OBJECT conId, BLE_READ_REQU void BLEManagerImpl::NotifyChipConnectionClosed(BLE_CONNECTION_OBJECT conId) { - // Intentionally empty. + CloseConnection(conId); } bool BLEManagerImpl::IsSubscribed(bt_conn * conn) @@ -686,6 +696,8 @@ bool BLEManagerImpl::SetSubscribed(bt_conn * conn) bool isSubscribed = mSubscribedConns[index]; mSubscribedConns[index] = true; + ChipLogError(DeviceLayer, "BLEManagerImpl::SetSubscribed %u", index); + // If we were not subscribed previously, increment the reference counter of the connection. if (!isSubscribed) { @@ -716,23 +728,12 @@ ssize_t BLEManagerImpl::HandleRXWrite(struct bt_conn * conId, const struct bt_ga ChipDeviceEvent event; PacketBufferHandle packetBuf = PacketBufferHandle::NewWithData(buf, len); - // Unfortunately the Zephyr logging macros end up assigning uint16_t - // variables to uint16_t:10 fields, which triggers integer conversion - // warnings. And treating the Zephyr headers as system headers does not - // help, apparently. Just turn off that warning around this log call. -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wconversion" - // LOG_HEXDUMP_DBG(buf, len, "Rx char write"); - blog_debug_hexdump("Rx char write", buf, len); -#pragma GCC diagnostic pop - - // If successful... if (!packetBuf.IsNull()) { // Arrange to post a CHIPoBLERXWriteEvent event to the CHIP queue. - event.Type = DeviceEventType::kPlatformZephyrBleRXWrite; - event.Platform.BleRXWriteEvent.BtConn = bt_conn_ref(conId); - event.Platform.BleRXWriteEvent.Data = std::move(packetBuf).UnsafeRelease(); + event.Type = DeviceEventType::kPlatformZephyrBleC1WriteEvent; + event.Platform.BleC1WriteEvent.BtConn = bt_conn_ref(conId); + event.Platform.BleC1WriteEvent.Data = std::move(packetBuf).UnsafeRelease(); } // If we failed to allocate a buffer, post a kPlatformZephyrBleOutOfBuffersEvent event. @@ -741,7 +742,7 @@ ssize_t BLEManagerImpl::HandleRXWrite(struct bt_conn * conId, const struct bt_ga event.Type = DeviceEventType::kPlatformZephyrBleOutOfBuffersEvent; } - PlatformMgr().PostEvent(&event); + PlatformMgr().PostEventOrDie(&event); return len; } @@ -750,53 +751,85 @@ bool BLEManagerImpl::HandleTXCCCWrite(struct bt_conn * conId, const struct bt_ga { ChipDeviceEvent event; - if (value != BT_GATT_CCC_NOTIFY && value != 0) + if (value != BT_GATT_CCC_INDICATE && value != 0) { - // return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED); - return false; + return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED); } event.Type = DeviceEventType::kPlatformZephyrBleCCCWrite; event.Platform.BleCCCWriteEvent.BtConn = bt_conn_ref(conId); event.Platform.BleCCCWriteEvent.Value = value; - PlatformMgr().PostEvent(&event); + PlatformMgr().PostEventOrDie(&event); - return true; + return sizeof(value); } -void BLEManagerImpl::HandleTXCompleted(struct bt_conn * conId, void * /* param */) +void BLEManagerImpl::HandleTXIndicated(struct bt_conn * conId, IndicationAttrType, uint8_t err) { ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformZephyrBleTXComplete; - event.Platform.BleTXCompleteEvent.BtConn = bt_conn_ref(conId); + event.Type = DeviceEventType::kPlatformZephyrBleC2IndDoneEvent; + event.Platform.BleC2IndDoneEvent.BtConn = bt_conn_ref(conId); + event.Platform.BleC2IndDoneEvent.Result = err; - PlatformMgr().PostEvent(&event); + PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::HandleConnect(struct bt_conn * conId, uint8_t err) { ChipDeviceEvent event; + PlatformMgr().LockChipStack(); + + // Don't handle BLE connecting events when it is not related to CHIPoBLE + VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), ); + event.Type = DeviceEventType::kPlatformZephyrBleConnected; event.Platform.BleConnEvent.BtConn = bt_conn_ref(conId); event.Platform.BleConnEvent.HciResult = err; - PlatformMgr().PostEvent(&event); + PlatformMgr().PostEventOrDie(&event); + +exit: + PlatformMgr().UnlockChipStack(); } void BLEManagerImpl::HandleDisconnect(struct bt_conn * conId, uint8_t reason) { ChipDeviceEvent event; + PlatformMgr().LockChipStack(); + + // Don't handle BLE disconnecting events when it is not related to CHIPoBLE + VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), ); + event.Type = DeviceEventType::kPlatformZephyrBleDisconnected; event.Platform.BleConnEvent.BtConn = bt_conn_ref(conId); event.Platform.BleConnEvent.HciResult = reason; - PlatformMgr().PostEvent(&event); + PlatformMgr().PostEventOrDie(&event); + +exit: + PlatformMgr().UnlockChipStack(); } +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +ssize_t BLEManagerImpl::HandleC3Read(struct bt_conn * conId, const struct bt_gatt_attr * attr, void * buf, uint16_t len, + uint16_t offset) +{ + ChipLogDetail(DeviceLayer, "Read request received for CHIPoBLE C3 (ConnId 0x%02x)", bt_conn_index(conId)); + + if (sInstance.c3CharDataBufferHandle.IsNull()) + { + return 0; + } + + return bt_gatt_attr_read(conId, attr, buf, len, offset, sInstance.c3CharDataBufferHandle->Start(), + sInstance.c3CharDataBufferHandle->DataLength()); +} +#endif + } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/bouffalolab/BL602/BLEManagerImpl.h b/src/platform/bouffalolab/BL602/BLEManagerImpl.h index 33ab32a4078cd6..0f9ff7666a1c1e 100644 --- a/src/platform/bouffalolab/BL602/BLEManagerImpl.h +++ b/src/platform/bouffalolab/BL602/BLEManagerImpl.h @@ -1,6 +1,6 @@ /* - * - * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the BLEManager singleton object - * for the BL602 platform. - */ - #pragma once #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE @@ -38,7 +32,7 @@ namespace Internal { using namespace chip::Ble; /** - * Concrete implementation of the BLEManager singleton object for the BL602 platform. + * Concrete implementation of the BLEManager singleton object for the BL702 platform. */ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePlatformDelegate, private BleApplicationDelegate { @@ -48,7 +42,9 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla private: // ===== Members that implement the BLEManager internal interface. - + using IndicationAttrType = + std::conditional_t::value, + bt_gatt_indicate_params *, const bt_gatt_attr *>; CHIP_ERROR _Init(void); void _Shutdown() {} bool _IsAdvertisingEnabled(void); @@ -90,6 +86,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla kAdvertising = 0x0008, /**< The system is currently CHIPoBLE advertising. */ kAdvertisingRefreshNeeded = 0x0010, /**< The advertising state/configuration has changed, but the SoftDevice has yet to be updated. */ + kChipoBleGattServiceRegister = 0x0020, /**< The system has currently CHIPoBLE GATT service registered. */ }; struct ServiceData; @@ -98,8 +95,11 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla uint16_t mGAPConns; CHIPoBLEServiceMode mServiceMode; bool mSubscribedConns[CONFIG_BT_MAX_CONN]; - bt_gatt_notify_params mNotifyParams[CONFIG_BT_MAX_CONN]; + bt_gatt_indicate_params mIndicateParams[CONFIG_BT_MAX_CONN]; bt_conn_cb mConnCallbacks; +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + PacketBufferHandle c3CharDataBufferHandle; +#endif void DriveBLEState(void); CHIP_ERROR ConfigureAdvertising(void); @@ -110,6 +110,9 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla CHIP_ERROR HandleRXCharWrite(const ChipDeviceEvent * event); CHIP_ERROR HandleTXCharCCCDWrite(const ChipDeviceEvent * event); CHIP_ERROR HandleTXCharComplete(const ChipDeviceEvent * event); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + CHIP_ERROR PrepareC3CharData(); +#endif bool IsSubscribed(bt_conn * conn); bool SetSubscribed(bt_conn * conn); bool UnsetSubscribed(bt_conn * conn); @@ -118,7 +121,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla static void DriveBLEState(intptr_t arg); // Below callbacks run from the system workqueue context and have a limited stack capacity. - static void HandleTXCompleted(bt_conn * conn, void * param); + static void HandleTXIndicated(bt_conn * conn, IndicationAttrType attr, uint8_t err); static void HandleConnect(bt_conn * conn, uint8_t err); static void HandleDisconnect(bt_conn * conn, uint8_t reason); static void HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param); @@ -135,6 +138,10 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla static ssize_t HandleRXWrite(bt_conn * conn, const bt_gatt_attr * attr, const void * buf, uint16_t len, uint16_t offset, uint8_t flags); static bool HandleTXCCCWrite(bt_conn * conn, const bt_gatt_attr * attr, uint16_t value); + +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + static ssize_t HandleC3Read(struct bt_conn * conn, const struct bt_gatt_attr * attr, void * buf, uint16_t len, uint16_t offset); +#endif }; /** @@ -152,7 +159,7 @@ inline BLEManager & BLEMgr(void) * Returns the platform-specific implementation of the BLEManager singleton object. * * Internal components can use this to gain access to features of the BLEManager - * that are specific to the BL602 platform. + * that are specific to the BL702 platform. */ inline BLEManagerImpl & BLEMgrImpl(void) { diff --git a/src/platform/bouffalolab/BL602/CHIPDevicePlatformEvent.h b/src/platform/bouffalolab/BL602/CHIPDevicePlatformEvent.h index 76bd2bcdc0476c..1342cc24ac2830 100644 --- a/src/platform/bouffalolab/BL602/CHIPDevicePlatformEvent.h +++ b/src/platform/bouffalolab/BL602/CHIPDevicePlatformEvent.h @@ -34,7 +34,7 @@ namespace DeviceLayer { namespace DeviceEventType { /** - * Enumerates BL602 platform-specific event types that are visible to the application. + * Enumerates Zephyr platform-specific event types that are visible to the application. */ enum PublicPlatformSpecificEventTypes { @@ -42,7 +42,7 @@ enum PublicPlatformSpecificEventTypes }; /** - * Enumerates BL602 platform-specific event types that are internal to the chip Device Layer. + * Enumerates Zephyr platform-specific event types that are internal to the chip Device Layer. */ enum InternalPlatformSpecificEventTypes { @@ -50,8 +50,8 @@ enum InternalPlatformSpecificEventTypes kPlatformZephyrBleConnected, kPlatformZephyrBleDisconnected, kPlatformZephyrBleCCCWrite, - kPlatformZephyrBleRXWrite, - kPlatformZephyrBleTXComplete, + kPlatformZephyrBleC1WriteEvent, + kPlatformZephyrBleC2IndDoneEvent, kPlatformZephyrBleOutOfBuffersEvent, }; @@ -69,19 +69,20 @@ struct BleCCCWriteEventType uint16_t Value; }; -struct BleRXWriteEventType +struct BleC1WriteEventType { bt_conn * BtConn; ::chip::System::PacketBuffer * Data; }; -struct BleTXCompleteEventType +struct BleC2IndDoneEventType { bt_conn * BtConn; + uint8_t Result; }; /** - * Represents platform-specific event information for the BL602 platform. + * Represents platform-specific event information for BL602 platforms. */ struct ChipDevicePlatformEvent final { @@ -89,8 +90,8 @@ struct ChipDevicePlatformEvent final { BleConnEventType BleConnEvent; BleCCCWriteEventType BleCCCWriteEvent; - BleRXWriteEventType BleRXWriteEvent; - BleTXCompleteEventType BleTXCompleteEvent; + BleC1WriteEventType BleC1WriteEvent; + BleC2IndDoneEventType BleC2IndDoneEvent; }; }; diff --git a/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.cpp b/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.cpp index 99aef24aac007e..02d296c492abd9 100644 --- a/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.cpp +++ b/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.cpp @@ -165,5 +165,15 @@ void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState) } } +void ConnectivityManagerImpl::OnIPv6AddressAvailable() +{ + ChipLogProgress(DeviceLayer, "IPv6 addr available."); + + ChipDeviceEvent event; + event.Type = DeviceEventType::kInterfaceIpAddressChanged; + event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV6_Assigned; + PlatformMgr().PostEventOrDie(&event); +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.h b/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.h index 692a4ae16ed1f9..3efacc02c75043 100644 --- a/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.h +++ b/src/platform/bouffalolab/BL602/ConnectivityManagerImpl.h @@ -75,6 +75,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void WifiStationStateChange(void); void OnStationConnected(void); void ChangeWiFiStationState(WiFiStationState newState); + void OnIPv6AddressAvailable(); private: // ===== Members that implement the ConnectivityManager abstract interface. diff --git a/src/platform/bouffalolab/BL602/DnssdImpl.cpp b/src/platform/bouffalolab/BL602/DnssdImpl.cpp index 15e2c984c272e8..398bfbfa5caf65 100644 --- a/src/platform/bouffalolab/BL602/DnssdImpl.cpp +++ b/src/platform/bouffalolab/BL602/DnssdImpl.cpp @@ -224,18 +224,6 @@ int mdns_responder_ops(struct netif * netif) mdns.slot_idx++; mdns_resp_announce(netif); -#if 0 - // for ota - slot = - mdns_resp_add_service(netif, "MATTER OTA", "_ota", static_cast(glservice->mProtocol), 3333, 1000, ota_txt, NULL); - if (slot < 0) - { - mdns_resp_remove_netif(netif); - mdns_resp_deinit(); - log_info("ota mdns fail.\r\n"); - } -#endif - return slot; } diff --git a/src/platform/bouffalolab/BL602/PlatformManagerImpl.cpp b/src/platform/bouffalolab/BL602/PlatformManagerImpl.cpp index 930ff9cd292bdf..8524bc51f4a0a7 100644 --- a/src/platform/bouffalolab/BL602/PlatformManagerImpl.cpp +++ b/src/platform/bouffalolab/BL602/PlatformManagerImpl.cpp @@ -203,6 +203,11 @@ void OnWiFiPlatformEvent(input_event_t * event, void * private_data) WifiStaConnected(); } break; + case CODE_WIFI_ON_GOT_IP6: { + log_info("[APP] [EVT] GOT IP6 %lld\r\n", aos_now_ms()); + ConnectivityMgrImpl().OnIPv6AddressAvailable(); + } + break; default: { log_info("[APP] [EVT] Unknown code %u, %lld\r\n", event->code, aos_now_ms()); /*nothing*/ diff --git a/src/platform/bouffalolab/BL702/BLEManagerImpl.cpp b/src/platform/bouffalolab/BL702/BLEManagerImpl.cpp index 72be562a444a7c..2a90fdf646aaba 100644 --- a/src/platform/bouffalolab/BL702/BLEManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/BLEManagerImpl.cpp @@ -15,12 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the BLEManager singleton object - * for the BL702 platform. - */ - #define __ZEPHYR__ 1 #include @@ -32,6 +26,9 @@ #include #include #include +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +#include +#endif extern "C" { #include @@ -59,6 +56,10 @@ 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 bt_uuid_128 UUID128_CHIPoBLEChar_C3 = + BT_UUID_INIT_128(0x04, 0x8F, 0x21, 0x83, 0x8A, 0x74, 0x7D, 0xB8, 0xF2, 0x45, 0x72, 0x87, 0x38, 0x02, 0x63, 0x64); +#endif _bt_gatt_ccc CHIPoBLEChar_TX_CCC = BT_GATT_CCC_INITIALIZER(nullptr, BLEManagerImpl::HandleTXCCCWrite, nullptr); @@ -66,8 +67,12 @@ struct bt_gatt_attr sChipoBleAttributes[] = { BT_GATT_PRIMARY_SERVICE(&UUID16_CHIPoBLEService.uuid), BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_RX.uuid, BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE, nullptr, BLEManagerImpl::HandleRXWrite, nullptr), - BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_TX.uuid, BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_NONE, nullptr, nullptr, nullptr), - BT_GATT_CCC_MANAGED(&CHIPoBLEChar_TX_CCC, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE) + BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_TX.uuid, BT_GATT_CHRC_INDICATE, BT_GATT_PERM_NONE, nullptr, nullptr, nullptr), + BT_GATT_CCC_MANAGED(&CHIPoBLEChar_TX_CCC, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + BT_GATT_CHARACTERISTIC(&UUID128_CHIPoBLEChar_C3.uuid, BT_GATT_CHRC_READ, BT_GATT_PERM_READ, BLEManagerImpl::HandleC3Read, + nullptr, nullptr), +#endif }; struct bt_gatt_service sChipoBleService = { @@ -251,6 +256,10 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) chip::Encoding::LittleEndian::Put16(serviceData.uuid, UUID16_CHIPoBLEService.val); ReturnErrorOnFailure(ConfigurationMgr().GetBLEDeviceIdentificationInfo(serviceData.deviceIdInfo)); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + ReturnErrorOnFailure(PrepareC3CharData()); +#endif + if (!isAdvertisingRerun) { #if CONFIG_BT_PRIVACY @@ -410,7 +419,7 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event) mGAPConns--; - // If notifications were enabled for this connection, record that they are now disabled and + // If indications were enabled for this connection, record that they are now disabled and // notify the BLE Layer of a disconnect. if (UnsetSubscribed(connEvent->BtConn)) { @@ -456,8 +465,8 @@ CHIP_ERROR BLEManagerImpl::HandleTXCharCCCDWrite(const ChipDeviceEvent * event) ChipLogDetail(DeviceLayer, "ConnId: 0x%02x, New CCCD value: 0x%04x", bt_conn_index(writeEvent->BtConn), writeEvent->Value); - // If the client has requested to enable notifications and if it is not yet subscribed - if (writeEvent->Value != 0 && SetSubscribed(writeEvent->BtConn)) + // If the client has requested to enable indications and if it is not yet subscribed + if (writeEvent->Value == BT_GATT_CCC_INDICATE && SetSubscribed(writeEvent->BtConn)) { // Alert the BLE layer that CHIPoBLE "subscribe" has been received and increment the bt_conn reference counter. HandleSubscribeReceived(writeEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); @@ -487,30 +496,64 @@ CHIP_ERROR BLEManagerImpl::HandleTXCharCCCDWrite(const ChipDeviceEvent * event) CHIP_ERROR BLEManagerImpl::HandleRXCharWrite(const ChipDeviceEvent * event) { - const BleRXWriteEventType * writeEvent = &event->Platform.BleRXWriteEvent; + const BleC1WriteEventType * c1WriteEvent = &event->Platform.BleC1WriteEvent; - ChipLogDetail(DeviceLayer, "Write request received for CHIPoBLE RX (ConnId 0x%02x)", bt_conn_index(writeEvent->BtConn)); + ChipLogDetail(DeviceLayer, "Write request received for CHIPoBLE RX characteristic (ConnId 0x%02x)", + bt_conn_index(c1WriteEvent->BtConn)); - HandleWriteReceived(writeEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_RX, - PacketBufferHandle::Adopt(writeEvent->Data)); - bt_conn_unref(writeEvent->BtConn); + HandleWriteReceived(c1WriteEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_RX, + PacketBufferHandle::Adopt(c1WriteEvent->Data)); + bt_conn_unref(c1WriteEvent->BtConn); return CHIP_NO_ERROR; } CHIP_ERROR BLEManagerImpl::HandleTXCharComplete(const ChipDeviceEvent * event) { - const BleTXCompleteEventType * completeEvent = &event->Platform.BleTXCompleteEvent; + const BleC2IndDoneEventType * c2IndDoneEvent = &event->Platform.BleC2IndDoneEvent; - ChipLogDetail(DeviceLayer, "Notification for CHIPoBLE TX done (ConnId 0x%02x)", bt_conn_index(completeEvent->BtConn)); + ChipLogDetail(DeviceLayer, "Indication for CHIPoBLE TX characteristic done (ConnId 0x%02x, result 0x%02x)", + bt_conn_index(c2IndDoneEvent->BtConn), c2IndDoneEvent->Result); - // Signal the BLE Layer that the outstanding notification is complete. - HandleIndicationConfirmation(completeEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); - bt_conn_unref(completeEvent->BtConn); + // Signal the BLE Layer that the outstanding indication is complete. + HandleIndicationConfirmation(c2IndDoneEvent->BtConn, &CHIP_BLE_SVC_ID, &chipUUID_CHIPoBLEChar_TX); + bt_conn_unref(c2IndDoneEvent->BtConn); return CHIP_NO_ERROR; } +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +CHIP_ERROR BLEManagerImpl::PrepareC3CharData() +{ + CHIP_ERROR err = CHIP_NO_ERROR; + BitFlags additionalDataFields; + AdditionalDataPayloadGeneratorParams additionalDataPayloadParams; + +#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, c3CharDataBufferHandle, + additionalDataFields); + +exit: + if (err != CHIP_NO_ERROR) + { + ChipLogError(DeviceLayer, "Failed to generate TLV encoded Additional Data (%s)", __func__); + } + + return err; +} +#endif + void BLEManagerImpl::HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param) { BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising); @@ -535,29 +578,21 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) err = HandleTXCharCCCDWrite(event); break; - case DeviceEventType::kPlatformZephyrBleRXWrite: + case DeviceEventType::kPlatformZephyrBleC1WriteEvent: err = HandleRXCharWrite(event); break; - case DeviceEventType::kPlatformZephyrBleTXComplete: + case DeviceEventType::kPlatformZephyrBleC2IndDoneEvent: err = HandleTXCharComplete(event); break; - case DeviceEventType::kServiceProvisioningChange: - // Force the advertising state to be refreshed to reflect new provisioning state. - mFlags.Set(Flags::kAdvertisingRefreshNeeded); - - DriveBLEState(); - - break; - default: break; } if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %s", ErrorStr(err)); + ChipLogError(DeviceLayer, "Disabling CHIPoBLE service due to error: %" CHIP_ERROR_FORMAT, err.Format()); mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Disabled; PlatformMgr().ScheduleWork(DriveBLEState, 0); } @@ -594,29 +629,30 @@ bool BLEManagerImpl::UnsubscribeCharacteristic(BLE_CONNECTION_OBJECT conId, cons bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUUID * svcId, const ChipBleUUID * charId, PacketBufferHandle pBuf) { - CHIP_ERROR err = CHIP_NO_ERROR; - int status = 0; - uint8_t index = bt_conn_index((bt_conn *) conId); - bt_gatt_notify_params * params = &mNotifyParams[index]; + CHIP_ERROR err = CHIP_NO_ERROR; + int status = 0; + uint8_t index = bt_conn_index(conId); + bt_gatt_indicate_params * params = &mIndicateParams[index]; - VerifyOrExit(IsSubscribed((bt_conn *) conId) == true, err = CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrExit(IsSubscribed(conId) == true, err = CHIP_ERROR_INVALID_ARGUMENT); - ChipLogDetail(DeviceLayer, "Sending notification for CHIPoBLE TX (ConnId %02x, len %u)", index, pBuf->DataLength()); + ChipLogDetail(DeviceLayer, "Sending indication for CHIPoBLE TX characteristic (ConnId %02x, len %u)", index, + pBuf->DataLength()); - params->uuid = nullptr; - params->attr = &sChipoBleAttributes[kCHIPoBLE_CCC_AttributeIndex]; - params->data = pBuf->Start(); - params->len = pBuf->DataLength(); - params->func = HandleTXCompleted; - params->user_data = nullptr; + params->uuid = nullptr; + params->attr = &sChipoBleAttributes[kCHIPoBLE_CCC_AttributeIndex]; + params->func = HandleTXIndicated; + params->data = pBuf->Start(); + params->len = pBuf->DataLength(); - status = bt_gatt_notify_cb((bt_conn *) conId, params); + status = bt_gatt_indicate(conId, params); VerifyOrExit(status == 0, err = MapErrorZephyr(status)); exit: if (err != CHIP_NO_ERROR) { - ChipLogError(DeviceLayer, "Sending notification for CHIPoBLE TX failed: %s", ErrorStr(err)); + ChipLogError(DeviceLayer, "BLEManagerImpl::SendIndication() failed: %." CHIP_ERROR_FORMAT, err.Format()); + ChipLogError(DeviceLayer, "BLEManagerImpl::SendIndication() failed: %d", status); } return err == CHIP_NO_ERROR; @@ -692,9 +728,9 @@ ssize_t BLEManagerImpl::HandleRXWrite(struct bt_conn * conId, const struct bt_ga if (!packetBuf.IsNull()) { // Arrange to post a CHIPoBLERXWriteEvent event to the CHIP queue. - event.Type = DeviceEventType::kPlatformZephyrBleRXWrite; - event.Platform.BleRXWriteEvent.BtConn = bt_conn_ref(conId); - event.Platform.BleRXWriteEvent.Data = std::move(packetBuf).UnsafeRelease(); + event.Type = DeviceEventType::kPlatformZephyrBleC1WriteEvent; + event.Platform.BleC1WriteEvent.BtConn = bt_conn_ref(conId); + event.Platform.BleC1WriteEvent.Data = std::move(packetBuf).UnsafeRelease(); } // If we failed to allocate a buffer, post a kPlatformZephyrBleOutOfBuffersEvent event. @@ -712,9 +748,9 @@ bool BLEManagerImpl::HandleTXCCCWrite(struct bt_conn * conId, const struct bt_ga { ChipDeviceEvent event; - if (value != BT_GATT_CCC_NOTIFY && value != 0) + if (value != BT_GATT_CCC_INDICATE && value != 0) { - return false; + return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED); } event.Type = DeviceEventType::kPlatformZephyrBleCCCWrite; @@ -723,15 +759,16 @@ bool BLEManagerImpl::HandleTXCCCWrite(struct bt_conn * conId, const struct bt_ga PlatformMgr().PostEventOrDie(&event); - return true; + return sizeof(value); } -void BLEManagerImpl::HandleTXCompleted(struct bt_conn * conId, void * /* param */) +void BLEManagerImpl::HandleTXIndicated(struct bt_conn * conId, IndicationAttrType, uint8_t err) { ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformZephyrBleTXComplete; - event.Platform.BleTXCompleteEvent.BtConn = bt_conn_ref(conId); + event.Type = DeviceEventType::kPlatformZephyrBleC2IndDoneEvent; + event.Platform.BleC2IndDoneEvent.BtConn = bt_conn_ref(conId); + event.Platform.BleC2IndDoneEvent.Result = err; PlatformMgr().PostEventOrDie(&event); } @@ -742,15 +779,6 @@ void BLEManagerImpl::HandleConnect(struct bt_conn * conId, uint8_t err) PlatformMgr().LockChipStack(); - // bt_le_conn_param connparam = { - // .interval_min = (u16_t)(100 / 1.25), - // .interval_max = (u16_t)(4000 / 1.25), - // .latency = 0, - // .timeout = 100 - // }; - - // bt_conn_le_param_update(conId, &connparam); - // Don't handle BLE connecting events when it is not related to CHIPoBLE VerifyOrExit(sInstance.mFlags.Has(Flags::kChipoBleGattServiceRegister), ); @@ -782,6 +810,23 @@ void BLEManagerImpl::HandleDisconnect(struct bt_conn * conId, uint8_t reason) exit: PlatformMgr().UnlockChipStack(); } + +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING +ssize_t BLEManagerImpl::HandleC3Read(struct bt_conn * conId, const struct bt_gatt_attr * attr, void * buf, uint16_t len, + uint16_t offset) +{ + ChipLogDetail(DeviceLayer, "Read request received for CHIPoBLE C3 (ConnId 0x%02x)", bt_conn_index(conId)); + + if (sInstance.c3CharDataBufferHandle.IsNull()) + { + return 0; + } + + return bt_gatt_attr_read(conId, attr, buf, len, offset, sInstance.c3CharDataBufferHandle->Start(), + sInstance.c3CharDataBufferHandle->DataLength()); +} +#endif + } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/bouffalolab/BL702/BLEManagerImpl.h b/src/platform/bouffalolab/BL702/BLEManagerImpl.h index b87b812662f00a..0f9ff7666a1c1e 100644 --- a/src/platform/bouffalolab/BL702/BLEManagerImpl.h +++ b/src/platform/bouffalolab/BL702/BLEManagerImpl.h @@ -1,6 +1,6 @@ /* - * - * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the BLEManager object for the Bouffalolab - * BL702 platform. - */ - #pragma once #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE @@ -48,7 +42,9 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla private: // ===== Members that implement the BLEManager internal interface. - + using IndicationAttrType = + std::conditional_t::value, + bt_gatt_indicate_params *, const bt_gatt_attr *>; CHIP_ERROR _Init(void); void _Shutdown() {} bool _IsAdvertisingEnabled(void); @@ -99,8 +95,11 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla uint16_t mGAPConns; CHIPoBLEServiceMode mServiceMode; bool mSubscribedConns[CONFIG_BT_MAX_CONN]; - bt_gatt_notify_params mNotifyParams[CONFIG_BT_MAX_CONN]; + bt_gatt_indicate_params mIndicateParams[CONFIG_BT_MAX_CONN]; bt_conn_cb mConnCallbacks; +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + PacketBufferHandle c3CharDataBufferHandle; +#endif void DriveBLEState(void); CHIP_ERROR ConfigureAdvertising(void); @@ -111,6 +110,9 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla CHIP_ERROR HandleRXCharWrite(const ChipDeviceEvent * event); CHIP_ERROR HandleTXCharCCCDWrite(const ChipDeviceEvent * event); CHIP_ERROR HandleTXCharComplete(const ChipDeviceEvent * event); +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + CHIP_ERROR PrepareC3CharData(); +#endif bool IsSubscribed(bt_conn * conn); bool SetSubscribed(bt_conn * conn); bool UnsetSubscribed(bt_conn * conn); @@ -119,7 +121,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla static void DriveBLEState(intptr_t arg); // Below callbacks run from the system workqueue context and have a limited stack capacity. - static void HandleTXCompleted(bt_conn * conn, void * param); + static void HandleTXIndicated(bt_conn * conn, IndicationAttrType attr, uint8_t err); static void HandleConnect(bt_conn * conn, uint8_t err); static void HandleDisconnect(bt_conn * conn, uint8_t reason); static void HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param); @@ -136,6 +138,10 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla static ssize_t HandleRXWrite(bt_conn * conn, const bt_gatt_attr * attr, const void * buf, uint16_t len, uint16_t offset, uint8_t flags); static bool HandleTXCCCWrite(bt_conn * conn, const bt_gatt_attr * attr, uint16_t value); + +#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING + static ssize_t HandleC3Read(struct bt_conn * conn, const struct bt_gatt_attr * attr, void * buf, uint16_t len, uint16_t offset); +#endif }; /** diff --git a/src/platform/bouffalolab/BL702/BUILD.gn b/src/platform/bouffalolab/BL702/BUILD.gn index 86424f63d99e58..efd5564e963b43 100644 --- a/src/platform/bouffalolab/BL702/BUILD.gn +++ b/src/platform/bouffalolab/BL702/BUILD.gn @@ -1,4 +1,5 @@ -# Copyright (c) 2021 Project CHIP Authors +# Copyright (c) 2022 Project CHIP Authors +# All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/src/platform/bouffalolab/BL702/BlePlatformConfig.h b/src/platform/bouffalolab/BL702/BlePlatformConfig.h index 0116c5df59d7ca..8579667417a7c9 100644 --- a/src/platform/bouffalolab/BL702/BlePlatformConfig.h +++ b/src/platform/bouffalolab/BL702/BlePlatformConfig.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/platform/bouffalolab/BL702/CHIPDevicePlatformConfig.h b/src/platform/bouffalolab/BL702/CHIPDevicePlatformConfig.h index 9a686ff494b492..e54231f2198166 100644 --- a/src/platform/bouffalolab/BL702/CHIPDevicePlatformConfig.h +++ b/src/platform/bouffalolab/BL702/CHIPDevicePlatformConfig.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/platform/bouffalolab/BL702/CHIPDevicePlatformEvent.h b/src/platform/bouffalolab/BL702/CHIPDevicePlatformEvent.h index 8e1a89c5344a8e..d14d1f7b9bb2ac 100644 --- a/src/platform/bouffalolab/BL702/CHIPDevicePlatformEvent.h +++ b/src/platform/bouffalolab/BL702/CHIPDevicePlatformEvent.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,12 +39,12 @@ enum PublicPlatformSpecificEventTypes */ enum InternalPlatformSpecificEventTypes { - kPlatformZephyrEvent = chip::DeviceLayer::DeviceEventType::kRange_InternalPlatformSpecific, + kPlatformZephyrEvent = kRange_InternalPlatformSpecific, kPlatformZephyrBleConnected, kPlatformZephyrBleDisconnected, kPlatformZephyrBleCCCWrite, - kPlatformZephyrBleRXWrite, - kPlatformZephyrBleTXComplete, + kPlatformZephyrBleC1WriteEvent, + kPlatformZephyrBleC2IndDoneEvent, kPlatformZephyrBleOutOfBuffersEvent, }; @@ -63,15 +62,16 @@ struct BleCCCWriteEventType uint16_t Value; }; -struct BleRXWriteEventType +struct BleC1WriteEventType { bt_conn * BtConn; ::chip::System::PacketBuffer * Data; }; -struct BleTXCompleteEventType +struct BleC2IndDoneEventType { bt_conn * BtConn; + uint8_t Result; }; /** @@ -83,8 +83,8 @@ struct ChipDevicePlatformEvent final { BleConnEventType BleConnEvent; BleCCCWriteEventType BleCCCWriteEvent; - BleRXWriteEventType BleRXWriteEvent; - BleTXCompleteEventType BleTXCompleteEvent; + BleC1WriteEventType BleC1WriteEvent; + BleC2IndDoneEventType BleC2IndDoneEvent; }; }; diff --git a/src/platform/bouffalolab/BL702/CHIPPlatformConfig.h b/src/platform/bouffalolab/BL702/CHIPPlatformConfig.h index c31bb5521a1349..8187b71999d43d 100644 --- a/src/platform/bouffalolab/BL702/CHIPPlatformConfig.h +++ b/src/platform/bouffalolab/BL702/CHIPPlatformConfig.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +15,6 @@ * limitations under the License. */ -/** - * @file - * Platform-specific configuration overrides for CHIP on - * the Texas Instruments CC1352 platform. - * - * NOTE: currently a bare-bones implementation to allow for building. - */ - #pragma once #include diff --git a/src/platform/bouffalolab/BL702/CHIPmemory.cpp b/src/platform/bouffalolab/BL702/CHIPmemory.cpp index 069fe29ea0bdc9..5438016ad53d0f 100644 --- a/src/platform/bouffalolab/BL702/CHIPmemory.cpp +++ b/src/platform/bouffalolab/BL702/CHIPmemory.cpp @@ -1,6 +1,5 @@ /* - * - * Copyright (c) 2020-2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,13 +15,6 @@ * limitations under the License. */ -/** - * @file - * This file implements heap memory allocation APIs for CHIP. These functions are platform - * specific and might be C Standard Library heap functions re-direction in most of cases. - * - */ - #include #include diff --git a/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.cpp b/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.cpp index 11a5dbe4ca25e7..a6be2c40158d58 100644 --- a/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.cpp @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides the implementation of the Device Layer ConfigurationManager object - * for the Bouffalolab BL702 platform. - * - */ - -/* this file behaves like a config.h, comes first */ #include #include diff --git a/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.h b/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.h index 1d9693b891e5b8..db7668bf0f5eab 100644 --- a/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.h +++ b/src/platform/bouffalolab/BL702/ConfigurationManagerImpl.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the ConfigurationManager object - * for the Bouffalolab bl702 platform. - */ - #pragma once #include diff --git a/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.cpp b/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.cpp index f3690a6c713545..cf1585a44a0e53 100644 --- a/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.cpp @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/** - * @file - * Provides an implementation of the ConfigurationManager object - * for the Bouffalolab bl702 platform. - */ -/* this file behaves like a config.h, comes first */ #include #include diff --git a/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.h b/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.h index b5f8f5e5cbcca5..06774161b8c3f2 100644 --- a/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.h +++ b/src/platform/bouffalolab/BL702/ConnectivityManagerImpl.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/** - * @file - * Platform-specific connectivity manager class for the Texas - * Instruments BL702 platform. - */ #pragma once diff --git a/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.cpp b/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.cpp index 6e873b4ed43c92..c96218933aff2c 100644 --- a/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.cpp +++ b/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.cpp @@ -1,6 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.h b/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.h index 44d8d1a5397b6b..d81c3c9821db8a 100644 --- a/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.h +++ b/src/platform/bouffalolab/BL702/DeviceNetworkProvisioningDelegateImpl.h @@ -1,6 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.cpp b/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.cpp index 3a591ef88117b7..90da3db7fab7da 100644 --- a/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.cpp +++ b/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.cpp @@ -1,6 +1,6 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the DiagnosticDataProvider object - * for BL702 platform. - */ - #include #include diff --git a/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.h b/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.h index ba2d4b16389be9..6411b8e4cbd501 100644 --- a/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.h +++ b/src/platform/bouffalolab/BL702/DiagnosticDataProviderImpl.h @@ -1,6 +1,6 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the DiagnosticDataProvider object. - */ - #pragma once #include diff --git a/src/platform/bouffalolab/BL702/InetPlatformConfig.h b/src/platform/bouffalolab/BL702/InetPlatformConfig.h index 1edbefcd784d4d..088999420a6aae 100644 --- a/src/platform/bouffalolab/BL702/InetPlatformConfig.h +++ b/src/platform/bouffalolab/BL702/InetPlatformConfig.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2019 Google LLC. + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +15,6 @@ * limitations under the License. */ -/** - * @file - * Platform-specific configuration overrides for the Openchip Inet - * Layer on BL702 platforms using the Bouffalolab IOT SDK. - * - */ - #pragma once // ==================== Platform Adaptations ==================== diff --git a/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.cpp b/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.cpp index 0231d6f823874c..929cfbace3668a 100644 --- a/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.cpp @@ -1,6 +1,5 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,11 +15,6 @@ * limitations under the License. */ -/** - * @file - * Platform-specific key value storage implementation for Bouffalolab BL702 - */ - /* this file behaves like a config.h, comes first */ #include diff --git a/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.h b/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.h index 0ddcf4aad0974d..65a9bfde758766 100644 --- a/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.h +++ b/src/platform/bouffalolab/BL702/KeyValueStoreManagerImpl.h @@ -1,6 +1,5 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,11 +15,6 @@ * limitations under the License. */ -/** - * @file - * Platform-specific key value storage implementation for Bouffalolab BL702 - */ - #pragma once namespace chip { diff --git a/src/platform/bouffalolab/BL702/Logging.cpp b/src/platform/bouffalolab/BL702/Logging.cpp index 58cd191453cc88..24193ea434ae28 100644 --- a/src/platform/bouffalolab/BL702/Logging.cpp +++ b/src/platform/bouffalolab/BL702/Logging.cpp @@ -1,4 +1,19 @@ -/* See Project CHIP LICENSE file for licensing information. */ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include diff --git a/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.cpp b/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.cpp index 5d014eea06216d..c59e34d366cfc2 100644 --- a/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.cpp +++ b/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.cpp @@ -1,6 +1,5 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.h b/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.h index 6ecf3e43c778c4..4bae9152f8f48c 100644 --- a/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.h +++ b/src/platform/bouffalolab/BL702/OTAImageProcessorImpl.h @@ -1,6 +1,5 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/src/platform/bouffalolab/BL702/PlatformManagerImpl.cpp b/src/platform/bouffalolab/BL702/PlatformManagerImpl.cpp index 65d61307109a36..c76b6b688f8a0f 100644 --- a/src/platform/bouffalolab/BL702/PlatformManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/PlatformManagerImpl.cpp @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the PlatformManager object - * for the Bouffalolab BL702 platform. - */ - /* this file behaves like a config.h, comes first */ #include diff --git a/src/platform/bouffalolab/BL702/PlatformManagerImpl.h b/src/platform/bouffalolab/BL702/PlatformManagerImpl.h index 3cd7c4e2962861..31cc14ebc97168 100644 --- a/src/platform/bouffalolab/BL702/PlatformManagerImpl.h +++ b/src/platform/bouffalolab/BL702/PlatformManagerImpl.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the PlatformManager object. - */ - #pragma once #include diff --git a/src/platform/bouffalolab/BL702/SystemPlatformConfig.h b/src/platform/bouffalolab/BL702/SystemPlatformConfig.h index 7de2e8f6379fb0..f3f843c0d55c99 100644 --- a/src/platform/bouffalolab/BL702/SystemPlatformConfig.h +++ b/src/platform/bouffalolab/BL702/SystemPlatformConfig.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2019 Google LLC. + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +15,6 @@ * limitations under the License. */ -/** - * @file - * Platform-specific configuration overrides for the CHIP System - * Layer - * - */ - #pragma once #include diff --git a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp index f165112facc91f..73a96a90a6e853 100644 --- a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp +++ b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.cpp @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2019 Nest Labs, Inc. + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the ThreadStackManager object for - * BL702 platforms using the Bouffalolab SDK and the OpenThread - * stack. - * - */ -/* this file behaves like a config.h, comes first */ #include #include diff --git a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.h b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.h index cc88caeeefbff2..61a9b72e3b6334 100644 --- a/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.h +++ b/src/platform/bouffalolab/BL702/ThreadStackManagerImpl.h @@ -1,7 +1,6 @@ /* - * - * Copyright (c) 2020 Project CHIP Authors - * Copyright (c) 2020 Texas Instruments Incorporated + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +15,6 @@ * limitations under the License. */ -/** - * @file - * Provides an implementation of the ThreadStackManager object - * for the Bouffalolab BL702 platform. - * - * NOTE: currently a bare-bones implementation to allow for building. - */ - #pragma once #include diff --git a/src/platform/bouffalolab/BL702/bl702Config.cpp b/src/platform/bouffalolab/BL702/bl702Config.cpp index 6cae8a52b3fea9..0c51dd6d9afbbc 100644 --- a/src/platform/bouffalolab/BL702/bl702Config.cpp +++ b/src/platform/bouffalolab/BL702/bl702Config.cpp @@ -1,8 +1,5 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors - * Copyright (c) 2019-2020 Google LLC. - * Copyright (c) 2019 Nest Labs, Inc. + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,11 +15,6 @@ * limitations under the License. */ -/** - * @file - * Utilities for interacting with the the BL702 Easyflash module. - */ -/* this file behaves like a config.h, comes first */ #include #include diff --git a/src/platform/bouffalolab/BL702/bl702Config.h b/src/platform/bouffalolab/BL702/bl702Config.h index 46ad99de297f89..967dfcdfcf8aa7 100644 --- a/src/platform/bouffalolab/BL702/bl702Config.h +++ b/src/platform/bouffalolab/BL702/bl702Config.h @@ -1,8 +1,5 @@ /* - * - * Copyright (c) 2021 Project CHIP Authors - * Copyright (c) 2019-2020 Google LLC. - * Copyright (c) 2019 Nest Labs, Inc. + * Copyright (c) 2022 Project CHIP Authors * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,11 +15,6 @@ * limitations under the License. */ -/** - * @file - * Utilities for interacting with the the BL702 Easyflash module. - */ - #pragma once #include diff --git a/third_party/bouffalolab/bl602/bl602_sdk.gni b/third_party/bouffalolab/bl602/bl602_sdk.gni index ce3ca73a90a7c7..5c9c2b370eb39d 100644 --- a/third_party/bouffalolab/bl602/bl602_sdk.gni +++ b/third_party/bouffalolab/bl602/bl602_sdk.gni @@ -60,6 +60,8 @@ template("bl602_sdk") { "${bl602_sdk_root}/components/network/lwip/src/include/lwip/apps", "${bl602_sdk_root}/components/network/lwip/lwip-port", "${bl602_sdk_root}/components/network/lwip/lwip-port/FreeRTOS", + "${bl602_sdk_root}/components/network/lwip/lwip-port/hook", + "${bl602_sdk_root}/components/network/lwip_mdns", #"${bl602_sdk_root}/components/network/lwip_mdns", @@ -250,6 +252,9 @@ template("bl602_sdk") { "OS_USING_FREERTOS", "BL602_MATTER_SUPPORT", "TD_DIAGNOSIS_STA", + "CONFIG_ENABLE_IPV6_ADDR_CALLBACK", + "CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT", + "CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT", ] if (bl602_board == "BL-HWC-G1") { diff --git a/third_party/bouffalolab/repo b/third_party/bouffalolab/repo index 880fc351904db2..9563086f233769 160000 --- a/third_party/bouffalolab/repo +++ b/third_party/bouffalolab/repo @@ -1 +1 @@ -Subproject commit 880fc351904db2825d3a2c767d60fe1ae8cc5722 +Subproject commit 9563086f233769db0163163307272f739d969cca