-
Notifications
You must be signed in to change notification settings - Fork 21
sdh: simplifications and improvements #388
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
base: main
Are you sure you want to change the base?
Conversation
You can find the documentation preview for this PR here. |
b6616a3
to
9cf07f8
Compare
5fd1e4e
to
749680f
Compare
subsys/softdevice_handler/Kconfig
Outdated
endmenu | ||
|
||
config NRF_SDH_SOC_RAND_SEED | ||
bool "Automatically seed SoftDevice random seeds requests" |
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.
bool "Automatically seed SoftDevice random seeds requests" | |
bool "Automatically respond to SoftDevice random seed requests" |
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.
Ok, reworded
subsys/softdevice_handler/Kconfig
Outdated
bool "Automatically seed SoftDevice random seeds requests" | ||
depends on NRF_SECURITY | ||
depends on MBEDTLS_PSA_CRYPTO_C | ||
depends on PSA_WANT_GENERATE_RANDOM |
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 realize now that PSA_WANT_GENERATE_RANDOM
is not required after we changed from using psa_generate_random()
to using cracen_get_trng()
. (PRNG to TRNG.)
So the PSA_WANT_GENERATE_RANDOM
dependency should be removed from here.
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.
Thanks, removed it
subsys/softdevice_handler/Kconfig
Outdated
Make prints nicer by printing the stringified version of certain enumerations. | ||
Disable to save non-volatile memory. | ||
Compile a table of stringified SoftDevice events for nrf_sdh_ble_evt_tostr() and | ||
nrf_sdh_soc_evt_tostr(). Disable to save non-volatile memory. |
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.
Nit
nrf_sdh_soc_evt_tostr(). Disable to save non-volatile memory. | |
nrf_sdh_soc_evt_tostr(). Disable to save non-volatile memory. |
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.
fixed
|
||
status = cracen_get_trng(seed, sizeof(seed)); | ||
if (status != PSA_SUCCESS) { | ||
LOG_ERR("Failed to generate true random number, psa_status %#x", status); |
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.
PSA error values uses 0 for success and negative integers for errors. Similar to errno.
I think format %d
will be more readable.
LOG_ERR("Failed to generate true random number, psa_status %#x", status); | |
LOG_ERR("Failed to generate true random number, psa_status %d", status); |
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.
You are right, I think I checked and saw they were hex values but I checked again and they are not. So I am reverting this to %d
as it was before.
/* Discard seed immediately */ | ||
memset(seed, 0, sizeof(seed)); | ||
|
||
if (nrf_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.
Could be more explicit when checking the error here.
if (nrf_err) { | |
if (nrf_err != NRF_SUCCESS) { |
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.
ok, just for this specific case since we have PSA errors as well, better be explicit I guess.
include/nrf_sdh.h
Outdated
* that did not acknowledge have the responsibility to restart it by calling | ||
* @ref nrf_sdh_request_continue when they are ready for the SoftDevice to change state. | ||
* Disable the SoftDevice and send state events to registered observers. | ||
* An observer may halt the SoftDevice state change by returnin non-zero when receiving |
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.
* An observer may halt the SoftDevice state change by returnin non-zero when receiving | |
* An observer may halt the SoftDevice state change by returning non-zero when receiving |
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.
Corrected
state.paused = true; | ||
|
||
return (state.type == BM_STORAGE_SD_STATE_IDLE); | ||
case NRF_SDH_STATE_EVT_BLE_ENABLED: |
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 guess it can make sense to explicitly list all events from enum nrf_sdh_state_evt
here. Though, having a case for NRF_SDH_STATE_EVT_BLE_ENABLED
that does the same as default looks a bit strange and redundant.
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'll leave this as-is if it's alright
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.
Ok with me 👍
if (IS_ENABLED(CONFIG_NRF_SDH_STR_TABLES)) { | ||
LOG_INF("State change request: %s", req_tostr(req)); | ||
LOG_DBG("State change: %s", state_tostr(state)); | ||
} else { | ||
LOG_INF("State change request: %#x", req); | ||
LOG_DBG("State change: %#x", state); | ||
} |
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.
Should we give this and the state_tostr()
function the same treatment as the nrf_sdh_ble_evt_tostr()
and nrf_sdh_soc_evt_tostr()
? Or are you happy with how it looks currently?
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.
Not sure we need to export this one, if needed we can take it later.
Here we are not stringifying the events, just printing "enabled" / "disabled".
LOG_DBG("Notify observer %p => ready", obs); | ||
} else { | ||
TYPE_SECTION_FOREACH(struct nrf_sdh_state_evt_observer, nrf_sdh_state_evt_observers, obs) { | ||
busy = obs->handler(state, obs->context); |
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.
The state event observers are also called from nrf_sdh_ble_enable()
.
Should we add (void)
in front here: https://github.com/lemrey/sdk-nrf-bm/blob/749680f702c3ec718f30aef0795d283555d1fa00/subsys/softdevice_handler/nrf_sdh_ble.c#L252v
to show that it returns a value but we choose to ignore it.
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.
Ok, I should have fixed this by calling to a common function now.
busy = obs->handler(state, obs->context); | ||
if (busy) { | ||
/* Do not let SoftDevice change state now */ | ||
LOG_DBG("Notify observer %p => busy", obs); | ||
return -EBUSY; | ||
} |
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.
This does not seem right.
If returning 1
from a state event observer as a response to NRF_SDH_STATE_EVT_ENABLED
or NRF_SDH_STATE_EVT_DISABLED
, busy would be set to 1
here and we return -EBUSY
. If there is more than one registered state event observer, this could led to one or more state event observers not being called, because we return early.
Or do we depend on the user to ensure that 0
is always returned when handling NRF_SDH_STATE_EVT_ENABLED
or NRF_SDH_STATE_EVT_DISABLED
state events?
I'm able to observe this if adding a second state event observer in the hello_softdevice
sample, then log from both and returning 1
from both. This does not make sense for NRF_SDH_STATE_EVT_ENABLED
and NRF_SDH_STATE_EVT_DISABLED
.
Another thing that we should maybe document or do something about is the fact that the state event observers can be invoked several times with the same event if one or more modules would return busy and later invoke nrf_sdh_request_continue()
, which would resend the event to all observers that have already returned not busy.
Consider three observers, where the first and third in the observer list would return zero while the second one in the observer list would return busy and later call nrf_sdh_request_continue()
. How would this look for each of the three?
Wrote this and saw later that you are thinking of removing the nrf_sdh_request_continue function.
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 see, let me adjust this and add some asserts.
include/nrf_sdh.h
Outdated
|
||
/* Helper macros to check for equality */ | ||
|
||
#define _NRF_SDH_OBSERVER_PRIO_HIGHEST_HIGHEST 1 |
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.
don't use underscores for first character
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.
Alright, will rename
include/nrf_sdh_soc.h
Outdated
* If :option:`CONFIG_NRF_SDH_STR_TABLES` is enabled, returns the event name. | ||
* Otherwise, returns the supplied integer as a string. | ||
* | ||
* @param evt A NRF_SOC_SVCS enumeration value. |
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.
@ before symbols links to it
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.
@nordicjm Can you please provide some context on this suggestion? Thanks.
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.
Thanks
subsys/softdevice_handler/nrf_sdh.c
Outdated
}; | ||
|
||
if (sdh_enabled) { | ||
(void) sd_softdevice_is_enabled(&sd_is_enabled); |
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.
(void) sd_softdevice_is_enabled(&sd_is_enabled); | |
(void)sd_softdevice_is_enabled(&sd_is_enabled); |
fix in whole PR
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.
OK
88c1e77
to
5032ba2
Compare
Add a placeholder entry for sdh to be able to reference it in the changelog. Signed-off-by: Emanuele Di Santo <[email protected]>
This change aims to simplify the configuration options. This is currently a core component, and can't really be optional. If it were to be turned off, no BLE library would work. Signed-off-by: Emanuele Di Santo <[email protected]>
It is more correct to use an SoC observer to do this, rather than doing it directly in nrf_sdh_soc (the stack observer). Make the feature optional, and leave it enabled by default. Signed-off-by: Emanuele Di Santo <[email protected]>
Introduce an allowed set of priorities for observers, with validity checks. These are: HIGHEST, HIGH, USER, USER_LOW, LOWEST. These could be extended ala SYS_INIT to include a priority within a level, but it seemed a bit overkill for now. Each library or piece of code in the SDK shall have an hardcoded priority, because just changing the priority of one component would break the others. In general, a component's observer must have a lower priority than the observers in its dependencies. Signed-off-by: Emanuele Di Santo <[email protected]>
Add GATTS and GATTC event strings to the table. Renamed gap_evt_tostr() to ble_evt_tostr() to reflect that it now can print all BLE events. Signed-off-by: Emanuele Di Santo <[email protected]>
Add a Kconfig menu for clock configuration, to make the menu nicer. Signed-off-by: Emanuele Di Santo <[email protected]>
Add a Kconfig menu for BLE configuration, to make the menu nicer. Signed-off-by: Emanuele Di Santo <[email protected]>
Rework the functions in nrf_sdh_ble and nrf_sdh_soc that mapped BLE and SOC events to their stringified version to make them public and align their names. Instead of printing "unknown" if the event is not known, default to printing its numerical value in hexadecimal. That is also the default behavior if CONFIG_NRF_SDH_STR_TABLES is unset, and the table is not compiled. This is better than toggling the availability of the whole function, because we avoid conditional compilation around it. Signed-off-by: Emanuele Di Santo <[email protected]>
Update the assert message at the end of the polling loops to print which kind of event we failed to pull, e.g. SoC or BLE. Signed-off-by: Emanuele Di Santo <[email protected]>
There was a distinction between observers that could stop the SoftDevice state changes, and those that didn't. The former were called "request observers" and the latter "state observers". Since both observers are interested in the SoftDevice state, this commit merges the two, keeping only "state observers", for the purpose of simplifying things a bit. Now, state observers can return non-zero to these events: - NRF_SDH_STATE_EVT_ENABLE_PREPARE and - NRF_SDH_STATE_EVT_DISABLE_PREPARE to halt the state change. The return value from the observer is ignored for other events. Signed-off-by: Emanuele Di Santo <[email protected]>
These can return -EBUSY, which was missing. Signed-off-by: Emanuele Di Santo <[email protected]>
This API is not useful in NCS BM. Signed-off-by: Emanuele Di Santo <[email protected]>
This function shifts the responsibility of changing the state of the SoftDevice to the observer that halted its state change. This means that potentially all observers that halt the state change not only become responsible of the process (which may not be ideal) but also have to maintain some state and logic to restart that process. Instead, let the component who requested the state change in the first place retry the operation. Signed-off-by: Emanuele Di Santo <[email protected]>
Use the native SoftDevice function sd_softdevice_is_enabled() instead. Signed-off-by: Emanuele Di Santo <[email protected]>
Only check if the SoftDevice handler is suspended, not if the SoftDevice is enabled. Signed-off-by: Emanuele Di Santo <[email protected]>
Rename an internal variable. Signed-off-by: Emanuele Di Santo <[email protected]>
Remove these, and force users to put a semicolon themselves. Signed-off-by: Emanuele Di Santo <[email protected]>
Added changlog entries |
SoftDevice handler | ||
################## | ||
|
||
The SoftDevice handler is a library that handles SoftDevice initialization tasks, and pulls and dispatches the SoftDevice events to registered components. |
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.
Would be good with more skin on the bone, though I guess that can be added later as well.
No changes since the latest nRF Connect SDK Bare Metal release. | ||
* Removed: | ||
|
||
* The ``NRF_SDH_BLE`` Kconfig option. |
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 it should be like this:
* The ``NRF_SDH_BLE`` Kconfig option. | |
* The ``CONFIG_NRF_SDH_BLE`` Kconfig option. |
|
||
* Added: | ||
|
||
* The :option:`NRF_SDH_SOC_RAND_SEED` Kconfig option to control whether to automatically respond to SoftDevice random seed requests. |
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.
* The :option:`NRF_SDH_SOC_RAND_SEED` Kconfig option to control whether to automatically respond to SoftDevice random seed requests. | |
* The :kconfig:option:`CONFIG_NRF_SDH_SOC_RAND_SEED` Kconfig option to control whether to automatically respond to SoftDevice random seed requests. |
* @{ | ||
*/ | ||
|
||
|
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.
Extra newline.
* A SoftDevice observer has a defined priority, which determines the order with | ||
* which the observer receives relevant events compared to other observers. | ||
* | ||
* Five priority levels are defined, highest, high, user, user low, lowest. |
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.
* Five priority levels are defined, highest, high, user, user low, lowest. | |
* Five priority levels are defined: highest, high, user, user low, and lowest. |
LOG_MODULE_DECLARE(nrf_sdh, CONFIG_NRF_SDH_LOG_LEVEL); | ||
|
||
const char *gap_evt_tostr(int evt) | ||
const char *ble_evt_tostr(int evt) |
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.
const char *ble_evt_tostr(int evt) | |
const char *ble_evt_to_str(int evt) |
case BLE_GAP_EVT_CONNECTED: | ||
return "BLE_GAP_EVT_CONNECTED"; |
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 I prefer the current implementation. Otherwise we start devolving into too much macro magic.
@lemrey Please rebase :) |
HIGHEST
,HIGH
,USER
,USER_LOW
,LOWEST
nrf_sdh_app_ram_start_get()
, unused and not usefulnrf_sdh_is_enabled()
in favor of native SoftDevice functionnrf_sdh_request_continue()