David Smart / Mbed 2 deprecated Smart-WiFly-WebServer

Dependencies:   SW_HTTPServer WiflyInterface mbed C12832 IniManager

Committer:
WiredHome
Date:
Tue Jun 25 18:12:24 2013 +0000
Revision:
7:72b5df2fac93
Parent:
6:c045118a21de
Child:
8:50fc3ddf828a
Updates to the examples based on API improvements.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 4:178df829d62b 1 /** @file main.cpp contains the main program that a user would write.
WiredHome 4:178df829d62b 2 * see the documentation above "main"
WiredHome 4:178df829d62b 3 */
WiredHome 4:178df829d62b 4 #include "mbed.h" // ver 63
WiredHome 4:178df829d62b 5 #include "SDFileSystem.h" // ver 2, standard
WiredHome 4:178df829d62b 6
WiredHome 4:178df829d62b 7 // Modified standard components
WiredHome 4:178df829d62b 8 #include "WiflyInterface.h" // ver 4+, derived from version 4 with my mods
WiredHome 4:178df829d62b 9
WiredHome 4:178df829d62b 10 // My components
WiredHome 4:178df829d62b 11 #include "SW_HTTPServer.h" // ver 0, the initial release.
WiredHome 4:178df829d62b 12 #include "Utility.h"
WiredHome 4:178df829d62b 13
WiredHome 4:178df829d62b 14 #include "DynamicPages.h" // my dynamically generated pages
WiredHome 4:178df829d62b 15
WiredHome 4:178df829d62b 16 Serial pc(USBTX, USBRX);
WiredHome 4:178df829d62b 17
WiredHome 4:178df829d62b 18 LocalFileSystem local("local"); // some place to hold settings and maybe he static web pages
WiredHome 4:178df829d62b 19 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // for the static web pages
WiredHome 4:178df829d62b 20
WiredHome 4:178df829d62b 21
WiredHome 4:178df829d62b 22 /// This section helps manage the credentials. I don't like the examples
WiredHome 4:178df829d62b 23 /// that show clear text credentials, even if they are simply the
WiredHome 4:178df829d62b 24 /// access point ssid and passphrase. So, I created this to let me
WiredHome 4:178df829d62b 25 /// edit a simple file on the file system and put them in there.
WiredHome 4:178df829d62b 26 /// Marginally more secure, but at least not in the source code that
WiredHome 4:178df829d62b 27 /// I might inadvertently share with others.
WiredHome 4:178df829d62b 28 ///
WiredHome 4:178df829d62b 29 typedef struct
WiredHome 4:178df829d62b 30 {
WiredHome 4:178df829d62b 31 char * ssid;
WiredHome 4:178df829d62b 32 char * pass;
WiredHome 4:178df829d62b 33 } credentials;
WiredHome 4:178df829d62b 34
WiredHome 4:178df829d62b 35 #define CREDENTIAL_SIZE 50
WiredHome 4:178df829d62b 36 bool ReadCredentials(char * file, credentials * cr)
WiredHome 4:178df829d62b 37 {
WiredHome 4:178df829d62b 38 FILE *fp = fopen(file, "r");
WiredHome 4:178df829d62b 39
WiredHome 4:178df829d62b 40 cr->ssid = (char *)malloc(CREDENTIAL_SIZE);
WiredHome 4:178df829d62b 41 cr->pass = (char *)malloc(CREDENTIAL_SIZE);
WiredHome 4:178df829d62b 42 if (fp) {
WiredHome 4:178df829d62b 43 fgets(cr->ssid, CREDENTIAL_SIZE, fp);
WiredHome 4:178df829d62b 44 fgets(cr->pass, CREDENTIAL_SIZE, fp);
WiredHome 4:178df829d62b 45 fclose(fp);
WiredHome 4:178df829d62b 46 RTrim(cr->ssid);
WiredHome 4:178df829d62b 47 RTrim(cr->pass);
WiredHome 4:178df829d62b 48 return true;
WiredHome 4:178df829d62b 49 } else {
WiredHome 4:178df829d62b 50 return false;
WiredHome 4:178df829d62b 51 }
WiredHome 4:178df829d62b 52 }
WiredHome 4:178df829d62b 53
WiredHome 4:178df829d62b 54 void FreeCredentials(credentials * cr)
WiredHome 4:178df829d62b 55 {
WiredHome 4:178df829d62b 56 // first secure them by wiping them out
WiredHome 4:178df829d62b 57 for (int i=0; i<CREDENTIAL_SIZE; i++) {
WiredHome 4:178df829d62b 58 cr->ssid[i] = cr->pass[i] = '*';
WiredHome 4:178df829d62b 59 }
WiredHome 4:178df829d62b 60 // then free the memory
WiredHome 4:178df829d62b 61 free(cr->ssid);
WiredHome 4:178df829d62b 62 free(cr->pass);
WiredHome 4:178df829d62b 63 }
WiredHome 4:178df829d62b 64
WiredHome 4:178df829d62b 65
WiredHome 4:178df829d62b 66 /// Smart-WiFly is a very primitive server using WiFly.
WiredHome 4:178df829d62b 67 ///
WiredHome 4:178df829d62b 68 /// This is a working version, but it is not using the standardized wifly
WiredHome 4:178df829d62b 69 /// library, which would not work for me... I had to make a number
WiredHome 4:178df829d62b 70 /// of changes to get it to work well.
WiredHome 4:178df829d62b 71 ///
WiredHome 4:178df829d62b 72 /// @caution If nothing visits its web page for a long time, it seems
WiredHome 4:178df829d62b 73 /// to disable the interface. Perhaps a sleep timer I'm not spotting?
WiredHome 4:178df829d62b 74 ///
WiredHome 4:178df829d62b 75 /// I created this because I couldn't find one that worked and wanted to
WiredHome 4:178df829d62b 76 /// learn the WiFly module. There are a lot of possible improvements:
WiredHome 4:178df829d62b 77 /// @li I think I'm not using the Socket interface as fully as I should.
WiredHome 4:178df829d62b 78 /// @li I would like it to be faster (the interface from mbed to wifly is
WiredHome 4:178df829d62b 79 /// limited to 230400 baud before it drops chars.
WiredHome 4:178df829d62b 80 /// @li I would like to integrate this with the rtos.
WiredHome 4:178df829d62b 81 /// @li If a page has multiple components (e.g. images), it appears
WiredHome 4:178df829d62b 82 /// unreliable. It doesn't see the request for the extra component.
WiredHome 4:178df829d62b 83 ///
WiredHome 4:178df829d62b 84 /// history:
WiredHome 4:178df829d62b 85 /// @li 20130602 added .txt to the supported types (e.g. robots.txt), so
WiredHome 4:178df829d62b 86 /// revised the credentials to .crd, which is an unsupported type
WiredHome 4:178df829d62b 87 /// so won't be delivered to the user.
WiredHome 4:178df829d62b 88 ///
WiredHome 4:178df829d62b 89 /// @note Copyright &copy; 2013 by Smartware Computing, all rights reserved.
WiredHome 4:178df829d62b 90 /// Individuals may use this application for evaluation or non-commercial
WiredHome 4:178df829d62b 91 /// purposes. Within this restriction, changes may be made to this application
WiredHome 4:178df829d62b 92 /// as long as this copyright notice is retained. The user shall make
WiredHome 4:178df829d62b 93 /// clear that their work is a derived work, and not the original.
WiredHome 4:178df829d62b 94 /// Users of this application and sources accept this application "as is" and
WiredHome 4:178df829d62b 95 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 4:178df829d62b 96 /// using this application - whether real or imagined.
WiredHome 4:178df829d62b 97 ///
WiredHome 4:178df829d62b 98 /// @author David Smart, Smartware Computing
WiredHome 4:178df829d62b 99 ///
WiredHome 4:178df829d62b 100 int main() {
WiredHome 4:178df829d62b 101 credentials cr; // handles the credentials
WiredHome 4:178df829d62b 102
WiredHome 4:178df829d62b 103 pc.baud(460800); // I like a snappy terminal, so crank it up!
WiredHome 4:178df829d62b 104
WiredHome 4:178df829d62b 105 pc.printf("\r\nSmart WiFly 4 - build " __DATE__ " " __TIME__ "\r\n");
WiredHome 4:178df829d62b 106
WiredHome 4:178df829d62b 107 if (!ReadCredentials("/local/user.crd", &cr)) {
WiredHome 4:178df829d62b 108 pc.printf("**** ERROR, no /local/user.crd file was found. ****\r\n");
WiredHome 4:178df829d62b 109 pc.printf(" awaiting watchdog (that means you push the button).\r\n");
WiredHome 4:178df829d62b 110 while(1);
WiredHome 4:178df829d62b 111 }
WiredHome 4:178df829d62b 112
WiredHome 4:178df829d62b 113 // instantiate the WiflyInterface, then release the credentials
WiredHome 4:178df829d62b 114 WiflyInterface wifly(p28, p27, p23, p24, cr.ssid, cr.pass, WPA);
WiredHome 4:178df829d62b 115 FreeCredentials(&cr);
WiredHome 4:178df829d62b 116
WiredHome 4:178df829d62b 117 // start it up as a client of my network
WiredHome 4:178df829d62b 118 wifly.init(); // use DHCP
WiredHome 4:178df829d62b 119
WiredHome 4:178df829d62b 120 wifly.baud(230400); // I like this fast, but it drops chars at 460800
WiredHome 4:178df829d62b 121
WiredHome 4:178df829d62b 122 while (!wifly.connect()) {
WiredHome 4:178df829d62b 123 printf(" ... failed to connect, retrying.\r\n");
WiredHome 4:178df829d62b 124 wifly.reset();
WiredHome 4:178df829d62b 125 wait(1.0);
WiredHome 4:178df829d62b 126 }
WiredHome 4:178df829d62b 127 printf("IP Address is %s\r\n", wifly.getIPAddress());
WiredHome 4:178df829d62b 128
WiredHome 4:178df829d62b 129 // Now let's instantiate the web server - along with a few settings
WiredHome 4:178df829d62b 130 // among which is the file system path to the static pages.
WiredHome 4:178df829d62b 131 #define HTTP_SERVER_PORT 80
WiredHome 4:178df829d62b 132 HTTPServer svr(&wifly, HTTP_SERVER_PORT, "/local/", 30, 10, &pc);
WiredHome 4:178df829d62b 133
WiredHome 4:178df829d62b 134 // But for even more fun, I'm registering a few dynamic pages
WiredHome 7:72b5df2fac93 135 // which you see the handlers for in DynamicPages.cpp.
WiredHome 4:178df829d62b 136 // Here you can see the path to place on the URL.
WiredHome 4:178df829d62b 137 // ex. http://192.168.1.140/dyn
WiredHome 4:178df829d62b 138 svr.RegisterHandler("/dyn", SuperSimpleDynamicPage);
WiredHome 4:178df829d62b 139 svr.RegisterHandler("/dyn1", SimpleDynamicPage);
WiredHome 4:178df829d62b 140 svr.RegisterHandler("/dyn2", SimpleDynamicForm);
WiredHome 4:178df829d62b 141
WiredHome 4:178df829d62b 142 // Let the human know it is ready - if they are watching
WiredHome 4:178df829d62b 143 pc.printf("Waiting for a connection...\r\n");
WiredHome 4:178df829d62b 144 while (true)
WiredHome 4:178df829d62b 145 {
WiredHome 4:178df829d62b 146 svr.Poll(); // unfortunately, this is a blocking process
WiredHome 4:178df829d62b 147 //pc.printf("this %d, longest %d\r\n", thisRun, longestRun);
WiredHome 4:178df829d62b 148 }
WiredHome 4:178df829d62b 149 }
WiredHome 4:178df829d62b 150
WiredHome 4:178df829d62b 151
WiredHome 7:72b5df2fac93 152