Skip to content

Commit

Permalink
net: lib: lwm2m: Replace deprecated function calls
Browse files Browse the repository at this point in the history
Replace calls to deprecated functions with new ones.

Signed-off-by: Jarno Lämsä <[email protected]>
  • Loading branch information
Jarno Lämsä authored and carlescufi committed Jan 19, 2023
1 parent 3b3463e commit abafd7e
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 130 deletions.
14 changes: 6 additions & 8 deletions subsys/net/lib/lwm2m/ipso_buzzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ static int get_buzzer_index(uint16_t obj_inst_id)
static int start_buzzer(struct ipso_buzzer_data *buzzer)
{
uint32_t temp = 0U;
char path[MAX_RESOURCE_LEN];
struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_BUZZER_ID, buzzer->obj_inst_id,
DIGITAL_INPUT_STATE_RID);

/* make sure buzzer is currently not active */
if (buzzer->active) {
Expand All @@ -116,9 +117,7 @@ static int start_buzzer(struct ipso_buzzer_data *buzzer)
/* TODO: check delay_duration > 0 */

buzzer->trigger_offset = k_uptime_get();
snprintk(path, MAX_RESOURCE_LEN, "%d/%u/%d", IPSO_OBJECT_BUZZER_ID,
buzzer->obj_inst_id, DIGITAL_INPUT_STATE_RID);
lwm2m_engine_set_bool(path, true);
lwm2m_set_bool(&path, true);

temp = (uint32_t)(buzzer->delay_duration * MSEC_PER_SEC);
k_work_reschedule(&buzzer->buzzer_work, K_MSEC(temp));
Expand All @@ -128,16 +127,15 @@ static int start_buzzer(struct ipso_buzzer_data *buzzer)

static int stop_buzzer(struct ipso_buzzer_data *buzzer, bool cancel)
{
char path[MAX_RESOURCE_LEN];
struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_BUZZER_ID, buzzer->obj_inst_id,
DIGITAL_INPUT_STATE_RID);

/* make sure buzzer is currently active */
if (!buzzer->active) {
return -EINVAL;
}

snprintk(path, MAX_RESOURCE_LEN, "%d/%u/%d", IPSO_OBJECT_BUZZER_ID,
buzzer->obj_inst_id, DIGITAL_INPUT_STATE_RID);
lwm2m_engine_set_bool(path, false);
lwm2m_set_bool(&path, false);

if (cancel) {
k_work_cancel_delayable(&buzzer->buzzer_work);
Expand Down
12 changes: 5 additions & 7 deletions subsys/net/lib/lwm2m/ipso_push_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ static int state_post_write_cb(uint16_t obj_inst_id,
bool last_block, size_t total_size)
{
int i;
char path[MAX_RESOURCE_LEN];

i = get_button_index(obj_inst_id);
if (i < 0) {
Expand All @@ -98,17 +97,16 @@ static int state_post_write_cb(uint16_t obj_inst_id,
if (button_data[i].state && !button_data[i].last_state) {
/* off to on transition, increment the counter */
int64_t counter = button_data[i].counter + 1;
struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_PUSH_BUTTON_ID, obj_inst_id,
DIGITAL_INPUT_COUNTER_RID);

if (counter < 0) {
counter = 0;
}

snprintk(path, sizeof(path), "%u/%u/%u",
IPSO_OBJECT_PUSH_BUTTON_ID, obj_inst_id,
DIGITAL_INPUT_COUNTER_RID);

if (lwm2m_engine_set_s64(path, counter) < 0) {
LOG_ERR("Failed to increment counter resource %s", path);
if (lwm2m_set_s64(&path, counter) < 0) {
LOG_ERR("Failed to increment counter resource %d/%d/%d", path.obj_id,
path.obj_inst_id, path.res_id);
}
}

Expand Down
14 changes: 6 additions & 8 deletions subsys/net/lib/lwm2m/ipso_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ static int get_timer_index(uint16_t obj_inst_id)
static int start_timer(struct ipso_timer_data *timer)
{
uint32_t temp = 0U;
char path[MAX_RESOURCE_LEN];
struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_TIMER_ID, timer->obj_inst_id,
DIGITAL_STATE_RID);

/* make sure timer is enabled and not already active */
if (timer->timer_mode == TIMER_MODE_OFF || timer->active ||
Expand All @@ -123,9 +124,7 @@ static int start_timer(struct ipso_timer_data *timer)
timer->trigger_offset = k_uptime_get();
timer->trigger_counter += 1U;

snprintk(path, MAX_RESOURCE_LEN, "%d/%u/%d", IPSO_OBJECT_TIMER_ID,
timer->obj_inst_id, DIGITAL_STATE_RID);
lwm2m_engine_set_bool(path, true);
lwm2m_set_bool(&path, true);

temp = timer->delay_duration * MSEC_PER_SEC;
k_work_reschedule(&timer->timer_work, K_MSEC(temp));
Expand All @@ -135,17 +134,16 @@ static int start_timer(struct ipso_timer_data *timer)

static int stop_timer(struct ipso_timer_data *timer, bool cancel)
{
char path[MAX_RESOURCE_LEN];
struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_TIMER_ID, timer->obj_inst_id,
DIGITAL_STATE_RID);

/* make sure timer is active */
if (!timer->active) {
return -EINVAL;
}

timer->cumulative_time_ms += k_uptime_get() - timer->trigger_offset;
snprintk(path, MAX_RESOURCE_LEN, "%d/%u/%d", IPSO_OBJECT_TIMER_ID,
timer->obj_inst_id, DIGITAL_STATE_RID);
lwm2m_engine_set_bool(path, false);
lwm2m_set_bool(&path, false);

if (cancel) {
k_work_cancel_delayable(&timer->timer_work);
Expand Down
20 changes: 8 additions & 12 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,12 @@ int lwm2m_engine_validate_write_access(struct lwm2m_message *msg,
#if defined(CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP)
static bool bootstrap_delete_allowed(int obj_id, int obj_inst_id)
{
char pathstr[MAX_RESOURCE_LEN];
bool bootstrap_server;
int ret;

if (obj_id == LWM2M_OBJECT_SECURITY_ID) {
snprintk(pathstr, sizeof(pathstr), "%d/%d/1", LWM2M_OBJECT_SECURITY_ID,
obj_inst_id);
ret = lwm2m_engine_get_bool(pathstr, &bootstrap_server);
ret = lwm2m_get_bool(&LWM2M_OBJ(LWM2M_OBJECT_SECURITY_ID, obj_inst_id, 1),
&bootstrap_server);
if (ret < 0) {
return false;
}
Expand Down Expand Up @@ -787,16 +785,15 @@ static int load_tls_credential(struct lwm2m_ctx *client_ctx, uint16_t res_id,
void *cred = NULL;
uint16_t cred_len;
uint8_t cred_flags;
char pathstr[MAX_RESOURCE_LEN];

/* ignore error value */
tls_credential_delete(client_ctx->tls_tag, type);

snprintk(pathstr, sizeof(pathstr), "0/%d/%u", client_ctx->sec_obj_inst, res_id);

ret = lwm2m_engine_get_res_buf(pathstr, &cred, NULL, &cred_len, &cred_flags);
ret = lwm2m_get_res_buf(&LWM2M_OBJ(0, client_ctx->sec_obj_inst, res_id), &cred, NULL,
&cred_len, &cred_flags);
if (ret < 0) {
LOG_ERR("Unable to get resource data for '%s'", pathstr);
LOG_ERR("Unable to get resource data for %d/%d/%d", 0, client_ctx->sec_obj_inst,
res_id);
return ret;
}

Expand Down Expand Up @@ -957,15 +954,14 @@ int lwm2m_engine_stop(struct lwm2m_ctx *client_ctx)

int lwm2m_engine_start(struct lwm2m_ctx *client_ctx)
{
char pathstr[MAX_RESOURCE_LEN];
char *url;
uint16_t url_len;
uint8_t url_data_flags;
int ret = 0U;

/* get the server URL */
snprintk(pathstr, sizeof(pathstr), "0/%d/0", client_ctx->sec_obj_inst);
ret = lwm2m_engine_get_res_buf(pathstr, (void **)&url, NULL, &url_len, &url_data_flags);
ret = lwm2m_get_res_buf(&LWM2M_OBJ(0, client_ctx->sec_obj_inst, 0), (void **)&url, NULL,
&url_len, &url_data_flags);
if (ret < 0) {
return ret;
}
Expand Down
25 changes: 10 additions & 15 deletions subsys/net/lib/lwm2m/lwm2m_obj_firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,23 @@ uint8_t lwm2m_firmware_get_update_state(void)
void lwm2m_firmware_set_update_state_inst(uint16_t obj_inst_id, uint8_t state)
{
bool error = false;
char path[LWM2M_MAX_PATH_STR_SIZE];

snprintk(path, sizeof(path), "%" PRIu16 "/%" PRIu16 "/%" PRIu16,
LWM2M_OBJECT_FIRMWARE_ID, obj_inst_id, FIRMWARE_UPDATE_RESULT_ID);
struct lwm2m_obj_path path = LWM2M_OBJ(LWM2M_OBJECT_FIRMWARE_ID, obj_inst_id,
FIRMWARE_UPDATE_RESULT_ID);

/* Check LWM2M SPEC appendix E.6.1 */
switch (state) {
case STATE_DOWNLOADING:
if (update_state[obj_inst_id] == STATE_IDLE) {
lwm2m_engine_set_u8(path, RESULT_DEFAULT);
lwm2m_set_u8(&path, RESULT_DEFAULT);
} else {
error = true;
}
break;
case STATE_DOWNLOADED:
if (update_state[obj_inst_id] == STATE_DOWNLOADING) {
lwm2m_engine_set_u8(path, RESULT_DEFAULT);
lwm2m_set_u8(&path, RESULT_DEFAULT);
} else if (update_state[obj_inst_id] == STATE_UPDATING) {
lwm2m_engine_set_u8(path, RESULT_UPDATE_FAILED);
lwm2m_set_u8(&path, RESULT_UPDATE_FAILED);
} else {
error = true;
}
Expand All @@ -137,10 +135,9 @@ void lwm2m_firmware_set_update_state_inst(uint16_t obj_inst_id, uint8_t state)
update_state[obj_inst_id], state);
}

snprintk(path, sizeof(path), "%" PRIu16 "/%" PRIu16 "/%" PRIu16,
LWM2M_OBJECT_FIRMWARE_ID, obj_inst_id, FIRMWARE_STATE_ID);
path.res_id = FIRMWARE_STATE_ID;

lwm2m_engine_set_u8(path, state);
lwm2m_set_u8(&path, state);

LOG_DBG("Update state = %d", state);
}
Expand All @@ -164,7 +161,8 @@ void lwm2m_firmware_set_update_result_inst(uint16_t obj_inst_id, uint8_t result)
{
uint8_t state;
bool error = false;
char path[LWM2M_MAX_PATH_STR_SIZE];
struct lwm2m_obj_path path = LWM2M_OBJ(LWM2M_OBJECT_FIRMWARE_ID, obj_inst_id,
FIRMWARE_UPDATE_RESULT_ID);

/* Check LWM2M SPEC appendix E.6.1 */
switch (result) {
Expand Down Expand Up @@ -220,10 +218,7 @@ void lwm2m_firmware_set_update_result_inst(uint16_t obj_inst_id, uint8_t result)
result, state);
}

snprintk(path, sizeof(path), "%" PRIu16 "/%" PRIu16 "/%" PRIu16,
LWM2M_OBJECT_FIRMWARE_ID, obj_inst_id, FIRMWARE_UPDATE_RESULT_ID);

lwm2m_engine_set_u8(path, result);
lwm2m_set_u8(&path, result);

LOG_DBG("Update result = %d", result);
}
Expand Down
24 changes: 12 additions & 12 deletions subsys/net/lib/lwm2m/lwm2m_obj_swmgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ static void *callback_read_not_defined(uint16_t obj_inst_id, uint16_t res_id, ui
static void set_sw_update_state(struct lwm2m_swmgmt_data *instance, uint8_t state)
{
int ret;
char obj_path[LWM2M_MAX_PATH_STR_SIZE];
struct lwm2m_obj_path obj_path = LWM2M_OBJ(LWM2M_OBJECT_SOFTWARE_MANAGEMENT_ID,
instance->obj_inst_id,
SWMGMT_UPDATE_STATE_ID);

(void)snprintk(obj_path, sizeof(obj_path), "%d/%d/%d", LWM2M_OBJECT_SOFTWARE_MANAGEMENT_ID,
instance->obj_inst_id, SWMGMT_UPDATE_STATE_ID);
ret = lwm2m_engine_set_u8(obj_path, state);
ret = lwm2m_set_u8(obj_path, state);
if (ret != 0) {
LOG_ERR("Could not set state");
}
Expand All @@ -218,11 +218,11 @@ static void set_sw_update_state(struct lwm2m_swmgmt_data *instance, uint8_t stat
static void set_sw_update_result(struct lwm2m_swmgmt_data *instance, uint8_t result)
{
int ret;
char obj_path[LWM2M_MAX_PATH_STR_SIZE];
struct lwm2m_obj_path obj_path = LWM2M_OBJ(LWM2M_OBJECT_SOFTWARE_MANAGEMENT_ID,
instance->obj_inst_id,
SWMGMT_UPDATE_RESULT_ID);

(void)snprintk(obj_path, sizeof(obj_path), "%d/%d/%d", LWM2M_OBJECT_SOFTWARE_MANAGEMENT_ID,
instance->obj_inst_id, SWMGMT_UPDATE_RESULT_ID);
ret = lwm2m_engine_set_u8(obj_path, result);
ret = lwm2m_set_u8(&obj_path, result);
if (ret != 0) {
LOG_ERR("Could not set result");
}
Expand All @@ -231,11 +231,11 @@ static void set_sw_update_result(struct lwm2m_swmgmt_data *instance, uint8_t res
static void set_sw_update_act_state(struct lwm2m_swmgmt_data *instance, bool state)
{
int ret;
char obj_path[LWM2M_MAX_PATH_STR_SIZE];
struct lwm2m_obj_path obj_path = LWM2M_OBJ(LWM2M_OBJECT_SOFTWARE_MANAGEMENT_ID,
instance->obj_inst_id,
SWMGMT_ACTIVATION_UPD_STATE_ID);

(void)snprintk(obj_path, sizeof(obj_path), "%d/%d/%d", LWM2M_OBJECT_SOFTWARE_MANAGEMENT_ID,
instance->obj_inst_id, SWMGMT_ACTIVATION_UPD_STATE_ID);
ret = lwm2m_engine_set_bool(obj_path, state);
ret = lwm2m_set_bool(&obj_path, state);
if (ret != 0) {
LOG_ERR("Could not set activation state");
}
Expand Down
12 changes: 3 additions & 9 deletions subsys/net/lib/lwm2m/lwm2m_rd_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,10 @@ static void do_deregister_timeout_cb(struct lwm2m_message *msg)

static bool sm_bootstrap_verify(bool bootstrap_server, int sec_obj_inst)
{
char pathstr[MAX_RESOURCE_LEN];
bool bootstrap;
int ret;

snprintk(pathstr, sizeof(pathstr), "0/%d/1", sec_obj_inst);
ret = lwm2m_engine_get_bool(pathstr, &bootstrap);
ret = lwm2m_get_bool(&LWM2M_OBJ(0, sec_obj_inst, 1), &bootstrap);
if (ret < 0) {
LOG_WRN("Failed to check bootstrap, err %d", ret);
return false;
Expand All @@ -591,11 +589,9 @@ static bool sm_bootstrap_verify(bool bootstrap_server, int sec_obj_inst)

static bool sm_update_lifetime(int srv_obj_inst, uint32_t *lifetime)
{
char pathstr[MAX_RESOURCE_LEN];
uint32_t new_lifetime;

snprintk(pathstr, sizeof(pathstr), "1/%d/1", srv_obj_inst);
if (lwm2m_engine_get_u32(pathstr, &new_lifetime) < 0) {
if (lwm2m_get_u32(&LWM2M_OBJ(1, srv_obj_inst, 1), &new_lifetime) < 0) {
new_lifetime = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME;
LOG_INF("Using default lifetime: %u", new_lifetime);
}
Expand All @@ -611,12 +607,10 @@ static bool sm_update_lifetime(int srv_obj_inst, uint32_t *lifetime)
static int sm_select_server_inst(int sec_obj_inst, int *srv_obj_inst,
uint32_t *lifetime)
{
char pathstr[MAX_RESOURCE_LEN];
uint16_t server_id;
int ret, obj_inst_id;

snprintk(pathstr, sizeof(pathstr), "0/%d/10", sec_obj_inst);
ret = lwm2m_engine_get_u16(pathstr, &server_id);
ret = lwm2m_get_u16(&LWM2M_OBJ(0, sec_obj_inst, 10), &server_id);
if (ret < 0) {
LOG_WRN("Failed to obtain Short Server ID, err %d", ret);
return -EINVAL;
Expand Down
19 changes: 3 additions & 16 deletions subsys/net/lib/lwm2m/lwm2m_rw_link_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,7 @@ static int put_corelink_ssid(struct lwm2m_output_context *out,
case LWM2M_OBJECT_SECURITY_ID: {
bool bootstrap_inst;

ret = snprintk(buf, buflen, "0/%d/1",
path->obj_inst_id);
if (ret < 0 || ret >= buflen) {
return -ENOMEM;
}

ret = lwm2m_engine_get_bool(buf, &bootstrap_inst);
ret = lwm2m_get_bool(&LWM2M_OBJ(0, path->obj_inst_id, 1), &bootstrap_inst);
if (ret < 0) {
return ret;
}
Expand All @@ -283,13 +277,11 @@ static int put_corelink_ssid(struct lwm2m_output_context *out,
return 0;
}

ret = snprintk(buf, buflen, "0/%d/10",
path->obj_inst_id);
if (ret < 0 || ret >= buflen) {
return -ENOMEM;
}

ret = lwm2m_engine_get_u16(buf, &server_id);
ret = lwm2m_get_u16(&LWM2M_OBJ(0, path->obj_inst_id, 10), &server_id);
if (ret < 0) {
return ret;
}
Expand All @@ -298,12 +290,7 @@ static int put_corelink_ssid(struct lwm2m_output_context *out,
}

case LWM2M_OBJECT_SERVER_ID:
ret = snprintk(buf, buflen, "1/%d/0", path->obj_inst_id);
if (ret < 0 || ret >= buflen) {
return -ENOMEM;
}

ret = lwm2m_engine_get_u16(buf, &server_id);
ret = lwm2m_get_u16(&LWM2M_OBJ(1, path->obj_inst_id, 0), &server_id);
if (ret < 0) {
return ret;
}
Expand Down
Loading

0 comments on commit abafd7e

Please sign in to comment.