// update the registration of a given server int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID, bool withObjects) { lwm2m_server_t * targetP; uint8_t result; LOG_ARG("State: %s, shortServerID: %d", STR_STATE(contextP->state), shortServerID); result = COAP_NO_ERROR; targetP = contextP->serverList; if (targetP == NULL) { if (object_getServers(contextP) == -1) { LOG("No server found"); return COAP_404_NOT_FOUND; } } while (targetP != NULL && result == COAP_NO_ERROR) { if (shortServerID != 0) { if (targetP->shortID == shortServerID) { // found the server, trigger the update transaction if (targetP->status == STATE_REGISTERED) { return prv_updateRegistration(contextP, targetP, withObjects); } else { return COAP_400_BAD_REQUEST; } } } else { if (targetP->status == STATE_REGISTERED) { result = prv_updateRegistration(contextP, targetP, withObjects); } } targetP = targetP->next; } if (shortServerID != 0 && targetP == NULL) { // no server found result = COAP_404_NOT_FOUND; } return result; }
// update the registration of a given server int lwm2m_update_registration(lwm2m_context_t * contextP, uint16_t shortServerID, bool withObjects) { lwm2m_server_t * targetP; uint8_t result; result = COAP_NO_ERROR; targetP = contextP->serverList; if (targetP == NULL) { if (object_getServers(contextP) == -1) { return COAP_404_NOT_FOUND; } } while (targetP != NULL && result == COAP_NO_ERROR) { if (shortServerID != 0) { if (targetP->shortID == shortServerID) { // found the server, trigger the update transaction return prv_updateRegistration(contextP, targetP, withObjects); } } else { result = prv_updateRegistration(contextP, targetP, withObjects); } targetP = targetP->next; } if (shortServerID != 0 && targetP == NULL) { // no server found result = COAP_404_NOT_FOUND; } return result; }
// for each server update the registration if needed // for each client check if the registration expired void registration_step(lwm2m_context_t * contextP, time_t currentTime, time_t * timeoutP) { #ifdef LWM2M_CLIENT_MODE lwm2m_server_t * targetP = contextP->serverList; targetP = contextP->serverList; while (targetP != NULL) { switch (targetP->status) { case STATE_REGISTERED: { time_t nextUpdate; time_t interval; nextUpdate = targetP->lifetime; if (30 < nextUpdate) { nextUpdate -= 15; // update 15s earlier to have a chance to resend } interval = targetP->registration + nextUpdate - currentTime; if (0 >= interval) { LOG("Updating registration...\r\n"); prv_updateRegistration(contextP, targetP, false); } else if (interval < *timeoutP) { *timeoutP = interval; } } break; case STATE_REG_FAILED: if (targetP->sessionH != NULL) { lwm2m_close_connection(targetP->sessionH, contextP->userData); targetP->sessionH = NULL; } break; default: break; } targetP = targetP->next; } #endif #ifdef LWM2M_SERVER_MODE lwm2m_client_t * clientP; // monitor clients lifetime clientP = contextP->clientList; while (clientP != NULL) { lwm2m_client_t * nextP = clientP->next; if (clientP->endOfLife <= currentTime) { contextP->clientList = (lwm2m_client_t *)LWM2M_LIST_RM(contextP->clientList, clientP->internalID, NULL); if (contextP->monitorCallback != NULL) { contextP->monitorCallback(clientP->internalID, NULL, COAP_202_DELETED, LWM2M_CONTENT_TEXT, NULL, 0, contextP->monitorUserData); } registration_freeClient(clientP); } else { time_t interval; interval = clientP->endOfLife - currentTime; if (*timeoutP > interval) { *timeoutP = interval; } } clientP = nextP; } #endif }