Libarary of sensor resources for mbed Device Service. Requires NSDL Support Libarary, etc.

Dependents:   mbed-IBooth-ETH

Committer:
andcor02
Date:
Thu Jul 16 12:45:41 2015 +0000
Revision:
4:bc54f10b3234
Parent:
3:a708d3686f4b
Added Serial VCOM compatibility to S2S-K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andcor02 2:f271388cbfa9 1 // accelerometer sensor resource implementation
andcor02 2:f271388cbfa9 2
andcor02 2:f271388cbfa9 3 #include "mbed.h"
andcor02 2:f271388cbfa9 4 #include "nsdl_support.h"
andcor02 2:f271388cbfa9 5 #include "node_cfg.h"
andcor02 2:f271388cbfa9 6
andcor02 3:a708d3686f4b 7 #define ACCELEROMETER_RES_ID "/sen/presence"
andcor02 3:a708d3686f4b 8 #define ACCELEROMETER_RES_RT "Presence"
andcor02 2:f271388cbfa9 9
andcor02 2:f271388cbfa9 10 #if NODE_ACCELEROMETER_STATION
andcor02 2:f271388cbfa9 11 extern bool current_accelerometer_value; //Either from Kiosk or PIR
andcor02 2:f271388cbfa9 12 static char accelerometer_val[2];
andcor02 2:f271388cbfa9 13 static uint8_t content_type = 50;
andcor02 2:f271388cbfa9 14
andcor02 2:f271388cbfa9 15
andcor02 2:f271388cbfa9 16 /* stored data for observable resource */
andcor02 2:f271388cbfa9 17 static uint8_t obs_number = 0;
andcor02 2:f271388cbfa9 18 static uint8_t *obs_token_ptr = NULL;
andcor02 2:f271388cbfa9 19 static uint8_t obs_token_len = 0;
andcor02 2:f271388cbfa9 20
andcor02 2:f271388cbfa9 21 //This is to be called from main program loop... it only sends report if accelerometer.
andcor02 2:f271388cbfa9 22 void accelerometer_report() {
andcor02 2:f271388cbfa9 23 printf("in report");
andcor02 2:f271388cbfa9 24 if(obs_number != 0){
andcor02 2:f271388cbfa9 25 obs_number++;
andcor02 2:f271388cbfa9 26 snprintf(accelerometer_val,2,"%d" ,current_accelerometer_value);
andcor02 2:f271388cbfa9 27 if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)accelerometer_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) {
andcor02 4:bc54f10b3234 28 pc.printf("accelerometer Observation Sending Failed\r\n");
andcor02 2:f271388cbfa9 29 } else {
andcor02 4:bc54f10b3234 30 pc.printf("accelerometer Observation Sent\r\n");
andcor02 2:f271388cbfa9 31 }
andcor02 2:f271388cbfa9 32 }
andcor02 2:f271388cbfa9 33 }
andcor02 2:f271388cbfa9 34
andcor02 2:f271388cbfa9 35 /* Only GET method allowed */
andcor02 2:f271388cbfa9 36 static uint8_t accelerometer_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
andcor02 2:f271388cbfa9 37 {
andcor02 2:f271388cbfa9 38 sn_coap_hdr_s *coap_res_ptr = 0;
andcor02 2:f271388cbfa9 39 snprintf(accelerometer_val,2,"%d" ,current_accelerometer_value);
andcor02 2:f271388cbfa9 40 printf("accelerometer callback\r\n");
andcor02 2:f271388cbfa9 41 printf("accelerometer: %s\r\n", accelerometer_val);
andcor02 2:f271388cbfa9 42
andcor02 2:f271388cbfa9 43 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
andcor02 2:f271388cbfa9 44 {
andcor02 2:f271388cbfa9 45 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
andcor02 2:f271388cbfa9 46
andcor02 2:f271388cbfa9 47 coap_res_ptr->payload_len = strlen(accelerometer_val);
andcor02 2:f271388cbfa9 48 coap_res_ptr->payload_ptr = (uint8_t*)accelerometer_val;
andcor02 2:f271388cbfa9 49
andcor02 2:f271388cbfa9 50 coap_res_ptr->content_type_ptr = &content_type;
andcor02 2:f271388cbfa9 51 coap_res_ptr->content_type_len = sizeof(content_type);
andcor02 2:f271388cbfa9 52
andcor02 2:f271388cbfa9 53
andcor02 2:f271388cbfa9 54 if(received_coap_ptr->token_ptr){
andcor02 2:f271388cbfa9 55 printf(" Token included\r\n");
andcor02 2:f271388cbfa9 56 if(obs_token_ptr)
andcor02 2:f271388cbfa9 57 {
andcor02 2:f271388cbfa9 58 free(obs_token_ptr);
andcor02 2:f271388cbfa9 59 obs_token_ptr = 0;
andcor02 2:f271388cbfa9 60 }
andcor02 2:f271388cbfa9 61 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
andcor02 2:f271388cbfa9 62 if(obs_token_ptr){
andcor02 2:f271388cbfa9 63 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
andcor02 2:f271388cbfa9 64 obs_token_len = received_coap_ptr->token_len;
andcor02 2:f271388cbfa9 65 }
andcor02 2:f271388cbfa9 66 }
andcor02 2:f271388cbfa9 67
andcor02 2:f271388cbfa9 68 if(received_coap_ptr->options_list_ptr->observe){
andcor02 2:f271388cbfa9 69 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
andcor02 2:f271388cbfa9 70 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
andcor02 2:f271388cbfa9 71 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
andcor02 2:f271388cbfa9 72 coap_res_ptr->options_list_ptr->observe_len = 1;
andcor02 2:f271388cbfa9 73 obs_number++;
andcor02 2:f271388cbfa9 74 }
andcor02 2:f271388cbfa9 75 printf(" Send observation %d... \r\n", obs_number);
andcor02 2:f271388cbfa9 76
andcor02 2:f271388cbfa9 77 sn_nsdl_send_coap_message(address, coap_res_ptr);
andcor02 2:f271388cbfa9 78 nsdl_free(coap_res_ptr->options_list_ptr);
andcor02 2:f271388cbfa9 79 coap_res_ptr->options_list_ptr = NULL;
andcor02 2:f271388cbfa9 80 coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
andcor02 2:f271388cbfa9 81
andcor02 2:f271388cbfa9 82 }
andcor02 2:f271388cbfa9 83
andcor02 2:f271388cbfa9 84 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
andcor02 2:f271388cbfa9 85
andcor02 2:f271388cbfa9 86 return 0;
andcor02 2:f271388cbfa9 87 }
andcor02 2:f271388cbfa9 88
andcor02 2:f271388cbfa9 89 int create_accelerometer_resource(sn_nsdl_resource_info_s *resource_ptr)
andcor02 2:f271388cbfa9 90 {
andcor02 2:f271388cbfa9 91 obs_number++;
andcor02 2:f271388cbfa9 92 nsdl_create_dynamic_resource(resource_ptr, sizeof(ACCELEROMETER_RES_ID)-1, (uint8_t*)ACCELEROMETER_RES_ID, sizeof(ACCELEROMETER_RES_RT)-1, (uint8_t*)ACCELEROMETER_RES_RT, 1, &accelerometer_resource_cb, (SN_GRS_GET_ALLOWED));
andcor02 2:f271388cbfa9 93 return 0;
andcor02 2:f271388cbfa9 94 }
andcor02 2:f271388cbfa9 95 #endif