mbed mqtt
Dependencies: TextLCD MQTT IAP
MQTTNetwork.h@2:562744909841, 2018-11-29 (annotated)
- Committer:
- ncshy
- Date:
- Thu Nov 29 03:28:14 2018 +0000
- Revision:
- 2:562744909841
- Parent:
- 1:91e33a7fe0b5
test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hungnguyenm | 1:91e33a7fe0b5 | 1 | #ifndef _MQTTNETWORK_H_ |
| hungnguyenm | 1:91e33a7fe0b5 | 2 | #define _MQTTNETWORK_H_ |
| hungnguyenm | 1:91e33a7fe0b5 | 3 | |
| hungnguyenm | 1:91e33a7fe0b5 | 4 | #include "NetworkInterface.h" |
| hungnguyenm | 1:91e33a7fe0b5 | 5 | |
| hungnguyenm | 1:91e33a7fe0b5 | 6 | class MQTTNetwork { |
| hungnguyenm | 1:91e33a7fe0b5 | 7 | public: |
| hungnguyenm | 1:91e33a7fe0b5 | 8 | MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) { |
| hungnguyenm | 1:91e33a7fe0b5 | 9 | socket = new TCPSocket(); |
| hungnguyenm | 1:91e33a7fe0b5 | 10 | } |
| hungnguyenm | 1:91e33a7fe0b5 | 11 | |
| hungnguyenm | 1:91e33a7fe0b5 | 12 | ~MQTTNetwork() { |
| hungnguyenm | 1:91e33a7fe0b5 | 13 | delete socket; |
| hungnguyenm | 1:91e33a7fe0b5 | 14 | } |
| hungnguyenm | 1:91e33a7fe0b5 | 15 | |
| hungnguyenm | 1:91e33a7fe0b5 | 16 | int read(unsigned char* buffer, int len, int timeout) { |
| ncshy | 2:562744909841 | 17 | socket->set_blocking(false); |
| ncshy | 2:562744909841 | 18 | socket->set_timeout(timeout); |
| ncshy | 2:562744909841 | 19 | int ret = socket->recv(buffer, len); |
| ncshy | 2:562744909841 | 20 | if (NSAPI_ERROR_WOULD_BLOCK == ret) |
| ncshy | 2:562744909841 | 21 | return 0; |
| ncshy | 2:562744909841 | 22 | else |
| ncshy | 2:562744909841 | 23 | return ret; |
| hungnguyenm | 1:91e33a7fe0b5 | 24 | } |
| hungnguyenm | 1:91e33a7fe0b5 | 25 | |
| hungnguyenm | 1:91e33a7fe0b5 | 26 | int write(unsigned char* buffer, int len, int timeout) { |
| hungnguyenm | 1:91e33a7fe0b5 | 27 | return socket->send(buffer, len); |
| hungnguyenm | 1:91e33a7fe0b5 | 28 | } |
| hungnguyenm | 1:91e33a7fe0b5 | 29 | |
| hungnguyenm | 1:91e33a7fe0b5 | 30 | int connect(const char* hostname, int port) { |
| hungnguyenm | 1:91e33a7fe0b5 | 31 | socket->open(network); |
| hungnguyenm | 1:91e33a7fe0b5 | 32 | return socket->connect(hostname, port); |
| hungnguyenm | 1:91e33a7fe0b5 | 33 | } |
| hungnguyenm | 1:91e33a7fe0b5 | 34 | |
| hungnguyenm | 1:91e33a7fe0b5 | 35 | int disconnect() { |
| hungnguyenm | 1:91e33a7fe0b5 | 36 | return socket->close(); |
| hungnguyenm | 1:91e33a7fe0b5 | 37 | } |
| hungnguyenm | 1:91e33a7fe0b5 | 38 | |
| hungnguyenm | 1:91e33a7fe0b5 | 39 | private: |
| hungnguyenm | 1:91e33a7fe0b5 | 40 | NetworkInterface* network; |
| hungnguyenm | 1:91e33a7fe0b5 | 41 | TCPSocket* socket; |
| hungnguyenm | 1:91e33a7fe0b5 | 42 | }; |
| hungnguyenm | 1:91e33a7fe0b5 | 43 | |
| hungnguyenm | 1:91e33a7fe0b5 | 44 | #endif // _MQTTNETWORK_H_ |