![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Student project by David Berlin and Boris Dogadov made for the Embedded Systems Workshop course given in Tel-Aviv University on 2010 by Sivan Toledo. Visit the project website for more details: http://davidberlin.co.il/sadna/ .
Dependencies: EthernetNetIf NTPClient_NetServices mbed HTTPServer HTTPClient CyaSSL
main.cpp@1:b05231650f32, 2011-04-25 (annotated)
- Committer:
- sivan_toledo
- Date:
- Mon Apr 25 12:31:46 2011 +0000
- Revision:
- 1:b05231650f32
- Parent:
- 0:3e7d6f496a67
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sivan_toledo | 0:3e7d6f496a67 | 1 | /* |
sivan_toledo | 0:3e7d6f496a67 | 2 | Permission is hereby granted, free of charge, to any person obtaining a copy |
sivan_toledo | 0:3e7d6f496a67 | 3 | of this software and associated documentation files (the "Software"), to deal |
sivan_toledo | 0:3e7d6f496a67 | 4 | in the Software without restriction, including without limitation the rights |
sivan_toledo | 0:3e7d6f496a67 | 5 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
sivan_toledo | 0:3e7d6f496a67 | 6 | copies of the Software, and to permit persons to whom the Software is |
sivan_toledo | 0:3e7d6f496a67 | 7 | furnished to do so, subject to the following conditions: |
sivan_toledo | 0:3e7d6f496a67 | 8 | |
sivan_toledo | 0:3e7d6f496a67 | 9 | The above copyright notice and this permission notice shall be included in |
sivan_toledo | 0:3e7d6f496a67 | 10 | all copies or substantial portions of the Software. |
sivan_toledo | 0:3e7d6f496a67 | 11 | |
sivan_toledo | 0:3e7d6f496a67 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
sivan_toledo | 0:3e7d6f496a67 | 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
sivan_toledo | 0:3e7d6f496a67 | 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
sivan_toledo | 0:3e7d6f496a67 | 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
sivan_toledo | 0:3e7d6f496a67 | 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
sivan_toledo | 0:3e7d6f496a67 | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
sivan_toledo | 0:3e7d6f496a67 | 18 | THE SOFTWARE. |
sivan_toledo | 0:3e7d6f496a67 | 19 | */ |
sivan_toledo | 0:3e7d6f496a67 | 20 | |
sivan_toledo | 0:3e7d6f496a67 | 21 | #include "mbed.h" |
sivan_toledo | 0:3e7d6f496a67 | 22 | #include "USBHost.h" |
sivan_toledo | 0:3e7d6f496a67 | 23 | #include "Utils.h" |
sivan_toledo | 0:3e7d6f496a67 | 24 | #include "UsbStorage.h" |
sivan_toledo | 0:3e7d6f496a67 | 25 | |
sivan_toledo | 0:3e7d6f496a67 | 26 | #include "EthernetNetIf.h" |
sivan_toledo | 0:3e7d6f496a67 | 27 | #include "Dropbox.h" |
sivan_toledo | 0:3e7d6f496a67 | 28 | #include "HTTPServer.h" |
sivan_toledo | 1:b05231650f32 | 29 | #include "HTTPServer.h" |
sivan_toledo | 0:3e7d6f496a67 | 30 | #include "HttpHandlerSetup.h" |
sivan_toledo | 0:3e7d6f496a67 | 31 | #include "HttpHandlerUsbBrowser.h" |
sivan_toledo | 0:3e7d6f496a67 | 32 | |
sivan_toledo | 0:3e7d6f496a67 | 33 | // USB |
sivan_toledo | 0:3e7d6f496a67 | 34 | Serial pc(USBTX, USBRX); |
sivan_toledo | 0:3e7d6f496a67 | 35 | USBFileSystem fs; |
sivan_toledo | 0:3e7d6f496a67 | 36 | int usbDevice = -1; |
sivan_toledo | 0:3e7d6f496a67 | 37 | |
sivan_toledo | 0:3e7d6f496a67 | 38 | // NET |
sivan_toledo | 0:3e7d6f496a67 | 39 | EthernetNetIf* eth; |
sivan_toledo | 0:3e7d6f496a67 | 40 | |
sivan_toledo | 0:3e7d6f496a67 | 41 | // LED |
sivan_toledo | 0:3e7d6f496a67 | 42 | DigitalOut led1(LED1); |
sivan_toledo | 0:3e7d6f496a67 | 43 | DigitalOut led2(LED2); |
sivan_toledo | 0:3e7d6f496a67 | 44 | DigitalOut led3(LED3); |
sivan_toledo | 0:3e7d6f496a67 | 45 | |
sivan_toledo | 1:b05231650f32 | 46 | // Occurs on usb flash disk connection |
sivan_toledo | 0:3e7d6f496a67 | 47 | int OnDiskInsert(int device) |
sivan_toledo | 0:3e7d6f496a67 | 48 | { |
sivan_toledo | 0:3e7d6f496a67 | 49 | printf("\r\nOnDiskInsert\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 50 | |
sivan_toledo | 0:3e7d6f496a67 | 51 | usbDevice = device; |
sivan_toledo | 0:3e7d6f496a67 | 52 | fs.SetDevice(usbDevice); |
sivan_toledo | 0:3e7d6f496a67 | 53 | |
sivan_toledo | 0:3e7d6f496a67 | 54 | return 0; |
sivan_toledo | 0:3e7d6f496a67 | 55 | } |
sivan_toledo | 0:3e7d6f496a67 | 56 | |
sivan_toledo | 1:b05231650f32 | 57 | // Setup ethernet connection |
sivan_toledo | 0:3e7d6f496a67 | 58 | bool setup_eth() |
sivan_toledo | 0:3e7d6f496a67 | 59 | { |
sivan_toledo | 0:3e7d6f496a67 | 60 | printf("\r\nSetting up...\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 61 | |
sivan_toledo | 0:3e7d6f496a67 | 62 | EthernetErr ethErr = eth->setup(); |
sivan_toledo | 0:3e7d6f496a67 | 63 | if(ethErr) |
sivan_toledo | 0:3e7d6f496a67 | 64 | { |
sivan_toledo | 0:3e7d6f496a67 | 65 | printf("Error %d in setup.\n", ethErr); |
sivan_toledo | 0:3e7d6f496a67 | 66 | return false; |
sivan_toledo | 0:3e7d6f496a67 | 67 | } |
sivan_toledo | 0:3e7d6f496a67 | 68 | printf("\r\nSetup OK\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 69 | |
sivan_toledo | 0:3e7d6f496a67 | 70 | return true; |
sivan_toledo | 0:3e7d6f496a67 | 71 | } |
sivan_toledo | 0:3e7d6f496a67 | 72 | |
sivan_toledo | 1:b05231650f32 | 73 | // Setup HttpHandlers |
sivan_toledo | 0:3e7d6f496a67 | 74 | bool setup_httpServer(HTTPServer& svr) |
sivan_toledo | 0:3e7d6f496a67 | 75 | { |
sivan_toledo | 0:3e7d6f496a67 | 76 | svr.addHandler<HttpHandlerUsbBrowser>("/UsbBrowser"); |
sivan_toledo | 0:3e7d6f496a67 | 77 | svr.addHandler<HttpHandlerSetup>("/"); |
sivan_toledo | 0:3e7d6f496a67 | 78 | svr.bind(80); |
sivan_toledo | 0:3e7d6f496a67 | 79 | |
sivan_toledo | 0:3e7d6f496a67 | 80 | printf("Http Server initialized\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 81 | |
sivan_toledo | 0:3e7d6f496a67 | 82 | return true; |
sivan_toledo | 0:3e7d6f496a67 | 83 | } |
sivan_toledo | 0:3e7d6f496a67 | 84 | |
sivan_toledo | 1:b05231650f32 | 85 | // Uploads a folder on usb flash-drive to dropbox |
sivan_toledo | 0:3e7d6f496a67 | 86 | bool upload_files(char* username, char* password) |
sivan_toledo | 0:3e7d6f496a67 | 87 | { |
sivan_toledo | 1:b05231650f32 | 88 | DIR *d = opendir("/usb/DROPBOX"); |
sivan_toledo | 0:3e7d6f496a67 | 89 | int totalFiles = -1; |
sivan_toledo | 1:b05231650f32 | 90 | |
sivan_toledo | 0:3e7d6f496a67 | 91 | if (d) |
sivan_toledo | 0:3e7d6f496a67 | 92 | { |
sivan_toledo | 0:3e7d6f496a67 | 93 | char filenameSrc[64] = "/usb/DROPBOX/"; |
sivan_toledo | 1:b05231650f32 | 94 | char filenameDst[32] = "USBSYNC_"; |
sivan_toledo | 0:3e7d6f496a67 | 95 | int c = 0; |
sivan_toledo | 0:3e7d6f496a67 | 96 | |
sivan_toledo | 0:3e7d6f496a67 | 97 | printf("Synchronize Start\n\r"); |
sivan_toledo | 0:3e7d6f496a67 | 98 | if (!dropbox_syncTime()) |
sivan_toledo | 0:3e7d6f496a67 | 99 | { |
sivan_toledo | 0:3e7d6f496a67 | 100 | printf("Failed to sync time"); |
sivan_toledo | 0:3e7d6f496a67 | 101 | led3 = 1; |
sivan_toledo | 0:3e7d6f496a67 | 102 | return totalFiles; |
sivan_toledo | 0:3e7d6f496a67 | 103 | } |
sivan_toledo | 0:3e7d6f496a67 | 104 | if (!dropbox_getToken(username, password)) |
sivan_toledo | 0:3e7d6f496a67 | 105 | { |
sivan_toledo | 0:3e7d6f496a67 | 106 | printf("Failed getting token\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 107 | led3 = 1; |
sivan_toledo | 0:3e7d6f496a67 | 108 | return totalFiles; |
sivan_toledo | 0:3e7d6f496a67 | 109 | } |
sivan_toledo | 0:3e7d6f496a67 | 110 | |
sivan_toledo | 0:3e7d6f496a67 | 111 | struct dirent *p = readdir(d); |
sivan_toledo | 0:3e7d6f496a67 | 112 | while (p) |
sivan_toledo | 0:3e7d6f496a67 | 113 | { |
sivan_toledo | 0:3e7d6f496a67 | 114 | strcpy(filenameSrc + 13, p->d_name); |
sivan_toledo | 0:3e7d6f496a67 | 115 | strcpy(filenameDst + 8, p->d_name); |
sivan_toledo | 0:3e7d6f496a67 | 116 | printf("Uploading %s to %s\r\n", filenameSrc, filenameDst); |
sivan_toledo | 0:3e7d6f496a67 | 117 | |
sivan_toledo | 0:3e7d6f496a67 | 118 | dropbox_upload(filenameSrc, filenameDst); |
sivan_toledo | 0:3e7d6f496a67 | 119 | c++; |
sivan_toledo | 0:3e7d6f496a67 | 120 | p = readdir(d); |
sivan_toledo | 0:3e7d6f496a67 | 121 | } |
sivan_toledo | 0:3e7d6f496a67 | 122 | |
sivan_toledo | 0:3e7d6f496a67 | 123 | closedir(d); |
sivan_toledo | 0:3e7d6f496a67 | 124 | |
sivan_toledo | 0:3e7d6f496a67 | 125 | totalFiles = c; |
sivan_toledo | 0:3e7d6f496a67 | 126 | printf("Synchronizing finished (%d)\n\r", totalFiles); |
sivan_toledo | 0:3e7d6f496a67 | 127 | led2 = 1; |
sivan_toledo | 0:3e7d6f496a67 | 128 | |
sivan_toledo | 0:3e7d6f496a67 | 129 | return totalFiles; |
sivan_toledo | 0:3e7d6f496a67 | 130 | } |
sivan_toledo | 0:3e7d6f496a67 | 131 | else |
sivan_toledo | 0:3e7d6f496a67 | 132 | { |
sivan_toledo | 0:3e7d6f496a67 | 133 | led3 = 1; |
sivan_toledo | 0:3e7d6f496a67 | 134 | printf("Failed opening /usb/DROPBOX/\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 135 | return totalFiles; |
sivan_toledo | 0:3e7d6f496a67 | 136 | } |
sivan_toledo | 0:3e7d6f496a67 | 137 | } |
sivan_toledo | 0:3e7d6f496a67 | 138 | |
sivan_toledo | 0:3e7d6f496a67 | 139 | int main() |
sivan_toledo | 0:3e7d6f496a67 | 140 | { |
sivan_toledo | 0:3e7d6f496a67 | 141 | bool ethSetup = false, httpSetup = false; |
sivan_toledo | 0:3e7d6f496a67 | 142 | USBInit(); |
sivan_toledo | 0:3e7d6f496a67 | 143 | HTTPServer svr; |
sivan_toledo | 0:3e7d6f496a67 | 144 | |
sivan_toledo | 1:b05231650f32 | 145 | int i = 0; // loop counter |
sivan_toledo | 0:3e7d6f496a67 | 146 | led3 = 0; // clear error LED |
sivan_toledo | 0:3e7d6f496a67 | 147 | |
sivan_toledo | 0:3e7d6f496a67 | 148 | for (;;) |
sivan_toledo | 0:3e7d6f496a67 | 149 | { |
sivan_toledo | 1:b05231650f32 | 150 | if (i++ % 500000 == 0) printf("looping %d\r\n", time(NULL)); |
sivan_toledo | 0:3e7d6f496a67 | 151 | |
sivan_toledo | 0:3e7d6f496a67 | 152 | USBLoop(); |
sivan_toledo | 1:b05231650f32 | 153 | Net::poll(); |
sivan_toledo | 0:3e7d6f496a67 | 154 | |
sivan_toledo | 1:b05231650f32 | 155 | // Setup ethernet |
sivan_toledo | 0:3e7d6f496a67 | 156 | if (!ethSetup) |
sivan_toledo | 0:3e7d6f496a67 | 157 | { |
sivan_toledo | 0:3e7d6f496a67 | 158 | eth = new EthernetNetIf(); |
sivan_toledo | 0:3e7d6f496a67 | 159 | ethSetup = setup_eth(); |
sivan_toledo | 0:3e7d6f496a67 | 160 | |
sivan_toledo | 0:3e7d6f496a67 | 161 | if (!ethSetup) |
sivan_toledo | 0:3e7d6f496a67 | 162 | { |
sivan_toledo | 0:3e7d6f496a67 | 163 | delete eth; |
sivan_toledo | 0:3e7d6f496a67 | 164 | eth = 0; |
sivan_toledo | 0:3e7d6f496a67 | 165 | } |
sivan_toledo | 0:3e7d6f496a67 | 166 | |
sivan_toledo | 0:3e7d6f496a67 | 167 | httpSetup = false; |
sivan_toledo | 0:3e7d6f496a67 | 168 | } |
sivan_toledo | 0:3e7d6f496a67 | 169 | |
sivan_toledo | 1:b05231650f32 | 170 | // Setup http-server |
sivan_toledo | 0:3e7d6f496a67 | 171 | if (ethSetup && !httpSetup) |
sivan_toledo | 0:3e7d6f496a67 | 172 | { |
sivan_toledo | 1:b05231650f32 | 173 | httpSetup = setup_httpServer(svr); |
sivan_toledo | 1:b05231650f32 | 174 | } |
sivan_toledo | 1:b05231650f32 | 175 | |
sivan_toledo | 1:b05231650f32 | 176 | // Check if Ethernet setup and USB-Disk connected |
sivan_toledo | 0:3e7d6f496a67 | 177 | if (usbDevice >= 0 && ethSetup) |
sivan_toledo | 1:b05231650f32 | 178 | { |
sivan_toledo | 0:3e7d6f496a67 | 179 | usbDevice = -1; |
sivan_toledo | 0:3e7d6f496a67 | 180 | |
sivan_toledo | 0:3e7d6f496a67 | 181 | led1 = 1; |
sivan_toledo | 0:3e7d6f496a67 | 182 | led2 = 0; |
sivan_toledo | 0:3e7d6f496a67 | 183 | |
sivan_toledo | 0:3e7d6f496a67 | 184 | // Read username/password |
sivan_toledo | 0:3e7d6f496a67 | 185 | char username[32] = {0}; |
sivan_toledo | 0:3e7d6f496a67 | 186 | char password[32] = {0}; |
sivan_toledo | 0:3e7d6f496a67 | 187 | if (!ReadSettings(username, password)) |
sivan_toledo | 0:3e7d6f496a67 | 188 | { |
sivan_toledo | 0:3e7d6f496a67 | 189 | printf("Failed opening /usb/DBSet.txt\r\n"); |
sivan_toledo | 0:3e7d6f496a67 | 190 | led3 = 1; |
sivan_toledo | 0:3e7d6f496a67 | 191 | continue; |
sivan_toledo | 0:3e7d6f496a67 | 192 | } |
sivan_toledo | 0:3e7d6f496a67 | 193 | |
sivan_toledo | 0:3e7d6f496a67 | 194 | // Start synchronizing |
sivan_toledo | 0:3e7d6f496a67 | 195 | int totalFiles = upload_files(username, password); |
sivan_toledo | 1:b05231650f32 | 196 | if (totalFiles >= 0) |
sivan_toledo | 1:b05231650f32 | 197 | { |
sivan_toledo | 1:b05231650f32 | 198 | //send_email(username); |
sivan_toledo | 1:b05231650f32 | 199 | } |
sivan_toledo | 0:3e7d6f496a67 | 200 | |
sivan_toledo | 1:b05231650f32 | 201 | // Update the log file |
sivan_toledo | 0:3e7d6f496a67 | 202 | FILE* logFile = fopen("/usb/dbLog.txt", "a+"); |
sivan_toledo | 0:3e7d6f496a67 | 203 | if (logFile) |
sivan_toledo | 0:3e7d6f496a67 | 204 | { |
sivan_toledo | 0:3e7d6f496a67 | 205 | if (totalFiles >= 0) |
sivan_toledo | 1:b05231650f32 | 206 | { |
sivan_toledo | 0:3e7d6f496a67 | 207 | fprintf(logFile, "%d Synchronizing successfull %d files\r\n", time(NULL), totalFiles); |
sivan_toledo | 1:b05231650f32 | 208 | } |
sivan_toledo | 0:3e7d6f496a67 | 209 | else |
sivan_toledo | 1:b05231650f32 | 210 | { |
sivan_toledo | 1:b05231650f32 | 211 | fputs("Synchronizing failed\r\n", logFile); |
sivan_toledo | 1:b05231650f32 | 212 | } |
sivan_toledo | 0:3e7d6f496a67 | 213 | |
sivan_toledo | 0:3e7d6f496a67 | 214 | fclose(logFile); |
sivan_toledo | 0:3e7d6f496a67 | 215 | } |
sivan_toledo | 0:3e7d6f496a67 | 216 | |
sivan_toledo | 0:3e7d6f496a67 | 217 | led1 = 0; |
sivan_toledo | 1:b05231650f32 | 218 | |
sivan_toledo | 1:b05231650f32 | 219 | //ClearPortPower(); |
sivan_toledo | 0:3e7d6f496a67 | 220 | } |
sivan_toledo | 0:3e7d6f496a67 | 221 | } |
sivan_toledo | 0:3e7d6f496a67 | 222 | } |