SQL Client with VodafoneUSBModem
Fork of MySQLClient by
Revision 0:403b85869e53, committed 2010-06-11
- Comitter:
- donatien
- Date:
- Fri Jun 11 16:26:30 2010 +0000
- Child:
- 1:c3a3a0aa03ec
- Commit message:
Changed in this revision
Binary file LPC1768/MySQLClient.ar has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/dbg/dbg.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,73 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +//#ifdef DBG_H +//#define DBG_H + +#ifdef __LWIP_DEBUG +#define __DEBUG +#endif + +#ifdef __DEBUG + +#ifndef __DEBUGSTREAM +#define __DEBUGSTREAM + + +class DebugStream +{ +public: +static void debug(const char* format, ...); +static void release(); +private: + +}; + +#undef DBG +#undef DBG_END +#define DBG DebugStream::debug +#define DBG_END DebugStream::release +#endif + +#else +#undef DBG +#undef DBG_END +#define DBG(...) +#define DBG_END() +#endif + +#ifdef __LWIP_DEBUG +#ifndef __SNPRINTF +#define __SNPRINTF +#include "mbed.h" + +//int snprintf(char *str, int size, const char *format, ...); +#endif +#endif + +#ifdef __LWIP_DEBUG +#undef __DEBUG +#endif + +//#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/mysql/MySQLClient.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,157 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef MYSQL_CLIENT_H +#define MYSQL_CLIENT_H + +#include "if/net/net.h" +#include "api/TCPSocket.h" +#include "api/DNSRequest.h" +#include "mbed.h" + +#include <string> +using std::string; + +#include <map> +using std::map; + +typedef unsigned char byte; + +enum MySQLResult +{ + MYSQL_OK, + MYSQL_PROCESSING, + MYSQL_PRTCL, + MYSQL_SETUP, //Not properly configured + MYSQL_DNS, //Could not resolve name + MYSQL_AUTHFAILED, //Auth failure + MYSQL_READY, //Ready to send commands + MYSQL_SQL, //SQL Error + MYSQL_TIMEOUT, //Connection timeout + MYSQL_CONN //Connection error +}; + +class MySQLClient : protected NetService +{ +public: + MySQLClient(); + virtual ~MySQLClient(); + + //High Level setup functions + MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking + template<class T> + MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking + { + setOnResult(pItem, pMethod); + setup(host, user, password, db); + return MYSQL_PROCESSING; + } + + MySQLResult sql(string& sqlCommand); + + MySQLResult exit(); + + void setOnResult( void (*pMethod)(MySQLResult) ); + class CDummy; + template<class T> + void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) ) + { + m_pCb = NULL; + m_pCbItem = (CDummy*) pItem; + m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod; + } + + void setTimeout(int ms); + + virtual void poll(); //Called by NetServices + +protected: + void resetTimeout(); + + void init(); + void close(); + + void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary + void connect(); //Start Connection + + void handleHandshake(); + void sendAuth(); + + void handleAuthResult(); + void sendAuth323(); + + void sendCommand(byte command, byte* arg, int len); + void handleCommandResult(); + + void readData(); //Copy to buf + void writeData(); //Copy from buf + + void onTCPSocketEvent(TCPSocketEvent e); + void onDNSReply(DNSReply r); + void onResult(MySQLResult r); //Called when exchange completed or on failure + void onTimeout(); //Connection has timed out + +private: + CDummy* m_pCbItem; + void (CDummy::*m_pCbMeth)(MySQLResult); + + void (*m_pCb)(MySQLResult); + + TCPSocket* m_pTCPSocket; + + Timer m_watchdog; + int m_timeout; + + DNSRequest* m_pDnsReq; + + bool m_closed; + + enum MySQLStep + { + // MYSQL_INIT, + MYSQL_HANDSHAKE, + MYSQL_AUTH, + MYSQL_COMMANDS, + MYSQL_CLOSED + }; + + //Parameters + Host m_host; + + string m_user; + string m_password; + string m_db; + + //Low-level buffers & state-machine + MySQLStep m_state; + + byte* m_buf; + byte* m_pPos; + int m_len; + int m_size; + + int m_packetId; + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/mysql/mycrypt.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,6 @@ +#ifndef MYCRYPT_H +#define MYCRYPT_H + +void scramble_323(char *to, const char *message, const char *password); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/mysql/sha1.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,145 @@ +/** + * \file sha1.h + * + * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef POLARSSL_SHA1_H +#define POLARSSL_SHA1_H + +/** + * \brief SHA-1 context structure + */ +typedef struct +{ + unsigned long total[2]; /*!< number of bytes processed */ + unsigned long state[5]; /*!< intermediate digest state */ + unsigned char buffer[64]; /*!< data block being processed */ + + unsigned char ipad[64]; /*!< HMAC: inner padding */ + unsigned char opad[64]; /*!< HMAC: outer padding */ +} +sha1_context; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief SHA-1 context setup + * + * \param ctx context to be initialized + */ +void sha1_starts( sha1_context *ctx ); + +/** + * \brief SHA-1 process buffer + * + * \param ctx SHA-1 context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen ); + +/** + * \brief SHA-1 final digest + * + * \param ctx SHA-1 context + * \param output SHA-1 checksum result + */ +void sha1_finish( sha1_context *ctx, unsigned char output[20] ); + +/** + * \brief Output = SHA-1( input buffer ) + * + * \param input buffer holding the data + * \param ilen length of the input data + * \param output SHA-1 checksum result + */ +void sha1( const unsigned char *input, int ilen, unsigned char output[20] ); + +#if 0 //No need for that +/** + * \brief Output = SHA-1( file contents ) + * + * \param path input file name + * \param output SHA-1 checksum result + * + * \return 0 if successful, 1 if fopen failed, + * or 2 if fread failed + */ +int sha1_file( const char *path, unsigned char output[20] ); +#endif + +/** + * \brief SHA-1 HMAC context setup + * + * \param ctx HMAC context to be initialized + * \param key HMAC secret key + * \param keylen length of the HMAC key + */ +void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen ); + +/** + * \brief SHA-1 HMAC process buffer + * + * \param ctx HMAC context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen ); + +/** + * \brief SHA-1 HMAC final digest + * + * \param ctx HMAC context + * \param output SHA-1 HMAC checksum result + */ +void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); + +/** + * \brief SHA-1 HMAC context reset + * + * \param ctx HMAC context to be reset + */ +void sha1_hmac_reset( sha1_context *ctx ); + +/** + * \brief Output = HMAC-SHA-1( hmac key, input buffer ) + * + * \param key HMAC secret key + * \param keylen length of the HMAC key + * \param input buffer holding the data + * \param ilen length of the input data + * \param output HMAC-SHA-1 result + */ +void sha1_hmac( const unsigned char *key, int keylen, + const unsigned char *input, int ilen, + unsigned char output[20] ); + +/** + * \brief Checkup routine + * + * \return 0 if successful, or 1 if the test failed + */ +int sha1_self_test( int verbose ); + +#ifdef __cplusplus +} +#endif + +#endif /* sha1.h */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/mysql/sha1config.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,6 @@ +#ifndef SHA1CONFIG_H +#define SHA1CONFIG_H + +#define POLARSSL_SHA1_C 1 + +#endif
Binary file LPC2368/MySQLClient.ar has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/dbg/dbg.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,73 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +//#ifdef DBG_H +//#define DBG_H + +#ifdef __LWIP_DEBUG +#define __DEBUG +#endif + +#ifdef __DEBUG + +#ifndef __DEBUGSTREAM +#define __DEBUGSTREAM + + +class DebugStream +{ +public: +static void debug(const char* format, ...); +static void release(); +private: + +}; + +#undef DBG +#undef DBG_END +#define DBG DebugStream::debug +#define DBG_END DebugStream::release +#endif + +#else +#undef DBG +#undef DBG_END +#define DBG(...) +#define DBG_END() +#endif + +#ifdef __LWIP_DEBUG +#ifndef __SNPRINTF +#define __SNPRINTF +#include "mbed.h" + +//int snprintf(char *str, int size, const char *format, ...); +#endif +#endif + +#ifdef __LWIP_DEBUG +#undef __DEBUG +#endif + +//#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/mysql/MySQLClient.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,157 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef MYSQL_CLIENT_H +#define MYSQL_CLIENT_H + +#include "if/net/net.h" +#include "api/TCPSocket.h" +#include "api/DNSRequest.h" +#include "mbed.h" + +#include <string> +using std::string; + +#include <map> +using std::map; + +typedef unsigned char byte; + +enum MySQLResult +{ + MYSQL_OK, + MYSQL_PROCESSING, + MYSQL_PRTCL, + MYSQL_SETUP, //Not properly configured + MYSQL_DNS, //Could not resolve name + MYSQL_AUTHFAILED, //Auth failure + MYSQL_READY, //Ready to send commands + MYSQL_SQL, //SQL Error + MYSQL_TIMEOUT, //Connection timeout + MYSQL_CONN //Connection error +}; + +class MySQLClient : protected NetService +{ +public: + MySQLClient(); + virtual ~MySQLClient(); + + //High Level setup functions + MySQLResult open(Host& host, const string& user, const string& password, const string& db, void (*pMethod)(MySQLResult)); //Non blocking + template<class T> + MySQLResult open(Host& host, const string& user, const string& password, const string& db, T* pItem, void (T::*pMethod)(MySQLResult)) //Non blocking + { + setOnResult(pItem, pMethod); + setup(host, user, password, db); + return MYSQL_PROCESSING; + } + + MySQLResult sql(string& sqlCommand); + + MySQLResult exit(); + + void setOnResult( void (*pMethod)(MySQLResult) ); + class CDummy; + template<class T> + void setOnResult( T* pItem, void (T::*pMethod)(MySQLResult) ) + { + m_pCb = NULL; + m_pCbItem = (CDummy*) pItem; + m_pCbMeth = (void (CDummy::*)(MySQLResult)) pMethod; + } + + void setTimeout(int ms); + + virtual void poll(); //Called by NetServices + +protected: + void resetTimeout(); + + void init(); + void close(); + + void setup(Host& host, const string& user, const string& password, const string& db); //Setup connection, make DNS Req if necessary + void connect(); //Start Connection + + void handleHandshake(); + void sendAuth(); + + void handleAuthResult(); + void sendAuth323(); + + void sendCommand(byte command, byte* arg, int len); + void handleCommandResult(); + + void readData(); //Copy to buf + void writeData(); //Copy from buf + + void onTCPSocketEvent(TCPSocketEvent e); + void onDNSReply(DNSReply r); + void onResult(MySQLResult r); //Called when exchange completed or on failure + void onTimeout(); //Connection has timed out + +private: + CDummy* m_pCbItem; + void (CDummy::*m_pCbMeth)(MySQLResult); + + void (*m_pCb)(MySQLResult); + + TCPSocket* m_pTCPSocket; + + Timer m_watchdog; + int m_timeout; + + DNSRequest* m_pDnsReq; + + bool m_closed; + + enum MySQLStep + { + // MYSQL_INIT, + MYSQL_HANDSHAKE, + MYSQL_AUTH, + MYSQL_COMMANDS, + MYSQL_CLOSED + }; + + //Parameters + Host m_host; + + string m_user; + string m_password; + string m_db; + + //Low-level buffers & state-machine + MySQLStep m_state; + + byte* m_buf; + byte* m_pPos; + int m_len; + int m_size; + + int m_packetId; + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/mysql/mycrypt.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,6 @@ +#ifndef MYCRYPT_H +#define MYCRYPT_H + +void scramble_323(char *to, const char *message, const char *password); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/mysql/sha1.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,145 @@ +/** + * \file sha1.h + * + * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef POLARSSL_SHA1_H +#define POLARSSL_SHA1_H + +/** + * \brief SHA-1 context structure + */ +typedef struct +{ + unsigned long total[2]; /*!< number of bytes processed */ + unsigned long state[5]; /*!< intermediate digest state */ + unsigned char buffer[64]; /*!< data block being processed */ + + unsigned char ipad[64]; /*!< HMAC: inner padding */ + unsigned char opad[64]; /*!< HMAC: outer padding */ +} +sha1_context; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief SHA-1 context setup + * + * \param ctx context to be initialized + */ +void sha1_starts( sha1_context *ctx ); + +/** + * \brief SHA-1 process buffer + * + * \param ctx SHA-1 context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen ); + +/** + * \brief SHA-1 final digest + * + * \param ctx SHA-1 context + * \param output SHA-1 checksum result + */ +void sha1_finish( sha1_context *ctx, unsigned char output[20] ); + +/** + * \brief Output = SHA-1( input buffer ) + * + * \param input buffer holding the data + * \param ilen length of the input data + * \param output SHA-1 checksum result + */ +void sha1( const unsigned char *input, int ilen, unsigned char output[20] ); + +#if 0 //No need for that +/** + * \brief Output = SHA-1( file contents ) + * + * \param path input file name + * \param output SHA-1 checksum result + * + * \return 0 if successful, 1 if fopen failed, + * or 2 if fread failed + */ +int sha1_file( const char *path, unsigned char output[20] ); +#endif + +/** + * \brief SHA-1 HMAC context setup + * + * \param ctx HMAC context to be initialized + * \param key HMAC secret key + * \param keylen length of the HMAC key + */ +void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen ); + +/** + * \brief SHA-1 HMAC process buffer + * + * \param ctx HMAC context + * \param input buffer holding the data + * \param ilen length of the input data + */ +void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen ); + +/** + * \brief SHA-1 HMAC final digest + * + * \param ctx HMAC context + * \param output SHA-1 HMAC checksum result + */ +void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); + +/** + * \brief SHA-1 HMAC context reset + * + * \param ctx HMAC context to be reset + */ +void sha1_hmac_reset( sha1_context *ctx ); + +/** + * \brief Output = HMAC-SHA-1( hmac key, input buffer ) + * + * \param key HMAC secret key + * \param keylen length of the HMAC key + * \param input buffer holding the data + * \param ilen length of the input data + * \param output HMAC-SHA-1 result + */ +void sha1_hmac( const unsigned char *key, int keylen, + const unsigned char *input, int ilen, + unsigned char output[20] ); + +/** + * \brief Checkup routine + * + * \return 0 if successful, or 1 if the test failed + */ +int sha1_self_test( int verbose ); + +#ifdef __cplusplus +} +#endif + +#endif /* sha1.h */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/mysql/sha1config.h Fri Jun 11 16:26:30 2010 +0000 @@ -0,0 +1,6 @@ +#ifndef SHA1CONFIG_H +#define SHA1CONFIG_H + +#define POLARSSL_SHA1_C 1 + +#endif