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