1218

Dependencies:   mbed-os-example-wifi-DISCO_IOTBOARD_MQTT MQTT

Committer:
cornetlin
Date:
Tue Apr 23 09:45:23 2019 +0000
Revision:
59:9bbcc1b368ba
MQTT_WIFI

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cornetlin 59:9bbcc1b368ba 1 #ifndef _MQTTNETWORK_H_
cornetlin 59:9bbcc1b368ba 2 #define _MQTTNETWORK_H_
cornetlin 59:9bbcc1b368ba 3
cornetlin 59:9bbcc1b368ba 4 #include "NetworkInterface.h"
cornetlin 59:9bbcc1b368ba 5 #include "TCPSocket.h"
cornetlin 59:9bbcc1b368ba 6
cornetlin 59:9bbcc1b368ba 7 class MQTTNetwork {
cornetlin 59:9bbcc1b368ba 8 public:
cornetlin 59:9bbcc1b368ba 9 MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
cornetlin 59:9bbcc1b368ba 10 socket = new TCPSocket();
cornetlin 59:9bbcc1b368ba 11 }
cornetlin 59:9bbcc1b368ba 12
cornetlin 59:9bbcc1b368ba 13 ~MQTTNetwork() {
cornetlin 59:9bbcc1b368ba 14 delete socket;
cornetlin 59:9bbcc1b368ba 15 }
cornetlin 59:9bbcc1b368ba 16
cornetlin 59:9bbcc1b368ba 17 int read(unsigned char* buffer, int len, int timeout) {
cornetlin 59:9bbcc1b368ba 18 return socket->recv(buffer, len);
cornetlin 59:9bbcc1b368ba 19 }
cornetlin 59:9bbcc1b368ba 20
cornetlin 59:9bbcc1b368ba 21 int write(unsigned char* buffer, int len, int timeout) {
cornetlin 59:9bbcc1b368ba 22 return socket->send(buffer, len);
cornetlin 59:9bbcc1b368ba 23 }
cornetlin 59:9bbcc1b368ba 24
cornetlin 59:9bbcc1b368ba 25 int connect(const char* hostname, int port) {
cornetlin 59:9bbcc1b368ba 26 socket->open(network);
cornetlin 59:9bbcc1b368ba 27 return socket->connect(hostname, port);
cornetlin 59:9bbcc1b368ba 28 }
cornetlin 59:9bbcc1b368ba 29
cornetlin 59:9bbcc1b368ba 30 int disconnect() {
cornetlin 59:9bbcc1b368ba 31 return socket->close();
cornetlin 59:9bbcc1b368ba 32 }
cornetlin 59:9bbcc1b368ba 33
cornetlin 59:9bbcc1b368ba 34 private:
cornetlin 59:9bbcc1b368ba 35 NetworkInterface* network;
cornetlin 59:9bbcc1b368ba 36 TCPSocket* socket;
cornetlin 59:9bbcc1b368ba 37 };
cornetlin 59:9bbcc1b368ba 38
cornetlin 59:9bbcc1b368ba 39 #endif // _MQTTNETWORK_H_