LWM2M Weather Station
Dependencies: EthernetInterfaceUpdate mbed-rtos mbed nanoservice_client_1_12_X
IPSO_presence.cpp
00001 // IPSO Presence sensor resource implementation 00002 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #include "nsdl_support.h" 00006 00007 #define PRESENCE_RES_ID "3302/0/5500" 00008 #define PRESENCE_RES_RT "urn:X-ipso:presence" 00009 00010 extern Serial pc; 00011 uint8_t presence_max_age = 0; 00012 uint8_t presence_content_type = 50; 00013 00014 static uint8_t pres_obs_number = 0; 00015 static uint8_t *pres_obs_token_ptr = NULL; 00016 static uint8_t pres_obs_token_len = 0; 00017 00018 DigitalIn presenceSensor(D2); 00019 uint8_t presence = 0; 00020 uint8_t last_presence = 0; 00021 char presenceString[1]; 00022 00023 static void pres_observe_thread(void const *args) 00024 { 00025 while (true) 00026 { 00027 wait(.1); 00028 presence = presenceSensor.read(); 00029 if((presence != last_presence) && pres_obs_number != 0 && pres_obs_token_ptr != NULL) 00030 { 00031 last_presence = presence; 00032 pc.printf("presence: %d\r\n", presence); 00033 pres_obs_number++; 00034 sprintf(presenceString,"%d", presence); 00035 if(sn_nsdl_send_observation_notification(pres_obs_token_ptr, pres_obs_token_len, (uint8_t*)presenceString, strlen(presenceString), &pres_obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) 00036 pc.printf("Presence observation sending failed\r\n"); 00037 else 00038 pc.printf("Presence observation\r\n"); 00039 } 00040 } 00041 } 00042 00043 00044 /* Only GET method allowed */ 00045 static uint8_t presence_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00046 { 00047 sn_coap_hdr_s *coap_res_ptr = 0; 00048 presence = presenceSensor.read(); 00049 sprintf(presenceString,"%d", presence); 00050 pc.printf("presence callback\r\n"); 00051 pc.printf("presence state %s\r\n", presenceString); 00052 00053 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) 00054 { 00055 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00056 00057 coap_res_ptr->payload_len = strlen(presenceString); 00058 coap_res_ptr->payload_ptr = (uint8_t*)presenceString; 00059 00060 coap_res_ptr->content_type_ptr = &presence_content_type; 00061 coap_res_ptr->content_type_len = sizeof(presence_content_type); 00062 00063 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s)); 00064 if(!coap_res_ptr->options_list_ptr) 00065 { 00066 pc.printf("cant alloc option list\r\n"); 00067 coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover 00068 } 00069 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00070 coap_res_ptr->options_list_ptr->max_age_ptr = &presence_max_age; 00071 coap_res_ptr->options_list_ptr->max_age_len = sizeof(presence_max_age); 00072 00073 if(received_coap_ptr->token_ptr) 00074 { 00075 pc.printf("Token included\r\n"); 00076 if(pres_obs_token_ptr) 00077 { 00078 free(pres_obs_token_ptr); 00079 pres_obs_token_ptr = 0; 00080 } 00081 pres_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); 00082 if(pres_obs_token_ptr) 00083 { 00084 memcpy(pres_obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); 00085 pres_obs_token_len = received_coap_ptr->token_len; 00086 } 00087 } 00088 00089 if(received_coap_ptr->options_list_ptr->observe) 00090 { 00091 coap_res_ptr->options_list_ptr->observe_ptr = &pres_obs_number; 00092 coap_res_ptr->options_list_ptr->observe_len = 1; 00093 pres_obs_number++; 00094 } 00095 00096 sn_nsdl_send_coap_message(address, coap_res_ptr); 00097 nsdl_free(coap_res_ptr->options_list_ptr); 00098 coap_res_ptr->options_list_ptr = NULL; 00099 coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory 00100 } 00101 00102 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00103 00104 return 0; 00105 } 00106 00107 int create_IPSO_presence_resource(sn_nsdl_resource_info_s *resource_ptr) 00108 { 00109 static Thread exec_thread(pres_observe_thread); 00110 00111 nsdl_create_dynamic_resource(resource_ptr, sizeof(PRESENCE_RES_ID)-1, (uint8_t*)PRESENCE_RES_ID, sizeof(PRESENCE_RES_RT)-1, (uint8_t*)PRESENCE_RES_RT, 1, &presence_resource_cb, (SN_GRS_GET_ALLOWED)); 00112 return 0; 00113 }
Generated on Tue Jul 12 2022 21:24:03 by 1.7.2