Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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