Rob Dobson / RdWebServer

Dependents:   RdBlindsServer SpideyWallWeb RdGasUseMonitor

Files at this revision

API Documentation at this revision

Comitter:
Bobty
Date:
Tue Oct 13 18:33:57 2015 +0000
Parent:
25:ffa1dddd3da4
Child:
27:0c2d6f598ae5
Commit message:
Added the capability to support a Mutex to control access to the SD card and avoid clashes

Changed in this revision

RdWebServer.cpp Show annotated file Show diff for this revision Revisions of this file
RdWebServer.h Show annotated file Show diff for this revision Revisions of this file
--- a/RdWebServer.cpp	Mon Sep 28 10:29:45 2015 +0000
+++ b/RdWebServer.cpp	Tue Oct 13 18:33:57 2015 +0000
@@ -20,7 +20,7 @@
 const int MAX_FILENAME_LEN = (MAX_ARGSTR_LEN + 20);
 
 // Constructor
-RdWebServer::RdWebServer()
+RdWebServer::RdWebServer(Mutex* pSdCardMutex)
 {
     _initOk = false;
     _pStatusLed = NULL;
@@ -31,6 +31,7 @@
     _curContentLen = -1;
     _curHttpMethod = METHOD_OTHER;
     _curWebServerCmdDef = NULL;
+    _pSdCardMutex = pSdCardMutex;
 }
 
 // Destructor
@@ -307,6 +308,11 @@
     {
         RD_INFO("Request directory %s%s", _pBaseWebFolder, inFileName);
         sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
+        
+        // Obtain lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->lock();
+            
         DIR *d = opendir(filename);
         if (d != NULL) 
         {
@@ -323,6 +329,11 @@
             }
         }
         closedir(d);
+        
+        // Release lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->unlock();
+
         RD_DBG("Directory closed\n");
         sprintf(_httpHeader,"</ul></body></html>");
         clientSocketConn.send_all(_httpHeader,strlen(_httpHeader));
@@ -334,6 +345,10 @@
         sprintf(filename, "%s%s", _pBaseWebFolder, inFileName);
         RD_INFO("Filename %s", filename);
             
+        // Obtain lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->lock();
+
         FILE* fp = fopen(filename, "r");
         if (fp == NULL)
         {
@@ -366,6 +381,11 @@
             fclose(fp);
             handledOk = true;
         }
+        
+        // Obtain lock on SD card (if reqd)
+        if (_pSdCardMutex)
+            _pSdCardMutex->unlock();
+
     }
     return handledOk;
 }
--- a/RdWebServer.h	Mon Sep 28 10:29:45 2015 +0000
+++ b/RdWebServer.h	Tue Oct 13 18:33:57 2015 +0000
@@ -128,7 +128,7 @@
         static const int METHOD_GET = 1;
         static const int METHOD_POST = 2;
         static const int METHOD_OPTIONS = 3;
-        RdWebServer();
+        RdWebServer(Mutex* pSdCardMutex = NULL);
         virtual ~RdWebServer();
         
         bool init(int port, DigitalOut* pStatusLed, char* pBaseWebFolder);
@@ -172,7 +172,9 @@
         int _curContentLen;
         int _curHttpMethod;
         RdWebServerCmdDef* _curWebServerCmdDef;
-
+        
+        // Mutex for SD card access (if supplied in constructor)
+        Mutex* _pSdCardMutex;
 };
 
 #endif