Senet Packet API
Diff: senet_packet.cpp
- Revision:
- 0:08689149c8e3
- Child:
- 1:c4435fed9eb9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/senet_packet.cpp Sat Mar 05 21:56:20 2016 +0000
@@ -0,0 +1,268 @@
+/*
+ * =====================================================================================
+ *
+ * Filename: senet_packet.cpp
+ *
+ * Description: Senet Packet Types implementation file
+ *
+ * Version: 1.0
+ * Created: 03/05/2016 04:23:40 PM
+ *
+ * Author: S. Nelson
+ * Company: Senet, Inc
+ *
+ * =====================================================================================
+ */
+
+
+#include "senet_packet.h"
+// #include <cstddef>
+
+
+int32_t SenetLoRaPacket::
+PacketHeader::serialize(uint8_t *frame, int32_t len)
+{
+ ASSERT(len > 2);
+
+ frame[0] = version;
+ frame[1] = type;
+
+ return 2;
+}
+
+bool SenetLoRaPacket::
+PacketHeader::deserialize(uint8_t *frame, int32_t len)
+{
+ if((frame != NULL) && (len >= 2))
+ {
+ version = frame[0];
+ type = frame[1];
+ return true;
+ }
+ return false;
+}
+
+bool SensorPacket::addSensorValue(uint8_t position, uint8_t type, uint16_t value)
+{
+ if (position < MAX_SENSOR_VALUES)
+ {
+ sensorValue[position].type = type;
+ sensorValue[position].value = value;
+ sensorValue[position].isSet = true;
+ return true;
+ }
+ else
+ return false;
+}
+
+int32_t SensorPacket::serializeData(uint8_t *buffer, int32_t len)
+{
+ int32_t bytes = 0;
+ int32_t dataLen = 0;
+
+ for(int32_t i = 0; i < MAX_SENSOR_VALUES; i++)
+ {
+ if(sensorValue[i].isSet == true)
+ {
+ dataLen = sensorValue[i].serialize(buffer+bytes, len - bytes);
+ if(dataLen == -1)
+ return -1;
+ bytes += dataLen;
+ }
+ }
+ return bytes;
+}
+
+bool SelfIdPacket::setDeviceType(uint32_t model, uint8_t revision)
+{
+ if((model & 0x00FFFFFF) != model)
+ return false;
+
+ deviceType = (model<<8)|revision;
+ return true;
+}
+
+bool SelfIdPacket::setSwVersion(uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developerId)
+{
+ uint8_t _major = major & 0xf;
+ uint8_t _minor = minor & 0xf;
+ uint8_t _point = point & 0x3f;
+ uint16_t _build = build & 0x3ff;
+ uint8_t _devid = developerId & 0xff;
+
+ if((_major != major) || (_minor != minor) || (_point != point) || (_build != build) || (_devid != developerId))
+ return false;
+
+ swVersion = (_major << 28) | (_minor << 24) | (_point << 18) | (_build << 8) | _devid;
+ return true;
+}
+
+void SelfIdPacket::setBatteryFailState(bool failed)
+{
+ if(failed == true)
+ powerMask |= 1 << 3;
+ else
+ powerMask &= ~(1 << 3);
+}
+
+bool SelfIdPacket::setBatteryLevel(uint8_t level)
+{
+ uint8_t _level = level & 0x7;
+
+ if(level != _level)
+ return false;
+
+ powerMask &= 0xf8;
+ powerMask |= _level;
+
+ return true;
+}
+
+bool SelfIdPacket::setExtPowerSupplyState(uint8_t id, bool isPresent)
+{
+ bool retVal = false;
+ if(id == EXT_POWER_SUPPLY_1)
+ {
+ powerMask &= 0x7F;
+ if(isPresent)
+ powerMask |= 0x80;
+ retVal = true;
+ }
+ else if(id == EXT_POWER_SUPPLY_2)
+ {
+ powerMask &= 0xBF;
+ if(isPresent)
+ powerMask |= 0x40;
+ retVal = true;
+ }
+ return retVal;
+}
+
+int32_t SelfIdPacket::serializeData(uint8_t *frame, int32_t len)
+{
+#define SELFID_PACKET_LEN 8
+
+ ASSERT(SELFID_PACKET_LEN <= len);
+
+ 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[7] = powerMask;
+
+ return SELFID_PACKET_LEN;
+}
+
+int32_t ConfigWordPacket::serializeData(uint8_t *frame, int32_t len)
+{
+#define CONTROL_PACKET_LENGTH 9
+
+ ASSERT(CONTROL_PACKET_LENGTH <= len);
+
+ 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[8] = authKey;
+
+ return CONTROL_PACKET_LENGTH;
+
+}
+
+int32_t BootInfoPacket::serializeData(uint8_t *frame, int32_t len)
+{
+#define BOOT_PACKET_LENGTH 8
+
+ ASSERT(BOOT_PACKET_LENGTH <= len);
+
+ frame[0] = (bootCount<<8) & 0xff;
+ frame[1] = bootCount & 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;
+
+ return BOOT_PACKET_LENGTH;
+}
+
+bool GpsPacket::setCoordinates(uint32_t _latitude, uint32_t _longitude, uint16_t _elevation)
+{
+
+ if(((_latitude & 0x00ffffff) != _latitude) || ((_longitude & 0x00ffffff) != _longitude))
+ return false;
+
+ latitude = _latitude;
+ longitude = _longitude;
+ elevation = _elevation;
+}
+
+int32_t GpsPacket::serializeData(uint8_t *frame, int32_t len)
+{
+#define GPS_PACKET_LENGTH 7
+
+ if(len < GPS_PACKET_LENGTH )
+ return -1;
+
+ 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] = txPower;
+
+ return GPS_PACKET_LENGTH;
+}
+
+int32_t RFDataPacket::serializeData(uint8_t *frame, int32_t len)
+{
+#define RFDATA_PACKET_LEN 8
+
+ ASSERT(len > RFDATA_PACKET_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;
+ return RFDATA_PACKET_LEN;
+
+}
+
+bool OctetStringPacket::setOctetString(uint8_t *os, uint8_t len)
+{
+ if(len > maxSize)
+ return false;
+
+ memcpy(osptr, os, len);
+ oslen = len;
+ return true;
+}
+
+int32_t OctetStringPacket::serializeData(uint8_t *frame, int32_t len)
+{
+ if(len < oslen)
+ oslen = len;
+ memcpy(frame, osptr, oslen);
+ return oslen;
+}
+