-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRHDatagramSocket.h
99 lines (76 loc) · 3.11 KB
/
RHDatagramSocket.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
/*
* The MIT License (MIT)
*
* Copyright (C) 2018 Gabriel Nikol
*/
#ifndef ARDUINO_MQTT_SN_CLIENT_RHDATAGRAMSOCKET_H
#define ARDUINO_MQTT_SN_CLIENT_RHDATAGRAMSOCKET_H
#include "MqttSnMessageHandler.h"
#include "SocketInterface.h"
class RHDatagramSocket : SocketInterface {
private:
RHDatagram &rhDatagram;
device_address own_address;
device_address broadcast_address;
public:
RHDatagramSocket(RHDatagram &rhDatagram) : rhDatagram(rhDatagram) {};
bool begin() override {
own_address = device_address(rhDatagram.thisAddress(), 0, 0, 0, 0, 0);
broadcast_address = device_address(RH_BROADCAST_ADDRESS, 0, 0, 0, 0, 0);
return rhDatagram.init();
}
device_address *getBroadcastAddress() override {
return &broadcast_address;
}
device_address *getAddress() override {
return &own_address;
}
uint8_t getMaximumMessageLength() override {
return RH_MAX_MESSAGE_LEN;
}
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 (!rhDatagram.sendto(bytes, bytes_len, destination->bytes[0])) {
rhDatagram.waitPacketSent();
rhDatagram.available(); // put it back to receive mode
return false;
}
rhDatagram.waitPacketSent();
rhDatagram.available(); // put it back to receive mode
return true;
}
bool loop() override {
if (rhDatagram.available()) {
device_address receive_address;
memset(&receive_address, 0x0, sizeof(device_address));
uint8_t receive_buffer[RH_MAX_MESSAGE_LEN];
memset(&receive_buffer, 0x0, RH_MAX_MESSAGE_LEN);
uint8_t receive_buffer_len = sizeof(receive_buffer);
if (rhDatagram.recvfrom(receive_buffer, &receive_buffer_len, &receive_address.bytes[0])) {
if (mqttSnMessageHandler != nullptr) {
mqttSnMessageHandler->receiveData(&receive_address, receive_buffer);
}
if (transmissionProtocolUartBridge != nullptr) {
transmissionProtocolUartBridge->receiveData(&receive_address, receive_buffer, receive_buffer_len);
}
}
}
return true;
}
template<class RHDatagramSocket>
void setMqttSnMessageHandler(
MqttSnMessageHandler<RHDatagramSocket> *mqttSnMessageHandler) {
this->mqttSnMessageHandler = mqttSnMessageHandler;
};
template<class RHDatagramSocket>
void setTransmissionProtocolUartBridge(
TransmissionProtocolUartBridge<RHDatagramSocket> *transmissionProtocolUartBridge) {
this->transmissionProtocolUartBridge = transmissionProtocolUartBridge;
};
private:
MqttSnMessageHandler<RHDatagramSocket> *mqttSnMessageHandler = nullptr;
TransmissionProtocolUartBridge<RHDatagramSocket> *transmissionProtocolUartBridge = nullptr;
};
#endif //ARDUINO_MQTT_SN_CLIENT_RHDATAGRAMSOCKET_H