Dependents: SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1
services/sql/MySQLClient.h@9:acb9b7d53771, 2010-06-01 (annotated)
- Committer:
- donatien
- Date:
- Tue Jun 01 12:57:28 2010 +0000
- Revision:
- 9:acb9b7d53771
- Parent:
- 8:65b403c38e41
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 8:65b403c38e41 | 1 | |
donatien | 8:65b403c38e41 | 2 | /* |
donatien | 8:65b403c38e41 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 8:65b403c38e41 | 4 | |
donatien | 8:65b403c38e41 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 8:65b403c38e41 | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 8:65b403c38e41 | 7 | in the Software without restriction, including without limitation the rights |
donatien | 8:65b403c38e41 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 8:65b403c38e41 | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 8:65b403c38e41 | 10 | furnished to do so, subject to the following conditions: |
donatien | 8:65b403c38e41 | 11 | |
donatien | 8:65b403c38e41 | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 8:65b403c38e41 | 13 | all copies or substantial portions of the Software. |
donatien | 8:65b403c38e41 | 14 | |
donatien | 8:65b403c38e41 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 8:65b403c38e41 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 8:65b403c38e41 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 8:65b403c38e41 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 8:65b403c38e41 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 8:65b403c38e41 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 8:65b403c38e41 | 21 | THE SOFTWARE. |
donatien | 8:65b403c38e41 | 22 | */ |
donatien | 8:65b403c38e41 | 23 | |
donatien | 8:65b403c38e41 | 24 | #ifndef MYSQL_CLIENT_H |
donatien | 8:65b403c38e41 | 25 | #define MYSQL_CLIENT_H |
donatien | 8:65b403c38e41 | 26 | |
donatien | 8:65b403c38e41 | 27 | #include "if/net/net.h" |
donatien | 8:65b403c38e41 | 28 | #include "api/TcpSocket.h" |
donatien | 8:65b403c38e41 | 29 | #include "api/DnsRequest.h" |
donatien | 8:65b403c38e41 | 30 | #include "mbed.h" |
donatien | 8:65b403c38e41 | 31 | |
donatien | 8:65b403c38e41 | 32 | #include <string> |
donatien | 8:65b403c38e41 | 33 | using std::string; |
donatien | 8:65b403c38e41 | 34 | |
donatien | 8:65b403c38e41 | 35 | #include <map> |
donatien | 8:65b403c38e41 | 36 | using std::map; |
donatien | 8:65b403c38e41 | 37 | |
donatien | 8:65b403c38e41 | 38 | #define MYSQL_TIMEOUT_MS 15000 |
donatien | 8:65b403c38e41 | 39 | #define MYSQL_PORT 3306 |
donatien | 8:65b403c38e41 | 40 | |
donatien | 8:65b403c38e41 | 41 | typedef unsigned char byte; |
donatien | 8:65b403c38e41 | 42 | |
donatien | 8:65b403c38e41 | 43 | enum MySQLResult |
donatien | 8:65b403c38e41 | 44 | { |
donatien | 8:65b403c38e41 | 45 | MYSQL_OK, |
donatien | 8:65b403c38e41 | 46 | MYSQL_PROCESSING, |
donatien | 8:65b403c38e41 | 47 | MYSQL_PRTCL, |
donatien | 8:65b403c38e41 | 48 | MYSQL_SETUP, //Not properly configured |
donatien | 8:65b403c38e41 | 49 | MYSQL_DNS, //Could not resolve name |
donatien | 8:65b403c38e41 | 50 | MYSQL_AUTHFAILED, //Auth failure |
donatien | 8:65b403c38e41 | 51 | MYSQL_READY, //Ready to send commands |
donatien | 8:65b403c38e41 | 52 | MYSQL_SQL, //SQL Error |
donatien | 8:65b403c38e41 | 53 | MYSQL_TIMEOUT, //Connection timeout |
donatien | 8:65b403c38e41 | 54 | MYSQL_CONN //Connection error |
donatien | 8:65b403c38e41 | 55 | }; |
donatien | 8:65b403c38e41 | 56 | |
donatien | 8:65b403c38e41 | 57 | class MySQLClient : protected NetService |
donatien | 8:65b403c38e41 | 58 | { |
donatien | 8:65b403c38e41 | 59 | public: |
donatien | 8:65b403c38e41 | 60 | MySQLClient(); |
donatien | 8:65b403c38e41 | 61 | virtual ~MySQLClient(); |
donatien | 8:65b403c38e41 | 62 | |
donatien | 8:65b403c38e41 | 63 | //High Level setup functions |
donatien | 8:65b403c38e41 | 64 | MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking |
donatien | 8:65b403c38e41 | 65 | template<class T> |
donatien | 8:65b403c38e41 | 66 | MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking |
donatien | 8:65b403c38e41 | 67 | { |
donatien | 8:65b403c38e41 | 68 | setOnResult(pItem, pMethod); |
donatien | 8:65b403c38e41 | 69 | setup(host, user, password, db); |
donatien | 8:65b403c38e41 | 70 | return MYSQL_PROCESSING; |
donatien | 8:65b403c38e41 | 71 | } |
donatien | 8:65b403c38e41 | 72 | |
donatien | 8:65b403c38e41 | 73 | MySQLResult sql(string& sqlCommand); |
donatien | 8:65b403c38e41 | 74 | |
donatien | 8:65b403c38e41 | 75 | MySQLResult exit(); |
donatien | 8:65b403c38e41 | 76 | |
donatien | 8:65b403c38e41 | 77 | void setOnResult( void (*pMethod)(MySQLResult) ); |
donatien | 8:65b403c38e41 | 78 | class CDummy; |
donatien | 8:65b403c38e41 | 79 | template<class T> |
donatien | 8:65b403c38e41 | 80 | void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) ) |
donatien | 8:65b403c38e41 | 81 | { |
donatien | 8:65b403c38e41 | 82 | m_pCb = NULL; |
donatien | 8:65b403c38e41 | 83 | m_pCbItem = (CDummy*) pItem; |
donatien | 8:65b403c38e41 | 84 | m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod; |
donatien | 8:65b403c38e41 | 85 | } |
donatien | 8:65b403c38e41 | 86 | |
donatien | 8:65b403c38e41 | 87 | void setTimeout(int ms); |
donatien | 8:65b403c38e41 | 88 | |
donatien | 8:65b403c38e41 | 89 | virtual void poll(); //Called by NetServices |
donatien | 8:65b403c38e41 | 90 | |
donatien | 8:65b403c38e41 | 91 | protected: |
donatien | 8:65b403c38e41 | 92 | void resetTimeout(); |
donatien | 8:65b403c38e41 | 93 | |
donatien | 8:65b403c38e41 | 94 | void init(); |
donatien | 8:65b403c38e41 | 95 | void close(); |
donatien | 8:65b403c38e41 | 96 | |
donatien | 8:65b403c38e41 | 97 | void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary |
donatien | 8:65b403c38e41 | 98 | void connect(); //Start Connection |
donatien | 8:65b403c38e41 | 99 | |
donatien | 8:65b403c38e41 | 100 | void handleHandshake(); |
donatien | 8:65b403c38e41 | 101 | void sendAuth(); |
donatien | 8:65b403c38e41 | 102 | |
donatien | 8:65b403c38e41 | 103 | void handleAuthResult(); |
donatien | 8:65b403c38e41 | 104 | |
donatien | 8:65b403c38e41 | 105 | void sendCommand(byte command, byte* arg, int len); |
donatien | 8:65b403c38e41 | 106 | void handleCommandResult(); |
donatien | 8:65b403c38e41 | 107 | |
donatien | 8:65b403c38e41 | 108 | void readData(); //Copy to buf |
donatien | 8:65b403c38e41 | 109 | void writeData(); //Copy from buf |
donatien | 8:65b403c38e41 | 110 | |
donatien | 8:65b403c38e41 | 111 | void onTcpSocketEvent(TcpSocketEvent e); |
donatien | 8:65b403c38e41 | 112 | void onDnsReply(DnsReply r); |
donatien | 8:65b403c38e41 | 113 | void onResult(MySQLResult r); //Called when exchange completed or on failure |
donatien | 8:65b403c38e41 | 114 | void onTimeout(); //Connection has timed out |
donatien | 8:65b403c38e41 | 115 | |
donatien | 8:65b403c38e41 | 116 | private: |
donatien | 8:65b403c38e41 | 117 | CDummy* m_pCbItem; |
donatien | 8:65b403c38e41 | 118 | void (CDummy::*m_pCbMeth)(MySQLResult); |
donatien | 8:65b403c38e41 | 119 | |
donatien | 8:65b403c38e41 | 120 | void (*m_pCb)(MySQLResult); |
donatien | 8:65b403c38e41 | 121 | |
donatien | 8:65b403c38e41 | 122 | TcpSocket* m_pTcpSocket; |
donatien | 8:65b403c38e41 | 123 | |
donatien | 8:65b403c38e41 | 124 | Timer m_watchdog; |
donatien | 8:65b403c38e41 | 125 | int m_timeout; |
donatien | 8:65b403c38e41 | 126 | |
donatien | 8:65b403c38e41 | 127 | DnsRequest* m_pDnsReq; |
donatien | 8:65b403c38e41 | 128 | |
donatien | 8:65b403c38e41 | 129 | bool m_closed; |
donatien | 8:65b403c38e41 | 130 | |
donatien | 8:65b403c38e41 | 131 | enum MySQLStep |
donatien | 8:65b403c38e41 | 132 | { |
donatien | 8:65b403c38e41 | 133 | // MYSQL_INIT, |
donatien | 8:65b403c38e41 | 134 | MYSQL_HANDSHAKE, |
donatien | 8:65b403c38e41 | 135 | MYSQL_AUTH, |
donatien | 8:65b403c38e41 | 136 | MYSQL_COMMANDS, |
donatien | 8:65b403c38e41 | 137 | MYSQL_CLOSED |
donatien | 8:65b403c38e41 | 138 | }; |
donatien | 8:65b403c38e41 | 139 | |
donatien | 8:65b403c38e41 | 140 | //Parameters |
donatien | 8:65b403c38e41 | 141 | Host m_host; |
donatien | 8:65b403c38e41 | 142 | |
donatien | 8:65b403c38e41 | 143 | string m_user; |
donatien | 8:65b403c38e41 | 144 | string m_password; |
donatien | 8:65b403c38e41 | 145 | string m_db; |
donatien | 8:65b403c38e41 | 146 | |
donatien | 8:65b403c38e41 | 147 | //Low-level buffers & state-machine |
donatien | 8:65b403c38e41 | 148 | MySQLStep m_state; |
donatien | 8:65b403c38e41 | 149 | |
donatien | 8:65b403c38e41 | 150 | byte* m_buf; |
donatien | 8:65b403c38e41 | 151 | byte* m_pPos; |
donatien | 8:65b403c38e41 | 152 | int m_len; |
donatien | 8:65b403c38e41 | 153 | int m_size; |
donatien | 8:65b403c38e41 | 154 | |
donatien | 8:65b403c38e41 | 155 | int m_packetId; |
donatien | 8:65b403c38e41 | 156 | |
donatien | 8:65b403c38e41 | 157 | }; |
donatien | 8:65b403c38e41 | 158 | |
donatien | 8:65b403c38e41 | 159 | #endif |