haopeng tang / Mbed 2 deprecated STM32F746_LWM2M_Client

Dependencies:   F7_Ethernet mbed-rtos mbed nsdl_lib

Fork of rtos_basic by Mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers temperature.cpp Source File

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     "sen/temp"
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 static uint8_t tmp=29;
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             sprintf(temp_val,"%2.2f" ,tmp);
00034             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)
00035                 pc.printf("Observation sending failed\r\n");
00036             else
00037                 pc.printf("Observation\r\n");
00038         }
00039     }
00040 }
00041 
00042 /* Only GET method allowed */
00043 /* Observable resource */
00044 static uint8_t temp_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00045 {
00046     //sprintf(temp_val,"%2.2f" ,tmp.read());
00047     sprintf(temp_val,"%2.2f" ,tmp);
00048     sn_coap_hdr_s *coap_res_ptr = 0;
00049 
00050     pc.printf("temp callback\r\n");
00051     coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00052 
00053     coap_res_ptr->payload_len = 5;
00054     coap_res_ptr->payload_ptr = (uint8_t*)temp_val;
00055 
00056     if(received_coap_ptr->token_ptr)
00057     {
00058         pc.printf("Token included\r\n");
00059         if(obs_token_ptr)
00060         {
00061             free(obs_token_ptr);
00062             obs_token_ptr = 0;
00063         }
00064         obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
00065         if(obs_token_ptr)
00066         {
00067             memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
00068             obs_token_len = received_coap_ptr->token_len;
00069         }
00070     }
00071 
00072     if(received_coap_ptr->options_list_ptr->observe)
00073     {
00074         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
00075         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
00076         coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
00077         coap_res_ptr->options_list_ptr->observe_len = 1;
00078         obs_number++;
00079     }
00080 
00081     sn_nsdl_send_coap_message(address, coap_res_ptr);
00082 
00083     coap_res_ptr->options_list_ptr->observe_ptr = 0;
00084     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00085     return 0;
00086 }
00087 
00088 int create_temperature_resource(sn_nsdl_resource_info_s *resource_ptr)
00089 {
00090     static Thread exec_thread(exec_call_thread);
00091     
00092     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);    
00093     return 0;
00094 }