2017-07-07 11:04:03 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited
|
2019-01-25 15:29:43 -08:00
|
|
|
* Copyright (c) 2018-2019 Foundries.io
|
2017-07-07 11:04:03 -07:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2018-09-19 11:22:19 +03:00
|
|
|
#define LOG_MODULE_NAME net_lwm2m_obj_server
|
|
|
|
#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
|
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/logging/log.h>
|
2018-09-19 11:22:19 +03:00
|
|
|
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|
|
|
|
2017-07-07 11:04:03 -07:00
|
|
|
#include <stdint.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/init.h>
|
2017-07-07 11:04:03 -07:00
|
|
|
|
|
|
|
#include "lwm2m_object.h"
|
2023-11-24 15:07:10 +02:00
|
|
|
#include "lwm2m_obj_server.h"
|
2017-07-07 11:04:03 -07:00
|
|
|
#include "lwm2m_rd_client.h"
|
2023-11-24 15:07:10 +02:00
|
|
|
#include "lwm2m_registry.h"
|
2024-01-19 16:14:37 +02:00
|
|
|
#include "lwm2m_engine.h"
|
2017-07-07 11:04:03 -07:00
|
|
|
|
2021-02-02 13:14:21 +01:00
|
|
|
#define SERVER_VERSION_MAJOR 1
|
2021-12-14 04:10:36 -08:00
|
|
|
#if defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)
|
|
|
|
#define SERVER_VERSION_MINOR 1
|
|
|
|
#define SERVER_MAX_ID 24
|
|
|
|
#else
|
2021-02-02 13:14:21 +01:00
|
|
|
#define SERVER_VERSION_MINOR 0
|
2021-12-14 04:10:36 -08:00
|
|
|
#define SERVER_MAX_ID 9
|
|
|
|
#endif /* defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1) */
|
2021-02-02 13:14:21 +01:00
|
|
|
|
2017-07-07 11:04:03 -07:00
|
|
|
/* Server flags */
|
|
|
|
#define SERVER_FLAG_STORE_NOTIFY 2
|
|
|
|
|
|
|
|
#define MAX_INSTANCE_COUNT CONFIG_LWM2M_SERVER_INSTANCE_COUNT
|
|
|
|
|
|
|
|
#define TRANSPORT_BINDING_LEN 4
|
|
|
|
|
2019-07-29 10:09:00 -07:00
|
|
|
/*
|
|
|
|
* Calculate resource instances as follows:
|
|
|
|
* start with SERVER_MAX_ID
|
|
|
|
* subtract EXEC resources (2)
|
|
|
|
*/
|
|
|
|
#define RESOURCE_INSTANCE_COUNT (SERVER_MAX_ID - 2)
|
|
|
|
|
2017-07-07 11:04:03 -07:00
|
|
|
/* resource state variables */
|
2020-05-27 11:26:57 -05:00
|
|
|
static uint16_t server_id[MAX_INSTANCE_COUNT];
|
|
|
|
static uint32_t lifetime[MAX_INSTANCE_COUNT];
|
|
|
|
static uint32_t default_min_period[MAX_INSTANCE_COUNT];
|
|
|
|
static uint32_t default_max_period[MAX_INSTANCE_COUNT];
|
2023-11-24 15:07:10 +02:00
|
|
|
static k_timepoint_t disabled_until[MAX_INSTANCE_COUNT];
|
2020-05-27 11:26:57 -05:00
|
|
|
static uint32_t disabled_timeout[MAX_INSTANCE_COUNT];
|
|
|
|
static uint8_t server_flag_store_notify[MAX_INSTANCE_COUNT];
|
2017-07-07 11:04:03 -07:00
|
|
|
static char transport_binding[MAX_INSTANCE_COUNT][TRANSPORT_BINDING_LEN];
|
2023-11-24 15:07:10 +02:00
|
|
|
/* Server object version 1.1 */
|
|
|
|
static uint8_t priority[MAX_INSTANCE_COUNT];
|
2022-04-01 00:13:01 -07:00
|
|
|
static bool mute_send[MAX_INSTANCE_COUNT];
|
2023-11-24 15:07:10 +02:00
|
|
|
static bool boostrap_on_fail[MAX_INSTANCE_COUNT];
|
2017-07-07 11:04:03 -07:00
|
|
|
|
|
|
|
static struct lwm2m_engine_obj server;
|
|
|
|
static struct lwm2m_engine_obj_field fields[] = {
|
2022-02-01 02:45:52 -08:00
|
|
|
OBJ_FIELD_DATA(SERVER_SHORT_SERVER_ID, R, U16),
|
2017-07-07 11:04:03 -07:00
|
|
|
OBJ_FIELD_DATA(SERVER_LIFETIME_ID, RW, U32),
|
2018-04-30 17:34:45 -07:00
|
|
|
OBJ_FIELD_DATA(SERVER_DEFAULT_MIN_PERIOD_ID, RW_OPT, U32),
|
|
|
|
OBJ_FIELD_DATA(SERVER_DEFAULT_MAX_PERIOD_ID, RW_OPT, U32),
|
|
|
|
OBJ_FIELD_EXECUTE_OPT(SERVER_DISABLE_ID),
|
|
|
|
OBJ_FIELD_DATA(SERVER_DISABLE_TIMEOUT_ID, RW_OPT, U32),
|
2020-05-21 17:21:47 +02:00
|
|
|
OBJ_FIELD_DATA(SERVER_STORE_NOTIFY_ID, RW, BOOL),
|
2019-01-25 22:36:47 -08:00
|
|
|
/* Mark Transport Binding is RO but BOOTSTRAP needs to write it */
|
|
|
|
OBJ_FIELD_DATA(SERVER_TRANSPORT_BINDING_ID, RW, STRING),
|
2017-07-07 11:04:03 -07:00
|
|
|
OBJ_FIELD_EXECUTE(SERVER_REG_UPDATE_TRIGGER_ID),
|
2021-12-14 04:10:36 -08:00
|
|
|
#if defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)
|
|
|
|
OBJ_FIELD_EXECUTE(SERVER_BOOTSTRAP_UPDATE_TRIGGER_ID),
|
|
|
|
OBJ_FIELD_DATA(SERVER_APN_LINK_ID, RW_OPT, OBJLNK),
|
|
|
|
OBJ_FIELD_DATA(SERVER_TLS_DTLS_ALERT_CODE_ID, R_OPT, U8),
|
|
|
|
OBJ_FIELD_DATA(SERVER_LAST_BOOTSTRAPPED_ID, R_OPT, TIME),
|
2023-11-24 15:07:10 +02:00
|
|
|
OBJ_FIELD_DATA(SERVER_REGISTRATION_PRIORITY_ORDER_ID, RW_OPT, U8),
|
2021-12-14 04:10:36 -08:00
|
|
|
OBJ_FIELD_DATA(SERVER_INITIAL_REGISTRATION_DELAY_TIMER_ID, W_OPT, U16),
|
|
|
|
OBJ_FIELD_DATA(SERVER_REGISTRATION_FAILURE_BLOCK_ID, W_OPT, BOOL),
|
2023-11-24 15:07:10 +02:00
|
|
|
OBJ_FIELD_DATA(SERVER_BOOTSTRAP_ON_REGISTRATION_FAILURE_ID, RW_OPT, BOOL),
|
2021-12-14 04:10:36 -08:00
|
|
|
OBJ_FIELD_DATA(SERVER_COMMUNICATION_RETRY_COUNT_ID, W_OPT, U16),
|
|
|
|
OBJ_FIELD_DATA(SERVER_COMMUNICATION_RETRY_TIMER_ID, W_OPT, U16),
|
|
|
|
OBJ_FIELD_DATA(SERVER_COMMUNICATION_SEQUENCE_DELAY_TIMER_ID, W_OPT, U16),
|
|
|
|
OBJ_FIELD_DATA(SERVER_COMMUNICATION_SEQUENCE_RETRY_TIMER_ID, W_OPT, U16),
|
|
|
|
OBJ_FIELD_DATA(SERVER_SMS_TRIGGER_ID, RW_OPT, BOOL),
|
|
|
|
OBJ_FIELD_DATA(SERVER_PREFERRED_TRANSPORT_ID, RW_OPT, STRING),
|
|
|
|
OBJ_FIELD_DATA(SERVER_MUTE_SEND_ID, RW_OPT, BOOL),
|
|
|
|
#endif /* defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1) */
|
|
|
|
|
2017-07-07 11:04:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
|
2019-07-29 10:09:00 -07:00
|
|
|
static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][SERVER_MAX_ID];
|
|
|
|
static struct lwm2m_engine_res_inst
|
|
|
|
res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
|
2017-07-07 11:04:03 -07:00
|
|
|
|
2020-12-10 10:17:14 +01:00
|
|
|
static int disable_cb(uint16_t obj_inst_id, uint8_t *args, uint16_t args_len)
|
2017-07-07 11:04:03 -07:00
|
|
|
{
|
2023-11-30 17:38:14 +02:00
|
|
|
ARG_UNUSED(args);
|
|
|
|
ARG_UNUSED(args_len);
|
2017-07-07 11:04:03 -07:00
|
|
|
|
2023-11-30 17:38:14 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_INSTANCE_COUNT; i++) {
|
2017-07-07 11:04:03 -07:00
|
|
|
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
|
2023-11-30 17:38:14 +02:00
|
|
|
LOG_DBG("DISABLE %d", obj_inst_id);
|
|
|
|
ret = lwm2m_rd_client_server_disabled(obj_inst_id);
|
|
|
|
if (ret == 0) {
|
|
|
|
disabled_until[i] =
|
|
|
|
sys_timepoint_calc(K_SECONDS(disabled_timeout[i]));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ret;
|
2017-07-07 11:04:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2020-12-10 10:17:14 +01:00
|
|
|
static int update_trigger_cb(uint16_t obj_inst_id,
|
|
|
|
uint8_t *args, uint16_t args_len)
|
2017-07-07 11:04:03 -07:00
|
|
|
{
|
2020-10-28 16:15:27 +01:00
|
|
|
engine_trigger_update(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-14 04:10:36 -08:00
|
|
|
static int bootstrap_trigger_cb(uint16_t obj_inst_id,
|
|
|
|
uint8_t *args, uint16_t args_len)
|
|
|
|
{
|
|
|
|
return engine_trigger_bootstrap();
|
|
|
|
}
|
2022-04-01 00:13:01 -07:00
|
|
|
|
|
|
|
bool lwm2m_server_get_mute_send(uint16_t obj_inst_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
|
|
|
|
return mute_send[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-14 04:10:36 -08:00
|
|
|
|
|
|
|
|
2020-10-28 16:15:27 +01:00
|
|
|
static int lifetime_write_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,
|
2024-05-10 13:35:11 +03:00
|
|
|
size_t total_size, size_t offset)
|
2020-10-28 16:15:27 +01:00
|
|
|
{
|
|
|
|
ARG_UNUSED(obj_inst_id);
|
|
|
|
ARG_UNUSED(res_id);
|
|
|
|
ARG_UNUSED(res_inst_id);
|
|
|
|
ARG_UNUSED(data);
|
|
|
|
ARG_UNUSED(data_len);
|
|
|
|
ARG_UNUSED(last_block);
|
|
|
|
ARG_UNUSED(total_size);
|
|
|
|
|
|
|
|
engine_trigger_update(false);
|
2017-07-07 11:04:03 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static int32_t server_get_instance_s32(uint16_t obj_inst_id, int32_t *data,
|
|
|
|
int32_t default_value)
|
2019-07-29 10:00:00 -07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
|
|
|
|
return data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
int32_t lwm2m_server_get_pmin(uint16_t obj_inst_id)
|
2019-07-29 10:00:00 -07:00
|
|
|
{
|
|
|
|
return server_get_instance_s32(obj_inst_id, default_min_period,
|
|
|
|
CONFIG_LWM2M_SERVER_DEFAULT_PMIN);
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
int32_t lwm2m_server_get_pmax(uint16_t obj_inst_id)
|
2019-07-29 10:00:00 -07:00
|
|
|
{
|
|
|
|
return server_get_instance_s32(obj_inst_id, default_max_period,
|
|
|
|
CONFIG_LWM2M_SERVER_DEFAULT_PMAX);
|
|
|
|
}
|
|
|
|
|
2022-07-08 14:42:53 +02:00
|
|
|
int lwm2m_server_get_ssid(uint16_t obj_inst_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
|
|
|
|
return server_id[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2020-07-31 11:30:11 +02:00
|
|
|
int lwm2m_server_short_id_to_inst(uint16_t short_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
if (inst[i].obj && server_id[i] == short_id) {
|
|
|
|
return inst[i].obj_inst_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2023-11-24 15:07:10 +02:00
|
|
|
static int lwm2m_server_inst_id_to_index(uint16_t obj_inst_id)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lwm2m_server_is_enabled(uint16_t obj_inst_id)
|
|
|
|
{
|
|
|
|
int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return sys_timepoint_expired(disabled_until[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lwm2m_server_disable(uint16_t obj_inst_id, k_timeout_t timeout)
|
|
|
|
{
|
|
|
|
int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
disabled_until[idx] = sys_timepoint_calc(timeout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_timepoint_t lwm2m_server_get_disabled_time(uint16_t obj_inst_id)
|
|
|
|
{
|
|
|
|
int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
return sys_timepoint_calc(K_FOREVER);
|
|
|
|
}
|
|
|
|
return disabled_until[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
void lwm2m_server_reset_timestamps(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
disabled_until[i] = sys_timepoint_calc(K_NO_WAIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool lwm2m_server_select(uint16_t *obj_inst_id)
|
|
|
|
{
|
|
|
|
uint8_t min = UINT8_MAX;
|
|
|
|
uint8_t max = 0;
|
|
|
|
|
|
|
|
/* Find priority boundaries */
|
|
|
|
if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
if (min > priority[i]) {
|
|
|
|
min = priority[i];
|
|
|
|
}
|
|
|
|
if (max < priority[i]) {
|
|
|
|
max = priority[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
min = max = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t prio = min; prio <= max; prio++) {
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(inst); i++) {
|
|
|
|
/* Disabled for a period */
|
|
|
|
if (!lwm2m_server_is_enabled(inst[i].obj_inst_id)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Invalid short IDs */
|
|
|
|
if (server_id[i] == 0 || server_id[i] == UINT16_MAX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check priority */
|
|
|
|
if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
|
|
|
|
if (priority[i] > prio) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (obj_inst_id) {
|
|
|
|
*obj_inst_id = inst[i].obj_inst_id;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_ERR("No server candidate found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t lwm2m_server_get_prio(uint16_t obj_inst_id)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
|
|
|
|
int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
|
|
|
|
|
|
|
|
if (idx < 0) {
|
|
|
|
return UINT8_MAX;
|
|
|
|
}
|
|
|
|
return priority[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
return (uint8_t)obj_inst_id % UINT8_MAX;
|
|
|
|
}
|
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
static struct lwm2m_engine_obj_inst *server_create(uint16_t obj_inst_id)
|
2017-07-07 11:04:03 -07:00
|
|
|
{
|
2019-07-29 10:09:00 -07:00
|
|
|
int index, i = 0, j = 0;
|
2017-07-07 11:04:03 -07:00
|
|
|
|
|
|
|
/* Check that there is no other instance with this ID */
|
|
|
|
for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
|
|
|
|
if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
|
2018-09-19 11:22:19 +03:00
|
|
|
LOG_ERR("Can not create instance - "
|
|
|
|
"already existing: %u", obj_inst_id);
|
2017-07-07 11:04:03 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
|
|
|
|
if (!inst[index].obj) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= MAX_INSTANCE_COUNT) {
|
2018-09-19 11:22:19 +03:00
|
|
|
LOG_ERR("Can not create instance - "
|
|
|
|
"no more room: %u", obj_inst_id);
|
2017-07-07 11:04:03 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set default values */
|
2023-11-24 15:07:10 +02:00
|
|
|
disabled_until[i] = sys_timepoint_calc(K_NO_WAIT);
|
2018-11-29 11:23:03 -08:00
|
|
|
server_flag_store_notify[index] = 0U;
|
2017-07-07 11:04:03 -07:00
|
|
|
server_id[index] = index + 1;
|
|
|
|
lifetime[index] = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME;
|
2019-07-29 10:00:00 -07:00
|
|
|
default_min_period[index] = CONFIG_LWM2M_SERVER_DEFAULT_PMIN;
|
|
|
|
default_max_period[index] = CONFIG_LWM2M_SERVER_DEFAULT_PMAX;
|
2018-11-29 11:23:03 -08:00
|
|
|
disabled_timeout[index] = 86400U;
|
2023-11-24 15:07:10 +02:00
|
|
|
boostrap_on_fail[index] = true;
|
2022-04-01 00:13:01 -07:00
|
|
|
|
2020-02-20 16:49:14 +01:00
|
|
|
lwm2m_engine_get_binding(transport_binding[index]);
|
2017-07-07 11:04:03 -07:00
|
|
|
|
2019-08-01 12:34:55 -07:00
|
|
|
(void)memset(res[index], 0,
|
|
|
|
sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
|
2019-07-29 10:09:00 -07:00
|
|
|
init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
|
|
|
|
|
2017-07-07 11:04:03 -07:00
|
|
|
/* initialize instance resource data */
|
2019-07-29 10:09:00 -07:00
|
|
|
INIT_OBJ_RES_DATA(SERVER_SHORT_SERVER_ID, res[index], i,
|
|
|
|
res_inst[index], j,
|
2017-07-07 11:04:03 -07:00
|
|
|
&server_id[index], sizeof(*server_id));
|
2020-10-28 16:15:27 +01:00
|
|
|
INIT_OBJ_RES(SERVER_LIFETIME_ID, res[index], i, res_inst[index], j,
|
2020-11-26 16:51:49 +01:00
|
|
|
1U, false, true, &lifetime[index], sizeof(*lifetime),
|
2021-02-17 15:47:28 +01:00
|
|
|
NULL, NULL, NULL, lifetime_write_cb, NULL);
|
2019-07-29 10:09:00 -07:00
|
|
|
INIT_OBJ_RES_DATA(SERVER_DEFAULT_MIN_PERIOD_ID, res[index], i,
|
|
|
|
res_inst[index], j,
|
2017-07-07 11:04:03 -07:00
|
|
|
&default_min_period[index],
|
|
|
|
sizeof(*default_min_period));
|
2019-07-29 10:09:00 -07:00
|
|
|
INIT_OBJ_RES_DATA(SERVER_DEFAULT_MAX_PERIOD_ID, res[index], i,
|
|
|
|
res_inst[index], j,
|
2017-07-07 11:04:03 -07:00
|
|
|
&default_max_period[index],
|
|
|
|
sizeof(*default_max_period));
|
2019-07-29 10:09:00 -07:00
|
|
|
INIT_OBJ_RES_EXECUTE(SERVER_DISABLE_ID, res[index], i, disable_cb);
|
|
|
|
INIT_OBJ_RES_DATA(SERVER_DISABLE_TIMEOUT_ID, res[index], i,
|
|
|
|
res_inst[index], j,
|
2017-07-07 11:04:03 -07:00
|
|
|
&disabled_timeout[index],
|
|
|
|
sizeof(*disabled_timeout));
|
2019-07-29 10:09:00 -07:00
|
|
|
INIT_OBJ_RES_DATA(SERVER_STORE_NOTIFY_ID, res[index], i,
|
|
|
|
res_inst[index], j,
|
2017-07-07 11:04:03 -07:00
|
|
|
&server_flag_store_notify[index],
|
|
|
|
sizeof(*server_flag_store_notify));
|
|
|
|
/* Mark Transport Binding RO as we only support UDP atm */
|
2023-10-17 13:47:47 +03:00
|
|
|
INIT_OBJ_RES_DATA_LEN(SERVER_TRANSPORT_BINDING_ID, res[index], i, res_inst[index], j,
|
|
|
|
transport_binding[index], TRANSPORT_BINDING_LEN,
|
|
|
|
strlen(transport_binding[index]) + 1);
|
|
|
|
INIT_OBJ_RES_EXECUTE(SERVER_REG_UPDATE_TRIGGER_ID, res[index], i, update_trigger_cb);
|
2023-11-24 15:07:10 +02:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
|
|
|
|
mute_send[index] = false;
|
|
|
|
priority[index] = 0;
|
|
|
|
INIT_OBJ_RES_EXECUTE(SERVER_BOOTSTRAP_UPDATE_TRIGGER_ID, res[index], i,
|
|
|
|
bootstrap_trigger_cb);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_APN_LINK_ID, res[index], i, res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_TLS_DTLS_ALERT_CODE_ID, res[index], i, res_inst[index],
|
|
|
|
j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_LAST_BOOTSTRAPPED_ID, res[index], i, res_inst[index],
|
|
|
|
j);
|
|
|
|
INIT_OBJ_RES_DATA(SERVER_REGISTRATION_PRIORITY_ORDER_ID, res[index], i,
|
|
|
|
res_inst[index], j, &priority[index], sizeof(uint8_t));
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_INITIAL_REGISTRATION_DELAY_TIMER_ID, res[index], i,
|
|
|
|
res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_REGISTRATION_FAILURE_BLOCK_ID, res[index], i,
|
|
|
|
res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_DATA(SERVER_BOOTSTRAP_ON_REGISTRATION_FAILURE_ID, res[index], i,
|
|
|
|
res_inst[index], j, &boostrap_on_fail[index], sizeof(bool));
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_RETRY_COUNT_ID, res[index], i,
|
|
|
|
res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_RETRY_TIMER_ID, res[index], i,
|
|
|
|
res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_SEQUENCE_DELAY_TIMER_ID, res[index], i,
|
|
|
|
res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_SEQUENCE_RETRY_TIMER_ID, res[index], i,
|
|
|
|
res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_SMS_TRIGGER_ID, res[index], i, res_inst[index], j);
|
|
|
|
INIT_OBJ_RES_OPTDATA(SERVER_PREFERRED_TRANSPORT_ID, res[index], i, res_inst[index],
|
|
|
|
j);
|
|
|
|
INIT_OBJ_RES_DATA(SERVER_MUTE_SEND_ID, res[index], i, res_inst[index], j,
|
|
|
|
&mute_send[index], sizeof(bool));
|
|
|
|
}
|
2017-07-07 11:04:03 -07:00
|
|
|
|
|
|
|
inst[index].resources = res[index];
|
|
|
|
inst[index].resource_count = i;
|
2018-09-19 11:22:19 +03:00
|
|
|
LOG_DBG("Create LWM2M server instance: %d", obj_inst_id);
|
2017-07-07 11:04:03 -07:00
|
|
|
return &inst[index];
|
|
|
|
}
|
|
|
|
|
init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:
- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices
They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:
```c
struct init_entry {
int (*init)(const struct device *dev);
/* only set by DEVICE_*, otherwise NULL */
const struct device *dev;
}
```
As a result, we end up with such weird/ugly pattern:
```c
static int my_init(const struct device *dev)
{
/* always NULL! add ARG_UNUSED to avoid compiler warning */
ARG_UNUSED(dev);
...
}
```
This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:
```c
static int my_init(void)
{
...
}
```
This is achieved using a union:
```c
union init_function {
/* for SYS_INIT, used when init_entry.dev == NULL */
int (*sys)(void);
/* for DEVICE*, used when init_entry.dev != NULL */
int (*dev)(const struct device *dev);
};
struct init_entry {
/* stores init function (either for SYS_INIT or DEVICE*)
union init_function init_fn;
/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
* to know which union entry to call.
*/
const struct device *dev;
}
```
This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.
**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
init: convert SYS_INIT functions to the new signature
Conversion scripted using scripts/utils/migrate_sys_init.py.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
manifest: update projects for SYS_INIT changes
Update modules with updated SYS_INIT calls:
- hal_ti
- lvgl
- sof
- TraceRecorderSource
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: devicetree: devices: adjust test
Adjust test according to the recently introduced SYS_INIT
infrastructure.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: kernel: threads: adjust SYS_INIT call
Adjust to the new signature: int (*init_fn)(void);
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 09:33:44 +02:00
|
|
|
static int lwm2m_server_init(void)
|
2017-07-07 11:04:03 -07:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
server.obj_id = LWM2M_OBJECT_SERVER_ID;
|
2021-02-02 13:14:21 +01:00
|
|
|
server.version_major = SERVER_VERSION_MAJOR;
|
|
|
|
server.version_minor = SERVER_VERSION_MINOR;
|
|
|
|
server.is_core = true;
|
2017-07-07 11:04:03 -07:00
|
|
|
server.fields = fields;
|
2018-06-14 12:27:11 -07:00
|
|
|
server.field_count = ARRAY_SIZE(fields);
|
2017-07-07 11:04:03 -07:00
|
|
|
server.max_instance_count = MAX_INSTANCE_COUNT;
|
|
|
|
server.create_cb = server_create;
|
|
|
|
lwm2m_register_obj(&server);
|
|
|
|
|
2022-04-29 14:56:25 +03:00
|
|
|
/* don't create automatically when using bootstrap */
|
|
|
|
if (!IS_ENABLED(CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP)) {
|
|
|
|
struct lwm2m_engine_obj_inst *obj_inst = NULL;
|
|
|
|
|
|
|
|
ret = lwm2m_create_obj_inst(LWM2M_OBJECT_SERVER_ID, 0, &obj_inst);
|
|
|
|
if (ret < 0) {
|
|
|
|
LOG_ERR("Create LWM2M server instance 0 error: %d", ret);
|
|
|
|
}
|
2017-07-07 11:04:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-01-19 16:14:37 +02:00
|
|
|
LWM2M_CORE_INIT(lwm2m_server_init);
|