-
Notifications
You must be signed in to change notification settings - Fork 8
/
ClientSocket.h
157 lines (127 loc) · 4.97 KB
/
ClientSocket.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* The MIT License (MIT)
*
* Copyright (C) 2018 Gabriel Nikol
*/
#ifndef ARDUINO_MQTTSN_CLIENT_CLIENTSOCKET_H
#define ARDUINO_MQTTSN_CLIENT_CLIENTSOCKET_H
#include "MqttSnMessageHandler.h"
#include "SocketInterface.h"
#include <IPAddress.h>
#include <Client.h>
#define RECEIVE_BUFFER_SIZE 255
class ClientSocket : SocketInterface {
private:
Client *client;
uint16_t localPort = 0;
device_address own_address;
device_address receive_address;
public:
ClientSocket(Client *client) : client(client){};
bool begin() override {
memset(&own_address, 0x0, sizeof(device_address));
memset(&receive_address, 0x0, sizeof(device_address));
return true;
}
device_address *getBroadcastAddress() override {
return nullptr;
}
void setIpAddressAndPort(IPAddress &ipAddress, uint16_t localPort){
this->localPort = localPort;
convertIPAddressAndPortToDeviceAddress(ipAddress, localPort, own_address);
}
device_address *getAddress() override {
// returns 0, 0, 0, 0, 0, 0 if no address was set
return &own_address;
}
uint8_t getMaximumMessageLength() override {
return RECEIVE_BUFFER_SIZE;
}
bool send(device_address *destination, uint8_t *bytes, uint16_t bytes_len) override {
return send(destination, bytes, bytes_len, UINT8_MAX);
}
bool send(device_address *destination, uint8_t *bytes, uint16_t bytes_len, uint8_t signal_strength) override {
if(client->connected()){
if(memcmp(&receive_address, destination, sizeof(device_address))==0){
if (client->write(bytes, bytes_len) == bytes_len) {
client->flush();
return true;
}
}
} else {
IPAddress ip;
uint16_t port;
convertDeviceAddressToIPAddressAndPort(destination, ip, &port);
if(client->connect(ip, port)){
if (client->write(bytes, bytes_len) == bytes_len) {
memcpy(&receive_address, destination, sizeof(device_address));
client->flush();
return true;
}
}
}
return false;
}
bool loop() override {
if(client->connected()) {
if (client->available()) {
uint8_t receive_buffer[RECEIVE_BUFFER_SIZE];
memset(receive_buffer, 0x0, RECEIVE_BUFFER_SIZE);
int available = client->available();
if (available < RECEIVE_BUFFER_SIZE) {
if (client->read(receive_buffer, RECEIVE_BUFFER_SIZE) == available) {
if (mqttSnMessageHandler != nullptr) {
mqttSnMessageHandler->receiveData(&receive_address, receive_buffer);
}
if (transmissionProtocolUartBridge != nullptr) {
transmissionProtocolUartBridge->receiveData(&receive_address, receive_buffer, available);
}
}
} else {
client->flush();
}
}
client->flush();
return true;
}
return false;
}
void convertIPAddressAndPortToDeviceAddress(IPAddress &source, uint16_t port, device_address &target) {
// IPAddress 0 - 3 bytes
target.bytes[0] = source[0];
target.bytes[1] = source[1];
target.bytes[2] = source[2];
target.bytes[3] = source[3];
// Port 4 - 5 bytes
target.bytes[4] = port >> 8;
target.bytes[5] = (uint8_t) port;
}
void convertDeviceAddressToIPAddressAndPort(device_address *source, IPAddress &destination_IPAddress,
uint16_t *destination_port) {
// convert device_address to IPAddress and port
// IPAddress 0 - 3 bytes
destination_IPAddress[0] = source->bytes[0];
destination_IPAddress[1] = source->bytes[1];
destination_IPAddress[2] = source->bytes[2];
destination_IPAddress[3] = source->bytes[3];
// Port 4 - 5 bytes
uint16_t tmp_port = 0;
tmp_port = ((uint16_t) source->bytes[4]) << 8;
tmp_port += (uint16_t) source->bytes[5];
*destination_port = tmp_port;
}
template<class ClientSocket>
void setMqttSnMessageHandler(
MqttSnMessageHandler<ClientSocket> *mqttSnMessageHandler) {
this->mqttSnMessageHandler = mqttSnMessageHandler;
};
template<class ClientSocket>
void setTransmissionProtocolUartBridge(
TransmissionProtocolUartBridge<ClientSocket> *transmissionProtocolUartBridge) {
this->transmissionProtocolUartBridge = transmissionProtocolUartBridge;
};
private:
MqttSnMessageHandler<ClientSocket> *mqttSnMessageHandler = nullptr;
TransmissionProtocolUartBridge<ClientSocket> *transmissionProtocolUartBridge = nullptr;
};
#endif //ARDUINO_MQTTSN_CLIENT_CLIENTSOCKET_H