From bd0ad5bd662ca812f43ce68af66f100105c068d1 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Tue, 17 Oct 2023 13:47:47 +0300 Subject: [PATCH] net: lwm2m: Initialize empty string sizes correctly on objects When objects are initialized, empty strings should be set to length of zero, instead of length of the full buffer. So use INIT_OBJ_RES_DATA_LEN() to give both, the buffer size and data length. Signed-off-by: Seppo Takalo --- subsys/net/lib/lwm2m/ipso_current_sensor.c | 8 ++++---- subsys/net/lib/lwm2m/ipso_generic_sensor.c | 12 ++++++------ subsys/net/lib/lwm2m/ipso_humidity_sensor.c | 4 ++-- subsys/net/lib/lwm2m/ipso_light_control.c | 8 ++++---- subsys/net/lib/lwm2m/ipso_pressure_sensor.c | 4 ++-- subsys/net/lib/lwm2m/ipso_temp_sensor.c | 4 ++-- subsys/net/lib/lwm2m/ipso_voltage_sensor.c | 8 ++++---- subsys/net/lib/lwm2m/lwm2m_obj_device.c | 4 ++-- subsys/net/lib/lwm2m/lwm2m_obj_gateway.c | 21 ++++++++++++--------- subsys/net/lib/lwm2m/lwm2m_obj_server.c | 9 ++++----- subsys/net/lib/lwm2m/ucifi_lpwan.c | 4 ++-- 11 files changed, 44 insertions(+), 42 deletions(-) diff --git a/subsys/net/lib/lwm2m/ipso_current_sensor.c b/subsys/net/lib/lwm2m/ipso_current_sensor.c index 99b12ea667..0684db9422 100644 --- a/subsys/net/lib/lwm2m/ipso_current_sensor.c +++ b/subsys/net/lib/lwm2m/ipso_current_sensor.c @@ -177,8 +177,8 @@ static struct lwm2m_engine_obj_inst *current_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1, false, true, &sensor_value[index], sizeof(*sensor_value), NULL, NULL, NULL, sensor_value_write_cb, NULL); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, - units[index], UNIT_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, + units[index], UNIT_STR_MAX_SIZE, 0); INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i, res_inst[index], j, &min_measured_value[index], sizeof(*min_measured_value)); @@ -194,8 +194,8 @@ static struct lwm2m_engine_obj_inst *current_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES_DATA(CURRENT_CALIBRATION_RID, res[index], i, res_inst[index], j, &calibration_coefficient[index], sizeof(*calibration_coefficient)); - INIT_OBJ_RES_DATA(APPLICATION_TYPE_RID, res[index], i, res_inst[index], - j, app_type[index], APP_TYPE_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(APPLICATION_TYPE_RID, res[index], i, res_inst[index], + j, app_type[index], APP_TYPE_STR_MAX_SIZE, 0); #if defined(CONFIG_LWM2M_IPSO_CURRENT_SENSOR_VERSION_1_1) INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j); diff --git a/subsys/net/lib/lwm2m/ipso_generic_sensor.c b/subsys/net/lib/lwm2m/ipso_generic_sensor.c index 5f1d9801a7..def650ce9e 100644 --- a/subsys/net/lib/lwm2m/ipso_generic_sensor.c +++ b/subsys/net/lib/lwm2m/ipso_generic_sensor.c @@ -186,8 +186,8 @@ static struct lwm2m_engine_obj_inst *generic_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1, false, true, &sensor_value[index], sizeof(*sensor_value), NULL, NULL, NULL, sensor_value_write_cb, NULL); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, - units[index], UNIT_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, + units[index], UNIT_STR_MAX_SIZE, 0); INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i, res_inst[index], j, &min_measured_value[index], sizeof(*min_measured_value)); @@ -200,10 +200,10 @@ static struct lwm2m_engine_obj_inst *generic_sensor_create(uint16_t obj_inst_id) j, &max_range_value[index], sizeof(*max_range_value)); INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID, res[index], i, reset_min_max_measured_values_cb); - INIT_OBJ_RES_DATA(APPLICATION_TYPE_RID, res[index], i, res_inst[index], - j, app_type[index], APP_TYPE_STR_MAX_SIZE); - INIT_OBJ_RES_DATA(SENSOR_TYPE_RID, res[index], i, res_inst[index], j, - sensor_type[index], SENSOR_TYPE_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(APPLICATION_TYPE_RID, res[index], i, res_inst[index], + j, app_type[index], APP_TYPE_STR_MAX_SIZE, 0); + INIT_OBJ_RES_DATA_LEN(SENSOR_TYPE_RID, res[index], i, res_inst[index], j, + sensor_type[index], SENSOR_TYPE_STR_MAX_SIZE, 0); #if defined(CONFIG_LWM2M_IPSO_GENERIC_SENSOR_VERSION_1_1) INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j); diff --git a/subsys/net/lib/lwm2m/ipso_humidity_sensor.c b/subsys/net/lib/lwm2m/ipso_humidity_sensor.c index fe338d1436..24be33d37f 100644 --- a/subsys/net/lib/lwm2m/ipso_humidity_sensor.c +++ b/subsys/net/lib/lwm2m/ipso_humidity_sensor.c @@ -170,8 +170,8 @@ humidity_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1, false, true, &sensor_value[index], sizeof(*sensor_value), NULL, NULL, NULL, sensor_value_write_cb, NULL); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, - units[index], UNIT_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, + units[index], UNIT_STR_MAX_SIZE, 0); INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i, res_inst[index], j, &min_measured_value[index], sizeof(*min_measured_value)); diff --git a/subsys/net/lib/lwm2m/ipso_light_control.c b/subsys/net/lib/lwm2m/ipso_light_control.c index beeb5f0f54..fc9a897461 100644 --- a/subsys/net/lib/lwm2m/ipso_light_control.c +++ b/subsys/net/lib/lwm2m/ipso_light_control.c @@ -171,10 +171,10 @@ static struct lwm2m_engine_obj_inst *light_control_create(uint16_t obj_inst_id) INIT_OBJ_RES_DATA(POWER_FACTOR_RID, res[avail], i, res_inst[avail], j, &power_factor_value[avail], sizeof(*power_factor_value)); - INIT_OBJ_RES_DATA(COLOUR_RID, res[avail], i, res_inst[avail], j, - colour[avail], LIGHT_STRING_LONG); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[avail], i, res_inst[avail], j, - units[avail], LIGHT_STRING_SHORT); + INIT_OBJ_RES_DATA_LEN(COLOUR_RID, res[avail], i, res_inst[avail], j, + colour[avail], LIGHT_STRING_LONG, 0); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[avail], i, res_inst[avail], j, + units[avail], LIGHT_STRING_SHORT, 0); INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i, res_inst[avail], j); diff --git a/subsys/net/lib/lwm2m/ipso_pressure_sensor.c b/subsys/net/lib/lwm2m/ipso_pressure_sensor.c index f58c718aed..87f87e4a27 100644 --- a/subsys/net/lib/lwm2m/ipso_pressure_sensor.c +++ b/subsys/net/lib/lwm2m/ipso_pressure_sensor.c @@ -171,8 +171,8 @@ pressure_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1, false, true, &sensor_value[index], sizeof(*sensor_value), NULL, NULL, NULL, sensor_value_write_cb, NULL); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, - units[index], UNIT_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, + units[index], UNIT_STR_MAX_SIZE, 0); INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i, res_inst[index], j, &min_measured_value[index], sizeof(*min_measured_value)); diff --git a/subsys/net/lib/lwm2m/ipso_temp_sensor.c b/subsys/net/lib/lwm2m/ipso_temp_sensor.c index ff5a3bf767..bdf5956383 100644 --- a/subsys/net/lib/lwm2m/ipso_temp_sensor.c +++ b/subsys/net/lib/lwm2m/ipso_temp_sensor.c @@ -172,8 +172,8 @@ static struct lwm2m_engine_obj_inst *temp_sensor_create(uint16_t obj_inst_id) res_inst[index], j, 1, false, true, &sensor_value[index], sizeof(*sensor_value), NULL, NULL, NULL, sensor_value_write_cb, NULL); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, - units[index], UNIT_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, + units[index], UNIT_STR_MAX_SIZE, 0); INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i, res_inst[index], j, &min_measured_value[index], sizeof(*min_measured_value)); diff --git a/subsys/net/lib/lwm2m/ipso_voltage_sensor.c b/subsys/net/lib/lwm2m/ipso_voltage_sensor.c index 1e4eff60b1..55b4590223 100644 --- a/subsys/net/lib/lwm2m/ipso_voltage_sensor.c +++ b/subsys/net/lib/lwm2m/ipso_voltage_sensor.c @@ -178,8 +178,8 @@ static struct lwm2m_engine_obj_inst *voltage_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1, false, true, &sensor_value[index], sizeof(*sensor_value), NULL, NULL, NULL, sensor_value_write_cb, NULL); - INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, - units[index], UNIT_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j, + units[index], UNIT_STR_MAX_SIZE, 0); INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i, res_inst[index], j, &min_measured_value[index], sizeof(*min_measured_value)); @@ -195,8 +195,8 @@ static struct lwm2m_engine_obj_inst *voltage_sensor_create(uint16_t obj_inst_id) INIT_OBJ_RES_DATA(CURRENT_CALIBRATION_RID, res[index], i, res_inst[index], j, &calibration_coefficient[index], sizeof(*calibration_coefficient)); - INIT_OBJ_RES_DATA(APPLICATION_TYPE_RID, res[index], i, res_inst[index], - j, app_type[index], APP_TYPE_STR_MAX_SIZE); + INIT_OBJ_RES_DATA_LEN(APPLICATION_TYPE_RID, res[index], i, res_inst[index], + j, app_type[index], APP_TYPE_STR_MAX_SIZE, 0); #if defined(CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_1) INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j); diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_device.c b/subsys/net/lib/lwm2m/lwm2m_obj_device.c index 84db2f8c14..c17815795c 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_device.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_device.c @@ -251,8 +251,8 @@ static struct lwm2m_engine_obj_inst *device_create(uint16_t obj_inst_id) NULL, current_time_post_write_cb, NULL); INIT_OBJ_RES_OPTDATA(DEVICE_UTC_OFFSET_ID, res, i, res_inst, j); INIT_OBJ_RES_OPTDATA(DEVICE_TIMEZONE_ID, res, i, res_inst, j); - INIT_OBJ_RES_DATA(DEVICE_SUPPORTED_BINDING_MODES_ID, res, i, - res_inst, j, binding_mode, DEVICE_STRING_SHORT); + INIT_OBJ_RES_DATA_LEN(DEVICE_SUPPORTED_BINDING_MODES_ID, res, i, + res_inst, j, binding_mode, DEVICE_STRING_SHORT, strlen(binding_mode) + 1); INIT_OBJ_RES_OPTDATA(DEVICE_TYPE_ID, res, i, res_inst, j); INIT_OBJ_RES_OPTDATA(DEVICE_HARDWARE_VERSION_ID, res, i, res_inst, j); INIT_OBJ_RES_OPTDATA(DEVICE_SOFTWARE_VERSION_ID, res, i, res_inst, j); diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_gateway.c b/subsys/net/lib/lwm2m/lwm2m_obj_gateway.c index 23b92fb19e..711fd5fe12 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_gateway.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_gateway.c @@ -127,15 +127,18 @@ static struct lwm2m_engine_obj_inst *lwm2m_gw_create(uint16_t obj_inst_id) init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index])); /* initialize instance resource data */ - INIT_OBJ_RES_DATA(LWM2M_GATEWAY_DEVICE_RID, res[index], i, res_inst[index], j, - device_table[index].device_id, - CONFIG_LWM2M_GATEWAY_DEVICE_ID_MAX_STR_SIZE); - INIT_OBJ_RES(LWM2M_GATEWAY_PREFIX_RID, res[index], i, res_inst[index], j, 1, false, true, - device_table[index].prefix, CONFIG_LWM2M_GATEWAY_PREFIX_MAX_STR_SIZE, NULL, - NULL, prefix_validation_cb, NULL, NULL); - INIT_OBJ_RES_DATA(LWM2M_GATEWAY_IOT_DEVICE_OBJECTS_RID, res[index], i, res_inst[index], j, - device_table[index].iot_device_objects, - sizeof(device_table[index].iot_device_objects)); + INIT_OBJ_RES_DATA_LEN(LWM2M_GATEWAY_DEVICE_RID, res[index], i, res_inst[index], j, + device_table[index].device_id, + CONFIG_LWM2M_GATEWAY_DEVICE_ID_MAX_STR_SIZE, + strlen(device_table[index].device_id) + 1); + INIT_OBJ_RES_LEN(LWM2M_GATEWAY_PREFIX_RID, res[index], i, res_inst[index], j, 1, false, + true, device_table[index].prefix, CONFIG_LWM2M_GATEWAY_PREFIX_MAX_STR_SIZE, + strlen(device_table[index].prefix) + 1, NULL, NULL, prefix_validation_cb, + NULL, NULL); + INIT_OBJ_RES_DATA_LEN(LWM2M_GATEWAY_IOT_DEVICE_OBJECTS_RID, res[index], i, res_inst[index], + j, device_table[index].iot_device_objects, + sizeof(device_table[index].iot_device_objects), + strlen(device_table[index].iot_device_objects) + 1); inst[index].resources = res[index]; inst[index].resource_count = i; diff --git a/subsys/net/lib/lwm2m/lwm2m_obj_server.c b/subsys/net/lib/lwm2m/lwm2m_obj_server.c index a73206df25..a1b2185ef1 100644 --- a/subsys/net/lib/lwm2m/lwm2m_obj_server.c +++ b/subsys/net/lib/lwm2m/lwm2m_obj_server.c @@ -301,11 +301,10 @@ static struct lwm2m_engine_obj_inst *server_create(uint16_t obj_inst_id) &server_flag_store_notify[index], sizeof(*server_flag_store_notify)); /* Mark Transport Binding RO as we only support UDP atm */ - INIT_OBJ_RES_DATA(SERVER_TRANSPORT_BINDING_ID, res[index], i, - res_inst[index], j, - transport_binding[index], TRANSPORT_BINDING_LEN); - INIT_OBJ_RES_EXECUTE(SERVER_REG_UPDATE_TRIGGER_ID, res[index], i, - update_trigger_cb); + INIT_OBJ_RES_DATA_LEN(SERVER_TRANSPORT_BINDING_ID, res[index], i, res_inst[index], j, + transport_binding[index], TRANSPORT_BINDING_LEN, + strlen(transport_binding[index]) + 1); + INIT_OBJ_RES_EXECUTE(SERVER_REG_UPDATE_TRIGGER_ID, res[index], i, update_trigger_cb); #if defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1) INIT_OBJ_RES_EXECUTE(SERVER_BOOTSTRAP_UPDATE_TRIGGER_ID, res[index], i, bootstrap_trigger_cb); diff --git a/subsys/net/lib/lwm2m/ucifi_lpwan.c b/subsys/net/lib/lwm2m/ucifi_lpwan.c index 4210387a20..8287872440 100644 --- a/subsys/net/lib/lwm2m/ucifi_lpwan.c +++ b/subsys/net/lib/lwm2m/ucifi_lpwan.c @@ -132,8 +132,8 @@ static struct lwm2m_engine_obj_inst *lpwan_create(uint16_t obj_inst_id) j, NETWORK_ADDRESS_MAX, false); INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_SECONDARY_ADDRESS_RID, res[index], i, res_inst[index], j, SECONDARY_NETWORK_ADDRESS_MAX, false); - INIT_OBJ_RES_DATA(UCIFI_LPWAN_MAC_ADDRESS_RID, res[index], i, res_inst[index], j, - mac[index], MAC_ADDRESS_SIZE); + INIT_OBJ_RES_DATA_LEN(UCIFI_LPWAN_MAC_ADDRESS_RID, res[index], i, res_inst[index], j, + mac[index], MAC_ADDRESS_SIZE, 0); INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_PEER_ADDRESS_RID, res[index], i, res_inst[index], j, PEER_ADDRESS_MAX, false); INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_MULTICAST_GRP_ADDRESS_RID, res[index], i,