Michael Spencer / Mbed 2 deprecated LaOS

Dependencies:   mbed

Committer:
Michael J. Spencer
Date:
Wed Mar 05 06:14:02 2014 -0800
Revision:
1:f5ac63519541
Initial commit.

Who changed what in which revision?

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