-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSX1276Socket.h
87 lines (69 loc) · 2.43 KB
/
SX1276Socket.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
//
// Created by Bassirou on 09.09.2018.
//
#ifndef ARDUINO_MQTTSN_CLIENT_LORASOCKET_H
#define ARDUINO_MQTTSN_CLIENT_LORASOCKET_H
#include "MqttSnMessageHandler.h"
#include "SocketInterface.h"
#include <SX1276.h>
#define LORA_MODE 4
#define LORA_CHANNEL CH_10_868
#define LORA_POWER 'H'
#define LORA_ADDR 3
class SX1276Socket : SocketInterface {
private:
SX1276 &loraConnection;
uint16_t port;
device_address own_address;
device_address receive_address;
uint8_t receive_buffer[MAX_PAYLOAD];
public:
SX1276Socket(SX1276 &loraConnection) : loraConnection(loraConnection){};
bool begin() override {
loraConnection.ON();
loraConnection.setMode(LORA_MODE);
loraConnection.setChannel(CH_10_868);
loraConnection.setPower(LORA_POWER);
loraConnection.setNodeAddress(LORA_ADDR);
return 1;
}
device_address *getBroadcastAddress() override {
// TODO return multicast address
return nullptr;
}
device_address *getAddress() override {
own_address.bytes[0] = LORA_ADDR;
return &own_address;
}
uint8_t getMaximumMessageLength() override {
return MAX_PAYLOAD;
}
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 {
loraConnection.sendPacketTimeout(destination->bytes[1], bytes, bytes_len);
return false;
}
bool loop() override {
if (loraConnection.availableData()) {
device_address from;
memset(&from.bytes[0], 0x0, sizeof(device_address));
int available = loraConnection.receive();
if (available == 0) {
memcpy(receive_buffer,loraConnection.packet_received.data, loraConnection.packet_received.length);
from.bytes[0] = loraConnection.packet_received.dst;
mqttSnMessageHandler->receiveData(&from, receive_buffer);
}
}
return true;
}
template<class SX1276Socket>
void setMqttSnMessageHandler(
MqttSnMessageHandler<SX1276Socket> *mqttSnMessageHandler) {
this->mqttSnMessageHandler = mqttSnMessageHandler;
};
private:
MqttSnMessageHandler<SX1276Socket> *mqttSnMessageHandler;
};
#endif //ARDUINO_MQTTSN_CLIENT_LORASOCKET_H