A sample program demonstrating a small but powerful web server using the Wifly module. This uses several libraries from others, but has a custom version of the WiflyInterface library, with numerous improvement to the mbed standard library.

Dependencies:   SW_HTTPServer WiflyInterface mbed C12832 IniManager

Here's the code

But you also might want to check out the SmartBoard-WiFly project page.

Basic Web Server

  • Serves static files from the selected file system. This is a compile-time setting, and a typical configuration supports gif, jpg, jpeg, ico, png, zip, gz, tar, txt, pdf, htm, and html.
  • It is designed to be small, thereby better supporting the limited resources of an embedded environment.

Advanced Web Services

  • Serves dynamically generated pages, where your software registers for a path, and then everything to that path activates your handler. Your handler then defines the header and body response.
  • Dynamic handlers can process GET query parameters (e.g. /dyn1?sky=blue&grass=green).
  • Dynamic handlers can process POST query parameters, as delivered from submission of a form.
  • Dynamic handlers can protect a resource with user:password access.

Run-Time Configurations

  • File System Support - using either the "local" file system supported by the magic chip, or from either an SD-Card or a USB flash drive.
  • Configurable to the maximum number of dynamic handlers (minimize memory requirements).
  • Configurable to the maximum number of name=value pairs for dynamic handlers (minimize memory requirements).

Compile-Time Configurations

  • Default filename for URL ending in '/' - default is 'index.htm'.
  • Configurable buffer sizes permit minimizing RAM requirements.
  • Configurable header response information.
  • Configurable for which serial port is used to communicate to the WiFly module.
  • Improved security option - to disable telnet access.

Diagnostics

  • API to determine the largest header (to more efficiently size the buffers).
  • API to gather the run-time characteristics - header processing time and content delivery time.

Limitations / Constraints

Known Issues

These are known issues, not yet resolved.

  1. Occasionally fails to serve a page - one test will constantly reload a web page every 30 seconds. It may run for hours, or minutes, then fail to load. Behaviors then are:
    • Hit the reload button in the browser and away it goes.
    • Hit the reload and you'll see the Wifly LEDs energize from the request, but no response by the web server. It appears that the embedded code does not "accept()" the connection in the TCP Socket Server.
      • In this case, the Wifly module has gone through an internal watchdog reset and the configuration parameters are such that it does not gracefully recover. Microchip is aware of this issue, but has not solved it.

Wifly Limitations

  • Single thread - it doesn't respond to overlapping requests (e.g. an embedded image may be requested before the main page completes transfer - the request is lost and the image not shown).
  • Single client - goes along with the single thread, but it doesn't support more than one client at a time.

Smart-Wifly-WebServer

  • Dynamic memory allocation - it does use dynamic memory allocation, which would be discouraged/avoided in many embedded systems. Here it uses it in parsing a request and it releases those resources upon completion of that request. If there is no other dynamic allocation that persists beyond a transaction, it should not cause memory fragmentation. Note that with multi-threading (if this is implemented with an OS), you then have race conditions that could cause fragmentation.

Web Server

Here's the web server in action. A combination of static pages served from the file system and dynamically generated pages.

/media/uploads/WiredHome/swsvr_1.pngPart of the main demo page,
which basically has all the
specifications, configurations, and limitations.
/media/uploads/WiredHome/swsvr_2.pngA zoomed out view of the same page.
/media/uploads/WiredHome/swsvr_3.pngIt would be possible to configure
the server via the web.
/media/uploads/WiredHome/swsvr_4.pngOne of the dynamically generated pages.
This one has parsed the query parameters.
/media/uploads/WiredHome/swsvr_5.pngA simple form which has a dynamic handler on the back end.
Here it takes the value associated with "leds"
and uses that to set the 4 LEDs on the mbed module.
/media/uploads/WiredHome/swsvr_6.pngA dynamic handler can require authentication.
/media/uploads/WiredHome/swsvr_7.pngSuccess!

But I've now gone so far beyond that in the current version. Here's what this one can do:

  1. It serves static web pages from a file system. I've only tested with the local file system and an SD card, but should work for any, so long as you remember that the local file system can't read subdirectories.
  2. It can serve dynamically generated web pages. This lets you accept name=value pairs using the URL (using either a GET or POST method). It can also accept them from forms. The demo lets you control the 4 LEDs from a form.
  3. As safely as possible it retrieves your credentials to the Wi-Fi Access Point. After using them, it overwrites that ram so they can't be as easily extracted.
  4. I made a large number of changes to the Wifly driver. It had too short of a timeout and I found quite a number of optimizations for performance and robustness.
  5. I have the start on a security feature - you can configure a resource to require user credentials to access it. The browser typically provides a username and password dialog. Take care however, as it does not support a secure (https) connection, so the credentials are not as securely transferred as I would like.

Optimizations I'd like to do:

  1. speed it up - I'm running the mbed to wifly module interface at 230K, which is about the top speed w/o flow control. There are other places where some time delays remain - I have eliminated a number of them.
  2. make it non-blocking, so other work can happen.
  3. integrate it with the rtos
  4. When a web page has referenced resources (e.g. an image tag), it rarely loads the image on the first try. I think the request for the resource comes in while it is still in the WiflyInterface cleaning up from the last connection. The Wifly module supports only a single connection at a time. I worked around this with a small bit of javascript to load the images after the web page.

But all in all I think it is a good start.

Program prerequisite

Here's the link to the program, but when you open it up, note a few very important items.

  1. Port Numbers listed in the constructor match the SmartBoard Baseboard.
  2. I sped up the communication baud rate to the mbed from the default 9600. Match your terminal program accordingly.
  3. Download this zip. Place it and an unzipped copy into the mbed local file system. These are the demo files.
  4. The typical ssid and password are not shown. See below to set yours.

ssid and password

You need to create a simple text file on your mbed root folder named "config.ini". The easiest way perhaps is to create "config.txt", add the information shown below and then rename it. This will be read at startup to connect you to the network. Something quite simple, like this:

[Wifi]
ssid=your_ssid
pass=your_pass_code

The program

And the program.

Import programSmart-WiFly-WebServer

A sample program demonstrating a small but powerful web server using the Wifly module. This uses several libraries from others, but has a custom version of the WiflyInterface library, with numerous improvement to the mbed standard library.

Revision:
34:77d0f63f612a
Parent:
33:41ac99847df8
--- a/Examples/DynamicFileIn.cpp	Fri Jan 03 19:57:31 2014 +0000
+++ b/Examples/DynamicFileIn.cpp	Sun May 11 21:20:00 2014 +0000
@@ -7,20 +7,26 @@
 #define DEBUG "File"
 #include "Utility.h"
 
-// This defines the size of the largest file you are willing to accept
-//
+// This defines the size of the largest file you are willing to accept.
+// Hopefully, space is also available on the file system for it.
 #define ACCEPT_MAX_BYTES (25000)
 
-// This defines the local cache, which should be about 2 x the size of a single chunk hand-off
-// which is based on the size of an ethernet data frame (~1500 bytes). So 2 x 1500 is a good number.
+// This defines the local cache, which should be at least 2 x the size of ETHER_CHUNK.
+// It can be any size above that, so long as RAM is available.
+#define PCACHE_SIZE (3000)
+
+// ETHER_CHUNK is the maximum size of a single packet handoff.
 #define ETHER_CHUNK (1500)
-#define PCACHE_SIZE (3000)
-#define MAX_BOUNDARY_LEN (150)      // Seems very generous...
+#if ((2 * ETHER_CHUNK) > PCACHE_SIZE)
+#error "Configuration error, PCACHE_SIZE must be at least 2 * ETHER_CHUNK size.
+#endif
+
 static int bytesInCache;
 static char * pCache;
 static char * boundary = "";        // for pointing at the boundary marker part of multipart/form-data in pCache
+static char * contenttype = NULL;   // for "text/plain", or "image/bmp"
 static char * fqfn = NULL;          // for the filepath/filename
-static char * contenttype = NULL;   // for "text/plain", or "image/bmp"
+static FILE * fp = NULL;
 static int bytesWritten;
 
 static int FileSize(const char * dir, const char * filename);
@@ -28,6 +34,7 @@
 static int _strnicmp(const char * left, const char * right, int len);
 static char * _stristr(char * pHaystack, const char * pNeedle);
 static char * _strnbin(char * pBinstack, const char *pNeedle, int len);
+static void FreeResources(void);
 
 typedef enum {
     IDLE,
@@ -74,8 +81,13 @@
         Seeking_ContentType,
         Seeking_Data
     } EF_State_E;
+    //const char * statelist[] = {
+    //    "Start",
+    //    "Filename",
+    //    "ContentType",
+    //    "Data"
+    //};
     static EF_State_E EF_State = Seeking_Boundary_Start;
-    static FILE * fp = NULL;
 
     char * pDataStart = pCache;                 // start of data in this transaction
     char * pDataEnd = pCache + bytesInCache;    // end of data in this transaction
@@ -83,8 +95,10 @@
     char *p1, *p2, *p3;                         // general purpose pointer for processing the results
     HTTPServer::CallBackResults ret = HTTPServer::ACCEPT_CONTINUE;
 
+    //INFO("ProcessCache %s", statelist[EF_State]);
     INFO("### Cache Begin\r\n%s\r\n### Cache End", pCache);
     if (EF_State == Seeking_Boundary_Start) {
+        INFO("Start");
         pDBLcrlf = _strnbin((char *)pDataStart, "\r\n\r\n", pDataEnd - pDataStart);    // find end of preamble
         if (pDBLcrlf) {
             p1 = _strnbin((char *)pDataStart, boundary, pDBLcrlf - pDataStart);   // is there a boundary in the cache?
@@ -95,6 +109,7 @@
     }
     
     if (EF_State == Seeking_Filename) {
+        INFO("File");
         pDBLcrlf = _strnbin((char *)pDataStart, "\r\n\r\n", pDataEnd - pDataStart);    // find end of preamble
         if (pDBLcrlf) {
             p2 = _strnbin((char *)pDataStart, "Content-Disposition: ", pDBLcrlf - pDataStart);   // and the filename container?
@@ -127,6 +142,7 @@
     }
     
     if (EF_State == Seeking_ContentType) {
+        INFO("ContentType");
         pDBLcrlf = _strnbin((char *)pDataStart, "\r\n\r\n", pDataEnd - pDataStart);    // find end of preamble
         if (pDBLcrlf) {
             p2 = _strnbin((char *)pDataStart, "Content-Type: ", pDBLcrlf - pDataStart);
@@ -154,17 +170,13 @@
     }
     
     if (EF_State == Seeking_Data) {
+        INFO("Data");
         if (!fp && fqfn) {
-            //INFO("Open file [%s]", fqfn);
+            INFO("Open file [%s]", fqfn);
             fp = fopen(fqfn, "w");
             if (!fp) {
                 ERR("failed to open [%s]", fqfn);
-                free(fqfn);
-                fqfn = NULL;
-                if (contenttype) {
-                    free(contenttype);
-                    contenttype = NULL;
-                }
+                FreeResources();
                 ret = HTTPServer::ACCEPT_ERROR;
                 return ret;
             }
@@ -216,24 +228,7 @@
     }
 
     if (type == HTTPServer::DATA_TRANSFER_END) {
-        if (fp) {
-            fclose(fp);
-            fp = NULL;
-            if (fqfn) {
-                free(fqfn);
-                fqfn = NULL;
-            }
-            if (contenttype) {
-                free(contenttype);
-                contenttype = NULL;
-            }
-        }
-        if (pCache) {
-            //INFO("pCache - freed");
-            free(pCache);
-            pCache = NULL;
-            bytesInCache = 0;
-        }
+        FreeResources();
         ret = HTTPServer::ACCEPT_COMPLETE;
     }
     return ret;
@@ -295,6 +290,7 @@
 
         case HTTPServer::DATA_TRANSFER_END:
             //INFO("\r\n\\\\\\\\\\\\ Transfer Begin [%d, %d]\r\n%s\r\n////// Transfer End", count, bytesInCache, path);
+            INFO("TransferEnd [%d, %d, %d, %d]", count, bytesInCache, PCACHE_SIZE - ETHER_CHUNK, PCACHE_SIZE);
             // if there is anything there, copy it in.
             if (count) {
                 memcpy(pCache + bytesInCache, path, count);
@@ -307,22 +303,25 @@
         case HTTPServer::DATA_TRANSFER:
             //INFO("\r\n\\\\\\\\\\\\ Transfer Begin [%d, %d]\r\n%s\r\n////// Transfer End", count, bytesInCache, path);
             // We chose to accept the transfer, and it may come in chunks.
-            if (bytesInCache + count < PCACHE_SIZE - ETHER_CHUNK) { // room in the cache for more, juat accept it locally
+            if ((bytesInCache + count) < (PCACHE_SIZE - ETHER_CHUNK)) { // room in the cache for more, juat accept it locally
+                INFO("Transfer A  [%d, %d, %d, %d]", count, bytesInCache, PCACHE_SIZE - ETHER_CHUNK, PCACHE_SIZE);
                 memcpy(pCache + bytesInCache, path, count);
                 bytesInCache += count;
                 pCache[bytesInCache] = '\0'; // one past the data, so ok (and good for debugging text streams)
                 ret = HTTPServer::ACCEPT_CONTINUE;
-            } else if (bytesInCache + count < PCACHE_SIZE) {        // room in the cache, now process it.
+            } else if ((bytesInCache + count) < PCACHE_SIZE) {        // room in the cache, now process it.
+                INFO("Transfer B  [%d, %d, %d, %d]", count, bytesInCache, PCACHE_SIZE - ETHER_CHUNK, PCACHE_SIZE);
                 memcpy(pCache + bytesInCache, path, count);
                 bytesInCache += count;
                 pCache[bytesInCache] = '\0';    // one past the data, so ok (and good for debugging text streams)
                 ret = ProcessCache(svr, type);  // now hand it off
             } else {
+                INFO("Transfer C  [%d, %d, %d, %d]", count, bytesInCache, PCACHE_SIZE - ETHER_CHUNK, PCACHE_SIZE);
                 ret = ProcessCache(svr, type);  // panic, ah!, ok, first hand it off then cache it
                 memcpy(pCache + bytesInCache, path, count);
                 bytesInCache += count;
                 pCache[bytesInCache] = '\0';    // one past the data, so ok (and good for debugging text streams)
-                ERR("FATAL - didn't flush pCache, was about to be overrun");
+                ERR("FATAL - didn't flush pCache, %d in cache.", bytesInCache);
                 break;
             }
             break;
@@ -388,6 +387,27 @@
     return ret;
 }
 
+static void FreeResources(void)
+{
+    if (fp) {
+        fclose(fp);
+        fp = NULL;
+    }
+    if (fqfn) {
+        free(fqfn);
+        fqfn = NULL;
+    }
+    if (contenttype) {
+        free(contenttype);
+        contenttype = NULL;
+    }
+    if (pCache) {
+        free(pCache);
+        pCache = NULL;
+        bytesInCache = 0;
+    }
+}
+
 static int FileSize(const char * dir, const char * filename)
 {
     int size = 0;
@@ -402,6 +422,7 @@
             size = ftell(f);
             fclose(f);
         }
+        free(fqfn);
     }
     return size;
 }