Skip to content

Commit

Permalink
Support for registration update
Browse files Browse the repository at this point in the history
Registration update can be used for keeping NAT route open
For now the registration update nothing in the registration
parameters, it just send a PUT on the registration path.
  • Loading branch information
jvermillard committed Aug 5, 2014
1 parent c3def89 commit 7c3bd2d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
8 changes: 7 additions & 1 deletion core/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Fabien Fleutot - Please refer to git log
* Simon Bernard - Please refer to git log
* Toby Jaffey - Please refer to git log
*
* Julien Vermillard - Please refer to git log
*******************************************************************************/

/*
Expand Down Expand Up @@ -284,6 +284,7 @@ typedef enum
STATE_UNKNOWN = 0,
STATE_REG_PENDING,
STATE_REGISTERED,
STATE_REG_UPDATE_PENDING,
STATE_DEREG_PENDING
} lwm2m_status_t;

Expand Down Expand Up @@ -407,6 +408,7 @@ typedef struct _lwm2m_watcher_
} lwm2m_watcher_t;

typedef struct _lwm2m_observed_

{
struct _lwm2m_observed_ * next;

Expand Down Expand Up @@ -463,6 +465,10 @@ int lwm2m_add_server(lwm2m_context_t * contextP, uint16_t shortID, void * sessio

// send registration message to all known LWM2M Servers.
int lwm2m_register(lwm2m_context_t * contextP);

// send a registration update to the server specified by the server short identifier
int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID);

// inform liblwm2m that a resource value has changed.
void lwm2m_resource_value_changed(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
#endif
Expand Down
81 changes: 78 additions & 3 deletions core/registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* Simon Bernard - Please refer to git log
* Toby Jaffey - Please refer to git log
* Manuel Sangoi - Please refer to git log
*
* Julien Vermillard - Please refer to git log
*
*******************************************************************************/

/*
Expand Down Expand Up @@ -149,6 +150,81 @@ int lwm2m_register(lwm2m_context_t * contextP)
return 0;
}

static void prv_handleRegistrationUpdateReply(lwm2m_transaction_t * transacP,
void * message)
{
lwm2m_server_t * targetP;
coap_packet_t * packet = (coap_packet_t *)message;

targetP = (lwm2m_server_t *)(transacP->peerP);

switch(targetP->status)
{
case STATE_REG_UPDATE_PENDING:
{
if (packet == NULL)
{
targetP->status = STATE_UNKNOWN;
targetP->mid = 0;
}
else if (packet->mid == targetP->mid
&& packet->type == COAP_TYPE_ACK)
{
if (packet->code == CHANGED_2_04)
{
targetP->status = STATE_REGISTERED;
}
else if (packet->code == BAD_REQUEST_4_00)
{
targetP->status = STATE_UNKNOWN;
targetP->mid = 0;
}
}
}
break;
default:
break;
}
}

int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID)
{
// look for the server
lwm2m_server_t * targetP;
targetP = contextP->serverList;
while (targetP != NULL)
{
if (targetP->shortID == shortServerID)
{
// found the server, trigger the update transaction
lwm2m_transaction_t * transaction;

transaction = transaction_new(COAP_PUT, NULL, contextP->nextMID++, ENDPOINT_SERVER, (void *)targetP);
if (transaction == NULL) return INTERNAL_SERVER_ERROR_5_00;

coap_set_header_uri_path(transaction->message, targetP->location);

transaction->callback = prv_handleRegistrationUpdateReply;
transaction->userData = (void *) contextP;

contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction);

if (transaction_send(contextP, transaction) == 0)
{
targetP->status = STATE_REG_UPDATE_PENDING;
targetP->mid = transaction->mID;
}
return 0;
} else {
// try next server
targetP = targetP->next;
}
}

// no server found
return NOT_FOUND_4_04;
}

static void prv_handleDeregistrationReply(lwm2m_transaction_t * transacP,
void * message)
{
Expand All @@ -167,8 +243,7 @@ static void prv_handleDeregistrationReply(lwm2m_transaction_t * transacP,
targetP->mid = 0;
}
else if (packet->mid == targetP->mid
&& packet->type == COAP_TYPE_ACK
&& packet->location_path != NULL)
&& packet->type == COAP_TYPE_ACK)
{
if (packet->code == DELETED_2_02)
{
Expand Down
25 changes: 25 additions & 0 deletions tests/client/lwm2mclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ static void prv_output_servers(char * buffer,
case STATE_REGISTERED:
fprintf(stdout, "REGISTERED location: \"%s\"\r\n", targetP->location);
break;
case STATE_REG_UPDATE_PENDING:
fprintf(stdout, "REGISTRATION UPDATE PENDING\r\n");
break;
case STATE_DEREG_PENDING:
fprintf(stdout, "DEREGISTRATION PENDING\r\n");
break;
}
fprintf(stdout, "\r\n");
}
Expand Down Expand Up @@ -225,6 +231,23 @@ static void prv_change(char * buffer,
fprintf(stdout, "Syntax error !\n");
}

static void prv_update(char * buffer,
void * user_data)
{
lwm2m_context_t * lwm2mH = (lwm2m_context_t *) user_data;
if (buffer[0] == 0) goto syntax_error;

uint16_t serverId = (uint16_t) atoi(buffer);
int res = lwm2m_update_registration(lwm2mH, serverId);
if (res != 0) {
fprintf(stdout, "Registration update error: %d\n",res);
}
return;

syntax_error:
fprintf(stdout, "Syntax error !\n");
}

int main(int argc, char *argv[])
{
int sock;
Expand Down Expand Up @@ -255,6 +278,8 @@ int main(int argc, char *argv[])
{"change", "Change the value of resource.", " change URI [DATA]\r\n"
" URI: uri of the resource such as /3/0, /3/0/2\r\n"
" DATA: (optional) new value\r\n", prv_change, NULL},
{"update", "Trigger a registration update", " update SERVER\r\n"
" SERVER: short server id such as 123\r\n", prv_update, NULL},
{"quit", "Quit the client gracefully.", NULL, prv_quit, NULL},
{"^C", "Quit the client abruptly (without sending a de-register message).", NULL, NULL, NULL},

Expand Down

0 comments on commit 7c3bd2d

Please sign in to comment.