From 005ea488de4f3b59861949a3c703bff8bebcf432 Mon Sep 17 00:00:00 2001 From: Konrad Derda Date: Tue, 10 Sep 2024 18:07:15 +0200 Subject: [PATCH 1/2] pal: switch to multi-advertising BLE API This commit prepares PAL to be used alongside other BLE instances. Signed-off-by: Konrad Derda --- Kconfig | 11 +++ Kconfig.dependencies | 1 + samples/sid_end_device/Kconfig.defconfig | 3 + .../sysbuild/ipc_radio/prj.conf | 5 +- scripts/ci/license.yml | 2 +- subsys/sal/sid_pal/include/sid_ble_advert.h | 7 ++ subsys/sal/sid_pal/src/sid_ble_adapter.c | 35 +++++++ subsys/sal/sid_pal/src/sid_ble_advert.c | 92 +++++++++++++++---- subsys/sal/sid_pal/src/sid_ble_connection.c | 22 ++++- subsys/sal/sid_pal/src/sid_storage.c | 3 +- 10 files changed, 155 insertions(+), 26 deletions(-) diff --git a/Kconfig b/Kconfig index 2dd48b1f12..9cf5440c54 100644 --- a/Kconfig +++ b/Kconfig @@ -14,6 +14,9 @@ if SIDEWALK config MAIN_STACK_SIZE default 2048 +config BT_ID_MAX + default 2 + config SIDEWALK_SUBGHZ_SUPPORT bool "Enable Sub-GHz link type support in Sidewalk libraries" default y @@ -142,6 +145,14 @@ config SIDEWALK_BLE_ADV_INT_SLOW range 20 10240 default 1000 +config SIDEWALK_BLE_ID + int "Set Sidewalk's BLE identity identifier" + range 1 255 + default 1 + help + Specify identifier used to distinguish Sidewalk related BLE + events. + config SIDEWALK_BLE_ADV_INT_TRANSITION int "Duration of fast advertisement after sid_start in seconds" range 1 2147483647 diff --git a/Kconfig.dependencies b/Kconfig.dependencies index 2c3eb0014c..519592afb2 100644 --- a/Kconfig.dependencies +++ b/Kconfig.dependencies @@ -34,6 +34,7 @@ config SIDEWALK_BLE imply BT_CTLR_CONN_RSSI imply BT_CTLR_TX_PWR_DYNAMIC_CONTROL imply BT_CTLR_ADVANCED_FEATURES + imply BT_EXT_ADV help Sidewalk Bluetooth Low Energy (BLE) module diff --git a/samples/sid_end_device/Kconfig.defconfig b/samples/sid_end_device/Kconfig.defconfig index 6620cd88a6..71fb2c1f02 100644 --- a/samples/sid_end_device/Kconfig.defconfig +++ b/samples/sid_end_device/Kconfig.defconfig @@ -42,6 +42,9 @@ config BT_PERIPHERAL_PREF_LATENCY config BT_PERIPHERAL_PREF_TIMEOUT default 400 +config BT_EXT_ADV_MAX_ADV_SET + default 2 + config NVS_LOOKUP_CACHE_SIZE default 256 if NVS diff --git a/samples/sid_end_device/sysbuild/ipc_radio/prj.conf b/samples/sid_end_device/sysbuild/ipc_radio/prj.conf index 654a36b22a..459a998e25 100644 --- a/samples/sid_end_device/sysbuild/ipc_radio/prj.conf +++ b/samples/sid_end_device/sysbuild/ipc_radio/prj.conf @@ -13,11 +13,14 @@ CONFIG_BT_HCI_RAW=y CONFIG_BT_CTLR_ASSERT_HANDLER=y CONFIG_BT_PERIPHERAL=y CONFIG_BT_CENTRAL=n -CONFIG_BT_MAX_CONN=1 +CONFIG_BT_MAX_CONN=2 CONFIG_BT_BUF_ACL_RX_SIZE=502 CONFIG_BT_BUF_ACL_TX_SIZE=251 CONFIG_BT_CTLR_DATA_LENGTH_MAX=251 +CONFIG_BT_CTLR_ADV_SET=3 +CONFIG_BT_CTLR_ADV_EXT=y + # IPC CONFIG_IPC_SERVICE=y CONFIG_MBOX=y diff --git a/scripts/ci/license.yml b/scripts/ci/license.yml index 547bf5ad24..d8a67d4cff 100644 --- a/scripts/ci/license.yml +++ b/scripts/ci/license.yml @@ -41,7 +41,7 @@ licenses: - sidewalk.*/subsys/config/common/src/.*(c|h)$ - sidewalk.*/subsys/hal(/.*)+h$ - sidewalk.*/subsys/hal/src/memory.c$ - - sidewalk.*/subsys/sal/common/.*(c|h)$ + - sidewalk.*/subsys/sal/common/.*/sid_.*(c|h)$ - sidewalk.*/subsys/semtech/include/semtech_radio_ifc.h$ - sidewalk.*/tools/.* - sidewalk.*/tests/validation/storage_kv/.*(c|h)$ diff --git a/subsys/sal/sid_pal/include/sid_ble_advert.h b/subsys/sal/sid_pal/include/sid_ble_advert.h index 4e83ccb11b..27c343abce 100644 --- a/subsys/sal/sid_pal/include/sid_ble_advert.h +++ b/subsys/sal/sid_pal/include/sid_ble_advert.h @@ -9,6 +9,13 @@ #include +/** + * @brief Initialize Bluetooth Advertising. + * + * @return Zero on success or (negative) error code on failure. + */ +int sid_ble_advert_init(void); + /** * @brief Start Bluetooth advertising. * diff --git a/subsys/sal/sid_pal/src/sid_ble_adapter.c b/subsys/sal/sid_pal/src/sid_ble_adapter.c index c4ffad93d8..a789ec3696 100644 --- a/subsys/sal/sid_pal/src/sid_ble_adapter.c +++ b/subsys/sal/sid_pal/src/sid_ble_adapter.c @@ -213,6 +213,29 @@ static sid_error_t ble_adapter_set_tx_pwr(int8_t tx_power) return SID_ERROR_NONE; } +static int create_ble_id(void) +{ + int ret; + size_t count; + + BUILD_ASSERT(CONFIG_SIDEWALK_BLE_ID < CONFIG_BT_ID_MAX, "CONFIG_BT_ID_MAX is too small."); + + /* Check if Bluetooth identites weren't already created. */ + bt_id_get(NULL, &count); + if (count > CONFIG_SIDEWALK_BLE_ID) { + return 0; + } + + do { + ret = bt_id_create(NULL, NULL); + if (ret < 0) { + return ret; + } + } while (ret != CONFIG_SIDEWALK_BLE_ID); + + return 0; +} + static sid_error_t ble_adapter_init(const sid_ble_config_t *cfg) { LOG_DBG("Sidewalk -> BLE"); @@ -231,6 +254,18 @@ static sid_error_t ble_adapter_init(const sid_ble_config_t *cfg) return SID_ERROR_GENERIC; } + err_code = create_ble_id(); + if (err_code) { + LOG_ERR("BT ID init failed (err: %d)", err_code); + return SID_ERROR_GENERIC; + } + + err_code = sid_ble_advert_init(); + if (err_code) { + LOG_ERR("BT Advertisement failed (err: %d)", err_code); + return SID_ERROR_GENERIC; + } + sid_ble_conn_init(); return SID_ERROR_NONE; diff --git a/subsys/sal/sid_pal/src/sid_ble_advert.c b/subsys/sal/sid_pal/src/sid_ble_advert.c index 438631fee8..a0a64577fc 100644 --- a/subsys/sal/sid_pal/src/sid_ble_advert.c +++ b/subsys/sal/sid_pal/src/sid_ble_advert.c @@ -36,17 +36,24 @@ LOG_MODULE_REGISTER(sid_ble_advert, CONFIG_SIDEWALK_BLE_ADAPTER_LOG_LEVEL); #endif /* Advertising parameters. */ -#define AMA_ADV_PARAM_FAST \ - BT_LE_ADV_PARAM(AMA_ADV_OPTIONS, MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST), \ - MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST + \ - CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION), \ - NULL) - -#define AMA_ADV_PARAM_SLOW \ - BT_LE_ADV_PARAM(AMA_ADV_OPTIONS, MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW), \ - MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW + \ - CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION), \ - NULL) +static struct bt_le_adv_param adv_param_fast = { + .id = CONFIG_SIDEWALK_BLE_ID, + .options = (AMA_ADV_OPTIONS), + .interval_min = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST), + .interval_max = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_FAST + + CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION), +}; + +static struct bt_le_adv_param adv_param_slow = { + .id = CONFIG_SIDEWALK_BLE_ID, + .options = (AMA_ADV_OPTIONS), + .interval_min = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW), + .interval_max = MS_TO_INTERVAL_VAL(CONFIG_SIDEWALK_BLE_ADV_INT_SLOW + + CONFIG_SIDEWALK_BLE_ADV_INT_PRECISION), +}; + +static struct bt_le_ext_adv *adv_set_fast; +static struct bt_le_ext_adv *adv_set_slow; /** * @brief Advertising data items values size in bytes. @@ -63,7 +70,7 @@ LOG_MODULE_REGISTER(sid_ble_advert, CONFIG_SIDEWALK_BLE_ADAPTER_LOG_LEVEL); enum adv_data_items { ADV_DATA_FLAGS, ADV_DATA_SERVICES, ADV_DATA_MANUF_DATA }; -typedef enum { BLE_ADV_DISABLE, BLE_ADV_ENABLE } sid_ble_adv_state_t; +typedef enum { BLE_ADV_DISABLE, BLE_ADV_FAST, BLE_ADV_SLOW } sid_ble_adv_state_t; static void change_advertisement_interval(struct k_work *); K_WORK_DELAYABLE_DEFINE(change_adv_work, change_advertisement_interval); @@ -103,28 +110,68 @@ static uint8_t advert_manuf_data_copy(uint8_t *data, uint8_t data_len) static void change_advertisement_interval(struct k_work *work) { ARG_UNUSED(work); - if (BLE_ADV_ENABLE == atomic_get(&adv_state)) { - if (bt_le_adv_stop()) { + + struct bt_le_ext_adv_start_param ext_adv_start_param = { 0 }; + + if (BLE_ADV_FAST == atomic_get(&adv_state)) { + if (bt_le_ext_adv_stop(adv_set_fast)) { + atomic_set(&adv_state, BLE_ADV_DISABLE); + return; + } + + if (bt_le_ext_adv_set_data(adv_set_slow, adv_data, ARRAY_SIZE(adv_data), NULL, 0)) { atomic_set(&adv_state, BLE_ADV_DISABLE); return; } - if (bt_le_adv_start(AMA_ADV_PARAM_SLOW, adv_data, ARRAY_SIZE(adv_data), NULL, 0)) { + + if (bt_le_ext_adv_start(adv_set_slow, &ext_adv_start_param)) { atomic_set(&adv_state, BLE_ADV_DISABLE); return; } + + atomic_set(&adv_state, BLE_ADV_SLOW); + LOG_DBG("BLE -> BLE"); } } +int sid_ble_advert_init(void) +{ + int ret; + + ret = bt_le_ext_adv_create(&adv_param_fast, NULL, &adv_set_fast); + if (ret) { + LOG_ERR("Failed to create fast advertising set"); + return ret; + } + + ret = bt_le_ext_adv_create(&adv_param_slow, NULL, &adv_set_slow); + if (ret) { + LOG_ERR("Failed to create slow advertising set"); + return ret; + } + + return 0; +} + int sid_ble_advert_start(void) { k_work_reschedule(&change_adv_work, K_SECONDS(CONFIG_SIDEWALK_BLE_ADV_INT_TRANSITION)); - int err = bt_le_adv_start(AMA_ADV_PARAM_FAST, adv_data, ARRAY_SIZE(adv_data), NULL, 0); + struct bt_le_ext_adv_start_param ext_adv_start_param = { 0 }; + int err; + + err = bt_le_ext_adv_set_data(adv_set_fast, adv_data, ARRAY_SIZE(adv_data), NULL, 0); if (err) { return err; } - atomic_set(&adv_state, BLE_ADV_ENABLE); + + err = bt_le_ext_adv_start(adv_set_fast, &ext_adv_start_param); + if (err) { + return err; + } + + atomic_set(&adv_state, BLE_ADV_FAST); return err; } @@ -132,7 +179,8 @@ int sid_ble_advert_start(void) int sid_ble_advert_stop(void) { k_work_cancel_delayable(&change_adv_work); - int err = bt_le_adv_stop(); + int err = bt_le_ext_adv_stop(atomic_get(&adv_state) == BLE_ADV_FAST ? adv_set_fast : + adv_set_slow); if (0 == err) { atomic_set(&adv_state, BLE_ADV_DISABLE); @@ -143,6 +191,8 @@ int sid_ble_advert_stop(void) int sid_ble_advert_update(uint8_t *data, uint8_t data_len) { + sid_ble_adv_state_t state = atomic_get(&adv_state); + if (!data || 0 == data_len) { return -EINVAL; } @@ -151,8 +201,10 @@ int sid_ble_advert_update(uint8_t *data, uint8_t data_len) int err = 0; - if (BLE_ADV_ENABLE == atomic_get(&adv_state)) { - err = bt_le_adv_update_data(adv_data, ARRAY_SIZE(adv_data), NULL, 0); + if (BLE_ADV_DISABLE != state) { + /* Update currently advertised set, the other one will be set on start/transition */ + err = bt_le_ext_adv_set_data((state == BLE_ADV_FAST ? adv_set_fast : adv_set_slow), + adv_data, ARRAY_SIZE(adv_data), NULL, 0); } return err; diff --git a/subsys/sal/sid_pal/src/sid_ble_connection.c b/subsys/sal/sid_pal/src/sid_ble_connection.c index 2ad4edbe5c..5202bef4ed 100644 --- a/subsys/sal/sid_pal/src/sid_ble_connection.c +++ b/subsys/sal/sid_pal/src/sid_ble_connection.c @@ -33,6 +33,16 @@ static struct bt_conn_cb conn_callbacks = { static struct bt_gatt_cb gatt_callbacks = { .att_mtu_updated = ble_mtu_cb }; +static bool should_handle_event(struct bt_conn *conn) { + struct bt_conn_info conn_info; + + if (!conn || bt_conn_get_info(conn, &conn_info) || conn_info.id != CONFIG_SIDEWALK_BLE_ID) { + return false; + } + + return true; +} + /** * @brief The function is called when a new connection is established. * @@ -41,12 +51,18 @@ static struct bt_gatt_cb gatt_callbacks = { .att_mtu_updated = ble_mtu_cb }; */ static void ble_connect_cb(struct bt_conn *conn, uint8_t err) { + const bt_addr_le_t *bt_addr_le; + + if (!should_handle_event(conn)) { + return; + } + if (err) { LOG_ERR("Connection failed (err %u)\n", err); return; } - const bt_addr_le_t *bt_addr_le = bt_conn_get_dst(conn); + bt_addr_le = bt_conn_get_dst(conn); if (bt_addr_le) { memcpy(conn_params.addr, bt_addr_le->a.val, BT_ADDR_SIZE); @@ -72,10 +88,10 @@ static void ble_connect_cb(struct bt_conn *conn, uint8_t err) */ static void ble_disconnect_cb(struct bt_conn *conn, uint8_t reason) { - if (!conn || conn_params.conn != conn) { - LOG_WRN("Unknow connection"); + if (!should_handle_event(conn)) { return; } + sid_ble_adapter_conn_disconnected((const uint8_t *)conn_params.addr); k_mutex_lock(&bt_conn_mutex, K_FOREVER); diff --git a/subsys/sal/sid_pal/src/sid_storage.c b/subsys/sal/sid_pal/src/sid_storage.c index e5dc826e8d..3619a61abb 100644 --- a/subsys/sal/sid_pal/src/sid_storage.c +++ b/subsys/sal/sid_pal/src/sid_storage.c @@ -219,7 +219,8 @@ sid_error_t sid_pal_storage_kv_record_delete(uint16_t group, uint16_t key) #ifdef CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE psa_key_id_t key_id = storage2key_id(group, key); if (SID_CRYPTO_KEYS_ID_IS_SIDEWALK_KEY(key_id)) { - return storage_key_delete_secure(key_id) ? SID_ERROR_NONE : SID_ERROR_STORAGE_ERASE_FAIL; + return storage_key_delete_secure(key_id) ? SID_ERROR_NONE : + SID_ERROR_STORAGE_ERASE_FAIL; } #endif /* CONFIG_SIDEWALK_CRYPTO_PSA_KEY_STORAGE */ From 5b52afe25eae572fea3fcb9723e2885f28af1f04 Mon Sep 17 00:00:00 2001 From: Konrad Derda Date: Mon, 16 Sep 2024 15:34:18 +0200 Subject: [PATCH 2/2] tests: update tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit update unit tests Co-authored-by: Robert Gałat Co-authored-by: Konrad Derda Signed-off-by: Konrad Derda Signed-off-by: Robert Gałat --- subsys/sal/sid_pal/src/sid_ble_adapter.c | 4 +- subsys/sal/sid_pal/src/sid_ble_connection.c | 4 +- tests/unit_tests/pal_ble_adapter/Kconfig | 6 ++ tests/unit_tests/pal_ble_adapter/src/main.c | 27 +++++++- tests/unit_tests/sid_ble_advert/Kconfig | 6 +- tests/unit_tests/sid_ble_advert/src/main.c | 69 ++++++++++--------- tests/unit_tests/sid_ble_connection/Kconfig | 6 ++ .../unit_tests/sid_ble_connection/src/main.c | 22 +++++- 8 files changed, 103 insertions(+), 41 deletions(-) diff --git a/subsys/sal/sid_pal/src/sid_ble_adapter.c b/subsys/sal/sid_pal/src/sid_ble_adapter.c index a789ec3696..13444cc036 100644 --- a/subsys/sal/sid_pal/src/sid_ble_adapter.c +++ b/subsys/sal/sid_pal/src/sid_ble_adapter.c @@ -216,7 +216,7 @@ static sid_error_t ble_adapter_set_tx_pwr(int8_t tx_power) static int create_ble_id(void) { int ret; - size_t count; + size_t count = 0; BUILD_ASSERT(CONFIG_SIDEWALK_BLE_ID < CONFIG_BT_ID_MAX, "CONFIG_BT_ID_MAX is too small."); @@ -440,6 +440,8 @@ static sid_error_t ble_adapter_deinit(void) LOG_DBG("Sidewalk -> BLE"); sid_ble_conn_deinit(); + bt_id_delete(CONFIG_SIDEWALK_BLE_ID); + bt_id_reset(CONFIG_SIDEWALK_BLE_ID, NULL, NULL); int err = bt_disable(); if (err) { diff --git a/subsys/sal/sid_pal/src/sid_ble_connection.c b/subsys/sal/sid_pal/src/sid_ble_connection.c index 5202bef4ed..e8a9cb26a4 100644 --- a/subsys/sal/sid_pal/src/sid_ble_connection.c +++ b/subsys/sal/sid_pal/src/sid_ble_connection.c @@ -34,7 +34,7 @@ static struct bt_conn_cb conn_callbacks = { static struct bt_gatt_cb gatt_callbacks = { .att_mtu_updated = ble_mtu_cb }; static bool should_handle_event(struct bt_conn *conn) { - struct bt_conn_info conn_info; + struct bt_conn_info conn_info = {}; if (!conn || bt_conn_get_info(conn, &conn_info) || conn_info.id != CONFIG_SIDEWALK_BLE_ID) { return false; @@ -88,7 +88,7 @@ static void ble_connect_cb(struct bt_conn *conn, uint8_t err) */ static void ble_disconnect_cb(struct bt_conn *conn, uint8_t reason) { - if (!should_handle_event(conn)) { + if (!should_handle_event(conn) || conn_params.conn != conn) { return; } diff --git a/tests/unit_tests/pal_ble_adapter/Kconfig b/tests/unit_tests/pal_ble_adapter/Kconfig index 062cbec5f7..67222c2460 100644 --- a/tests/unit_tests/pal_ble_adapter/Kconfig +++ b/tests/unit_tests/pal_ble_adapter/Kconfig @@ -31,4 +31,10 @@ config SIDEWALK_LOG_LEVEL config SIDEWALK_BLE_ADAPTER_LOG_LEVEL default 0 +config SIDEWALK_BLE_ID + default 1 + +config BT_ID_MAX + default 2 + source "Kconfig.zephyr" diff --git a/tests/unit_tests/pal_ble_adapter/src/main.c b/tests/unit_tests/pal_ble_adapter/src/main.c index 72bfbcc60b..a608686bd0 100644 --- a/tests/unit_tests/pal_ble_adapter/src/main.c +++ b/tests/unit_tests/pal_ble_adapter/src/main.c @@ -22,7 +22,11 @@ #include DEFINE_FFF_GLOBALS; - +FAKE_VALUE_FUNC(int, bt_le_ext_adv_stop, struct bt_le_ext_adv *); +FAKE_VALUE_FUNC(int, bt_le_ext_adv_set_data, struct bt_le_ext_adv *, const struct bt_data *, size_t, + const struct bt_data *, size_t); +FAKE_VALUE_FUNC(int, bt_le_ext_adv_start, struct bt_le_ext_adv *, + const struct bt_le_ext_adv_start_param *); FAKE_VALUE_FUNC(int, bt_enable, bt_ready_cb_t); FAKE_VALUE_FUNC(int, bt_disable); FAKE_VALUE_FUNC(int, bt_le_adv_start, const struct bt_le_adv_param *, const struct bt_data *, @@ -41,6 +45,10 @@ FAKE_VALUE_FUNC(ssize_t, bt_gatt_attr_read_ccc, struct bt_conn *, const struct b void *, uint16_t, uint16_t); FAKE_VALUE_FUNC(ssize_t, bt_gatt_attr_write_ccc, struct bt_conn *, const struct bt_gatt_attr *, const void *, uint16_t, uint16_t, uint8_t); +FAKE_VOID_FUNC(bt_id_get, bt_addr_le_t *, size_t *); +FAKE_VALUE_FUNC(int, bt_id_create, bt_addr_le_t *, uint8_t *); +FAKE_VALUE_FUNC(int, bt_id_reset, uint8_t, bt_addr_le_t *, uint8_t *); +FAKE_VALUE_FUNC(int, bt_id_delete, uint8_t); FAKE_VALUE_FUNC(struct net_buf *, bt_hci_cmd_create, uint16_t, uint8_t); FAKE_VALUE_FUNC(int, bt_hci_cmd_send_sync, uint16_t, struct net_buf *, struct net_buf **); @@ -48,6 +56,7 @@ FAKE_VALUE_FUNC(int, bt_hci_cmd_send_sync, uint16_t, struct net_buf *, struct ne FAKE_VALUE_FUNC(int, bt_hci_get_conn_handle, const struct bt_conn *, uint16_t *); FAKE_VALUE_FUNC(void *, net_buf_simple_add, struct net_buf_simple *, size_t); FAKE_VOID_FUNC(net_buf_unref, struct net_buf *); +FAKE_VALUE_FUNC(int, bt_conn_get_info, const struct bt_conn *, struct bt_conn_info *); #define FFF_FAKES_LIST(FAKE) \ FAKE(bt_enable) \ @@ -58,6 +67,10 @@ FAKE_VOID_FUNC(net_buf_unref, struct net_buf *); FAKE(bt_conn_ref) \ FAKE(bt_conn_unref) \ FAKE(bt_conn_get_dst) \ + FAKE(bt_id_get) \ + FAKE(bt_id_create) \ + FAKE(bt_id_reset) \ + FAKE(bt_id_delete) \ FAKE(bt_gatt_attr_read_service) \ FAKE(bt_gatt_attr_read_chrc) \ FAKE(bt_gatt_attr_read_ccc) \ @@ -66,7 +79,11 @@ FAKE_VOID_FUNC(net_buf_unref, struct net_buf *); FAKE(bt_hci_cmd_send_sync) \ FAKE(bt_hci_get_conn_handle) \ FAKE(net_buf_simple_add) \ - FAKE(net_buf_unref) + FAKE(net_buf_unref) \ + FAKE(bt_le_ext_adv_stop) \ + FAKE(bt_le_ext_adv_set_data) \ + FAKE(bt_le_ext_adv_start) \ + FAKE(bt_conn_get_info) #define ESUCCESS (0) #define FAKE_SERVICE (9) @@ -91,11 +108,13 @@ void setUp(void) FFF_RESET_HISTORY(); memset(&data_cb_test, 0x00, sizeof(data_cb_test)); cmock_sid_ble_adapter_callbacks_Init(); + cmock_sid_ble_advert_Init(); } void tearDown(void) { cmock_sid_ble_adapter_callbacks_Verify(); + cmock_sid_ble_advert_Verify(); } static void ble_data_callback(sid_ble_cfg_service_identifier_t id, uint8_t *data, uint16_t length) @@ -155,7 +174,9 @@ void test_sid_pal_ble_adapter_init(void) TEST_ASSERT_EQUAL(SID_ERROR_NONE, sid_pal_ble_adapter_create(&p_test_ble_ifc)); + bt_id_create_fake.return_val = CONFIG_SIDEWALK_BLE_ID; __cmock_sid_ble_conn_init_Expect(); + __cmock_sid_ble_advert_init_IgnoreAndReturn(0); TEST_ASSERT_EQUAL(SID_ERROR_NONE, p_test_ble_ifc->init(&test_ble_cfg)); __cmock_sid_ble_conn_init_Expect(); @@ -172,6 +193,8 @@ void test_sid_pal_ble_adapter_deinit(void) TEST_ASSERT_EQUAL(SID_ERROR_NONE, sid_pal_ble_adapter_create(&p_test_ble_ifc)); __cmock_settings_load_ExpectAndReturn(ESUCCESS); __cmock_sid_ble_conn_init_Expect(); + bt_id_create_fake.return_val = CONFIG_SIDEWALK_BLE_ID; + __cmock_sid_ble_advert_init_IgnoreAndReturn(0); TEST_ASSERT_EQUAL(SID_ERROR_NONE, p_test_ble_ifc->init(&test_ble_cfg)); __cmock_sid_ble_conn_deinit_Expect(); diff --git a/tests/unit_tests/sid_ble_advert/Kconfig b/tests/unit_tests/sid_ble_advert/Kconfig index 5b1ee8f4a4..296292f60e 100644 --- a/tests/unit_tests/sid_ble_advert/Kconfig +++ b/tests/unit_tests/sid_ble_advert/Kconfig @@ -20,7 +20,7 @@ config SIDEWALK_BLE_ADV_INT_PRECISION config SIDEWALK_BLE_ADV_INT_FAST int "test value for Sidewalk configuration macro" default 160 - + config SIDEWALK_BLE_ADV_INT_SLOW int "test value for Sidewalk configuration macro" default 1000 @@ -29,4 +29,8 @@ config SIDEWALK_BLE_ADV_INT_TRANSITION int "test value for Sidewalk configuration macro" default 30 +config SIDEWALK_BLE_ID + int "test value for Sidewalk configuration" + default 1 + source "Kconfig.zephyr" diff --git a/tests/unit_tests/sid_ble_advert/src/main.c b/tests/unit_tests/sid_ble_advert/src/main.c index d77d19dd91..73cebe7ef5 100644 --- a/tests/unit_tests/sid_ble_advert/src/main.c +++ b/tests/unit_tests/sid_ble_advert/src/main.c @@ -15,16 +15,16 @@ DEFINE_FFF_GLOBALS; -FAKE_VALUE_FUNC(int, bt_le_adv_start, const struct bt_le_adv_param *, const struct bt_data *, - size_t, const struct bt_data *, size_t); -FAKE_VALUE_FUNC(int, bt_le_adv_stop); -FAKE_VALUE_FUNC(int, bt_le_adv_update_data, const struct bt_data *, size_t, const struct bt_data *, - size_t); +FAKE_VALUE_FUNC(int, bt_le_ext_adv_start, struct bt_le_ext_adv *, + const struct bt_le_ext_adv_start_param *); +FAKE_VALUE_FUNC(int, bt_le_ext_adv_stop, struct bt_le_ext_adv *); +FAKE_VALUE_FUNC(int, bt_le_ext_adv_set_data, struct bt_le_ext_adv *, const struct bt_data *, size_t, + const struct bt_data *, size_t); #define FFF_FAKES_LIST(FAKE) \ - FAKE(bt_le_adv_start) \ - FAKE(bt_le_adv_stop) \ - FAKE(bt_le_adv_update_data) + FAKE(bt_le_ext_adv_start) \ + FAKE(bt_le_ext_adv_stop) \ + FAKE(bt_le_ext_adv_set_data) #define ESUCCESS (0) #define TEST_BUFFER_LEN (100) @@ -40,30 +40,30 @@ void test_sid_ble_advert_start(void) { size_t adv_start_call_count = 0; - bt_le_adv_start_fake.return_val = ESUCCESS; + bt_le_ext_adv_start_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_start()); adv_start_call_count++; - TEST_ASSERT_EQUAL(adv_start_call_count, bt_le_adv_start_fake.call_count); + TEST_ASSERT_EQUAL(adv_start_call_count, bt_le_ext_adv_start_fake.call_count); - bt_le_adv_start_fake.return_val = -ENOENT; + bt_le_ext_adv_start_fake.return_val = -ENOENT; TEST_ASSERT_EQUAL(-ENOENT, sid_ble_advert_start()); adv_start_call_count++; - TEST_ASSERT_EQUAL(adv_start_call_count, bt_le_adv_start_fake.call_count); + TEST_ASSERT_EQUAL(adv_start_call_count, bt_le_ext_adv_start_fake.call_count); } void test_sid_ble_advert_stop(void) { size_t adv_stop_call_count = 0; - bt_le_adv_stop_fake.return_val = ESUCCESS; + bt_le_ext_adv_stop_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_stop()); adv_stop_call_count++; - TEST_ASSERT_EQUAL(adv_stop_call_count, bt_le_adv_stop_fake.call_count); + TEST_ASSERT_EQUAL(adv_stop_call_count, bt_le_ext_adv_stop_fake.call_count); - bt_le_adv_stop_fake.return_val = -ENOENT; + bt_le_ext_adv_stop_fake.return_val = -ENOENT; TEST_ASSERT_EQUAL(-ENOENT, sid_ble_advert_stop()); adv_stop_call_count++; - TEST_ASSERT_EQUAL(adv_stop_call_count, bt_le_adv_stop_fake.call_count); + TEST_ASSERT_EQUAL(adv_stop_call_count, bt_le_ext_adv_stop_fake.call_count); } void test_sid_ble_advert_update(void) @@ -71,19 +71,20 @@ void test_sid_ble_advert_update(void) uint8_t test_data[] = "Lorem ipsum."; size_t adv_update_call_count = 0; - bt_le_adv_start_fake.return_val = ESUCCESS; - bt_le_adv_update_data_fake.return_val = ESUCCESS; + bt_le_ext_adv_start_fake.return_val = ESUCCESS; + bt_le_ext_adv_set_data_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_start()); + adv_update_call_count++; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_update(test_data, sizeof(test_data))); adv_update_call_count++; - TEST_ASSERT_EQUAL(adv_update_call_count, bt_le_adv_update_data_fake.call_count); + TEST_ASSERT_EQUAL(adv_update_call_count, bt_le_ext_adv_set_data_fake.call_count); - bt_le_adv_update_data_fake.return_val = -ENOENT; + bt_le_ext_adv_set_data_fake.return_val = -ENOENT; TEST_ASSERT_EQUAL(-ENOENT, sid_ble_advert_update(test_data, sizeof(test_data))); adv_update_call_count++; - TEST_ASSERT_EQUAL(adv_update_call_count, bt_le_adv_update_data_fake.call_count); + TEST_ASSERT_EQUAL(adv_update_call_count, bt_le_ext_adv_set_data_fake.call_count); - bt_le_adv_update_data_fake.return_val = ESUCCESS; + bt_le_ext_adv_set_data_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(-EINVAL, sid_ble_advert_update(NULL, sizeof(test_data))); TEST_ASSERT_EQUAL(-EINVAL, sid_ble_advert_update(test_data, 0)); } @@ -94,12 +95,12 @@ void test_sid_ble_advert_update_in_every_state(void) TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_update(test_data, sizeof(test_data))); - bt_le_adv_start_fake.return_val = ESUCCESS; - bt_le_adv_update_data_fake.return_val = ESUCCESS; + bt_le_ext_adv_start_fake.return_val = ESUCCESS; + bt_le_ext_adv_set_data_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_start()); TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_update(test_data, sizeof(test_data))); - bt_le_adv_stop_fake.return_val = ESUCCESS; + bt_le_ext_adv_stop_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_stop()); TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_update(test_data, sizeof(test_data))); } @@ -130,17 +131,17 @@ void test_sid_ble_advert_update_before_start(void) size_t advert_data_size; bool found; - bt_le_adv_stop_fake.return_val = ESUCCESS; + bt_le_ext_adv_stop_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_stop()); - bt_le_adv_update_data_fake.return_val = ESUCCESS; + bt_le_ext_adv_set_data_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_update(test_data, sizeof(test_data))); - bt_le_adv_start_fake.return_val = ESUCCESS; + bt_le_ext_adv_start_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_start()); - advert_data = bt_le_adv_start_fake.arg1_val; - advert_data_size = bt_le_adv_start_fake.arg2_val; + advert_data = bt_le_ext_adv_set_data_fake.arg1_val; + advert_data_size = bt_le_ext_adv_set_data_fake.arg2_val; TEST_ASSERT_NOT_NULL(advert_data); TEST_ASSERT_GREATER_THAN_size_t(0, advert_data_size); @@ -159,13 +160,13 @@ void check_sid_ble_advert_update(uint8_t *data, uint8_t data_len) size_t advert_data_size; bool found; - bt_le_adv_start_fake.return_val = ESUCCESS; - bt_le_adv_update_data_fake.return_val = ESUCCESS; + bt_le_ext_adv_start_fake.return_val = ESUCCESS; + bt_le_ext_adv_set_data_fake.return_val = ESUCCESS; TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_start()); TEST_ASSERT_EQUAL(ESUCCESS, sid_ble_advert_update(data, data_len)); - advert_data = bt_le_adv_update_data_fake.arg0_val; - advert_data_size = bt_le_adv_update_data_fake.arg1_val; + advert_data = bt_le_ext_adv_set_data_fake.arg1_val; + advert_data_size = bt_le_ext_adv_set_data_fake.arg2_val; TEST_ASSERT_NOT_NULL(advert_data); TEST_ASSERT_GREATER_THAN_size_t(0, advert_data_size); diff --git a/tests/unit_tests/sid_ble_connection/Kconfig b/tests/unit_tests/sid_ble_connection/Kconfig index 50ffb0aeed..081b348335 100644 --- a/tests/unit_tests/sid_ble_connection/Kconfig +++ b/tests/unit_tests/sid_ble_connection/Kconfig @@ -12,4 +12,10 @@ config SIDEWALK_LOG_LEVEL config SIDEWALK_BLE_ADAPTER_LOG_LEVEL default 0 +config SIDEWALK_BLE_ID + default 1 + +config BT_ID_MAX + default 2 + source "Kconfig.zephyr" diff --git a/tests/unit_tests/sid_ble_connection/src/main.c b/tests/unit_tests/sid_ble_connection/src/main.c index d119a2ed07..bfcaf0d77d 100644 --- a/tests/unit_tests/sid_ble_connection/src/main.c +++ b/tests/unit_tests/sid_ble_connection/src/main.c @@ -24,6 +24,7 @@ FAKE_VALUE_FUNC(struct bt_conn *, bt_conn_ref, struct bt_conn *); FAKE_VOID_FUNC(bt_conn_unref, struct bt_conn *); FAKE_VALUE_FUNC(const bt_addr_le_t *, bt_conn_get_dst, const struct bt_conn *); FAKE_VALUE_FUNC(int, bt_conn_disconnect, struct bt_conn *, uint8_t); +FAKE_VALUE_FUNC(int,bt_conn_get_info,const struct bt_conn *, struct bt_conn_info *); #define FFF_FAKES_LIST(FAKE) \ FAKE(bt_conn_cb_register) \ @@ -31,7 +32,8 @@ FAKE_VALUE_FUNC(int, bt_conn_disconnect, struct bt_conn *, uint8_t); FAKE(bt_conn_ref) \ FAKE(bt_conn_unref) \ FAKE(bt_conn_get_dst) \ - FAKE(bt_conn_disconnect) + FAKE(bt_conn_disconnect)\ + FAKE(bt_conn_get_info) #define CONNECTED (true) #define DISCONNECTED (false) @@ -102,6 +104,11 @@ void test_sid_ble_conn_params_get(void) TEST_ASSERT_NULL(params); } +int bt_conn_get_info_fake1(const struct bt_conn * a, struct bt_conn_info * b){ + b->id = CONFIG_SIDEWALK_BLE_ID; + return 0; +} + void test_sid_ble_conn_positive(void) { uint8_t test_no_error = BT_HCI_ERR_SUCCESS; @@ -115,6 +122,9 @@ void test_sid_ble_conn_positive(void) bt_conn_get_dst_fake.return_val = &test_addr; bt_conn_ref_fake.return_val = &test_conn; + int (*custom_fakes[])(const struct bt_conn *, struct bt_conn_info *) = {bt_conn_get_info_fake1}; + SET_CUSTOM_FAKE_SEQ(bt_conn_get_info, custom_fakes, 1); + sid_ble_conn_deinit(); sid_ble_conn_init(); @@ -143,6 +153,8 @@ void test_sid_ble_set_conn_cb_positive(void) bt_conn_get_dst_fake.return_val = &test_addr; bt_conn_ref_fake.return_val = &test_conn; + int (*custom_fakes[])(const struct bt_conn *, struct bt_conn_info *) = {bt_conn_get_info_fake1}; + SET_CUSTOM_FAKE_SEQ(bt_conn_get_info, custom_fakes, 1); sid_ble_conn_init(); __cmock_sid_ble_adapter_conn_connected_StubWithCallback(connection_callback); @@ -173,6 +185,8 @@ void test_sid_ble_conn_cb_set_call_count(void) bt_conn_get_dst_fake.return_val = &test_addr; bt_conn_ref_fake.return_val = &test_conn; + int (*custom_fakes[])(const struct bt_conn *, struct bt_conn_info *) = {bt_conn_get_info_fake1}; + SET_CUSTOM_FAKE_SEQ(bt_conn_get_info, custom_fakes, 1); sid_ble_conn_init(); __cmock_sid_ble_adapter_conn_connected_StubWithCallback(connection_callback); @@ -208,6 +222,8 @@ void test_sid_ble_disconnected_wrong_conn(void) bt_conn_get_dst_fake.return_val = &test_addr; bt_conn_ref_fake.return_val = &test_conn; + int (*custom_fakes[])(const struct bt_conn *, struct bt_conn_info *) = {bt_conn_get_info_fake1}; + SET_CUSTOM_FAKE_SEQ(bt_conn_get_info, custom_fakes, 1); sid_ble_conn_init(); __cmock_sid_ble_adapter_conn_connected_StubWithCallback(connection_callback); @@ -233,6 +249,8 @@ void test_sid_ble_cb_set_before_init(void) struct bt_conn test_conn = { .dummy = 0xDC }; bt_conn_ref_fake.return_val = &test_conn; + int (*custom_fakes[])(const struct bt_conn *, struct bt_conn_info *) = {bt_conn_get_info_fake1}; + SET_CUSTOM_FAKE_SEQ(bt_conn_get_info, custom_fakes, 1); sid_ble_conn_deinit(); sid_ble_conn_init(); @@ -269,6 +287,8 @@ void test_sid_ble_conn_mtu_callback_curent_connection(void) sid_ble_conn_init(); bt_conn_ref_fake.return_val = &curr_conn; + int (*custom_fakes[])(const struct bt_conn *, struct bt_conn_info *) = {bt_conn_get_info_fake1}; + SET_CUSTOM_FAKE_SEQ(bt_conn_get_info, custom_fakes, 1); __cmock_sid_ble_adapter_conn_connected_ExpectAnyArgs(); sid_bt_conn_cb->connected(&curr_conn, 0);