LWM2M Weather Station

Dependencies:   EthernetInterfaceUpdate mbed-rtos mbed nanoservice_client_1_12_X

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers barometer.cpp Source File

barometer.cpp

00001 // IPSO Barometer sensor resource implementation
00002 
00003 #include "mbed.h"
00004 #include "nsdl_support.h"
00005 
00006 #define BAROMETER_RES_ID    "3315/0/5700"
00007 #define BAROMETER_RES_RT    "urn:X-ipso:barometer"
00008 
00009 extern Serial pc;
00010 uint8_t barometer_max_age = 0; 
00011 uint8_t barometer_content_type = 50;
00012 
00013 float barometer_reading;
00014 char barometer_string[7];
00015 
00016 /* Only GET method allowed */
00017 static uint8_t barometer_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00018 {
00019     sn_coap_hdr_s *coap_res_ptr = 0;
00020 
00021     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00022     {
00023         sprintf(barometer_string,"%4.2f", barometer_reading);
00024         pc.printf("barometer: %s\r\n", barometer_string);
00025 
00026         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00027 
00028         coap_res_ptr->payload_len = strlen(barometer_string);
00029         coap_res_ptr->payload_ptr = (uint8_t*)barometer_string;
00030         
00031         coap_res_ptr->content_type_ptr = &barometer_content_type;
00032         coap_res_ptr->content_type_len = sizeof(barometer_content_type);
00033         
00034         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)nsdl_alloc(sizeof(sn_coap_options_list_s));
00035         if(!coap_res_ptr->options_list_ptr)
00036             {
00037             pc.printf("cant alloc option list for max-age\r\n");
00038             coap_res_ptr->options_list_ptr = NULL; //FIXME report error and recover
00039             }
00040         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
00041         coap_res_ptr->options_list_ptr->max_age_ptr = &barometer_max_age;
00042         coap_res_ptr->options_list_ptr->max_age_len = sizeof(barometer_max_age);
00043 
00044         sn_nsdl_send_coap_message(address, coap_res_ptr);
00045         nsdl_free(coap_res_ptr->options_list_ptr);
00046         coap_res_ptr->options_list_ptr = NULL;
00047         coap_res_ptr->content_type_ptr = NULL;// parser_release below tries to free this memory
00048 
00049     }
00050 
00051     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00052 
00053     return 0;
00054 }
00055 
00056 int create_barometer_resource(sn_nsdl_resource_info_s *resource_ptr)
00057 {
00058     nsdl_create_dynamic_resource(resource_ptr, sizeof(BAROMETER_RES_ID)-1, (uint8_t*)BAROMETER_RES_ID, sizeof(BAROMETER_RES_RT)-1, (uint8_t*)BAROMETER_RES_RT, 0, &barometer_resource_cb, (SN_GRS_GET_ALLOWED));
00059     return 0;
00060 }