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

mdltext.cpp

00001 // mdl text resource implementation
00002 
00003 #include "mdltext.h"
00004 
00005 #include "NSPio.h"
00006 
00007 extern Logger *logger;
00008 NSPio *mdltext = NULL;
00009 
00010 // observation support
00011 static uint8_t mdltext_obs_number = 0;
00012 static uint8_t *mdltext_obs_token_ptr = NULL;
00013 static uint8_t mdltext_obs_token_len = 0;
00014 
00015 // send our mdltext observation
00016 void send_mdltext_observation() {
00017     if (mdltext_obs_token_ptr != NULL && mdltext_obs_number != 0) {
00018         //if (m_logger != NULL) m_logger->log("Sending Observation (mdltext): %s",mdltext->resource()->getValuePointer());
00019         sn_nsdl_send_observation_notification(mdltext_obs_token_ptr,mdltext_obs_token_len,(uint8_t*)mdltext->resource()->getValuePointer(),strlen(mdltext->resource()->getValuePointer()),&mdltext_obs_number,sizeof(mdltext_obs_number),COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
00020     }
00021 }
00022 
00023 // init mdltext
00024 void init_mdltext(Resource *resource) {
00025     if (mdltext == NULL && resource != NULL) mdltext = new NSPio(m_logger,resource,&send_mdltext_observation);
00026 }
00027 
00028 // update our mdltext observation
00029 void update_mdltext_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 (mdltext)...starting");
00031     if (received_coap_ptr->token_ptr != NULL) {
00032         if (mdltext_obs_token_ptr != NULL) free(mdltext_obs_token_ptr);
00033         mdltext_obs_token_ptr = (uint8_t *)malloc(received_coap_ptr->token_len);
00034         if(mdltext_obs_token_ptr != NULL) {
00035             memcpy(mdltext_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
00036             mdltext_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 = &(mdltext_obs_number);
00043         coap_res_ptr->options_list_ptr->observe_len = 1;
00044         mdltext_obs_number++;
00045     }
00046     //if (m_logger != NULL) m_logger->log("Updating Observation (mdltext)...done");
00047 }
00048 
00049 /* Only GET method allowed */
00050 uint8_t mdltext_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) {
00051     return nsp_get("mdltext",mdltext,received_coap_ptr,address,proto,&update_mdltext_observation);
00052 }