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

battery.cpp

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