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

mfgtext.cpp

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