Updated Wiflash
Dependents: wiflash-demo-blinkk wiflashdemo
wiflash.cpp
00001 #include "wiflash.h" 00002 #include "rtos.h" 00003 #include <fstream> 00004 #include <stdio.h> 00005 #include <string> 00006 #define IAP_LOCATION 0x1FFF1FF1 00007 #define START_THREAD 1 00008 extern "C" void mbed_reset(); 00009 00010 Serial pc(USBTX,USBRX); 00011 typedef void (*IAP)(unsigned long [], unsigned long[] ); 00012 IAP iap_entry = (IAP) IAP_LOCATION; 00013 Mutex stdio_mutex; 00014 Thread *t; 00015 00016 void listdir(void) { 00017 DIR *d; 00018 struct dirent *p; 00019 string l; 00020 string full_file_name; 00021 string test1 = "_LPC1768.BIN"; 00022 string test2 = "MBED.HTM"; 00023 string test3 = "WIFLASH.TXT"; 00024 d = opendir("/local"); 00025 int ret; 00026 if (d != NULL) { 00027 while ((p = readdir(d)) != NULL) { 00028 l = string(p->d_name); 00029 00030 if(l != test1 && l != test2 && l != test3){ 00031 full_file_name = string("/local/") + l; 00032 ret = remove(full_file_name.c_str()); 00033 if(ret == 0) 00034 { 00035 printf("File deleted successfully\n"); 00036 } 00037 else 00038 { 00039 printf("Error: unable to delete the file\n"); 00040 } 00041 } 00042 } 00043 } else { 00044 printf("Could not open directory!\n"); 00045 } 00046 closedir(d); 00047 mbed_reset(); 00048 } 00049 00050 Wiflash::Wiflash() : local("local"){ 00051 00052 } 00053 00054 string get_serial_number(){ 00055 unsigned long command[5] = {0,0,0,0,0}; 00056 unsigned long result[5] = {0,0,0,0,0}; 00057 command[0] = 58; // read device serial number 00058 iap_entry(command, result); 00059 string output; 00060 stringstream out; 00061 if (result[0] == 0) { 00062 for(int i = 1; i < 5; i++) { 00063 out << result[i]; 00064 } 00065 } else { 00066 printf("Status error!\r\n"); 00067 } 00068 output = out.str(); 00069 return output; 00070 00071 } 00072 00073 00074 00075 void check_for_new_firmware(void const *args) { 00076 00077 while (true) { 00078 TCPSocketConnection sock; 00079 sock.connect("104.236.198.189", 80); 00080 stdio_mutex.lock(); 00081 pc.printf("I am here\n"); 00082 char url_buffer[100]; 00083 pc.printf("I am here 2\n"); 00084 sprintf (url_buffer, "GET /firmware_check/%s.json HTTP/1.0\n\n", get_serial_number().c_str()); 00085 pc.printf(url_buffer); 00086 sock.send_all(url_buffer, sizeof(url_buffer)-1); 00087 char buffer[600]; 00088 int ret; 00089 int total_chars = 0; 00090 while (true) { 00091 ret = sock.receive(buffer, sizeof(buffer)-1); 00092 if (ret <= 0) 00093 break; 00094 buffer[ret] = '\0'; 00095 if(ret == 25){ 00096 pc.printf("Received the timestamp %s\n", buffer); 00097 pc.printf("%d\n",ret); 00098 FILE *ptr = fopen("/local/wiflash.txt", "r"); 00099 char str[30]; 00100 int i; 00101 i = fread(str, 1,30,ptr); 00102 str[i] = '\0'; 00103 fclose(ptr); 00104 string response = string(buffer); 00105 string file_data = string(str); 00106 pc.printf("File timestamp %s\n",str); 00107 pc.printf("file data length %d\n",file_data.length()); 00108 pc.printf("response data length %d\n",response.length()); 00109 size_t found = response.find(file_data); 00110 if(file_data!= response){ 00111 ptr = fopen("/local/wiflash.txt", "w"); 00112 pc.printf("Received the timestamp %s\n", buffer); 00113 fprintf(ptr,buffer); 00114 fclose(ptr); 00115 pc.printf("I am inside socket\n"); 00116 TCPSocketConnection sock2; 00117 sock.close(); 00118 sock2.connect("104.236.198.189", 80); 00119 char url2_buffer[100]; 00120 sprintf (url2_buffer, "GET /devices/%s/download.json HTTP/1.0\n\n", get_serial_number().c_str()); 00121 sock2.send_all(url2_buffer, sizeof(url_buffer)-1); 00122 int ret2; 00123 FILE* pFile; 00124 pFile = fopen("/local/_LPC1768.bin", "wb"); 00125 if(pFile){ 00126 pc.printf("File already exists\n"); 00127 pc.printf("Deleting file\n"); 00128 ret = remove("/local/_LPC1768.bin"); 00129 if(ret == 0) 00130 { 00131 pc.printf("File deleted successfully\n"); 00132 pFile = fopen("/local/_tmp.bin", "wb"); 00133 } 00134 } 00135 pFile = fopen("/local/_LPC1768.bin", "wb"); 00136 pc.printf("Writing a new empty _LPC1768.bin file\n"); 00137 int counter = 0; 00138 char test[] = "\n\nHello\n\n"; 00139 memset( buffer, '\0', sizeof(char)*600 ); 00140 string k; 00141 string phusion_passenger = "X-Powered-By: Phusion Passenger 5.0.6"; 00142 while (true) { 00143 counter += 1; 00144 ret2 = sock2.receive(buffer, sizeof(buffer)-1); 00145 if (ret2 <= 0) 00146 break; 00147 buffer[ret2] = '\0'; 00148 k = string(buffer); 00149 size_t found = k.find(phusion_passenger); 00150 if (found == string::npos){ 00151 printf("Download data\n"); 00152 total_chars += ret2; 00153 fwrite (buffer , sizeof(char), ret2, pFile); 00154 } 00155 memset( buffer, '\0', sizeof(char)*600 ); 00156 } 00157 fclose(pFile); 00158 pc.printf("Total downloade chars %d\n",total_chars); 00159 sock2.close(); 00160 listdir(); 00161 t->terminate(); 00162 } 00163 } 00164 } 00165 wait(20); 00166 stdio_mutex.unlock(); 00167 } 00168 } 00169 00170 00171 00172 bool Wiflash::setUp(){ 00173 int ret; 00174 stdio_mutex.lock(); 00175 file = fopen("/local/wiflash.txt", "r"); 00176 if(file){ 00177 fclose(file); 00178 } else { 00179 file = fopen("/local/wiflash.txt", "w"); 00180 fprintf(file, "Hello World!"); 00181 fclose(file); 00182 } 00183 file = fopen("/local/_LPC1768.bin", "r"); 00184 if(file){ 00185 fclose(file); 00186 } else { 00187 file = fopen("/local/_LPC1768.bin", "w"); 00188 fclose(file); 00189 } 00190 stdio_mutex.unlock(); 00191 eth.init(); //Use DHCP 00192 eth.connect(); 00193 printf("IP Address is %s\n", eth.getIPAddress()); 00194 t = new Thread(check_for_new_firmware); 00195 return true; 00196 }
Generated on Thu Jul 21 2022 01:36:53 by 1.7.2