This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

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