static void handle_bootstrap_response(lwm2m_server_t * bootstrapServer, coap_packet_t * message) { if (COAP_204_CHANGED == message->code) { LOG("[BOOTSTRAP] Received ACK/2.04, Bootstrap pending, waiting for DEL/PUT from BS server...\r\n"); bootstrapServer->status = STATE_BS_PENDING; } else { bootstrap_failed(bootstrapServer); } }
static void prv_handleBootstrapReply(lwm2m_transaction_t * transaction, void * message) { LOG("[BOOTSTRAP] Handling bootstrap reply...\r\n"); lwm2m_context_t * context = (lwm2m_context_t *)transaction->userData; coap_packet_t * coapMessage = (coap_packet_t *)message; if (NULL != coapMessage && COAP_TYPE_RST != coapMessage->type) { handle_bootstrap_response(context, coapMessage, NULL); } else { bootstrap_failed(context); } }
void handle_bootstrap_response(lwm2m_context_t * context, coap_packet_t * message, void * fromSessionH) { if (COAP_204_CHANGED == message->code) { context->bsState = BOOTSTRAP_PENDING; LOG("[BOOTSTRAP] Received ACK/2.04, Bootstrap pending, waiting for DEL/PUT from BS server...\r\n"); reset_bootstrap_timer(context); } else { bootstrap_failed(context); } }
static void prv_handleBootstrapReply(lwm2m_transaction_t * transaction, void * message) { lwm2m_server_t * bootstrapServer = (lwm2m_server_t *)transaction->userData; coap_packet_t * coapMessage = (coap_packet_t *)message; LOG("[BOOTSTRAP] Handling bootstrap reply...\r\n"); if (bootstrapServer->status == STATE_BS_INITIATED) { if (NULL != coapMessage && COAP_TYPE_RST != coapMessage->type) { handle_bootstrap_response(bootstrapServer, coapMessage); } else { bootstrap_failed(bootstrapServer); } } }
void update_bootstrap_state(lwm2m_context_t * context, uint32_t currentTime, time_t* timeoutP) { if (context->bsState == BOOTSTRAP_REQUESTED) { context->bsState = BOOTSTRAP_CLIENT_HOLD_OFF; context->bsStart = currentTime; LOG("[BOOTSTRAP] Bootstrap requested at: %lu, now waiting during ClientHoldOffTime...\r\n", (unsigned long)context->bsStart); } if (context->bsState == BOOTSTRAP_CLIENT_HOLD_OFF) { lwm2m_server_t * bootstrapServer = context->bootstrapServerList; if (bootstrapServer != NULL) { // get ClientHoldOffTime from bootstrapServer->lifetime // (see objects.c => object_getServers()) int32_t timeToBootstrap = (context->bsStart + bootstrapServer->lifetime) - currentTime; LOG("[BOOTSTRAP] ClientHoldOffTime %ld\r\n", (long)timeToBootstrap); if (0 >= timeToBootstrap) { bootstrap_initiating_request(context); } else if (timeToBootstrap < *timeoutP) { *timeoutP = timeToBootstrap; } } else { bootstrap_failed(context); } } if (context->bsState == BOOTSTRAP_PENDING) { // Use COAP_DEFAULT_MAX_AGE according proposal in // https://github.com/OpenMobileAlliance/OMA-LwM2M-Public-Review/issues/35 int32_t timeToBootstrap = (context->bsStart + COAP_DEFAULT_MAX_AGE) - currentTime; LOG("[BOOTSTRAP] Pending %ld\r\n", (long)timeToBootstrap); if (0 >= timeToBootstrap) { // Time out and no error => bootstrap OK // TODO: add smarter condition for bootstrap success: // 1) security object contains at least one bootstrap server // 2) there are coherent configurations for provisioned DM servers // if these conditions are not met, then bootstrap has failed and previous security // and server object configurations might be restored by client LOG("\r\n[BOOTSTRAP] Bootstrap finished at: %lu (difftime: %lu s)\r\n", (unsigned long)currentTime, (unsigned long)(currentTime - context->bsStart)); context->bsState = BOOTSTRAP_FINISHED; context->bsStart = currentTime; *timeoutP = 1; } else if (timeToBootstrap < *timeoutP) { *timeoutP = timeToBootstrap; } } else if (context->bsState == BOOTSTRAP_FINISHED) { context->bsStart = currentTime; if (0 <= lwm2m_start(context)) { context->bsState = BOOTSTRAPPED; } else { bootstrap_failed(context); } // during next step, lwm2m_update_registrations will connect the client to DM server } if (BOOTSTRAP_FAILED == context->bsState) { lwm2m_server_t * bootstrapServer = context->bootstrapServerList; if (bootstrapServer != NULL) { // get ClientHoldOffTime from bootstrapServer->lifetime // (see objects.c => object_getServers()) int32_t timeToBootstrap = (context->bsStart + bootstrapServer->lifetime) - currentTime; LOG("[BOOTSTRAP] Bootstrap failed: %lu, now waiting during ClientHoldOffTime %ld ...\r\n", (unsigned long)context->bsStart, (long)timeToBootstrap); if (0 >= timeToBootstrap) { context->bsState = NOT_BOOTSTRAPPED; context->bsStart = currentTime; LOG("[BOOTSTRAP] Bootstrap failed: retry ...\r\n"); *timeoutP = 1; } else if (timeToBootstrap < *timeoutP) { *timeoutP = timeToBootstrap; } } } }
coap_status_t ICACHE_FLASH_ATTR object_write(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, size_t length) { coap_status_t result = NO_ERROR; lwm2m_object_t * targetP; lwm2m_data_t * dataP = NULL; int size = 0; targetP = prv_find_object(contextP, uriP->objectId); if (NULL == targetP) { result = NOT_FOUND_4_04; } else if (NULL == targetP->writeFunc) { result = METHOD_NOT_ALLOWED_4_05; } else { if (LWM2M_URI_IS_SET_RESOURCE(uriP)) { size = 1; dataP = lwm2m_data_new(size); if (dataP == NULL) { return COAP_500_INTERNAL_SERVER_ERROR; } dataP->flags = LWM2M_TLV_FLAG_TEXT_FORMAT | LWM2M_TLV_FLAG_STATIC_DATA; dataP->type = LWM2M_TYPE_RESOURCE; dataP->id = uriP->resourceId; dataP->length = length; dataP->value = (uint8_t *)buffer; } else { size = lwm2m_data_parse(buffer, length, format, &dataP); if (size == 0) { result = COAP_500_INTERNAL_SERVER_ERROR; } } } if (result == NO_ERROR) { #ifdef ICACHE_FLASH_ATTR if (contextP->bsState == BOOTSTRAP_PENDING) { dataP->flags |= LWM2M_TLV_FLAG_BOOTSTRAPPING; } #endif result = targetP->writeFunc(uriP->instanceId, size, dataP, targetP); lwm2m_data_free(size, dataP); } #ifdef ICACHE_FLASH_ATTR if (contextP->bsState == BOOTSTRAP_PENDING) { if (result == COAP_204_CHANGED) { reset_bootstrap_timer(contextP); } else { bootstrap_failed(contextP); } } #endif return result; }