Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Tue Nov 09 20:54:15 2010 +0000
Revision:
0:ac1725ba162c

        

Who changed what in which revision?

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