LoRaMesher Library  0.0.5
A LoRa Mesh library for the IoT
PacketService.h
1#ifndef _LORAMESHER_PACKET_SERVICE_H
2#define _LORAMESHER_PACKET_SERVICE_H
3
4#include "entities/packets/Packet.h"
5#include "entities/packets/ControlPacket.h"
6#include "entities/packets/DataPacket.h"
7#include "entities/packets/AppPacket.h"
8#include "entities/packets/RoutePacket.h"
9
10class PacketService {
11public:
12
19 static DataPacket* dataPacket(Packet<uint8_t>* p);
20
31 static ControlPacket* createControlPacket(uint16_t dst, uint16_t src, uint8_t type, uint8_t* payload, uint8_t payloadSize);
32
43 static ControlPacket* createEmptyControlPacket(uint16_t dst, uint16_t src, uint8_t type, uint8_t seq_id, uint16_t num_packets);
44
55 static DataPacket* createDataPacket(uint16_t dst, uint16_t src, uint8_t type, uint8_t* payload, uint8_t payloadSize);
56
63 static Packet<uint8_t>* createEmptyPacket(size_t packetSize);
64
73 template<class T>
74 static Packet<uint8_t>* copyPacket(T* p, size_t packetLength) {
75 Packet<uint8_t>* cpPacket = static_cast<Packet<uint8_t>*>(malloc(packetLength));
76
77 if (cpPacket) {
78 memcpy(cpPacket, p, packetLength);
79 } else {
80 Log.errorln(F("Copy Packet not allocated"));
81 return nullptr;
82 }
83
84 return cpPacket;
85 }
86
95 static RoutePacket* createRoutingPacket(uint16_t localAddress, NetworkNode* nodes, size_t numOfNodes);
96
106 static AppPacket<uint8_t>* createAppPacket(uint16_t dst, uint16_t src, uint8_t* payload, uint32_t payloadSize);
107
114 static AppPacket<uint8_t>* convertPacket(DataPacket* p);
115
122 static size_t getPacketPayloadLength(DataPacket* p) { return p->payloadSize + sizeof(DataPacket) - sizeof(PacketHeader); }
123
130 static size_t getPacketPayloadLength(ControlPacket* p) { return p->payloadSize - (sizeof(ControlPacket) - sizeof(PacketHeader)); }
131
138 static uint8_t getMaximumPayloadLengthControlPacket(uint8_t type);
139
147 static bool isDataPacket(uint8_t type);
148
156 static bool isControlPacket(uint8_t type);
157
158#ifndef LM_GOD_MODE
159private:
160#endif
161
169 template<class T>
170 static T* createPacket(uint8_t* payload, uint8_t payloadSize) {
171 //Packet length = size of the packet + size of the payload
172 int packetLength = sizeof(T) + payloadSize;
173
174 if (packetLength > MAXPACKETSIZE) {
175 Log.warningln(F("Trying to create a packet greater than MAXPACKETSIZE"));
176 return nullptr;
177 }
178
179 T* p = static_cast<T*>(malloc(packetLength));
180
181 if (p) {
182 //Copy the payload into the packet
183 memcpy(reinterpret_cast<void*>((unsigned long) p + (sizeof(T))), payload, payloadSize);
184 } else {
185 Log.errorln(F("packet not allocated"));
186 return nullptr;
187 }
188
189 Log.traceln(F("Packet created with %d bytes"), packetLength);
190
191 return p;
192 };
193};
194
195#endif
Application packet, it is used to send the packet to the application layer.
Definition: AppPacket.h:11
Network Node.
Definition: NetworkNode.h:12