Weather control switch for connected day. NXP LPC 1768 module. Ethernet connectivity.

Dependencies:   EthernetInterface mbed-rtos mbed nanoservice_client_1_12

Fork of Trenton_Switch_LPC1768_WIFLY by Demo Team

Committer:
andcor02
Date:
Wed Dec 03 09:03:29 2014 +0000
Revision:
25:cb16c5248769
ETH CES

Who changed what in which revision?

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