// start a device initiated bootstrap static void prv_requestBootstrap(lwm2m_context_t * context, lwm2m_server_t * bootstrapServer) { char query[PRV_QUERY_BUFFER_LENGTH]; int query_length = 0; int res; LOG("Entering"); query_length = utils_stringCopy(query, PRV_QUERY_BUFFER_LENGTH, QUERY_STARTER QUERY_NAME); if (query_length < 0) { bootstrapServer->status = STATE_BS_FAILING; return; } res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, context->endpointName); if (res < 0) { bootstrapServer->status = STATE_BS_FAILING; return; } query_length += res; if (bootstrapServer->sessionH == NULL) { bootstrapServer->sessionH = lwm2m_connect_server(bootstrapServer->secObjInstID, context->userData); } if (bootstrapServer->sessionH != NULL) { lwm2m_transaction_t * transaction = NULL; LOG("Bootstrap server connection opened"); transaction = transaction_new(bootstrapServer->sessionH, COAP_POST, NULL, NULL, context->nextMID++, 4, NULL); if (transaction == NULL) { bootstrapServer->status = STATE_BS_FAILING; return; } coap_set_header_uri_path(transaction->message, "/"URI_BOOTSTRAP_SEGMENT); coap_set_header_uri_query(transaction->message, query); transaction->callback = prv_handleBootstrapReply; transaction->userData = (void *)bootstrapServer; context->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(context->transactionList, transaction); if (transaction_send(context, transaction) == 0) { LOG("CI bootstrap requested to BS server"); bootstrapServer->status = STATE_BS_INITIATED; } } else { LOG("Connecting bootstrap server failed"); bootstrapServer->status = STATE_BS_FAILED; } }
// start a device initiated bootstrap static void bootstrap_initiating_request(lwm2m_context_t * context, lwm2m_server_t * bootstrapServer) { char query[PRV_QUERY_BUFFER_LENGTH]; int query_length = 0; int res; query_length = utils_stringCopy(query, PRV_QUERY_BUFFER_LENGTH, "?ep="); if (query_length < 0) { bootstrapServer->status = STATE_BS_FAILED; return; } res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, context->endpointName); if (res < 0) { bootstrapServer->status = STATE_BS_FAILED; return; } query_length += res; if (bootstrapServer->sessionH == NULL) { bootstrapServer->sessionH = context->connectCallback(bootstrapServer->secObjInstID, context->userData); } if (bootstrapServer->sessionH != NULL) { lwm2m_transaction_t * transaction = NULL; LOG("[BOOTSTRAP] Bootstrap session starting...\r\n"); transaction = transaction_new(COAP_TYPE_CON, COAP_POST, NULL, NULL, context->nextMID++, 4, NULL, ENDPOINT_SERVER, (void *)bootstrapServer); if (transaction == NULL) { bootstrapServer->status = STATE_BS_FAILED; return; } coap_set_header_uri_path(transaction->message, "/"URI_BOOTSTRAP_SEGMENT); coap_set_header_uri_query(transaction->message, query); transaction->callback = prv_handleBootstrapReply; transaction->userData = (void *)bootstrapServer; context->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(context->transactionList, transaction); if (transaction_send(context, transaction) == 0) { LOG("[BOOTSTRAP] CI bootstrap requested to BS server\r\n"); bootstrapServer->status = STATE_BS_INITIATED; } } else { LOG("No bootstrap session handler found\r\n"); bootstrapServer->status = STATE_BS_FAILED; } }
static int prv_getRegistrationQuery(lwm2m_context_t * contextP, lwm2m_server_t * server, char * buffer, size_t length) { int index; int res; index = utils_stringCopy(buffer, length, "?ep="); if (index < 0) return 0; res = utils_stringCopy(buffer + index, length - index, contextP->endpointName); if (res < 0) return 0; index += res; if (NULL != contextP->msisdn) { res = utils_stringCopy(buffer + index, length - index, QUERY_DELIMITER QUERY_SMS); if (res < 0) return 0; index += res; res = utils_stringCopy(buffer + index, length - index, contextP->msisdn); if (res < 0) return 0; index += res; } switch (server->binding) { case BINDING_U: res = utils_stringCopy(buffer + index, length - index, "&b=U"); break; case BINDING_UQ: res = utils_stringCopy(buffer + index, length - index, "&b=UQ"); break; case BINDING_S: res = utils_stringCopy(buffer + index, length - index, "&b=S"); break; case BINDING_SQ: res = utils_stringCopy(buffer + index, length - index, "&b=SQ"); break; case BINDING_US: res = utils_stringCopy(buffer + index, length - index, "&b=US"); break; case BINDING_UQS: res = utils_stringCopy(buffer + index, length - index, "&b=UQS"); break; default: res = -1; } if (res < 0) return 0; return index + res; }
// send the registration for a single server static uint8_t prv_register(lwm2m_context_t * contextP, lwm2m_server_t * server) { char query[200]; int query_length; uint8_t payload[512]; int payload_length; lwm2m_transaction_t * transaction; payload_length = object_getRegisterPayload(contextP, payload, sizeof(payload)); if (payload_length == 0) return COAP_500_INTERNAL_SERVER_ERROR; query_length = prv_getRegistrationQuery(contextP, server, query, sizeof(query)); if (query_length == 0) return COAP_500_INTERNAL_SERVER_ERROR; #if !defined(COAP_TCP) if (0 != server->lifetime) { int res; res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, QUERY_DELIMITER QUERY_LIFETIME); if (res < 0) return COAP_500_INTERNAL_SERVER_ERROR; query_length += res; res = utils_intCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, server->lifetime); if (res < 0) return COAP_500_INTERNAL_SERVER_ERROR; query_length += res; } #endif if (server->sessionH == NULL) { server->sessionH = lwm2m_connect_server(server->secObjInstID, contextP->userData); } if (NULL == server->sessionH) return COAP_503_SERVICE_UNAVAILABLE; transaction = transaction_new(COAP_TYPE_CON, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL, ENDPOINT_SERVER, (void *)server); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT); coap_set_header_uri_query(transaction->message, query); coap_set_header_content_type(transaction->message, LWM2M_CONTENT_LINK); coap_set_payload(transaction->message, payload, payload_length); transaction->callback = prv_handleRegistrationReply; transaction->userData = (void *) server; contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); if (transaction_send(contextP, transaction) != 0) return COAP_500_INTERNAL_SERVER_ERROR; server->status = STATE_REG_PENDING; return COAP_NO_ERROR; }
static int prv_getLocationString(uint16_t id, char location[MAX_LOCATION_LENGTH]) { int index; int result; memset(location, 0, MAX_LOCATION_LENGTH); result = utils_stringCopy(location, MAX_LOCATION_LENGTH, "/"URI_REGISTRATION_SEGMENT"/"); if (result < 0) return 0; index = result; result = utils_intCopy(location + index, MAX_LOCATION_LENGTH - index, id); if (result < 0) return 0; return index + result; }
int object_getRegisterPayload(lwm2m_context_t * contextP, uint8_t * buffer, size_t bufferLen) { size_t index; int result; lwm2m_object_t * objectP; // index can not be greater than bufferLen index = 0; result = utils_stringCopy((char *)buffer, bufferLen, REG_START); if (result < 0) return 0; index += result; if ((contextP->altPath != NULL) && (contextP->altPath[0] != 0)) { result = utils_stringCopy((char *)buffer + index, bufferLen - index, contextP->altPath); } else { result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_DEFAULT_PATH); } if (result < 0) return 0; index += result; result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_LWM2M_RESOURCE_TYPE); if (result < 0) return 0; index += result; for (objectP = contextP->objectList; objectP != NULL; objectP = objectP->next) { size_t start; size_t length; if (objectP->objID == LWM2M_SECURITY_OBJECT_ID) continue; start = index; result = prv_getObjectTemplate(buffer + index, bufferLen - index, objectP->objID); if (result < 0) return 0; length = result; index += length; if (objectP->instanceList == NULL) { index--; result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_PATH_END); if (result < 0) return 0; index += result; } else { lwm2m_list_t * targetP; for (targetP = objectP->instanceList ; targetP != NULL ; targetP = targetP->next) { if (bufferLen - index <= length) return 0; if (index != start + length) { memcpy(buffer + index, buffer + start, length); index += length; } result = utils_intCopy((char *)buffer + index, bufferLen - index, targetP->id); if (result < 0) return 0; index += result; result = utils_stringCopy((char *)buffer + index, bufferLen - index, REG_PATH_END); if (result < 0) return 0; index += result; } } } if (index > 0) { index = index - 1; // remove trailing ',' } buffer[index] = 0; return index; }