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
wattage.cpp@0:b26c3526ee89, 2014-09-17 (annotated)
- Committer:
- ansond
- Date:
- Wed Sep 17 15:24:11 2014 +0000
- Revision:
- 0:b26c3526ee89
- Child:
- 3:30c96bd77160
initial checkin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:b26c3526ee89 | 1 | // Voltage resource implementation |
ansond | 0:b26c3526ee89 | 2 | |
ansond | 0:b26c3526ee89 | 3 | #include "wattage.h" |
ansond | 0:b26c3526ee89 | 4 | |
ansond | 0:b26c3526ee89 | 5 | #include "NSPio.h" |
ansond | 0:b26c3526ee89 | 6 | #include "MBEDWattage.h" |
ansond | 0:b26c3526ee89 | 7 | |
ansond | 0:b26c3526ee89 | 8 | extern ErrorHandler *error_handler; |
ansond | 0:b26c3526ee89 | 9 | |
ansond | 0:b26c3526ee89 | 10 | NSPio *wattage = NULL; |
ansond | 0:b26c3526ee89 | 11 | |
ansond | 0:b26c3526ee89 | 12 | // observation support |
ansond | 0:b26c3526ee89 | 13 | static uint8_t wattage_obs_number = 0; |
ansond | 0:b26c3526ee89 | 14 | static uint8_t *wattage_obs_token_ptr = NULL; |
ansond | 0:b26c3526ee89 | 15 | static uint8_t wattage_obs_token_len = 0; |
ansond | 0:b26c3526ee89 | 16 | |
ansond | 0:b26c3526ee89 | 17 | // send our wattage observation |
ansond | 0:b26c3526ee89 | 18 | void send_wattage_observation() { |
ansond | 0:b26c3526ee89 | 19 | if (wattage_obs_token_ptr != NULL && wattage_obs_number != 0) { |
ansond | 0:b26c3526ee89 | 20 | //if (error_handler != NULL) error_handler->log("Sending Observation (wattage): %s",wattage->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 21 | sn_nsdl_send_observation_notification(wattage_obs_token_ptr,wattage_obs_token_len,(uint8_t*)wattage->resource()->getValuePointer(),strlen(wattage->resource()->getValuePointer()),&wattage_obs_number,sizeof(wattage_obs_number),COAP_MSG_TYPE_NON_CONFIRMABLE, 0); |
ansond | 0:b26c3526ee89 | 22 | } |
ansond | 0:b26c3526ee89 | 23 | } |
ansond | 0:b26c3526ee89 | 24 | |
ansond | 0:b26c3526ee89 | 25 | // init wattage |
ansond | 0:b26c3526ee89 | 26 | void init_wattage(Resource *resource) { |
ansond | 0:b26c3526ee89 | 27 | if (wattage == NULL && resource != NULL) wattage = new NSPio(new MBEDWattage(error_handler,resource),&send_wattage_observation); |
ansond | 0:b26c3526ee89 | 28 | } |
ansond | 0:b26c3526ee89 | 29 | |
ansond | 0:b26c3526ee89 | 30 | // update our wattage observation |
ansond | 0:b26c3526ee89 | 31 | void update_wattage_observation(sn_coap_hdr_s *received_coap_ptr,sn_coap_hdr_s *coap_res_ptr) { |
ansond | 0:b26c3526ee89 | 32 | //if (error_handler != NULL) error_handler->log("Updating Observation (wattage)...starting"); |
ansond | 0:b26c3526ee89 | 33 | if (received_coap_ptr->token_ptr != NULL) { |
ansond | 0:b26c3526ee89 | 34 | if (wattage_obs_token_ptr != NULL) free(wattage_obs_token_ptr); |
ansond | 0:b26c3526ee89 | 35 | wattage_obs_token_ptr = (uint8_t *)malloc(received_coap_ptr->token_len); |
ansond | 0:b26c3526ee89 | 36 | if(wattage_obs_token_ptr != NULL) { |
ansond | 0:b26c3526ee89 | 37 | memcpy(wattage_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
ansond | 0:b26c3526ee89 | 38 | wattage_obs_token_len = received_coap_ptr->token_len; |
ansond | 0:b26c3526ee89 | 39 | } |
ansond | 0:b26c3526ee89 | 40 | } |
ansond | 0:b26c3526ee89 | 41 | if (received_coap_ptr->options_list_ptr->observe != NULL) { |
ansond | 0:b26c3526ee89 | 42 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
ansond | 0:b26c3526ee89 | 43 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
ansond | 0:b26c3526ee89 | 44 | coap_res_ptr->options_list_ptr->observe_ptr = &(wattage_obs_number); |
ansond | 0:b26c3526ee89 | 45 | coap_res_ptr->options_list_ptr->observe_len = 1; |
ansond | 0:b26c3526ee89 | 46 | wattage_obs_number++; |
ansond | 0:b26c3526ee89 | 47 | } |
ansond | 0:b26c3526ee89 | 48 | //if (error_handler != NULL) error_handler->log("Updating Observation (wattage)...done"); |
ansond | 0:b26c3526ee89 | 49 | } |
ansond | 0:b26c3526ee89 | 50 | |
ansond | 0:b26c3526ee89 | 51 | /* Only GET method allowed */ |
ansond | 0:b26c3526ee89 | 52 | uint8_t wattage_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) { |
ansond | 0:b26c3526ee89 | 53 | return nsp_get("wattage",wattage,received_coap_ptr,address,proto,&update_wattage_observation); |
ansond | 0:b26c3526ee89 | 54 | } |