static void prv_create_client(char * buffer, void * user_data) { lwm2m_context_t * lwm2mH = (lwm2m_context_t *) user_data; uint16_t clientId; lwm2m_uri_t uri; char * end = NULL; int result; int64_t value; uint8_t temp_buffer[MAX_PACKET_SIZE]; int temp_length = 0; lwm2m_media_type_t format = LWM2M_CONTENT_TEXT; //Get Client ID result = prv_read_id(buffer, &clientId); if (result != 1) goto syntax_error; //Get Uri buffer = get_next_arg(buffer, &end); if (buffer[0] == 0) goto syntax_error; result = lwm2m_stringToUri(buffer, end - buffer, &uri); if (result == 0) goto syntax_error; //Get Data to Post buffer = get_next_arg(end, &end); if (buffer[0] == 0) goto syntax_error; if (!check_end_of_args(end)) goto syntax_error; // TLV /* Client dependent part */ if (uri.objectId == 1024) { result = lwm2m_PlainTextToInt64((uint8_t *)buffer, end - buffer, &value); temp_length = lwm2m_intToTLV(LWM2M_TYPE_RESOURCE, value, (uint16_t) 1, temp_buffer, MAX_PACKET_SIZE); format = LWM2M_CONTENT_TLV; } /* End Client dependent part*/ //Create result = lwm2m_dm_create(lwm2mH, clientId, &uri, format, temp_buffer, temp_length, prv_result_callback, NULL); if (result == 0) { fprintf(stdout, "OK"); } else { prv_print_error(result); } return; syntax_error: fprintf(stdout, "Syntax error !"); }
static int prv_get_object_tlv(char ** bufferP, firmware_data_t* dataP) { int length = 0; int result; char temp_buffer[16]; int temp_length; *bufferP = (uint8_t *)malloc(PRV_TLV_BUFFER_SIZE); if (NULL == *bufferP) return 0; result = lwm2m_intToTLV(TLV_RESSOURCE, dataP->state, 3, *bufferP, PRV_TLV_BUFFER_SIZE); if (0 == result) goto error; length += result; result = lwm2m_boolToTLV(TLV_RESSOURCE, dataP->supported, 4, *bufferP + length, PRV_TLV_BUFFER_SIZE - length); if (0 == result) goto error; length += result; result = lwm2m_intToTLV(TLV_RESSOURCE, dataP->result, 5, *bufferP + length, PRV_TLV_BUFFER_SIZE - length); if (0 == result) goto error; length += result; fprintf(stderr, "TLV (%d bytes):\r\n", length); prv_output_buffer(*bufferP, length); return length; error: fprintf(stderr, "TLV generation failed:\r\n"); free(*bufferP); *bufferP = NULL; return 0; }
static uint8_t prv_read(lwm2m_uri_t * uriP, char ** bufferP, int * lengthP, lwm2m_object_t * objectP) { prv_instance_t * targetP; *bufferP = NULL; *lengthP = 0; if (!LWM2M_URI_IS_SET_INSTANCE(uriP)) { *bufferP = (char *)malloc(PRV_TLV_BUFFER_SIZE); if (NULL == *bufferP) return COAP_500_INTERNAL_SERVER_ERROR; // TLV for (targetP = (prv_instance_t *)(objectP->instanceList); targetP != NULL ; targetP = targetP->next) { char temp_buffer[16]; int temp_length = 0; int result; result = lwm2m_intToTLV(TLV_RESSOURCE, targetP->test, 1, temp_buffer, 16); if (0 == result) { *lengthP = 0; break; } temp_length += result; result = lwm2m_opaqueToTLV(TLV_OBJECT_INSTANCE, temp_buffer, temp_length, targetP->shortID, *bufferP + *lengthP, PRV_TLV_BUFFER_SIZE - *lengthP); if (0 == result) { *lengthP = 0; break; } *lengthP += result; } if (*lengthP == 0) { free(*bufferP); *bufferP = NULL; return COAP_500_INTERNAL_SERVER_ERROR; } prv_output_buffer(*bufferP, *lengthP); return COAP_205_CONTENT; } else { targetP = (prv_instance_t *)lwm2m_list_find(objectP->instanceList, uriP->instanceId); if (NULL == targetP) return COAP_404_NOT_FOUND; if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) { // TLV *bufferP = (char *)malloc(PRV_TLV_BUFFER_SIZE); if (NULL == *bufferP) return COAP_500_INTERNAL_SERVER_ERROR; *lengthP = lwm2m_intToTLV(TLV_RESSOURCE, targetP->test, 1, *bufferP, PRV_TLV_BUFFER_SIZE); if (0 == *lengthP) { free(*bufferP); *bufferP = NULL; return COAP_500_INTERNAL_SERVER_ERROR; } prv_output_buffer(*bufferP, *lengthP); return COAP_205_CONTENT; } switch (uriP->resourceId) { case 1: // we use int16 because value is unsigned *lengthP = lwm2m_int16ToPlainText(targetP->test, bufferP); if (*lengthP <= 0) return COAP_500_INTERNAL_SERVER_ERROR; return COAP_205_CONTENT; default: return COAP_404_NOT_FOUND; } } }