Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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