nsp resources for the mbed nsp lighting endpoint
Dependencies: nsdl_lib
Dependents: mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet mbed_nsp_endpoint_nxp
nsp_getput.cpp@0:b26c3526ee89, 2014-09-17 (annotated)
- Committer:
- ansond
- Date:
- Wed Sep 17 15:24:11 2014 +0000
- Revision:
- 0:b26c3526ee89
- Child:
- 3:30c96bd77160
initial checkin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:b26c3526ee89 | 1 | #include "nsp_getput.h" |
ansond | 0:b26c3526ee89 | 2 | |
ansond | 0:b26c3526ee89 | 3 | extern char *strdup(char *str); |
ansond | 0:b26c3526ee89 | 4 | |
ansond | 0:b26c3526ee89 | 5 | // NSP Get/Put Method |
ansond | 0:b26c3526ee89 | 6 | uint8_t nsp_getput(char *tag,NSPio *io,sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) { return nsp_getput(tag,io,received_coap_ptr,address,proto,NULL); } |
ansond | 0:b26c3526ee89 | 7 | uint8_t nsp_getput(char *tag,NSPio *io,sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto,sn_update_observation_t observer) { |
ansond | 0:b26c3526ee89 | 8 | if (error_handler != NULL) error_handler->log_memory("NSP GETPUT MEMORY CHECK(begin)"); |
ansond | 0:b26c3526ee89 | 9 | #ifdef NSP_ENABLE_GETPUT |
ansond | 0:b26c3526ee89 | 10 | sn_coap_hdr_s *coap_res_ptr = NULL; |
ansond | 0:b26c3526ee89 | 11 | bool is_get = false; |
ansond | 0:b26c3526ee89 | 12 | |
ansond | 0:b26c3526ee89 | 13 | if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) |
ansond | 0:b26c3526ee89 | 14 | { |
ansond | 0:b26c3526ee89 | 15 | //if (error_handler != NULL) error_handler->log("NSP: %s requested",tag); |
ansond | 0:b26c3526ee89 | 16 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); |
ansond | 0:b26c3526ee89 | 17 | if (coap_res_ptr != NULL && io != NULL && io->stringValue() != NULL) { // io->stringValue() refreshes the value |
ansond | 0:b26c3526ee89 | 18 | coap_res_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE; |
ansond | 0:b26c3526ee89 | 19 | coap_res_ptr->payload_len = strlen(io->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 20 | coap_res_ptr->payload_ptr = (uint8_t*)io->resource()->getValuePointer(); |
ansond | 0:b26c3526ee89 | 21 | //if (error_handler != NULL) error_handler->log("NSP: Sending %s = %s...",tag,io->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 22 | if (observer != NULL) (observer)(received_coap_ptr,coap_res_ptr); |
ansond | 0:b26c3526ee89 | 23 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
ansond | 0:b26c3526ee89 | 24 | if (error_handler != NULL) error_handler->log("NSP: Sent %s = %s...",tag,io->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 25 | } |
ansond | 0:b26c3526ee89 | 26 | else if (coap_res_ptr == NULL) { |
ansond | 0:b26c3526ee89 | 27 | if (error_handler != NULL) error_handler->log("NSP: %s location request: coap_res_ptr is NULL!",tag); |
ansond | 0:b26c3526ee89 | 28 | } |
ansond | 0:b26c3526ee89 | 29 | else { |
ansond | 0:b26c3526ee89 | 30 | if (error_handler != NULL) error_handler->log("NSP: %s location request: NSPio is NULL!",tag); |
ansond | 0:b26c3526ee89 | 31 | } |
ansond | 0:b26c3526ee89 | 32 | is_get = true; |
ansond | 0:b26c3526ee89 | 33 | } |
ansond | 0:b26c3526ee89 | 34 | else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) |
ansond | 0:b26c3526ee89 | 35 | { //if (error_handler != NULL) error_handler->log("NSP: setting %s",tag); |
ansond | 0:b26c3526ee89 | 36 | // copy the new value |
ansond | 0:b26c3526ee89 | 37 | if (io->resource() != NULL && received_coap_ptr->payload_ptr != NULL && received_coap_ptr->payload_len > 0) { |
ansond | 0:b26c3526ee89 | 38 | io->resource()->setValue((char *)received_coap_ptr->payload_ptr,received_coap_ptr->payload_len); |
ansond | 0:b26c3526ee89 | 39 | if (error_handler != NULL) error_handler->log("NSP: set %s to %s...",tag,io->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 40 | } |
ansond | 0:b26c3526ee89 | 41 | else { |
ansond | 0:b26c3526ee89 | 42 | if (error_handler != NULL) error_handler->log("NSP: %s resource pointer is NULL",tag); |
ansond | 0:b26c3526ee89 | 43 | } |
ansond | 0:b26c3526ee89 | 44 | |
ansond | 0:b26c3526ee89 | 45 | // response back |
ansond | 0:b26c3526ee89 | 46 | coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); |
ansond | 0:b26c3526ee89 | 47 | sn_nsdl_send_coap_message(address, coap_res_ptr); |
ansond | 0:b26c3526ee89 | 48 | } |
ansond | 0:b26c3526ee89 | 49 | if (is_get && coap_res_ptr != NULL && coap_res_ptr->options_list_ptr != NULL) coap_res_ptr->options_list_ptr->observe_ptr = NULL; |
ansond | 0:b26c3526ee89 | 50 | if (coap_res_ptr != NULL) sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); |
ansond | 0:b26c3526ee89 | 51 | if (error_handler != NULL) error_handler->log_memory("NSP GETPUT MEMORY CHECK(done)"); |
ansond | 0:b26c3526ee89 | 52 | return 0; |
ansond | 0:b26c3526ee89 | 53 | #else |
ansond | 0:b26c3526ee89 | 54 | if (error_handler != NULL) error_handler->log("NSP GetPut() functions disabled for %s",tag); |
ansond | 0:b26c3526ee89 | 55 | if (error_handler != NULL) error_handler->log_memory("NSP GETPUT MEMORY CHECK(off)"); |
ansond | 0:b26c3526ee89 | 56 | return 0; |
ansond | 0:b26c3526ee89 | 57 | #endif |
ansond | 0:b26c3526ee89 | 58 | } |