Eric Micallef / Mbed OS smat_controller

Dependencies:   MQTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MQTTNetwork.h Source File

MQTTNetwork.h

00001 #ifndef _MQTTNETWORK_H_
00002 #define _MQTTNETWORK_H_
00003 
00004 #include "NetworkInterface.h"
00005 
00006 class MQTTNetwork {
00007 public:
00008     MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
00009         socket = new TCPSocket();
00010     }
00011 
00012     ~MQTTNetwork() {
00013         delete socket;
00014     }
00015 
00016     int read(unsigned char* buffer, int len, int timeout) {
00017         socket->set_blocking(false);
00018         socket->set_timeout(timeout);
00019         int ret = socket->recv(buffer, len);
00020         if (NSAPI_ERROR_WOULD_BLOCK == ret)
00021             return 0;
00022         else
00023             return ret;
00024 }
00025 
00026     int write(unsigned char* buffer, int len, int timeout) {
00027         return socket->send(buffer, len);
00028     }
00029 
00030     int connect(const char* hostname, int port) {
00031         socket->open(network);
00032         return socket->connect(hostname, port);
00033     }
00034 
00035     int disconnect() {
00036         return socket->close();
00037     }
00038 
00039 private:
00040     NetworkInterface* network;
00041     TCPSocket* socket;
00042 };
00043 
00044 #endif // _MQTTNETWORK_H_