Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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