Senet Packet API

Dependents:   MTDOT-UDKDemo_Senet Senet NAMote mDot-IKS01A1 unh-hackathon-example ... more

Revision:
2:9c971be7692b
Parent:
0:cc9f4010bba6
Child:
3:b6823438e893
--- a/senet_packet.cpp	Tue Mar 08 09:08:44 2016 -0500
+++ b/senet_packet.cpp	Mon May 23 14:09:52 2016 -0400
@@ -23,26 +23,32 @@
 int32_t SenetPacket::
 PacketHeader::serialize(uint8_t *frame, int32_t len)
 {
-    int32_t serializedLen = 0;
+    int32_t serializedLen = -1;
 
     if(len >= PacketHeader::HEADER_SIZE)
     {
-        frame[0] = version;
-        frame[1] = type;
-        serializedLen = PacketHeader::HEADER_SIZE;
+        serializedLen = 0;
+        frame[serializedLen++] = version;
+        frame[serializedLen++] = type;
+
+        ASSERT(serializedLen == PacketHeader::HEADER_SIZE);
     }
 
     return serializedLen;
 }
 
-bool SenetPacket::
+int32_t SenetPacket::
 PacketHeader::deserialize(uint8_t *frame, int32_t len)
 {
     if((frame != NULL) && (len >= PacketHeader::HEADER_SIZE))
     {
-        version = frame[0];
-        type    = frame[1];
-        return true;
+        int32_t offset = 0;
+        version = frame[offset++];
+        type    = frame[offset++];
+
+        ASSERT(offset == PacketHeader::HEADER_SIZE);
+
+        return  PacketHeader::HEADER_SIZE;
     }
     return false;
 }
@@ -103,6 +109,23 @@
     return -1;
 }
 
+int32_t SenetPacket::deserialize(uint8_t *frame, int32_t len)
+{
+    int32_t bytes = 0;
+
+    bytes = header.deserialize(frame, len);
+    if(bytes > 0)
+    {
+        int32_t payloadLen = deserializePayload(frame, len - bytes);
+        if(payloadLen > 0)
+            bytes += payloadLen; 
+        else
+            bytes = payloadLen; 
+
+    }
+    return bytes;
+}
+
 
 bool SensorPacket::addSensorValue(uint8_t position, uint8_t type, uint16_t value)
 {
@@ -206,16 +229,17 @@
 
     if(SELFID_PAYLOAD_LEN <= len)
     {
-        frame[0] = (deviceType>>16) & 0xff;
-        frame[1] = (deviceType>>8)  & 0xff;
-        frame[2] = deviceType & 0xff;
+        frame[0] = (deviceType>>24) & 0xff;
+        frame[1] = (deviceType>>16)  & 0xff;
+        frame[2] = (deviceType>>8)  & 0xff;
+        frame[3] = deviceType & 0xff;
 
-        frame[3] = (swVersion >> 24) & 0xff;
-        frame[4] = (swVersion >> 16) & 0xff;
-        frame[5] = (swVersion >> 8)  & 0xff;
-        frame[6] = swVersion & 0xff;
+        frame[4] = (swVersion >> 24) & 0xff;
+        frame[5] = (swVersion >> 16) & 0xff;
+        frame[6] = (swVersion >> 8)  & 0xff;
+        frame[7] = swVersion & 0xff;
 
-        frame[7] = powerMask;
+        frame[8] = powerMask;
 
         out = SELFID_PAYLOAD_LEN;
     }
@@ -247,21 +271,47 @@
 
 }
 
+int32_t ConfigWordPacket::deserializePayload(uint8_t *frame, int32_t len) 
+{
+    if(CONTROL_PAYLOAD_LENGTH <= len)
+    {
+        int32_t offset = 0;
+
+        config  = frame[offset++]<<24; 
+        config |= frame[offset++]<<16; 
+        config |= frame[offset++]<<8; 
+        config |= frame[offset++]; 
+
+        mask  = frame[offset++]<<24; 
+        mask |= frame[offset++]<<16; 
+        mask |= frame[offset++]<<8; 
+        mask |= frame[offset++]; 
+
+        authKey = frame[offset++];
+
+        return offset;
+    }
+    return -1;
+}
+
+
 int32_t BootInfoPacket::serializePayload(uint8_t *frame, int32_t len) 
 {
     int32_t out = -1;
 
     if(BOOT_PAYLOAD_LENGTH <= len)
     {
-        frame[0] = (bootCount<<8) & 0xff;
+        frame[0] = (bootCount>>8) & 0xff;
         frame[1] = bootCount & 0xff;
 
-        frame[2] = (resetCount<<8) & 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;
+        frame[7] = lastBootReason  & 0xff;
+        frame[8] = authKey;
 
         out = BOOT_PAYLOAD_LENGTH;
     }
@@ -269,12 +319,33 @@
     return out;
 }
 
-bool GpsPacket::setCoordinates(uint32_t _latitude, uint32_t _longitude, uint16_t _elevation)
+
+int32_t BootInfoPacket::deserializePayload(uint8_t *frame, int32_t len) 
 {
+    if(BOOT_PAYLOAD_LENGTH <= len)
+    {
+        int32_t offset = 0;
+
+        bootCount  = frame[offset++]<<8; 
+        bootCount |= frame[offset++]; 
+
+        resetCount  = frame[offset++]<<8; 
+        resetCount |= frame[offset++]; 
 
-    if(((_latitude & 0x00ffffff) != _latitude) || ((_longitude & 0x00ffffff) != _longitude))
-        return false;
+        lastBootReason  = frame[offset++] << 24;
+        lastBootReason |= frame[offset++] << 16;
+        lastBootReason |= frame[offset++] << 8;
+        lastBootReason |= frame[offset++];
+
+        authKey =  frame[offset++];
 
+        return offset;
+    }
+    return -1;
+}
+
+bool GpsPacket::setCoordinates(int32_t _latitude, int32_t _longitude, uint16_t _elevation)
+{
     latitude  = _latitude; 
     longitude = _longitude; 
     elevation = _elevation;
@@ -317,10 +388,11 @@
         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;
+        frame[4] = (rssi >>8) & 0xff;
+        frame[5] = rssi & 0xff;
+        frame[6] = (timestamp >> 16) & 0xff;
+        frame[7] = (timestamp >> 8)  & 0xff;
+        frame[8] = timestamp & 0xff;
         out = RFDATA_PAYLOAD_LEN;
     }
     return out;