Skip to content

Commit

Permalink
Reduce code duplication in client example
Browse files Browse the repository at this point in the history
Some parts of the code were copy-and-pasted several times.
  • Loading branch information
LukasWoodtli committed Sep 9, 2024
1 parent 4b8a3b9 commit 0b4b967
Showing 1 changed file with 16 additions and 36 deletions.
52 changes: 16 additions & 36 deletions examples/client/object_connectivity_moni.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ typedef struct
int linkUtilization;
} conn_m_data_t;

static void reduce_single_instance(lwm2m_data_t *dataP, lwm2m_data_t **subTlvP, size_t *count) {
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE) {
(*count) = dataP->value.asChildren.count;
(*subTlvP) = dataP->value.asChildren.array;
} else {
(*count) = 1; // reduced to 1 instance to fit in one block size
(*subTlvP) = lwm2m_data_new((*count));
for (size_t i = 0; i < (*count); i++)
(*subTlvP)[i].id = i;
lwm2m_data_encode_instances((*subTlvP), (*count), dataP);
}
}

static uint8_t prv_set_value(lwm2m_data_t * dataP,
conn_m_data_t * connDataP)
{
Expand All @@ -102,18 +115,7 @@ static uint8_t prv_set_value(lwm2m_data_t * dataP,

case RES_M_AVL_NETWORK_BEARER:
{
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
count = dataP->value.asChildren.count;
subTlvP = dataP->value.asChildren.array;
}
else
{
count = 1; // reduced to 1 instance to fit in one block size
subTlvP = lwm2m_data_new(count);
for (i = 0; i < count; i++) subTlvP[i].id = i;
lwm2m_data_encode_instances(subTlvP, count, dataP);
}
reduce_single_instance(dataP, &subTlvP, &count);

for (i = 0; i < count; i++)
{
Expand Down Expand Up @@ -141,18 +143,7 @@ static uint8_t prv_set_value(lwm2m_data_t * dataP,

case RES_M_IP_ADDRESSES:
{
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
count = dataP->value.asChildren.count;
subTlvP = dataP->value.asChildren.array;
}
else
{
count = 1; // reduced to 1 instance to fit in one block size
subTlvP = lwm2m_data_new(count);
for (i = 0; i < count; i++) subTlvP[i].id = i;
lwm2m_data_encode_instances(subTlvP, count, dataP);
}
reduce_single_instance(dataP, &subTlvP, &count);

for (i = 0; i < count; i++)
{
Expand Down Expand Up @@ -204,18 +195,7 @@ static uint8_t prv_set_value(lwm2m_data_t * dataP,

case RES_O_APN:
{
if (dataP->type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
count = dataP->value.asChildren.count;
subTlvP = dataP->value.asChildren.array;
}
else
{
count = 1; // reduced to 1 instance to fit in one block size
subTlvP = lwm2m_data_new(count);
for (i = 0; i < count; i++) subTlvP[i].id = i;
lwm2m_data_encode_instances(subTlvP, count, dataP);
}
reduce_single_instance(dataP, &subTlvP, &count);

for (i = 0; i < count; i++)
{
Expand Down

0 comments on commit 0b4b967

Please sign in to comment.