-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make smp and sidewalk parallel #643
base: main
Are you sure you want to change the base?
Changes from all commits
005ea48
5b52afe
871ea7b
d9da400
ed56cc7
d373619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2024 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||
*/ | ||
#include <bt_app_callbacks.h> | ||
#include <zephyr/bluetooth/bluetooth.h> | ||
#include <stdbool.h> | ||
#include <zephyr/kernel.h> | ||
|
||
static uint32_t bt_enable_count = 0; | ||
|
||
int app_bt_enable(bt_ready_cb_t cb) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In genreal, I would recommend to keep the naming convention of Sidewalk module. The |
||
{ | ||
if (bt_enable_count == 0) { | ||
int ret = bt_enable(cb); | ||
if (ret == 0) { | ||
bt_enable_count++; | ||
} | ||
return ret; | ||
} | ||
|
||
bt_enable_count++; | ||
if (cb) { | ||
cb(0); | ||
} | ||
return 0; | ||
} | ||
|
||
int app_bt_disable() | ||
{ | ||
if (bt_enable_count <= 0) { | ||
bt_enable_count = 0; | ||
return -EALREADY; | ||
} | ||
if (bt_enable_count == 1) { | ||
bt_enable_count = 0; | ||
return bt_disable(); | ||
} else { | ||
bt_enable_count--; | ||
return 0; | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is not a good place dor this file (in this directory there are files to be changed when Sidewalk libraries are updates) - move to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree, this will be moved |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,50 @@ | ||||||||
/* | ||||||||
* Copyright (c) 2024 Nordic Semiconductor ASA | ||||||||
* | ||||||||
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause | ||||||||
*/ | ||||||||
|
||||||||
#ifndef SID_PAL_APP_CALLBACKS_H | ||||||||
#define SID_PAL_APP_CALLBACKS_H | ||||||||
|
||||||||
#include <stdbool.h> | ||||||||
#include <zephyr/bluetooth/bluetooth.h> | ||||||||
|
||||||||
#if defined (CONFIG_BT_APP_IFC) | ||||||||
|
||||||||
/** | ||||||||
* @brief Wrapper for @bt_enable, with reference tracking. | ||||||||
* Real @bt_enable is called only of first call to app_bt_enable | ||||||||
* | ||||||||
* @param cb callback passed to @bt_enable | ||||||||
* @return int result from @bt_enable call or 0 if called multiple times | ||||||||
*/ | ||||||||
int app_bt_enable(bt_ready_cb_t cb); | ||||||||
|
||||||||
|
||||||||
/** | ||||||||
* @brief Wrapper for @bt_disable. | ||||||||
* This function removes internal reference. | ||||||||
* If the internal reference counter shows 0, real @bt_disable is called | ||||||||
* | ||||||||
* @return int result from @bt_disable or 0 if app_bt_enable has been called more than app_bt_disable | ||||||||
*/ | ||||||||
int app_bt_disable(); | ||||||||
|
||||||||
|
||||||||
/** | ||||||||
* @brief BT ids used for extended advertising. | ||||||||
* This allows to identify connections from different extended adverticements. | ||||||||
*/ | ||||||||
enum BT_id_values{ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use uppercase in type names |
||||||||
_BT_ID_DEFAULT = BT_ID_DEFAULT, | ||||||||
BT_ID_SIDEWALK, | ||||||||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not think it is required, enums automatically build values as previous +1. The Ids are described in documentation for BLE as just an id in array of advertiser sets, so it is guaranteed by the API documentation, that they will be starting from 0 (default), and any new ID is just last one +1 up to the total defined size of the advertising array |
||||||||
#if defined (CONFIG_SIDEWALK_DFU) | ||||||||
BT_ID_SMP_DFU, | ||||||||
#endif | ||||||||
_BT_ID_MAX | ||||||||
}; | ||||||||
|
||||||||
BUILD_ASSERT(_BT_ID_MAX <= CONFIG_BT_ID_MAX, "Too many BT Ids! increase CONFIG_BT_ID_MAX, to match _BT_ID_MAX"); | ||||||||
Comment on lines
+45
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea to get the fast feedback to developer during build, but I think the BLE module should assert when you try to use too many ids. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we removed this build assert, we would find out we do not have enough BT_ID_MAX in runtime, when we try to get new ID, and IMO that is a little too late. |
||||||||
#endif | ||||||||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
#include <zephyr/bluetooth/hci.h> | ||
#include <zephyr/bluetooth/bluetooth.h> | ||
#include <bt_app_callbacks.h> | ||
|
||
#include <zephyr/bluetooth/gatt.h> | ||
#include <zephyr/bluetooth/uuid.h> | ||
|
@@ -213,14 +214,35 @@ 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 = 0; | ||
|
||
/* Check if Bluetooth identites weren't already created. */ | ||
bt_id_get(NULL, &count); | ||
if (count > BT_ID_SIDEWALK) { | ||
return 0; | ||
} | ||
|
||
do { | ||
ret = bt_id_create(NULL, NULL); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
} while (ret != BT_ID_SIDEWALK); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. risk of inifinite loop? Why isn't the id saved in some static struct and compared in callback? I see something like this is done in nrodic_dfu and some sources in ncs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will not get an infinite loop here, because in previous call, we checked if the advertising sets were loaded from settings, if yes, than we do not go into this loop. If we end up in the loop it means, that there are some ID that we still expect, so we call the function to get them. I think this can be rewritten to better express the behaviour |
||
|
||
return 0; | ||
} | ||
|
||
static sid_error_t ble_adapter_init(const sid_ble_config_t *cfg) | ||
{ | ||
LOG_DBG("Sidewalk -> BLE"); | ||
ARG_UNUSED(cfg); | ||
|
||
LOG_INF("Enable BT"); | ||
int err_code; | ||
err_code = bt_enable(NULL); | ||
err_code = app_bt_enable(NULL); | ||
switch (err_code) { | ||
case -EALREADY: | ||
case 0: | ||
|
@@ -231,6 +253,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; | ||
|
@@ -404,8 +438,10 @@ static sid_error_t ble_adapter_deinit(void) | |
{ | ||
LOG_DBG("Sidewalk -> BLE"); | ||
sid_ble_conn_deinit(); | ||
|
||
int err = bt_disable(); | ||
sid_ble_advert_deinit(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why to add advertisiement deinit here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we can free the advertising from BLE stack. Maybe for now it is not very useful, but I figured out if we asked for some resources, it would be nice to give them back when we are done working with them. |
||
bt_id_delete(BT_ID_SIDEWALK); | ||
bt_id_reset(BT_ID_SIDEWALK, NULL, NULL); | ||
int err = app_bt_disable(); | ||
|
||
if (err) { | ||
LOG_ERR("BT disable failed (error %d)", err); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not a good place dor this file (in this directory there are files to be changed when Sidewalk libraries are updates) - move to
sid_pal/include
?