Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Server Object resources #561

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" : \
Expand All @@ -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" : \
Expand Down Expand Up @@ -347,7 +348,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);
Expand Down
29 changes: 29 additions & 0 deletions core/liblwm2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ 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_NEEDED && serverP->sessionH != NULL)
{
lwm2m_close_connection(serverP->sessionH, contextP->userData);
serverP->sessionH = NULL;
}

serverP->registration = lwm2m_gettime() + timeout;
}
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
Expand Down
4 changes: 0 additions & 4 deletions core/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
79 changes: 60 additions & 19 deletions core/registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -1130,17 +1131,29 @@ 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)
{
lwm2m_transaction_t * transaction;
int result = NO_ERROR;
#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));

Expand All @@ -1150,22 +1163,14 @@ void registration_deregister(lwm2m_context_t * contextP,
|| serverP->status == STATE_REG_FAILED
|| serverP->location == NULL)
{
return;
result = COAP_405_METHOD_NOT_ALLOWED;
}

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;

contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);
if (transaction_send(contextP, transaction) == 0)
else
{
serverP->status = STATE_DEREG_PENDING;
serverP->status = STATE_DEREG_NEEDED;
}

return result;
}
#endif

Expand Down Expand Up @@ -2048,7 +2053,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;
Expand All @@ -2062,7 +2066,6 @@ void registration_step(lwm2m_context_t * contextP,
}
break;
}
#endif
case STATE_REGISTERED:
{
time_t nextUpdate;
Expand Down Expand Up @@ -2107,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;
}
Expand Down
34 changes: 23 additions & 11 deletions examples/client/lwm2mclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,33 @@ 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_NEEDED:
fprintf(stdout, "DEREGISTRATION 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);
}
Expand Down Expand Up @@ -657,18 +669,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");
}
}

Expand Down
Loading