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

Dependents:   mbed-IBooth-ETH

Committer:
erigow01
Date:
Wed Dec 10 14:08:28 2014 +0000
Revision:
0:fe416b02628b
Child:
4:bc54f10b3234
Initial Check-in

Who changed what in which revision?

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