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