Skip to content

Commit a9c30d7

Browse files
rlubosdanieldegrasse
authored andcommitted
net: lwm2m: Implement gt/lt/st attributes handling
While it was possible to specify Greater Than / Less Than / Step observe attributes for a resource, they were not taken into consideration when evaluating notification criteria. This commit adds support for checking if the resource value meets the value criteria specified by gt/lt/st attributes. Note that gt/lt attributes are thresholds - notification should only sent if the resource value crosses them. Signed-off-by: Robert Lubos <[email protected]>
1 parent 1bfd2ab commit a9c30d7

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

subsys/net/lib/lwm2m/lwm2m_observation.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,86 @@ static int resource_value_as_double(const struct lwm2m_obj_path *path,
549549
return ret;
550550
}
551551

552+
static bool value_conditions_satisfied(const struct lwm2m_obj_path *path,
553+
uint16_t srv_obj_inst)
554+
{
555+
struct lwm2m_notify_value_register *last_notified;
556+
struct notification_attrs attrs = { 0 };
557+
double res_value, old_value;
558+
void *ref;
559+
int ret;
560+
561+
/* Value check only applies to resource or resource instance levels. */
562+
if (path->level < LWM2M_PATH_LEVEL_RESOURCE) {
563+
return true;
564+
}
565+
566+
ret = engine_observe_get_attributes(path, &attrs, srv_obj_inst);
567+
if (ret < 0) {
568+
return true;
569+
}
570+
571+
/* Check if any of the value attributes is actually set. */
572+
if ((attrs.flags & (BIT(LWM2M_ATTR_GT) |
573+
BIT(LWM2M_ATTR_LT) |
574+
BIT(LWM2M_ATTR_STEP))) == 0) {
575+
return true;
576+
}
577+
578+
ret = lwm2m_get_path_reference_ptr(NULL, path, &ref);
579+
if (ret < 0 || ref == NULL) {
580+
return true;
581+
}
582+
583+
/* Notification value register uses resource instance pointer as ref */
584+
if (path->level == LWM2M_PATH_LEVEL_RESOURCE) {
585+
struct lwm2m_engine_res *res = ref;
586+
587+
ref = res->res_instances;
588+
if (ref == NULL) {
589+
return true;
590+
}
591+
}
592+
593+
last_notified = notify_value_reg_get(ref);
594+
if (last_notified == NULL || !last_notified->notified) {
595+
return true;
596+
}
597+
598+
old_value = last_notified->value;
599+
600+
/* Value check only applies to numerical resources. */
601+
ret = resource_value_as_double(path, &res_value);
602+
if (ret < 0) {
603+
return true;
604+
}
605+
606+
if ((attrs.flags & BIT(LWM2M_ATTR_STEP)) != 0) {
607+
double res_diff = old_value > res_value ?
608+
old_value - res_value : res_value - old_value;
609+
610+
if (res_diff >= attrs.st) {
611+
return true;
612+
}
613+
}
614+
615+
if ((attrs.flags & BIT(LWM2M_ATTR_GT)) != 0) {
616+
if ((old_value <= attrs.gt && res_value > attrs.gt) ||
617+
(old_value >= attrs.gt && res_value < attrs.gt)) {
618+
return true;
619+
}
620+
}
621+
622+
if ((attrs.flags & BIT(LWM2M_ATTR_LT)) != 0) {
623+
if ((old_value <= attrs.lt && res_value > attrs.lt) ||
624+
(old_value >= attrs.lt && res_value < attrs.lt)) {
625+
return true;
626+
}
627+
}
628+
629+
return false;
630+
}
631+
552632
int lwm2m_notify_observer_path(const struct lwm2m_obj_path *path)
553633
{
554634
struct observe_node *obs;
@@ -573,6 +653,10 @@ int lwm2m_notify_observer_path(const struct lwm2m_obj_path *path)
573653
return ret;
574654
}
575655

656+
if (!value_conditions_satisfied(path, sock_ctx[i]->srv_obj_inst)) {
657+
continue;
658+
}
659+
576660
if (nattrs.pmin) {
577661
timestamp =
578662
obs->last_timestamp + MSEC_PER_SEC * nattrs.pmin;

0 commit comments

Comments
 (0)