Skip to content

Commit

Permalink
wip: fota
Browse files Browse the repository at this point in the history
  • Loading branch information
maxd-nordic committed May 2, 2024
1 parent 9b19d40 commit 4ffff67
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 5 deletions.
1 change: 1 addition & 0 deletions firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_subdirectory(src/modules/transport)
add_subdirectory(src/modules/error)
add_subdirectory(src/modules/location)
add_subdirectory(src/modules/app)
add_subdirectory(src/modules/fota)

# Optional modules
add_subdirectory_ifdef(CONFIG_APP_LED src/modules/led)
1 change: 1 addition & 0 deletions firmware/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rsource "src/modules/error/Kconfig.error"
rsource "src/modules/location/Kconfig.location"
rsource "src/modules/led/Kconfig.led"
rsource "src/modules/app/Kconfig.app"
rsource "src/modules/fota/Kconfig.fota"

endmenu

Expand Down
3 changes: 3 additions & 0 deletions firmware/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_NRF_CLOUD_ALERT=y
CONFIG_NRF_CLOUD_LOG_DIRECT=y
CONFIG_NRF_CLOUD_LOG_OUTPUT_LEVEL=3
CONFIG_NRF_CLOUD_FOTA_POLL=y
CONFIG_FOTA_DOWNLOAD=y
CONFIG_DFU_TARGET=y

# These options are required because the defaults will result in errors (cool..)
CONFIG_COAP_CLIENT_THREAD_PRIORITY=0
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/common/message_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ZBUS_CHAN_DEFINE(TRIGGER_CHAN, /* Name */
int, /* Message type */
NULL, /* Validator */
NULL, /* User data */
ZBUS_OBSERVERS(sampler, app, location), /* Observers */
ZBUS_OBSERVERS(fota, sampler, app, location), /* Observers */
ZBUS_MSG_INIT(0) /* Initial value {0} */
);

Expand Down Expand Up @@ -54,6 +54,6 @@ ZBUS_CHAN_DEFINE(CLOUD_CHAN,
enum cloud_status,
NULL,
NULL,
ZBUS_OBSERVERS(app, location),
ZBUS_OBSERVERS(fota, app, location),
CLOUD_DISCONNECTED
);
7 changes: 7 additions & 0 deletions firmware/src/modules/fota/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/fota.c)
18 changes: 18 additions & 0 deletions firmware/src/modules/fota/Kconfig.fota
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 20224 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menuconfig APP_FOTA
bool "FOTA"
select FOTA
default y if !BOARD_NATIVE_POSIX

if APP_FOTA

module = APP_FOTA
module-str = FOTA
source "subsys/logging/Kconfig.template.log_config"

endif # APP_FOTA
80 changes: 80 additions & 0 deletions firmware/src/modules/fota/fota.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/zbus/zbus.h>
#include <net/nrf_cloud_coap.h>
#include <net/nrf_cloud_fota_poll.h>
#include <zephyr/sys/reboot.h>

#include "message_channel.h"

/* Register log module */
LOG_MODULE_REGISTER(fota, CONFIG_APP_FOTA_LOG_LEVEL);

/* FOTA support context */
static void fota_reboot(enum nrf_cloud_fota_reboot_status status);
static struct nrf_cloud_fota_poll_ctx ctx = {
.reboot_fn = fota_reboot
};

void fota_reboot(enum nrf_cloud_fota_reboot_status status)
{
LOG_INF("Rebooting with FOTA status %d", status);

/* TODO: disconnect from network? */

/* Flush the logging buffers */
LOG_PANIC();

/* Reboot the device */
sys_reboot(SYS_REBOOT_COLD);
}


void fota_callback(const struct zbus_channel *chan)
{
int err = 0;

if (&CLOUD_CHAN == chan)
{
enum cloud_status status = CLOUD_DISCONNECTED;
int err = zbus_chan_read(chan, &status, K_NO_WAIT);

if (!err && status == CLOUD_CONNECTED)
{
err = nrf_cloud_fota_poll_init(&ctx);
if (err) {
LOG_ERR("nrf_cloud_fota_poll_init failed: %d", err);
return;
/* TODO: can we recover from this? */
}

/* Process pending FOTA job, the FOTA type is returned */
err = nrf_cloud_fota_poll_process_pending(&ctx);
if (err < 0) {
LOG_ERR("nrf_cloud_fota_poll_process_pending failed: %d", err);
} else if (err != NRF_CLOUD_FOTA_TYPE__INVALID) {
LOG_INF("Processed pending FOTA job type: %d", err);
}
}
}
if (&TRIGGER_CHAN == chan)
{
err = nrf_cloud_fota_poll_process(&ctx);
if (err && err != -EAGAIN) {
LOG_ERR("nrf_cloud_fota_poll_process failed: %d", err);
}
}
}


/* Register listener - led_callback will be called everytime a channel that the module listens on
* receives a new message.
*/
ZBUS_LISTENER_DEFINE(fota, fota_callback);
6 changes: 3 additions & 3 deletions firmware/src/modules/location/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ static void location_event_handler(const struct location_event_data *event_data)
{
case LOCATION_EVT_LOCATION:
LOG_DBG("Got location: lat: %f, lon: %f, acc: %f",
event_data->location.latitude,
event_data->location.longitude,
event_data->location.accuracy);
(double) event_data->location.latitude,
(double) event_data->location.longitude,
(double) event_data->location.accuracy);

/* GNSS location needs to be reported manually */
if (event_data->method == LOCATION_METHOD_GNSS) {
Expand Down

0 comments on commit 4ffff67

Please sign in to comment.