2015-06-11 00:38:34 +02:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Adafruit Industries
|
|
|
|
//
|
|
|
|
// 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:
|
|
|
|
//
|
2020-06-25 15:56:46 +02:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
2015-06-11 00:38:34 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2015-06-08 10:48:25 +02:00
|
|
|
#include "Adafruit_MQTT_Client.h"
|
|
|
|
|
|
|
|
bool Adafruit_MQTT_Client::connectServer() {
|
|
|
|
// Grab server name from flash and copy to buffer for name resolution.
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
2016-08-10 03:12:49 +02:00
|
|
|
strcpy((char *)buffer, servername);
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Connecting to: "));
|
|
|
|
DEBUG_PRINTLN((char *)buffer);
|
2015-06-08 10:48:25 +02:00
|
|
|
// Connect and check for success (0 result).
|
|
|
|
int r = client->connect((char *)buffer, portnum);
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Connect result: "));
|
|
|
|
DEBUG_PRINTLN(r);
|
2015-06-08 10:48:25 +02:00
|
|
|
return r != 0;
|
|
|
|
}
|
|
|
|
|
2015-10-05 20:18:49 +02:00
|
|
|
bool Adafruit_MQTT_Client::disconnectServer() {
|
2015-07-05 23:29:13 +02:00
|
|
|
// Stop connection if connected and return success (stop has no indication of
|
|
|
|
// failure).
|
|
|
|
if (client->connected()) {
|
|
|
|
client->stop();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Adafruit_MQTT_Client::connected() {
|
|
|
|
// Return true if connected, false if not connected.
|
|
|
|
return client->connected();
|
2015-06-08 10:48:25 +02:00
|
|
|
}
|
|
|
|
|
2016-07-06 23:20:02 +02:00
|
|
|
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
|
2016-05-18 21:23:41 +02:00
|
|
|
int16_t timeout) {
|
2020-06-25 15:56:46 +02:00
|
|
|
/* Read data until either the connection is closed, or the idle timeout is
|
|
|
|
* reached. */
|
2015-06-08 10:48:25 +02:00
|
|
|
uint16_t len = 0;
|
|
|
|
int16_t t = timeout;
|
|
|
|
|
2020-11-06 16:20:52 +01:00
|
|
|
if (maxlen == 0) { // handle zero-length packets
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-18 00:01:52 +02:00
|
|
|
|
2015-06-08 10:48:25 +02:00
|
|
|
while (client->connected() && (timeout >= 0)) {
|
2020-06-25 15:56:46 +02:00
|
|
|
// DEBUG_PRINT('.');
|
2015-06-08 10:48:25 +02:00
|
|
|
while (client->available()) {
|
2020-06-25 15:56:46 +02:00
|
|
|
// DEBUG_PRINT('!');
|
2015-06-08 10:48:25 +02:00
|
|
|
char c = client->read();
|
2020-06-25 15:56:46 +02:00
|
|
|
timeout = t; // reset the timeout
|
2015-06-08 10:48:25 +02:00
|
|
|
buffer[len] = c;
|
2020-06-25 15:56:46 +02:00
|
|
|
// DEBUG_PRINTLN((uint8_t)c, HEX);
|
2015-06-08 10:48:25 +02:00
|
|
|
len++;
|
2018-08-18 00:01:52 +02:00
|
|
|
|
2020-11-06 16:20:52 +01:00
|
|
|
if (len == maxlen) { // we read all we want, bail
|
2016-05-18 21:23:41 +02:00
|
|
|
DEBUG_PRINT(F("Read data:\t"));
|
2015-06-08 10:48:25 +02:00
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timeout -= MQTT_CLIENT_READINTERVAL_MS;
|
|
|
|
delay(MQTT_CLIENT_READINTERVAL_MS);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2016-07-07 00:32:21 +02:00
|
|
|
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
|
|
|
|
uint16_t ret = 0;
|
2021-05-13 06:31:22 +02:00
|
|
|
uint16_t offset = 0;
|
2016-07-07 00:32:21 +02:00
|
|
|
while (len > 0) {
|
|
|
|
if (client->connected()) {
|
|
|
|
// send 250 bytes at most at a time, can adjust this later based on Client
|
|
|
|
|
2018-01-06 23:52:40 +01:00
|
|
|
uint16_t sendlen = len > 250 ? 250 : len;
|
2020-06-25 15:56:46 +02:00
|
|
|
// Serial.print("Sending: "); Serial.println(sendlen);
|
2021-05-13 06:31:22 +02:00
|
|
|
ret = client->write(buffer + offset, sendlen);
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Client sendPacket returned: "));
|
|
|
|
DEBUG_PRINTLN(ret);
|
2016-07-07 00:32:21 +02:00
|
|
|
len -= ret;
|
2021-05-13 06:31:22 +02:00
|
|
|
offset += ret;
|
2021-05-14 17:09:07 +02:00
|
|
|
|
2016-07-07 00:32:21 +02:00
|
|
|
if (ret != sendlen) {
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINTLN("Failed to send packet.");
|
|
|
|
return false;
|
2016-07-07 00:32:21 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DEBUG_PRINTLN(F("Connection failed!"));
|
2015-06-08 10:48:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|