From 54b7b28fec3efa4cbdf862b204d4531623b79cee Mon Sep 17 00:00:00 2001 From: Scott Bertin Date: Thu, 11 Jul 2019 08:38:41 -0400 Subject: [PATCH 1/2] Implement Server Object resources Resources implemented are: Disable (4), Registration Update Trigger (8), Bootstrap-Request Trigger (9), and Last Bootstrapped (12). The default value of 86400 is also assigned to Disable Timeout (5) when creating instances if not provided. Signed-off-by: Scott Bertin --- core/bootstrap.c | 11 ++++++ core/internals.h | 2 +- core/liblwm2m.c | 30 +++++++++++++++ core/objects.c | 4 -- core/registration.c | 56 +++++++++++++++++++--------- examples/client/lwm2mclient.c | 31 +++++++++------ examples/client/object_server.c | 56 +++++++++++++++++++++++----- examples/lightclient/object_server.c | 23 ++++++++---- include/liblwm2m.h | 13 ++++++- 9 files changed, 175 insertions(+), 51 deletions(-) diff --git a/core/bootstrap.c b/core/bootstrap.c index d844c4f6a..03e884b6a 100644 --- a/core/bootstrap.c +++ b/core/bootstrap.c @@ -1028,6 +1028,17 @@ uint8_t bootstrap_handleDeleteAll(lwm2m_context_t * contextP, return result; } + +int lwm2m_initiate_bootstrap(lwm2m_context_t * contextP) +{ + if (contextP->state != STATE_INITIAL + && contextP->state != STATE_BOOTSTRAPPING) + { + contextP->state = STATE_BOOTSTRAP_REQUIRED; + return NO_ERROR; + } + return COAP_405_METHOD_NOT_ALLOWED; +} #endif #endif diff --git a/core/internals.h b/core/internals.h index a99e26d3f..533bdae01 100644 --- a/core/internals.h +++ b/core/internals.h @@ -347,7 +347,7 @@ void applyObservationCallback(lwm2m_observation_t * observation, int status, blo // defined in registration.c uint8_t registration_handleRequest(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, void * fromSessionH, coap_packet_t * message, coap_packet_t * response); -void registration_deregister(lwm2m_context_t * contextP, lwm2m_server_t * serverP); +uint8_t registration_deregister(lwm2m_context_t * contextP, lwm2m_server_t * serverP); void registration_freeClient(lwm2m_client_t * clientP); uint8_t registration_start(lwm2m_context_t * contextP, bool restartFailed); void registration_step(lwm2m_context_t * contextP, time_t currentTime, time_t * timeoutP); diff --git a/core/liblwm2m.c b/core/liblwm2m.c index c5b3da187..b500db829 100644 --- a/core/liblwm2m.c +++ b/core/liblwm2m.c @@ -88,6 +88,36 @@ void lwm2m_deregister(lwm2m_context_t * context) } } +int lwm2m_registration_disable(lwm2m_context_t * contextP, + uint16_t shortServerID, + time_t timeout) +{ + int result = COAP_404_NOT_FOUND; + lwm2m_server_t * serverP; + + for (serverP = contextP->serverList; serverP; serverP = serverP->next) + { + if (serverP->shortID == shortServerID) + { + result = registration_deregister(contextP, serverP); + if (result == NO_ERROR) + { + if (serverP->status != STATE_DEREG_PENDING && serverP->sessionH != NULL) + { + lwm2m_close_connection(serverP->sessionH, contextP->userData); + serverP->sessionH = NULL; + } + + serverP->registration = lwm2m_gettime() + timeout; + serverP->status = STATE_REG_HOLD_OFF; + } + break; + } + } + + return result; +} + static void prv_deleteServer(lwm2m_server_t * serverP, void *userData) { // TODO parse transaction and observation to remove the ones related to this server diff --git a/core/objects.c b/core/objects.c index 28c7b5fd6..f6d95ccf3 100644 --- a/core/objects.c +++ b/core/objects.c @@ -1096,9 +1096,7 @@ int object_getServers(lwm2m_context_t * contextP, bool checkOnly) if (isBootstrap) { targetP->shortID = 0; -#ifndef LWM2M_VERSION_1_0 targetP->servObjInstID = LWM2M_MAX_ID; -#endif lwm2m_data_free(size, dataP); size = 1; @@ -1169,9 +1167,7 @@ int object_getServers(lwm2m_context_t * contextP, bool checkOnly) } else { -#ifndef LWM2M_VERSION_1_0 targetP->servObjInstID = serverInstP->id; -#endif if (0 != prv_getMandatoryInfo(contextP, serverObjP, serverInstP->id, targetP)) { lwm2m_free(targetP); diff --git a/core/registration.c b/core/registration.c index 4a082c7af..67340d0ee 100644 --- a/core/registration.c +++ b/core/registration.c @@ -1130,16 +1130,26 @@ static void prv_handleDeregistrationReply(lwm2m_context_t * contextP, targetP = (lwm2m_server_t *)(transacP->userData); if (NULL != targetP) { - if (targetP->status == STATE_DEREG_PENDING) + if (targetP->status == STATE_DEREG_PENDING || + targetP->status == STATE_REG_HOLD_OFF) { - targetP->status = STATE_DEREGISTERED; + if (targetP->sessionH != NULL) + { + lwm2m_close_connection(targetP->sessionH, contextP->userData); + targetP->sessionH = NULL; + } + if (targetP->status == STATE_DEREG_PENDING) + { + targetP->status = STATE_DEREGISTERED; + } } } } -void registration_deregister(lwm2m_context_t * contextP, +uint8_t registration_deregister(lwm2m_context_t * contextP, lwm2m_server_t * serverP) { + int result = NO_ERROR; lwm2m_transaction_t * transaction; LOG_ARG("State: %s, %d status: %s", STR_STATE(contextP->state), serverP->shortID, STR_STATUS(serverP->status)); @@ -1150,22 +1160,36 @@ void registration_deregister(lwm2m_context_t * contextP, || serverP->status == STATE_REG_FAILED || serverP->location == NULL) { - return; + result = COAP_405_METHOD_NOT_ALLOWED; } + else + { + transaction = transaction_new(serverP->sessionH, COAP_DELETE, NULL, NULL, contextP->nextMID++, 4, NULL); + if (transaction != NULL) + { + coap_set_header_uri_path(transaction->message, serverP->location); - transaction = transaction_new(serverP->sessionH, COAP_DELETE, NULL, NULL, contextP->nextMID++, 4, NULL); - if (transaction == NULL) return; - - coap_set_header_uri_path(transaction->message, serverP->location); - - transaction->callback = prv_handleDeregistrationReply; - transaction->userData = (void *) serverP; + transaction->callback = prv_handleDeregistrationReply; + transaction->userData = (void *) serverP; - contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); - if (transaction_send(contextP, transaction) == 0) - { - serverP->status = STATE_DEREG_PENDING; + contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); + result = transaction_send(contextP, transaction); + if (result == 0) + { + serverP->status = STATE_DEREG_PENDING; + } + else if (result < 0) + { + result = COAP_500_INTERNAL_SERVER_ERROR; + } + } + else + { + result = COAP_500_INTERNAL_SERVER_ERROR; + } } + + return result; } #endif @@ -2048,7 +2072,6 @@ void registration_step(lwm2m_context_t * contextP, { switch (targetP->status) { -#ifndef LWM2M_VERSION_1_0 case STATE_REG_HOLD_OFF: { time_t interval = targetP->registration - currentTime; @@ -2062,7 +2085,6 @@ void registration_step(lwm2m_context_t * contextP, } break; } -#endif case STATE_REGISTERED: { time_t nextUpdate; diff --git a/examples/client/lwm2mclient.c b/examples/client/lwm2mclient.c index 57e76906c..8bc39303a 100644 --- a/examples/client/lwm2mclient.c +++ b/examples/client/lwm2mclient.c @@ -393,21 +393,30 @@ static void prv_output_servers(lwm2m_context_t * lwm2mH, case STATE_DEREGISTERED: fprintf(stdout, "DEREGISTERED\r\n"); break; + case STATE_REG_HOLD_OFF: + fprintf(stdout, "REGISTRATION HOLD OFF\r\n"); + break; case STATE_REG_PENDING: fprintf(stdout, "REGISTRATION PENDING\r\n"); break; case STATE_REGISTERED: fprintf(stdout, "REGISTERED\tlocation: \"%s\"\tLifetime: %lus\r\n", targetP->location, (unsigned long)targetP->lifetime); break; + case STATE_REG_FAILED: + fprintf(stdout, "REGISTRATION FAILED\r\n"); + break; case STATE_REG_UPDATE_PENDING: fprintf(stdout, "REGISTRATION UPDATE PENDING\r\n"); break; + case STATE_REG_UPDATE_NEEDED: + fprintf(stdout, "REGISTRATION UPDATE NEEDED\r\n"); + break; + case STATE_REG_FULL_UPDATE_NEEDED: + fprintf(stdout, "REGISTRATION FULL UPDATE NEEDED\r\n"); + break; case STATE_DEREG_PENDING: fprintf(stdout, "DEREGISTRATION PENDING\r\n"); break; - case STATE_REG_FAILED: - fprintf(stdout, "REGISTRATION FAILED\r\n"); - break; default: fprintf(stdout, "INVALID (%d)\r\n", (int)targetP->status); } @@ -657,18 +666,18 @@ static void prv_initiate_bootstrap(lwm2m_context_t * lwm2mH, char * buffer, void * user_data) { - lwm2m_server_t * targetP; + int res; - /* unused parameter */ + /* unused parameters */ + (void)buffer; (void)user_data; - // HACK !!! - lwm2mH->state = STATE_BOOTSTRAP_REQUIRED; - targetP = lwm2mH->bootstrapServerList; - while (targetP != NULL) + res = lwm2m_initiate_bootstrap(lwm2mH); + if (res != 0) { - targetP->lifetime = 0; - targetP = targetP->next; + fprintf(stdout, "Initiating bootstrap failed: "); + print_status(stdout, res); + fprintf(stdout, "\r\n"); } } diff --git a/examples/client/object_server.c b/examples/client/object_server.c index 6d48c6aee..d7f96e1d8 100644 --- a/examples/client/object_server.c +++ b/examples/client/object_server.c @@ -33,6 +33,8 @@ * Binding | 7 | R/W | Single | Yes | String | | | * Registration Update | 8 | E | Single | Yes | | | | #ifndef LWM2M_VERSION_1_0 + * Bootstrap-Request Trigger | 9 | E | Single | No | | | | + * Last Bootstrapped | 12 | R/W | Single | No | Time | | | * Registration Priority Order | 13 | R/W | Single | No | Unsigned | | | * Initial Registration Delay Timer | 14 | R/W | Single | No | Unsigned | | s | * Registration Failure Block | 15 | R/W | Single | No | Boolean | | | @@ -51,6 +53,7 @@ #include #include #include +#include typedef struct _server_instance_ { @@ -64,6 +67,7 @@ typedef struct _server_instance_ bool storing; char binding[4]; #ifndef LWM2M_VERSION_1_0 + time_t lastBootstrapped; int registrationPriorityOrder; // <0 when it doesn't exist int initialRegistrationDelayTimer; // <0 when it doesn't exist int8_t registrationFailureBlock; // <0 when it doesn't exist, 0 for false, > 0 for true @@ -124,6 +128,10 @@ static uint8_t prv_get_value(lwm2m_data_t * dataP, return COAP_405_METHOD_NOT_ALLOWED; #ifndef LWM2M_VERSION_1_0 + case LWM2M_SERVER_LAST_BOOTSTRAP_ID: + lwm2m_data_encode_int(targetP->lastBootstrapped, dataP); + return COAP_205_CONTENT; + case LWM2M_SERVER_REG_ORDER_ID: if (targetP->registrationPriorityOrder >= 0) { @@ -247,6 +255,7 @@ static uint8_t prv_server_read(lwm2m_context_t *contextP, LWM2M_SERVER_STORING_ID, LWM2M_SERVER_BINDING_ID, #ifndef LWM2M_VERSION_1_0 + LWM2M_SERVER_LAST_BOOTSTRAP_ID, LWM2M_SERVER_REG_ORDER_ID, LWM2M_SERVER_INITIAL_REG_DELAY_ID, LWM2M_SERVER_REG_FAIL_BLOCK_ID, @@ -417,6 +426,7 @@ static uint8_t prv_server_discover(lwm2m_context_t *contextP, LWM2M_SERVER_BINDING_ID, LWM2M_SERVER_UPDATE_ID, #ifndef LWM2M_VERSION_1_0 + LWM2M_SERVER_BOOTSTRAP_ID, LWM2M_SERVER_REG_ORDER_ID, LWM2M_SERVER_INITIAL_REG_DELAY_ID, LWM2M_SERVER_REG_FAIL_BLOCK_ID, @@ -554,6 +564,9 @@ static uint8_t prv_server_discover(lwm2m_context_t *contextP, case LWM2M_SERVER_UPDATE_ID: break; #ifndef LWM2M_VERSION_1_0 + case LWM2M_SERVER_BOOTSTRAP_ID: + case LWM2M_SERVER_LAST_BOOTSTRAP_ID: + break; case LWM2M_SERVER_REG_ORDER_ID: if(targetP->registrationPriorityOrder < 0) { @@ -937,6 +950,13 @@ static uint8_t prv_server_write(lwm2m_context_t *contextP, i++; } while (i < numData && result == COAP_204_CHANGED); +#ifndef LWM2M_VERSION_1_0 + if (result == COAP_204_CHANGED && contextP->state == STATE_BOOTSTRAPPING) + { + targetP->lastBootstrapped = time(NULL); + } +#endif + return result; } @@ -949,9 +969,7 @@ static uint8_t prv_server_execute(lwm2m_context_t *contextP, { server_instance_t * targetP; - - /* unused parameter */ - (void)contextP; + int res = COAP_405_METHOD_NOT_ALLOWED; targetP = (server_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId); if (NULL == targetP) return COAP_404_NOT_FOUND; @@ -959,15 +977,31 @@ static uint8_t prv_server_execute(lwm2m_context_t *contextP, switch (resourceId) { case LWM2M_SERVER_DISABLE_ID: - // executed in core, if COAP_204_CHANGED is returned - if (0 < targetP->disableTimeout) return COAP_204_CHANGED; - else return COAP_405_METHOD_NOT_ALLOWED; + if (targetP->shortServerId != 0 && 0 < targetP->disableTimeout) + { + res = lwm2m_registration_disable(contextP, targetP->shortServerId, targetP->disableTimeout); + } + break; + case LWM2M_SERVER_UPDATE_ID: - // executed in core, if COAP_204_CHANGED is returned - return COAP_204_CHANGED; + if (targetP->shortServerId != 0) + { + res = lwm2m_update_registration(contextP, targetP->shortServerId, false); + } + break; + +#if !defined(LWM2M_VERSION_1_0) && defined(LWM2M_BOOTSTRAP) + case LWM2M_SERVER_BOOTSTRAP_ID: + res = lwm2m_initiate_bootstrap(contextP); + break; +#endif + default: - return COAP_405_METHOD_NOT_ALLOWED; + break; } + + if (res == 0) res = COAP_204_CHANGED; + return res; } static uint8_t prv_server_delete(lwm2m_context_t *contextP, @@ -1001,7 +1035,9 @@ static uint8_t prv_server_create(lwm2m_context_t *contextP, memset(serverInstance, 0, sizeof(server_instance_t)); serverInstance->instanceId = instanceId; + serverInstance->disableTimeout = 86400; #ifndef LWM2M_VERSION_1_0 + serverInstance->lastBootstrapped = (time_t)-1; serverInstance->registrationPriorityOrder = -1; serverInstance->initialRegistrationDelayTimer = -1; serverInstance->registrationFailureBlock = -1; @@ -1126,9 +1162,11 @@ lwm2m_object_t * get_server_object(int serverId, serverInstance->instanceId = 0; serverInstance->shortServerId = serverId; serverInstance->lifetime = lifetime; + serverInstance->disableTimeout = 86400; serverInstance->storing = storing; memcpy (serverInstance->binding, binding, strlen(binding)+1); #ifndef LWM2M_VERSION_1_0 + serverInstance->lastBootstrapped = (time_t)-1; serverInstance->registrationPriorityOrder = -1; serverInstance->initialRegistrationDelayTimer = -1; serverInstance->registrationFailureBlock = -1; diff --git a/examples/lightclient/object_server.c b/examples/lightclient/object_server.c index 1e4bc5fb0..53519997d 100644 --- a/examples/lightclient/object_server.c +++ b/examples/lightclient/object_server.c @@ -362,27 +362,34 @@ static uint8_t prv_server_execute(lwm2m_context_t * contextP, lwm2m_object_t * objectP) { + int res = COAP_405_METHOD_NOT_ALLOWED; server_instance_t * targetP; - /* Unused parameter */ - (void)contextP; - targetP = (server_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId); if (NULL == targetP) return COAP_404_NOT_FOUND; switch (resourceId) { case LWM2M_SERVER_DISABLE_ID: - // executed in core, if COAP_204_CHANGED is returned - return COAP_204_CHANGED; + if (targetP->shortServerId != 0) + { + res = lwm2m_registration_disable(contextP, targetP->shortServerId, 86400); + } + break; case LWM2M_SERVER_UPDATE_ID: - // executed in core, if COAP_204_CHANGED is returned - return COAP_204_CHANGED; + if (targetP->shortServerId != 0) + { + res = lwm2m_update_registration(contextP, targetP->shortServerId, false); + } + break; default: - return COAP_405_METHOD_NOT_ALLOWED; + break; } + + if (res == 0) res = COAP_204_CHANGED; + return res; } static uint8_t prv_server_delete(lwm2m_context_t * contextP, diff --git a/include/liblwm2m.h b/include/liblwm2m.h index db15f2599..e7947808a 100644 --- a/include/liblwm2m.h +++ b/include/liblwm2m.h @@ -573,8 +573,8 @@ typedef struct _lwm2m_server_ char * location; bool dirty; lwm2m_block_data_t * blockData; // list to handle temporary block data. -#ifndef LWM2M_VERSION_1_0 uint16_t servObjInstID;// Server object instance ID if not a bootstrap server. +#ifndef LWM2M_VERSION_1_0 uint8_t attempt; // Current registration attempt uint8_t sequence; // Current registration sequence #endif @@ -822,6 +822,17 @@ int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID // send deregistration to all servers connected to client void lwm2m_deregister(lwm2m_context_t * context); void lwm2m_resource_value_changed(lwm2m_context_t * contextP, lwm2m_uri_t * uriP); + +// Deregister from a server for a period of time +int lwm2m_registration_disable(lwm2m_context_t * contextP, + uint16_t shortServerID, + time_t timeout); + +#ifdef LWM2M_BOOTSTRAP +// Start a client initiated bootstrap +int lwm2m_initiate_bootstrap(lwm2m_context_t * contextP); +#endif + #endif #ifdef LWM2M_SERVER_MODE From e9a648ed5badcb8b85e6e626bf562a40c2fc1cdf Mon Sep 17 00:00:00 2001 From: Scott Bertin Date: Mon, 29 Mar 2021 11:18:16 -0400 Subject: [PATCH 2/2] Delay deregistration slightly. This allows the response to an Exec of /1/0/4 to be sent to the server before the deregistration is sent. Signed-off-by: Scott Bertin --- core/internals.h | 3 +- core/liblwm2m.c | 3 +- core/registration.c | 67 +++++++++++++++++++----------- examples/client/lwm2mclient.c | 3 ++ examples/lightclient/lightclient.c | 3 ++ include/liblwm2m.h | 1 + 6 files changed, 53 insertions(+), 27 deletions(-) diff --git a/core/internals.h b/core/internals.h index 533bdae01..b7886b695 100644 --- a/core/internals.h +++ b/core/internals.h @@ -98,6 +98,7 @@ ((S) == STATE_REG_UPDATE_PENDING ? "STATE_REG_UPDATE_PENDING" : \ ((S) == STATE_REG_UPDATE_NEEDED ? "STATE_REG_UPDATE_NEEDED" : \ ((S) == STATE_REG_FULL_UPDATE_NEEDED ? "STATE_REG_FULL_UPDATE_NEEDED" : \ +((S) == STATE_DEREG_NEEDED ? "STATE_DEREG_NEEDED" : \ ((S) == STATE_DEREG_PENDING ? "STATE_DEREG_PENDING" : \ ((S) == STATE_BS_HOLD_OFF ? "STATE_BS_HOLD_OFF" : \ ((S) == STATE_BS_INITIATED ? "STATE_BS_INITIATED" : \ @@ -106,7 +107,7 @@ ((S) == STATE_BS_FINISHING ? "STATE_BS_FINISHING" : \ ((S) == STATE_BS_FAILING ? "STATE_BS_FAILING" : \ ((S) == STATE_BS_FAILED ? "STATE_BS_FAILED" : \ -"Unknown")))))))))))))))) +"Unknown"))))))))))))))))) #define STR_MEDIA_TYPE(M) \ ((M) == LWM2M_CONTENT_TEXT ? "LWM2M_CONTENT_TEXT" : \ ((M) == LWM2M_CONTENT_LINK ? "LWM2M_CONTENT_LINK" : \ diff --git a/core/liblwm2m.c b/core/liblwm2m.c index b500db829..c1c6efe2e 100644 --- a/core/liblwm2m.c +++ b/core/liblwm2m.c @@ -102,14 +102,13 @@ int lwm2m_registration_disable(lwm2m_context_t * contextP, result = registration_deregister(contextP, serverP); if (result == NO_ERROR) { - if (serverP->status != STATE_DEREG_PENDING && serverP->sessionH != NULL) + if (serverP->status != STATE_DEREG_NEEDED && serverP->sessionH != NULL) { lwm2m_close_connection(serverP->sessionH, contextP->userData); serverP->sessionH = NULL; } serverP->registration = lwm2m_gettime() + timeout; - serverP->status = STATE_REG_HOLD_OFF; } break; } diff --git a/core/registration.c b/core/registration.c index 67340d0ee..8df370029 100644 --- a/core/registration.c +++ b/core/registration.c @@ -1093,6 +1093,7 @@ lwm2m_status_t registration_getStatus(lwm2m_context_t * contextP) case STATE_REG_UPDATE_NEEDED: case STATE_REG_FULL_UPDATE_NEEDED: case STATE_REG_UPDATE_PENDING: + case STATE_DEREG_NEEDED: case STATE_DEREG_PENDING: if (reg_status == STATE_REG_FAILED) { @@ -1150,7 +1151,9 @@ uint8_t registration_deregister(lwm2m_context_t * contextP, lwm2m_server_t * serverP) { int result = NO_ERROR; - lwm2m_transaction_t * transaction; +#ifndef LWM2M_WITH_LOGS + (void)contextP; /* unused */ +#endif LOG_ARG("State: %s, %d status: %s", STR_STATE(contextP->state), serverP->shortID, STR_STATUS(serverP->status)); @@ -1164,29 +1167,7 @@ uint8_t registration_deregister(lwm2m_context_t * contextP, } else { - transaction = transaction_new(serverP->sessionH, COAP_DELETE, NULL, NULL, contextP->nextMID++, 4, NULL); - if (transaction != NULL) - { - coap_set_header_uri_path(transaction->message, serverP->location); - - transaction->callback = prv_handleDeregistrationReply; - transaction->userData = (void *) serverP; - - contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); - result = transaction_send(contextP, transaction); - if (result == 0) - { - serverP->status = STATE_DEREG_PENDING; - } - else if (result < 0) - { - result = COAP_500_INTERNAL_SERVER_ERROR; - } - } - else - { - result = COAP_500_INTERNAL_SERVER_ERROR; - } + serverP->status = STATE_DEREG_NEEDED; } return result; @@ -2129,6 +2110,44 @@ void registration_step(lwm2m_context_t * contextP, } break; + case STATE_DEREG_NEEDED: + { + lwm2m_transaction_t *transaction; + int result; + LOG_ARG("%d Deregistering", targetP->shortID); + transaction = transaction_new(targetP->sessionH, COAP_DELETE, NULL, NULL, contextP->nextMID++, 4, NULL); + if (transaction != NULL) + { + coap_set_header_uri_path(transaction->message, targetP->location); + + transaction->callback = prv_handleDeregistrationReply; + transaction->userData = (void *) targetP; + + contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); + result = transaction_send(contextP, transaction); + if (result == 0) + { + if (targetP->registration <= lwm2m_gettime()) + { + targetP->status = STATE_DEREG_PENDING; + } + else + { + targetP->status = STATE_REG_HOLD_OFF; + } + } + else if (result < 0) + { + targetP->status = STATE_REG_FAILED; + } + } + else + { + targetP->status = STATE_REG_FAILED; + } + } + break; + default: break; } diff --git a/examples/client/lwm2mclient.c b/examples/client/lwm2mclient.c index 8bc39303a..93e48d282 100644 --- a/examples/client/lwm2mclient.c +++ b/examples/client/lwm2mclient.c @@ -414,6 +414,9 @@ static void prv_output_servers(lwm2m_context_t * lwm2mH, case STATE_REG_FULL_UPDATE_NEEDED: fprintf(stdout, "REGISTRATION FULL UPDATE NEEDED\r\n"); break; + case STATE_DEREG_NEEDED: + fprintf(stdout, "DEREGISTRATION NEEDED\r\n"); + break; case STATE_DEREG_PENDING: fprintf(stdout, "DEREGISTRATION PENDING\r\n"); break; diff --git a/examples/lightclient/lightclient.c b/examples/lightclient/lightclient.c index a356b81cf..793aec4a1 100644 --- a/examples/lightclient/lightclient.c +++ b/examples/lightclient/lightclient.c @@ -303,6 +303,9 @@ void print_state(lwm2m_context_t * lwm2mH) case STATE_REG_UPDATE_NEEDED: fprintf(stderr, "REGISTRATION UPDATE REQUIRED\r\n"); break; + case STATE_DEREG_NEEDED: + fprintf(stdout, "DEREGISTRATION NEEDED\r\n"); + break; case STATE_DEREG_PENDING: fprintf(stderr, "DEREGISTRATION PENDING\r\n"); break; diff --git a/include/liblwm2m.h b/include/liblwm2m.h index e7947808a..d5366296d 100644 --- a/include/liblwm2m.h +++ b/include/liblwm2m.h @@ -495,6 +495,7 @@ typedef enum STATE_REG_UPDATE_PENDING, // registration update pending STATE_REG_UPDATE_NEEDED, // registration update required STATE_REG_FULL_UPDATE_NEEDED, // registration update with objects required + STATE_DEREG_NEEDED, // deregistration needed STATE_DEREG_PENDING, // deregistration pending STATE_BS_HOLD_OFF, // bootstrap hold off time STATE_BS_INITIATED, // bootstrap request sent