Adafruit_MQTT_Library/Adafruit_MQTT_FONA.h

143 lines
4.3 KiB
C
Raw Permalink Normal View History

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:
//
// 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.
2015-06-10 06:22:36 +02:00
#ifndef _ADAFRUIT_MQTT_FONA_H_
#define _ADAFRUIT_MQTT_FONA_H_
#include <Adafruit_FONA.h>
#include "Adafruit_MQTT.h"
#define MQTT_FONA_INTERAVAILDELAY 100
#define MQTT_FONA_QUERYDELAY 500
2015-06-11 00:38:34 +02:00
// FONA-specific version of the Adafruit_MQTT class.
// Note that this is defined as a header-only class to prevent issues with using
// the library on non-FONA platforms (since Arduino will include all .cpp files
// in the compilation of the library).
2015-06-10 06:22:36 +02:00
class Adafruit_MQTT_FONA : public Adafruit_MQTT {
public:
2015-10-22 16:09:16 +02:00
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
const char *cid, const char *user, const char *pass):
Adafruit_MQTT(server, port, cid, user, pass),
fona(f)
{}
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
const char *user="", const char *pass=""):
2015-10-22 16:09:16 +02:00
Adafruit_MQTT(server, port, user, pass),
2015-06-10 06:22:36 +02:00
fona(f)
{}
bool connectServer() {
char server[40];
2016-08-09 20:21:46 +02:00
strncpy(server, servername, 40);
2015-12-10 20:46:40 +01:00
#ifdef ADAFRUIT_SLEEPYDOG_H
2015-06-10 06:22:36 +02:00
Watchdog.reset();
2015-12-10 20:46:40 +01:00
#endif
2015-06-10 06:22:36 +02:00
// connect to server
DEBUG_PRINTLN(F("Connecting to TCP"));
return fona->TCPconnect(server, portnum);
}
bool disconnectServer() {
2015-06-10 06:22:36 +02:00
return fona->TCPclose();
}
bool connected() {
// Return true if connected, false if not connected.
return fona->TCPconnected();
}
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
2015-06-10 06:22:36 +02:00
uint8_t *buffp = buffer;
DEBUG_PRINTLN(F("Reading data.."));
2015-06-10 06:22:36 +02:00
if (!fona->TCPconnected()) return 0;
/* Read data until either the connection is closed, or the idle timeout is reached. */
uint16_t len = 0;
int16_t t = timeout;
uint16_t avail;
while (fona->TCPconnected() && (timeout >= 0)) {
//DEBUG_PRINT('.');
2015-06-10 06:22:36 +02:00
while (avail = fona->TCPavailable()) {
//DEBUG_PRINT('!');
2015-06-10 06:22:36 +02:00
if (len + avail > maxlen) {
2015-06-10 06:30:52 +02:00
avail = maxlen - len;
if (avail == 0) return len;
2015-06-10 06:22:36 +02:00
}
// try to read the data into the end of the pointer
if (! fona->TCPread(buffp, avail)) return len;
2015-06-10 06:22:36 +02:00
// read it! advance pointer
buffp += avail;
len += avail;
timeout = t; // reset the timeout
//DEBUG_PRINTLN((uint8_t)c, HEX);
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read:\t"));
2015-06-10 06:22:36 +02:00
DEBUG_PRINTBUFFER(buffer, len);
2016-06-08 04:17:05 +02:00
return len;
2015-06-10 06:22:36 +02:00
}
}
2015-12-10 20:46:40 +01:00
#ifdef ADAFRUIT_SLEEPYDOG_H
2015-06-10 06:22:36 +02:00
Watchdog.reset();
2015-12-10 20:46:40 +01:00
#endif
2015-06-10 06:22:36 +02:00
timeout -= MQTT_FONA_INTERAVAILDELAY;
timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the FONA for avail()
delay(MQTT_FONA_INTERAVAILDELAY);
}
2016-06-08 04:17:05 +02:00
2015-06-10 06:22:36 +02:00
return len;
}
bool sendPacket(uint8_t *buffer, uint16_t len) {
2015-06-10 06:22:36 +02:00
DEBUG_PRINTLN(F("Writing packet"));
if (fona->TCPconnected()) {
boolean ret = fona->TCPsend((char *)buffer, len);
//DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
if (!ret) {
DEBUG_PRINTLN("Failed to send packet.");
2015-06-10 06:22:36 +02:00
return false;
}
} else {
DEBUG_PRINTLN(F("Connection failed!"));
return false;
}
return true;
2015-06-10 06:22:36 +02:00
}
2015-06-10 06:22:36 +02:00
private:
uint32_t serverip;
Adafruit_FONA *fona;
};
2015-06-11 00:38:34 +02:00
2015-06-10 06:22:36 +02:00
#endif