adding resources firmware and 1/0/8
Dependencies: Beep C12832_lcd EthernetInterface EthernetNetIf HTTPClient LM75B MMA7660 mbed-rtos mbed nsdl_lib
Fork of LWM2M_NanoService_Ethernet by
temperature.cpp
00001 // Temperature resource implementation 00002 00003 #include "mbed.h" 00004 #include "rtos.h" 00005 #include "LM75B.h" 00006 #include "nsdl_support.h" 00007 #include "temperature.h" 00008 00009 #define TEMP_RES_ID "303/0/5700" 00010 00011 static LM75B tmp(p28,p27); 00012 /* stored data for observable resource */ 00013 static uint8_t obs_number = 0; 00014 static uint8_t *obs_token_ptr = NULL; 00015 static uint8_t obs_token_len = 0; 00016 static char temp_val[5]; 00017 extern Serial pc; 00018 00019 /* Thread for calling libNsdl exec function (cleanup, resendings etc..) */ 00020 /* Node updates temperature every 10 seconds. Notification sending is done here. */ 00021 static void exec_call_thread(void const *args) 00022 { 00023 int32_t time = 0; 00024 while (true) 00025 { 00026 wait(1); 00027 time++; 00028 sn_nsdl_exec(time); 00029 if((!(time % 10)) && obs_number != 0 && obs_token_ptr != NULL) 00030 { 00031 obs_number++; 00032 sprintf(temp_val,"%2.2f" ,tmp.read()); 00033 if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temp_val, 5, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0) 00034 pc.printf("Observation sending failed\r\n"); 00035 else 00036 pc.printf("Observation, temp=%s\r\n", temp_val); 00037 } 00038 } 00039 } 00040 00041 /* Only GET method allowed */ 00042 /* Observable resource */ 00043 static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) 00044 { 00045 sprintf(temp_val,"%2.2f" ,tmp.read()); 00046 sn_coap_hdr_s *coap_res_ptr = 0; 00047 00048 pc.printf("temp callback\r\n"); 00049 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); 00050 00051 coap_res_ptr->payload_len = 5; 00052 coap_res_ptr->payload_ptr = (uint8_t*)temp_val; 00053 00054 if(received_coap_ptr->token_ptr) 00055 { 00056 pc.printf("Token included\r\n"); 00057 if(obs_token_ptr) 00058 { 00059 free(obs_token_ptr); 00060 obs_token_ptr = 0; 00061 } 00062 obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); 00063 if(obs_token_ptr) 00064 { 00065 memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); 00066 obs_token_len = received_coap_ptr->token_len; 00067 } 00068 } 00069 00070 if(received_coap_ptr->options_list_ptr->observe) 00071 { 00072 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); 00073 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); 00074 coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; 00075 coap_res_ptr->options_list_ptr->observe_len = 1; 00076 obs_number++; 00077 } 00078 00079 sn_nsdl_send_coap_message(address, coap_res_ptr); 00080 00081 coap_res_ptr->options_list_ptr->observe_ptr = 0; 00082 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); 00083 return 0; 00084 } 00085 00086 int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr) 00087 { 00088 static Thread exec_thread(exec_call_thread); 00089 00090 nsdl_create_dynamic_resource(resource_ptr, sizeof(TEMP_RES_ID)-1, (uint8_t*)TEMP_RES_ID, 0, 0, 1, &temp_resource_cb, SN_GRS_GET_ALLOWED); 00091 return 0; 00092 }
Generated on Sat Jul 16 2022 02:03:09 by 1.7.2