posilani dat

Dependencies:   FatFileSystemCpp mbed PowerControl USBHostLite

Revision:
17:ca53e6d36163
Parent:
15:baa2672a9b38
Child:
18:7acae34b518d
--- a/main.cpp	Thu Oct 22 17:18:10 2015 +0000
+++ b/main.cpp	Tue Nov 10 13:39:39 2015 +0000
@@ -262,6 +262,32 @@
     //flagSetNotify = 0;                                                        // Used for saving debug
 }
 
+/* Sees whether giver file exists on flash disk */
+bool doesFileExist(const char *filename)
+{
+    /* Dir related structs & vars */
+    DIR *dir;                                                                   // Prepare directory handle
+    struct dirent *file;                                                        // Directory entry structure
+    dir = opendir("/usb");                                                      // Open root of usb flash disk                                                      
+    
+    /* Iterate all available files in root directory */
+    while((file = readdir(dir)) != NULL)                                        // Incrementally points to next files, until NULL is reached
+    {
+        /* Check if it's file we are looking for */
+        if(strcmp(filename+5, file->d_name) == 0)                               // +5 moves us beyond /usb/, not cool I know...
+        {
+            closedir(dir);                                                      // Close dir & return true
+            return true;   
+        }
+    }
+    
+    /* If file is not present */
+    closedir(dir);                                                              // Close dir & return false
+    return false;                                                               // If not, return false
+} 
+
+// Functions -> Commands =======================================================
+
 /* Starts to acquire data from sensors */
 void startSaving(void)
 {
@@ -329,28 +355,39 @@
 /* Sends acquired data via Wi-Fi */
 void transfer(void)
 {
-    /* Timeout (to make sure C# app gets ready) */
-    wait_ms(100);
+     /* Vars */
+    char name[30];                                                              // File name buffer  
+    
+    /* File transfer prerequisites */
+    fclose(gfp);                                                                // Close curretly opened file
+    int requested_id = wifi.readByte();                                         // Get byte containing requested file ID
+    sprintf(name, "/usb/swimmer%d.txt", requested_id);                          // Create file name based on current swimmer ID
     
-    /* Vars */
-    int i;                                                                      // Iterator
-    char name[30];                                                              // File name buffer
-
-    /* Iterate all captured swimmers */
-    for (i = 0; i < swimmer_id; i++)                                            // Iterate all available swimmer files
+    /* Handle transfer */
+    if(doesFileExist(name))                                                     // At first check whether file exists (fopen used to freeze mbed)
     {
-        sprintf(name, "/usb/swimmer%d.txt", i);                                 // Create file name based on current swimmer ID
-        fclose(gfp);
+        /* Send ACK (should make this more abstract) */
+        wifi.sendByte('#');
+        wifi.sendByte('A');
+        wait_ms(50);                                                            // Timeout is used to make sure C# gets ready (sockets are messy, flushing is pain)
+        /* Actually try to send file */                                         
         printf("\r\nSending %s", name);                                         // Notify user which user is being sent
-        if (wifi.sendFile(gfp ,name, i))                                        // Send !
+        if (wifi.sendFile(name, requested_id))                                  // Send !
         {
             leds_error();                                                       // Handle error
             printf("\r\nUnable to send data");                                  // Notify user via text also
         }
         else
         {
-            printf("\r\nSwimmer %d finished", i);                               // Otherwise all is AOK
-        }
+            printf("\r\nSwimmer %d finished", requested_id);                    // Otherwise all is AOK
+        }  
+    }
+    else
+    {
+        /* In case file doest not exist send NACK */
+        wifi.sendByte('#');                                                     
+        wifi.sendByte('F');
+        printf("\r\nRequest non-existing file...");
     }
 }
 
@@ -415,7 +452,6 @@
                     measuring();                                                // Measure !
                     measuringFlag = 0;                                          // Reset flag
                 }
-                
                 break;                                                          // Break from switch
             }