d

Dependencies:   MQTTPacket FP

Committer:
JavierGC
Date:
Sun Mar 20 19:25:46 2022 +0000
Revision:
60:f1a42823e381
Parent:
54:ff9e5c4b52d0
Rev1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 31:a51dd239b78e 1 #if !defined(MQTTSOCKET_H)
icraggs 31:a51dd239b78e 2 #define MQTTSOCKET_H
icraggs 31:a51dd239b78e 3
icraggs 43:21da1f744243 4 #include "MQTTmbed.h"
icraggs 54:ff9e5c4b52d0 5 #include <EthernetInterface.h>
icraggs 54:ff9e5c4b52d0 6 #include <Timer.h>
icraggs 31:a51dd239b78e 7
icraggs 31:a51dd239b78e 8 class MQTTSocket
icraggs 31:a51dd239b78e 9 {
Jan Jongboom 49:08571008b958 10 public:
icraggs 54:ff9e5c4b52d0 11 MQTTSocket(EthernetInterface *anet)
icraggs 54:ff9e5c4b52d0 12 {
icraggs 54:ff9e5c4b52d0 13 net = anet;
icraggs 54:ff9e5c4b52d0 14 open = false;
icraggs 54:ff9e5c4b52d0 15 }
icraggs 54:ff9e5c4b52d0 16
icraggs 31:a51dd239b78e 17 int connect(char* hostname, int port, int timeout=1000)
icraggs 31:a51dd239b78e 18 {
icraggs 54:ff9e5c4b52d0 19 if (open)
icraggs 54:ff9e5c4b52d0 20 disconnect();
icraggs 54:ff9e5c4b52d0 21 nsapi_error_t rc = mysock.open(net);
icraggs 54:ff9e5c4b52d0 22 open = true;
icraggs 54:ff9e5c4b52d0 23 mysock.set_blocking(true);
icraggs 54:ff9e5c4b52d0 24 mysock.set_timeout((unsigned int)timeout);
JavierGC 60:f1a42823e381 25 SocketAddress socketAddr(hostname,port);
JavierGC 60:f1a42823e381 26 rc = mysock.connect(socketAddr);
icraggs 54:ff9e5c4b52d0 27 mysock.set_blocking(false); // blocking timeouts seem not to work
icraggs 54:ff9e5c4b52d0 28 return rc;
icraggs 31:a51dd239b78e 29 }
icraggs 31:a51dd239b78e 30
icraggs 54:ff9e5c4b52d0 31 // common read/write routine, avoiding blocking timeouts
icraggs 54:ff9e5c4b52d0 32 int common(unsigned char* buffer, int len, int timeout, bool read)
icraggs 54:ff9e5c4b52d0 33 {
icraggs 54:ff9e5c4b52d0 34 timer.start();
icraggs 54:ff9e5c4b52d0 35 mysock.set_blocking(false); // blocking timeouts seem not to work
icraggs 54:ff9e5c4b52d0 36 int bytes = 0;
icraggs 54:ff9e5c4b52d0 37 bool first = true;
icraggs 54:ff9e5c4b52d0 38 do
icraggs 54:ff9e5c4b52d0 39 {
icraggs 54:ff9e5c4b52d0 40 if (first)
icraggs 54:ff9e5c4b52d0 41 first = false;
icraggs 54:ff9e5c4b52d0 42 else
JavierGC 60:f1a42823e381 43 wait_us((timeout*1000) < 100 ? (timeout*1000) : 100);
icraggs 54:ff9e5c4b52d0 44 int rc;
icraggs 54:ff9e5c4b52d0 45 if (read)
icraggs 54:ff9e5c4b52d0 46 rc = mysock.recv((char*)buffer, len);
icraggs 54:ff9e5c4b52d0 47 else
icraggs 54:ff9e5c4b52d0 48 rc = mysock.send((char*)buffer, len);
icraggs 54:ff9e5c4b52d0 49 if (rc < 0)
icraggs 54:ff9e5c4b52d0 50 {
icraggs 54:ff9e5c4b52d0 51 if (rc != NSAPI_ERROR_WOULD_BLOCK)
icraggs 54:ff9e5c4b52d0 52 {
icraggs 54:ff9e5c4b52d0 53 bytes = -1;
icraggs 54:ff9e5c4b52d0 54 break;
icraggs 54:ff9e5c4b52d0 55 }
icraggs 54:ff9e5c4b52d0 56 }
icraggs 54:ff9e5c4b52d0 57 else
icraggs 54:ff9e5c4b52d0 58 bytes += rc;
icraggs 54:ff9e5c4b52d0 59 }
icraggs 54:ff9e5c4b52d0 60 while (bytes < len && timer.read_ms() < timeout);
icraggs 54:ff9e5c4b52d0 61 timer.stop();
icraggs 54:ff9e5c4b52d0 62 return bytes;
icraggs 54:ff9e5c4b52d0 63 }
icraggs 54:ff9e5c4b52d0 64
icraggs 54:ff9e5c4b52d0 65 /* returns the number of bytes read, which could be 0.
icraggs 54:ff9e5c4b52d0 66 -1 if there was an error on the socket
icraggs 54:ff9e5c4b52d0 67 */
icraggs 36:2f1ada427e56 68 int read(unsigned char* buffer, int len, int timeout)
icraggs 31:a51dd239b78e 69 {
icraggs 54:ff9e5c4b52d0 70 return common(buffer, len, timeout, true);
icraggs 31:a51dd239b78e 71 }
JavierGC 60:f1a42823e381 72
JavierGC 60:f1a42823e381 73
JavierGC 60:f1a42823e381 74 /*int read(unsigned char* buffer, int len, int timeout)
JavierGC 60:f1a42823e381 75 {
JavierGC 60:f1a42823e381 76 mysock.set_timeout(timeout);
JavierGC 60:f1a42823e381 77 int rc = mysock.recv((char*)buffer, len);
JavierGC 60:f1a42823e381 78 mysock.set_timeout(0);
JavierGC 60:f1a42823e381 79 return rc;
JavierGC 60:f1a42823e381 80 }
JavierGC 60:f1a42823e381 81 */
Jan Jongboom 49:08571008b958 82
icraggs 36:2f1ada427e56 83 int write(unsigned char* buffer, int len, int timeout)
icraggs 31:a51dd239b78e 84 {
icraggs 54:ff9e5c4b52d0 85 return common(buffer, len, timeout, false);
icraggs 31:a51dd239b78e 86 }
Jan Jongboom 49:08571008b958 87
icraggs 31:a51dd239b78e 88 int disconnect()
icraggs 31:a51dd239b78e 89 {
icraggs 54:ff9e5c4b52d0 90 open = false;
icraggs 31:a51dd239b78e 91 return mysock.close();
icraggs 31:a51dd239b78e 92 }
Jan Jongboom 49:08571008b958 93
icraggs 54:ff9e5c4b52d0 94 /*bool is_connected()
Jan Jongboom 49:08571008b958 95 {
Jan Jongboom 49:08571008b958 96 return mysock.is_connected();
icraggs 54:ff9e5c4b52d0 97 }*/
Jan Jongboom 49:08571008b958 98
icraggs 31:a51dd239b78e 99 private:
icraggs 31:a51dd239b78e 100
icraggs 54:ff9e5c4b52d0 101 bool open;
icraggs 54:ff9e5c4b52d0 102 TCPSocket mysock;
icraggs 54:ff9e5c4b52d0 103 EthernetInterface *net;
icraggs 54:ff9e5c4b52d0 104 Timer timer;
Jan Jongboom 49:08571008b958 105
icraggs 31:a51dd239b78e 106 };
icraggs 31:a51dd239b78e 107
icraggs 31:a51dd239b78e 108 #endif