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 MBED_DEMOS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers firmware_result.cpp Source File

firmware_result.cpp

00001 // Firmware result resource implementation
00002 
00003 #include "mbed.h"
00004 #include "rtos.h"
00005 #include "LM75B.h"
00006 #include "nsdl_support.h"
00007 #include "firmware_result.h"
00008 
00009 #define FIRMWARE_UPD_RESULT_RES_ID    "5/0/5"
00010 
00011 static uint8_t obs_number = 0;
00012 static uint8_t *obs_token_ptr = NULL;
00013 static uint8_t obs_token_len = 0;
00014 static char temp_val[1];
00015 
00016 int result = 1;
00017 
00018 extern Serial pc;
00019 
00020 int setResult(int presult)
00021 {
00022     result = presult;
00023     return result;
00024 }
00025 
00026 /* Only GET method allowed */
00027 static uint8_t firmware_resource_result_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto)
00028 {
00029     sn_coap_hdr_s *coap_res_ptr = 0;
00030     char firmware_upd_result[16];
00031 
00032     pc.printf("firmware result callback\r\n");
00033 
00034     if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
00035     {
00036         coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
00037   
00038         sprintf(firmware_upd_result, "%d", result);
00039 
00040         coap_res_ptr->payload_len = strlen(firmware_upd_result);
00041         coap_res_ptr->payload_ptr = (uint8_t*)firmware_upd_result;
00042     }
00043 
00044     if(received_coap_ptr->token_ptr)
00045     {
00046         pc.printf("Token included\r\n");
00047         if(obs_token_ptr)
00048         {
00049             free(obs_token_ptr);
00050             obs_token_ptr = 0;
00051         }
00052         obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
00053         if(obs_token_ptr)
00054         {
00055             memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len);
00056             obs_token_len = received_coap_ptr->token_len;
00057         }
00058     }
00059 
00060     if(received_coap_ptr->options_list_ptr->observe)
00061     {
00062         coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
00063         memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
00064         coap_res_ptr->options_list_ptr->observe_ptr = &obs_number;
00065         coap_res_ptr->options_list_ptr->observe_len = 1;
00066         obs_number++;
00067     }
00068 
00069     sn_nsdl_send_coap_message(address, coap_res_ptr);
00070 
00071     coap_res_ptr->options_list_ptr->observe_ptr = 0;
00072     sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
00073     return 0;
00074 }
00075 
00076 
00077 int send_firmware_result_observation(int value)
00078 {
00079     sprintf(temp_val,"%d" ,value);
00080     obs_number++;
00081     if (obs_token_ptr != NULL)
00082     {
00083         if(sn_nsdl_send_observation_notification(obs_token_ptr, obs_token_len, (uint8_t*)temp_val, 1, &obs_number, 1, COAP_MSG_TYPE_NON_CONFIRMABLE, 0) == 0)
00084             pc.printf("Observation firmware result sending failed\r\n");
00085         else
00086             pc.printf("Observation, firmware result = %s observer number: %d\r\n", temp_val, obs_number);
00087     }
00088     else
00089         pc.printf("Observation firmware result sending failed: null pointer\r\n");    
00090     return 0;
00091 }
00092 
00093 int create_firmware_result_resource(sn_nsdl_resource_info_s *resource_ptr)
00094 {
00095     nsdl_create_dynamic_resource(resource_ptr, sizeof(FIRMWARE_UPD_RESULT_RES_ID)-1, (uint8_t*) FIRMWARE_UPD_RESULT_RES_ID, 0, 0, 0, &firmware_resource_result_cb, SN_GRS_GET_ALLOWED | SN_GRS_POST_ALLOWED);
00096     return 0;
00097 }
00098