MBED_DEMOS / nsp_resources

Dependencies:   nsdl_lib

Dependents:   mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet mbed_nsp_endpoint_nxp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nsp_getput.cpp Source File

nsp_getput.cpp

00001 #include "nsp_getput.h"
00002 
00003 extern char *strdup(char *str);
00004 
00005 // NSP Get/Put Method
00006 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); }
00007 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) {
00008 if (m_logger != NULL) m_logger->log_memory("NSP GETPUT MEMORY CHECK(begin)");
00009 #ifdef NSP_ENABLE_GETPUT
00010     sn_coap_hdr_s *coap_res_ptr = NULL;
00011     bool is_get = false;
00012 
00013     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00014     {
00015         //if (m_logger != NULL) m_logger->log("NSP: %s requested",tag);
00016         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00017         if (coap_res_ptr != NULL && io != NULL && io->stringValue() != NULL) {      // io->stringValue() refreshes the value
00018             coap_res_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
00019             coap_res_ptr->payload_len = strlen(io->resource()->getValuePointer());
00020             coap_res_ptr->payload_ptr = (uint8_t*)io->resource()->getValuePointer();
00021             //if (m_logger != NULL) m_logger->log("NSP: Sending %s = %s...",tag,io->resource()->getValuePointer());
00022             if (observer != NULL) (observer)(received_coap_ptr,coap_res_ptr);
00023             sn_nsdl_send_coap_message(address, coap_res_ptr);
00024             if (m_logger != NULL) m_logger->log("NSP: Sent %s = %s...",tag,io->resource()->getValuePointer());
00025         }
00026         else if (coap_res_ptr == NULL) {
00027             if (m_logger != NULL) m_logger->log("NSP: %s location request: coap_res_ptr is NULL!",tag);
00028         }
00029         else {
00030             if (m_logger != NULL) m_logger->log("NSP: %s location request: NSPio is NULL!",tag);
00031         }
00032         is_get = true;
00033     }
00034     else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
00035     {   //if (m_logger != NULL) m_logger->log("NSP: setting %s",tag);
00036         // copy the new value
00037         if (io->resource() != NULL && received_coap_ptr->payload_ptr != NULL && received_coap_ptr->payload_len > 0) {
00038             io->resource()->setValue((char *)received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
00039             if (m_logger != NULL) m_logger->log("NSP: set %s to %s...",tag,io->resource()->getValuePointer());
00040         }
00041         else {
00042             if (m_logger != NULL) m_logger->log("NSP: %s resource pointer is NULL",tag);
00043         }
00044         
00045         // response back
00046         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
00047         sn_nsdl_send_coap_message(address, coap_res_ptr);
00048     }
00049     if (is_get && coap_res_ptr != NULL && coap_res_ptr->options_list_ptr != NULL) coap_res_ptr->options_list_ptr->observe_ptr = NULL;
00050     if (coap_res_ptr != NULL) sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00051     if (m_logger != NULL) m_logger->log_memory("NSP GETPUT MEMORY CHECK(done)");
00052     return 0;
00053 #else
00054     if (m_logger != NULL) m_logger->log("NSP GetPut() functions disabled for %s",tag);
00055     if (m_logger != NULL) m_logger->log_memory("NSP GETPUT MEMORY CHECK(off)");
00056     return 0;
00057 #endif
00058 }