1 | // The MIT License (MIT)
|
---|
2 | //
|
---|
3 | // Copyright (c) 2015 Adafruit Industries
|
---|
4 | //
|
---|
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
6 | // of this software and associated documentation files (the "Software"), to deal
|
---|
7 | // in the Software without restriction, including without limitation the rights
|
---|
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
9 | // copies of the Software, and to permit persons to whom the Software is
|
---|
10 | // furnished to do so, subject to the following conditions:
|
---|
11 | //
|
---|
12 | // The above copyright notice and this permission notice shall be included in all
|
---|
13 | // copies or substantial portions of the Software.
|
---|
14 | //
|
---|
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
---|
21 | // SOFTWARE.
|
---|
22 | #include "include/Adafruit/Adafruit_MQTT_Client.h"
|
---|
23 |
|
---|
24 |
|
---|
25 | bool Adafruit_MQTT_Client::connectServer() {
|
---|
26 | // Grab server name from flash and copy to buffer for name resolution.
|
---|
27 | memset(buffer, 0, sizeof(buffer));
|
---|
28 | strcpy_P((char *)buffer, servername);
|
---|
29 | DEBUG_PRINT(F("Connecting to: ")); DEBUG_PRINTLN((char *)buffer);
|
---|
30 | // Connect and check for success (0 result).
|
---|
31 | int r = client->connect((char *)buffer, portnum);
|
---|
32 | DEBUG_PRINT(F("Connect result: ")); DEBUG_PRINTLN(r);
|
---|
33 | return r != 0;
|
---|
34 | }
|
---|
35 |
|
---|
36 | bool Adafruit_MQTT_Client::disconnect() {
|
---|
37 | // Stop connection if connected and return success (stop has no indication of
|
---|
38 | // failure).
|
---|
39 | if (client->connected()) {
|
---|
40 | client->stop();
|
---|
41 | }
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 |
|
---|
45 | bool Adafruit_MQTT_Client::connected() {
|
---|
46 | // Return true if connected, false if not connected.
|
---|
47 | return client->connected();
|
---|
48 | }
|
---|
49 |
|
---|
50 | uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen,
|
---|
51 | int16_t timeout,
|
---|
52 | bool checkForValidPubPacket) {
|
---|
53 | /* Read data until either the connection is closed, or the idle timeout is reached. */
|
---|
54 | uint16_t len = 0;
|
---|
55 | int16_t t = timeout;
|
---|
56 |
|
---|
57 | while (client->connected() && (timeout >= 0)) {
|
---|
58 | DEBUG_PRINT('.');
|
---|
59 | while (client->available()) {
|
---|
60 | DEBUG_PRINT('!');
|
---|
61 | char c = client->read();
|
---|
62 | timeout = t; // reset the timeout
|
---|
63 | buffer[len] = c;
|
---|
64 | DEBUG_PRINTLN((uint8_t)c, HEX);
|
---|
65 | len++;
|
---|
66 | if (len == maxlen) { // we read all we want, bail
|
---|
67 | DEBUG_PRINT(F("Read packet:\t"));
|
---|
68 | DEBUG_PRINTBUFFER(buffer, len);
|
---|
69 | return len;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // special case where we just one one publication packet at a time
|
---|
73 | if (checkForValidPubPacket) {
|
---|
74 | if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
|
---|
75 | // oooh a valid publish packet!
|
---|
76 | DEBUG_PRINT(F("Read PUBLISH packet:\t"));
|
---|
77 | DEBUG_PRINTBUFFER(buffer, len);
|
---|
78 | return len;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | timeout -= MQTT_CLIENT_READINTERVAL_MS;
|
---|
83 | delay(MQTT_CLIENT_READINTERVAL_MS);
|
---|
84 | }
|
---|
85 | return len;
|
---|
86 | }
|
---|
87 |
|
---|
88 | bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint8_t len) {
|
---|
89 | if (client->connected()) {
|
---|
90 | uint16_t ret = client->write(buffer, len);
|
---|
91 | DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
---|
92 | if (ret != len) {
|
---|
93 | DEBUG_PRINTLN("Failed to send complete packet.")
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 | } else {
|
---|
97 | DEBUG_PRINTLN(F("Connection failed!"));
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 | return true;
|
---|
101 | }
|
---|