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

Files at this revision

API Documentation at this revision

Comitter:
sivan_toledo
Date:
Mon Apr 25 12:31:46 2011 +0000
Parent:
0:3e7d6f496a67
Commit message:

Changed in this revision

Dropbox/Dropbox.cpp Show annotated file Show diff for this revision Revisions of this file
Dropbox/Dropbox.h Show annotated file Show diff for this revision Revisions of this file
Dropbox/HTTPFileSender.h Show annotated file Show diff for this revision Revisions of this file
HttpHandlerSetup.cpp Show annotated file Show diff for this revision Revisions of this file
HttpHandlerSetup.h Show annotated file Show diff for this revision Revisions of this file
HttpHandlerUsbBrowser.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Dropbox/Dropbox.cpp	Sun Apr 17 21:30:10 2011 +0000
+++ b/Dropbox/Dropbox.cpp	Mon Apr 25 12:31:46 2011 +0000
@@ -1,158 +1,181 @@
-#include "Dropbox.h"
-#include "HTTPClient.h"
-#include "HTTPFileSender.h"
-#include "base64.h"
-#include "url.h"
-#include "ctc_hmac.h"
-#include "NTPClient.h"
-#include "HTTPData.h"
-
-// NET
-NTPClient ntpClient;
-HTTPResult result;
-
-// Dropbox
-string consumer_token = "p4t8cn8b3ddci2d";
-string consumer_token_secret = "wtib99x350qo5l6";
-char token[16] = {0};
-char token_secret[16] = {0};
-bool completed = false;
-int v = 6000;
-char buffer[1024] = {0};
-
-extern DigitalOut led1;
-extern DigitalOut led3;
-extern void USBLoop();
-
-Timer timer;
-
-void request_callback(HTTPResult r)
-{
-  result = r;
-  completed = true;
-}
-
-bool dropbox_getToken(char* username, char* password)
-{
-    HTTPClient client;
-    HTTPText data;
-    
-    /* Request Token */ 
-    printf("Requesting token\r\n");
-    
-    char request[128] = {0};
-    sprintf(request,
-            "http://api.getdropbox.com/0/token?oauth_consumer_key=p4t8cn8b3ddci2d&email=%s&password=%s",
-            username,
-            password);
-    
-    printf(request);
-    HTTPResult result = client.get(request, &data);
-                                   
-    if (result != HTTP_OK)
-    {
-        printf("R: %d", result);
-        return false;
-    }
-    
-    const char *dataContent = data.gets();    
-
-    memcpy(token, dataContent + 11, 15); 
-    memcpy(token_secret, dataContent + 40, 15); 
-    
-    printf("Got token %s %s %s\r\n", dataContent, token, token_secret);
-    
-    return true;
-}
-
-bool dropbox_syncTime()
-{
-    /* Get Time */
-    printf("Synchronizing time\r\n");
-    Host server(IpAddr(), 123, "0.pool.ntp.org");
-    NTPResult result = ntpClient.setTime(server);
-    
-    //srand(time(NULL));
-    
-    return (result == NTP_OK);
-}
-
-    
-bool dropbox_upload(string srcFile, string dstFile)
-{
-    HTTPClient client;
-    string dst = dstFile;
-    
-    time_t currentTime = time(NULL);
-      
-    v++; //= (rand() % 9999999) + 1000;
-
-    //printf("T: %d NONCE: %d\r\n", currentTime, v); 
-    
-    memset(buffer, 0x00, 1024);
-    sprintf(buffer, 
-            "POST&http%%3A%%2F%%2Fapi-content.dropbox.com%%2F0%%2Ffiles%%2Fdropbox%%2F&file%%3D%s%%26oauth_consumer_key%%3D%s%%26oauth_nonce%%3D%d%%26oauth_signature_method%%3DHMAC-SHA1%%26oauth_timestamp%%3D%d%%26oauth_token%%3D%s",
-            dst.c_str(),
-            consumer_token.c_str(),
-            v,
-            currentTime,
-            token);
-   
-    //printf("Sig: %s %d\n\r", buffer, strlen(buffer)); 
-              
-    string key = consumer_token_secret + "&" + token_secret;
-    
-    //printf("Key: %s %d\n\r", key.c_str(), key.size()); 
-    
-    byte hash[SHA_DIGEST_SIZE];
-    Hmac hmac;
-    HmacSetKey(&hmac, SHA, (const byte*)key.c_str(), key.size());
-    HmacUpdate(&hmac, (const byte*)buffer, strlen(buffer));
-    HmacFinal(&hmac, hash);
-
-    char sig[128] = {0};
-    base64enc((char*)hash, SHA_DIGEST_SIZE, sig);
-
-    char *encodedSig = url_encode(sig);
-    
-    memset(buffer, 0x00, 1024);
-    sprintf(buffer, 
-            "http://api-content.dropbox.com/0/files/dropbox/?file=%s&oauth_consumer_key=%s&oauth_nonce=%d&oauth_timestamp=%d&oauth_token=%s&oauth_signature_method=HMAC-SHA1&oauth_signature=%s",
-            dst.c_str(),
-            consumer_token.c_str(),
-            v,
-            currentTime,
-            token,
-            encodedSig);
-            
-    //printf("Req: %s %d\n\r", buffer, strlen(buffer));
-
-    HTTPFileSender httpFile(srcFile.c_str(), dst.c_str());
-
-    HTTPText res;
-
-    //client.setTimeout(5000);
-    
-    completed = false;
-    HTTPResult postResult = client.post(buffer, httpFile, &res, request_callback);
-    
-    timer.start();
-    
-    while (!completed)
-    {
-        led1 = (timer.read_ms() % 250) >= 125;
-        USBLoop();
-        Net::poll();
-    }
-    
-    timer.stop();
-    timer.reset();
-    
-    led1 = 1;
-    
-    printf("Res: %s %d %d\n\r", res.gets(), postResult, result);
-    
-    free(encodedSig);
-    
-    return (result == HTTP_OK);
+#include "Dropbox.h"
+#include "HTTPClient.h"
+#include "HTTPFileSender.h"
+#include "base64.h"
+#include "url.h"
+#include "ctc_hmac.h"
+#include "NTPClient.h"
+#include "HTTPData.h"
+
+// NET
+NTPClient ntpClient;
+HTTPResult result;
+
+// Dropbox
+string consumer_token = "p4t8cn8b3ddci2d";
+string consumer_token_secret = "wtib99x350qo5l6";
+char token[16] = {0};
+char token_secret[16] = {0};
+volatile bool completed = false;
+int v = 6000;
+char buffer[1024] = {0};
+
+Timer timer;
+
+extern DigitalOut led1;
+extern DigitalOut led3;
+extern void USBLoop();
+
+void request_callback(HTTPResult r)
+{
+  result = r;
+  completed = true;
+}
+
+bool dropbox_getToken(char* username, char* password)
+{
+    HTTPText data;
+    HTTPClient client;
+    
+    /* Request Token */ 
+    printf("Requesting token\r\n");
+    
+    char request[128] = {0};
+    sprintf(request,
+            "http://api.getdropbox.com/0/token?oauth_consumer_key=p4t8cn8b3ddci2d&email=%s&password=%s",
+            username,
+            password);
+    
+    printf(request);
+    HTTPResult result = client.get(request, &data);
+                                   
+    if (result != HTTP_OK)
+    {
+        printf("R: %d", result);
+        return false;
+    }
+    
+    const char *dataContent = data.gets();    
+
+    memcpy(token, dataContent + 11, 15); 
+    memcpy(token_secret, dataContent + 40, 15); 
+    
+    printf("Got token %s %s %s\r\n", dataContent, token, token_secret);
+    
+    return true;
+}
+
+bool dropbox_syncTime()
+{
+    /* Get Time */
+    printf("Synchronizing time\r\n");
+    Host server(IpAddr(), 123, "0.pool.ntp.org");
+    NTPResult result = ntpClient.setTime(server);
+    
+    //srand(time(NULL));
+    
+    return (result == NTP_OK);
+}
+
+bool dropbox_upload(string srcFile, string dstFile)
+{    
+    HTTPClient client;
+    string dst = dstFile;
+    
+    time_t currentTime = time(NULL);
+      
+    v++; //= (rand() % 9999999) + 1000;
+
+    //printf("T: %d NONCE: %d\r\n", currentTime, v); 
+    
+    memset(buffer, 0x00, 1024);
+    sprintf(buffer, 
+            "POST&http%%3A%%2F%%2Fapi-content.dropbox.com%%2F0%%2Ffiles%%2Fdropbox%%2F&file%%3D%s%%26oauth_consumer_key%%3D%s%%26oauth_nonce%%3D%d%%26oauth_signature_method%%3DHMAC-SHA1%%26oauth_timestamp%%3D%d%%26oauth_token%%3D%s",
+            dst.c_str(),
+            consumer_token.c_str(),
+            v,
+            currentTime,
+            token);
+   
+    //printf("Sig: %s %d\n\r", buffer, strlen(buffer)); 
+              
+    string key = consumer_token_secret + "&" + token_secret;
+    
+    //printf("Key: %s %d\n\r", key.c_str(), key.size()); 
+    
+    byte hash[SHA_DIGEST_SIZE];
+    Hmac hmac;
+    HmacSetKey(&hmac, SHA, (const byte*)key.c_str(), key.size());
+    HmacUpdate(&hmac, (const byte*)buffer, strlen(buffer));
+    HmacFinal(&hmac, hash);
+
+    char sig[128] = {0};
+    base64enc((char*)hash, SHA_DIGEST_SIZE, sig);
+
+    char *encodedSig = url_encode(sig);
+    
+    memset(buffer, 0x00, 1024);
+    sprintf(buffer, 
+            "http://api-content.dropbox.com/0/files/dropbox/?file=%s&oauth_consumer_key=%s&oauth_nonce=%d&oauth_timestamp=%d&oauth_token=%s&oauth_signature_method=HMAC-SHA1&oauth_signature=%s",
+            dst.c_str(),
+            consumer_token.c_str(),
+            v,
+            currentTime,
+            token,
+            encodedSig);
+            
+    //printf("Req: %s %d\n\r", buffer, strlen(buffer));
+
+    HTTPFileSender httpFile(srcFile.c_str(), dst.c_str());
+
+    HTTPText res;
+
+    //client.setTimeout(5000);
+    
+    completed = false;
+    HTTPResult postResult = client.post(buffer, httpFile, &res, request_callback);
+    
+    timer.start();
+    
+    while (!completed)
+    {
+        led1 = (timer.read_ms() % 250) >= 125;
+        USBLoop();
+        Net::poll();
+    }
+    
+    timer.stop();
+    timer.reset();
+    
+    led1 = 1;
+    
+    printf("Res: %s %d %d\n\r", res.gets(), postResult, result);
+    
+    free(encodedSig);
+    
+    return (result == HTTP_OK);
+}
+
+bool send_email(char* email)
+{
+    HTTPText txt;
+    HTTPClient client;
+    //char request[128] = "http://www.davidberlin.co.il/mail.php?pass=borisdavid&email=";    
+    //strcpy(request + 60, email);
+    char request[128] = "http://holev.com/hello.txt";
+    
+    //printf(request);
+    
+    completed = false;
+    HTTPResult result2 = client.get(request, &txt, request_callback);
+    
+    while (!completed)
+    {
+        Net::poll();
+    }
+    led1 = 1;
+    
+    printf("Result %d:\"%s\"\r\n", result, txt.gets());
+    
+    return result == HTTP_OK;
 }
\ No newline at end of file
--- a/Dropbox/Dropbox.h	Sun Apr 17 21:30:10 2011 +0000
+++ b/Dropbox/Dropbox.h	Mon Apr 25 12:31:46 2011 +0000
@@ -1,8 +1,14 @@
 #include "mbed.h"
 #include <string>
 
+// Get user token and secret based on username and password
 bool dropbox_getToken(char* username, char* password);
 
+// Syncrhonize controller time to real-world time for dropbox authorization process
 bool dropbox_syncTime();
 
-bool dropbox_upload(string srcFile, string dstFile);
\ No newline at end of file
+// Upload local srcFile to dropbox with dstFile name
+bool dropbox_upload(string srcFile, string dstFile);
+
+// Sends successful-sync confirmation email
+bool send_email(char* email);
\ No newline at end of file
--- a/Dropbox/HTTPFileSender.h	Sun Apr 17 21:30:10 2011 +0000
+++ b/Dropbox/HTTPFileSender.h	Mon Apr 25 12:31:46 2011 +0000
@@ -1,7 +1,7 @@
 #include "HTTPData.h"
-//#include "HTTPText.h"
 #include "HTTPClient.h"
 
+// uploads a local file using multipart encoding
 class HTTPFileSender : public HTTPData
 {
  public:
--- a/HttpHandlerSetup.cpp	Sun Apr 17 21:30:10 2011 +0000
+++ b/HttpHandlerSetup.cpp	Mon Apr 25 12:31:46 2011 +0000
@@ -6,7 +6,7 @@
     FILE* settingsFile = fopen("/usb/DBSet.txt", "r");
     if (settingsFile)
     {
-       //printf("before fseek %d\n\r", settingsFile);
+       printf("before fseek %d\n\r", settingsFile);
        fseek(settingsFile, 9L, SEEK_SET);
        fgets(username, 32, settingsFile);
        fseek(settingsFile, 9L, SEEK_CUR);
@@ -15,7 +15,7 @@
 
        int len = strlen(username); username[len - 2] = 0; // remove /r/n
        //printf("ReadSettings %s, %s, %d, %d,  %s\r\n", username, password, len, ftell(settingsFile), data);
-       
+       //printf("ReadSettings OK %s, %s\r\n", username, password);
        fclose(settingsFile);
     }
     
@@ -52,19 +52,20 @@
 }
 
 void HttpHandlerSetup::UpdateSettingsFile()
-{ 
+{   
   if (m_WasDataRead)
   {
     return;
   }
-
+  
   char buffer[256];
   int dataLength = dataLen();
   int readBytes = readData(buffer, 45 + dataLength);
   
   if (readBytes > 0)
   {  
-      int offset = readBytes > dataLength ? 45 : 0; // Inconsistency id data reading
+      //printf("UpdateSettingsFile %s \r\n", buffer);
+      int offset = readBytes > dataLength ? 45 : 0; // Inconsistent data reading
       char* c1 = strchr(buffer, '&');
       char* c2 = strchr(buffer + offset, '=') + 1;
       char* c3 = strchr(c1, '=') + 1;
@@ -177,13 +178,14 @@
 {
   printf("onReadable\r\n");
   UpdateSettingsFile();  
+  close();
 }
 
 void HttpHandlerSetup::onWriteable() //Data has been written & buf is free
 {
 //  printf("onWriteable() password: %s, username:%s \r\n", Password, Username);
   printf("onWriteable\r\n");
-  //close(); //Data written, we can close the connection
+  close(); //Data written, we can close the connection
 }
 
 void HttpHandlerSetup::onClose() //Connection is closing
--- a/HttpHandlerSetup.h	Sun Apr 17 21:30:10 2011 +0000
+++ b/HttpHandlerSetup.h	Mon Apr 25 12:31:46 2011 +0000
@@ -3,8 +3,10 @@
 #ifndef HTTP_HANDLER_SETUP
 #define HTTP_HANDLER_SETUP
 
+// Read password and username from the settting file (/usb/DBSet.txt)
 bool ReadSettings(char* username, char* password);
 
+// Http handler that provides welcome and setup pages
 class HttpHandlerSetup : public HTTPRequestHandler
 {
 public:
@@ -23,8 +25,8 @@
   virtual void onClose(); //Connection is closing
 
 private:
-  void doGetPageWelcome();
-  void UpdateSettingsFile();
+  void doGetPageWelcome(); // Called from doGet(), get the welcome webpage
+  void UpdateSettingsFile(); // Updates the username and password in the settings file
   
   bool m_WasDataRead;
 };
--- a/HttpHandlerUsbBrowser.h	Sun Apr 17 21:30:10 2011 +0000
+++ b/HttpHandlerUsbBrowser.h	Mon Apr 25 12:31:46 2011 +0000
@@ -1,5 +1,6 @@
 #include "HTTPServer.h"
 
+// Http handler that provides usb-browsing functionality.
 class HttpHandlerUsbBrowser : public HTTPRequestHandler
 {
 public:
--- a/main.cpp	Sun Apr 17 21:30:10 2011 +0000
+++ b/main.cpp	Mon Apr 25 12:31:46 2011 +0000
@@ -1,6 +1,4 @@
 /*
-Copyright (c) 2010 Peter Barrett
-
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
@@ -26,19 +24,11 @@
 #include "UsbStorage.h"
 
 #include "EthernetNetIf.h"
-#include "HTTPClient.h"
 #include "Dropbox.h"
 #include "HTTPServer.h"
+#include "HTTPServer.h"
 #include "HttpHandlerSetup.h"
 #include "HttpHandlerUsbBrowser.h"
-#include "HTTPFileSender.h"
-#include "base64.h"
-#include "url.h"
-#include "ctc_hmac.h"
-#include "NTPClient.h"
-#include "HTTPData.h"
-
-void test_dropbox();
 
 // USB
 Serial pc(USBTX, USBRX);
@@ -53,6 +43,7 @@
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 
+// Occurs on usb flash disk connection
 int OnDiskInsert(int device)
 {
     printf("\r\nOnDiskInsert\r\n");
@@ -63,7 +54,7 @@
     return 0;
 }
 
-
+// Setup ethernet connection
 bool setup_eth()
 {
   printf("\r\nSetting up...\r\n");
@@ -79,6 +70,7 @@
   return true;
 }
 
+// Setup HttpHandlers
 bool setup_httpServer(HTTPServer& svr)
 {
    svr.addHandler<HttpHandlerUsbBrowser>("/UsbBrowser");  
@@ -90,14 +82,16 @@
    return true;   
 }
 
+// Uploads a folder on usb flash-drive to dropbox
 bool upload_files(char* username, char* password)
 {
-    DIR *d = opendir("/usb/DROPBOX"); //TODO - global
+    DIR *d = opendir("/usb/DROPBOX");
     int totalFiles = -1;
+    
     if (d)
     {
         char filenameSrc[64] = "/usb/DROPBOX/";
-        char filenameDst[32] = "USBSYNC_"; //TODO - remove?
+        char filenameDst[32] = "USBSYNC_";
         int c = 0;
         
         printf("Synchronize Start\n\r");
@@ -148,17 +142,17 @@
     USBInit();    
     HTTPServer svr;
     
-    int i = 0;
-    
+    int i = 0; // loop counter    
     led3 = 0; // clear error LED
     
     for (;;)
     {
-        if (i++ % 500000 == 0) printf("looping\r\n"); //TODO replace with time
+        if (i++ % 500000 == 0) printf("looping %d\r\n", time(NULL));
         
         USBLoop();
-        Net::poll();
+        Net::poll();       
         
+        // Setup ethernet
         if (!ethSetup)
         {
             eth = new EthernetNetIf();
@@ -173,14 +167,15 @@
             httpSetup = false;
         }
         
+        // Setup http-server
         if (ethSetup && !httpSetup)
         {
-           httpSetup = setup_httpServer(svr);         
-        }
-        
-        // Cehck if Ethernet setup and USB-Disk connected
+           httpSetup = setup_httpServer(svr);
+        }                
+
+        // Check if Ethernet setup and USB-Disk connected
         if (usbDevice >= 0 && ethSetup)
-        {                       
+        {
             usbDevice = -1;
 
             led1 = 1;
@@ -198,20 +193,30 @@
             
             // Start synchronizing
             int totalFiles = upload_files(username, password);
+            if (totalFiles >= 0)
+            {
+                //send_email(username);
+            }
 
+            // Update the log file
             FILE* logFile = fopen("/usb/dbLog.txt", "a+");
             if (logFile)
             {
                 if (totalFiles >= 0)
+                {
                     fprintf(logFile, "%d Synchronizing successfull %d files\r\n", time(NULL), totalFiles);
+                }
                 else
-                    fputs("Synchronizing failed\r\n", logFile); //TODO whats this?
+                {
+                    fputs("Synchronizing failed\r\n", logFile);
+                }
                 
                 fclose(logFile);
             }
 
             led1 = 0;
-           ClearPortPower();
+
+            //ClearPortPower();
         }
     }
 }
\ No newline at end of file