Skip to content

Commit 0645609

Browse files
nixwardaescolar
authored andcommitted
net: lwm2m: observations: add attr type to str
Allows strings used to be exposed outside of file. Signed-off-by: Nick Ward <[email protected]>
1 parent 22d6bfa commit 0645609

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

subsys/net/lib/lwm2m/lwm2m_observation.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ struct notification_attrs {
8383
};
8484

8585
/* write-attribute related definitions */
86-
static const char *const LWM2M_ATTR_STR[] = {"pmin", "pmax", "gt", "lt", "st"};
8786
static const uint8_t LWM2M_ATTR_LEN[] = {4, 4, 2, 2, 2};
8887

8988
static struct lwm2m_attr write_attr_pool[CONFIG_LWM2M_NUM_ATTR];
@@ -94,6 +93,24 @@ void lwm2m_engine_free_list(sys_slist_t *path_list, sys_slist_t *free_list);
9493

9594
struct lwm2m_obj_path_list *lwm2m_engine_get_from_list(sys_slist_t *path_list);
9695

96+
const char *const lwm2m_attr_to_str(uint8_t type)
97+
{
98+
switch (type) {
99+
case LWM2M_ATTR_PMIN:
100+
return "pmin";
101+
case LWM2M_ATTR_PMAX:
102+
return "pmax";
103+
case LWM2M_ATTR_GT:
104+
return "gt";
105+
case LWM2M_ATTR_LT:
106+
return "lt";
107+
case LWM2M_ATTR_STEP:
108+
return "st";
109+
default:
110+
return "unknown";
111+
}
112+
}
113+
97114
static int update_attrs(void *ref, struct notification_attrs *out)
98115
{
99116
int i;
@@ -873,10 +890,10 @@ static int lwm2m_update_or_allocate_attribute(void *ref, uint8_t type, void *dat
873890

874891
if (type <= LWM2M_ATTR_PMAX) {
875892
attr->int_val = *(int32_t *)data;
876-
LOG_DBG("Update %s to %d", LWM2M_ATTR_STR[type], attr->int_val);
893+
LOG_DBG("Update %s to %d", lwm2m_attr_to_str(type), attr->int_val);
877894
} else {
878895
attr->float_val = *(double *)data;
879-
LOG_DBG("Update %s to %f", LWM2M_ATTR_STR[type], attr->float_val);
896+
LOG_DBG("Update %s to %f", lwm2m_attr_to_str(type), attr->float_val);
880897
}
881898
return 0;
882899
}
@@ -899,10 +916,10 @@ static int lwm2m_update_or_allocate_attribute(void *ref, uint8_t type, void *dat
899916

900917
if (type <= LWM2M_ATTR_PMAX) {
901918
attr->int_val = *(int32_t *)data;
902-
LOG_DBG("Add %s to %d", LWM2M_ATTR_STR[type], attr->int_val);
919+
LOG_DBG("Add %s to %d", lwm2m_attr_to_str(type), attr->int_val);
903920
} else {
904921
attr->float_val = *(double *)data;
905-
LOG_DBG("Add %s to %f", LWM2M_ATTR_STR[type], attr->float_val);
922+
LOG_DBG("Add %s to %f", lwm2m_attr_to_str(type), attr->float_val);
906923
}
907924
return 0;
908925
}
@@ -913,7 +930,7 @@ const char *lwm2m_engine_get_attr_name(const struct lwm2m_attr *attr)
913930
return NULL;
914931
}
915932

916-
return LWM2M_ATTR_STR[attr->type];
933+
return lwm2m_attr_to_str(attr->type);
917934
}
918935

919936
static int lwm2m_engine_observer_timestamp_update(sys_slist_t *observer,
@@ -1175,7 +1192,8 @@ int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message
11751192
/* matching attribute name */
11761193
for (type = 0U; type < NR_LWM2M_ATTR; type++) {
11771194
if (LWM2M_ATTR_LEN[type] == plen &&
1178-
!memcmp(options[i].value, LWM2M_ATTR_STR[type], LWM2M_ATTR_LEN[type])) {
1195+
!memcmp(options[i].value, lwm2m_attr_to_str(type),
1196+
LWM2M_ATTR_LEN[type])) {
11791197
break;
11801198
}
11811199
}
@@ -1224,7 +1242,7 @@ int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message
12241242
}
12251243

12261244
if (ret < 0) {
1227-
LOG_ERR("invalid attr[%s] value", LWM2M_ATTR_STR[type]);
1245+
LOG_ERR("invalid attr[%s] value", lwm2m_attr_to_str(type));
12281246
/* bad request */
12291247
return -EEXIST;
12301248
}
@@ -1270,7 +1288,7 @@ int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message
12701288
type = attr->type;
12711289

12721290
if (!(BIT(type) & nattrs.flags)) {
1273-
LOG_DBG("Unset attr %s", LWM2M_ATTR_STR[type]);
1291+
LOG_DBG("Unset attr %s", lwm2m_attr_to_str(type));
12741292
(void)memset(attr, 0, sizeof(*attr));
12751293

12761294
if (type <= LWM2M_ATTR_PMAX) {
@@ -1290,15 +1308,15 @@ int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message
12901308
attr->int_val = *(int32_t *)nattr_ptrs[type];
12911309
update_observe_node = true;
12921310

1293-
LOG_DBG("Update %s to %d", LWM2M_ATTR_STR[type], attr->int_val);
1311+
LOG_DBG("Update %s to %d", lwm2m_attr_to_str(type), attr->int_val);
12941312
} else {
12951313
if (attr->float_val == *(double *)nattr_ptrs[type]) {
12961314
continue;
12971315
}
12981316

12991317
attr->float_val = *(double *)nattr_ptrs[type];
13001318

1301-
LOG_DBG("Update %s to %f", LWM2M_ATTR_STR[type], attr->float_val);
1319+
LOG_DBG("Update %s to %f", lwm2m_attr_to_str(type), attr->float_val);
13021320
}
13031321
}
13041322

@@ -1327,11 +1345,11 @@ int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message
13271345
attr->int_val = *(int32_t *)nattr_ptrs[type];
13281346
update_observe_node = true;
13291347

1330-
LOG_DBG("Add %s to %d", LWM2M_ATTR_STR[type], attr->int_val);
1348+
LOG_DBG("Add %s to %d", lwm2m_attr_to_str(type), attr->int_val);
13311349
} else {
13321350
attr->float_val = *(double *)nattr_ptrs[type];
13331351

1334-
LOG_DBG("Add %s to %f", LWM2M_ATTR_STR[type], attr->float_val);
1352+
LOG_DBG("Add %s to %f", lwm2m_attr_to_str(type), attr->float_val);
13351353
}
13361354

13371355
nattrs.flags &= ~BIT(type);

subsys/net/lib/lwm2m/lwm2m_observation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct observe_node {
3030
struct lwm2m_attr *lwm2m_engine_get_next_attr(const void *ref, struct lwm2m_attr *prev);
3131
const char *lwm2m_engine_get_attr_name(const struct lwm2m_attr *attr);
3232

33+
const char *const lwm2m_attr_to_str(uint8_t type);
34+
3335
void clear_attrs(void *ref);
3436

3537
int64_t engine_observe_shedule_next_event(struct observe_node *obs, uint16_t srv_obj_inst,

0 commit comments

Comments
 (0)