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

switch.cpp

00001 // light_sw resource implementation
00002 
00003 #include "mbed.h"
00004 #include "nsdl_support.h"
00005 #include "switch.h"
00006 #include "NSPLightSwitchAction.h"
00007 #include "NSPio.h"
00008 
00009 extern Logger *logger;
00010 extern NSPLightSwitchAction *_switchAction;
00011 
00012 NSPio *light_sw = NULL;
00013 
00014 // observation support
00015 static uint8_t light_sw_obs_number = 0;
00016 static uint8_t *light_sw_obs_token_ptr = NULL;
00017 static uint8_t light_sw_obs_token_len = 0;
00018 
00019 // send our light_sw observation
00020 void send_light_sw_observation() {
00021     if (light_sw_obs_token_ptr != NULL && light_sw_obs_number != 0) {
00022         //if (m_logger != NULL) m_logger->log("Sending Observation (switch): %s",light_sw->resource()->getValuePointer());
00023         sn_nsdl_send_observation_notification(light_sw_obs_token_ptr,light_sw_obs_token_len,(uint8_t*)light_sw->resource()->getValuePointer(),strlen(light_sw->resource()->getValuePointer()),&light_sw_obs_number,sizeof(light_sw_obs_number),COAP_MSG_TYPE_NON_CONFIRMABLE, 0);
00024     }
00025 }
00026 
00027 // init light switch
00028 void init_switch(Resource *resource) {
00029     if (light_sw == NULL && resource != NULL) light_sw = new NSPio(m_logger,resource,&send_light_sw_observation);
00030 }
00031 
00032 
00033 // update our light_sw observation
00034 void update_light_sw_observation(sn_coap_hdr_s *received_coap_ptr,sn_coap_hdr_s *coap_res_ptr) {
00035     //if (m_logger != NULL) m_logger->log("Updating Observation (switch)...starting");
00036     if (received_coap_ptr->token_ptr != NULL) {
00037         if (light_sw_obs_token_ptr != NULL) free(light_sw_obs_token_ptr);
00038         light_sw_obs_token_ptr = (uint8_t *)malloc(received_coap_ptr->token_len);
00039         if(light_sw_obs_token_ptr != NULL) {
00040             memcpy(light_sw_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
00041             light_sw_obs_token_len = received_coap_ptr->token_len;
00042         }
00043     }
00044     if (received_coap_ptr->options_list_ptr->observe != NULL) {
00045         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
00046         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
00047         coap_res_ptr->options_list_ptr->observe_ptr = &(light_sw_obs_number);
00048         coap_res_ptr->options_list_ptr->observe_len = 1;
00049         light_sw_obs_number++;
00050     }
00051     //if (m_logger != NULL) m_logger->log("Updating Observation (switch)...done");
00052 }
00053 
00054 /* Only GET and PUT method allowed */
00055 uint8_t switch_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) {
00056     uint8_t result = nsp_getput("switch",light_sw,received_coap_ptr,address,proto,&update_light_sw_observation);
00057     
00058     // action upon it...
00059     if (result == 0 && _switchAction != NULL) {
00060         if (light_sw->intValue() == 1)
00061             _switchAction->on();
00062         else if (light_sw->intValue() == 2)
00063             _switchAction->blink();
00064         else
00065             _switchAction->off();
00066     }
00067         
00068     return result;
00069 }