This version has the index file and data pages on a SD card (512Mbyte) it does not suffer from the autorun problem when directly writtin to the mbed flash memory It makes readings from one solar panel for open and loaded voltages every 'interval' seconds Every readingsPerPage a new web page is created and indexed on a readings web page Activty is shown by the flashing blue led (0.5s) means it is connected and output via the serial over usb port. Data is preserved on subsequent power ups by incrementing file number. PMR 15/9/10 */

Dependencies:   EthernetNetIf NTPClient_NetServices mbed SDFileSystem

Committer:
pmr1
Date:
Sat Sep 18 13:31:41 2010 +0000
Revision:
0:d6b2d5c4c48f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmr1 0:d6b2d5c4c48f 1
pmr1 0:d6b2d5c4c48f 2 /*
pmr1 0:d6b2d5c4c48f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
pmr1 0:d6b2d5c4c48f 4
pmr1 0:d6b2d5c4c48f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
pmr1 0:d6b2d5c4c48f 6 of this software and associated documentation files (the "Software"), to deal
pmr1 0:d6b2d5c4c48f 7 in the Software without restriction, including without limitation the rights
pmr1 0:d6b2d5c4c48f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pmr1 0:d6b2d5c4c48f 9 copies of the Software, and to permit persons to whom the Software is
pmr1 0:d6b2d5c4c48f 10 furnished to do so, subject to the following conditions:
pmr1 0:d6b2d5c4c48f 11
pmr1 0:d6b2d5c4c48f 12 The above copyright notice and this permission notice shall be included in
pmr1 0:d6b2d5c4c48f 13 all copies or substantial portions of the Software.
pmr1 0:d6b2d5c4c48f 14
pmr1 0:d6b2d5c4c48f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pmr1 0:d6b2d5c4c48f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pmr1 0:d6b2d5c4c48f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pmr1 0:d6b2d5c4c48f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pmr1 0:d6b2d5c4c48f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pmr1 0:d6b2d5c4c48f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pmr1 0:d6b2d5c4c48f 21 THE SOFTWARE.
pmr1 0:d6b2d5c4c48f 22 */
pmr1 0:d6b2d5c4c48f 23
pmr1 0:d6b2d5c4c48f 24 #include "FSHandler.h"
pmr1 0:d6b2d5c4c48f 25
pmr1 0:d6b2d5c4c48f 26 //#define __DEBUG
pmr1 0:d6b2d5c4c48f 27 #include "dbg/dbg.h"
pmr1 0:d6b2d5c4c48f 28
pmr1 0:d6b2d5c4c48f 29 #define CHUNK_SIZE 128
pmr1 0:d6b2d5c4c48f 30
pmr1 0:d6b2d5c4c48f 31 #define DEFAULT_PAGE "/index.htm"
pmr1 0:d6b2d5c4c48f 32
pmr1 0:d6b2d5c4c48f 33 FSHandler::FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false)
pmr1 0:d6b2d5c4c48f 34 {}
pmr1 0:d6b2d5c4c48f 35
pmr1 0:d6b2d5c4c48f 36 FSHandler::~FSHandler()
pmr1 0:d6b2d5c4c48f 37 {
pmr1 0:d6b2d5c4c48f 38 if(m_fp)
pmr1 0:d6b2d5c4c48f 39 fclose(m_fp);
pmr1 0:d6b2d5c4c48f 40 DBG("\r\nHandler destroyed\r\n");
pmr1 0:d6b2d5c4c48f 41 }
pmr1 0:d6b2d5c4c48f 42
pmr1 0:d6b2d5c4c48f 43 //static init
pmr1 0:d6b2d5c4c48f 44 map<string,string> FSHandler::m_lFsPath = map<string,string>();
pmr1 0:d6b2d5c4c48f 45
pmr1 0:d6b2d5c4c48f 46 void FSHandler::mount(const string& fsPath, const string& rootPath)
pmr1 0:d6b2d5c4c48f 47 {
pmr1 0:d6b2d5c4c48f 48 m_lFsPath[rootPath]=fsPath;
pmr1 0:d6b2d5c4c48f 49 }
pmr1 0:d6b2d5c4c48f 50
pmr1 0:d6b2d5c4c48f 51 void FSHandler::doGet()
pmr1 0:d6b2d5c4c48f 52 {
pmr1 0:d6b2d5c4c48f 53 DBG("\r\nIn FSHandler::doGet() - rootPath=%s, path=%s\r\n", rootPath().c_str(), path().c_str());
pmr1 0:d6b2d5c4c48f 54 //FIXME: Translate path to local/path
pmr1 0:d6b2d5c4c48f 55 string checkedRootPath = rootPath();
pmr1 0:d6b2d5c4c48f 56 if(checkedRootPath.empty())
pmr1 0:d6b2d5c4c48f 57 checkedRootPath="/";
pmr1 0:d6b2d5c4c48f 58 string filePath = m_lFsPath[checkedRootPath];
pmr1 0:d6b2d5c4c48f 59 if (path().size() > 1)
pmr1 0:d6b2d5c4c48f 60 {
pmr1 0:d6b2d5c4c48f 61 filePath += path();
pmr1 0:d6b2d5c4c48f 62 }
pmr1 0:d6b2d5c4c48f 63 else
pmr1 0:d6b2d5c4c48f 64 {
pmr1 0:d6b2d5c4c48f 65 filePath += DEFAULT_PAGE;
pmr1 0:d6b2d5c4c48f 66 }
pmr1 0:d6b2d5c4c48f 67
pmr1 0:d6b2d5c4c48f 68 DBG("Trying to open %s\n", filePath.c_str());
pmr1 0:d6b2d5c4c48f 69
pmr1 0:d6b2d5c4c48f 70 m_fp = fopen(filePath.c_str(), "r"); //FIXME: if null, error 404
pmr1 0:d6b2d5c4c48f 71
pmr1 0:d6b2d5c4c48f 72 if(!m_fp)
pmr1 0:d6b2d5c4c48f 73 {
pmr1 0:d6b2d5c4c48f 74 m_err404 = true;
pmr1 0:d6b2d5c4c48f 75 setErrCode(404);
pmr1 0:d6b2d5c4c48f 76 const char* msg = "File not found.";
pmr1 0:d6b2d5c4c48f 77 setContentLen(strlen(msg));
pmr1 0:d6b2d5c4c48f 78 respHeaders()["Content-Type"] = "text/html";
pmr1 0:d6b2d5c4c48f 79 respHeaders()["Connection"] = "close";
pmr1 0:d6b2d5c4c48f 80 writeData(msg,strlen(msg)); //Only send header
pmr1 0:d6b2d5c4c48f 81 DBG("\r\nExit FSHandler::doGet() w Error 404\r\n");
pmr1 0:d6b2d5c4c48f 82 return;
pmr1 0:d6b2d5c4c48f 83 }
pmr1 0:d6b2d5c4c48f 84
pmr1 0:d6b2d5c4c48f 85 //Seek EOF to get length
pmr1 0:d6b2d5c4c48f 86 fseek(m_fp, 0, SEEK_END);
pmr1 0:d6b2d5c4c48f 87 setContentLen( ftell(m_fp) );
pmr1 0:d6b2d5c4c48f 88 fseek(m_fp, 0, SEEK_SET); //Goto SOF
pmr1 0:d6b2d5c4c48f 89
pmr1 0:d6b2d5c4c48f 90 respHeaders()["Connection"] = "close";
pmr1 0:d6b2d5c4c48f 91 onWriteable();
pmr1 0:d6b2d5c4c48f 92 DBG("\r\nExit SimpleHandler::doGet()\r\n");
pmr1 0:d6b2d5c4c48f 93 }
pmr1 0:d6b2d5c4c48f 94
pmr1 0:d6b2d5c4c48f 95 void FSHandler::doPost()
pmr1 0:d6b2d5c4c48f 96 {
pmr1 0:d6b2d5c4c48f 97
pmr1 0:d6b2d5c4c48f 98 }
pmr1 0:d6b2d5c4c48f 99
pmr1 0:d6b2d5c4c48f 100 void FSHandler::doHead()
pmr1 0:d6b2d5c4c48f 101 {
pmr1 0:d6b2d5c4c48f 102
pmr1 0:d6b2d5c4c48f 103 }
pmr1 0:d6b2d5c4c48f 104
pmr1 0:d6b2d5c4c48f 105 void FSHandler::onReadable() //Data has been read
pmr1 0:d6b2d5c4c48f 106 {
pmr1 0:d6b2d5c4c48f 107
pmr1 0:d6b2d5c4c48f 108 }
pmr1 0:d6b2d5c4c48f 109
pmr1 0:d6b2d5c4c48f 110 void FSHandler::onWriteable() //Data has been written & buf is free
pmr1 0:d6b2d5c4c48f 111 {
pmr1 0:d6b2d5c4c48f 112 DBG("\r\nFSHandler::onWriteable() event\r\n");
pmr1 0:d6b2d5c4c48f 113 if(m_err404)
pmr1 0:d6b2d5c4c48f 114 {
pmr1 0:d6b2d5c4c48f 115 //Error has been served, now exit
pmr1 0:d6b2d5c4c48f 116 close();
pmr1 0:d6b2d5c4c48f 117 return;
pmr1 0:d6b2d5c4c48f 118 }
pmr1 0:d6b2d5c4c48f 119
pmr1 0:d6b2d5c4c48f 120 static char rBuf[CHUNK_SIZE];
pmr1 0:d6b2d5c4c48f 121 while(true)
pmr1 0:d6b2d5c4c48f 122 {
pmr1 0:d6b2d5c4c48f 123 int len = fread(rBuf, 1, CHUNK_SIZE, m_fp);
pmr1 0:d6b2d5c4c48f 124 if(len>0)
pmr1 0:d6b2d5c4c48f 125 {
pmr1 0:d6b2d5c4c48f 126 int writtenLen = writeData(rBuf, len);
pmr1 0:d6b2d5c4c48f 127 if(writtenLen < 0) //Socket error
pmr1 0:d6b2d5c4c48f 128 {
pmr1 0:d6b2d5c4c48f 129 DBG("FSHandler: Socket error %d\n", writtenLen);
pmr1 0:d6b2d5c4c48f 130 if(writtenLen == TCPSOCKET_MEM)
pmr1 0:d6b2d5c4c48f 131 {
pmr1 0:d6b2d5c4c48f 132 fseek(m_fp, -len, SEEK_CUR);
pmr1 0:d6b2d5c4c48f 133 return; //Wait for the queued TCP segments to be transmitted
pmr1 0:d6b2d5c4c48f 134 }
pmr1 0:d6b2d5c4c48f 135 else
pmr1 0:d6b2d5c4c48f 136 {
pmr1 0:d6b2d5c4c48f 137 //This is a critical error
pmr1 0:d6b2d5c4c48f 138 close();
pmr1 0:d6b2d5c4c48f 139 return;
pmr1 0:d6b2d5c4c48f 140 }
pmr1 0:d6b2d5c4c48f 141 }
pmr1 0:d6b2d5c4c48f 142 else if(writtenLen < len) //Short write, socket's buffer is full
pmr1 0:d6b2d5c4c48f 143 {
pmr1 0:d6b2d5c4c48f 144 fseek(m_fp, writtenLen - len, SEEK_CUR);
pmr1 0:d6b2d5c4c48f 145 return;
pmr1 0:d6b2d5c4c48f 146 }
pmr1 0:d6b2d5c4c48f 147 }
pmr1 0:d6b2d5c4c48f 148 else
pmr1 0:d6b2d5c4c48f 149 {
pmr1 0:d6b2d5c4c48f 150 close(); //Data written, we can close the connection
pmr1 0:d6b2d5c4c48f 151 return;
pmr1 0:d6b2d5c4c48f 152 }
pmr1 0:d6b2d5c4c48f 153 }
pmr1 0:d6b2d5c4c48f 154 }
pmr1 0:d6b2d5c4c48f 155
pmr1 0:d6b2d5c4c48f 156 void FSHandler::onClose() //Connection is closing
pmr1 0:d6b2d5c4c48f 157 {
pmr1 0:d6b2d5c4c48f 158 if(m_fp)
pmr1 0:d6b2d5c4c48f 159 fclose(m_fp);
pmr1 0:d6b2d5c4c48f 160 }