|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <string.h> |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <zephyr/drivers/uart.h> |
| 10 | +#include <zephyr/mgmt/mcumgr/transport/serial.h> |
| 11 | +#include <zephyr/drivers/console/uart_mcumgr.h> |
| 12 | +#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h> |
| 13 | +#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h> |
| 14 | +#include <zephyr/sys/reboot.h> |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +#include <nrfx_uarte.h> |
| 17 | +#include <smp_uart.h> |
| 18 | +#include <board-config.h> |
| 19 | + |
| 20 | +LOG_MODULE_REGISTER(app_uart_mcumgr, CONFIG_APP_UART_MCUMGR_SAMPLE_LOG_LEVEL); |
| 21 | + |
| 22 | +static bool should_reboot; |
| 23 | + |
| 24 | +static enum mgmt_cb_return os_mgmt_reboot_hook(uint32_t event, enum mgmt_cb_return prev_status, |
| 25 | + int32_t *rc, uint16_t *group, bool *abort_more, |
| 26 | + void *data, size_t data_size); |
| 27 | + |
| 28 | +static struct mgmt_callback os_mgmt_reboot_callback = { |
| 29 | + .callback = os_mgmt_reboot_hook, |
| 30 | + .event_id = MGMT_EVT_OP_OS_MGMT_RESET, |
| 31 | +}; |
| 32 | + |
| 33 | +static enum mgmt_cb_return os_mgmt_reboot_hook(uint32_t event, enum mgmt_cb_return prev_status, |
| 34 | + int32_t *rc, uint16_t *group, bool *abort_more, |
| 35 | + void *data, size_t data_size) |
| 36 | +{ |
| 37 | + if (event == MGMT_EVT_OP_OS_MGMT_RESET) { |
| 38 | + should_reboot = true; |
| 39 | + *rc = MGMT_ERR_EOK; |
| 40 | + |
| 41 | + return MGMT_CB_ERROR_RC; |
| 42 | + } |
| 43 | + |
| 44 | + return MGMT_CB_OK; |
| 45 | +} |
| 46 | + |
| 47 | +int main(void) |
| 48 | +{ |
| 49 | + LOG_INF("UART MCUmgr sample started"); |
| 50 | + |
| 51 | + mgmt_callback_register(&os_mgmt_reboot_callback); |
| 52 | + |
| 53 | + while (should_reboot == false) { |
| 54 | + /* Wait for an event. */ |
| 55 | + __WFE(); |
| 56 | + |
| 57 | + /* Clear Event Register */ |
| 58 | + __SEV(); |
| 59 | + __WFE(); |
| 60 | + |
| 61 | + smp_uart_process_rx_queue(); |
| 62 | + } |
| 63 | + |
| 64 | + sys_reboot(SYS_REBOOT_WARM); |
| 65 | +} |
0 commit comments