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 #pragma diag_remark 1293
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 "RPCHandler.h"
Yann 0:f30e2135b0db 25 #include "rpc.h"
Yann 0:f30e2135b0db 26 #include "url.h"
Yann 0:f30e2135b0db 27
Yann 0:f30e2135b0db 28 //#define __DEBUG
Yann 0:f30e2135b0db 29 #include "dbg/dbg.h"
Yann 0:f30e2135b0db 30
Yann 0:f30e2135b0db 31 #define RPC_DATA_LEN 128
Yann 0:f30e2135b0db 32
Yann 0:f30e2135b0db 33 RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
Yann 0:f30e2135b0db 34 {}
Yann 0:f30e2135b0db 35
Yann 0:f30e2135b0db 36 RPCHandler::~RPCHandler()
Yann 0:f30e2135b0db 37 {
Yann 0:f30e2135b0db 38 DBG("\r\nHandler destroyed\r\n");
Yann 0:f30e2135b0db 39 }
Yann 0:f30e2135b0db 40
Yann 0:f30e2135b0db 41 void RPCHandler::doGet()
Yann 0:f30e2135b0db 42 {
Yann 0:f30e2135b0db 43 DBG("\r\nIn RPCHandler::doGet()\r\n");
Yann 0:f30e2135b0db 44 char resp[RPC_DATA_LEN] = {0};
Yann 0:f30e2135b0db 45 char req[RPC_DATA_LEN] = {0};
Yann 0:f30e2135b0db 46
Yann 0:f30e2135b0db 47 DBG("\r\nPath : %s\r\n", path().c_str());
Yann 0:f30e2135b0db 48 DBG("\r\nRoot Path : %s\r\n", rootPath().c_str());
Yann 0:f30e2135b0db 49
Yann 0:f30e2135b0db 50 //Remove path
Yann 0:f30e2135b0db 51 strncpy(req, path().c_str(), RPC_DATA_LEN-1);
Yann 0:f30e2135b0db 52 DBG("\r\nRPC req : %s\r\n", req);
Yann 0:f30e2135b0db 53
Yann 0:f30e2135b0db 54 //Remove %20, +, from req
Yann 0:f30e2135b0db 55 cleanReq(req);
Yann 0:f30e2135b0db 56 DBG("\r\nRPC req : %s\r\n", req);
Yann 0:f30e2135b0db 57
Yann 0:f30e2135b0db 58 //Do RPC Call
Yann 0:f30e2135b0db 59 mbed::rpc(req, resp); //FIXME: Use bool result
Yann 0:f30e2135b0db 60
Yann 0:f30e2135b0db 61 //Response
Yann 0:f30e2135b0db 62 setContentLen( strlen(resp) );
Yann 0:f30e2135b0db 63
Yann 0:f30e2135b0db 64 //Make sure that the browser won't cache this request
Yann 0:f30e2135b0db 65 respHeaders()["Cache-control"]="no-cache;no-store";
Yann 0:f30e2135b0db 66 // respHeaders()["Cache-control"]="no-store";
Yann 0:f30e2135b0db 67 respHeaders()["Pragma"]="no-cache";
Yann 0:f30e2135b0db 68 respHeaders()["Expires"]="0";
Yann 0:f30e2135b0db 69
Yann 0:f30e2135b0db 70 //Write data
Yann 0:f30e2135b0db 71 respHeaders()["Connection"] = "close";
Yann 0:f30e2135b0db 72 writeData(resp, strlen(resp));
Yann 0:f30e2135b0db 73 DBG("\r\nExit RPCHandler::doGet()\r\n");
Yann 0:f30e2135b0db 74 }
Yann 0:f30e2135b0db 75
Yann 0:f30e2135b0db 76 void RPCHandler::doPost()
Yann 0:f30e2135b0db 77 {
Yann 0:f30e2135b0db 78
Yann 0:f30e2135b0db 79 }
Yann 0:f30e2135b0db 80
Yann 0:f30e2135b0db 81 void RPCHandler::doHead()
Yann 0:f30e2135b0db 82 {
Yann 0:f30e2135b0db 83
Yann 0:f30e2135b0db 84 }
Yann 0:f30e2135b0db 85
Yann 0:f30e2135b0db 86
Yann 0:f30e2135b0db 87 void RPCHandler::onReadable() //Data has been read
Yann 0:f30e2135b0db 88 {
Yann 0:f30e2135b0db 89
Yann 0:f30e2135b0db 90 }
Yann 0:f30e2135b0db 91
Yann 0:f30e2135b0db 92 void RPCHandler::onWriteable() //Data has been written & buf is free
Yann 0:f30e2135b0db 93 {
Yann 0:f30e2135b0db 94 DBG("\r\nRPCHandler::onWriteable() event\r\n");
Yann 0:f30e2135b0db 95 close(); //Data written, we can close the connection
Yann 0:f30e2135b0db 96 }
Yann 0:f30e2135b0db 97
Yann 0:f30e2135b0db 98 void RPCHandler::onClose() //Connection is closing
Yann 0:f30e2135b0db 99 {
Yann 0:f30e2135b0db 100 //Nothing to do
Yann 0:f30e2135b0db 101 }
Yann 0:f30e2135b0db 102
Yann 0:f30e2135b0db 103 void RPCHandler::cleanReq(char* data)
Yann 0:f30e2135b0db 104 {
Yann 0:f30e2135b0db 105 char* decoded = url_decode(data);
Yann 0:f30e2135b0db 106 strcpy(data, decoded);
Yann 0:f30e2135b0db 107 free(decoded);
Yann 0:f30e2135b0db 108 /*
Yann 0:f30e2135b0db 109 char* p;
Yann 0:f30e2135b0db 110 static const char* lGarbage[2] = {"%20", "+"};
Yann 0:f30e2135b0db 111 for(int i = 0; i < 2; i++)
Yann 0:f30e2135b0db 112 {
Yann 0:f30e2135b0db 113 while( p = strstr(data, lGarbage[i]) )
Yann 0:f30e2135b0db 114 {
Yann 0:f30e2135b0db 115 memset((void*) p, ' ', strlen(lGarbage[i]));
Yann 0:f30e2135b0db 116 }
Yann 0:f30e2135b0db 117 }
Yann 0:f30e2135b0db 118 */
Yann 0:f30e2135b0db 119 }
Yann 0:f30e2135b0db 120
Yann 0:f30e2135b0db 121