Senet Packet API
Revision 1:c4435fed9eb9, committed 2016-03-07
- Comitter:
- shaunkrnelson
- Date:
- Mon Mar 07 12:08:34 2016 -0500
- Parent:
- 0:08689149c8e3
- Child:
- 2:6b85f72fe93b
- Commit message:
- Add buffer parameter to packet constructor
General cleanup
Changed in this revision
| senet_packet.cpp | Show annotated file Show diff for this revision Revisions of this file |
| senet_packet.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/senet_packet.cpp Sat Mar 05 21:56:20 2016 +0000
+++ b/senet_packet.cpp Mon Mar 07 12:08:34 2016 -0500
@@ -13,27 +13,32 @@
*
* =====================================================================================
*/
+#include "senet_packet.h"
+#include <assert.h>
+#include <stdio.h>
+
+#define ASSERT(_expr) assert(_expr)
-#include "senet_packet.h"
-// #include <cstddef>
-
-
-int32_t SenetLoRaPacket::
+int32_t SenetPacket::
PacketHeader::serialize(uint8_t *frame, int32_t len)
{
- ASSERT(len > 2);
+ int32_t serializedLen = 0;
- frame[0] = version;
- frame[1] = type;
+ if(len >= PacketHeader::HEADER_SIZE)
+ {
+ frame[0] = version;
+ frame[1] = type;
+ serializedLen = PacketHeader::HEADER_SIZE;
+ }
- return 2;
+ return serializedLen;
}
-bool SenetLoRaPacket::
+bool SenetPacket::
PacketHeader::deserialize(uint8_t *frame, int32_t len)
{
- if((frame != NULL) && (len >= 2))
+ if((frame != NULL) && (len >= PacketHeader::HEADER_SIZE))
{
version = frame[0];
type = frame[1];
@@ -42,6 +47,63 @@
return false;
}
+
+SenetPacket::SenetPacket(uint8_t senetPktType, uint8_t *_buffer, uint8_t _buflen)
+{
+ header.type = senetPktType;
+ header.version = VERSION;
+ pktLen = 0;
+
+ if(_buffer != NULL)
+ {
+ buffer = _buffer;
+ bufferLen = _buflen;
+ ownBuffer = false;
+ }
+ else
+ {
+ if(_buflen != 0)
+ bufferLen = _buflen;
+ else
+ bufferLen = MAX_FRAME_SIZE;
+
+ buffer = new uint8_t[bufferLen];
+ ASSERT(buffer != NULL);
+ ownBuffer = true;
+ }
+ memset(buffer, 0, bufferLen);
+}
+
+
+SenetPacket::~SenetPacket()
+{
+ if(ownBuffer == true)
+ delete buffer;
+}
+
+
+int32_t SenetPacket::serialize()
+{
+ pktLen = header.serialize(buffer, bufferLen);
+ ASSERT(pktLen > 0);
+
+ if(pktLen > 0)
+ {
+ int32_t payloadLen = serializePayload(buffer + pktLen, bufferLen - pktLen);
+
+ ASSERT(payloadLen > 0);
+
+ if(payloadLen > 0)
+ {
+ pktLen += payloadLen;
+ return pktLen;
+ }
+ }
+
+ return -1;
+}
+
+
bool SensorPacket::addSensorValue(uint8_t position, uint8_t type, uint16_t value)
{
if (position < MAX_SENSOR_VALUES)
@@ -55,7 +117,7 @@
return false;
}
-int32_t SensorPacket::serializeData(uint8_t *buffer, int32_t len)
+int32_t SensorPacket::serializePayload(uint8_t *buffer, int32_t len)
{
int32_t bytes = 0;
int32_t dataLen = 0;
@@ -138,65 +200,73 @@
return retVal;
}
-int32_t SelfIdPacket::serializeData(uint8_t *frame, int32_t len)
+int32_t SelfIdPacket::serializePayload(uint8_t *frame, int32_t len)
{
-#define SELFID_PACKET_LEN 8
+ int32_t out = -1;
- ASSERT(SELFID_PACKET_LEN <= len);
+ if(SELFID_PAYLOAD_LEN <= len)
+ {
+ frame[0] = (deviceType>>16) & 0xff;
+ frame[1] = (deviceType>>8) & 0xff;
+ frame[2] = deviceType & 0xff;
- frame[0] = (deviceType>>16) & 0xff;
- frame[1] = (deviceType>>8) & 0xff;
- frame[2] = deviceType & 0xff;
+ frame[3] = (swVersion >> 24) & 0xff;
+ frame[4] = (swVersion >> 16) & 0xff;
+ frame[5] = (swVersion >> 8) & 0xff;
+ frame[6] = swVersion & 0xff;
- frame[3] = (swVersion >> 24) & 0xff;
- frame[4] = (swVersion >> 16) & 0xff;
- frame[5] = (swVersion >> 8) & 0xff;
- frame[6] = swVersion & 0xff;
+ frame[7] = powerMask;
- frame[7] = powerMask;
+ out = SELFID_PAYLOAD_LEN;
+ }
- return SELFID_PACKET_LEN;
+ return out;
}
-int32_t ConfigWordPacket::serializeData(uint8_t *frame, int32_t len)
+int32_t ConfigWordPacket::serializePayload(uint8_t *frame, int32_t len)
{
-#define CONTROL_PACKET_LENGTH 9
+ int32_t out = -1;
- ASSERT(CONTROL_PACKET_LENGTH <= len);
+ if(CONTROL_PAYLOAD_LENGTH <= len)
+ {
+ frame[0] = (config>>24) & 0xff;
+ frame[1] = (config>>16) & 0xff;
+ frame[2] = (config>>8) & 0xff;
+ frame[3] = config & 0xff;
- frame[0] = (config>>24) & 0xff;
- frame[1] = (config>>16) & 0xff;
- frame[2] = (config>>8) & 0xff;
- frame[3] = config & 0xff;
+ frame[4] = (mask>>24) & 0xff;
+ frame[5] = (mask>>16) & 0xff;
+ frame[6] = (mask>>8) & 0xff;
+ frame[7] = mask & 0xff;
- frame[4] = (mask>>24) & 0xff;
- frame[5] = (mask>>16) & 0xff;
- frame[6] = (mask>>8) & 0xff;
- frame[7] = mask & 0xff;
+ frame[8] = authKey;
+ out = CONTROL_PAYLOAD_LENGTH;
+ }
- frame[8] = authKey;
-
- return CONTROL_PACKET_LENGTH;
+ return out;
}
-int32_t BootInfoPacket::serializeData(uint8_t *frame, int32_t len)
+int32_t BootInfoPacket::serializePayload(uint8_t *frame, int32_t len)
{
-#define BOOT_PACKET_LENGTH 8
+ int32_t out = -1;
- ASSERT(BOOT_PACKET_LENGTH <= len);
-
- frame[0] = (bootCount<<8) & 0xff;
- frame[1] = bootCount & 0xff;
+ if(BOOT_PAYLOAD_LENGTH <= len)
+ {
+ frame[0] = (bootCount<<8) & 0xff;
+ frame[1] = bootCount & 0xff;
- frame[2] = (resetCount<<8) & 0xff;
- frame[3] = resetCount & 0xff;
+ frame[2] = (resetCount<<8) & 0xff;
+ frame[3] = resetCount & 0xff;
- frame[4] = (lastBootReason<<24) & 0xff;
- frame[5] = (lastBootReason<<16) & 0xff;
- frame[6] = (lastBootReason<<8) & 0xff;
+ frame[4] = (lastBootReason<<24) & 0xff;
+ frame[5] = (lastBootReason<<16) & 0xff;
+ frame[6] = (lastBootReason<<8) & 0xff;
- return BOOT_PACKET_LENGTH;
+ out = BOOT_PAYLOAD_LENGTH;
+ }
+
+ return out;
}
bool GpsPacket::setCoordinates(uint32_t _latitude, uint32_t _longitude, uint16_t _elevation)
@@ -208,61 +278,82 @@
latitude = _latitude;
longitude = _longitude;
elevation = _elevation;
+
+ return true;
}
-int32_t GpsPacket::serializeData(uint8_t *frame, int32_t len)
+int32_t GpsPacket::serializePayload(uint8_t *frame, int32_t len)
{
-#define GPS_PACKET_LENGTH 7
+ int32_t out = -1;
- if(len < GPS_PACKET_LENGTH )
- return -1;
+ if(GPS_PAYLOAD_LENGTH <= len)
+ {
+ frame[0] = (latitude>>16) & 0xff;
+ frame[1] = (latitude>>8) & 0xff;
+ frame[2] = latitude & 0xff;
- frame[0] = (latitude>>16) & 0xff;
- frame[1] = (latitude>>8) & 0xff;
- frame[2] = latitude & 0xff;
+ frame[3] = (longitude>>16) & 0xff;
+ frame[4] = (longitude>>8) & 0xff;
+ frame[5] = longitude & 0xff;
+
+ frame[6] = (elevation>>8) & 0xff;
+ frame[7] = elevation & 0xff;
- frame[3] = (longitude>>16) & 0xff;
- frame[4] = (longitude>>8) & 0xff;
- frame[5] = longitude & 0xff;
+ frame[8] = txPower;
- frame[6] = txPower;
+ out = GPS_PAYLOAD_LENGTH;
+ }
- return GPS_PACKET_LENGTH;
+ return out;
}
-int32_t RFDataPacket::serializeData(uint8_t *frame, int32_t len)
+int32_t RFDataPacket::serializePayload(uint8_t *frame, int32_t len)
{
-#define RFDATA_PACKET_LEN 8
-
- ASSERT(len > RFDATA_PACKET_LEN);
+ int32_t out = -1;
- frame[0] = channel;
- frame[1] = txpower;
- frame[2] = datarate;
- frame[3] = snr;
- frame[4] = rssi;
- frame[5] = (timestamp >> 16)& 0xff;
- frame[6] = (timestamp >> 8)& 0xff;
- frame[7] = timestamp & 0xff;
- return RFDATA_PACKET_LEN;
+ if(RFDATA_PAYLOAD_LEN <= len)
+ {
+ frame[0] = channel;
+ frame[1] = txpower;
+ frame[2] = datarate;
+ frame[3] = snr;
+ frame[4] = rssi;
+ frame[5] = (timestamp >> 16)& 0xff;
+ frame[6] = (timestamp >> 8)& 0xff;
+ frame[7] = timestamp & 0xff;
+ out = RFDATA_PAYLOAD_LEN;
+ }
+ return out;
+}
+
+OctetStringPacket::OctetStringPacket(uint8_t size) :
+ SenetPacket(OCTET_STRING_PACKET, NULL, size + SenetPacket::PacketHeader::HEADER_SIZE)
+{
+ max = size;
+ oslen = 0;
}
bool OctetStringPacket::setOctetString(uint8_t *os, uint8_t len)
{
- if(len > maxSize)
+ if(len > max)
return false;
- memcpy(osptr, os, len);
oslen = len;
+ memcpy(buffer+PacketHeader::HEADER_SIZE, os, oslen);
return true;
}
-int32_t OctetStringPacket::serializeData(uint8_t *frame, int32_t len)
+int32_t OctetStringPacket::serializePayload(uint8_t *frame, int32_t len)
{
- if(len < oslen)
- oslen = len;
- memcpy(frame, osptr, oslen);
- return oslen;
+ int32_t out = -1;
+
+ if(oslen >= len)
+ {
+ memcpy(frame, buffer + PacketHeader::HEADER_SIZE, oslen);
+ out = oslen;
+ }
+
+ return out;
}
--- a/senet_packet.h Sat Mar 05 21:56:20 2016 +0000
+++ b/senet_packet.h Mon Mar 07 12:08:34 2016 -0500
@@ -14,11 +14,13 @@
*
* =====================================================================================
*/
+
+#ifndef __SENET_PACKET__
+#define __SENET_PACKET__
+
#include <stdint.h>
-#include <assert.h>
#include <string.h>
-#define ASSERT(_expr) assert(_expr)
// Senet packet types
enum SenetPacketT
@@ -36,19 +38,33 @@
/*
* =====================================================================================
- * Class: SenetLoRaPacket
+ * Class: SenetPacket
* Description: Senet Packet Base class
* =====================================================================================
*/
-struct SenetLoRaPacket
+struct SenetPacket
{
static const uint32_t MAX_FRAME_SIZE = 242;
static const uint8_t VERSION = 1;
+ /*
+ *--------------------------------------------------------------------------------------
+ * Class: SenetPacket
+ * Method: serialize
+ * Description: Packet serializer
+ *--------------------------------------------------------------------------------------
+ */
+ int32_t serialize();
+ inline const uint8_t* payload() { return buffer;}
+ uint8_t length () { return pktLen; }
+
+ protected:
// Common packet header
struct PacketHeader
{
+ static const uint8_t HEADER_SIZE = 2;
+
uint8_t version; // packet format versioni
uint8_t type; // Senet packet type
@@ -62,45 +78,22 @@
bool deserialize(uint8_t *frame, int32_t len);
} header;
- uint8_t pkt[MAX_FRAME_SIZE]; // serialized packet buffer
- uint8_t pktLen; // serialized packet length
+ uint8_t pktLen;
+ uint8_t *buffer;
+ uint8_t bufferLen;
+ bool ownBuffer;
- SenetLoRaPacket(uint8_t senetPktType)
- {
- header.type = senetPktType;
- header.version = VERSION;
- pktLen = 0;
- }
+ SenetPacket(uint8_t senetPktType, uint8_t *_buffer=NULL, uint8_t _buflen=0);
+ ~SenetPacket();
/*
*--------------------------------------------------------------------------------------
- * Class: SenetLoRaPacket
- * Method: serialize
- * Description: Packet serializer
+ * Class: SenetPacket
+ * Method: serializePayload
+ * Description: Each unique packet type implements this to serialize its payload
*--------------------------------------------------------------------------------------
*/
- virtual int32_t serialize()
- {
- pktLen = header.serialize(pkt, MAX_FRAME_SIZE);
-
- int32_t payloadLen = serializeData(pkt + pktLen, MAX_FRAME_SIZE - pktLen);
- if(payloadLen > 0)
- {
- pktLen += payloadLen;
- return pktLen;
- }
- return -1;
- }
-
-
- /*
- *--------------------------------------------------------------------------------------
- * Class: SenetLoRaPacket
- * Method: serializeData
- * Description: Each unique packet type implements this to serialize its data
- *--------------------------------------------------------------------------------------
- */
- virtual int32_t serializeData(uint8_t *frame, int32_t len) {return 0;}
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len) = 0;
};
@@ -111,14 +104,16 @@
* Description: Device Boot information packet
* =====================================================================================
*/
-struct BootInfoPacket : public SenetLoRaPacket
+struct BootInfoPacket : public SenetPacket
{
+ static const uint8_t BOOT_PAYLOAD_LENGTH = 8;
+
uint16_t bootCount; // number of device boots
uint16_t resetCount; // number of device resets
uint32_t lastBootReason; // last boot reason
- BootInfoPacket() :
- SenetLoRaPacket(BOOT_INFO_PACKET)
+ BootInfoPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
+ SenetPacket(BOOT_INFO_PACKET, _buffer, _buflen)
{
bootCount = 0;
resetCount = 0;
@@ -129,11 +124,11 @@
/*
*--------------------------------------------------------------------------------------
* Class: BootInfoPacket
- * Method: serializeData
+ * Method: serializePayload
* Description: Serialize packet data
*--------------------------------------------------------------------------------------
*/
- int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
@@ -143,10 +138,12 @@
* Description: Packet to configure device
* =====================================================================================
*/
-struct ConfigWordPacket : public SenetLoRaPacket
+struct ConfigWordPacket : public SenetPacket
{
- ConfigWordPacket() :
- SenetLoRaPacket(CONTROL_PACKET) { config = 0; mask = 0; authKey = 0; }
+ static const uint8_t CONTROL_PAYLOAD_LENGTH = 9;
+
+ ConfigWordPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
+ SenetPacket(CONTROL_PACKET, _buffer, _buflen) { config = 0; mask = 0; authKey = 0; }
uint32_t config; // configuration word
uint32_t mask; // valid bit mask applied to configuration word
@@ -155,11 +152,11 @@
/*
*--------------------------------------------------------------------------------------
* Class: ConfigWordPacket
- * Method: serializeData
+ * Method: serializePayload
* Description: Serialize packet data
*--------------------------------------------------------------------------------------
*/
- int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
@@ -169,13 +166,16 @@
* Description: Transmit device location in Decimal degress (http://www.en.wikipedia.org/wiki/Decimal_degrees)
* =====================================================================================
*/
-struct GpsPacket : public SenetLoRaPacket
+struct GpsPacket : public SenetPacket
{
+ static const uint8_t GPS_PAYLOAD_LENGTH = 9;
+
bool setCoordinates(uint32_t latitude, uint32_t longitude, uint16_t elevation);
inline void setTxPower(uint8_t dBm) { txPower = dBm; }
- GpsPacket():
- SenetLoRaPacket(GPS_PACKET)
+
+ GpsPacket(uint8_t* _buffer=NULL, uint8_t _buflen=0):
+ SenetPacket(GPS_PACKET, _buffer, _buflen)
{
latitude = 0;
longitude = 0;
@@ -192,11 +192,11 @@
/*
*--------------------------------------------------------------------------------------
* Class: GpsPacket
- * Method: serializeData
+ * Method: serializePayload
* Description: Serialize the data
*--------------------------------------------------------------------------------------
*/
- int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
@@ -206,36 +206,29 @@
* Description: Variable length Octet String packet
* =====================================================================================
*/
-struct OctetStringPacket : public SenetLoRaPacket
+struct OctetStringPacket : public SenetPacket
{
bool setOctetString(uint8_t *os, uint8_t len);
- OctetStringPacket(uint8_t size)
- : SenetLoRaPacket(OCTET_STRING_PACKET)
- {
- osptr = new uint8_t[size];
- ASSERT(osptr != NULL);
- maxSize = size;
- oslen = 0;
- }
+ OctetStringPacket(uint8_t size);
protected:
- uint8_t *osptr;
- uint8_t maxSize;
uint8_t oslen;
+ uint8_t max;
- virtual int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
-
/*
* =====================================================================================
* Class: RFDataPacket
* Description: Radio Data packet
* =====================================================================================
*/
-struct RFDataPacket : public SenetLoRaPacket
+struct RFDataPacket : public SenetPacket
{
+ static const uint8_t RFDATA_PAYLOAD_LEN = 8;
+
uint8_t channel; // The channel the device sent on
uint8_t txpower; // The transmit power in dBm used by the device
uint8_t datarate; // The datarate used by the device
@@ -243,8 +236,8 @@
uint8_t rssi; // RSSI of the last frame received
uint32_t timestamp; // The device's current timestamp
- RFDataPacket():
- SenetLoRaPacket(RF_PACKET)
+ RFDataPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0):
+ SenetPacket(RF_PACKET, _buffer, _buflen)
{
channel = 0;
txpower = 0;
@@ -257,11 +250,11 @@
/*
*--------------------------------------------------------------------------------------
* Class: RFDataPacket
- * Method: serializeData
+ * Method: serializePayload
* Description: Serialize the data
*--------------------------------------------------------------------------------------
*/
- int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
@@ -271,12 +264,13 @@
* Description:
* =====================================================================================
*/
-struct SelfIdPacket : public SenetLoRaPacket
+struct SelfIdPacket : public SenetPacket
{
const static uint8_t EXT_POWER_SUPPLY_ID_MAX = 2;
const static uint8_t EXT_POWER_SUPPLY_1 = 1;
const static uint8_t EXT_POWER_SUPPLY_2 = 2;
const static uint8_t BATTERY_LEVEL_MAX = 7;
+ const static uint8_t SELFID_PAYLOAD_LEN = 8;
bool setDeviceType (uint32_t model, uint8_t revision);
bool setSwVersion (uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developer);
@@ -284,8 +278,8 @@
bool setBatteryLevel (uint8_t level);
bool setExtPowerSupplyState (uint8_t id, bool isPresent);
- SelfIdPacket() :
- SenetLoRaPacket(SELF_ID_PACKET) { deviceType = 0; swVersion = 0; powerMask = 0; }
+ SelfIdPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
+ SenetPacket(SELF_ID_PACKET, _buffer, _buflen) { deviceType = 0; swVersion = 0; powerMask = 0; }
protected:
uint32_t deviceType;
@@ -295,11 +289,11 @@
/*
*--------------------------------------------------------------------------------------
* Class: SelfIdPacket
- * Method: serializeData
+ * Method: serializePayload
* Description: Serialize the data
*--------------------------------------------------------------------------------------
*/
- virtual int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
@@ -309,14 +303,14 @@
* Description:
* =====================================================================================
*/
-struct SensorPacket : public SenetLoRaPacket
+struct SensorPacket : public SenetPacket
{
bool setPrimarySensor(uint16_t value) { return addSensorValue(0,1, value);};
bool setTemperature (uint16_t value) { return addSensorValue(1,2, value);}
void reset();
- SensorPacket() :
- SenetLoRaPacket(SENSOR_PACKET) {}
+ SensorPacket(uint8_t *_buffer=NULL, uint8_t _buflen=0) :
+ SenetPacket(SENSOR_PACKET, _buffer, _buflen) {}
protected:
static const uint8_t MAX_SENSOR_VALUES = 2;
@@ -346,10 +340,13 @@
/*
*--------------------------------------------------------------------------------------
* Class: SelfIdPacket
- * Method: serializeData
+ * Method: serializePayload
* Description: Serialize the data
*--------------------------------------------------------------------------------------
*/
- virtual int32_t serializeData(uint8_t *frame, int32_t len);
+ virtual int32_t serializePayload(uint8_t *frame, int32_t len);
};
+
+#endif // __SENET_PACKET__
+