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 locationtext.cpp Source File

locationtext.cpp

00001 // location text resource implementation
00002 
00003 #include "locationtext.h"
00004 
00005 #include "NSPio.h"
00006 
00007 extern Logger *logger;
00008 NSPio *locationtext = NULL;
00009 
00010 // observation support
00011 static uint8_t locationtext_obs_number = 0;
00012 static uint8_t *locationtext_obs_token_ptr = NULL;
00013 static uint8_t locationtext_obs_token_len = 0;
00014 
00015 // send our locationtext observation
00016 void send_locationtext_observation() {
00017     if (locationtext_obs_token_ptr != NULL && locationtext_obs_number != 0) {
00018         //if (m_logger != NULL) m_logger->log("Sending Observation (locationtext): %s",locationtext->resource()->getValuePointer());
00019         sn_nsdl_send_observation_notification(locationtext_obs_token_ptr,locationtext_obs_token_len,(uint8_t*)locationtext->resource()->getValuePointer(),strlen(locationtext->resource()->getValuePointer()),&locationtext_obs_number,sizeof(locationtext_obs_number),COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
00020     }
00021 }
00022 
00023 // init locationtext
00024 void init_locationtext(Resource *resource) {
00025     if (locationtext == NULL && resource != NULL) locationtext = new NSPio(m_logger,resource,&send_locationtext_observation);
00026 }
00027 
00028 // update our locationtext observation
00029 void update_locationtext_observation(sn_coap_hdr_s *received_coap_ptr,sn_coap_hdr_s *coap_res_ptr) {
00030     //if (m_logger != NULL) m_logger->log("Updating Observation (locationtext)...starting");
00031     if (received_coap_ptr->token_ptr != NULL) {
00032         if (locationtext_obs_token_ptr != NULL) free(locationtext_obs_token_ptr);
00033         locationtext_obs_token_ptr = (uint8_t *)malloc(received_coap_ptr->token_len);
00034         if(locationtext_obs_token_ptr != NULL) {
00035             memcpy(locationtext_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
00036             locationtext_obs_token_len = received_coap_ptr->token_len;
00037         }
00038     }
00039     if (received_coap_ptr->options_list_ptr->observe != NULL) {
00040         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
00041         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
00042         coap_res_ptr->options_list_ptr->observe_ptr = &(locationtext_obs_number);
00043         coap_res_ptr->options_list_ptr->observe_len = 1;
00044         locationtext_obs_number++;
00045     }
00046     //if (m_logger != NULL) m_logger->log("Updating Observation (locationtext)...done");
00047 }
00048 
00049 /* Only GET method allowed */
00050 uint8_t locationtext_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) {
00051     return nsp_get("locationtext",locationtext,received_coap_ptr,address,proto,&update_locationtext_observation);
00052 }