static void prv_cancel_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; result = prv_read_id(buffer, &clientId); if (result != 1) goto syntax_error; 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; if (!check_end_of_args(end)) goto syntax_error; result = lwm2m_observe_cancel(lwm2mH, clientId, &uri, prv_result_callback, NULL); if (result == 0) { fprintf(stdout, "OK"); } else { prv_print_error(result); } return; syntax_error: fprintf(stdout, "Syntax error !"); }
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 void prv_time_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; lwm2m_attributes_t attr; int nb; int value; result = prv_read_id(buffer, &clientId); if (result != 1) goto syntax_error; 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; memset(&attr, 0, sizeof(lwm2m_attributes_t)); attr.toSet = LWM2M_ATTR_FLAG_MIN_PERIOD | LWM2M_ATTR_FLAG_MAX_PERIOD; buffer = get_next_arg(end, &end); if (buffer[0] == 0) goto syntax_error; nb = sscanf(buffer, "%d", &value); if (nb != 1) goto syntax_error; if (value < 0) goto syntax_error; attr.minPeriod = value; buffer = get_next_arg(end, &end); if (buffer[0] == 0) goto syntax_error; nb = sscanf(buffer, "%d", &value); if (nb != 1) goto syntax_error; if (value < 0) goto syntax_error; attr.maxPeriod = value; if (!check_end_of_args(end)) goto syntax_error; result = lwm2m_dm_write_attributes(lwm2mH, clientId, &uri, &attr, prv_result_callback, NULL); if (result == 0) { fprintf(stdout, "OK"); } else { prv_print_error(result); } return; syntax_error: fprintf(stdout, "Syntax error !"); }
static void prv_clear_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; lwm2m_attributes_t attr; result = prv_read_id(buffer, &clientId); if (result != 1) goto syntax_error; 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; memset(&attr, 0, sizeof(lwm2m_attributes_t)); attr.toClear = LWM2M_ATTR_FLAG_LESS_THAN | LWM2M_ATTR_FLAG_GREATER_THAN | LWM2M_ATTR_FLAG_STEP | LWM2M_ATTR_FLAG_MIN_PERIOD | LWM2M_ATTR_FLAG_MAX_PERIOD ; buffer = get_next_arg(end, &end); if (!check_end_of_args(end)) goto syntax_error; result = lwm2m_dm_write_attributes(lwm2mH, clientId, &uri, &attr, prv_result_callback, NULL); if (result == 0) { fprintf(stdout, "OK"); } else { prv_print_error(result); } return; syntax_error: fprintf(stdout, "Syntax error !"); }
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 = NULL; 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) { lwm2m_data_t * dataP; dataP = lwm2m_data_new(1); if (dataP == NULL) { fprintf(stdout, "Allocation error !"); return; } lwm2m_data_encode_int(value, dataP); dataP->id = 1; format = LWM2M_CONTENT_TLV; temp_length = lwm2m_data_serialize(NULL, 1, dataP, &format, &temp_buffer); } /* 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 void prv_attr_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; lwm2m_attributes_t attr; int nb; float value; result = prv_read_id(buffer, &clientId); if (result != 1) goto syntax_error; 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; memset(&attr, 0, sizeof(lwm2m_attributes_t)); attr.toSet = LWM2M_ATTR_FLAG_LESS_THAN | LWM2M_ATTR_FLAG_GREATER_THAN; buffer = get_next_arg(end, &end); if (buffer[0] == 0) goto syntax_error; nb = sscanf(buffer, "%f", &value); if (nb != 1) goto syntax_error; attr.lessThan = value; buffer = get_next_arg(end, &end); if (buffer[0] == 0) goto syntax_error; nb = sscanf(buffer, "%f", &value); if (nb != 1) goto syntax_error; attr.greaterThan = value; buffer = get_next_arg(end, &end); if (buffer[0] != 0) { nb = sscanf(buffer, "%f", &value); if (nb != 1) goto syntax_error; attr.step = value; attr.toSet |= LWM2M_ATTR_FLAG_STEP; } if (!check_end_of_args(end)) goto syntax_error; result = lwm2m_dm_write_attributes(lwm2mH, clientId, &uri, &attr, prv_result_callback, NULL); if (result == 0) { fprintf(stdout, "OK"); } else { prv_print_error(result); } return; syntax_error: fprintf(stdout, "Syntax error !"); }
static void prv_bootstrap_client(char * buffer, void * user_data) { internal_data_t * dataP = (internal_data_t *)user_data; char * uri; char * name; char* end = NULL; char * host; char * port; connection_t * newConnP = NULL; uri = buffer; end = get_end_of_arg(buffer); if (end[0] != 0) { *end = 0; buffer = end + 1; name = get_next_arg(buffer, &end); } if (!check_end_of_args(end)) goto syntax_error; // parse uri in the form "coaps://[host]:[port]" if (0==strncmp(uri, "coaps://", strlen("coaps://"))) { host = uri+strlen("coaps://"); } else if (0==strncmp(uri, "coap://", strlen("coap://"))) { host = uri+strlen("coap://"); } else { goto syntax_error; } port = strrchr(host, ':'); if (port == NULL) goto syntax_error; // remove brackets if (host[0] == '[') { host++; if (*(port - 1) == ']') { *(port - 1) = 0; } else goto syntax_error; } // split strings *port = 0; port++; fprintf(stderr, "Trying to connect to LWM2M CLient at %s:%s\r\n", host, port); newConnP = connection_create(dataP->connList, dataP->sock, host, port); if (newConnP == NULL) { fprintf(stderr, "Connection creation failed.\r\n"); return; } dataP->connList = newConnP; // simulate a client bootstrap request. if (COAP_204_CHANGED == prv_bootstrap_callback(newConnP, COAP_NO_ERROR, NULL, name, user_data)) { fprintf(stdout, "OK"); } else { fprintf(stdout, "Error"); } return; syntax_error: fprintf(stdout, "Syntax error !"); }