1#ifndef _LORAMESHER_PACKET_SERVICE_H
2#define _LORAMESHER_PACKET_SERVICE_H
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"
19 static DataPacket* dataPacket(Packet<uint8_t>* p);
31 static ControlPacket* createControlPacket(uint16_t dst, uint16_t src, uint8_t type, uint8_t* payload, uint8_t payloadSize);
43 static ControlPacket* createEmptyControlPacket(uint16_t dst, uint16_t src, uint8_t type, uint8_t seq_id, uint16_t num_packets);
55 static DataPacket* createDataPacket(uint16_t dst, uint16_t src, uint8_t type, uint8_t* payload, uint8_t payloadSize);
63 static Packet<uint8_t>* createEmptyPacket(
size_t packetSize);
74 static Packet<uint8_t>* copyPacket(T* p,
size_t packetLength) {
75 Packet<uint8_t>* cpPacket =
static_cast<Packet<uint8_t>*
>(malloc(packetLength));
78 memcpy(cpPacket, p, packetLength);
80 Log.errorln(F(
"Copy Packet not allocated"));
95 static RoutePacket* createRoutingPacket(uint16_t localAddress,
NetworkNode* nodes,
size_t numOfNodes);
106 static AppPacket<uint8_t>* createAppPacket(uint16_t dst, uint16_t src, uint8_t* payload, uint32_t payloadSize);
122 static size_t getPacketPayloadLength(DataPacket* p) {
return p->payloadSize +
sizeof(DataPacket) -
sizeof(PacketHeader); }
130 static size_t getPacketPayloadLength(ControlPacket* p) {
return p->payloadSize - (
sizeof(ControlPacket) -
sizeof(PacketHeader)); }
138 static uint8_t getMaximumPayloadLengthControlPacket(uint8_t type);
147 static bool isDataPacket(uint8_t type);
156 static bool isControlPacket(uint8_t type);
170 static T* createPacket(uint8_t* payload, uint8_t payloadSize) {
172 int packetLength =
sizeof(T) + payloadSize;
174 if (packetLength > MAXPACKETSIZE) {
175 Log.warningln(F(
"Trying to create a packet greater than MAXPACKETSIZE"));
179 T* p =
static_cast<T*
>(malloc(packetLength));
183 memcpy(
reinterpret_cast<void*
>((
unsigned long) p + (
sizeof(T))), payload, payloadSize);
185 Log.errorln(F(
"packet not allocated"));
189 Log.traceln(F(
"Packet created with %d bytes"), packetLength);
Application packet, it is used to send the packet to the application layer.
Definition: AppPacket.h:11
Network Node.
Definition: NetworkNode.h:12