SQL Client with VodafoneUSBModem

Fork of MySQLClient by Donatien Garnier

Committer:
loc_tran17
Date:
Fri Nov 22 09:16:20 2013 +0000
Revision:
7:f06820c3b8e8
Child:
8:cdb6d1236f37
Chang to try

Who changed what in which revision?

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