nsp resources for the mbed nsp lighting endpoint
Dependencies: nsdl_lib
Dependents: mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet mbed_nsp_endpoint_nxp
switch.cpp@3:30c96bd77160, 2014-09-26 (annotated)
- Committer:
- ansond
- Date:
- Fri Sep 26 05:55:55 2014 +0000
- Revision:
- 3:30c96bd77160
- Parent:
- 0:b26c3526ee89
updates for new logger
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:b26c3526ee89 | 1 | // light_sw resource implementation |
ansond | 0:b26c3526ee89 | 2 | |
ansond | 0:b26c3526ee89 | 3 | #include "mbed.h" |
ansond | 0:b26c3526ee89 | 4 | #include "nsdl_support.h" |
ansond | 0:b26c3526ee89 | 5 | #include "switch.h" |
ansond | 0:b26c3526ee89 | 6 | #include "NSPLightSwitchAction.h" |
ansond | 0:b26c3526ee89 | 7 | #include "NSPio.h" |
ansond | 0:b26c3526ee89 | 8 | |
ansond | 3:30c96bd77160 | 9 | extern Logger *logger; |
ansond | 0:b26c3526ee89 | 10 | extern NSPLightSwitchAction *_switchAction; |
ansond | 0:b26c3526ee89 | 11 | |
ansond | 0:b26c3526ee89 | 12 | NSPio *light_sw = NULL; |
ansond | 0:b26c3526ee89 | 13 | |
ansond | 0:b26c3526ee89 | 14 | // observation support |
ansond | 0:b26c3526ee89 | 15 | static uint8_t light_sw_obs_number = 0; |
ansond | 0:b26c3526ee89 | 16 | static uint8_t *light_sw_obs_token_ptr = NULL; |
ansond | 0:b26c3526ee89 | 17 | static uint8_t light_sw_obs_token_len = 0; |
ansond | 0:b26c3526ee89 | 18 | |
ansond | 0:b26c3526ee89 | 19 | // send our light_sw observation |
ansond | 0:b26c3526ee89 | 20 | void send_light_sw_observation() { |
ansond | 0:b26c3526ee89 | 21 | if (light_sw_obs_token_ptr != NULL && light_sw_obs_number != 0) { |
ansond | 3:30c96bd77160 | 22 | //if (m_logger != NULL) m_logger->log("Sending Observation (switch): %s",light_sw->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 23 | 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); |
ansond | 0:b26c3526ee89 | 24 | } |
ansond | 0:b26c3526ee89 | 25 | } |
ansond | 0:b26c3526ee89 | 26 | |
ansond | 0:b26c3526ee89 | 27 | // init light switch |
ansond | 0:b26c3526ee89 | 28 | void init_switch(Resource *resource) { |
ansond | 3:30c96bd77160 | 29 | if (light_sw == NULL && resource != NULL) light_sw = new NSPio(m_logger,resource,&send_light_sw_observation); |
ansond | 0:b26c3526ee89 | 30 | } |
ansond | 0:b26c3526ee89 | 31 | |
ansond | 0:b26c3526ee89 | 32 | |
ansond | 0:b26c3526ee89 | 33 | // update our light_sw observation |
ansond | 0:b26c3526ee89 | 34 | void update_light_sw_observation(sn_coap_hdr_s *received_coap_ptr,sn_coap_hdr_s *coap_res_ptr) { |
ansond | 3:30c96bd77160 | 35 | //if (m_logger != NULL) m_logger->log("Updating Observation (switch)...starting"); |
ansond | 0:b26c3526ee89 | 36 | if (received_coap_ptr->token_ptr != NULL) { |
ansond | 0:b26c3526ee89 | 37 | if (light_sw_obs_token_ptr != NULL) free(light_sw_obs_token_ptr); |
ansond | 0:b26c3526ee89 | 38 | light_sw_obs_token_ptr = (uint8_t *)malloc(received_coap_ptr->token_len); |
ansond | 0:b26c3526ee89 | 39 | if(light_sw_obs_token_ptr != NULL) { |
ansond | 0:b26c3526ee89 | 40 | memcpy(light_sw_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
ansond | 0:b26c3526ee89 | 41 | light_sw_obs_token_len = received_coap_ptr->token_len; |
ansond | 0:b26c3526ee89 | 42 | } |
ansond | 0:b26c3526ee89 | 43 | } |
ansond | 0:b26c3526ee89 | 44 | if (received_coap_ptr->options_list_ptr->observe != NULL) { |
ansond | 0:b26c3526ee89 | 45 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
ansond | 0:b26c3526ee89 | 46 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
ansond | 0:b26c3526ee89 | 47 | coap_res_ptr->options_list_ptr->observe_ptr = &(light_sw_obs_number); |
ansond | 0:b26c3526ee89 | 48 | coap_res_ptr->options_list_ptr->observe_len = 1; |
ansond | 0:b26c3526ee89 | 49 | light_sw_obs_number++; |
ansond | 0:b26c3526ee89 | 50 | } |
ansond | 3:30c96bd77160 | 51 | //if (m_logger != NULL) m_logger->log("Updating Observation (switch)...done"); |
ansond | 0:b26c3526ee89 | 52 | } |
ansond | 0:b26c3526ee89 | 53 | |
ansond | 0:b26c3526ee89 | 54 | /* Only GET and PUT method allowed */ |
ansond | 0:b26c3526ee89 | 55 | uint8_t switch_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) { |
ansond | 0:b26c3526ee89 | 56 | uint8_t result = nsp_getput("switch",light_sw,received_coap_ptr,address,proto,&update_light_sw_observation); |
ansond | 0:b26c3526ee89 | 57 | |
ansond | 0:b26c3526ee89 | 58 | // action upon it... |
ansond | 0:b26c3526ee89 | 59 | if (result == 0 && _switchAction != NULL) { |
ansond | 0:b26c3526ee89 | 60 | if (light_sw->intValue() == 1) |
ansond | 0:b26c3526ee89 | 61 | _switchAction->on(); |
ansond | 0:b26c3526ee89 | 62 | else if (light_sw->intValue() == 2) |
ansond | 0:b26c3526ee89 | 63 | _switchAction->blink(); |
ansond | 0:b26c3526ee89 | 64 | else |
ansond | 0:b26c3526ee89 | 65 | _switchAction->off(); |
ansond | 0:b26c3526ee89 | 66 | } |
ansond | 0:b26c3526ee89 | 67 | |
ansond | 0:b26c3526ee89 | 68 | return result; |
ansond | 0:b26c3526ee89 | 69 | } |