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