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
Revision 21:978281bfb26e, committed 2015-10-27
- Comitter:
- pnysten
- Date:
- Tue Oct 27 07:11:46 2015 +0000
- Parent:
- 20:84ee332ba360
- Child:
- 22:2fab757f9c2a
- Commit message:
- 1/0/8
Changed in this revision
--- a/C12832_lcd.lib Tue Apr 08 01:02:06 2014 +0000 +++ b/C12832_lcd.lib Tue Oct 27 07:11:46 2015 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a +https://mbed.org/users/dreschpe/code/C12832_lcd/#8f86576007d6
--- a/EthernetInterface.lib Tue Apr 08 01:02:06 2014 +0000 +++ b/EthernetInterface.lib Tue Oct 27 07:11:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/EthernetInterface/#6a67d2bddc7c +http://mbed.org/users/mbed_official/code/EthernetInterface/#882c47b4b7a0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetNetIf.lib Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient.lib Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/WiredHome/code/HTTPClient/#3556275bebf3
--- a/MMA7660.lib Tue Apr 08 01:02:06 2014 +0000 +++ b/MMA7660.lib Tue Oct 27 07:11:46 2015 +0000 @@ -1,1 +1,1 @@ -https://mbed.org/users/Sissors/code/MMA7660/#a8e20db7901e +https://mbed.org/users/Sissors/code/MMA7660/#36a163511e34
--- a/dbg.h Tue Apr 08 01:02:06 2014 +0000 +++ b/dbg.h Tue Oct 27 07:11:46 2015 +0000 @@ -9,6 +9,7 @@ #if (DEBUG) extern Serial pc; + #define NSDL_DEBUG(x, ...) pc.printf("[NSDL_DEBUG: %s:%d]" x "\r\n", __FILE__, __LINE__, ##__VA_ARGS__); #else #define NSDL_DEBUG(x, ...)
--- a/main.cpp Tue Apr 08 01:02:06 2014 +0000 +++ b/main.cpp Tue Oct 27 07:11:46 2015 +0000 @@ -1,3 +1,20 @@ +//Hello Pascal, + +//to be able to support MDM in a clustered environment and permanent tcp connection with devices, we had to restrict the supported lwm2m scenario’s to the queued mode only. +//This because jobs launched via the MDM console could be executed by a MDM node of the cluster that doesn’t have a tcp connection with the device. +//With the latest lwm2m plugin, it is up to the device to come to the plugin with a lwm2m Register Update on one of the node and the Job waiting in shared MDM queue will execute. + +//To show demo’s this is a bit embarrassing since updates are sent every time device lifetime expires and this could be long. + +//So what we have introduced is the ability for the lwm2m plugin to send a Registration Update Trigger to the device if it has an available connection to it. +//In a single node environment this is working fine. + +//Can you please check if this registration update trigger can be implemented in the NXP and the freescale so we could upgrade our plugin when required. +//All what is needed is to send a registration update message when then device receive an execute on resource 1/0/8. + + + + #include "mbed.h" #include "EthernetInterface.h" #include "C12832_lcd.h" @@ -10,7 +27,18 @@ #include "relay.h" #include "rgb.h" +#include "firmware.h" +#include "firmware_result.h" +#include "firmware_status.h" + +#include "register_upd_trigger.h" + static C12832_LCD lcd; +static PwmOut led1(LED1); +static PwmOut led2(LED2); +static PwmOut led3(LED3); +static PwmOut led4(LED4); + Serial pc(USBTX, USBRX); // tx, rx // **************************************************************************** @@ -28,14 +56,18 @@ #define MASK "255.255.255.0" #define GW "10.45.0.1" -// NSP configuration +// UDM configuration /* Change this IP address to that of your NanoService Platform installation */ -static const char* NSP_ADDRESS = "137.135.13.28"; /* demo NSP, web interface at http://red-hat-summit.cloudapp.net*/ -static const int NSP_PORT = 5683; -char endpoint_name[20] = "mbed-ethernet-"; +//static const char* UDM_ADDRESS = "137.135.13.28"; /* demo UDM, web interface at http://red-hat-summit.cloudapp.net*/ +//static const char* UDM_ADDRESS = "137.117.164.225"; /* m2mcc02 */ +static const char* UDM_ADDRESS = "68.235.31.32"; +static const int UDM_PORT = 5684; +//char endpoint_name[20] = "ntcmbed-ethernet-"; +char endpoint_name[20] = "NXP1768-"; char mbed_uid[33]; // for creating unique name for the board uint8_t ep_type[] = {"mbed_lpc1768_appboard"}; uint8_t lifetime_ptr[] = {"60"}; + static const char* FIRMWARE_VER = "18"; // Committed revision number char* mac; char* ipAddr; @@ -57,7 +89,7 @@ #else eth.init(IP, MASK, GW); #endif - if(eth.connect(30000) == 0) + if(eth.connect(60000) == 0) NSDL_DEBUG("Ethernet up"); mac = eth.getMACAddress(); @@ -68,29 +100,33 @@ mbed_interface_uid(mbed_uid); mbed_uid[32] = '\0'; + NSDL_DEBUG("Full interface uid=%s", mbed_uid); strncat(endpoint_name, mbed_uid + 27, 20 - strlen(endpoint_name)); + +// EP_NAME = (char*) malloc(strlen(endpoint_name)+1); +// strcpy(EP_NAME, endpoint_name); lcd.locate(0,11); lcd.printf("IP:%s", eth.getIPAddress()); -} + } // **************************************************************************** -// NSP initialization +// UDM initialization UDPSocket server; -Endpoint nsp; +Endpoint udm; -static void nsp_init() +static void udm_init() { server.init(); - server.bind(NSP_PORT); + server.bind(UDM_PORT); - nsp.set_address(NSP_ADDRESS, NSP_PORT); + udm.set_address(UDM_ADDRESS, UDM_PORT); NSDL_DEBUG("name: %s", endpoint_name); - NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); + NSDL_DEBUG("UDM=%s - port %d\n", UDM_ADDRESS, UDM_PORT); lcd.locate(0,22); lcd.printf("EP:%s\n", endpoint_name); @@ -140,7 +176,13 @@ create_light_resource(resource_ptr); create_gps_resource(resource_ptr); create_relay_resource(resource_ptr); - + create_register_upd_trigger_resource(resource_ptr); + + // Dynamic Firmware resources + create_firmware_resource(resource_ptr); + create_firmware_result_resource(resource_ptr); + create_firmware_status_resource(resource_ptr); + #ifdef USE_RGBLED NSDL_DEBUG("Enabling RGB LED due to USE_RGBLED=%d\n", USE_RGBLED); create_rgb_resource(resource_ptr); @@ -148,12 +190,12 @@ NSDL_DEBUG("Skipped RGB LED resource, change USE_RGBLED to 1 in main.cpp to test"); #endif - /* Register with NSP */ + /* Register with UDM */ endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) - pc.printf("NSP registering failed\r\n"); + pc.printf("UDM registering failed\r\n"); else - pc.printf("NSP registering OK\r\n"); + pc.printf("UDM registering OK\r\n"); nsdl_clean_register_endpoint(&endpoint_ptr); nsdl_free(resource_ptr->resource_parameters_ptr); @@ -166,22 +208,33 @@ int main() { +// strcpy((char*) ep_type, "mbed_lpc1768_appboard"); + strcpy((char*) lifetime_ptr, "60"); + lcd.cls(); lcd.locate(0,0); - lcd.printf("NanoService LWM2M r%s", FIRMWARE_VER); + lcd.printf("NanoService LWM2M v%s", FIRMWARE_VER); NSDL_DEBUG("NanoService LWM2M Demo for LPC1768 App Board\n"); + NSDL_DEBUG("NanoService LWM2M version %s\n", FIRMWARE_VER); // Initialize Ethernet interface first ethernet_init(); - // Initialize NSP node - nsp_init(); + // Initialize UDM node + udm_init(); // Initialize NSDL stack nsdl_init(); // Create NSDL resources create_resources(); + + //Version 18 + led3.write(0.1); + + //Version 19 +// led2.write(0.5); +// led3.write(0.5); // Run the NSDL event loop (never returns) nsdl_event_loop();
--- a/mbed-rtos.lib Tue Apr 08 01:02:06 2014 +0000 +++ b/mbed-rtos.lib Tue Oct 27 07:11:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#53e6cccd8782 +http://mbed.org/users/mbed_official/code/mbed-rtos/#3516160e016b
--- a/mbed.bld Tue Apr 08 01:02:06 2014 +0000 +++ b/mbed.bld Tue Oct 27 07:11:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/dbbf35b96557 \ No newline at end of file
--- a/nsdl_lib.lib Tue Apr 08 01:02:06 2014 +0000 +++ b/nsdl_lib.lib Tue Oct 27 07:11:46 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/teams/Sensinode/code/nsdl_lib/#050d0f54a0dc +http://mbed.org/teams/Sensinode/code/nsdl_lib/#8a47d4c71067
--- a/nsdl_support.cpp Tue Apr 08 01:02:06 2014 +0000 +++ b/nsdl_support.cpp Tue Oct 27 07:11:46 2015 +0000 @@ -2,19 +2,18 @@ #include "mbed.h" #include "nsdl_support.h" -#include "mbed.h" #include "rtos.h" #include "EthernetInterface.h" extern Serial pc; extern EthernetInterface eth; -extern Endpoint nsp; +extern Endpoint udm; extern UDPSocket server; extern char endpoint_name[16]; extern uint8_t ep_type[]; extern uint8_t lifetime_ptr[]; -/* The number of seconds between NSP registration messages */ +/* The number of seconds between UDM registration messages */ #define RD_UPDATE_PERIOD 60 void *nsdl_alloc(uint16_t size) @@ -91,7 +90,7 @@ { pc.printf("TX callback!\n\rSending %d bytes\r\n", data_len); - if(server.sendTo(nsp, (char*)data_ptr, data_len) != data_len) + if(server.sendTo(udm, (char*)data_ptr, data_len) != data_len) pc.printf("sending failed\n\r"); return 1; @@ -112,16 +111,16 @@ wait(RD_UPDATE_PERIOD); endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) - pc.printf("NSP re-registering failed\r\n"); + pc.printf("UDM re-registering failed\r\n"); else - pc.printf("NSP re-registering OK\r\n"); + pc.printf("UDM re-registering OK\r\n"); nsdl_clean_register_endpoint(&endpoint_ptr); } } void nsdl_init() { - uint8_t nsp_addr[4]; + uint8_t udm_addr[4]; sn_nsdl_mem_s memory_cbs; /* Initialize libNsdl */ @@ -132,8 +131,8 @@ else pc.printf("libNsdl init done\r\n"); - /* Set nsp address for library */ - set_NSP_address(nsp_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); + /* Set udm address for library */ + set_NSP_address(udm_addr, 5683, SN_NSDL_ADDRESS_TYPE_IPV4); } void nsdl_event_loop()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/firmware.cpp Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,99 @@ +// Light resource implementation + +#include "mbed.h" +#include "nsdl_support.h" +#include "EthernetInterface.h" +#include "firmware.h" +#include "firmware_status.h" +#include "firmware_result.h" +#include "HTTPClient.h" +#include "C12832_lcd.h" + +#define FIRMWARE_WRITE_RES_ID "5/0/1" +//5/0/1 should be used instead + +static C12832_LCD lcd; +extern Serial pc; +extern "C" void mbed_reset(); + +LocalFileSystem local("local"); +HTTPClient client; + +char *remove_ext(char *dst, const char *filename) { + size_t len = strlen(filename); + memcpy(dst, filename, len-3); + dst[len - 3] = 0; + return dst; +} + +/* Only PUT method allowed */ +static uint8_t firmware_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + pc.printf("Firmware updating\r\n"); + lcd.locate(0,0); + lcd.printf("Firmware Upgrade...\n"); + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + setResult(0); + pc.printf("Firmware URL length: %d\r\n",received_coap_ptr->payload_len); + char firmware_url[200]; + memcpy(firmware_url, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len); + firmware_url[received_coap_ptr->payload_len]='\0'; + + pc.printf("Firmware URL: %s\r\n",firmware_url); + lcd.locate(0,11); + lcd.printf("Downloading new Firmware...\n"); + + HTTPFile file("/local/out.b__"); + pc.printf("INFO: Getting from URL '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + + int r = client.get(firmware_url, &file); + if (r != HTTP_OK) { + if (r == HTTP_PARSE) + pc.printf("ERR Parse URL: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_DNS) + pc.printf("ERR DNS: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_NOTFOUND) + pc.printf("ERR 404 Not Found: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_REFUSED) + pc.printf("ERR 403 Refused: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_ERROR) + pc.printf("ERR Other HTTP Error: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_TIMEOUT) + pc.printf("ERR Timeout: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_CONN) + pc.printf("ERR Connection: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else if (r == HTTP_CLOSED) + pc.printf("ERR Connection Closed: Fetch '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + else + pc.printf("ERR Other Error %d: Fetch '%s' to '%s'.\r\n", r, firmware_url, "/local/out.b__"); + pc.printf("ERR : Cleaning temp file.\r\n"); + remove("/local/out.b__"); + setResult(2); + // send_firmware_result_observation(2); + lcd.locate(0,22); + lcd.printf("Failed...\n"); + return 0; + } + pc.printf("INFO: Fetched '%s' to '%s'.\r\n", firmware_url, "/local/out.b__"); + + send_firmware_result_observation(1); + lcd.locate(0,22); + lcd.printf("Success...\n"); + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_firmware_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(FIRMWARE_WRITE_RES_ID)-1, (uint8_t*)FIRMWARE_WRITE_RES_ID, 0, 0, 0, &firmware_resource_cb, SN_GRS_PUT_ALLOWED); + return 0; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/firmware.h Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,10 @@ +// Firmware resource implementation + +#ifndef FIRMWARE_H +#define FIRMWARE_H + +#include "nsdl_support.h" + +int create_firmware_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/firmware_result.cpp Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,98 @@ +// Firmware result resource implementation + +#include "mbed.h" +#include "rtos.h" +#include "LM75B.h" +#include "nsdl_support.h" +#include "firmware_result.h" + +#define FIRMWARE_UPD_RESULT_RES_ID "5/0/5" + +static uint8_t obs_number = 0; +static uint8_t *obs_token_ptr = NULL; +static uint8_t obs_token_len = 0; +static char temp_val[1]; + +int result = 1; + +extern Serial pc; + +int setResult(int presult) +{ + result = presult; + return result; +} + +/* Only GET method allowed */ +static uint8_t firmware_resource_result_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + char firmware_upd_result[16]; + + pc.printf("firmware result callback\r\n"); + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) + { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + sprintf(firmware_upd_result, "%d", result); + + coap_res_ptr->payload_len = strlen(firmware_upd_result); + coap_res_ptr->payload_ptr = (uint8_t*)firmware_upd_result; + } + + if(received_coap_ptr->token_ptr) + { + pc.printf("Token included\r\n"); + if(obs_token_ptr) + { + free(obs_token_ptr); + obs_token_ptr = 0; + } + obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len); + if(obs_token_ptr) + { + memcpy(obs_token_ptr, received_coap_ptr->token_ptr, received_coap_ptr->token_len); + obs_token_len = received_coap_ptr->token_len; + } + } + + if(received_coap_ptr->options_list_ptr->observe) + { + coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); + memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); + coap_res_ptr->options_list_ptr->observe_ptr = &obs_number; + coap_res_ptr->options_list_ptr->observe_len = 1; + obs_number++; + } + + sn_nsdl_send_coap_message(address, coap_res_ptr); + + coap_res_ptr->options_list_ptr->observe_ptr = 0; + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + + +int send_firmware_result_observation(int value) +{ + sprintf(temp_val,"%d" ,value); + obs_number++; + if (obs_token_ptr != NULL) + { + 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) + pc.printf("Observation firmware result sending failed\r\n"); + else + pc.printf("Observation, firmware result = %s observer number: %d\r\n", temp_val, obs_number); + } + else + pc.printf("Observation firmware result sending failed: null pointer\r\n"); + return 0; +} + +int create_firmware_result_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + 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); + return 0; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/firmware_result.h Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,12 @@ +// Firmware resource implementation + +#ifndef FIRMWARE_RESULT_H +#define FIRMWARE_RESULT_H + +#include "nsdl_support.h" + +int create_firmware_result_resource(sn_nsdl_resource_info_s *resource_ptr); +int send_firmware_result_observation(int value); +int setResult(int presult); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/firmware_status.cpp Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,151 @@ +// Firmware status resource implementation + +#include "mbed.h" +#include "rtos.h" +#include "LM75B.h" +#include "nsdl_support.h" +#include "firmware_status.h" +#include "firmware_result.h" + +#define FIRMWARE_UPD_STATUS_RES_ID "5/0/2" + +extern Serial pc; +extern "C" void mbed_reset(); + +/** + * Cleanup all bin files. + */ +int cleanupAllBinFiles(void) { + struct dirent *p; + DIR *dir = opendir("/local"); + if (dir == NULL) { + return -1; + } + while ((p = readdir(dir)) != NULL) { + char *str = p->d_name; + + if ((strstr(str, ".bin") != NULL) || (strstr(str, ".BIN") != NULL)) { + char buf[BUFSIZ]; + snprintf(buf, sizeof(buf) - 1, "/local/%s", str); + if (remove(buf) == 0) { + pc.printf("INFO: Deleted '%s'.\r\n", buf); + } else { + pc.printf("ERR : Delete '%s' failed.\r\n", buf); + } + } + } + closedir(dir); + return 0; +} + +/** fcopy: Copies a file + * Checks to ensure destination file was created. + * Returns -1 = error; 0 = success + */ +int fcopy (const char *src, const char *dst) { + FILE *fpsrc = fopen(src, "r"); + + if (fpsrc == NULL) + return 0; + + FILE *fpdst = fopen(dst, "w"); + + if (fpdst == NULL) + return 0; + + int ch = fgetc(fpsrc); + while (ch != EOF) { + fputc(ch, fpdst); + ch = fgetc(fpsrc); + } + fclose(fpsrc); + fclose(fpdst); + int retval = 0; + fpdst = fopen(dst, "r"); + if (fpdst == NULL) { + retval = 0; + } else { + fclose(fpdst); + retval = 1; + } + return retval; +} + +/* Only GET, POST method allowed */ +static uint8_t firmware_resource_status_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + char firmware_upd_status[16]; + + pc.printf("firmware status callback\r\n"); + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) + { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + wait(2); + FILE *fpsrc = fopen("/local/OUT.B__", "r"); + + if (fpsrc == NULL) + { + pc.printf("Tmp file not found...\r\n"); + setResult(7); + send_firmware_result_observation(7); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; + } + + if (cleanupAllBinFiles() == -1) + { + pc.printf("Cleaning files failed...\r\n"); + setResult(2); + send_firmware_result_observation(2); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; + } + + pc.printf("Copying NEW_FW...\r\n"); + if (fcopy("/local/OUT.B__","/local/NEW_FW.BIN") == 0) + { + setResult(2); + send_firmware_result_observation(2); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; + } + pc.printf("Deleting tmp file...\r\n"); + if (remove("/local/out.b__") != 0) + { + /*setResult(0); + send_firmware_result_observation(0); + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0;*/ + } + pc.printf("Resetting...\r\n"); + wait(3); + setResult(1); + mbed_reset(); + + } + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) + { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT); + + sprintf(firmware_upd_status, "%d", 1); + + coap_res_ptr->payload_len = strlen(firmware_upd_status); + coap_res_ptr->payload_ptr = (uint8_t*)firmware_upd_status; + sn_nsdl_send_coap_message(address, coap_res_ptr); + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_firmware_status_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, sizeof(FIRMWARE_UPD_STATUS_RES_ID)-1, (uint8_t*) FIRMWARE_UPD_STATUS_RES_ID, 0, 0, 0, &firmware_resource_status_cb, SN_GRS_GET_ALLOWED | SN_GRS_POST_ALLOWED); + return 0; +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/firmware_status.h Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,10 @@ +// Firmware status resource implementation + +#ifndef FIRMWARE_STATUS_H +#define FIRMWARE_STATUS_H + +#include "nsdl_support.h" + +int create_firmware_status_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/register_upd_trigger.cpp Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,52 @@ +#include "mbed.h" +#include "rtos.h" +#include "nsdl_support.h" +#include "register_upd_trigger.h" + +#define REGISTER_UPD_TRIGGER_ID "1/0/8" + +extern char endpoint_name[16]; +extern uint8_t ep_type[]; +extern uint8_t lifetime_ptr[]; +extern Serial pc; + +/* Only POST and PUT method allowed */ +static uint8_t register_upd_trigger_resource_cb(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto) +{ + sn_coap_hdr_s *coap_res_ptr = 0; + + strcpy((char*) ep_type, "mbed_lpc1768_appboard"); + strcpy((char*) lifetime_ptr, "60"); + pc.printf("Register Update Trigger callback\r\n"); + + if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) + { + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); + + sn_nsdl_update_registration(endpoint_ptr); + } + else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) + { + + coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED); + sn_nsdl_send_coap_message(address, coap_res_ptr); + + sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; + endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, (uint8_t*)ep_type, (uint8_t*)lifetime_ptr); + + sn_nsdl_update_registration(endpoint_ptr); + } + + sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr); + return 0; +} + +int create_register_upd_trigger_resource(sn_nsdl_resource_info_s *resource_ptr) +{ + nsdl_create_dynamic_resource(resource_ptr, strlen(REGISTER_UPD_TRIGGER_ID), (uint8_t*)REGISTER_UPD_TRIGGER_ID, 0, 0, 0, ®ister_upd_trigger_resource_cb, (SN_GRS_POST_ALLOWED | SN_GRS_PUT_ALLOWED)); + return 0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/resources/register_upd_trigger.h Tue Oct 27 07:11:46 2015 +0000 @@ -0,0 +1,10 @@ +// Register Update Trigger resource implementation + +#ifndef REGISTER_UPD_TRIGGER_H +#define REGISTER_UPD_TRIGGER_H + +#include "nsdl_support.h" + +int create_register_upd_trigger_resource(sn_nsdl_resource_info_s *resource_ptr); + +#endif