int lwm2m_add_object(lwm2m_context_t * contextP, lwm2m_object_t * objectP) { uint16_t i; lwm2m_object_t ** newList; for (i = 0 ; i < contextP->numObject ; i++) { if (contextP->objectList[i]->objID == objectP->objID) return COAP_406_NOT_ACCEPTABLE; } newList = (lwm2m_object_t **)lwm2m_malloc((contextP->numObject + 1) * sizeof(lwm2m_object_t *)); if (newList == NULL) return COAP_500_INTERNAL_SERVER_ERROR; memcpy(newList, contextP->objectList, contextP->numObject * sizeof(lwm2m_object_t *)); newList[contextP->numObject] = objectP; lwm2m_free(contextP->objectList); contextP->objectList = newList; contextP->numObject += 1; if (contextP->state == STATE_READY) { return lwm2m_update_registration(contextP, 0, true); } return COAP_NO_ERROR; }
int lwm2m_remove_object(lwm2m_context_t * contextP, uint16_t id) { lwm2m_object_t * targetP; LOG_ARG("ID: %d", id); contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_RM(contextP->objectList, id, &targetP); if (targetP == NULL) return COAP_404_NOT_FOUND; if (contextP->state == STATE_READY) { return lwm2m_update_registration(contextP, 0, true); } return 0; }
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 lwm2m_add_object(lwm2m_context_t * contextP, lwm2m_object_t * objectP) { lwm2m_object_t * targetP; LOG_ARG("ID: %d", objectP->objID); targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, objectP->objID); if (targetP != NULL) return COAP_406_NOT_ACCEPTABLE; objectP->next = NULL; contextP->objectList = (lwm2m_object_t *)LWM2M_LIST_ADD(contextP->objectList, objectP); if (contextP->state == STATE_READY) { return lwm2m_update_registration(contextP, 0, true); } return COAP_NO_ERROR; }
int lwm2m_remove_object(lwm2m_context_t * contextP, uint16_t id) { uint16_t i; lwm2m_object_t ** newList; i = 0; while (i < contextP->numObject && contextP->objectList[i]->objID != id) { i++; } if (i == contextP->numObject) return COAP_404_NOT_FOUND; if (contextP->numObject > 1) { newList = (lwm2m_object_t **)lwm2m_malloc((contextP->numObject - 1) * sizeof(lwm2m_object_t *)); if (newList == NULL) return COAP_500_INTERNAL_SERVER_ERROR; if (i > 0) { memcpy(newList, contextP->objectList, i * sizeof(lwm2m_object_t *)); } memcpy(newList + i, contextP->objectList + i + 1, (contextP->numObject - i - 1) * sizeof(lwm2m_object_t *)); } else { newList = NULL; } lwm2m_free(contextP->objectList); contextP->objectList = newList; contextP->numObject -= 1; if (contextP->state == STATE_READY) { return lwm2m_update_registration(contextP, 0, true); } return 0; }
uint8_t dm_handleRequest(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, coap_packet_t * message, coap_packet_t * response) { uint8_t result; lwm2m_media_type_t format; LOG_ARG("Code: %02X, server status: %s", message->code, STR_STATUS(serverP->status)); LOG_URI(uriP); if (IS_OPTION(message, COAP_OPTION_CONTENT_TYPE)) { format = utils_convertMediaType(message->content_type); } else { format = LWM2M_CONTENT_TLV; } if (uriP->objectId == LWM2M_SECURITY_OBJECT_ID) { return COAP_404_NOT_FOUND; } if (serverP->status != STATE_REGISTERED && serverP->status != STATE_REG_UPDATE_NEEDED && serverP->status != STATE_REG_FULL_UPDATE_NEEDED && serverP->status != STATE_REG_UPDATE_PENDING) { return COAP_IGNORE; } // TODO: check ACL switch (message->code) { case COAP_GET: { uint8_t * buffer = NULL; size_t length = 0; int res; if (IS_OPTION(message, COAP_OPTION_OBSERVE)) { lwm2m_data_t * dataP = NULL; int size = 0; result = object_readData(contextP, uriP, &size, &dataP); if (COAP_205_CONTENT == result) { result = observe_handleRequest(contextP, uriP, serverP, size, dataP, message, response); if (COAP_205_CONTENT == result) { if (IS_OPTION(message, COAP_OPTION_ACCEPT)) { format = utils_convertMediaType(message->accept[0]); } else { format = LWM2M_CONTENT_TLV; } res = lwm2m_data_serialize(uriP, size, dataP, &format, &buffer); if (res < 0) { result = COAP_500_INTERNAL_SERVER_ERROR; } else { length = (size_t)res; LOG_ARG("Observe Request[/%d/%d/%d]: %.*s\n", uriP->objectId, uriP->instanceId, uriP->resourceId, length, buffer); } } lwm2m_data_free(size, dataP); } } else if (IS_OPTION(message, COAP_OPTION_ACCEPT) && message->accept_num == 1 && message->accept[0] == APPLICATION_LINK_FORMAT) { format = LWM2M_CONTENT_LINK; result = object_discover(contextP, uriP, serverP, &buffer, &length); } else { if (IS_OPTION(message, COAP_OPTION_ACCEPT)) { format = utils_convertMediaType(message->accept[0]); } result = object_read(contextP, uriP, &format, &buffer, &length); } if (COAP_205_CONTENT == result) { coap_set_header_content_type(response, format); coap_set_payload(response, buffer, length); // lwm2m_handle_packet will free buffer } else { lwm2m_free(buffer); } } break; case COAP_POST: { if (!LWM2M_URI_IS_SET_INSTANCE(uriP)) { result = object_create(contextP, uriP, format, message->payload, message->payload_len); if (result == COAP_201_CREATED) { //longest uri is /65535/65535 = 12 + 1 (null) chars char location_path[13] = ""; //instanceId expected if ((uriP->flag & LWM2M_URI_FLAG_INSTANCE_ID) == 0) { result = COAP_500_INTERNAL_SERVER_ERROR; break; } if (sprintf(location_path, "/%d/%d", uriP->objectId, uriP->instanceId) < 0) { result = COAP_500_INTERNAL_SERVER_ERROR; break; } coap_set_header_location_path(response, location_path); lwm2m_update_registration(contextP, 0, true); } } else if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) { result = object_write(contextP, uriP, format, message->payload, message->payload_len); } else { result = object_execute(contextP, uriP, message->payload, message->payload_len); } } break; case COAP_PUT: { if (IS_OPTION(message, COAP_OPTION_URI_QUERY)) { lwm2m_attributes_t attr; if (0 != prv_readAttributes(message->uri_query, &attr)) { result = COAP_400_BAD_REQUEST; } else { result = observe_setParameters(contextP, uriP, serverP, &attr); } } else if (LWM2M_URI_IS_SET_INSTANCE(uriP)) { result = object_write(contextP, uriP, format, message->payload, message->payload_len); } else { result = COAP_400_BAD_REQUEST; } } break; case COAP_DELETE: { if (!LWM2M_URI_IS_SET_INSTANCE(uriP) || LWM2M_URI_IS_SET_RESOURCE(uriP)) { result = COAP_400_BAD_REQUEST; } else { result = object_delete(contextP, uriP); if (result == COAP_202_DELETED) { lwm2m_update_registration(contextP, 0, true); } } } break; default: result = COAP_400_BAD_REQUEST; break; } return result; }