/*-----------------------------------------------------------------------------------*/ void coap_observe_handler(resource_t *resource, void *request, void *response) { static char content[26]; if (response && ((coap_packet_t *)response)->code<128) /* response without error code */ { if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_OBSERVE)) { if (IS_OPTION((coap_packet_t *)request, COAP_OPTION_TOKEN)) { if (coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, ((coap_packet_t *)request)->token, ((coap_packet_t *)request)->token_len, resource->url)) { coap_set_header_observe(response, 0); coap_set_payload(response, (uint8_t *)content, snprintf(content, sizeof(content), "Added as observer %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS)); } else { ((coap_packet_t *)response)->code = SERVICE_UNAVAILABLE_503; coap_set_payload(response, (uint8_t *)"Too many observers", 18); } /* if (added observer) */ } else /* if (token) */ { ((coap_packet_t *)response)->code = TOKEN_OPTION_REQUIRED; coap_set_payload(response, (uint8_t *)"Observing requires token", 24); } /* if (token) */ } else /* if (observe) */ { /* Remove client if it is currently observing. */ coap_remove_observer_by_client(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport); } /* if (observe) */ } }
/*-----------------------------------------------------------------------------------*/ void coap_observe_handler(resource_t *resource, void *request, void *response) { coap_packet_t *const coap_req = (coap_packet_t *) request; coap_packet_t *const coap_res = (coap_packet_t *) response; static char content[16]; if (coap_req->code==COAP_GET && coap_res->code<128) /* GET request and response without error code */ { if (IS_OPTION(coap_req, COAP_OPTION_OBSERVE)) { if (coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len, resource->url)) { coap_set_header_observe(coap_res, 0); /* * For demonstration purposes only. A subscription should return the same representation as a normal GET. * TODO: Comment the following line for any real application. */ coap_set_payload(coap_res, content, snprintf(content, sizeof(content), "Added %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS)); } else { coap_res->code = SERVICE_UNAVAILABLE_5_03; coap_set_payload(coap_res, "TooManyObservers", 16); } /* if (added observer) */ } else /* if (observe) */ { /* Remove client if it is currently observing. */ coap_remove_observer_by_url(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, resource->url); } /* if (observe) */ } }
void coap_SendNotify(AddressType * addr, const char * path, const char * token, int tokenSize, ContentType contentType, const char * payload, int payloadLen, int sequence) { // TODO - FIXME: if path is not full uri then map addr to Network address + append path(?) coap_packet_t notify; coap_transaction_t *transaction; NetworkAddress * remoteAddress = NetworkAddress_New(path, strlen(path)); Lwm2m_Debug("Coap notify: %s\n", path); //Lwm2m_Debug("Coap IPv6 request address: " PRINT6ADDR(&addr->Addr)); //Lwm2m_Debug("Coap request port: %d\n", addr->Port); coap_init_message(¬ify, COAP_TYPE_NON, CONTENT_2_05, coap_get_mid()); if (contentType != ContentType_None) { coap_set_header_content_format(¬ify, contentType); coap_set_payload(¬ify, payload, payloadLen); } coap_set_token(¬ify, token, tokenSize); coap_set_header_observe(¬ify, sequence); if ((transaction = coap_new_transaction(networkSocket, notify.mid, remoteAddress))) { transaction->packet_len = coap_serialize_message(¬ify, transaction->packet); coap_send_transaction(transaction); // for NON confirmable messages this will call coap_clear_transaction(); } }
static void send_data() { int data_size = 0; static unsigned temperature; temperature = 21; clear_buffer(outputBuffer); clear_buffer(payload_buf); generate_payload(payload_buf,temperature); if(init_buffer(COAP_DATA_BUFF_SIZE)){ coap_packet_t* request =\ (coap_packet_t*)allocate_buffer(sizeof(coap_packet_t)); init_packet(request); coap_set_method(request, COAP_POST); request->tid = xact_id++; request->type = MESSAGE_TYPE_CON; coap_set_header_uri(request,service_uri); coap_set_option(request, Option_Type_Uri_Host, sizeof(char)*strlen(server_ip), (uint8_t*)server_ip); coap_set_option(request, Option_Type_Proxy_Uri, sizeof(char)*strlen(proxy_uri), (uint8_t*)proxy_uri); coap_set_payload(request,(uint8_t*)payload_buf, sizeof(char)*strlen(payload_buf)); data_size = serialize_packet(request, (uint8_t*)outputBuffer); PRINTF("Now sending request to base station ["); PRINTF(&client_conn->ripaddr); PRINTF("]:%u/%s\n",REMOTE_PORT,service_uri); uip_udp_packet_send(client_conn, outputBuffer, data_size); delete_buffer(); } }
void coap_SendNotify(AddressType * addr, const char * path, const char * token, int tokenSize, ContentType contentType, const char * payload, int payloadLen, int sequence) { coap_packet_t notify; coap_transaction_t *transaction; Lwm2m_Debug("Coap notify: %s\n", path); Lwm2m_Debug("Coap IPv6 request address: " PRINT6ADDR(&addr->Addr)); Lwm2m_Debug("Coap request port: %d\n", addr->Port); coap_init_message(¬ify, COAP_TYPE_NON, CONTENT_2_05, coap_get_mid()); if (contentType != ContentType_None) { coap_set_header_content_format(¬ify, contentType); coap_set_payload(¬ify, payload, payloadLen); } coap_set_token(¬ify, token, tokenSize); coap_set_header_observe(¬ify, sequence); if ((transaction = coap_new_transaction(notify.mid, &addr->Addr, uip_htons(addr->Port)))) { transaction->packet_len = coap_serialize_message(¬ify, transaction->packet); coap_send_transaction(transaction); // for NON confirmable messages this will call coap_clear_transaction(); } }
void coap_createCoapRequest(void * context, coap_method_t method, const char * uri, ContentType contentType, const char * payload, int payloadLen, TransactionCallback callback) { coap_packet_t request; uip_ipaddr_t * remote_ipaddr = coap_getIpFromURI(uri); int remote_port = coap_getPortFromURI(uri); coap_transaction_t *transaction; char path[128] = {0}; char query[128] = {0}; coap_getPathQueryFromURI(uri, path, query); Lwm2m_Debug("Coap request: %s\n", uri); Lwm2m_Debug("Coap IPv6 request address: " PRINT6ADDR(remote_ipaddr)); Lwm2m_Debug("Coap request port: %d\n", remote_port); Lwm2m_Debug("Coap request path: %s\n", path); Lwm2m_Debug("Coap request query: %s\n", query); coap_init_message(&request, COAP_TYPE_CON, method, coap_get_mid()); coap_set_header_uri_path(&request, path); coap_set_header_uri_query(&request, query); if (contentType != ContentType_None) { coap_set_header_content_format(&request, contentType); coap_set_payload(&request, payload, payloadLen); } if (CurrentTransaction[CurrentTransactionIndex].TransactionUsed && CurrentTransaction[CurrentTransactionIndex].TransactionPtr) { Lwm2m_Warning("Canceled previous transaction [%d]: %p\n", CurrentTransactionIndex, CurrentTransaction[CurrentTransactionIndex].TransactionPtr); coap_clear_transaction(CurrentTransaction[CurrentTransactionIndex].TransactionPtr); } if ((transaction = coap_new_transaction(request.mid, remote_ipaddr, uip_htons(remote_port)))) { transaction->callback = coap_CoapRequestCallback; CurrentTransaction[CurrentTransactionIndex].Callback = callback; CurrentTransaction[CurrentTransactionIndex].Context = context; CurrentTransaction[CurrentTransactionIndex].TransactionUsed = true; CurrentTransaction[CurrentTransactionIndex].TransactionPtr = transaction; memcpy(&CurrentTransaction[CurrentTransactionIndex].Address.Addr, remote_ipaddr, sizeof(uip_ipaddr_t)); CurrentTransaction[CurrentTransactionIndex].Address.Port = uip_htons(remote_port); transaction->callback_data = &CurrentTransaction[CurrentTransactionIndex]; transaction->packet_len = coap_serialize_message(&request, transaction->packet); Lwm2m_Debug("Sending transaction [%d]: %p\n", CurrentTransactionIndex, CurrentTransaction[CurrentTransactionIndex].TransactionPtr); coap_send_transaction(transaction); CurrentTransactionIndex++; if(CurrentTransactionIndex >= MAX_COAP_TRANSACTIONS) { CurrentTransactionIndex = 0; } } }
void rest_set_response_payload(RESPONSE* response, uint8_t* payload, uint16_t size) { #ifdef WITH_COAP coap_set_payload(response, payload, size); #else http_set_res_payload(response, payload, size); #endif /*WITH_COAP*/ }
/*---------------------------------------------------------------------------*/ void co2_resource_periodic_handler(resource_t *r) { static int event_counter; char buffer[16]; read_co2(buffer); coap_packet_t notification[1]; coap_init_message(notification, COAP_TYPE_NON, REST.status.OK, 0); coap_set_payload(notification, buffer, strlen(buffer)+1); REST.notify_subscribers(r, event_counter++, notification); }
// 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; }
/*---------------------------------------------------------------------------*/ void coap_observe_handler(resource_t *resource, void *request, void *response) { coap_packet_t *const coap_req = (coap_packet_t *)request; coap_packet_t *const coap_res = (coap_packet_t *)response; coap_observer_t * obs; if(coap_req->code == COAP_GET && coap_res->code < 128) { /* GET request and response without error code */ if(IS_OPTION(coap_req, COAP_OPTION_OBSERVE)) { if(coap_req->observe == 0) { obs = coap_add_observer(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len, resource->url); if(obs) { coap_set_header_observe(coap_res, (obs->obs_counter)++); /* * Following payload is for demonstration purposes only. * A subscription should return the same representation as a normal GET. * Uncomment if you want an information about the avaiable observers. */ #if 0 static char content[16]; coap_set_payload(coap_res, content, snprintf(content, sizeof(content), "Added %u/%u", list_length(observers_list), COAP_MAX_OBSERVERS)); #endif } else { coap_res->code = SERVICE_UNAVAILABLE_5_03; coap_set_payload(coap_res, "TooManyObservers", 16); } } else if(coap_req->observe == 1) { /* remove client if it is currently observe */ coap_remove_observer_by_token(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, coap_req->token, coap_req->token_len); } } } }
/*----------------------------------------------------------------------------*/ PROCESS_THREAD(tres_process, ev, data) { PROCESS_BEGIN(); srand(node_id); rest_init_engine(); tres_init(); SENSORS_ACTIVATE(light_sensor); rest_activate_periodic_resource(&periodic_resource_light); rplinfo_activate_resources(); static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */ SERVER_NODE(&server_ipaddr); /* receives all CoAP messages */ coap_receiver_init(); int wait_time = getRandUint(MAX_WAITING); int base_wait = BASE_WAITING; static int g_time=0; static char content[12]; etimer_set(&et, (wait_time + base_wait) * CLOCK_SECOND); while(1) { PROCESS_YIELD(); if (etimer_expired(&et)) break; } etimer_reset(&et); etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); while(1) { PROCESS_YIELD(); if (etimer_expired(&et)) { coap_init_message(request, COAP_TYPE_NON, COAP_POST, 0 ); coap_set_header_uri_path(request, service_urls[1]); coap_set_payload(request, content, snprintf(content, sizeof(content), "%d", g_time++)); coap_transaction_t *transaction; request->mid = coap_get_mid(); if ((transaction = coap_new_transaction(request->mid, &server_ipaddr, REMOTE_PORT))) { transaction->packet_len = coap_serialize_message(request, transaction->packet); coap_send_transaction(transaction); } etimer_reset(&et); } } /* while (1) */ PROCESS_END(); }
/*----------------------------------------------------------------------------*/ PROCESS_THREAD(tres_process, ev, data) { PROCESS_BEGIN(); srand(node_id); rest_init_engine(); tres_init(); rest_activate_resource(&actuator, "actuator"); rplinfo_activate_resources(); sprintf(setpoint, "0"); #if PYOT_KEEPALIVE static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */ SERVER_NODE(&server_ipaddr); int wait_time = (unsigned int)(rand() % MAX_WAITING); int base_wait = BASE_WAITING; static int g_time=0; static char content[12]; etimer_set(&et, (wait_time + base_wait) * CLOCK_SECOND); while(1) { PROCESS_YIELD(); //PROCESS_WAIT_EVENT(); if (etimer_expired(&et)) break; } etimer_reset(&et); etimer_set(&et, TOGGLE_INTERVAL * CLOCK_SECOND); while(1) { PROCESS_YIELD(); if (etimer_expired(&et)) { coap_init_message(request, COAP_TYPE_NON, COAP_POST, 0 ); coap_set_header_uri_path(request, "/rd"); coap_set_payload(request, content, snprintf(content, sizeof(content), "%d", g_time++)); //PRINT6ADDR(&server_ipaddr); //PRINTF(" : %u\n", UIP_HTONS(REMOTE_PORT)); coap_transaction_t *transaction; request->mid = coap_get_mid(); if ((transaction = coap_new_transaction(request->mid, &server_ipaddr, REMOTE_PORT))) { transaction->packet_len = coap_serialize_message(request, transaction->packet); coap_send_transaction(transaction); } etimer_reset(&et); } } /* while (1) */ #endif PROCESS_END(); }
coap_status_t handle_dm_request(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, uint8_t * fromAddr, size_t fromAddrLen, coap_packet_t * message, coap_packet_t * response) { coap_status_t result; switch (message->code) { case COAP_GET: { char * buffer = NULL; int length = 0; result = object_read(contextP, uriP, &buffer, &length); if (result == COAP_205_CONTENT) { if (IS_OPTION(message, COAP_OPTION_OBSERVE)) { result = handle_observe_request(contextP, uriP, fromAddr, fromAddrLen, message, response); } if (result == COAP_205_CONTENT) { coap_set_payload(response, buffer, length); // lwm2m_handle_packet will free buffer } } } break; case COAP_POST: { result = object_create_execute(contextP, uriP, (char *)message->payload, message->payload_len); } break; case COAP_PUT: { result = object_write(contextP, uriP, (char *)message->payload, message->payload_len); } break; case COAP_DELETE: { result = object_delete(contextP, uriP); } break; default: result = BAD_REQUEST_4_00; break; } return result; }
static int prv_makeOperation(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, coap_method_t method, lwm2m_media_type_t format, uint8_t * buffer, int length, lwm2m_result_callback_t callback, void * userData) { lwm2m_client_t * clientP; lwm2m_transaction_t * transaction; dm_data_t * dataP; clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); if (clientP == NULL) return COAP_404_NOT_FOUND; transaction = transaction_new(clientP->sessionH, method, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; if (method == COAP_GET) { coap_set_header_accept(transaction->message, format); } else if (buffer != NULL) { coap_set_header_content_type(transaction->message, format); // TODO: Take care of fragmentation coap_set_payload(transaction->message, buffer, length); } if (callback != NULL) { dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); if (dataP == NULL) { transaction_free(transaction); return COAP_500_INTERNAL_SERVER_ERROR; } memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); dataP->clientID = clientP->internalID; dataP->callback = callback; dataP->userData = userData; transaction->callback = prv_resultCallback; transaction->userData = (void *)dataP; } contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); return transaction_send(contextP, transaction); }
// send the registration for a single server static void 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 = prv_getRegisterPayload(contextP, payload, sizeof(payload)); if (payload_length == 0) return; query_length = prv_getRegistrationQuery(contextP, server, query, sizeof(query)); if (query_length == 0) return; if (0 != server->lifetime) { if (snprintf(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, QUERY_DELIMITER QUERY_LIFETIME "%d", (int)server->lifetime) <= 0) { return; } } if (server->sessionH == NULL) { server->sessionH = contextP->connectCallback(server->secObjInstID, contextP->userData); } if (NULL != server->sessionH) { transaction = transaction_new(COAP_TYPE_CON, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL, ENDPOINT_SERVER, (void *)server); if (transaction == NULL) return; coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT); coap_set_header_uri_query(transaction->message, query); 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) { server->status = STATE_REG_PENDING; } } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(rd_client_process, ev, data) { static struct etimer et; static coap_packet_t request[1]; /* This way the packet can be treated as pointer as usual. */ static char query_buffer[200]; static char rd_client_name[64]; PROCESS_BEGIN(); PROCESS_PAUSE(); PRINTF("RD client started\n"); sprintf(rd_client_name, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", uip_lladdr.addr[0], uip_lladdr.addr[1], uip_lladdr.addr[2], uip_lladdr.addr[3], uip_lladdr.addr[4], uip_lladdr.addr[5], uip_lladdr.addr[6], uip_lladdr.addr[7]); while(1) { new_address = 0; while(!registered) { while(uip_is_addr_unspecified(&rd_server_ipaddr)) { status = RD_CLIENT_UNCONFIGURED; PROCESS_YIELD(); } status = RD_CLIENT_REGISTERING; etimer_set(&et, CLOCK_SECOND); PROCESS_YIELD_UNTIL(etimer_expired(&et)); PRINTF("Registering to "); PRINT6ADDR(&rd_server_ipaddr); PRINTF(" %d with %s\n", rd_server_port, resources_list); coap_init_message(request, COAP_TYPE_CON, COAP_POST, 0); coap_set_header_uri_path(request, "rd"); sprintf(query_buffer, "ep=%s&b=U<=%d", rd_client_name, RD_CLIENT_LIFETIME); coap_set_header_uri_query(request, query_buffer); coap_set_payload(request, (uint8_t *) resources_list, resources_list_size); COAP_BLOCKING_REQUEST_BLOCK_RESPONSE(coap_default_context, &rd_server_ipaddr, UIP_HTONS(rd_server_port), request, client_registration_request_handler, client_registration_response_handler); } status = RD_CLIENT_REGISTERED; etimer_set(&et, RD_CLIENT_LIFETIME * CLOCK_SECOND / 10 * 9); PROCESS_YIELD_UNTIL(etimer_expired(&et) || new_address); registered = 0; if(!new_address) { PRINTF("Update endpoint %s\n", registration_name); coap_init_message(request, COAP_TYPE_CON, COAP_PUT, 0); coap_set_header_uri_path(request, registration_name); sprintf(query_buffer, "b=U<=%d", RD_CLIENT_LIFETIME); coap_set_header_uri_query(request, query_buffer); COAP_BLOCKING_REQUEST(coap_default_context, &rd_server_ipaddr, UIP_HTONS(rd_server_port), request, client_update_response_handler); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ void motion_resource_periodic_handler(resource_t *r) { static int event_counter; char buffer[16]; PRINTF("*** motion_resource_periodic_handler(): called!\n"); int value = 4095-sensor_value(0); PRINTF("%d", value); sprintf(buffer, "%d", value); coap_packet_t notification[1]; coap_init_message(notification, COAP_TYPE_NON, REST.status.OK, 0); coap_set_payload(notification, buffer, strlen(buffer)+1); REST.notify_subscribers(r, event_counter++, notification); PRINTF("*** motion_resource_periodic_handler(): done\n"); }
void light_periodic_handler(resource_t *r) { uint16_t light_photosynthetic = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC); static uint16_t obs_counter = 0; static char content[11]; ++obs_counter; /* Build notification. */ coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */ coap_init_message(notification, COAP_TYPE_NON, REST.status.OK, 0 ); coap_set_payload(notification, content, snprintf(content, sizeof(content), "%u", light_photosynthetic)); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, obs_counter, notification); }
static void AI1_periodic_handler(resource_t *r) { static uint16_t obs_counter = 0; ++obs_counter; char msg[4]; snprintf((char *)msg, sizeof(msg), "%u", ai1_in()); /* Build notification. */ coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */ coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, TEXT_PLAIN ); coap_set_payload(notification, msg, strlen(msg)); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, obs_counter, notification); }
int lwm2m_ping(lwm2m_context_t * contextP) { lwm2m_server_t *srv = contextP->serverList; while (NULL != srv) { if (srv->status == STATE_REGISTERED) { coap_packet_t message[1]; coap_init_message(message, COAP_TYPE_CON, COAP_204_CHANGED, 0); coap_set_payload(message, NULL, 0); (void)message_send(contextP, message, srv->addr, srv->addrLen); break; } srv = srv->next; } return NO_ERROR; }
void separate_finalize_handler() { if(separate_active) { coap_transaction_t *transaction = NULL; if((transaction = coap_new_transaction(separate_store->request_metadata.mid, &separate_store->request_metadata.addr, separate_store->request_metadata.port))) { /* This way the packet can be treated as pointer as usual. */ coap_packet_t response[1]; /* Restore the request information for the response. */ coap_separate_resume(response, &separate_store->request_metadata, CONTENT_2_05); coap_set_payload(response, separate_store->buffer, strlen(separate_store->buffer)); /* * Be aware to respect the Block2 option, which is also stored * in the coap_separate_t. As it is a critical option, this * example resource pretends to handle it for compliance. */ coap_set_header_block2(response, separate_store->request_metadata.block2_num, 0, separate_store->request_metadata.block2_size); /* Warning: No check for serialization error. */ transaction->packet_len = coap_serialize_message(response, transaction->packet); coap_send_transaction(transaction); /* The engine will clear the transaction (right after send for NON, after acked for CON). */ separate_active = 0; } else { /* * Set timer for retry, send error message, ... * The example simply waits for another button press. */ } } /* if (separate_active) */ }
static int prv_make_operation(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, coap_method_t method, char * buffer, int length, lwm2m_result_callback_t callback, void * userData) { lwm2m_client_t * clientP; lwm2m_transaction_t * transaction; dm_data_t * dataP; clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); if (clientP == NULL) return COAP_404_NOT_FOUND; transaction = transaction_new(method, uriP, contextP->nextMID++, ENDPOINT_CLIENT, (void *)clientP); if (transaction == NULL) return INTERNAL_SERVER_ERROR_5_00; if (buffer != NULL) { // TODO: Take care of fragmentation coap_set_payload(transaction->message, buffer, length); } if (callback != NULL) { dataP = (dm_data_t *)lwm2m_malloc(sizeof(dm_data_t)); if (dataP == NULL) { transaction_free(transaction); return COAP_500_INTERNAL_SERVER_ERROR; } memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); dataP->callback = callback; dataP->userData = userData; transaction->callback = dm_result_callback; transaction->userData = (void *)dataP; } contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); return transaction_send(contextP, transaction); }
/*----------------------------------------------------------------------------*/ void lo_event_handler(tres_res_t *task) { coap_packet_t notification[1]; resource_t r; int len; // Build notification coap_init_message(notification, COAP_TYPE_CON, REST.status.OK, 0); len = strlen((char *)task->last_output); coap_set_payload(notification, task->last_output, len); // Notify the registered observers with the given message type, observe // option, and payload r.url = task->lo_url; task->obs_count++; REST.notify_subscribers(&r, task->obs_count, notification); }
/* Additionally, a handler function named [resource name]_event_handler must be implemented for each PERIODIC_RESOURCE defined. * It will be called by the REST manager process with the defined period. */ void event_event_handler(resource_t *r) { static uint16_t event_counter = 0; static char content[12]; ++event_counter; PRINTF("TICK %u for /%s\n", event_counter, r->url); /* Build notification. */ coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */ coap_init_message(notification, COAP_TYPE_CON, REST.status.OK, 0 ); coap_set_payload(notification, content, snprintf(content, sizeof(content), "EVENT %u", event_counter)); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, event_counter, notification); }
void co2_periodic_handler(resource_t *r) { static uint32_t obs_counter = 0; char content[32]; obs_counter++; uint16_t co2 = co2_get(); /* Build notification. */ coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */ coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0 ); coap_set_payload(notification, content, snprintf(content, sizeof(content), "%u", co2)); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, obs_counter, notification); }
int lwm2m_register(lwm2m_context_t * contextP) { char * query; char payload[512]; int payload_length; lwm2m_server_t * targetP; payload_length = prv_getRegisterPayload(contextP, payload, sizeof(payload)); if (payload_length == 0) return INTERNAL_SERVER_ERROR_5_00; query = (char*)lwm2m_malloc(QUERY_LENGTH + strlen(contextP->endpointName) + 1); if (query == NULL) return INTERNAL_SERVER_ERROR_5_00; strcpy(query, QUERY_TEMPLATE); strcpy(query + QUERY_LENGTH, contextP->endpointName); targetP = contextP->serverList; while (targetP != NULL) { lwm2m_transaction_t * transaction; transaction = transaction_new(COAP_POST, NULL, contextP->nextMID++, ENDPOINT_SERVER, (void *)targetP); if (transaction == NULL) return INTERNAL_SERVER_ERROR_5_00; coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT); coap_set_header_uri_query(transaction->message, query); coap_set_payload(transaction->message, payload, payload_length); transaction->callback = prv_handleRegistrationReply; transaction->userData = (void *) contextP; contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); if (transaction_send(contextP, transaction) == 0) { targetP->status = STATE_REG_PENDING; targetP->mid = transaction->mID; } targetP = targetP->next; } lwm2m_free(query); return 0; }
void oc_send_separate_response(oc_separate_response_t *handle, oc_status_t response_code) { oc_response_buffer_t response_buffer; response_buffer.buffer = handle->buffer; response_buffer.response_length = response_length(); response_buffer.code = oc_status_code(response_code); coap_separate_t *cur = oc_list_head(handle->requests), *next = NULL; coap_packet_t response[1]; while (cur != NULL) { next = cur->next; if (cur->observe > 0) { coap_transaction_t *t = coap_new_transaction(coap_get_mid(), &cur->endpoint); if (t) { coap_separate_resume(response, cur, oc_status_code(response_code), t->mid); coap_set_header_content_format(response, APPLICATION_CBOR); if (cur->observe == 1) { coap_set_header_observe(response, 1); } if (response_buffer.response_length > 0) { coap_set_payload(response, handle->buffer, response_buffer.response_length); } t->message->length = coap_serialize_message(response, t->message->data); coap_send_transaction(t); } coap_separate_clear(handle, cur); } else { if (coap_notify_observers(NULL, &response_buffer, &cur->endpoint) == 0) { coap_separate_clear(handle, cur); } } cur = next; } if (oc_list_length(handle->requests) == 0) { handle->active = 0; } }
/* Additionally, a handler function named [resource name]_event_handler must be implemented for each PERIODIC_RESOURCE defined. * It will be called by the REST manager process with the defined period. */ void DI1_event_handler(resource_t *r) { static uint16_t event_counter = 0; ++event_counter; PRINTF("TICK %u for /%s\n", event_counter, r->url); char msg[4]; snprintf(msg, sizeof(msg), "%u", di1_in()); /* Build notification. */ coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */ coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0); REST.set_header_content_type(notification, REST.type.APPLICATION_JSON); coap_set_payload(notification, msg, strlen(msg)); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, event_counter, notification); }
int lwm2m_bootstrap_write(lwm2m_context_t * contextP, void * sessionH, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, size_t length) { lwm2m_transaction_t * transaction; bs_data_t * dataP; LOG_URI(uriP); if (uriP == NULL || buffer == NULL || length == 0) { return COAP_400_BAD_REQUEST; } transaction = transaction_new(sessionH, COAP_PUT, NULL, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_content_type(transaction->message, format); coap_set_payload(transaction->message, buffer, length); dataP = (bs_data_t *)lwm2m_malloc(sizeof(bs_data_t)); if (dataP == NULL) { transaction_free(transaction); return COAP_500_INTERNAL_SERVER_ERROR; } dataP->isUri = true; memcpy(&dataP->uri, uriP, sizeof(lwm2m_uri_t)); dataP->callback = contextP->bootstrapCallback; dataP->userData = contextP->bootstrapUserData; transaction->callback = prv_resultCallback; transaction->userData = (void *)dataP; contextP->transactionList = (lwm2m_transaction_t *)LWM2M_LIST_ADD(contextP->transactionList, transaction); return transaction_send(contextP, transaction); }
void ICACHE_FLASH_ATTR lwm2m_resource_value_changed(lwm2m_context_t * contextP, lwm2m_uri_t * uriP) { int result; obs_list_t * listP; lwm2m_watcher_t * watcherP; listP = prv_getObservedList(contextP, uriP); while (listP != NULL) { obs_list_t * targetP; uint8_t * buffer = NULL; size_t length = 0; lwm2m_media_type_t format; format = LWM2M_CONTENT_TEXT; result = object_read(contextP, &listP->item->uri, &format, &buffer, &length); if (result == COAP_205_CONTENT) { coap_packet_t message[1]; coap_init_message(message, COAP_TYPE_NON, COAP_205_CONTENT, 0); coap_set_header_content_type(message, format); coap_set_payload(message, buffer, length); for (watcherP = listP->item->watcherList ; watcherP != NULL ; watcherP = watcherP->next) { watcherP->lastMid = contextP->nextMID++; message->mid = watcherP->lastMid; coap_set_header_token(message, watcherP->token, watcherP->tokenLen); coap_set_header_observe(message, watcherP->counter++); (void)message_send(contextP, message, watcherP->server->sessionH); } } targetP = listP; listP = listP->next; lwm2m_free(targetP); } }