|
| 1 | +/* |
| 2 | + Copyright (c) 2012, 2016 Oracle and/or its affiliates. All rights reserved. |
| 3 | +
|
| 4 | + This program is free software; you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation; version 2 of the License. |
| 7 | +
|
| 8 | + This program is distributed in the hope that it will be useful, |
| 9 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + GNU General Public License for more details. |
| 12 | +
|
| 13 | + You should have received a copy of the GNU General Public License |
| 14 | + along with this program; if not, write to the Free Software |
| 15 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | +
|
| 17 | + MySQL_Connection.cpp - Library for communicating with a MySQL Server over |
| 18 | + Ethernet. (formerly mysql.cpp) |
| 19 | +
|
| 20 | + Change History: |
| 21 | +
|
| 22 | + Version 1.0.0a Created by Dr. Charles A. Bell, April 2012. |
| 23 | + Version 1.0.0b Updated by Dr. Charles A. Bell, October 2013. |
| 24 | + Version 1.0.1b Updated by Dr. Charles A. Bell, February 2014. |
| 25 | + Version 1.0.2b Updated by Dr. Charles A. Bell, April 2014. |
| 26 | + Version 1.0.3rc Updated by Dr. Charles A. Bell, March 2015. |
| 27 | + Version 1.0.4ga Updated by Dr. Charles A. Bell, July 2015. |
| 28 | + Version 1.1.0a Created by Dr. Charles A. Bell, January 2016. |
| 29 | +*/ |
| 30 | +#include <Arduino.h> |
| 31 | +#include <MySQL_Connection.h> |
| 32 | +#include <MySQL_Encrypt_Sha1.h> |
| 33 | + |
| 34 | +#define MAX_CONNECT_ATTEMPTS 3 |
| 35 | + |
| 36 | +const char CONNECTED[] PROGMEM = "Connected to server version "; |
| 37 | +const char DISCONNECTED[] PROGMEM = "Disconnected."; |
| 38 | + |
| 39 | +/* |
| 40 | + connect - Connect to a MySQL server. |
| 41 | +
|
| 42 | + This method is used to connect to a MySQL server. It will attempt to |
| 43 | + connect to the server as a client retrying up to MAX_CONNECT_ATTEMPTS. |
| 44 | + This permits the possibility of longer than normal network lag times |
| 45 | + for wireless networks. You can adjust MAX_CONNECT_ATTEMPTS to suit |
| 46 | + your environment. |
| 47 | +
|
| 48 | + server[in] IP address of the server as IPAddress type |
| 49 | + port[in] port number of the server |
| 50 | + user[in] user name |
| 51 | + password[in] (optional) user password |
| 52 | +
|
| 53 | + Returns boolean - True = connection succeeded |
| 54 | +*/ |
| 55 | +boolean MySQL_Connection::connect(IPAddress server, int port, char *user, |
| 56 | + char *password) |
| 57 | +{ |
| 58 | + int connected = 0; |
| 59 | + int i = -1; |
| 60 | + |
| 61 | + // Retry up to MAX_CONNECT_ATTEMPTS times 1 second apart. |
| 62 | + do { |
| 63 | + delay(1000); |
| 64 | + connected = client->connect(server, port); |
| 65 | + i++; |
| 66 | + } while (i < MAX_CONNECT_ATTEMPTS && !connected); |
| 67 | + |
| 68 | + if (connected) { |
| 69 | + read_packet(); |
| 70 | + parse_handshake_packet(); |
| 71 | + send_authentication_packet(user, password); |
| 72 | + read_packet(); |
| 73 | + if (check_ok_packet() != 0) { |
| 74 | + parse_error_packet(); |
| 75 | + return false; |
| 76 | + } |
| 77 | + show_error(CONNECTED); |
| 78 | + Serial.println(server_version); |
| 79 | + free(server_version); // don't need it anymore |
| 80 | + return true; |
| 81 | + } |
| 82 | + return false; |
| 83 | +} |
| 84 | + |
| 85 | +/* |
| 86 | + close - cancel the connection |
| 87 | +
|
| 88 | + This method closes the connection to the server and frees up any memory |
| 89 | + used in the buffer. |
| 90 | +*/ |
| 91 | +void MySQL_Connection::close() |
| 92 | +{ |
| 93 | + if (connected()) |
| 94 | + { |
| 95 | + client->flush(); |
| 96 | + client->stop(); |
| 97 | + show_error(DISCONNECTED, true); |
| 98 | + } |
| 99 | +} |
0 commit comments