net: lwm2m: use server record to set default observe notify timing

Server records contain the default PMIN and PMAX settings for how
often we can send observe notifications.  We are currently using
arbitrary defaults which cannot be changed when compiled or
during runtime.

Let's add Kconfig settings for the default settings to use and
also lookup the current values in the active server record when
an observe is added.

The actual PMIN/PMAX values can still be set via WRITE_ATTRIBUTE
operation.

Signed-off-by: Michael Scott <mike@foundries.io>
This commit is contained in:
Michael Scott 2019-07-29 10:00:00 -07:00 committed by Jukka Rissanen
commit c0e313aae1
4 changed files with 56 additions and 14 deletions

View file

@ -107,6 +107,23 @@ config LWM2M_SECURITY_KEY_SIZE
This setting establishes the size of the key (pre-shared / public) This setting establishes the size of the key (pre-shared / public)
resources in the security object instances. resources in the security object instances.
config LWM2M_SERVER_DEFAULT_PMIN
int "Default server record PMIN"
default 10
help
Default minimum amount of time in seconds the client must wait
between notifications. If a resource has to be notified during this
minimum time period, the notification must be sent after the time
period expires.
config LWM2M_SERVER_DEFAULT_PMAX
int "Default server record PMAX"
default 60
help
Default maximum amount of time in seconds the client may wait
between notifications. When this time period expires a notification
must be sent.
config LWM2M_SERVER_INSTANCE_COUNT config LWM2M_SERVER_INSTANCE_COUNT
int "Maximum # of LWM2M Server object instances" int "Maximum # of LWM2M Server object instances"
default 1 default 1

View file

@ -83,10 +83,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define COAP_OPTION_BUF_LEN 13 #define COAP_OPTION_BUF_LEN 13
#endif #endif
/* TODO: grab this from server obj */
#define DEFAULT_SERVER_PMIN 10
#define DEFAULT_SERVER_PMAX 60
#define MAX_TOKEN_LEN 8 #define MAX_TOKEN_LEN 8
struct observe_node { struct observe_node {
@ -414,8 +410,6 @@ static int engine_add_observer(struct lwm2m_message *msg,
struct observe_node *obs; struct observe_node *obs;
struct notification_attrs attrs = { struct notification_attrs attrs = {
.flags = BIT(LWM2M_ATTR_PMIN) | BIT(LWM2M_ATTR_PMAX), .flags = BIT(LWM2M_ATTR_PMIN) | BIT(LWM2M_ATTR_PMAX),
.pmin = DEFAULT_SERVER_PMIN,
.pmax = DEFAULT_SERVER_PMAX,
}; };
int i, ret; int i, ret;
@ -430,9 +424,11 @@ static int engine_add_observer(struct lwm2m_message *msg,
return -EINVAL; return -EINVAL;
} }
/* TODO: get server object for default pmin/pmax /* defaults from server object */
* and observe dup checking attrs.pmin = lwm2m_server_get_pmin(msg->ctx->sec_obj_inst);
*/ attrs.pmax = lwm2m_server_get_pmax(msg->ctx->sec_obj_inst);
/* TODO: observe dup checking */
/* make sure this observer doesn't exist already */ /* make sure this observer doesn't exist already */
SYS_SLIST_FOR_EACH_CONTAINER(&engine_observer_list, obs, node) { SYS_SLIST_FOR_EACH_CONTAINER(&engine_observer_list, obs, node) {
@ -2535,9 +2531,9 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
continue; continue;
} }
/* TODO: grab default from server obj */ /* defaults from server object */
nattrs.pmin = DEFAULT_SERVER_PMIN; nattrs.pmin = lwm2m_server_get_pmin(msg->ctx->sec_obj_inst);
nattrs.pmax = DEFAULT_SERVER_PMAX; nattrs.pmax = lwm2m_server_get_pmax(msg->ctx->sec_obj_inst);
ret = update_attrs(obj, &nattrs); ret = update_attrs(obj, &nattrs);
if (ret < 0) { if (ret < 0) {

View file

@ -102,6 +102,9 @@ size_t lwm2m_engine_get_opaque_more(struct lwm2m_input_context *in,
int lwm2m_security_inst_id_to_index(u16_t obj_inst_id); int lwm2m_security_inst_id_to_index(u16_t obj_inst_id);
int lwm2m_security_index_to_inst_id(int index); int lwm2m_security_index_to_inst_id(int index);
s32_t lwm2m_server_get_pmin(u16_t obj_inst_id);
s32_t lwm2m_server_get_pmax(u16_t obj_inst_id);
#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT) #if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_OBJ_SUPPORT)
u8_t lwm2m_firmware_get_update_state(void); u8_t lwm2m_firmware_get_update_state(void);
void lwm2m_firmware_set_update_state(u8_t state); void lwm2m_firmware_set_update_state(u8_t state);

View file

@ -98,6 +98,32 @@ static int update_trigger_cb(u16_t obj_inst_id)
#endif #endif
} }
static s32_t server_get_instance_s32(u16_t obj_inst_id, s32_t *data,
s32_t default_value)
{
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;
}
s32_t lwm2m_server_get_pmin(u16_t obj_inst_id)
{
return server_get_instance_s32(obj_inst_id, default_min_period,
CONFIG_LWM2M_SERVER_DEFAULT_PMIN);
}
s32_t lwm2m_server_get_pmax(u16_t obj_inst_id)
{
return server_get_instance_s32(obj_inst_id, default_max_period,
CONFIG_LWM2M_SERVER_DEFAULT_PMAX);
}
static struct lwm2m_engine_obj_inst *server_create(u16_t obj_inst_id) static struct lwm2m_engine_obj_inst *server_create(u16_t obj_inst_id)
{ {
int index, i = 0; int index, i = 0;
@ -128,8 +154,8 @@ static struct lwm2m_engine_obj_inst *server_create(u16_t obj_inst_id)
server_flag_store_notify[index] = 0U; server_flag_store_notify[index] = 0U;
server_id[index] = index + 1; server_id[index] = index + 1;
lifetime[index] = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME; lifetime[index] = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME;
default_min_period[index] = 0U; default_min_period[index] = CONFIG_LWM2M_SERVER_DEFAULT_PMIN;
default_max_period[index] = 0U; default_max_period[index] = CONFIG_LWM2M_SERVER_DEFAULT_PMAX;
disabled_timeout[index] = 86400U; disabled_timeout[index] = 86400U;
strcpy(transport_binding[index], "U"); strcpy(transport_binding[index], "U");