Skip to content
Permalink
Browse files

net: lwm2m: cleanup memset usage during init

Several problems with memset usage in the LwM2M subsystem were
identified:
- Every single object that can have multiple instances is using
  memset to initialize static resource data during init.  This data
  will already be set to 0 because it is static, so the memset
  statements are unneeded.
- Instead of using memset during object init which is only called
  one time during kernel startup, let's add a memset to the
  object create function to ensure the resource data is cleared out.
  It could have been used prior and then released via a DELETE op.
- the IPSO Timer object was setting a lot of data structure members
  to 0 in the create function.  Let's do 1 memset on the entire
  structure and then only the non-zero values afterward.

Signed-off-by: Michael Scott <mike@foundries.io>
  • Loading branch information...
mike-scott authored and jukkar committed Aug 1, 2019
1 parent 6af8fa6 commit e81332d4bdc96e10085bd0d655dfe6dc6de5527a
@@ -161,6 +161,8 @@ static struct lwm2m_engine_obj_inst *light_control_create(u16_t obj_inst_id)
colour[avail][0] = '\0';
units[avail][0] = '\0';

(void)memset(res[avail], 0,
sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));

/* initialize instance resource data */
@@ -199,11 +201,6 @@ static struct lwm2m_engine_obj_inst *light_control_create(u16_t obj_inst_id)

static int ipso_light_control_init(struct device *dev)
{
/* Set default values */
(void)memset(inst, 0, sizeof(*inst) * MAX_INSTANCE_COUNT);
(void)memset(res, 0, sizeof(struct lwm2m_engine_res) *
MAX_INSTANCE_COUNT * LIGHT_MAX_ID);

light_control.obj_id = IPSO_OBJECT_LIGHT_CONTROL_ID;
light_control.fields = fields;
light_control.field_count = ARRAY_SIZE(fields);
@@ -182,6 +182,8 @@ static struct lwm2m_engine_obj_inst *temp_sensor_create(u16_t obj_inst_id)
max_range_value[index].val1 = 0;
max_range_value[index].val2 = 0;

(void)memset(res[index], 0,
sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));

/* initialize instance resource data */
@@ -214,11 +216,6 @@ static struct lwm2m_engine_obj_inst *temp_sensor_create(u16_t obj_inst_id)

static int ipso_temp_sensor_init(struct device *dev)
{
/* Set default values */
(void)memset(inst, 0, sizeof(*inst) * MAX_INSTANCE_COUNT);
(void)memset(res, 0, sizeof(struct lwm2m_engine_res) *
MAX_INSTANCE_COUNT * TEMP_MAX_ID);

temp_sensor.obj_id = IPSO_OBJECT_TEMP_SENSOR_ID;
temp_sensor.fields = fields;
temp_sensor.field_count = ARRAY_SIZE(fields);
@@ -324,21 +324,15 @@ static struct lwm2m_engine_obj_inst *timer_create(u16_t obj_inst_id)
}

/* Set default values */
(void)memset(&timer_data[avail], 0, sizeof(timer_data[avail]));
k_delayed_work_init(&timer_data[avail].timer_work, timer_work_cb);
timer_data[avail].trigger_offset = 0U;
timer_data[avail].delay_duration.val1 = 5; /* 5 seconds */
timer_data[avail].delay_duration.val2 = 0;
timer_data[avail].remaining_time.val1 = 0;
timer_data[avail].remaining_time.val2 = 0;
timer_data[avail].min_off_time.val1 = 0;
timer_data[avail].min_off_time.val2 = 0;
timer_data[avail].cumulative_time.val1 = 0;
timer_data[avail].cumulative_time.val2 = 0;
timer_data[avail].active = false;
timer_data[avail].enabled = true;
timer_data[avail].timer_mode = TIMER_MODE_ONE_SHOT;
timer_data[avail].obj_inst_id = obj_inst_id;

(void)memset(res[avail], 0,
sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));

/* initialize instance resource data */
@@ -390,11 +384,6 @@ static struct lwm2m_engine_obj_inst *timer_create(u16_t obj_inst_id)

static int ipso_timer_init(struct device *dev)
{
/* Set default values */
(void)memset(inst, 0, sizeof(*inst) * MAX_INSTANCE_COUNT);
(void)memset(res, 0, sizeof(struct lwm2m_engine_res) *
MAX_INSTANCE_COUNT * TIMER_MAX_ID);

timer.obj_id = IPSO_OBJECT_TIMER_ID;
timer.fields = fields;
timer.field_count = ARRAY_SIZE(fields);
@@ -109,6 +109,8 @@ static struct lwm2m_engine_obj_inst *security_create(u16_t obj_inst_id)
security_mode[index] = 0U;
short_server_id[index] = 0U;

(void)memset(res[index], 0,
sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));

/* initialize instance resource data */
@@ -173,11 +175,6 @@ static int lwm2m_security_init(struct device *dev)
struct lwm2m_engine_obj_inst *obj_inst = NULL;
int ret = 0;

/* Set default values */
(void)memset(inst, 0, sizeof(*inst) * MAX_INSTANCE_COUNT);
(void)memset(res, 0, sizeof(struct lwm2m_engine_res) *
MAX_INSTANCE_COUNT * SECURITY_MAX_ID);

security.obj_id = LWM2M_OBJECT_SECURITY_ID;
security.fields = fields;
security.field_count = ARRAY_SIZE(fields);
@@ -168,6 +168,8 @@ static struct lwm2m_engine_obj_inst *server_create(u16_t obj_inst_id)
disabled_timeout[index] = 86400U;
strcpy(transport_binding[index], "U");

(void)memset(res[index], 0,
sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));

/* initialize instance resource data */
@@ -212,11 +214,6 @@ static int lwm2m_server_init(struct device *dev)
struct lwm2m_engine_obj_inst *obj_inst = NULL;
int ret = 0;

/* Set default values */
(void)memset(inst, 0, sizeof(*inst) * MAX_INSTANCE_COUNT);
(void)memset(res, 0, sizeof(struct lwm2m_engine_res) *
MAX_INSTANCE_COUNT * SERVER_MAX_ID);

server.obj_id = LWM2M_OBJECT_SERVER_ID;
server.fields = fields;
server.field_count = ARRAY_SIZE(fields);

0 comments on commit e81332d

Please sign in to comment.
You can’t perform that action at this time.