-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMQTTNetwork.h
54 lines (44 loc) · 1.35 KB
/
MQTTNetwork.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef _MQTTNETWORK_H_
#define _MQTTNETWORK_H_
#include "NetworkInterface.h"
#include "TLSSocket.h"
class MQTTNetwork {
public:
MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
socket = new TLSSocket;
}
~MQTTNetwork() {
delete socket;
}
int read(unsigned char* buffer, int len, int timeout) {
nsapi_size_or_error_t rc = 0;
socket->set_timeout(timeout);
rc = socket->recv(buffer, len);
if (rc == NSAPI_ERROR_WOULD_BLOCK){
// time out and no data
// MQTTClient.readPacket() requires 0 on time out and no data.
return 0;
}
return rc;
}
int write(unsigned char* buffer, int len, int timeout) {
// TODO: handle time out
return socket->send(buffer, len);
}
int connect(const char* hostname, int port, const char *ssl_ca_pem = NULL,
const char *ssl_cli_pem = NULL, const char *ssl_pk_pem = NULL) {
int ret = socket->open(network);
if(ret < 0)
return ret;
socket->set_root_ca_cert(ssl_ca_pem);
socket->set_client_cert_key(ssl_cli_pem, ssl_pk_pem);
return socket->connect(hostname, port);
}
int disconnect() {
return socket->close();
}
private:
NetworkInterface* network;
TLSSocket* socket;
};
#endif // _MQTTNETWORK_H_