Skip to content

Commit

Permalink
dfu: Enter to DFU after reset
Browse files Browse the repository at this point in the history
[KRKNWK-16402]
The DFU service starts after reset.
Button 4 sets flag in flash, and when application starts and flag is set
The Sidewalk stack is not initalized and only SMP DFU service starts.

Signed-off-by: Robert Gałat <[email protected]>
  • Loading branch information
RobertGalatNordic committed May 18, 2023
1 parent cfde6b6 commit 086f293
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 43 deletions.
12 changes: 12 additions & 0 deletions Kconfig.dependecies
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ config SIDEWALK_DFU_SERVICE_BLE_DEFAULTS
config MCUMGR_TRANSPORT_BT_AUTHEN
default n

config MCUMGR_MGMT_NOTIFICATION_HOOKS
bool
default y

config MCUMGR_GRP_IMG_UPLOAD_CHECK_HOOK
bool
default y

config MCUMGR_SMP_COMMAND_STATUS_HOOKS
bool
default y

endif # SIDEWALK_DFU_SERVICE_BLE

if SIDEWALK_DFU_SERVICE_USB
Expand Down
2 changes: 1 addition & 1 deletion samples/template_ble/src/application_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void sidewalk_app_entry(void *ctx, void *unused, void *unused2)
case BUTTON_EVENT_FACTORY_RESET: button_event_factory_reset(application_ctx); break;
case BUTTON_EVENT_CONNECTION_REQUEST: button_event_connection_request(application_ctx); break;

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
case BUTTON_EVENT_NORDIC_DFU: button_event_DFU(application_ctx); break;
#endif

Expand Down
25 changes: 11 additions & 14 deletions samples/template_ble/src/board_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <zephyr/kernel.h>
#include <sid_api.h>
#include <sid_error.h>

Expand All @@ -15,6 +16,8 @@
#endif

#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>
#include <zephyr/sys/reboot.h>

LOG_MODULE_REGISTER(board_events, CONFIG_SIDEWALK_LOG_LEVEL);

Expand Down Expand Up @@ -83,27 +86,21 @@ void button_event_set_battery(app_ctx_t *application_ctx)
}
}

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
void button_event_DFU(app_ctx_t *application_ctx)
{
sid_error_t err = sid_deinit(application_ctx->handle);

if (err != SID_ERROR_NONE) {
LOG_ERR("Failed to deinitialize sidewalk! sid_deinit returned %d", err);
return;
}
bool DFU_mode = true;

application_ctx->handle = NULL;
(void) settings_save_one(CONFIG_DFU_FLAG_SETTINGS_KEY, (const void *)&DFU_mode,
sizeof(DFU_mode));

int ret = nordic_dfu_ble_start();
sid_deinit(application_ctx->handle);
k_sleep(K_SECONDS(1));

if (ret) {
LOG_ERR("DFU SMP start error (code %d)", ret);
}
application_state_dfu(&global_state_notifier, true);
sys_reboot(SYS_REBOOT_COLD);
}

#endif
#endif /* CONFIG_SIDEWALK_DFU_SERVICE_BLE */

void button_event_factory_reset(app_ctx_t *application_ctx)
{
Expand Down
26 changes: 22 additions & 4 deletions samples/template_ble/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
#include <stdbool.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>

#include <settings_utils.h>
#include <dk_buttons_and_leds.h>
#include <sidewalk_version.h>
#include <nordic_dfu.h>
#if defined(CONFIG_BOOTLOADER_MCUBOOT)
#include <zephyr/dfu/mcuboot.h>
#endif
Expand Down Expand Up @@ -47,7 +51,7 @@ static sid_error_t app_buttons_init(btn_handler_t handler)
button_set_action(DK_BTN2, handler, BUTTON_EVENT_CONNECTION_REQUEST);
button_set_action(DK_BTN3, handler, BUTTON_EVENT_SEND_HELLO);
button_set_action_short_press(DK_BTN4, handler, BUTTON_EVENT_SET_BATTERY_LEVEL);
#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
button_set_action_long_press(DK_BTN4, handler, BUTTON_EVENT_NORDIC_DFU);
#endif

Expand Down Expand Up @@ -109,9 +113,23 @@ int main(void)
{
PRINT_SIDEWALK_VERSION();

app_setup();
if (app_thread_init(&app_context)) {
LOG_ERR("Failed to start Sidewalk thread");
switch (application_to_start()) {
case SIDEWALK_APPLICATION: {
app_setup();
if (app_thread_init(&app_context)) {
LOG_ERR("Failed to start Sidewalk thread");
}
break;
};
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
case DFU_APPLICATION: {
const int ret = nordic_dfu_ble_start();
LOG_INF("DFU service started, return value %d", ret);
break;
}
#endif
default:
LOG_ERR("Unknown application to start.");
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion samples/template_subghz/src/application_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void sidewalk_app_entry(void *ctx, void *unused, void *unused2)
case BUTTON_EVENT_GET_DEVICE_PROFILE: button_event_get_profile(application_ctx); break;
case BUTTON_EVENT_SET_DEVICE_PROFILE: button_event_set_ptofile(application_ctx); break;

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
case BUTTON_EVENT_NORDIC_DFU: button_event_DFU(application_ctx); break;
#endif

Expand Down
25 changes: 11 additions & 14 deletions samples/template_subghz/src/board_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#endif

#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/kernel.h>

LOG_MODULE_REGISTER(board_events, CONFIG_SIDEWALK_LOG_LEVEL);

Expand Down Expand Up @@ -122,27 +125,21 @@ void button_event_set_battery(app_ctx_t *application_ctx)
}
}

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
void button_event_DFU(app_ctx_t *application_ctx)
{
sid_error_t err = sid_deinit(application_ctx->handle);

if (err != SID_ERROR_NONE) {
LOG_ERR("Failed to deinitialize sidewalk! sid_deinit returned %d", err);
return;
}
bool DFU_mode = true;

application_ctx->handle = NULL;
(void) settings_save_one(CONFIG_DFU_FLAG_SETTINGS_KEY, (const void *)&DFU_mode,
sizeof(DFU_mode));

int ret = nordic_dfu_ble_start();
sid_deinit(application_ctx->handle);
k_sleep(K_SECONDS(1));

if (ret) {
LOG_ERR("DFU SMP start error (code %d)", ret);
}
application_state_dfu(&global_state_notifier, true);
sys_reboot(SYS_REBOOT_COLD);
}

#endif
#endif /* CONFIG_SIDEWALK_DFU_SERVICE_BLE */

void button_event_factory_reset(app_ctx_t *application_ctx)
{
Expand Down
26 changes: 22 additions & 4 deletions samples/template_subghz/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
#include <stdbool.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/settings/settings.h>

#include <settings_utils.h>
#include <dk_buttons_and_leds.h>
#include <sidewalk_version.h>
#include <nordic_dfu.h>
#if defined(CONFIG_BOOTLOADER_MCUBOOT)
#include <zephyr/dfu/mcuboot.h>
#endif
Expand Down Expand Up @@ -48,7 +52,7 @@ static sid_error_t app_buttons_init(btn_handler_t handler)
button_set_action_short_press(DK_BTN2, handler, BUTTON_EVENT_GET_DEVICE_PROFILE);
button_set_action_long_press(DK_BTN2, handler, BUTTON_EVENT_SET_DEVICE_PROFILE);
button_set_action(DK_BTN3, handler, BUTTON_EVENT_SEND_HELLO);
#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
button_set_action_long_press(DK_BTN4, handler, BUTTON_EVENT_NORDIC_DFU);
#endif

Expand Down Expand Up @@ -110,9 +114,23 @@ int main(void)
{
PRINT_SIDEWALK_VERSION();

app_setup();
if (app_thread_init(&app_context)) {
LOG_ERR("Failed to start Sidewalk thread");
switch (application_to_start()) {
case SIDEWALK_APPLICATION: {
app_setup();
if (app_thread_init(&app_context)) {
LOG_ERR("Failed to start Sidewalk thread");
}
break;
};
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
case DFU_APPLICATION: {
const int ret = nordic_dfu_ble_start();
LOG_INF("DFU service started, return value %d", ret);
break;
}
#endif
default:
LOG_ERR("Unknown application to start.");
}

return 0;
Expand Down
7 changes: 7 additions & 0 deletions subsys/sal/sid_pal/src/sid_ble_adapter.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ static sid_error_t ble_adapter_deinit(void)
LOG_DBG("Sidewalk -> BLE");
sid_ble_conn_deinit();

int err = bt_disable();

if (err) {
LOG_ERR("BT disable failed (error %d)", err);
return SID_ERROR_GENERIC;
}

return SID_ERROR_NONE;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/manual/power/src/application_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void sidewalk_app_entry(void *ctx, void *unused, void *unused2)
case BUTTON_EVENT_FACTORY_RESET: button_event_factory_reset(application_ctx); break;
case BUTTON_EVENT_CONNECTION_REQUEST: button_event_connection_request(application_ctx); break;

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
case BUTTON_EVENT_NORDIC_DFU: button_event_DFU(application_ctx); break;
#endif

Expand Down
2 changes: 1 addition & 1 deletion tests/manual/power/src/board_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void button_event_set_battery(app_ctx_t *application_ctx)
}
}

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
void button_event_DFU(app_ctx_t *application_ctx)
{
sid_error_t err = sid_deinit(application_ctx->handle);
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/power_ble_only/src/application_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static void sidewalk_app_entry(void *ctx, void *unused, void *unused2)
case BUTTON_EVENT_FACTORY_RESET: button_event_factory_reset(application_ctx); break;
case BUTTON_EVENT_CONNECTION_REQUEST: button_event_connection_request(application_ctx); break;

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
case BUTTON_EVENT_NORDIC_DFU: button_event_DFU(application_ctx); break;
#endif

Expand Down
2 changes: 1 addition & 1 deletion tests/manual/power_ble_only/src/board_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void button_event_set_battery(app_ctx_t *application_ctx)
}
}

#if defined(CONFIG_SIDEWALK_DFU)
#if defined(CONFIG_SIDEWALK_DFU_SERVICE_BLE)
void button_event_DFU(app_ctx_t *application_ctx)
{
sid_error_t err = sid_deinit(application_ctx->handle);
Expand Down
23 changes: 23 additions & 0 deletions utils/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ config STATE_NOTIFIER_HANDLER_MAX
default 1
help
Maximum number of the notifier listeners.
if SIDEWALK_DFU_SERVICE_BLE

config DFU_FLAG_SETTINGS_KEY
string
default "application/settings/DFU_mode"
help
Key for settings to store DFU flag in flash for next reboot
If value is set to True, the applicaiton will start in DFU mode after reset

config DFU_UPLOAD_START_TIMEOUT
int
default 5
help
Time in minutes in which the DFU has to start, after this time the device will reboot and exit DFU mode

config DFU_UPLOAD_COMPLETE_TIMEOUT
int
default 60
help
Time in minutes in which the DFU has to complete (starting from entering to DFU mode)
if it do not complete in this time, the device will reboot and exit DFU mode

endif # SIDEWALK_DFU_SERVICE_BLE
13 changes: 13 additions & 0 deletions utils/include/settings_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <stdlib.h>

typedef enum { DFU_APPLICATION, SIDEWALK_APPLICATION } app_start_t;

int load_immediate_value(const char *name, void *dest, size_t len);

app_start_t application_to_start();
3 changes: 2 additions & 1 deletion utils/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ if (CONFIG_SIDEWALK_CLI)
zephyr_include_directories(${ZEPHYR_BASE}/../sidewalk/samples/template/include)
endif() # CONFIG_SIDEWALK_CLI

zephyr_library_sources_ifdef(CONFIG_SIDEWALK_DFU nordic_dfu.c)
zephyr_library_sources_ifdef(CONFIG_SIDEWALK_DFU_SERVICE_BLE nordic_dfu.c)
zephyr_library_sources(settings_utils.c)
Loading

0 comments on commit 086f293

Please sign in to comment.