Wirelessly programming mbed through Ethernet

Committer:
adhithyan15
Date:
Thu Apr 30 23:57:41 2015 +0000
Revision:
0:6b372eb53d67
Initial Commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adhithyan15 0:6b372eb53d67 1 #include "wiflash.h"
adhithyan15 0:6b372eb53d67 2 #include "rtos.h"
adhithyan15 0:6b372eb53d67 3 #include <fstream>
adhithyan15 0:6b372eb53d67 4 #include <stdio.h>
adhithyan15 0:6b372eb53d67 5 #include <string>
adhithyan15 0:6b372eb53d67 6 #define IAP_LOCATION 0x1FFF1FF1
adhithyan15 0:6b372eb53d67 7 #define START_THREAD 1
adhithyan15 0:6b372eb53d67 8 extern "C" void mbed_reset();
adhithyan15 0:6b372eb53d67 9
adhithyan15 0:6b372eb53d67 10 Serial pc(USBTX,USBRX);
adhithyan15 0:6b372eb53d67 11 typedef void (*IAP)(unsigned long [], unsigned long[] );
adhithyan15 0:6b372eb53d67 12 IAP iap_entry = (IAP) IAP_LOCATION;
adhithyan15 0:6b372eb53d67 13 Mutex stdio_mutex;
adhithyan15 0:6b372eb53d67 14 Thread *t;
adhithyan15 0:6b372eb53d67 15
adhithyan15 0:6b372eb53d67 16 void listdir(void) {
adhithyan15 0:6b372eb53d67 17 DIR *d;
adhithyan15 0:6b372eb53d67 18 struct dirent *p;
adhithyan15 0:6b372eb53d67 19 string l;
adhithyan15 0:6b372eb53d67 20 string full_file_name;
adhithyan15 0:6b372eb53d67 21 string test1 = "_LPC1768.BIN";
adhithyan15 0:6b372eb53d67 22 string test2 = "MBED.HTM";
adhithyan15 0:6b372eb53d67 23 string test3 = "WIFLASH.TXT";
adhithyan15 0:6b372eb53d67 24 d = opendir("/local");
adhithyan15 0:6b372eb53d67 25 int ret;
adhithyan15 0:6b372eb53d67 26 if (d != NULL) {
adhithyan15 0:6b372eb53d67 27 while ((p = readdir(d)) != NULL) {
adhithyan15 0:6b372eb53d67 28 l = string(p->d_name);
adhithyan15 0:6b372eb53d67 29
adhithyan15 0:6b372eb53d67 30 if(l != test1 && l != test2 && l != test3){
adhithyan15 0:6b372eb53d67 31 full_file_name = string("/local/") + l;
adhithyan15 0:6b372eb53d67 32 ret = remove(full_file_name.c_str());
adhithyan15 0:6b372eb53d67 33 if(ret == 0)
adhithyan15 0:6b372eb53d67 34 {
adhithyan15 0:6b372eb53d67 35 printf("File deleted successfully\n");
adhithyan15 0:6b372eb53d67 36 }
adhithyan15 0:6b372eb53d67 37 else
adhithyan15 0:6b372eb53d67 38 {
adhithyan15 0:6b372eb53d67 39 printf("Error: unable to delete the file\n");
adhithyan15 0:6b372eb53d67 40 }
adhithyan15 0:6b372eb53d67 41 }
adhithyan15 0:6b372eb53d67 42 }
adhithyan15 0:6b372eb53d67 43 } else {
adhithyan15 0:6b372eb53d67 44 printf("Could not open directory!\n");
adhithyan15 0:6b372eb53d67 45 }
adhithyan15 0:6b372eb53d67 46 closedir(d);
adhithyan15 0:6b372eb53d67 47 mbed_reset();
adhithyan15 0:6b372eb53d67 48 }
adhithyan15 0:6b372eb53d67 49
adhithyan15 0:6b372eb53d67 50 Wiflash::Wiflash() : local("local"){
adhithyan15 0:6b372eb53d67 51
adhithyan15 0:6b372eb53d67 52 }
adhithyan15 0:6b372eb53d67 53
adhithyan15 0:6b372eb53d67 54 string get_serial_number(){
adhithyan15 0:6b372eb53d67 55 unsigned long command[5] = {0,0,0,0,0};
adhithyan15 0:6b372eb53d67 56 unsigned long result[5] = {0,0,0,0,0};
adhithyan15 0:6b372eb53d67 57 command[0] = 58; // read device serial number
adhithyan15 0:6b372eb53d67 58 iap_entry(command, result);
adhithyan15 0:6b372eb53d67 59 string output;
adhithyan15 0:6b372eb53d67 60 stringstream out;
adhithyan15 0:6b372eb53d67 61 if (result[0] == 0) {
adhithyan15 0:6b372eb53d67 62 for(int i = 1; i < 5; i++) {
adhithyan15 0:6b372eb53d67 63 out << result[i];
adhithyan15 0:6b372eb53d67 64 }
adhithyan15 0:6b372eb53d67 65 } else {
adhithyan15 0:6b372eb53d67 66 printf("Status error!\r\n");
adhithyan15 0:6b372eb53d67 67 }
adhithyan15 0:6b372eb53d67 68 output = out.str();
adhithyan15 0:6b372eb53d67 69 return output;
adhithyan15 0:6b372eb53d67 70
adhithyan15 0:6b372eb53d67 71 }
adhithyan15 0:6b372eb53d67 72
adhithyan15 0:6b372eb53d67 73
adhithyan15 0:6b372eb53d67 74
adhithyan15 0:6b372eb53d67 75 void check_for_new_firmware(void const *args) {
adhithyan15 0:6b372eb53d67 76
adhithyan15 0:6b372eb53d67 77 while (true) {
adhithyan15 0:6b372eb53d67 78 TCPSocketConnection sock;
adhithyan15 0:6b372eb53d67 79 sock.connect("104.236.198.189", 80);
adhithyan15 0:6b372eb53d67 80 stdio_mutex.lock();
adhithyan15 0:6b372eb53d67 81 pc.printf("I am here\n");
adhithyan15 0:6b372eb53d67 82 char url_buffer[100];
adhithyan15 0:6b372eb53d67 83 pc.printf("I am here 2\n");
adhithyan15 0:6b372eb53d67 84 sprintf (url_buffer, "GET /firmware_check/%s.json HTTP/1.0\n\n", get_serial_number().c_str());
adhithyan15 0:6b372eb53d67 85 pc.printf(url_buffer);
adhithyan15 0:6b372eb53d67 86 sock.send_all(url_buffer, sizeof(url_buffer)-1);
adhithyan15 0:6b372eb53d67 87 char buffer[600];
adhithyan15 0:6b372eb53d67 88 int ret;
adhithyan15 0:6b372eb53d67 89 int total_chars = 0;
adhithyan15 0:6b372eb53d67 90 while (true) {
adhithyan15 0:6b372eb53d67 91 ret = sock.receive(buffer, sizeof(buffer)-1);
adhithyan15 0:6b372eb53d67 92 if (ret <= 0)
adhithyan15 0:6b372eb53d67 93 break;
adhithyan15 0:6b372eb53d67 94 buffer[ret] = '\0';
adhithyan15 0:6b372eb53d67 95 if(ret == 25){
adhithyan15 0:6b372eb53d67 96 pc.printf("Received the timestamp %s\n", buffer);
adhithyan15 0:6b372eb53d67 97 pc.printf("%d\n",ret);
adhithyan15 0:6b372eb53d67 98 FILE *ptr = fopen("/local/wiflash.txt", "r");
adhithyan15 0:6b372eb53d67 99 char str[30];
adhithyan15 0:6b372eb53d67 100 int i;
adhithyan15 0:6b372eb53d67 101 i = fread(str, 1,30,ptr);
adhithyan15 0:6b372eb53d67 102 str[i] = '\0';
adhithyan15 0:6b372eb53d67 103 fclose(ptr);
adhithyan15 0:6b372eb53d67 104 string response = string(buffer);
adhithyan15 0:6b372eb53d67 105 string file_data = string(str);
adhithyan15 0:6b372eb53d67 106 pc.printf("File timestamp %s\n",str);
adhithyan15 0:6b372eb53d67 107 pc.printf("file data length %d\n",file_data.length());
adhithyan15 0:6b372eb53d67 108 pc.printf("response data length %d\n",response.length());
adhithyan15 0:6b372eb53d67 109 size_t found = response.find(file_data);
adhithyan15 0:6b372eb53d67 110 if(file_data!= response){
adhithyan15 0:6b372eb53d67 111 ptr = fopen("/local/wiflash.txt", "w");
adhithyan15 0:6b372eb53d67 112 pc.printf("Received the timestamp %s\n", buffer);
adhithyan15 0:6b372eb53d67 113 fprintf(ptr,buffer);
adhithyan15 0:6b372eb53d67 114 fclose(ptr);
adhithyan15 0:6b372eb53d67 115 pc.printf("I am inside socket\n");
adhithyan15 0:6b372eb53d67 116 TCPSocketConnection sock2;
adhithyan15 0:6b372eb53d67 117 sock2.connect("104.236.198.189", 80);
adhithyan15 0:6b372eb53d67 118 char url2_buffer[100];
adhithyan15 0:6b372eb53d67 119 sprintf (url2_buffer, "GET /devices/%s/download.json HTTP/1.0\n\n", get_serial_number().c_str());
adhithyan15 0:6b372eb53d67 120 sock2.send_all(url2_buffer, sizeof(url_buffer)-1);
adhithyan15 0:6b372eb53d67 121 int ret2;
adhithyan15 0:6b372eb53d67 122 FILE* pFile;
adhithyan15 0:6b372eb53d67 123 pFile = fopen("/local/_LPC1768.bin", "wb");
adhithyan15 0:6b372eb53d67 124 int counter = 0;
adhithyan15 0:6b372eb53d67 125 char test[] = "\n\nHello\n\n";
adhithyan15 0:6b372eb53d67 126 memset( buffer, '\0', sizeof(char)*600 );
adhithyan15 0:6b372eb53d67 127 string k;
adhithyan15 0:6b372eb53d67 128 string phusion_passenger = "X-Powered-By: Phusion Passenger 5.0.6";
adhithyan15 0:6b372eb53d67 129 while (true) {
adhithyan15 0:6b372eb53d67 130 counter += 1;
adhithyan15 0:6b372eb53d67 131 ret2 = sock2.receive(buffer, sizeof(buffer)-1);
adhithyan15 0:6b372eb53d67 132 if (ret2 <= 0)
adhithyan15 0:6b372eb53d67 133 break;
adhithyan15 0:6b372eb53d67 134 buffer[ret2] = '\0';
adhithyan15 0:6b372eb53d67 135 k = string(buffer);
adhithyan15 0:6b372eb53d67 136 size_t found = k.find(phusion_passenger);
adhithyan15 0:6b372eb53d67 137 if (found == string::npos){
adhithyan15 0:6b372eb53d67 138 printf("Download data\n");
adhithyan15 0:6b372eb53d67 139 total_chars += ret2;
adhithyan15 0:6b372eb53d67 140 fwrite (buffer , sizeof(char), ret2, pFile);
adhithyan15 0:6b372eb53d67 141 }
adhithyan15 0:6b372eb53d67 142 memset( buffer, '\0', sizeof(char)*600 );
adhithyan15 0:6b372eb53d67 143 }
adhithyan15 0:6b372eb53d67 144 fclose(pFile);
adhithyan15 0:6b372eb53d67 145 pc.printf("Total downloade chars %d\n",total_chars);
adhithyan15 0:6b372eb53d67 146 sock2.close();
adhithyan15 0:6b372eb53d67 147 listdir();
adhithyan15 0:6b372eb53d67 148 t->terminate();
adhithyan15 0:6b372eb53d67 149 }
adhithyan15 0:6b372eb53d67 150 }
adhithyan15 0:6b372eb53d67 151 }
adhithyan15 0:6b372eb53d67 152 sock.close();
adhithyan15 0:6b372eb53d67 153 wait(20);
adhithyan15 0:6b372eb53d67 154 stdio_mutex.unlock();
adhithyan15 0:6b372eb53d67 155 }
adhithyan15 0:6b372eb53d67 156 }
adhithyan15 0:6b372eb53d67 157
adhithyan15 0:6b372eb53d67 158
adhithyan15 0:6b372eb53d67 159
adhithyan15 0:6b372eb53d67 160 bool Wiflash::setUp(){
adhithyan15 0:6b372eb53d67 161 int ret;
adhithyan15 0:6b372eb53d67 162 stdio_mutex.lock();
adhithyan15 0:6b372eb53d67 163 file = fopen("/local/wiflash.txt", "r");
adhithyan15 0:6b372eb53d67 164 if(file){
adhithyan15 0:6b372eb53d67 165 fclose(file);
adhithyan15 0:6b372eb53d67 166 } else {
adhithyan15 0:6b372eb53d67 167 file = fopen("/local/wiflash.txt", "w");
adhithyan15 0:6b372eb53d67 168 fprintf(file, "Hello World!");
adhithyan15 0:6b372eb53d67 169 fclose(file);
adhithyan15 0:6b372eb53d67 170 }
adhithyan15 0:6b372eb53d67 171 file = fopen("/local/_LPC1768.bin", "r");
adhithyan15 0:6b372eb53d67 172 if(file){
adhithyan15 0:6b372eb53d67 173 fclose(file);
adhithyan15 0:6b372eb53d67 174 } else {
adhithyan15 0:6b372eb53d67 175 file = fopen("/local/_LPC1768.bin", "w");
adhithyan15 0:6b372eb53d67 176 fclose(file);
adhithyan15 0:6b372eb53d67 177 }
adhithyan15 0:6b372eb53d67 178 stdio_mutex.unlock();
adhithyan15 0:6b372eb53d67 179 eth.init(); //Use DHCP
adhithyan15 0:6b372eb53d67 180 eth.connect();
adhithyan15 0:6b372eb53d67 181 printf("IP Address is %s\n", eth.getIPAddress());
adhithyan15 0:6b372eb53d67 182 t = new Thread(check_for_new_firmware);
adhithyan15 0:6b372eb53d67 183 return true;
adhithyan15 0:6b372eb53d67 184 }