diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 903abc44..059e70f1 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -12,6 +12,9 @@ on: - ".github/workflows/compile-examples.yml" - "examples/**" - "src/**" + schedule: + # Run every Tuesday at 8 AM UTC to catch breakage caused by changes to external resources (libraries, platforms). + - cron: "0 8 * * TUE" workflow_dispatch: jobs: diff --git a/README.md b/README.md index 94d95daa..8369a4e8 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,17 @@ The user can include the file `app_conf_custom.h` to customize the BLE applicati Options wrapped in `#ifndef`, `#endif` in `app_conf_default.h` can be overwritten. Additional options can be added. +The user can refer to [AN5270](https://www.st.com/resource/en/application_note/an5270-introduction-to-stm32wb-bluetooth-low-energy-wireless-interface-stmicroelectronics.pdf) + +##### Examples + +The user can change the Tx Power by redefining `CFG_TX_POWER` using the [`build_opt.h`](https://github.com/stm32duino/Arduino_Core_STM32/wiki/Customize-build-options-using-build_opt.h) file. Possible values are listed in the chapter +**4.2 Tx power level**, default value is `0x18`(`-0.15dBm`). To set it at `+1dBm`, `CFG_TX_POWER` have to be defined at `0x1A`: + +``` +-DCFG_TX_POWER=0x1A +``` + ### Shield The user can include the file `ble_spi_conf.h` to define which shield and configuration to use from the following list: @@ -111,18 +122,17 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ``` - -B-L475E-IOT01A: https://www.st.com/en/evaluation-tools/b-l475e-iot01a.html -B-L4S5I-IOT01A: https://www.st.com/en/evaluation-tools/b-l4s5i-iot01a.html -NUCLEO-F401RE: https://www.st.com/en/evaluation-tools/nucleo-f401re.html -NUCLEO-L053R8: https://www.st.com/en/evaluation-tools/nucleo-l053r8.html -NUCLEO-L476RG: https://www.st.com/en/evaluation-tools/nucleo-l476rg.html -NUCLEO-WB15CC: https://www.st.com/en/evaluation-tools/nucleo-wb15cc.html -P-NUCLEO-WB55RG: https://www.st.com/en/evaluation-tools/p-nucleo-wb55.html -STEVAL-MKSBOX1V1: https://www.st.com/en/evaluation-tools/steval-mksbox1v1.html -STEVAL-MKBOXPRO: https://www.st.com/en/evaluation-tools/steval-mkboxpro.html -STM32L562E-DK: https://www.st.com/en/evaluation-tools/stm32l562e-dk.html -STM32WB5MM-DK: https://www.st.com/en/evaluation-tools/stm32wb5mm-dk.html -X-NUCLEO-BNRG2A1: https://www.st.com/en/ecosystems/x-nucleo-bnrg2a1.html -X-NUCLEO-IDB05A2: https://www.st.com/en/ecosystems/x-nucleo-idb05a2.html -X-NUCLEO-IDB05A1: https://www.st.com/en/ecosystems/x-nucleo-idb05a1.html +[B-L475E-IOT01A]: https://www.st.com/en/evaluation-tools/b-l475e-iot01a.html +[B-L4S5I-IOT01A]: https://www.st.com/en/evaluation-tools/b-l4s5i-iot01a.html +[NUCLEO-F401RE]: https://www.st.com/en/evaluation-tools/nucleo-f401re.html +[NUCLEO-L053R8]: https://www.st.com/en/evaluation-tools/nucleo-l053r8.html +[NUCLEO-L476RG]: https://www.st.com/en/evaluation-tools/nucleo-l476rg.html +[NUCLEO-WB15CC]: https://www.st.com/en/evaluation-tools/nucleo-wb15cc.html +[P-NUCLEO-WB55RG]: https://www.st.com/en/evaluation-tools/p-nucleo-wb55.html +[STEVAL-MKSBOX1V1]: https://www.st.com/en/evaluation-tools/steval-mksbox1v1.html +[STEVAL-MKBOXPRO]: https://www.st.com/en/evaluation-tools/steval-mkboxpro.html +[STM32L562E-DK]: https://www.st.com/en/evaluation-tools/stm32l562e-dk.html +[STM32WB5MM-DK]: https://www.st.com/en/evaluation-tools/stm32wb5mm-dk.html +[X-NUCLEO-BNRG2A1]: https://www.st.com/en/ecosystems/x-nucleo-bnrg2a1.html +[X-NUCLEO-IDB05A2]: https://www.st.com/en/ecosystems/x-nucleo-idb05a2.html +[X-NUCLEO-IDB05A1]: https://www.st.com/en/ecosystems/x-nucleo-idb05a1.html diff --git a/extras/STM32_WPAN/0001-chore-adapt-STM32_WPAN-sources.patch b/extras/STM32_WPAN/0001-chore-adapt-STM32_WPAN-sources.patch index a3f62011..0db755ba 100644 --- a/extras/STM32_WPAN/0001-chore-adapt-STM32_WPAN-sources.patch +++ b/extras/STM32_WPAN/0001-chore-adapt-STM32_WPAN-sources.patch @@ -1,7 +1,7 @@ -From 7f99c89e8a6f834daf4a76bf98307e9ebcd01c91 Mon Sep 17 00:00:00 2001 +From dca5f9d1a4246cd158d38e1eb140bb40513618fc Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 10 Jan 2024 18:16:01 +0100 -Subject: [PATCH 1/4] chore: adapt STM32_WPAN sources +Subject: [PATCH 1/6] chore: adapt STM32_WPAN sources Signed-off-by: Frederic Pillon --- @@ -15,7 +15,7 @@ Signed-off-by: Frederic Pillon 7 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/utility/STM32_WPAN/app_conf_default.h b/src/utility/STM32_WPAN/app_conf_default.h -index 71fc107..bf2274a 100644 +index 8356f0d..8db164d 100644 --- a/src/utility/STM32_WPAN/app_conf_default.h +++ b/src/utility/STM32_WPAN/app_conf_default.h @@ -1,9 +1,9 @@ @@ -90,7 +90,7 @@ index 71fc107..bf2274a 100644 /** * BLE stack Options_extension flags to be configured with: -@@ -292,7 +315,11 @@ +@@ -299,7 +322,11 @@ * This parameter is considered by the CPU2 when CFG_BLE_OPTIONS has SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV flag set */ @@ -103,7 +103,7 @@ index 71fc107..bf2274a 100644 /* Maximum advertising data length (in bytes) * Range: 31 .. 1650 with limitation: -@@ -301,7 +328,11 @@ +@@ -308,7 +335,11 @@ * This parameter is considered by the CPU2 when CFG_BLE_OPTIONS has SHCI_C2_BLE_INIT_OPTIONS_EXT_ADV flag set */ @@ -116,7 +116,7 @@ index 71fc107..bf2274a 100644 /* RF TX Path Compensation Value (16-bit signed integer). Units: 0.1 dB. * Range: -1280 .. 1280 -@@ -324,6 +355,7 @@ +@@ -331,6 +362,7 @@ #define CFG_BLE_CORE_VERSION (SHCI_C2_BLE_INIT_BLE_CORE_5_4) @@ -124,7 +124,7 @@ index 71fc107..bf2274a 100644 /****************************************************************************** * Transport Layer ******************************************************************************/ -@@ -660,4 +692,5 @@ typedef enum +@@ -667,4 +699,5 @@ typedef enum #define CFG_OTP_END_ADRESS OTP_AREA_END_ADDR @@ -267,7 +267,7 @@ index 4c92864..df6c215 100644 } +#endif /* STM32WBxx */ diff --git a/src/utility/STM32_WPAN/tl_mbox.c b/src/utility/STM32_WPAN/tl_mbox.c -index df07a19..9a2a297 100644 +index c13f098..e2a0400 100644 --- a/src/utility/STM32_WPAN/tl_mbox.c +++ b/src/utility/STM32_WPAN/tl_mbox.c @@ -16,6 +16,7 @@ @@ -278,7 +278,7 @@ index df07a19..9a2a297 100644 /* Includes ------------------------------------------------------------------*/ #include "stm32_wpan_common.h" #include "hw.h" -@@ -51,8 +52,10 @@ PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static MB_BleLldTable_t TL_BleLldTable; +@@ -53,8 +54,10 @@ PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static MB_BleLldTable_t TL_BleLldTable; PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static MB_SysTable_t TL_SysTable; PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static MB_MemManagerTable_t TL_MemManagerTable; PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static MB_TracesTable_t TL_TracesTable; @@ -289,7 +289,7 @@ index df07a19..9a2a297 100644 /**< tables */ PLACE_IN_SECTION("MB_MEM1") ALIGN(4) static tListNode FreeBufQueue; -@@ -97,8 +100,10 @@ void TL_Init( void ) +@@ -99,8 +102,10 @@ void TL_Init( void ) TL_RefTable.p_sys_table = &TL_SysTable; TL_RefTable.p_mem_manager_table = &TL_MemManagerTable; TL_RefTable.p_traces_table = &TL_TracesTable; @@ -300,7 +300,7 @@ index df07a19..9a2a297 100644 HW_IPCC_Init(); return; -@@ -847,3 +852,4 @@ static void OutputDbgTrace(TL_MB_PacketType_t packet_type, uint8_t* buffer) +@@ -874,3 +879,4 @@ static void OutputDbgTrace(TL_MB_PacketType_t packet_type, uint8_t* buffer) return; } diff --git a/extras/STM32_WPAN/0002-fix-include-a-timeout-when-waiting-for-the-cmd_resp.patch b/extras/STM32_WPAN/0002-fix-include-a-timeout-when-waiting-for-the-cmd_resp.patch index bd6f66a2..6de2c60f 100644 --- a/extras/STM32_WPAN/0002-fix-include-a-timeout-when-waiting-for-the-cmd_resp.patch +++ b/extras/STM32_WPAN/0002-fix-include-a-timeout-when-waiting-for-the-cmd_resp.patch @@ -1,7 +1,7 @@ -From a33328182e334e1ddedd368a047d75cf1662e330 Mon Sep 17 00:00:00 2001 +From 1af1d327b5532597512216349abb0d1edef1d89e Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Thu, 13 Jul 2023 17:16:40 +0200 -Subject: [PATCH 2/4] fix: include a timeout when waiting for the cmd_resp +Subject: [PATCH 2/6] fix: include a timeout when waiting for the cmd_resp Signed-off-by: Frederic Pillon --- diff --git a/extras/STM32_WPAN/0003-chore-add-support-for-customize-app_conf_default.h.patch b/extras/STM32_WPAN/0003-chore-add-support-for-customize-app_conf_default.h.patch index 3c5e66ae..79a1fc83 100644 --- a/extras/STM32_WPAN/0003-chore-add-support-for-customize-app_conf_default.h.patch +++ b/extras/STM32_WPAN/0003-chore-add-support-for-customize-app_conf_default.h.patch @@ -1,7 +1,7 @@ -From a973b405bf34a93b0c300c8bbc4aa5d59fa182e5 Mon Sep 17 00:00:00 2001 +From 4a42f3ebb91d4065d2ed54a80baab22d6feb5bad Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Wed, 10 Jan 2024 18:45:17 +0100 -Subject: [PATCH 3/4] chore: add support for customize app_conf_default.h +Subject: [PATCH 3/6] chore: add support for customize app_conf_default.h Signed-off-by: Frederic Pillon --- @@ -9,7 +9,7 @@ Signed-off-by: Frederic Pillon 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/src/utility/STM32_WPAN/app_conf_default.h b/src/utility/STM32_WPAN/app_conf_default.h -index bf2274a..ff2dc01 100644 +index 8db164d..2fd67c7 100644 --- a/src/utility/STM32_WPAN/app_conf_default.h +++ b/src/utility/STM32_WPAN/app_conf_default.h @@ -50,7 +50,9 @@ diff --git a/extras/STM32_WPAN/0004-fix-TL_Evt_t-payload-size-for-reset.patch b/extras/STM32_WPAN/0004-fix-TL_Evt_t-payload-size-for-reset.patch index 110a8410..4f85ea78 100644 --- a/extras/STM32_WPAN/0004-fix-TL_Evt_t-payload-size-for-reset.patch +++ b/extras/STM32_WPAN/0004-fix-TL_Evt_t-payload-size-for-reset.patch @@ -1,7 +1,7 @@ -From 324eef795bfd0a754aae4d5f9b528d4c8ad706c8 Mon Sep 17 00:00:00 2001 +From 8da8c8da4c1f62efc3f49690e8bb42c52c4ffc64 Mon Sep 17 00:00:00 2001 From: Frederic Pillon Date: Mon, 24 Jul 2023 10:55:20 +0200 -Subject: [PATCH 4/4] fix: TL_Evt_t payload size for reset +Subject: [PATCH 4/6] fix: TL_Evt_t payload size for reset Within STM32CubeWB v1.17.0 update TL_Evt_t payload size was reduced. This produce a warning -Warray-bounds due to the reset management diff --git a/extras/test/src/test_characteristic/test_permissions.cpp b/extras/test/src/test_characteristic/test_permissions.cpp new file mode 100644 index 00000000..cd658580 --- /dev/null +++ b/extras/test/src/test_characteristic/test_permissions.cpp @@ -0,0 +1,126 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#define private public +#define protected public + +#include "FakeBLELocalDevice.h" +#include "BLEAdvertisingData.h" +#include "BLETypedCharacteristics.h" +#include "BLELocalCharacteristic.h" +#include "BLEStringCharacteristic.h" +#include "BLEProperty.h" +#include + +int property[] = { + BLEBroadcast, + BLERead, + BLEWriteWithoutResponse, + BLEWrite, + BLENotify, + BLEIndicate, + BLEAuthSignedWrite, + BLEExtProp, + BLERead | BLEWrite | BLENotify +}; + +int permission[] = { + BLEEncryption, + BLEAuthentication, + BLEAuthorization, + BLEEncryption | BLEAuthentication +}; + +const char uuid[][31] = { + "1 Bool", + "2 Char", + "3 UnsignedChar", + "4 Byte", + "5 Short", + "6 UnsignedShort", + "7 Word", + "8 Int", + "9 UnsignedInt", + "A Long", + "B UnsignedLong", + "C Float", + "D Double", + "E String" +}; + +std::unique_ptr createCharacteristic(const char* uuid, unsigned int properties) +{ + switch(uuid[0]) + { + case '1': + return std::unique_ptr(new BLEBoolCharacteristic(uuid, properties)); + case '2': + return std::unique_ptr(new BLECharCharacteristic(uuid, properties)); + case '3': + return std::unique_ptr(new BLEUnsignedCharCharacteristic(uuid, properties)); + case '4': + return std::unique_ptr(new BLEByteCharacteristic(uuid, properties)); + case '5': + return std::unique_ptr(new BLEShortCharacteristic(uuid, properties)); + case '6': + return std::unique_ptr(new BLEUnsignedShortCharacteristic(uuid, properties)); + case '7': + return std::unique_ptr(new BLEWordCharacteristic(uuid, properties)); + case '8': + return std::unique_ptr(new BLEIntCharacteristic(uuid, properties)); + case '9': + return std::unique_ptr(new BLEUnsignedIntCharacteristic(uuid, properties)); + case 'A': + return std::unique_ptr(new BLELongCharacteristic(uuid, properties)); + case 'B': + return std::unique_ptr(new BLEUnsignedLongCharacteristic(uuid, properties)); + case 'C': + return std::unique_ptr(new BLEFloatCharacteristic(uuid, properties)); + case 'D': + return std::unique_ptr(new BLEDoubleCharacteristic(uuid, properties)); + case 'E': + return std::unique_ptr(new BLEStringCharacteristic(uuid, properties, 2)); + default: + break; + } + return nullptr; +} + +TEST_CASE("Test characteristic properties and permissions", "[ArduinoBLE::BLECharacteristic]") +{ + WHEN("Create a characteristic") + { + for(int i = 0; i < sizeof(property)/sizeof(int); i++) + { + for(int j = 0; j < sizeof(permission)/sizeof(int); j++) + { + for(int k = 0; k < 14; k++) + { + std::unique_ptr ptr = createCharacteristic(uuid[k], property[i] | permission[j]); + REQUIRE(ptr != nullptr); + REQUIRE(ptr->properties() == (property[i])); + BLELocalCharacteristic * local = ptr->local(); + REQUIRE(local->permissions() == (permission[j] >> 8)); + } + } + } + } +} diff --git a/extras/test/src/test_characteristic/test_writeValue.cpp b/extras/test/src/test_characteristic/test_writeValue.cpp new file mode 100644 index 00000000..6c7e1cd1 --- /dev/null +++ b/extras/test/src/test_characteristic/test_writeValue.cpp @@ -0,0 +1,389 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#define private public +#define protected public + +#include "FakeBLELocalDevice.h" +#include "BLEAdvertisingData.h" +#include "BLETypedCharacteristics.h" +#include "BLELocalCharacteristic.h" +#include "BLEStringCharacteristic.h" +#include "BLEProperty.h" + + +TEST_CASE("Test characteristic writeValue", "[ArduinoBLE::BLECharacteristic]") +{ + WHEN("Create a bool characteristic") + { + BLEBoolCharacteristic boolCharacteristic("Bool", BLEBroadcast| BLEIndicate | BLENotify ); + bool v = false;; + int written = boolCharacteristic.writeValue(v); + REQUIRE( written == sizeof(bool) ); + + boolCharacteristic.broadcast(); + written = boolCharacteristic.writeValue(v); + REQUIRE( written == sizeof(bool) ); + + BLEDevice device; + boolCharacteristic.local()->writeCccdValue(device, 0x002); + written = boolCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + boolCharacteristic.local()->writeCccdValue(device, 0x001); + written = boolCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a boolean characteristic") + { + BLEBooleanCharacteristic booleanCharacteristic("Boolean", BLEBroadcast| BLEIndicate | BLENotify); + bool v = false; + int written = booleanCharacteristic.writeValue(v); + REQUIRE( written == sizeof(bool) ); + + booleanCharacteristic.broadcast(); + written = booleanCharacteristic.writeValue(v); + REQUIRE( written == sizeof(bool) ); + + BLEDevice device; + booleanCharacteristic.local()->writeCccdValue(device, 0x002); + written = booleanCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + booleanCharacteristic.local()->writeCccdValue(device, 0x001); + written = booleanCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a char characteristic") + { + BLECharCharacteristic charCharacteristic("Char", BLEBroadcast| BLEIndicate | BLENotify); + char v = 'a'; + int written = charCharacteristic.writeValue(v); + REQUIRE( written == sizeof(char) ); + + charCharacteristic.broadcast(); + written = charCharacteristic.writeValue(v); + REQUIRE( written == sizeof(char) ); + + BLEDevice device; + charCharacteristic.local()->writeCccdValue(device, 0x002); + written = charCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + charCharacteristic.local()->writeCccdValue(device, 0x001); + written = charCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a unsigned char characteristic") + { + BLEUnsignedCharCharacteristic unsignedCharCharacteristic("UnsignedChar", BLEBroadcast| BLEIndicate | BLENotify); + unsigned char v = 0x01; + int written = unsignedCharCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned char) ); + + unsignedCharCharacteristic.broadcast(); + written = unsignedCharCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned char) ); + + BLEDevice device; + unsignedCharCharacteristic.local()->writeCccdValue(device, 0x002); + written = unsignedCharCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + unsignedCharCharacteristic.local()->writeCccdValue(device, 0x001); + written = unsignedCharCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a byte characteristic") + { + BLEByteCharacteristic byteCharacteristic("Byte", BLEBroadcast| BLEIndicate | BLENotify); + byte v = 0x01; + int written = byteCharacteristic.writeValue(v); + REQUIRE( written == sizeof(byte) ); + + byteCharacteristic.broadcast(); + written = byteCharacteristic.writeValue(v); + REQUIRE( written == sizeof(byte) ); + + BLEDevice device; + byteCharacteristic.local()->writeCccdValue(device, 0x002); + written = byteCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + byteCharacteristic.local()->writeCccdValue(device, 0x001); + written = byteCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a short characteristic") + { + BLEShortCharacteristic shortCharacteristic("Short", BLEBroadcast| BLEIndicate | BLENotify); + short v = -1; + int written = shortCharacteristic.writeValue(v); + REQUIRE( written == sizeof(short) ); + + shortCharacteristic.broadcast(); + written = shortCharacteristic.writeValue(v); + REQUIRE( written == sizeof(short) ); + + BLEDevice device; + shortCharacteristic.local()->writeCccdValue(device, 0x002); + written = shortCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + shortCharacteristic.local()->writeCccdValue(device, 0x001); + written = shortCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a unsigned short characteristic") + { + BLEUnsignedShortCharacteristic unsignedShortCharacteristic("UnsignedShort", BLEBroadcast| BLEIndicate | BLENotify); + unsigned short v = 1; + int written = unsignedShortCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned short) ); + + unsignedShortCharacteristic.broadcast(); + written = unsignedShortCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned short) ); + + BLEDevice device; + unsignedShortCharacteristic.local()->writeCccdValue(device, 0x002); + written = unsignedShortCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + unsignedShortCharacteristic.local()->writeCccdValue(device, 0x001); + written = unsignedShortCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a word characteristic") + { + BLEWordCharacteristic wordCharacteristic("Word", BLEBroadcast| BLEIndicate | BLENotify); + word v = -1; + int written = wordCharacteristic.writeValue(v); + REQUIRE( written == sizeof(word) ); + + wordCharacteristic.broadcast(); + written = wordCharacteristic.writeValue(v); + REQUIRE( written == sizeof(word) ); + + BLEDevice device; + wordCharacteristic.local()->writeCccdValue(device, 0x002); + written = wordCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + wordCharacteristic.local()->writeCccdValue(device, 0x001); + written = wordCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a int characteristic") + { + BLEIntCharacteristic intCharacteristic("Int", BLEBroadcast| BLEIndicate | BLENotify); + int v = -1; + int written = intCharacteristic.writeValue(v); + REQUIRE( written == sizeof(int) ); + + intCharacteristic.broadcast(); + written = intCharacteristic.writeValue(v); + REQUIRE( written == sizeof(int) ); + + BLEDevice device; + intCharacteristic.local()->writeCccdValue(device, 0x002); + written = intCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + intCharacteristic.local()->writeCccdValue(device, 0x001); + written = intCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a unsigned int characteristic") + { + BLEUnsignedIntCharacteristic unsignedIntCharacteristic("UnsignedInt", BLEBroadcast| BLEIndicate | BLENotify); + unsigned int v = 1; + int written = unsignedIntCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned int) ); + + unsignedIntCharacteristic.broadcast(); + written = unsignedIntCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned int) ); + + BLEDevice device; + unsignedIntCharacteristic.local()->writeCccdValue(device, 0x002); + written = unsignedIntCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + unsignedIntCharacteristic.local()->writeCccdValue(device, 0x001); + written = unsignedIntCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a long characteristic") + { + BLELongCharacteristic longCharacteristic("Long", BLEBroadcast| BLEIndicate | BLENotify); + long v = -1; + int written = longCharacteristic.writeValue(v); + REQUIRE( written == sizeof(long) ); + + longCharacteristic.broadcast(); + written = longCharacteristic.writeValue(v); + REQUIRE( written == sizeof(long) ); + + BLEDevice device; + longCharacteristic.local()->writeCccdValue(device, 0x002); + written = longCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + longCharacteristic.local()->writeCccdValue(device, 0x001); + written = longCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a unsigned long characteristic") + { + BLEUnsignedLongCharacteristic unsignedLongCharacteristic("UnsignedLong", BLEBroadcast| BLEIndicate | BLENotify); + unsigned long v = 1; + int written = unsignedLongCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned long) ); + + unsignedLongCharacteristic.broadcast(); + written = unsignedLongCharacteristic.writeValue(v); + REQUIRE( written == sizeof(unsigned long) ); + + BLEDevice device; + unsignedLongCharacteristic.local()->writeCccdValue(device, 0x002); + written = unsignedLongCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + unsignedLongCharacteristic.local()->writeCccdValue(device, 0x001); + written = unsignedLongCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a float characteristic") + { + BLEFloatCharacteristic floatCharacteristic("Float", BLEBroadcast| BLEIndicate | BLENotify); + float v = -1.0f; + int written = floatCharacteristic.writeValue(v); + REQUIRE( written == sizeof(float) ); + + floatCharacteristic.broadcast(); + written = floatCharacteristic.writeValue(v); + REQUIRE( written == sizeof(float) ); + + BLEDevice device; + floatCharacteristic.local()->writeCccdValue(device, 0x002); + written = floatCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + floatCharacteristic.local()->writeCccdValue(device, 0x001); + written = floatCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a double characteristic") + { + BLEDoubleCharacteristic doubleCharacteristic("Double", BLEBroadcast| BLEIndicate | BLENotify); + double v = -1.0; + int written = doubleCharacteristic.writeValue(v); + REQUIRE( written == sizeof(double) ); + + doubleCharacteristic.broadcast(); + written = doubleCharacteristic.writeValue(v); + REQUIRE( written == sizeof(double) ); + + BLEDevice device; + doubleCharacteristic.local()->writeCccdValue(device, 0x002); + written = doubleCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + doubleCharacteristic.local()->writeCccdValue(device, 0x001); + written = doubleCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a string characteristic") + { + const int maxStringLength = 64; + BLEStringCharacteristic stringCharacteristic("String", BLEBroadcast| BLEIndicate | BLENotify, maxStringLength); + const char* v = "Hello"; + int written = stringCharacteristic.writeValue(v); + REQUIRE( written == min(strlen(v), maxStringLength) ); + + stringCharacteristic.broadcast(); + written = stringCharacteristic.writeValue(v); + REQUIRE( written == min(strlen(v), maxStringLength) ); + + BLEDevice device; + stringCharacteristic.local()->writeCccdValue(device, 0x002); + written = stringCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + stringCharacteristic.local()->writeCccdValue(device, 0x001); + written = stringCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + + WHEN("Create a too long string characteristic") + { + const int maxStringLength = 4; + BLEStringCharacteristic stringCharacteristic("String", BLEBroadcast| BLEIndicate | BLENotify, maxStringLength); + const char* v = "Hello"; + int written = stringCharacteristic.writeValue(v); + REQUIRE( written == min(strlen(v), maxStringLength) ); + + stringCharacteristic.broadcast(); + written = stringCharacteristic.writeValue(v); + REQUIRE( written == min(strlen(v), maxStringLength) ); + + BLEDevice device; + stringCharacteristic.local()->writeCccdValue(device, 0x002); + written = stringCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + stringCharacteristic.local()->writeCccdValue(device, 0x001); + written = stringCharacteristic.writeValue(v); + /* No peers connected */ + REQUIRE( written == 0 ); + } + +} diff --git a/keywords.txt b/keywords.txt index b908b573..150e6262 100644 --- a/keywords.txt +++ b/keywords.txt @@ -119,10 +119,6 @@ valueBE KEYWORD2 uuid KEYWORD2 addCharacteristic KEYWORD2 -wait KEYWORD2 -peek KEYWORD2 -write KEYWORD2 - ####################################### # Constants (LITERAL1) ####################################### diff --git a/library.properties b/library.properties index 1842c0ee..8696add7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=STM32duinoBLE -version=1.3.7 +version=1.4.1 author=Arduino, SRA maintainer=stm32duino sentence=Fork of ArduinoBLE library to add the support of STM32WB, SPBTLE-RF, SPBTLE-1S, BLUENRG-M2SP, BLUENRG-LP and BLUENRG-M0 BLE modules. diff --git a/src/BLEStringCharacteristic.cpp b/src/BLEStringCharacteristic.cpp index 76f0812c..a12e722d 100644 --- a/src/BLEStringCharacteristic.cpp +++ b/src/BLEStringCharacteristic.cpp @@ -19,7 +19,7 @@ #include "BLEStringCharacteristic.h" -BLEStringCharacteristic::BLEStringCharacteristic(const char* uuid, unsigned char properties, int valueSize) : +BLEStringCharacteristic::BLEStringCharacteristic(const char* uuid, unsigned int properties, int valueSize) : BLECharacteristic(uuid, properties, valueSize) { } diff --git a/src/BLEStringCharacteristic.h b/src/BLEStringCharacteristic.h index c9f28fca..65604846 100644 --- a/src/BLEStringCharacteristic.h +++ b/src/BLEStringCharacteristic.h @@ -27,7 +27,7 @@ class BLEStringCharacteristic : public BLECharacteristic { public: - BLEStringCharacteristic(const char* uuid, unsigned char properties, int valueSize); + BLEStringCharacteristic(const char* uuid, unsigned int properties, int valueSize); int writeValue(const String& value); int setValue(const String& value) { return writeValue(value); } diff --git a/src/local/BLELocalCharacteristic.cpp b/src/local/BLELocalCharacteristic.cpp index 207425bd..dc8a819d 100644 --- a/src/local/BLELocalCharacteristic.cpp +++ b/src/local/BLELocalCharacteristic.cpp @@ -126,15 +126,13 @@ int BLELocalCharacteristic::writeValue(const uint8_t value[], int length) if (_broadcast) { uint16_t serviceUuid = GATT.serviceUuidForCharacteristic(this); - - BLE.setAdvertisedServiceData(serviceUuid, value, length); - + BLE.setAdvertisedServiceData(serviceUuid, value, _valueLength); if (!ATT.connected() && GAP.advertising()) { BLE.advertise(); } } - return 1; + return _valueLength; } int BLELocalCharacteristic::writeValue(const char* value) diff --git a/src/utility/ATT.cpp b/src/utility/ATT.cpp index d19b9b92..4fe4b71a 100644 --- a/src/utility/ATT.cpp +++ b/src/utility/ATT.cpp @@ -553,6 +553,22 @@ bool ATTClass::disconnect() numDisconnects++; + BLEDevice bleDevice(_peers[i].addressType, _peers[i].address); + + // clear CCCD values on disconnect + for (uint16_t att = 0; att < GATT.attributeCount(); att++) { + BLELocalAttribute* attribute = GATT.attribute(att); + + if (attribute->type() == BLETypeCharacteristic) { + BLELocalCharacteristic* characteristic = (BLELocalCharacteristic*)attribute; + + characteristic->writeCccdValue(bleDevice, 0x0000); + } + } + + _longWriteHandle = 0x0000; + _longWriteValueLength = 0; + _peers[i].connectionHandle = 0xffff; _peers[i].role = 0x00; _peers[i].addressType = 0x00; @@ -582,7 +598,7 @@ BLEDevice ATTClass::central() return BLEDevice(); } -bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length) +int ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length) { int numNotifications = 0; @@ -610,10 +626,10 @@ bool ATTClass::handleNotify(uint16_t handle, const uint8_t* value, int length) numNotifications++; } - return (numNotifications > 0); + return (numNotifications > 0) ? length : 0; } -bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, int length) +int ATTClass::handleInd(uint16_t handle, const uint8_t* value, int length) { int numIndications = 0; @@ -650,7 +666,7 @@ bool ATTClass::handleInd(uint16_t handle, const uint8_t* value, int length) numIndications++; } - return (numIndications > 0); + return (numIndications > 0) ? length : 0; } void ATTClass::error(uint16_t connectionHandle, uint8_t dlen, uint8_t data[]) diff --git a/src/utility/ATT.h b/src/utility/ATT.h index 5e41e1fa..2d69d2b5 100644 --- a/src/utility/ATT.h +++ b/src/utility/ATT.h @@ -83,8 +83,8 @@ class ATTClass { virtual BLEDevice central(); - virtual bool handleNotify(uint16_t handle, const uint8_t* value, int length); - virtual bool handleInd(uint16_t handle, const uint8_t* value, int length); + virtual int handleNotify(uint16_t handle, const uint8_t* value, int length); + virtual int handleInd(uint16_t handle, const uint8_t* value, int length); virtual void setEventHandler(BLEDeviceEvent event, BLEDeviceEventHandler eventHandler); diff --git a/src/utility/GATT.cpp b/src/utility/GATT.cpp index be914f6f..3550fd99 100644 --- a/src/utility/GATT.cpp +++ b/src/utility/GATT.cpp @@ -70,21 +70,31 @@ void GATTClass::begin() void GATTClass::end() { - if (_genericAccessService->release() == 0) + if (_genericAccessService && _genericAccessService->release() == 0) { delete(_genericAccessService); - - if (_deviceNameCharacteristic->release() == 0) + _genericAccessService = NULL; + } + + if (_deviceNameCharacteristic && _deviceNameCharacteristic->release() == 0) { delete(_deviceNameCharacteristic); - - if (_appearanceCharacteristic->release() == 0) + _deviceNameCharacteristic = NULL; + } + + if (_appearanceCharacteristic && _appearanceCharacteristic->release() == 0) { delete(_appearanceCharacteristic); - - if (_genericAttributeService->release() == 0) + _appearanceCharacteristic = NULL; + } + + if (_genericAttributeService && _genericAttributeService->release() == 0) { delete(_genericAttributeService); - - if (_servicesChangedCharacteristic->release() == 0) + _genericAttributeService = NULL; + } + + if (_servicesChangedCharacteristic && _servicesChangedCharacteristic->release() == 0) { delete(_servicesChangedCharacteristic); - + _servicesChangedCharacteristic = NULL; + } + clearAttributes(); } diff --git a/src/utility/HCI.cpp b/src/utility/HCI.cpp index 8ac2419a..40afd7c7 100644 --- a/src/utility/HCI.cpp +++ b/src/utility/HCI.cpp @@ -101,7 +101,8 @@ String commandToString(LE_COMMAND command){ HCIClass::HCIClass() : _debug(NULL), _recvIndex(0), - _pendingPkt(0) + _pendingPkt(0), + _l2CapPduBufferSize(0) { } @@ -136,13 +137,16 @@ void HCIClass::poll(unsigned long timeout) HCITransport.wait(timeout); } + HCITransport.lockForRead(); while (HCITransport.available()) { byte b = HCITransport.read(); if (_recvIndex >= (int)sizeof(_recvBuffer)) { _recvIndex = 0; if (_debug) { + HCITransport.unlockForRead(); _debug->println("_recvBuffer overflow"); + HCITransport.lockForRead(); } } @@ -150,6 +154,7 @@ void HCIClass::poll(unsigned long timeout) if (_recvBuffer[0] == HCI_ACLDATA_PKT) { if (_recvIndex > 5 && _recvIndex >= (5 + (_recvBuffer[3] + (_recvBuffer[4] << 8)))) { + HCITransport.unlockForRead(); if (_debug) { dumpPkt("HCI ACLDATA RX <- ", _recvIndex, _recvBuffer); } @@ -164,9 +169,11 @@ void HCIClass::poll(unsigned long timeout) #ifdef ARDUINO_AVR_UNO_WIFI_REV2 digitalWrite(NINA_RTS, LOW); #endif + HCITransport.lockForRead(); } } else if (_recvBuffer[0] == HCI_EVENT_PKT) { if (_recvIndex > 3 && _recvIndex >= (3 + _recvBuffer[2])) { + HCITransport.unlockForRead(); if (_debug) { dumpPkt("HCI EVENT RX <- ", _recvIndex, _recvBuffer); } @@ -182,12 +189,15 @@ void HCIClass::poll(unsigned long timeout) #ifdef ARDUINO_AVR_UNO_WIFI_REV2 digitalWrite(NINA_RTS, LOW); #endif + HCITransport.lockForRead(); } } else { _recvIndex = 0; if (_debug) { + HCITransport.unlockForRead(); _debug->println(b, HEX); + HCITransport.lockForRead(); } } } @@ -195,6 +205,7 @@ void HCIClass::poll(unsigned long timeout) #ifdef ARDUINO_AVR_UNO_WIFI_REV2 digitalWrite(NINA_RTS, HIGH); #endif + HCITransport.unlockForRead(); } int HCIClass::reset() @@ -485,6 +496,7 @@ void HCIClass::leAddResolvingAddress(uint8_t addressType, uint8_t* peerAddress, addDevice.peerIRK[15-i] = peerIrk[i]; addDevice.localIRK[15-i] = localIrk[i]; } +#ifdef _BLE_TRACE_ Serial.print("ADDTYPE :"); btct.printBytes(&addDevice.peerAddressType,1); Serial.print("adddddd :"); @@ -493,6 +505,7 @@ void HCIClass::leAddResolvingAddress(uint8_t addressType, uint8_t* peerAddress, btct.printBytes(addDevice.peerIRK,16); Serial.print("localIRK :"); btct.printBytes(addDevice.localIRK,16); +#endif sendCommand(OGF_LE_CTL << 10 | 0x27, sizeof(addDevice), &addDevice); leStartResolvingAddresses(); @@ -516,6 +529,7 @@ int HCIClass::leReadPeerResolvableAddress(uint8_t peerAddressType, uint8_t* peer int res = sendCommand(OGF_LE_CTL << 10 | 0x2B, sizeof(request), &request); +#ifdef _BLE_TRACE_ Serial.print("res: 0x"); Serial.println(res, HEX); if(res==0){ @@ -528,6 +542,7 @@ int HCIClass::leReadPeerResolvableAddress(uint8_t peerAddressType, uint8_t* peer Serial.print("peer resolvable address: "); btct.printBytes(response->peerResolvableAddress,6); } + #endif return res; } @@ -566,7 +581,9 @@ int HCIClass::tryResolveAddress(uint8_t* BDAddr, uint8_t* address){ if(!HCI._getIRKs(&nIRKs, BDAddrType, BADDRs, IRKs)){ +#ifdef _BLE_TRACE_ Serial.println("error getting IRKs."); +#endif } for(int i=0; iconnectionHandleWithFlags & 0x3000) >> 12; + uint16_t connectionHandle = aclHeader->connectionHandleWithFlags & 0x0fff; - uint16_t aclFlags = (aclHdr->handle & 0xf000) >> 12; + uint8_t *aclSdu = &pdata[sizeof(HCIACLHdr)]; - if ((aclHdr->dlen - 4) != aclHdr->len) { - // packet is fragmented - if (aclFlags != 0x01) { - // copy into ACL buffer - memcpy(_aclPktBuffer, &_recvBuffer[1], sizeof(HCIACLHdr) + aclHdr->dlen - 4); - } else { - // copy next chunk into the buffer - HCIACLHdr* aclBufferHeader = (HCIACLHdr*)_aclPktBuffer; +#ifdef _BLE_TRACE_ + uint8_t bcFlag = (aclHeader->connectionHandleWithFlags & 0xc000) >> 14; + Serial.print("Acl packet bcFlag = "); + Serial.print(bcFlag, BIN); + Serial.print(" pbFlag = "); + Serial.print(pbFlag, BIN); + Serial.print(" connectionHandle = "); + Serial.print(connectionHandle, HEX); + Serial.print(" dlen = "); + Serial.println(aclHeader->dlen, DEC); +#endif - memcpy(&_aclPktBuffer[sizeof(HCIACLHdr) + aclBufferHeader->dlen - 4], &_recvBuffer[1 + sizeof(aclHdr->handle) + sizeof(aclHdr->dlen)], aclHdr->dlen); + // Pointer to the L2CAP PDU (might be reconstructed from multiple fragments) + uint8_t *l2CapPdu; + uint8_t l2CapPduSize; - aclBufferHeader->dlen += aclHdr->dlen; - aclHdr = aclBufferHeader; - } - } + if (pbFlag == 0b10) { + // "First automatically flushable packet" = Start of our L2CAP PDU + + l2CapPdu = aclSdu; + l2CapPduSize = aclHeader->dlen; + } else if (pbFlag == 0b01) { + // "Continuing Fragment" = Continued L2CAP PDU +#ifdef _BLE_TRACE_ + Serial.print("Continued packet. Appending to L2CAP PDU buffer (previously "); + Serial.print(_l2CapPduBufferSize, DEC); + Serial.println(" bytes in buffer)"); +#endif + // If we receive a fragment, we always need to append it to the L2CAP PDU buffer + memcpy(&_l2CapPduBuffer[_l2CapPduBufferSize], aclSdu, aclHeader->dlen); + _l2CapPduBufferSize += aclHeader->dlen; - if ((aclHdr->dlen - 4) != aclHdr->len) { + l2CapPdu = _l2CapPduBuffer; + l2CapPduSize = _l2CapPduBufferSize; + } else { + // I don't think other values are allowed for BLE #ifdef _BLE_TRACE_ - Serial.println("Don't have full packet yet"); - Serial.print("Handle: "); - btct.printBytes((uint8_t*)&aclHdr->handle,2); - Serial.print("dlen: "); - btct.printBytes((uint8_t*)&aclHdr->dlen,2); - Serial.print("len: "); - btct.printBytes((uint8_t*)&aclHdr->len,2); - Serial.print("cid: "); - btct.printBytes((uint8_t*)&aclHdr->cid,2); + Serial.println("Invalid pbFlag, discarding packet"); #endif - // don't have the full packet yet return; } - if (aclHdr->cid == ATT_CID) { - if (aclFlags == 0x01) { - // use buffered packet - ATT.handleData(aclHdr->handle & 0x0fff, aclHdr->len, &_aclPktBuffer[sizeof(HCIACLHdr)]); - } else { - // use the recv buffer - ATT.handleData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]); - } - } else if (aclHdr->cid == SIGNALING_CID) { + // We now have a valid L2CAP header in l2CapPdu and can parse the headers + struct __attribute__ ((packed)) HCIL2CapHdr { + uint16_t len; // size of the L2CAP SDU + uint16_t cid; + } *l2CapHeader = (HCIL2CapHdr*)l2CapPdu; + #ifdef _BLE_TRACE_ - Serial.println("Signaling"); + Serial.print("Received "); + Serial.print(l2CapPduSize - 4, DEC); + Serial.print("B/"); + Serial.print(l2CapHeader->len, DEC); + Serial.print("B of the L2CAP SDU. CID = "); + Serial.println(l2CapHeader->cid, HEX); #endif - L2CAPSignaling.handleData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]); - } else if (aclHdr->cid == SECURITY_CID){ - // Security manager + + // -4 because the buffer is the L2CAP PDU (with L2CAP header). The len field is only the L2CAP SDU (without L2CAP header). + if (l2CapPduSize - 4 != l2CapHeader->len) { #ifdef _BLE_TRACE_ - Serial.println("Security data"); + Serial.println("L2CAP SDU incomplete"); #endif - if (aclFlags == 0x1){ - L2CAPSignaling.handleSecurityData(aclHdr->handle & 0x0fff, aclHdr->len, &_aclPktBuffer[sizeof(HCIACLHdr)]); - }else{ - L2CAPSignaling.handleSecurityData(aclHdr->handle & 0x0fff, aclHdr->len, &_recvBuffer[1 + sizeof(HCIACLHdr)]); + + // If this is a first packet, we have not copied it into the buffer yet + if (pbFlag == 0b10) { +#ifdef _BLE_TRACE_ + Serial.println("Storing first packet to L2CAP PDU buffer"); + if (_l2CapPduBufferSize != 0) { + Serial.print("Warning: Discarding "); + Serial.print(_l2CapPduBufferSize, DEC); + Serial.println(" bytes from buffer"); + } +#endif + + memcpy(_l2CapPduBuffer, l2CapPdu, l2CapPduSize); + _l2CapPduBufferSize = l2CapPduSize; } - }else { + // We need to wait for the missing parts of the L2CAP SDU + return; + } + +#ifdef _BLE_TRACE_ + Serial.println("L2CAP SDU complete"); +#endif + + if (l2CapHeader->cid == ATT_CID) { +#ifdef _BLE_TRACE_ + Serial.println("CID: ATT"); +#endif + ATT.handleData(connectionHandle, l2CapHeader->len, &l2CapPdu[sizeof(HCIL2CapHdr)]); + } else if (l2CapHeader->cid == SIGNALING_CID) { +#ifdef _BLE_TRACE_ + Serial.println("CID: SIGNALING"); +#endif + L2CAPSignaling.handleData(connectionHandle, l2CapHeader->len, &l2CapPdu[sizeof(HCIL2CapHdr)]); + } else if (l2CapHeader->cid == SECURITY_CID) { + // Security manager +#ifdef _BLE_TRACE_ + Serial.println("CID: SECURITY"); +#endif + L2CAPSignaling.handleSecurityData(connectionHandle, l2CapHeader->len, &l2CapPdu[sizeof(HCIL2CapHdr)]); + } else { struct __attribute__ ((packed)) { uint8_t op; uint8_t id; @@ -783,14 +846,16 @@ void HCIClass::handleAclDataPkt(uint8_t /*plen*/, uint8_t pdata[]) uint16_t reason; uint16_t localCid; uint16_t remoteCid; - } l2capRejectCid= { 0x01, 0x00, 0x006, 0x0002, aclHdr->cid, 0x0000 }; + } l2capRejectCid= { 0x01, 0x00, 0x006, 0x0002, l2CapHeader->cid, 0x0000 }; #ifdef _BLE_TRACE_ - Serial.print("rejecting packet cid: 0x"); - Serial.println(aclHdr->cid,HEX); + Serial.println("Rejecting packet cid"); #endif - sendAclPkt(aclHdr->handle & 0x0fff, 0x0005, sizeof(l2capRejectCid), &l2capRejectCid); + sendAclPkt(connectionHandle, 0x0005, sizeof(l2capRejectCid), &l2capRejectCid); } + + // We have processed everything in the buffer. Discard the contents. + _l2CapPduBufferSize = 0; } void HCIClass::handleNumCompPkts(uint16_t /*handle*/, uint16_t numPkts) diff --git a/src/utility/HCI.h b/src/utility/HCI.h index a6fa66e8..532cf5d6 100644 --- a/src/utility/HCI.h +++ b/src/utility/HCI.h @@ -159,7 +159,8 @@ class HCIClass { uint8_t _maxPkt; uint8_t _pendingPkt; - uint8_t _aclPktBuffer[255]; + uint8_t _l2CapPduBuffer[255]; + uint8_t _l2CapPduBufferSize; }; extern HCIClass& HCI; diff --git a/src/utility/HCISharedMemTransport.cpp b/src/utility/HCISharedMemTransport.cpp index f4ce1d0b..6821493c 100644 --- a/src/utility/HCISharedMemTransport.cpp +++ b/src/utility/HCISharedMemTransport.cpp @@ -22,11 +22,10 @@ #include "STM32_WPAN/hw.h" #include "otp.h" -#if defined(ARDUINO_NUCLEO_WB15CC) || defined(ARDUINO_P_NUCLEO_WB55RG) ||\ - defined(ARDUINO_STM32WB5MM_DK) || defined(ARDUINO_P_NUCLEO_WB55_USB_DONGLE) HCISharedMemTransportClass HCISharedMemTransport; -#else -#error "Unsupported board or shield selected!" +#if !defined(ARDUINO_NUCLEO_WB15CC) && !defined(ARDUINO_P_NUCLEO_WB55RG) &&\ + !defined(ARDUINO_STM32WB5MM_DK) && !defined(ARDUINO_P_NUCLEO_WB55_USB_DONGLE) +#warning "Selected board has never been tested with this library, ensure to have a correct configuration!" #endif /* Private variables ---------------------------------------------------------*/ @@ -666,7 +665,8 @@ int HCISharedMemTransportClass::stm32wb_start_ble(void) CFG_BLE_TX_PATH_COMPENS, CFG_BLE_RX_PATH_COMPENS, CFG_BLE_CORE_VERSION, - CFG_BLE_OPTIONS_EXT + CFG_BLE_OPTIONS_EXT, + CFG_BLE_MAX_ADD_EATT_BEARERS }; /** * Starts the BLE Stack on CPU2 diff --git a/src/utility/HCITransport.h b/src/utility/HCITransport.h index 02e7962f..20259b86 100644 --- a/src/utility/HCITransport.h +++ b/src/utility/HCITransport.h @@ -37,6 +37,12 @@ class HCITransportInterface { virtual int peek() = 0; virtual int read() = 0; + // Some transports require a lock to use available/peek/read + // These methods allow to keep the lock while reading an unknown number of bytes + // These methods might disable interrupts. Only keep the lock as long as necessary. + virtual void lockForRead() {} + virtual void unlockForRead() {} + virtual size_t write(const uint8_t* data, size_t length) = 0; }; diff --git a/src/utility/STM32_WPAN/LICENSE.md b/src/utility/STM32_WPAN/LICENSE.md index 4e70ee7c..ea34f9bc 100644 --- a/src/utility/STM32_WPAN/LICENSE.md +++ b/src/utility/STM32_WPAN/LICENSE.md @@ -26,83 +26,92 @@ shall apply unless other terms are clearly stated in the package. -SLA0044 Rev5/February 2018 +SLA0044 Rev6/October 2025 ## Software license agreement ### __ULTIMATE LIBERTY SOFTWARE LICENSE AGREEMENT__ -BY INSTALLING, COPYING, DOWNLOADING, ACCESSING OR OTHERWISE USING THIS SOFTWARE -OR ANY PART THEREOF (AND THE RELATED DOCUMENTATION) FROM STMICROELECTRONICS -INTERNATIONAL N.V, SWISS BRANCH AND/OR ITS AFFILIATED COMPANIES -(STMICROELECTRONICS), THE RECIPIENT, ON BEHALF OF HIMSELF OR HERSELF, OR ON -BEHALF OF ANY ENTITY BY WHICH SUCH RECIPIENT IS EMPLOYED AND/OR ENGAGED AGREES -TO BE BOUND BY THIS SOFTWARE LICENSE AGREEMENT. - -Under STMicroelectronics’ intellectual property rights, the redistribution, -reproduction and use in source and binary forms of the software or any part +BY CLICKING ON THE "I ACCEPT" BUTTON OR BY UNZIPPING, INSTALLING, COPYING, +DOWNLOADING, ACCESSING OR OTHERWISE USING THIS SOFTWARE OR ANY PART THEREOF, +INCLUDING ANY RELATED DOCUMENTATION (collectively the “SOFTWARE”) +FROM STMICROELECTRONICS INTERNATIONAL N.V, SWISS BRANCH AND/OR +ITS AFFILIATED COMPANIES (collectively “STMICROELECTRONICS”), +YOU (hereinafter referred also to as “THE RECIPIENT”), ON BEHALF OF YOURSELF, +OR ON BEHALF OF ANY ENTITY BY WHICH YOU ARE EMPLOYED AND/OR ENGAGED, +AGREE TO BE BOUND BY THIS AGREEMENT. + +Under STMICROELECTRONICS’ intellectual property rights, the redistribution, +reproduction and use in source and binary forms of the SOFTWARE or any part thereof, with or without modification, are permitted provided that the following conditions are met: 1. Redistribution of source code (modified or not) must retain any copyright -notice, this list of conditions and the disclaimer set forth below as items 10 -and 11. - -2. Redistributions in binary form, except as embedded into microcontroller or -microprocessor device manufactured by or for STMicroelectronics or a software -update for such device, must reproduce any copyright notice provided with the -binary code, this list of conditions, and the disclaimer set forth below as -items 10 and 11, in documentation and/or other materials provided with the -distribution. - -3. Neither the name of STMicroelectronics nor the names of other contributors to -this software may be used to endorse or promote products derived from this -software or part thereof without specific written permission. - -4. This software or any part thereof, including modifications and/or derivative -works of this software, must be used and execute solely and exclusively on or in -combination with a microcontroller or microprocessor device manufactured by or -for STMicroelectronics. - -5. No use, reproduction or redistribution of this software partially or totally -may be done in any manner that would subject this software to any Open Source -Terms. “Open Source Terms” shall mean any open source license which requires as -part of distribution of software that the source code of such software is -distributed therewith or otherwise made available, or open source license that -substantially complies with the Open Source definition specified at -www.opensource.org and any other comparable open source license such as for -example GNU General Public License (GPL), Eclipse Public License (EPL), Apache -Software License, BSD license or MIT license. - -6. STMicroelectronics has no obligation to provide any maintenance, support or -updates for the software. - -7. The software is and will remain the exclusive property of STMicroelectronics -and its licensors. The recipient will not take any action that jeopardizes -STMicroelectronics and its licensors' proprietary rights or acquire any rights -in the software, except the limited rights specified hereunder. - -8. The recipient shall comply with all applicable laws and regulations affecting -the use of the software or any part thereof including any applicable export -control law or regulation. - -9. Redistribution and use of this software or any part thereof other than as -permitted under this license is void and will automatically terminate your -rights under this license. - -10. THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY RIGHTS, WHICH ARE -DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT SHALL -STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE -OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -11. EXCEPT AS EXPRESSLY PERMITTED HEREUNDER, NO LICENSE OR OTHER RIGHTS, WHETHER -EXPRESS OR IMPLIED, ARE GRANTED UNDER ANY PATENT OR OTHER INTELLECTUAL PROPERTY -RIGHTS OF STMICROELECTRONICS OR ANY THIRD PARTY. +notice accompanying the SOFTWARE, this list of conditions and the disclaimer below. + +2. Redistributions in binary form, except as embedded into a processing unit device +manufactured by or for STMicroelectronics or a software update for any such device, +must reproduce the accompanying copyright notice, this list of conditions, +and the below disclaimer in capital type, in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of STMicroelectronics nor the names of other contributors +to the SOFTWARE may be used to endorse or promote products derived +from the SOFTWARE or part thereof without specific written permission. + +4. The SOFTWARE or any part thereof, including modifications and/or +derivative works of the SOFTWARE, must be used and execute solely +and exclusively on or in combination with a processing unit device +manufactured by or for STMicroelectronics. + +5. No use, reproduction or redistribution of the SOFTWARE partially +or totally may be done in any manner that would subject the SOFTWARE +to any Open Source Terms. “Open Source Terms” shall mean +any open source license which requires as part of distribution +of software that the source code of such software is distributed +therewith or otherwise made available, or open source license +that substantially complies with the Open Source definition specified +at www.opensource.org and any other comparable open source license +such as for example GNU General Public License (GPL), +Eclipse Public License (EPL), Apache Software License, BSD license +or MIT license. + +6. STMicroelectronics has no obligation to provide any maintenance, +support or updates for the SOFTWARE. + +7. The SOFTWARE is and will remain the exclusive property of +STMicroelectronics and its licensors. The RECIPIENT will not take +any action that jeopardizes STMicroelectronics and its +licensors' proprietary rights or acquire any rights in the SOFTWARE, +except the limited rights specified hereunder. + +8. The RECIPIENT shall comply with all applicable laws and regulations +affecting the use of the SOFTWARE or any part thereof including +any applicable export control law or regulation. + +9. Redistribution and use of the SOFTWARE or any part thereof other +than as permitted under this AGREEMENT is void and will automatically +terminate RECIPIENT’s rights under this AGREEMENT. + +10. The RECIPIENT shall be solely liable to determine and verify that +the SOFTWARE is fit for the RECIPIENT intended use, environment or +application and comply with all regulatory, safety and security +related requirements concerning any use. + +DISCLAIMER: + +THE SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING,BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE +AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY RIGHTS, ARE +DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. +IN NO EVENT SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +EXCEPT AS EXPRESSLY PERMITTED HEREUNDER, NO LICENSE OR OTHER RIGHTS, +WHETHER EXPRESS OR IMPLIED, ARE GRANTED UNDER ANY PATENT OR OTHER INTELLECTUAL +PROPERTY RIGHTS OF STMICROELECTRONICS OR ANY THIRD PARTY. \ No newline at end of file diff --git a/src/utility/STM32_WPAN/README.md b/src/utility/STM32_WPAN/README.md index 90f96452..27010ad2 100644 --- a/src/utility/STM32_WPAN/README.md +++ b/src/utility/STM32_WPAN/README.md @@ -1,6 +1,6 @@ ## Source -[STMicroelectronics/STM32CubeWB Release v1.22.0](https://github.com/STMicroelectronics/STM32CubeWB/releases/tag/v1.22.0) -- Application: [BLE_TransparentMode](https://github.com/STMicroelectronics/STM32CubeWB/tree/v1.22.0/Projects/P-NUCLEO-WB55.Nucleo/Applications/BLE/BLE_TransparentMode) +[STMicroelectronics/STM32CubeWB Release v1.24.0](https://github.com/STMicroelectronics/STM32CubeWB/releases/tag/v1.24.0) +- Application: [BLE_TransparentMode](https://github.com/STMicroelectronics/STM32CubeWB/tree/v1.24.0/Projects/P-NUCLEO-WB55.Nucleo/Applications/BLE/BLE_TransparentMode) diff --git a/src/utility/STM32_WPAN/app_conf_default.h b/src/utility/STM32_WPAN/app_conf_default.h index ff2dc017..5fbb234f 100644 --- a/src/utility/STM32_WPAN/app_conf_default.h +++ b/src/utility/STM32_WPAN/app_conf_default.h @@ -328,6 +328,19 @@ #define CFG_BLE_MAX_TX_POWER (6) +/** +* BLE stack Maximum number of created Enhanced ATT bearers to be configured +* in addition to the number of links +* - Range: 0 .. 4 +*/ +#define CFG_BLE_MAX_ADD_EATT_BEARERS (4) + +/** +* Size of the RAM buffer extra_data_buffer allocated for the extension of Host commands +* - Range: 0..500 (at least >250) +*/ +#define CFG_BLE_EXTRA_DATA_BUFFER_SIZE (500) + /** * BLE Rx model configuration flags to be configured with: * - SHCI_C2_BLE_INIT_RX_MODEL_AGC_RSSI_LEGACY diff --git a/src/utility/STM32_WPAN/ble_bufsize.h b/src/utility/STM32_WPAN/ble_bufsize.h index 26551c09..c4a36199 100644 --- a/src/utility/STM32_WPAN/ble_bufsize.h +++ b/src/utility/STM32_WPAN/ble_bufsize.h @@ -99,15 +99,15 @@ #if (BEACON_ONLY != 0) #define BLE_FIXED_BUFFER_SIZE_BYTES 4200 /* Beacon only */ #elif (LL_ONLY_BASIC != 0) -#define BLE_FIXED_BUFFER_SIZE_BYTES 6040 /* LL only Basic*/ +#define BLE_FIXED_BUFFER_SIZE_BYTES 5960 /* LL only Basic*/ #elif (LL_ONLY != 0) #define BLE_FIXED_BUFFER_SIZE_BYTES 6288 /* LL only Full */ #elif (SLAVE_ONLY != 0) #define BLE_FIXED_BUFFER_SIZE_BYTES 6408 /* Peripheral only */ #elif (BASIC_FEATURES != 0) -#define BLE_FIXED_BUFFER_SIZE_BYTES 7184 /* Basic Features */ +#define BLE_FIXED_BUFFER_SIZE_BYTES 6928 /* Basic Features */ #else -#define BLE_FIXED_BUFFER_SIZE_BYTES 7468 /* Full stack */ +#define BLE_FIXED_BUFFER_SIZE_BYTES 7212 /* Full stack */ #endif /* diff --git a/src/utility/STM32_WPAN/shci.c b/src/utility/STM32_WPAN/shci.c index 40110f42..e6415307 100644 --- a/src/utility/STM32_WPAN/shci.c +++ b/src/utility/STM32_WPAN/shci.c @@ -115,6 +115,24 @@ SHCI_CmdStatus_t SHCI_C2_FUS_FwDelete( void ) return (SHCI_CmdStatus_t)(((TL_CcEvt_t*)(p_rsp->evtserial.evt.payload))->payload[0]); } +SHCI_CmdStatus_t SHCI_C2_FUS_FwPurge( void ) +{ + /** + * Buffer is large enough to hold command complete without payload + */ + uint8_t local_buffer[TL_BLEEVT_CC_BUFFER_SIZE]; + TL_EvtPacket_t * p_rsp; + + p_rsp = (TL_EvtPacket_t *)local_buffer; + + shci_send( SHCI_OPCODE_C2_FUS_FW_PURGE, + 0, + 0, + p_rsp ); + + return (SHCI_CmdStatus_t)(((TL_CcEvt_t*)(p_rsp->evtserial.evt.payload))->payload[0]); +} + SHCI_CmdStatus_t SHCI_C2_FUS_UpdateAuthKey( SHCI_C2_FUS_UpdateAuthKey_Cmd_Param_t *pParam ) { /** diff --git a/src/utility/STM32_WPAN/shci.h b/src/utility/STM32_WPAN/shci.h index 59c6fd46..74bf3fe9 100644 --- a/src/utility/STM32_WPAN/shci.h +++ b/src/utility/STM32_WPAN/shci.h @@ -35,6 +35,8 @@ extern "C" { { WIRELESS_FW_RUNNING = 0x00, FUS_FW_RUNNING = 0x01, + NVM_BACKUP_RUNNING = 0x10, + NVM_RESTORE_RUNNING = 0x11 } SHCI_SysEvt_Ready_Rsp_t; /* ERROR CODES @@ -197,7 +199,7 @@ extern "C" { SHCI_OCF_C2_FUS_STORE_USR_KEY, SHCI_OCF_C2_FUS_LOAD_USR_KEY, SHCI_OCF_C2_FUS_START_WS, - SHCI_OCF_C2_FUS_RESERVED2, + SHCI_OCF_C2_FUS_FW_PURGE, SHCI_OCF_C2_FUS_RESERVED3, SHCI_OCF_C2_FUS_LOCK_USR_KEY, SHCI_OCF_C2_FUS_UNLOAD_USR_KEY, @@ -276,6 +278,10 @@ extern "C" { /** No command parameters */ /** No response parameters*/ +#define SHCI_OPCODE_C2_FUS_FW_PURGE (( SHCI_OGF << 10) + SHCI_OCF_C2_FUS_FW_PURGE) +/** No command parameters */ +/** No response parameters*/ + #define SHCI_OPCODE_C2_FUS_UPDATE_AUTH_KEY (( SHCI_OGF << 10) + SHCI_OCF_C2_FUS_UPDATE_AUTH_KEY) typedef PACKED_STRUCT{ uint8_t KeySize; @@ -304,6 +310,7 @@ extern "C" { { KEYSIZE_16 = 16, KEYSIZE_32 = 32, + KEYSIZE_64 = 64 }; typedef PACKED_STRUCT{ @@ -611,7 +618,29 @@ extern "C" { */ uint8_t Options_extension; - } SHCI_C2_Ble_Init_Cmd_Param_t; + /** + * MaxAddEattBearers + * + * Maximum number of bearers that can be created for Enhanced ATT + * in addition to the number of links + * - Range: 0 .. 4 + */ + uint8_t MaxAddEattBearers; + + /** + * Address of the RAM buffer allocated for the extension of Host commands. + * This buffer is referred as the "extra data" buffer in the BLE Wireless + * Interface document. If the commands that need this extension are never + * used, this parameter can be set to NULL. + */ + uint8_t* extra_data_buffer; + + /** + * Size of the RAM buffer allocated for the extension of Host commands. + */ + uint32_t extra_data_buffer_size; + + } SHCI_C2_Ble_Init_Cmd_Param_t; typedef PACKED_STRUCT{ SHCI_Header_t Header; /** Does not need to be initialized by the user */ @@ -991,6 +1020,8 @@ extern "C" { #define INFO_STACK_TYPE_BLE_ZIGBEE_RFD_DYNAMIC 0x79 #define INFO_STACK_TYPE_RLV 0x80 #define INFO_STACK_TYPE_BLE_MAC_STATIC 0x90 +#define INFO_STACK_TYPE_NVM_BACKUP 0xF0 +#define INFO_STACK_TYPE_NVM_RESTORE 0xF1 typedef struct { /** @@ -1056,6 +1087,16 @@ typedef struct { */ SHCI_CmdStatus_t SHCI_C2_FUS_FwDelete( void ); + /** + * SHCI_C2_FUS_FwPurge + * @brief Delete the wireless stack on CPU2 and the NVM section (if any) + * Note: This command is only supported by the FUS. + * + * @param None + * @retval Status + */ + SHCI_CmdStatus_t SHCI_C2_FUS_FwPurge( void ); + /** * SHCI_C2_FUS_UpdateAuthKey * @brief Request the FUS to update the authentication key diff --git a/src/utility/STM32_WPAN/stm_list.h b/src/utility/STM32_WPAN/stm_list.h index b7c3254c..83bbe546 100644 --- a/src/utility/STM32_WPAN/stm_list.h +++ b/src/utility/STM32_WPAN/stm_list.h @@ -20,6 +20,10 @@ #ifndef _STM_LIST_H_ #define _STM_LIST_H_ +#ifdef __cplusplus +extern "C" { +#endif + /* Includes ------------------------------------------------------------------*/ #include "stm32_wpan_common.h" @@ -52,4 +56,8 @@ void LST_get_next_node (tListNode * ref_node, tListNode ** node); void LST_get_prev_node (tListNode * ref_node, tListNode ** node); +#ifdef __cplusplus +} +#endif + #endif /* _STM_LIST_H_ */ diff --git a/src/utility/STM32_WPAN/tl_mbox.c b/src/utility/STM32_WPAN/tl_mbox.c index 9a2a2973..21fa35f2 100644 --- a/src/utility/STM32_WPAN/tl_mbox.c +++ b/src/utility/STM32_WPAN/tl_mbox.c @@ -32,6 +32,8 @@ typedef enum TL_MB_MM_RELEASE_BUFFER, TL_MB_BLE_CMD, TL_MB_BLE_CMD_RSP, + TL_MB_ACL_DATA, + TL_MB_ACL_DATA_RSP, TL_MB_BLE_ASYNCH_EVT, TL_MB_SYS_CMD, TL_MB_SYS_CMD_RSP, @@ -179,6 +181,8 @@ int32_t TL_BLE_SendAclData( uint8_t* buffer, uint16_t size ) ((TL_AclDataPacket_t *)(TL_RefTable.p_ble_table->phci_acl_data_buffer))->AclDataSerial.type = TL_ACL_DATA_PKT_TYPE; + OutputDbgTrace(TL_MB_ACL_DATA, TL_RefTable.p_ble_table->phci_acl_data_buffer); + HW_IPCC_BLE_SendAclData(); return 0; @@ -186,6 +190,8 @@ int32_t TL_BLE_SendAclData( uint8_t* buffer, uint16_t size ) void HW_IPCC_BLE_AclDataAckNot(void) { + OutputDbgTrace(TL_MB_ACL_DATA_RSP, (uint8_t*)NULL); + BLE_IoBusAclDataTxAck( ); return; @@ -670,184 +676,205 @@ __WEAK void TL_TRACES_EvtReceived( TL_EvtPacket_t * hcievt ) } /****************************************************************************** - * DEBUG INFORMATION - ******************************************************************************/ +* DEBUG INFORMATION +******************************************************************************/ static void OutputDbgTrace(TL_MB_PacketType_t packet_type, uint8_t* buffer) { TL_EvtPacket_t *p_evt_packet; TL_CmdPacket_t *p_cmd_packet; + TL_AclDataPacket_t *p_acldata_packet; TL_EvtSerial_t *p_cmd_rsp_packet; switch(packet_type) { - case TL_MB_MM_RELEASE_BUFFER: - p_evt_packet = (TL_EvtPacket_t*)buffer; - switch(p_evt_packet->evtserial.evt.evtcode) - { - case TL_BLEEVT_CS_OPCODE: - TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_MM_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); - TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); - break; - - case TL_BLEEVT_CC_OPCODE: - TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_MM_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); - TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); - break; - - case TL_BLEEVT_VS_OPCODE: - TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_MM_DBG_MSG(" subevtcode: 0x%04X", ((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->subevtcode); - TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); - break; - - default: - TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); - break; - } + case TL_MB_MM_RELEASE_BUFFER: + p_evt_packet = (TL_EvtPacket_t*)buffer; + switch(p_evt_packet->evtserial.evt.evtcode) + { + case TL_BLEEVT_CS_OPCODE: + TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_MM_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); + TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); + break; - TL_MM_DBG_MSG("\r\n"); + case TL_BLEEVT_CC_OPCODE: + TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_MM_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); + TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); break; - case TL_MB_BLE_CMD: - p_cmd_packet = (TL_CmdPacket_t*)buffer; - TL_HCI_CMD_DBG_MSG("ble cmd: 0x%04X", p_cmd_packet->cmdserial.cmd.cmdcode); - if(p_cmd_packet->cmdserial.cmd.plen != 0) - { - TL_HCI_CMD_DBG_MSG(" payload:"); - TL_HCI_CMD_DBG_BUF(p_cmd_packet->cmdserial.cmd.payload, p_cmd_packet->cmdserial.cmd.plen, ""); - } + case TL_BLEEVT_VS_OPCODE: + TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_MM_DBG_MSG(" subevtcode: 0x%04X", ((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->subevtcode); + TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); + break; + + default: + TL_MM_DBG_MSG("mm evt released: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_MM_DBG_MSG(" buffer addr: 0x%08X", p_evt_packet); + break; + } + + TL_MM_DBG_MSG("\r\n"); + break; + + case TL_MB_BLE_CMD: + p_cmd_packet = (TL_CmdPacket_t*)buffer; + TL_HCI_CMD_DBG_MSG("ble cmd: 0x%04X", p_cmd_packet->cmdserial.cmd.cmdcode); + if(p_cmd_packet->cmdserial.cmd.plen != 0) + { + TL_HCI_CMD_DBG_MSG(" payload:"); + TL_HCI_CMD_DBG_BUF(p_cmd_packet->cmdserial.cmd.payload, p_cmd_packet->cmdserial.cmd.plen, ""); + } + TL_HCI_CMD_DBG_MSG("\r\n"); + + TL_HCI_CMD_DBG_RAW(&p_cmd_packet->cmdserial, p_cmd_packet->cmdserial.cmd.plen+TL_CMD_HDR_SIZE); + break; + + case TL_MB_ACL_DATA: + (void)p_acldata_packet; + p_acldata_packet = (TL_AclDataPacket_t*)buffer; + TL_HCI_CMD_DBG_MSG("acl_data: 0x%02X", p_acldata_packet->AclDataSerial.type); + TL_HCI_CMD_DBG_MSG("acl_data: 0x%04X", p_acldata_packet->AclDataSerial.handle); + TL_HCI_CMD_DBG_MSG("acl_data: 0x%04X", p_acldata_packet->AclDataSerial.length); + /*if(p_acldata_packet->AclDataSerial.length != 0) + { + TL_HCI_CMD_DBG_MSG(" payload:"); + TL_HCI_CMD_DBG_BUF(p_acldata_packet->AclDataSerial.acl_data, p_acldata_packet->AclDataSerial.length, ""); + }*/ + TL_HCI_CMD_DBG_MSG("\r\n"); + /*TL_HCI_CMD_DBG_RAW(&p_acldata_packet->AclDataSerial, p_acldata_packet->AclDataSerial.length+TL_CMD_HDR_SIZE);*/ + break; + + case TL_MB_ACL_DATA_RSP: + TL_HCI_CMD_DBG_MSG(" ACL Data Tx Ack received"); TL_HCI_CMD_DBG_MSG("\r\n"); + break; - TL_HCI_CMD_DBG_RAW(&p_cmd_packet->cmdserial, p_cmd_packet->cmdserial.cmd.plen+TL_CMD_HDR_SIZE); + case TL_MB_BLE_CMD_RSP: + p_evt_packet = (TL_EvtPacket_t*)buffer; + switch(p_evt_packet->evtserial.evt.evtcode) + { + case TL_BLEEVT_CS_OPCODE: + TL_HCI_CMD_DBG_MSG("ble rsp: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_HCI_CMD_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); + TL_HCI_CMD_DBG_MSG(" numhci: 0x%02X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->numcmd); + TL_HCI_CMD_DBG_MSG(" status: 0x%02X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->status); break; - case TL_MB_BLE_CMD_RSP: - p_evt_packet = (TL_EvtPacket_t*)buffer; - switch(p_evt_packet->evtserial.evt.evtcode) + case TL_BLEEVT_CC_OPCODE: + TL_HCI_CMD_DBG_MSG("ble rsp: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_HCI_CMD_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); + TL_HCI_CMD_DBG_MSG(" numhci: 0x%02X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->numcmd); + TL_HCI_CMD_DBG_MSG(" status: 0x%02X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload[0]); + if((p_evt_packet->evtserial.evt.plen-4) != 0) { - case TL_BLEEVT_CS_OPCODE: - TL_HCI_CMD_DBG_MSG("ble rsp: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_HCI_CMD_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); - TL_HCI_CMD_DBG_MSG(" numhci: 0x%02X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->numcmd); - TL_HCI_CMD_DBG_MSG(" status: 0x%02X", ((TL_CsEvt_t*)(p_evt_packet->evtserial.evt.payload))->status); - break; - - case TL_BLEEVT_CC_OPCODE: - TL_HCI_CMD_DBG_MSG("ble rsp: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_HCI_CMD_DBG_MSG(" cmd opcode: 0x%04X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->cmdcode); - TL_HCI_CMD_DBG_MSG(" numhci: 0x%02X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->numcmd); - TL_HCI_CMD_DBG_MSG(" status: 0x%02X", ((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload[0]); - if((p_evt_packet->evtserial.evt.plen-4) != 0) - { - TL_HCI_CMD_DBG_MSG(" payload:"); - TL_HCI_CMD_DBG_BUF(&((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload[1], p_evt_packet->evtserial.evt.plen-4, ""); - } - break; - - default: - TL_HCI_CMD_DBG_MSG("unknown ble rsp received: %02X", p_evt_packet->evtserial.evt.evtcode); - break; + TL_HCI_CMD_DBG_MSG(" payload:"); + TL_HCI_CMD_DBG_BUF(&((TL_CcEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload[1], p_evt_packet->evtserial.evt.plen-4, ""); } + break; - TL_HCI_CMD_DBG_MSG("\r\n"); - - TL_HCI_CMD_DBG_RAW(&p_evt_packet->evtserial, p_evt_packet->evtserial.evt.plen+TL_EVT_HDR_SIZE); + default: + TL_HCI_CMD_DBG_MSG("unknown ble rsp received: %02X", p_evt_packet->evtserial.evt.evtcode); break; + } + + TL_HCI_CMD_DBG_MSG("\r\n"); + + TL_HCI_CMD_DBG_RAW(&p_evt_packet->evtserial, p_evt_packet->evtserial.evt.plen+TL_EVT_HDR_SIZE); + break; - case TL_MB_BLE_ASYNCH_EVT: - p_evt_packet = (TL_EvtPacket_t*)buffer; - if(p_evt_packet->evtserial.evt.evtcode != TL_BLEEVT_VS_OPCODE) + case TL_MB_BLE_ASYNCH_EVT: + p_evt_packet = (TL_EvtPacket_t*)buffer; + if(p_evt_packet->evtserial.evt.evtcode != TL_BLEEVT_VS_OPCODE) + { + TL_HCI_EVT_DBG_MSG("ble evt: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + if((p_evt_packet->evtserial.evt.plen) != 0) { - TL_HCI_EVT_DBG_MSG("ble evt: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - if((p_evt_packet->evtserial.evt.plen) != 0) - { - TL_HCI_EVT_DBG_MSG(" payload:"); - TL_HCI_EVT_DBG_BUF(p_evt_packet->evtserial.evt.payload, p_evt_packet->evtserial.evt.plen, ""); - } + TL_HCI_EVT_DBG_MSG(" payload:"); + TL_HCI_EVT_DBG_BUF(p_evt_packet->evtserial.evt.payload, p_evt_packet->evtserial.evt.plen, ""); } - else + } + else + { + TL_HCI_EVT_DBG_MSG("ble evt: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_HCI_EVT_DBG_MSG(" subevtcode: 0x%04X", ((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->subevtcode); + if((p_evt_packet->evtserial.evt.plen-2) != 0) { - TL_HCI_EVT_DBG_MSG("ble evt: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_HCI_EVT_DBG_MSG(" subevtcode: 0x%04X", ((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->subevtcode); - if((p_evt_packet->evtserial.evt.plen-2) != 0) - { - TL_HCI_EVT_DBG_MSG(" payload:"); - TL_HCI_EVT_DBG_BUF(((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload, p_evt_packet->evtserial.evt.plen-2, ""); - } + TL_HCI_EVT_DBG_MSG(" payload:"); + TL_HCI_EVT_DBG_BUF(((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload, p_evt_packet->evtserial.evt.plen-2, ""); } + } - TL_HCI_EVT_DBG_MSG("\r\n"); + TL_HCI_EVT_DBG_MSG("\r\n"); - TL_HCI_EVT_DBG_RAW(&p_evt_packet->evtserial, p_evt_packet->evtserial.evt.plen+TL_EVT_HDR_SIZE); - break; + TL_HCI_EVT_DBG_RAW(&p_evt_packet->evtserial, p_evt_packet->evtserial.evt.plen+TL_EVT_HDR_SIZE); + break; + + case TL_MB_SYS_CMD: + p_cmd_packet = (TL_CmdPacket_t*)buffer; - case TL_MB_SYS_CMD: - p_cmd_packet = (TL_CmdPacket_t*)buffer; + TL_SHCI_CMD_DBG_MSG("sys cmd: 0x%04X", p_cmd_packet->cmdserial.cmd.cmdcode); - TL_SHCI_CMD_DBG_MSG("sys cmd: 0x%04X", p_cmd_packet->cmdserial.cmd.cmdcode); + if(p_cmd_packet->cmdserial.cmd.plen != 0) + { + TL_SHCI_CMD_DBG_MSG(" payload:"); + TL_SHCI_CMD_DBG_BUF(p_cmd_packet->cmdserial.cmd.payload, p_cmd_packet->cmdserial.cmd.plen, ""); + } + TL_SHCI_CMD_DBG_MSG("\r\n"); - if(p_cmd_packet->cmdserial.cmd.plen != 0) + TL_SHCI_CMD_DBG_RAW(&p_cmd_packet->cmdserial, p_cmd_packet->cmdserial.cmd.plen+TL_CMD_HDR_SIZE); + break; + + case TL_MB_SYS_CMD_RSP: + p_cmd_rsp_packet = (TL_EvtSerial_t*)buffer; + switch(p_cmd_rsp_packet->evt.evtcode) + { + case TL_BLEEVT_CC_OPCODE: + TL_SHCI_CMD_DBG_MSG("sys rsp: 0x%02X", p_cmd_rsp_packet->evt.evtcode); + TL_SHCI_CMD_DBG_MSG(" cmd opcode: 0x%02X", ((TL_CcEvt_t*)(p_cmd_rsp_packet->evt.payload))->cmdcode); + TL_SHCI_CMD_DBG_MSG(" status: 0x%02X", ((TL_CcEvt_t*)(p_cmd_rsp_packet->evt.payload))->payload[0]); + if((p_cmd_rsp_packet->evt.plen-4) != 0) { TL_SHCI_CMD_DBG_MSG(" payload:"); - TL_SHCI_CMD_DBG_BUF(p_cmd_packet->cmdserial.cmd.payload, p_cmd_packet->cmdserial.cmd.plen, ""); + TL_SHCI_CMD_DBG_BUF(&((TL_CcEvt_t*)(p_cmd_rsp_packet->evt.payload))->payload[1], p_cmd_rsp_packet->evt.plen-4, ""); } - TL_SHCI_CMD_DBG_MSG("\r\n"); - - TL_SHCI_CMD_DBG_RAW(&p_cmd_packet->cmdserial, p_cmd_packet->cmdserial.cmd.plen+TL_CMD_HDR_SIZE); break; - case TL_MB_SYS_CMD_RSP: - p_cmd_rsp_packet = (TL_EvtSerial_t*)buffer; - switch(p_cmd_rsp_packet->evt.evtcode) - { - case TL_BLEEVT_CC_OPCODE: - TL_SHCI_CMD_DBG_MSG("sys rsp: 0x%02X", p_cmd_rsp_packet->evt.evtcode); - TL_SHCI_CMD_DBG_MSG(" cmd opcode: 0x%02X", ((TL_CcEvt_t*)(p_cmd_rsp_packet->evt.payload))->cmdcode); - TL_SHCI_CMD_DBG_MSG(" status: 0x%02X", ((TL_CcEvt_t*)(p_cmd_rsp_packet->evt.payload))->payload[0]); - if((p_cmd_rsp_packet->evt.plen-4) != 0) - { - TL_SHCI_CMD_DBG_MSG(" payload:"); - TL_SHCI_CMD_DBG_BUF(&((TL_CcEvt_t*)(p_cmd_rsp_packet->evt.payload))->payload[1], p_cmd_rsp_packet->evt.plen-4, ""); - } - break; - - default: - TL_SHCI_CMD_DBG_MSG("unknown sys rsp received: %02X", p_cmd_rsp_packet->evt.evtcode); - break; - } + default: + TL_SHCI_CMD_DBG_MSG("unknown sys rsp received: %02X", p_cmd_rsp_packet->evt.evtcode); + break; + } - TL_SHCI_CMD_DBG_MSG("\r\n"); + TL_SHCI_CMD_DBG_MSG("\r\n"); - TL_SHCI_CMD_DBG_RAW(&p_cmd_rsp_packet->evt, p_cmd_rsp_packet->evt.plen+TL_EVT_HDR_SIZE); - break; + TL_SHCI_CMD_DBG_RAW(&p_cmd_rsp_packet->evt, p_cmd_rsp_packet->evt.plen+TL_EVT_HDR_SIZE); + break; - case TL_MB_SYS_ASYNCH_EVT: - p_evt_packet = (TL_EvtPacket_t*)buffer; - if(p_evt_packet->evtserial.evt.evtcode != TL_BLEEVT_VS_OPCODE) - { - TL_SHCI_EVT_DBG_MSG("unknown sys evt received: %02X", p_evt_packet->evtserial.evt.evtcode); - } - else + case TL_MB_SYS_ASYNCH_EVT: + p_evt_packet = (TL_EvtPacket_t*)buffer; + if(p_evt_packet->evtserial.evt.evtcode != TL_BLEEVT_VS_OPCODE) + { + TL_SHCI_EVT_DBG_MSG("unknown sys evt received: %02X", p_evt_packet->evtserial.evt.evtcode); + } + else + { + TL_SHCI_EVT_DBG_MSG("sys evt: 0x%02X", p_evt_packet->evtserial.evt.evtcode); + TL_SHCI_EVT_DBG_MSG(" subevtcode: 0x%04X", ((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->subevtcode); + if((p_evt_packet->evtserial.evt.plen-2) != 0) { - TL_SHCI_EVT_DBG_MSG("sys evt: 0x%02X", p_evt_packet->evtserial.evt.evtcode); - TL_SHCI_EVT_DBG_MSG(" subevtcode: 0x%04X", ((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->subevtcode); - if((p_evt_packet->evtserial.evt.plen-2) != 0) - { - TL_SHCI_EVT_DBG_MSG(" payload:"); - TL_SHCI_EVT_DBG_BUF(((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload, p_evt_packet->evtserial.evt.plen-2, ""); - } + TL_SHCI_EVT_DBG_MSG(" payload:"); + TL_SHCI_EVT_DBG_BUF(((TL_AsynchEvt_t*)(p_evt_packet->evtserial.evt.payload))->payload, p_evt_packet->evtserial.evt.plen-2, ""); } + } - TL_SHCI_EVT_DBG_MSG("\r\n"); + TL_SHCI_EVT_DBG_MSG("\r\n"); - TL_SHCI_EVT_DBG_RAW(&p_evt_packet->evtserial, p_evt_packet->evtserial.evt.plen+TL_EVT_HDR_SIZE); - break; + TL_SHCI_EVT_DBG_RAW(&p_evt_packet->evtserial, p_evt_packet->evtserial.evt.plen+TL_EVT_HDR_SIZE); + break; - default: - break; + default: + break; } return; diff --git a/src/utility/btct.cpp b/src/utility/btct.cpp index eeb7a8cd..cb67fdf5 100644 --- a/src/utility/btct.cpp +++ b/src/utility/btct.cpp @@ -27,6 +27,7 @@ unsigned char const_Rb[16] = { #define LEN_MAC_KEY 16 void BluetoothCryptoToolbox::printBytes(uint8_t bytes[], uint8_t length){ +#ifdef _BLE_TRACE_ for(int i=0; i0){ Serial.print(", 0x"); @@ -36,7 +37,13 @@ void BluetoothCryptoToolbox::printBytes(uint8_t bytes[], uint8_t length){ Serial.print(bytes[i],HEX); } Serial.print('\n'); +#else + (void)bytes; + (void)length; +#endif } + + int BluetoothCryptoToolbox::f5(uint8_t DHKey[],uint8_t N_master[], uint8_t N_slave[], uint8_t BD_ADDR_master[], uint8_t BD_ADDR_slave[], uint8_t MacKey[], uint8_t LTK[]) { @@ -126,17 +133,17 @@ int BluetoothCryptoToolbox::ah(uint8_t k[16], uint8_t r[3], uint8_t* result) } void BluetoothCryptoToolbox::testAh() { - uint8_t irk[16] = {0xec,0x02,0x34,0xa3,0x57,0xc8,0xad,0x05,0x34,0x10,0x10,0xa6,0x0a,0x39,0x7d,0x9b}; + uint8_t irk[16] = {0xec,0x02,0x34,0xa3,0x57,0xc8,0xad,0x05,0x34,0x10,0x10,0xa6,0x0a,0x39,0x7d,0x9b}; uint8_t expected_final[3] = {0x0d,0xfb,0xaa}; - uint8_t ourResult[3]; ah(irk, expected_final, ourResult); - +#ifdef _BLE_TRACE_ Serial.print("Expected : "); printBytes(&expected_final[3], 3); Serial.print("Actual : "); printBytes(ourResult, 3); +#endif } int BluetoothCryptoToolbox::g2(uint8_t U[], uint8_t V[], uint8_t X[], uint8_t Y[], uint8_t out[4]) @@ -160,19 +167,17 @@ void BluetoothCryptoToolbox::testg2(){ uint8_t X[16] = {0xd5,0xcb,0x84,0x54,0xd1,0x77,0x73,0x3e,0xff,0xff,0xb2,0xec,0x71,0x2b,0xae,0xab}; uint8_t Y[16] = {0xa6,0xe8,0xe7,0xcc,0x25,0xa7,0x5f,0x6e,0x21,0x65,0x83,0xf7,0xff,0x3d,0xc4,0xcf}; uint8_t out[4]; - - - uint32_t expected = 0; + g2(U,V,X,Y,out); uint32_t result = 0; for(int i=0; i<4; i++) result += out[i] << 8*i; - Serial.print("Expected : "); - Serial.println(expected); +#ifdef _BLE_TRACE_ + Serial.println("Expected : 0"); Serial.print("Result : "); Serial.println(result); Serial.println(); - +#endif } void BluetoothCryptoToolbox::AES_CMAC ( unsigned char *key, unsigned char *input, int length, @@ -264,8 +269,10 @@ int BluetoothCryptoToolbox::AES_128(uint8_t* key, uint8_t* data_in, uint8_t* dat int n = 0; int tries = 30; while(HCI.leEncrypt(key,data_in, &status, data_out)!=1&&n