diff --git a/port/zephyr/golioth_fw_zephyr.c b/port/zephyr/golioth_fw_zephyr.c index c525f510b..f84c0e40e 100644 --- a/port/zephyr/golioth_fw_zephyr.c +++ b/port/zephyr/golioth_fw_zephyr.c @@ -15,8 +15,10 @@ LOG_MODULE_REGISTER(golioth_fw_zephyr); struct flash_img_context _flash_img_context; -bool fw_update_is_pending_verify(void) { - if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) { +bool fw_update_is_pending_verify(void) +{ + if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) + { return false; } @@ -25,8 +27,10 @@ bool fw_update_is_pending_verify(void) { void fw_update_rollback(void) {} -void fw_update_reboot(void) { - if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) { +void fw_update_reboot(void) +{ + if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) + { return; } @@ -43,7 +47,8 @@ void fw_update_reboot(void) { * * @note This is a copy of zephyr_img_mgmt_flash_check_empty() from mcumgr. */ -static int flash_area_check_empty(const struct flash_area* fa, bool* out_empty) { +static int flash_area_check_empty(const struct flash_area *fa, bool *out_empty) +{ uint32_t data[16]; off_t addr; off_t end; @@ -59,21 +64,28 @@ static int flash_area_check_empty(const struct flash_area* fa, bool* out_empty) erased_val_32 = ERASED_VAL_32(erased_val); end = fa->fa_size; - for (addr = 0; addr < end; addr += sizeof(data)) { - if (end - addr < sizeof(data)) { + for (addr = 0; addr < end; addr += sizeof(data)) + { + if (end - addr < sizeof(data)) + { bytes_to_read = end - addr; - } else { + } + else + { bytes_to_read = sizeof(data); } rc = flash_area_read(fa, addr, data, bytes_to_read); - if (rc != 0) { + if (rc != 0) + { flash_area_close(fa); return rc; } - for (i = 0; i < bytes_to_read / 4; i++) { - if (data[i] != erased_val_32) { + for (i = 0; i < bytes_to_read / 4; i++) + { + if (data[i] != erased_val_32) + { *out_empty = false; flash_area_close(fa); return 0; @@ -86,33 +98,40 @@ static int flash_area_check_empty(const struct flash_area* fa, bool* out_empty) return 0; } -static int flash_img_erase_if_needed(struct flash_img_context* ctx) { +static int flash_img_erase_if_needed(struct flash_img_context *ctx) +{ bool empty; int err; - if (IS_ENABLED(CONFIG_IMG_ERASE_PROGRESSIVELY)) { + if (IS_ENABLED(CONFIG_IMG_ERASE_PROGRESSIVELY)) + { return 0; } err = flash_area_check_empty(ctx->flash_area, &empty); - if (err) { + if (err) + { return err; } - if (empty) { + if (empty) + { return 0; } err = flash_area_erase(ctx->flash_area, 0, ctx->flash_area->fa_size); - if (err) { + if (err) + { return err; } return 0; } -static const char* swap_type_str(int swap_type) { - switch (swap_type) { +static const char *swap_type_str(int swap_type) +{ + switch (swap_type) + { case BOOT_SWAP_TYPE_NONE: return "none"; case BOOT_SWAP_TYPE_TEST: @@ -128,12 +147,14 @@ static const char* swap_type_str(int swap_type) { return "unknown"; } -static int flash_img_prepare(struct flash_img_context* flash) { +static int flash_img_prepare(struct flash_img_context *flash) +{ int swap_type; int err; swap_type = mcuboot_swap_type(); - switch (swap_type) { + switch (swap_type) + { case BOOT_SWAP_TYPE_REVERT: LOG_WRN("'revert' swap type detected, it is not safe to continue"); return -EBUSY; @@ -143,13 +164,15 @@ static int flash_img_prepare(struct flash_img_context* flash) { } err = flash_img_init(flash); - if (err) { + if (err) + { LOG_ERR("failed to init: %d", err); return err; } err = flash_img_erase_if_needed(flash); - if (err) { + if (err) + { LOG_ERR("failed to erase: %d", err); return err; } @@ -157,34 +180,40 @@ static int flash_img_prepare(struct flash_img_context* flash) { return 0; } -void fw_update_cancel_rollback(void) { - if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) { +void fw_update_cancel_rollback(void) +{ + if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) + { return; } boot_write_img_confirmed(); } -enum golioth_status fw_update_handle_block( - const uint8_t* block, - size_t block_size, - size_t offset, - size_t total_size) { +enum golioth_status fw_update_handle_block(const uint8_t *block, + size_t block_size, + size_t offset, + size_t total_size) +{ int err; - if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) { + if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) + { return GOLIOTH_OK; } - if (offset == 0) { + if (offset == 0) + { err = flash_img_prepare(&_flash_img_context); - if (err) { + if (err) + { return GOLIOTH_ERR_FAIL; } } err = flash_img_buffered_write(&_flash_img_context, block, block_size, false); - if (err) { + if (err) + { LOG_ERR("Failed to write to flash: %d", err); return GOLIOTH_ERR_FAIL; } @@ -192,32 +221,39 @@ enum golioth_status fw_update_handle_block( return GOLIOTH_OK; } -void fw_update_post_download(void) { +void fw_update_post_download(void) +{ int err; - if (!_flash_img_context.stream.buf_bytes) { + if (!_flash_img_context.stream.buf_bytes) + { return; } err = flash_img_buffered_write(&_flash_img_context, NULL, 0, true); - if (err) { + if (err) + { LOG_ERR("Failed to write to flash: %d", err); } } -enum golioth_status fw_update_validate(void) { +enum golioth_status fw_update_validate(void) +{ return GOLIOTH_OK; } -enum golioth_status fw_update_change_boot_image(void) { +enum golioth_status fw_update_change_boot_image(void) +{ int err; - if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) { + if (!IS_ENABLED(CONFIG_BOOTLOADER_MCUBOOT)) + { return GOLIOTH_ERR_NOT_IMPLEMENTED; } err = boot_request_upgrade(BOOT_UPGRADE_TEST); - if (err) { + if (err) + { return GOLIOTH_ERR_FAIL; } diff --git a/port/zephyr/golioth_log_zephyr.h b/port/zephyr/golioth_log_zephyr.h index 8af3d92a0..f03f94fe2 100644 --- a/port/zephyr/golioth_log_zephyr.h +++ b/port/zephyr/golioth_log_zephyr.h @@ -4,5 +4,5 @@ * SPDX-License-Identifier: Apache-2.0 */ -int log_backend_golioth_enable(void* client); -int log_backend_golioth_disable(void* client); +int log_backend_golioth_enable(void *client); +int log_backend_golioth_disable(void *client); diff --git a/port/zephyr/golioth_sys_zephyr.c b/port/zephyr/golioth_sys_zephyr.c index 4b59816ef..464c29e8b 100644 --- a/port/zephyr/golioth_sys_zephyr.c +++ b/port/zephyr/golioth_sys_zephyr.c @@ -16,11 +16,13 @@ LOG_TAG_DEFINE(golioth_sys_zephyr); * Time *------------------------------------------------*/ -void golioth_sys_msleep(uint32_t ms) { +void golioth_sys_msleep(uint32_t ms) +{ k_msleep(ms); } -uint64_t golioth_sys_now_ms(void) { +uint64_t golioth_sys_now_ms(void) +{ return k_uptime_get(); } @@ -28,14 +30,16 @@ uint64_t golioth_sys_now_ms(void) { * Semaphores *------------------------------------------------*/ -#define SEM_TO_FD(sem) ((int)(intptr_t)(sem)) -#define FD_TO_SEM(sem) ((golioth_sys_sem_t)(intptr_t)(fd)) +#define SEM_TO_FD(sem) ((int) (intptr_t) (sem)) +#define FD_TO_SEM(sem) ((golioth_sys_sem_t) (intptr_t) (fd)) -golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count) { +golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_initial_count) +{ int fd; fd = eventfd(sem_initial_count, EFD_SEMAPHORE); - if (fd < 0) { + if (fd < 0) + { GLTH_LOGE(TAG, "eventfd creation failed, errno: %d", errno); return NULL; } @@ -43,9 +47,11 @@ golioth_sys_sem_t golioth_sys_sem_create(uint32_t sem_max_count, uint32_t sem_in return FD_TO_SEM(fd); } -bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait) { +bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait) +{ int fd = SEM_TO_FD(sem); - while (true) { + while (true) + { struct zsock_pollfd pfd = { .fd = fd, .events = ZSOCK_POLLIN, @@ -54,19 +60,24 @@ bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait) { int ret; ret = zsock_poll(&pfd, 1, ms_to_wait); - if (ret < 0) { - if (errno == EINTR) { + if (ret < 0) + { + if (errno == EINTR) + { GLTH_LOGI(TAG, "EINTR"); continue; } GLTH_LOGE(TAG, "sem poll failed, errno: %d", errno); return false; - } else if (ret == 0) { + } + else if (ret == 0) + { return false; } ret = eventfd_read(fd, &val); - if (ret < 0) { + if (ret < 0) + { GLTH_LOGE(TAG, "sem eventfd_read failed, errno: %d", errno); return false; } @@ -77,15 +88,18 @@ bool golioth_sys_sem_take(golioth_sys_sem_t sem, int32_t ms_to_wait) { return true; } -bool golioth_sys_sem_give(golioth_sys_sem_t sem) { +bool golioth_sys_sem_give(golioth_sys_sem_t sem) +{ return (0 == eventfd_write(SEM_TO_FD(sem), 1)); } -void golioth_sys_sem_destroy(golioth_sys_sem_t sem) { +void golioth_sys_sem_destroy(golioth_sys_sem_t sem) +{ zsock_close(SEM_TO_FD(sem)); } -int golioth_sys_sem_get_fd(golioth_sys_sem_t sem) { +int golioth_sys_sem_get_fd(golioth_sys_sem_t sem) +{ return SEM_TO_FD(sem); } @@ -93,7 +107,8 @@ int golioth_sys_sem_get_fd(golioth_sys_sem_t sem) { * Software Timers *------------------------------------------------*/ -struct golioth_timer { +struct golioth_timer +{ struct k_timer timer; struct k_work work; struct golioth_timer_config config; @@ -103,22 +118,26 @@ static void timer_handler_worker(struct k_work *work) { struct golioth_timer *timer = CONTAINER_OF(work, struct golioth_timer, work); - if (timer->config.fn) { + if (timer->config.fn) + { timer->config.fn(timer, timer->config.user_arg); } } -static void on_timer(struct k_timer* ztimer) { - struct golioth_timer* timer = CONTAINER_OF(ztimer, struct golioth_timer, timer); +static void on_timer(struct k_timer *ztimer) +{ + struct golioth_timer *timer = CONTAINER_OF(ztimer, struct golioth_timer, timer); k_work_submit(&timer->work); } -golioth_sys_timer_t golioth_sys_timer_create(const struct golioth_timer_config *config) { - struct golioth_timer* timer; +golioth_sys_timer_t golioth_sys_timer_create(const struct golioth_timer_config *config) +{ + struct golioth_timer *timer; timer = golioth_sys_malloc(sizeof(*timer)); - if (!timer) { + if (!timer) + { return NULL; } @@ -127,23 +146,26 @@ golioth_sys_timer_t golioth_sys_timer_create(const struct golioth_timer_config * k_timer_init(&timer->timer, on_timer, NULL); k_work_init(&timer->work, timer_handler_worker); - return (golioth_sys_timer_t)timer; + return (golioth_sys_timer_t) timer; } -bool golioth_sys_timer_start(golioth_sys_timer_t gtimer) { - struct golioth_timer* timer = gtimer; +bool golioth_sys_timer_start(golioth_sys_timer_t gtimer) +{ + struct golioth_timer *timer = gtimer; k_timer_start(&timer->timer, K_MSEC(timer->config.expiration_ms), K_NO_WAIT); return true; } -bool golioth_sys_timer_reset(golioth_sys_timer_t timer) { +bool golioth_sys_timer_reset(golioth_sys_timer_t timer) +{ return golioth_sys_timer_start(timer); } -void golioth_sys_timer_destroy(golioth_sys_timer_t gtimer) { - struct golioth_timer* timer = gtimer; +void golioth_sys_timer_destroy(golioth_sys_timer_t gtimer) +{ + struct golioth_timer *timer = gtimer; k_timer_stop(&timer->timer); golioth_sys_free(timer); @@ -156,65 +178,70 @@ void golioth_sys_timer_destroy(golioth_sys_timer_t gtimer) { // Wrap pthread due to incompatibility with thread function type. // pthread fn returns void* // golioth_sys_thread_fn_t returns void -struct golioth_thread { +struct golioth_thread +{ struct k_thread thread; k_tid_t tid; golioth_sys_thread_fn_t fn; - void* user_arg; + void *user_arg; }; -K_THREAD_STACK_ARRAY_DEFINE( - golioth_thread_stacks, - CONFIG_GOLIOTH_ZEPHYR_THREAD_STACKS, - CONFIG_GOLIOTH_ZEPHYR_THREAD_STACK_SIZE); +K_THREAD_STACK_ARRAY_DEFINE(golioth_thread_stacks, + CONFIG_GOLIOTH_ZEPHYR_THREAD_STACKS, + CONFIG_GOLIOTH_ZEPHYR_THREAD_STACK_SIZE); static atomic_t golioth_thread_idx; -static k_thread_stack_t* stack_alloc(void) { +static k_thread_stack_t *stack_alloc(void) +{ atomic_val_t idx = atomic_inc(&golioth_thread_idx); - if (idx > ARRAY_SIZE(golioth_thread_stacks)) { + if (idx > ARRAY_SIZE(golioth_thread_stacks)) + { return NULL; } return golioth_thread_stacks[idx]; } -static void golioth_thread_main(void* p1, void* p2, void* p3) { +static void golioth_thread_main(void *p1, void *p2, void *p3) +{ golioth_sys_thread_fn_t fn = p1; - void* user_arg = p2; + void *user_arg = p2; fn(user_arg); } -golioth_sys_thread_t golioth_sys_thread_create(const struct golioth_thread_config *config) { +golioth_sys_thread_t golioth_sys_thread_create(const struct golioth_thread_config *config) +{ // Intentionally ignoring from config: // name // stack_size // prio - struct golioth_thread* thread = golioth_sys_malloc(sizeof(*thread)); - k_thread_stack_t* stack; + struct golioth_thread *thread = golioth_sys_malloc(sizeof(*thread)); + k_thread_stack_t *stack; stack = stack_alloc(); - if (!stack) { + if (!stack) + { goto free_thread; } - thread->tid = k_thread_create( - &thread->thread, - stack, - CONFIG_GOLIOTH_ZEPHYR_THREAD_STACK_SIZE, - golioth_thread_main, - config->fn, - config->user_arg, - NULL, - config->prio, - 0, - K_NO_WAIT); - if (config->name) { + thread->tid = k_thread_create(&thread->thread, + stack, + CONFIG_GOLIOTH_ZEPHYR_THREAD_STACK_SIZE, + golioth_thread_main, + config->fn, + config->user_arg, + NULL, + config->prio, + 0, + K_NO_WAIT); + if (config->name) + { k_thread_name_set(thread->tid, config->name); } - return (golioth_sys_thread_t)thread; + return (golioth_sys_thread_t) thread; free_thread: golioth_sys_free(thread); @@ -222,8 +249,9 @@ golioth_sys_thread_t golioth_sys_thread_create(const struct golioth_thread_confi return NULL; } -void golioth_sys_thread_destroy(golioth_sys_thread_t gthread) { - struct golioth_thread* thread = gthread; +void golioth_sys_thread_destroy(golioth_sys_thread_t gthread) +{ + struct golioth_thread *thread = gthread; k_thread_abort(thread->tid); golioth_sys_free(thread); @@ -233,14 +261,18 @@ void golioth_sys_thread_destroy(golioth_sys_thread_t gthread) { * Misc *------------------------------------------------*/ -void golioth_sys_client_connected(void* client) { - if (IS_ENABLED(CONFIG_LOG_BACKEND_GOLIOTH)) { +void golioth_sys_client_connected(void *client) +{ + if (IS_ENABLED(CONFIG_LOG_BACKEND_GOLIOTH)) + { log_backend_golioth_enable(client); } } -void golioth_sys_client_disconnected(void* client) { - if (IS_ENABLED(CONFIG_LOG_BACKEND_GOLIOTH)) { +void golioth_sys_client_disconnected(void *client) +{ + if (IS_ENABLED(CONFIG_LOG_BACKEND_GOLIOTH)) + { log_backend_golioth_disable(client); } }