Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michielber 0:f615d151a72c 1
Michielber 0:f615d151a72c 2 /*
Michielber 0:f615d151a72c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
Michielber 0:f615d151a72c 4
Michielber 0:f615d151a72c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
Michielber 0:f615d151a72c 6 of this software and associated documentation files (the "Software"), to deal
Michielber 0:f615d151a72c 7 in the Software without restriction, including without limitation the rights
Michielber 0:f615d151a72c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michielber 0:f615d151a72c 9 copies of the Software, and to permit persons to whom the Software is
Michielber 0:f615d151a72c 10 furnished to do so, subject to the following conditions:
Michielber 0:f615d151a72c 11
Michielber 0:f615d151a72c 12 The above copyright notice and this permission notice shall be included in
Michielber 0:f615d151a72c 13 all copies or substantial portions of the Software.
Michielber 0:f615d151a72c 14
Michielber 0:f615d151a72c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michielber 0:f615d151a72c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michielber 0:f615d151a72c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michielber 0:f615d151a72c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Michielber 0:f615d151a72c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Michielber 0:f615d151a72c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michielber 0:f615d151a72c 21 THE SOFTWARE.
Michielber 0:f615d151a72c 22 */
Michielber 0:f615d151a72c 23
Michielber 0:f615d151a72c 24 /** \file
Michielber 0:f615d151a72c 25 MySQL Client header file
Michielber 0:f615d151a72c 26 */
Michielber 0:f615d151a72c 27
Michielber 0:f615d151a72c 28 #ifndef MYSQL_CLIENT_H
Michielber 0:f615d151a72c 29 #define MYSQL_CLIENT_H
Michielber 0:f615d151a72c 30
Michielber 0:f615d151a72c 31 #include "core/net.h"
Michielber 0:f615d151a72c 32 #include "core/netservice.h"
Michielber 0:f615d151a72c 33 #include "api/TCPSocket.h"
Michielber 0:f615d151a72c 34 #include "api/DNSRequest.h"
Michielber 0:f615d151a72c 35 #include "mbed.h"
Michielber 0:f615d151a72c 36
Michielber 0:f615d151a72c 37 #include <string>
Michielber 0:f615d151a72c 38 using std::string;
Michielber 0:f615d151a72c 39
Michielber 0:f615d151a72c 40 #include <map>
Michielber 0:f615d151a72c 41 using std::map;
Michielber 0:f615d151a72c 42
Michielber 0:f615d151a72c 43 typedef unsigned char byte;
Michielber 0:f615d151a72c 44
Michielber 0:f615d151a72c 45 ///MySQL client results
Michielber 0:f615d151a72c 46 enum MySQLResult
Michielber 0:f615d151a72c 47 {
Michielber 0:f615d151a72c 48 MYSQL_OK, ///<Success
Michielber 0:f615d151a72c 49 MYSQL_PROCESSING, ///<Processing
Michielber 0:f615d151a72c 50 MYSQL_PRTCL, ///<Protocol error
Michielber 0:f615d151a72c 51 MYSQL_SETUP, ///<Not properly configured
Michielber 0:f615d151a72c 52 MYSQL_DNS, ///<Could not resolve name
Michielber 0:f615d151a72c 53 MYSQL_AUTHFAILED, ///<Auth failure
Michielber 0:f615d151a72c 54 MYSQL_READY, ///<Ready to send commands
Michielber 0:f615d151a72c 55 MYSQL_SQL, ///<SQL Error
Michielber 0:f615d151a72c 56 MYSQL_TIMEOUT, ///<Connection timeout
Michielber 0:f615d151a72c 57 MYSQL_CONN ///<Connection error
Michielber 0:f615d151a72c 58 };
Michielber 0:f615d151a72c 59
Michielber 0:f615d151a72c 60 ///A MySQL Client
Michielber 0:f615d151a72c 61 /**
Michielber 0:f615d151a72c 62 This MySQL client implements a limited subset of the MySQL internal client/server protocol (including authentication), for server versions 4.1 and newer.
Michielber 0:f615d151a72c 63 */
Michielber 0:f615d151a72c 64 class MySQLClient : protected NetService
Michielber 0:f615d151a72c 65 {
Michielber 0:f615d151a72c 66 public:
Michielber 0:f615d151a72c 67 ///Instantiates the MySQL client
Michielber 0:f615d151a72c 68 MySQLClient();
Michielber 0:f615d151a72c 69 virtual ~MySQLClient();
Michielber 0:f615d151a72c 70
Michielber 0:f615d151a72c 71 //High Level setup functions
Michielber 0:f615d151a72c 72
Michielber 0:f615d151a72c 73 ///Opens a connection to a server
Michielber 0:f615d151a72c 74 /**
Michielber 0:f615d151a72c 75 Opens a connection to the server host using the provided username, password passowrd and selecting database
Michielber 0:f615d151a72c 76 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
Michielber 0:f615d151a72c 77 @param host : server
Michielber 0:f615d151a72c 78 @param user : username
Michielber 0:f615d151a72c 79 @param db : database to use
Michielber 0:f615d151a72c 80 @param pMethod : callback to call on each request completion
Michielber 0:f615d151a72c 81 */
Michielber 0:f615d151a72c 82 MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking
Michielber 0:f615d151a72c 83
Michielber 0:f615d151a72c 84 ///Opens a connection to a server
Michielber 0:f615d151a72c 85 /**
Michielber 0:f615d151a72c 86 Opens a connection to the server host using the provided username, password passowrd and selecting database
Michielber 0:f615d151a72c 87 On completion of this call (and any further one), the callback set in parameter is fired with the result of that command in parameter
Michielber 0:f615d151a72c 88 @param host : server
Michielber 0:f615d151a72c 89 @param user : username
Michielber 0:f615d151a72c 90 @param db : database to use
Michielber 0:f615d151a72c 91 @param pItem : callback's class instance
Michielber 0:f615d151a72c 92 @param pMethod : callback's method to call on each request completion
Michielber 0:f615d151a72c 93 */
Michielber 0:f615d151a72c 94 template<class T>
Michielber 0:f615d151a72c 95 MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking
Michielber 0:f615d151a72c 96 {
Michielber 0:f615d151a72c 97 setOnResult(pItem, pMethod);
Michielber 0:f615d151a72c 98 setup(host, user, password, db);
Michielber 0:f615d151a72c 99 return MYSQL_PROCESSING;
Michielber 0:f615d151a72c 100 }
Michielber 0:f615d151a72c 101
Michielber 0:f615d151a72c 102
Michielber 0:f615d151a72c 103 ///Executes an SQL command
Michielber 0:f615d151a72c 104 /**
Michielber 0:f615d151a72c 105 Executes an SQL request on the SQL server
Michielber 0:f615d151a72c 106 This is a non-blocking function
Michielber 0:f615d151a72c 107 On completion, the callback set in the open function is fired with the result of the command in parameter
Michielber 0:f615d151a72c 108 @param sqlCommand SQL request to execute
Michielber 0:f615d151a72c 109 */
Michielber 0:f615d151a72c 110 MySQLResult sql(string& sqlCommand);
Michielber 0:f615d151a72c 111
Michielber 0:f615d151a72c 112 ///Closes the connection to the server
Michielber 0:f615d151a72c 113 MySQLResult exit();
Michielber 0:f615d151a72c 114
Michielber 0:f615d151a72c 115 void setOnResult( void (*pMethod)(MySQLResult) );
Michielber 0:f615d151a72c 116 class CDummy;
Michielber 0:f615d151a72c 117 template<class T>
Michielber 0:f615d151a72c 118 void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) )
Michielber 0:f615d151a72c 119 {
Michielber 0:f615d151a72c 120 m_pCb = NULL;
Michielber 0:f615d151a72c 121 m_pCbItem = (CDummy*) pItem;
Michielber 0:f615d151a72c 122 m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod;
Michielber 0:f615d151a72c 123 }
Michielber 0:f615d151a72c 124
Michielber 0:f615d151a72c 125 ///Setups timeout
Michielber 0:f615d151a72c 126 /**
Michielber 0:f615d151a72c 127 @param ms : time of connection inactivity in ms after which the request should timeout
Michielber 0:f615d151a72c 128 */
Michielber 0:f615d151a72c 129 void setTimeout(int ms);
Michielber 0:f615d151a72c 130
Michielber 0:f615d151a72c 131 virtual void poll(); //Called by NetServices
Michielber 0:f615d151a72c 132
Michielber 0:f615d151a72c 133 protected:
Michielber 0:f615d151a72c 134 void resetTimeout();
Michielber 0:f615d151a72c 135
Michielber 0:f615d151a72c 136 void init();
Michielber 0:f615d151a72c 137 void close();
Michielber 0:f615d151a72c 138
Michielber 0:f615d151a72c 139 void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary
Michielber 0:f615d151a72c 140 void connect(); //Start Connection
Michielber 0:f615d151a72c 141
Michielber 0:f615d151a72c 142 void handleHandshake();
Michielber 0:f615d151a72c 143 void sendAuth();
Michielber 0:f615d151a72c 144
Michielber 0:f615d151a72c 145 void handleAuthResult();
Michielber 0:f615d151a72c 146 void sendAuth323();
Michielber 0:f615d151a72c 147
Michielber 0:f615d151a72c 148 void sendCommand(byte command, byte* arg, int len);
Michielber 0:f615d151a72c 149 void handleCommandResult();
Michielber 0:f615d151a72c 150
Michielber 0:f615d151a72c 151 void readData(); //Copy to buf
Michielber 0:f615d151a72c 152 void writeData(); //Copy from buf
Michielber 0:f615d151a72c 153
Michielber 0:f615d151a72c 154 void onTCPSocketEvent(TCPSocketEvent e);
Michielber 0:f615d151a72c 155 void onDNSReply(DNSReply r);
Michielber 0:f615d151a72c 156 void onResult(MySQLResult r); //Called when exchange completed or on failure
Michielber 0:f615d151a72c 157 void onTimeout(); //Connection has timed out
Michielber 0:f615d151a72c 158
Michielber 0:f615d151a72c 159 private:
Michielber 0:f615d151a72c 160 CDummy* m_pCbItem;
Michielber 0:f615d151a72c 161 void (CDummy::*m_pCbMeth)(MySQLResult);
Michielber 0:f615d151a72c 162
Michielber 0:f615d151a72c 163 void (*m_pCb)(MySQLResult);
Michielber 0:f615d151a72c 164
Michielber 0:f615d151a72c 165 TCPSocket* m_pTCPSocket;
Michielber 0:f615d151a72c 166
Michielber 0:f615d151a72c 167 Timer m_watchdog;
Michielber 0:f615d151a72c 168 int m_timeout;
Michielber 0:f615d151a72c 169
Michielber 0:f615d151a72c 170 DNSRequest* m_pDnsReq;
Michielber 0:f615d151a72c 171
Michielber 0:f615d151a72c 172 bool m_closed;
Michielber 0:f615d151a72c 173
Michielber 0:f615d151a72c 174 enum MySQLStep
Michielber 0:f615d151a72c 175 {
Michielber 0:f615d151a72c 176 // MYSQL_INIT,
Michielber 0:f615d151a72c 177 MYSQL_HANDSHAKE,
Michielber 0:f615d151a72c 178 MYSQL_AUTH,
Michielber 0:f615d151a72c 179 MYSQL_COMMANDS,
Michielber 0:f615d151a72c 180 MYSQL_CLOSED
Michielber 0:f615d151a72c 181 };
Michielber 0:f615d151a72c 182
Michielber 0:f615d151a72c 183 //Parameters
Michielber 0:f615d151a72c 184 Host m_host;
Michielber 0:f615d151a72c 185
Michielber 0:f615d151a72c 186 string m_user;
Michielber 0:f615d151a72c 187 string m_password;
Michielber 0:f615d151a72c 188 string m_db;
Michielber 0:f615d151a72c 189
Michielber 0:f615d151a72c 190 //Low-level buffers & state-machine
Michielber 0:f615d151a72c 191 MySQLStep m_state;
Michielber 0:f615d151a72c 192
Michielber 0:f615d151a72c 193 byte* m_buf;
Michielber 0:f615d151a72c 194 byte* m_pPos;
Michielber 0:f615d151a72c 195 int m_len;
Michielber 0:f615d151a72c 196 int m_size;
Michielber 0:f615d151a72c 197
Michielber 0:f615d151a72c 198 int m_packetId;
Michielber 0:f615d151a72c 199
Michielber 0:f615d151a72c 200 };
Michielber 0:f615d151a72c 201
Michielber 0:f615d151a72c 202 #endif