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
current.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 | // Current resource implementation |
ansond | 0:b26c3526ee89 | 2 | |
ansond | 0:b26c3526ee89 | 3 | #include "current.h" |
ansond | 0:b26c3526ee89 | 4 | |
ansond | 0:b26c3526ee89 | 5 | #include "NSPio.h" |
ansond | 0:b26c3526ee89 | 6 | #include "MBEDCurrent.h" |
ansond | 0:b26c3526ee89 | 7 | |
ansond | 3:30c96bd77160 | 8 | extern Logger *logger; |
ansond | 0:b26c3526ee89 | 9 | NSPio *current = NULL; |
ansond | 0:b26c3526ee89 | 10 | |
ansond | 0:b26c3526ee89 | 11 | // observation support |
ansond | 0:b26c3526ee89 | 12 | static uint8_t current_obs_number = 0; |
ansond | 0:b26c3526ee89 | 13 | static uint8_t *current_obs_token_ptr = NULL; |
ansond | 0:b26c3526ee89 | 14 | static uint8_t current_obs_token_len = 0; |
ansond | 0:b26c3526ee89 | 15 | |
ansond | 0:b26c3526ee89 | 16 | // send our current observation |
ansond | 0:b26c3526ee89 | 17 | void send_current_observation() { |
ansond | 0:b26c3526ee89 | 18 | if (current_obs_token_ptr != NULL && current_obs_number != 0) { |
ansond | 3:30c96bd77160 | 19 | //if (m_logger != NULL) m_logger->log("Sending Observation (current): %s",current->resource()->getValuePointer()); |
ansond | 0:b26c3526ee89 | 20 | sn_nsdl_send_observation_notification(current_obs_token_ptr,current_obs_token_len,(uint8_t*)current->resource()->getValuePointer(),strlen(current->resource()->getValuePointer()),¤t_obs_number,sizeof(current_obs_number),COAP_MSG_TYPE_NON_CONFIRMABLE, 0); |
ansond | 0:b26c3526ee89 | 21 | } |
ansond | 0:b26c3526ee89 | 22 | } |
ansond | 0:b26c3526ee89 | 23 | |
ansond | 0:b26c3526ee89 | 24 | // init current |
ansond | 0:b26c3526ee89 | 25 | void init_current(Resource *resource) { |
ansond | 3:30c96bd77160 | 26 | if (current == NULL && resource != NULL) current = new NSPio(new MBEDCurrent(m_logger,resource),&send_current_observation); |
ansond | 0:b26c3526ee89 | 27 | } |
ansond | 0:b26c3526ee89 | 28 | |
ansond | 0:b26c3526ee89 | 29 | // update our current observation |
ansond | 0:b26c3526ee89 | 30 | void update_current_observation(sn_coap_hdr_s *received_coap_ptr,sn_coap_hdr_s *coap_res_ptr) { |
ansond | 3:30c96bd77160 | 31 | //if (m_logger != NULL) m_logger->log("Updating Observation (current)...starting"); |
ansond | 0:b26c3526ee89 | 32 | if (received_coap_ptr->token_ptr != NULL) { |
ansond | 0:b26c3526ee89 | 33 | if (current_obs_token_ptr != NULL) free(current_obs_token_ptr); |
ansond | 0:b26c3526ee89 | 34 | current_obs_token_ptr = (uint8_t *)malloc(received_coap_ptr->token_len); |
ansond | 0:b26c3526ee89 | 35 | if(current_obs_token_ptr != NULL) { |
ansond | 0:b26c3526ee89 | 36 | memcpy(current_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); |
ansond | 0:b26c3526ee89 | 37 | current_obs_token_len = received_coap_ptr->token_len; |
ansond | 0:b26c3526ee89 | 38 | } |
ansond | 0:b26c3526ee89 | 39 | } |
ansond | 0:b26c3526ee89 | 40 | if (received_coap_ptr->options_list_ptr->observe != NULL) { |
ansond | 0:b26c3526ee89 | 41 | coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); |
ansond | 0:b26c3526ee89 | 42 | memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); |
ansond | 0:b26c3526ee89 | 43 | coap_res_ptr->options_list_ptr->observe_ptr = &(current_obs_number); |
ansond | 0:b26c3526ee89 | 44 | coap_res_ptr->options_list_ptr->observe_len = 1; |
ansond | 0:b26c3526ee89 | 45 | current_obs_number++; |
ansond | 0:b26c3526ee89 | 46 | } |
ansond | 3:30c96bd77160 | 47 | //if (m_logger != NULL) m_logger->log("Updating Observation (current)...done"); |
ansond | 0:b26c3526ee89 | 48 | } |
ansond | 0:b26c3526ee89 | 49 | |
ansond | 0:b26c3526ee89 | 50 | /* Only GET method allowed */ |
ansond | 0:b26c3526ee89 | 51 | uint8_t current_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) { |
ansond | 0:b26c3526ee89 | 52 | return nsp_get("current",current,received_coap_ptr,address,proto,&update_current_observation); |
ansond | 0:b26c3526ee89 | 53 | } |