[144] | 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 | #ifndef _ADAFRUIT_MQTT_FONA_H_
|
---|
| 23 | #define _ADAFRUIT_MQTT_FONA_H_
|
---|
| 24 |
|
---|
| 25 | #include <Adafruit_FONA.h>
|
---|
| 26 | #include "Adafruit_MQTT.h"
|
---|
| 27 |
|
---|
| 28 | #define MQTT_FONA_INTERAVAILDELAY 100
|
---|
| 29 | #define MQTT_FONA_QUERYDELAY 500
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | // FONA-specific version of the Adafruit_MQTT class.
|
---|
| 33 | // Note that this is defined as a header-only class to prevent issues with using
|
---|
| 34 | // the library on non-FONA platforms (since Arduino will include all .cpp files
|
---|
| 35 | // in the compilation of the library).
|
---|
| 36 | class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
---|
| 37 | public:
|
---|
| 38 | Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
---|
| 39 | const char *cid, const char *user, const char *pass):
|
---|
| 40 | Adafruit_MQTT(server, port, cid, user, pass),
|
---|
| 41 | fona(f)
|
---|
| 42 | {}
|
---|
| 43 |
|
---|
| 44 | bool connectServer() {
|
---|
| 45 | char server[40];
|
---|
| 46 | strncpy_P(server, servername, 40);
|
---|
| 47 | Watchdog.reset();
|
---|
| 48 |
|
---|
| 49 | // connect to server
|
---|
| 50 | DEBUG_PRINTLN(F("Connecting to TCP"));
|
---|
| 51 | return fona->TCPconnect(server, portnum);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | bool disconnect() {
|
---|
| 55 | return fona->TCPclose();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | bool connected() {
|
---|
| 59 | // Return true if connected, false if not connected.
|
---|
| 60 | return fona->TCPconnected();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
|
---|
| 64 | bool checkForValidPubPacket = false) {
|
---|
| 65 | uint8_t *buffp = buffer;
|
---|
| 66 | DEBUG_PRINTLN(F("Reading a packet.."));
|
---|
| 67 |
|
---|
| 68 | if (!fona->TCPconnected()) return 0;
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /* Read data until either the connection is closed, or the idle timeout is reached. */
|
---|
| 72 | uint16_t len = 0;
|
---|
| 73 | int16_t t = timeout;
|
---|
| 74 | uint16_t avail;
|
---|
| 75 |
|
---|
| 76 | while (fona->TCPconnected() && (timeout >= 0)) {
|
---|
| 77 | DEBUG_PRINT('.');
|
---|
| 78 | while (avail = fona->TCPavailable()) {
|
---|
| 79 | DEBUG_PRINT('!');
|
---|
| 80 |
|
---|
| 81 | if (len + avail > maxlen) {
|
---|
| 82 | avail = maxlen - len;
|
---|
| 83 | if (avail == 0) return len;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | // try to read the data into the end of the pointer
|
---|
| 87 | if (! fona->TCPread(buffp, avail)) return len;
|
---|
| 88 |
|
---|
| 89 | // read it! advance pointer
|
---|
| 90 | buffp += avail;
|
---|
| 91 | len += avail;
|
---|
| 92 | timeout = t; // reset the timeout
|
---|
| 93 |
|
---|
| 94 | //DEBUG_PRINTLN((uint8_t)c, HEX);
|
---|
| 95 |
|
---|
| 96 | if (len == maxlen) { // we read all we want, bail
|
---|
| 97 | DEBUG_PRINT(F("Read packet:\t"));
|
---|
| 98 | DEBUG_PRINTBUFFER(buffer, len);
|
---|
| 99 | return len;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | // special case where we just one one publication packet at a time
|
---|
| 103 | if (checkForValidPubPacket) {
|
---|
| 104 | if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
|
---|
| 105 | // oooh a valid publish packet!
|
---|
| 106 | DEBUG_PRINT(F("Read PUBLISH packet:\t"));
|
---|
| 107 | DEBUG_PRINTBUFFER(buffer, len);
|
---|
| 108 | return len;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | }
|
---|
| 113 | Watchdog.reset();
|
---|
| 114 | timeout -= MQTT_FONA_INTERAVAILDELAY;
|
---|
| 115 | timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the FONA for avail()
|
---|
| 116 | delay(MQTT_FONA_INTERAVAILDELAY);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | return len;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | bool sendPacket(uint8_t *buffer, uint8_t len) {
|
---|
| 123 | DEBUG_PRINTLN(F("Writing packet"));
|
---|
| 124 | if (fona->TCPconnected()) {
|
---|
| 125 | boolean ret = fona->TCPsend((char *)buffer, len);
|
---|
| 126 | //DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
---|
| 127 | if (!ret) {
|
---|
| 128 | DEBUG_PRINTLN("Failed to send packet.")
|
---|
| 129 | return false;
|
---|
| 130 | }
|
---|
| 131 | } else {
|
---|
| 132 | DEBUG_PRINTLN(F("Connection failed!"));
|
---|
| 133 | return false;
|
---|
| 134 | }
|
---|
| 135 | return true;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private:
|
---|
| 139 | uint32_t serverip;
|
---|
| 140 | Adafruit_FONA *fona;
|
---|
| 141 | };
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | #endif
|
---|