From 02199ffcb2651eb1382779b26686ff792c910655 Mon Sep 17 00:00:00 2001 From: Jonas Remmert Date: Mon, 27 May 2024 19:52:57 +0200 Subject: [PATCH] zephyr: lwm2m_client: Update to most recent Zephyr sample Update to most recent zephyr sample. Add resource 5/0/1 in order to test sending an URL where a firmware update can be downloaded. This is the first step to test OTA updates. Signed-off-by: Jonas Remmert --- fw_test/lwm2m_client/Kconfig | 3 ++- fw_test/lwm2m_client/README.rst | 29 --------------------- fw_test/lwm2m_client/overlay-bootstrap.conf | 1 - fw_test/lwm2m_client/overlay-bt.conf | 13 --------- fw_test/lwm2m_client/overlay-queue.conf | 9 ++++++- fw_test/lwm2m_client/sample.yaml | 13 +-------- fw_test/lwm2m_client/src/firmware_update.c | 23 +++++++++++----- fw_test/lwm2m_client/src/led.c | 2 +- fw_test/lwm2m_client/src/lwm2m-client.c | 22 +++++++++++++++- fw_test/lwm2m_client/src/timer.c | 4 +-- 10 files changed, 52 insertions(+), 67 deletions(-) delete mode 100644 fw_test/lwm2m_client/overlay-bt.conf diff --git a/fw_test/lwm2m_client/Kconfig b/fw_test/lwm2m_client/Kconfig index 22fab04b..2037d281 100644 --- a/fw_test/lwm2m_client/Kconfig +++ b/fw_test/lwm2m_client/Kconfig @@ -29,8 +29,9 @@ endif config LWM2M_APP_SERVER string "LwM2M server address" - default "coaps://192.0.2.2:5684" if LWM2M_DTLS_SUPPORT default "coap://192.0.2.2:5683" if !LWM2M_DTLS_SUPPORT + default "coaps://192.0.2.2:5684" if (LWM2M_DTLS_SUPPORT && !LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP) + default "coaps://192.0.2.2:5784" if (LWM2M_DTLS_SUPPORT && LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP) help LwM2M server address. Write as a full URI including optional port number. Only accepted protocols are "coap://" and "coaps://" diff --git a/fw_test/lwm2m_client/README.rst b/fw_test/lwm2m_client/README.rst index 2225f789..42759f6b 100644 --- a/fw_test/lwm2m_client/README.rst +++ b/fw_test/lwm2m_client/README.rst @@ -49,9 +49,6 @@ samples/net/lwm2m_client directory: * - :file:`overlay-dtls.conf` - This overlay config can be added for DTLS support via MBEDTLS. - * - :file:`overlay-bt.conf` - - This overlay config can be added to enable Bluetooth networking support. - * - :file:`overlay-queue.conf` - This overlay config can be added to enable LWM2M Queue Mode support. @@ -179,32 +176,6 @@ the overlay file for Bootstrap over DTLS (5784 in case of Leshan Demo Bootstrap Server) and to configure correct security mode in the ``LWM2M Bootstrap Server`` tab in the web UI (Pre-shared Key). -Bluetooth Support -================= - -To build the lwm2m-client sample for hardware requiring Bluetooth for -networking (IPSP node connected via 6lowpan) do the following: - -.. zephyr-app-commands:: - :zephyr-app: samples/net/lwm2m_client - :host-os: unix - :board: - :conf: "prj.conf overlay-bt.conf" - :goals: build - :compact: - -The overlay-\*.conf files can also be combined. For example, you could build a -DTLS-enabled LwM2M client sample for BLENano2 hardware by using the following -commands (requires Bluetooth for networking): - -.. zephyr-app-commands:: - :zephyr-app: samples/net/lwm2m_client - :host-os: unix - :board: nrf52_blenano2 - :conf: "prj.conf overlay-bt.conf overlay-dtls.conf" - :goals: build - :compact: - OpenThread Support ================== diff --git a/fw_test/lwm2m_client/overlay-bootstrap.conf b/fw_test/lwm2m_client/overlay-bootstrap.conf index 68308bbd..5993329c 100644 --- a/fw_test/lwm2m_client/overlay-bootstrap.conf +++ b/fw_test/lwm2m_client/overlay-bootstrap.conf @@ -1,2 +1 @@ -CONFIG_LWM2M_PEER_PORT=5783 CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP=y diff --git a/fw_test/lwm2m_client/overlay-bt.conf b/fw_test/lwm2m_client/overlay-bt.conf deleted file mode 100644 index 01c59b59..00000000 --- a/fw_test/lwm2m_client/overlay-bt.conf +++ /dev/null @@ -1,13 +0,0 @@ -CONFIG_BT=y -CONFIG_LOG=y -CONFIG_BT_SMP=y -CONFIG_BT_PERIPHERAL=y -CONFIG_BT_CENTRAL=y -CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y -CONFIG_BT_DEVICE_NAME="LwM2M IPSP node" -CONFIG_NET_L2_BT=y -CONFIG_NET_CONFIG_BT_NODE=y - -# raise bluetooth RX buffer settings for 6lowpan traffic -CONFIG_BT_BUF_ACL_RX_COUNT=20 -CONFIG_BT_BUF_ACL_RX_SIZE=124 diff --git a/fw_test/lwm2m_client/overlay-queue.conf b/fw_test/lwm2m_client/overlay-queue.conf index 946c0fba..2f2ee2ac 100644 --- a/fw_test/lwm2m_client/overlay-queue.conf +++ b/fw_test/lwm2m_client/overlay-queue.conf @@ -1,5 +1,12 @@ CONFIG_LWM2M_QUEUE_MODE_ENABLED=y -CONFIG_LWM2M_QUEUE_MODE_UPTIME=20 + +# Listen for incoming CoAP messages for 30 seconds after last TX. +# This is a common NAT timeout for cellular networks. +CONFIG_LWM2M_QUEUE_MODE_UPTIME=30 + +# When DTLS Connection Identifier is used, we can keep socket open +# and just stop polling while in idle state (QUEUE_RX_OFF). +# If the server does not support DTLS-CID, use CONFIG_LWM2M_RD_CLIENT_STOP_POLLING_AT_IDLE=y # Default lifetime is 1 day diff --git a/fw_test/lwm2m_client/sample.yaml b/fw_test/lwm2m_client/sample.yaml index 9c0cada2..f70290d2 100644 --- a/fw_test/lwm2m_client/sample.yaml +++ b/fw_test/lwm2m_client/sample.yaml @@ -52,17 +52,6 @@ tests: tags: - net - lwm2m - sample.net.lwm2m_client.bt: - harness: net - extra_args: OVERLAY_CONFIG=overlay-bt.conf - platform_allow: - - nrf52840dk_nrf52840 - - disco_l475_iot1 - tags: - - net - - lwm2m - integration_platforms: - - disco_l475_iot1 sample.net.lwm2m_client.queue_mode: harness: net depends_on: netif @@ -80,7 +69,7 @@ tests: extra_args: SHIELD=wnc_m14a2a platform_allow: - frdm_k64f - - nrf52840dk_nrf52840 + - nrf52840dk/nrf52840 integration_platforms: - frdm_k64f tags: diff --git a/fw_test/lwm2m_client/src/firmware_update.c b/fw_test/lwm2m_client/src/firmware_update.c index dd6dcd8d..da362865 100644 --- a/fw_test/lwm2m_client/src/firmware_update.c +++ b/fw_test/lwm2m_client/src/firmware_update.c @@ -38,13 +38,23 @@ static void *firmware_get_buf(uint16_t obj_inst_id, uint16_t res_id, return firmware_buf; } -static int firmware_block_received_cb(uint16_t obj_inst_id, - uint16_t res_id, uint16_t res_inst_id, - uint8_t *data, uint16_t data_len, - bool last_block, size_t total_size) +static int firmware_download_cb(uint16_t obj_inst_id, + uint16_t res_id, uint16_t res_inst_id, + uint8_t *data, uint16_t data_len, + bool last_block, size_t total_size, size_t offset) { - LOG_INF("FIRMWARE: BLOCK RECEIVED: len:%u last_block:%d", - data_len, last_block); + LOG_INF("FIRMWARE: DOWNLOAD"); + LOG_INF("Download Link: %s", data); + return 0; +} + +static int firmware_block_received_cb(uint16_t obj_inst_id, uint16_t res_id, + uint16_t res_inst_id, uint8_t *data, + uint16_t data_len, bool last_block, + size_t total_size, size_t offset) +{ + LOG_INF("FIRMWARE: BLOCK RECEIVED: offset:%zd len:%u last_block:%d", + offset, data_len, last_block); return 0; } @@ -58,6 +68,7 @@ void init_firmware_update(void) { /* setup data buffer for block-wise transfer */ lwm2m_register_pre_write_callback(&LWM2M_OBJ(5, 0, 0), firmware_get_buf); + lwm2m_register_post_write_callback(&LWM2M_OBJ(5, 0, 1), firmware_download_cb); lwm2m_firmware_set_write_cb(firmware_block_received_cb); /* register cancel callback */ diff --git a/fw_test/lwm2m_client/src/led.c b/fw_test/lwm2m_client/src/led.c index 98055baa..d4aa6388 100644 --- a/fw_test/lwm2m_client/src/led.c +++ b/fw_test/lwm2m_client/src/led.c @@ -22,7 +22,7 @@ static uint32_t led_state; /* TODO: Move to a pre write hook that can handle ret codes once available */ static int led_on_off_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, uint8_t *data, - uint16_t data_len, bool last_block, size_t total_size) + uint16_t data_len, bool last_block, size_t total_size, size_t offset) { int ret = 0; uint32_t led_val; diff --git a/fw_test/lwm2m_client/src/lwm2m-client.c b/fw_test/lwm2m_client/src/lwm2m-client.c index 2e9792e5..f8feceff 100644 --- a/fw_test/lwm2m_client/src/lwm2m-client.c +++ b/fw_test/lwm2m_client/src/lwm2m-client.c @@ -19,6 +19,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #include #include #include "modules.h" +#include "lwm2m_resource_ids.h" #define APP_BANNER "Run LWM2M client" @@ -30,6 +31,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); #define CLIENT_SERIAL_NUMBER "345000123" #define CLIENT_FIRMWARE_VER "1.0" #define CLIENT_HW_VER "1.0.1" +#define TEMP_SENSOR_UNITS "Celcius" /* Macros used to subscribe to specific Zephyr NET management events. */ #define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED) @@ -45,6 +47,8 @@ static uint8_t bat_level = 95; static uint8_t bat_status = LWM2M_DEVICE_BATTERY_STATUS_CHARGING; static int mem_free = 15; static int mem_total = 25; +static double min_range = 0.0; +static double max_range = 100; static struct lwm2m_ctx client_ctx; @@ -88,9 +92,17 @@ static int device_factory_default_cb(uint16_t obj_inst_id, return 0; } - static int lwm2m_setup(void) { + struct lwm2m_res_item temp_sensor_items[] = { + {&LWM2M_OBJ(IPSO_OBJECT_TEMP_SENSOR_ID, 0, MIN_RANGE_VALUE_RID), &min_range, + sizeof(min_range)}, + {&LWM2M_OBJ(IPSO_OBJECT_TEMP_SENSOR_ID, 0, MAX_RANGE_VALUE_RID), &max_range, + sizeof(max_range)}, + {&LWM2M_OBJ(IPSO_OBJECT_TEMP_SENSOR_ID, 0, SENSOR_UNITS_RID), TEMP_SENSOR_UNITS, + sizeof(TEMP_SENSOR_UNITS)} + }; + /* setup SECURITY object */ /* Server URL */ @@ -173,6 +185,14 @@ static int lwm2m_setup(void) /* setup TEMP SENSOR object */ init_temp_sensor(); + /* Set multiple TEMP SENSOR resource values in one function call. */ + int err = lwm2m_set_bulk(temp_sensor_items, ARRAY_SIZE(temp_sensor_items)); + + if (err) { + LOG_ERR("Failed to set TEMP SENSOR resources"); + return err; + } + /* IPSO: Light Control object */ init_led_device(); diff --git a/fw_test/lwm2m_client/src/timer.c b/fw_test/lwm2m_client/src/timer.c index 17814ae1..86107451 100644 --- a/fw_test/lwm2m_client/src/timer.c +++ b/fw_test/lwm2m_client/src/timer.c @@ -17,7 +17,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME); /* An example data validation callback. */ static int timer_on_off_validate_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, uint8_t *data, uint16_t data_len, bool last_block, - size_t total_size) + size_t total_size, size_t offset) { LOG_INF("Validating On/Off data"); @@ -34,7 +34,7 @@ static int timer_on_off_validate_cb(uint16_t obj_inst_id, uint16_t res_id, uint1 static int timer_digital_state_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, uint8_t *data, uint16_t data_len, bool last_block, - size_t total_size) + size_t total_size, size_t offset) { bool *digital_state = (bool *)data;