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

Committer:
sivan_toledo
Date:
2011-04-17
Revision:
0:3e7d6f496a67
Child:
1:b05231650f32

File content as of revision 0:3e7d6f496a67:

/*
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
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "mbed.h"
#include "USBHost.h"
#include "Utils.h"
#include "UsbStorage.h"

#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "Dropbox.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);
USBFileSystem fs;
int usbDevice = -1;

// NET
EthernetNetIf* eth;

// LED
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

int OnDiskInsert(int device)
{
    printf("\r\nOnDiskInsert\r\n");
    
    usbDevice = device;
    fs.SetDevice(usbDevice);
    
    return 0;
}


bool setup_eth()
{
  printf("\r\nSetting up...\r\n");

  EthernetErr ethErr = eth->setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return false;
  }
  printf("\r\nSetup OK\r\n");
  
  return true;
}

bool setup_httpServer(HTTPServer& svr)
{
   svr.addHandler<HttpHandlerUsbBrowser>("/UsbBrowser");  
   svr.addHandler<HttpHandlerSetup>("/");
   svr.bind(80);

   printf("Http Server initialized\r\n");  
   
   return true;   
}

bool upload_files(char* username, char* password)
{
    DIR *d = opendir("/usb/DROPBOX"); //TODO - global
    int totalFiles = -1;
    if (d)
    {
        char filenameSrc[64] = "/usb/DROPBOX/";
        char filenameDst[32] = "USBSYNC_"; //TODO - remove?
        int c = 0;
        
        printf("Synchronize Start\n\r");
        if (!dropbox_syncTime())
        {
            printf("Failed to sync time");
            led3 = 1;
            return totalFiles;
        }
        if (!dropbox_getToken(username, password))
        {
            printf("Failed getting token\r\n");
            led3 = 1;
            return totalFiles;
        }
    
        struct dirent *p = readdir(d);
        while (p)
        {
            strcpy(filenameSrc + 13, p->d_name);
            strcpy(filenameDst + 8, p->d_name);
            printf("Uploading %s to %s\r\n", filenameSrc, filenameDst);
            
            dropbox_upload(filenameSrc, filenameDst);
            c++;
            p = readdir(d);
        }
        
        closedir(d);
        
        totalFiles = c;
        printf("Synchronizing finished (%d)\n\r", totalFiles);
        led2 = 1;
        
        return totalFiles;
    }
    else
    {
        led3 = 1;
        printf("Failed opening /usb/DROPBOX/\r\n");
        return totalFiles;
    }
}

int main()
{
    bool ethSetup = false, httpSetup = false;
    USBInit();    
    HTTPServer svr;
    
    int i = 0;
    
    led3 = 0; // clear error LED
    
    for (;;)
    {
        if (i++ % 500000 == 0) printf("looping\r\n"); //TODO replace with time
        
        USBLoop();
        Net::poll();
        
        if (!ethSetup)
        {
            eth = new EthernetNetIf();
            ethSetup = setup_eth();
            
            if (!ethSetup)
            {
                delete eth;
                eth = 0;
            }
            
            httpSetup = false;
        }
        
        if (ethSetup && !httpSetup)
        {
           httpSetup = setup_httpServer(svr);         
        }
        
        // Cehck if Ethernet setup and USB-Disk connected
        if (usbDevice >= 0 && ethSetup)
        {                       
            usbDevice = -1;

            led1 = 1;
            led2 = 0;
            
            // Read username/password
            char username[32] = {0};
            char password[32] = {0};
            if (!ReadSettings(username, password))
            {
                printf("Failed opening /usb/DBSet.txt\r\n");
                led3 = 1;
                continue;
            }
            
            // Start synchronizing
            int totalFiles = upload_files(username, password);

            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?
                
                fclose(logFile);
            }

            led1 = 0;
           ClearPortPower();
        }
    }
}