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

rssi.cpp

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