microServiceBus.com is an integration platform for IoT and enterprise applications. This platform lets you expose microservices from small devices and large systems using a hosting infrastructure. These host can run on both Linux and Windows using components built either natively or using node.js.
Dependencies: C12832 EthernetInterface MbedJSONValue WebSocketClient mbed-rtos mbed
Revision 0:e542df4c4901, committed 2016-08-25
- Comitter:
- wmmihaa
- Date:
- Thu Aug 25 13:53:00 2016 +0000
- Commit message:
- Created
Changed in this revision
diff -r 000000000000 -r e542df4c4901 C12832.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/C12832.lib Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/askksa12543/code/C12832/#990d5eec2ef6
diff -r 000000000000 -r e542df4c4901 EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#4d7bff17a592
diff -r 000000000000 -r e542df4c4901 MbedJSONValue.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 000000000000 -r e542df4c4901 WebSocketClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/wmmihaa/code/WebSocketClient/#b3925e750b5f
diff -r 000000000000 -r e542df4c4901 bootloader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bootloader.h Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,136 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "rtos.h" + +#define START_ADDRESS 0x80000 +#define NUM_SECTORS 120 +#define SECTOR_SIZE 4096 +#define BUFFER_SIZE 1024 + + +/*************************************************************/ +// Bootloader functions +/*************************************************************/ +int *(*program_flash_boot)(int, char*, unsigned int) = (int *(*)(int, char*, unsigned int))0x795C9; +int *(*erase_sector_boot)(int) = (int *(*)(int))0x79465; +void *(*bootloader)(int) = (void *(*)(int))0x79121; + + +/*************************************************************/ +// Retrieving binary from server +/*************************************************************/ +void write_flash(void) +{ + + printf("Erasing flash!\r\n"); + for (int i = 0; i < NUM_SECTORS; i++) + erase_sector_boot(START_ADDRESS + i * SECTOR_SIZE); + + printf("Connecting ethernet\r\n"); + EthernetInterface eth; + //eth.init(); //Use DHCP + if(eth.connect() != 0){ + printf("Unable to connect"); + } + + printf("IP Address is %s\r\n", eth.getIPAddress()); + + + TCPSocketConnection sock; + + //----------------YOUR SERVER DETAILS-----------------------// + sock.connect("192.168.1.64", 80); + char http_cmd[] = "GET /api/mbed HTTP/1.1\r\nHost: 192.168.1.64\r\n\r\n"; + //----------------YOUR SERVER DETAILS-----------------------// + + sock.send_all(http_cmd, sizeof(http_cmd)-1); + + char buffer[BUFFER_SIZE]; + int received; + int binsize; + int bufcount; + int remaining; + int count = 0; + + //Receive first packet + received = sock.receive(buffer, sizeof(buffer)); + if (received <= 0) { + printf("No data received from server\r\n"); + while(1); + } + + //Search for "Content-Length", if not available, receive more until buffer is full + while(strstr(buffer, "Content-Length: ") == 0) { + if (received == sizeof(buffer)) { + printf("Could not determine size of bin file\r\n"); + while(1); + } else { + received += sock.receive(buffer + received, sizeof(buffer) - received); + } + } + //Determine size of the file + char *temp = strstr(buffer, "Content-Length: ") + 16; //'16' is size of "Content-Length: " + printf(buffer); + sscanf(temp, "%d", &binsize); + printf("Size of the binary = %d bytes\r\n", binsize); + + //Search for "\r\n\r\n" (beginning of bin file), if not available, receive more until buffer is full + while(strstr(buffer, "\r\n\r\n") == 0) { + if (received == sizeof(buffer)) { + printf("Could not find start of bin file\r\n"); + while(1); + } else { + received += sock.receive(buffer+received, sizeof(buffer) - received); + } + } + //Get pointer to begin of the file in the buffer + temp = strstr(buffer, "\r\n\r\n") + 4; //'16' is size of "\r\n\r\n" + + //See how much of the bin file we already received, and move this to the start of the buffer + bufcount = received - ((uint32_t)temp - (uint32_t)buffer); + memmove(buffer, temp, bufcount); + printf("Received %d bytes\r\n", bufcount); + + //Start receiving the remaining bin file + remaining = binsize - bufcount; + + while (remaining > 0) { + //Completely fill the buffer each time so we can easily write it to flash + while (bufcount < sizeof(buffer)) { + //Try to receive remainder of the buffer + received = sock.receive(&buffer[bufcount], sizeof(buffer)-bufcount); + //printf("Received %d\r\n", received); + printf("."); + if (received <= 0) { + printf("Error, should not happen\r\n"); + while(1); + } + + //Track how much we received and how much is left + bufcount += received; + remaining -= received; + if (remaining == 0) { + if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { + printf("Error @ 0x%X!\r\n", count); + while(1); + } + count += sizeof(buffer); + break; + } + } + //Buffer is full, program it and increase the counter (the counter is a bit redundant, we could get it from the other variables) + if (program_flash_boot(count+START_ADDRESS, buffer, sizeof(buffer)) != 0) { + printf("Error @ 0x%X!\r\n", count); + while(1); + } + count += sizeof(buffer); + bufcount = 0; + } + printf("Done\r\n"); + sock.close(); + + eth.disconnect(); + + bootloader(count); + +} \ No newline at end of file
diff -r 000000000000 -r e542df4c4901 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,125 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "Websocket.h" +#include "MbedJSONValue.h" +#include "C12832.h" +#include <string> +#include "bootloader.h" + + +C12832 lcd(D11, D13, D12, D7, D10); +Serial pc1(USBTX, USBRX); // tx, rx + +string _organizationId = "74ff8902-2057-499a-bca8-36a699c2458f"; + +int main() { + + lcd.cls(); + pc1.printf("\r\nStarting up\r\n"); + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + pc1.printf("IP Address is %s\r\n", eth.getIPAddress()); + + //Websocket ws("ws://microservicebus-northeurope-stage.azurewebsites.net/Services/WsHandler.ashx?id=42"); + Websocket ws("ws://192.168.1.64/Services/WsHandler.ashx?id=42"); + bool connected; + connected = ws.connect(); + + if(connected) + { + pc1.printf("Connected successfully\r\n"); + } + else + { + pc1.printf("Unable to connect\r\n"); + ws.close(); + exit(0); + } + + //while (!ws.connect()); + char str[10000]; + + MbedJSONValue signInJson; + signInJson["nodeName"] = eth.getMACAddress(); + signInJson["organizationId"] = _organizationId; + signInJson["machineName"] = eth.getMACAddress(); + signInJson["ip"] = eth.getIPAddress(); + + char buf[256]; + snprintf(buf, sizeof buf, "signIn::%s", signInJson.serialize()); + ws.send(buf); + + while(1){ + memset(str, 0, 10000); + wait(1.0f); + ///pc1.printf("."); + + if (ws.read(str)) { + + string msg(str); + + if (msg.find("broadcast::") == 0) + { + pc1.printf("Broadcast: "); + pc1.printf(str); + pc1.printf("\r\n"); + } + else if (msg.find("signInMessage::") == 0) + { + pc1.printf("Sign In successfully\r\n"); + } + else if (msg.find("reboot::") == 0) + { + pc1.printf("Disconnecting...\r\n"); + eth.disconnect(); + pc1.printf("\r\nCalling bootloader...\r\n"); + write_flash(); + } + else if (msg.find("errorMessage::") == 0) + { + pc1.printf("ERROR...\r\n"); + pc1.printf(str); + pc1.printf("\r\n"); + } + else if (msg.find("ping::") == 0) + { + msg.replace(0,6,""); + pc1.printf("ping from:"); + pc1.printf(msg.c_str()); + pc1.printf("\r\n"); + + MbedJSONValue pingRequestJson; + const char * json = msg.c_str(); + parse(pingRequestJson, json); + std::string connectionid; + connectionid = pingRequestJson["connectionid"].get<std::string>(); + + MbedJSONValue pingResponseJson; + pingResponseJson["nodeName"] = eth.getMACAddress(); + pingResponseJson["organizationId"] = _organizationId; + pingResponseJson["connectionid"] = connectionid; + pingResponseJson["status"] = "online"; + + snprintf(buf, sizeof buf, "pingResponse::%s", pingResponseJson.serialize()); + ws.send(buf); + } + } + + } + ws.close(); + + // lcd.printf("Signing out..."); +} +char* appendCharToCharArray(char* array, char a) +{ + size_t len = strlen(array); + + char* ret = new char[len+2]; + + strcpy(ret, array); + ret[len] = a; + ret[len+1] = '\0'; + + return ret; +} \ No newline at end of file
diff -r 000000000000 -r e542df4c4901 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#bdd541595fc5
diff -r 000000000000 -r e542df4c4901 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Aug 25 13:53:00 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/082adc85693f \ No newline at end of file