Senet Packet API

Committer:
shaunkrnelson
Date:
Sat Mar 05 21:56:20 2016 +0000
Revision:
0:08689149c8e3
Child:
1:c4435fed9eb9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shaunkrnelson 0:08689149c8e3 1 /*
shaunkrnelson 0:08689149c8e3 2 * =====================================================================================
shaunkrnelson 0:08689149c8e3 3 *
shaunkrnelson 0:08689149c8e3 4 * Filename: senet_packet.cpp
shaunkrnelson 0:08689149c8e3 5 *
shaunkrnelson 0:08689149c8e3 6 * Description: Senet Packet Types implementation file
shaunkrnelson 0:08689149c8e3 7 *
shaunkrnelson 0:08689149c8e3 8 * Version: 1.0
shaunkrnelson 0:08689149c8e3 9 * Created: 03/05/2016 04:23:40 PM
shaunkrnelson 0:08689149c8e3 10 *
shaunkrnelson 0:08689149c8e3 11 * Author: S. Nelson
shaunkrnelson 0:08689149c8e3 12 * Company: Senet, Inc
shaunkrnelson 0:08689149c8e3 13 *
shaunkrnelson 0:08689149c8e3 14 * =====================================================================================
shaunkrnelson 0:08689149c8e3 15 */
shaunkrnelson 0:08689149c8e3 16
shaunkrnelson 0:08689149c8e3 17
shaunkrnelson 0:08689149c8e3 18 #include "senet_packet.h"
shaunkrnelson 0:08689149c8e3 19 // #include <cstddef>
shaunkrnelson 0:08689149c8e3 20
shaunkrnelson 0:08689149c8e3 21
shaunkrnelson 0:08689149c8e3 22 int32_t SenetLoRaPacket::
shaunkrnelson 0:08689149c8e3 23 PacketHeader::serialize(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 24 {
shaunkrnelson 0:08689149c8e3 25 ASSERT(len > 2);
shaunkrnelson 0:08689149c8e3 26
shaunkrnelson 0:08689149c8e3 27 frame[0] = version;
shaunkrnelson 0:08689149c8e3 28 frame[1] = type;
shaunkrnelson 0:08689149c8e3 29
shaunkrnelson 0:08689149c8e3 30 return 2;
shaunkrnelson 0:08689149c8e3 31 }
shaunkrnelson 0:08689149c8e3 32
shaunkrnelson 0:08689149c8e3 33 bool SenetLoRaPacket::
shaunkrnelson 0:08689149c8e3 34 PacketHeader::deserialize(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 35 {
shaunkrnelson 0:08689149c8e3 36 if((frame != NULL) && (len >= 2))
shaunkrnelson 0:08689149c8e3 37 {
shaunkrnelson 0:08689149c8e3 38 version = frame[0];
shaunkrnelson 0:08689149c8e3 39 type = frame[1];
shaunkrnelson 0:08689149c8e3 40 return true;
shaunkrnelson 0:08689149c8e3 41 }
shaunkrnelson 0:08689149c8e3 42 return false;
shaunkrnelson 0:08689149c8e3 43 }
shaunkrnelson 0:08689149c8e3 44
shaunkrnelson 0:08689149c8e3 45 bool SensorPacket::addSensorValue(uint8_t position, uint8_t type, uint16_t value)
shaunkrnelson 0:08689149c8e3 46 {
shaunkrnelson 0:08689149c8e3 47 if (position < MAX_SENSOR_VALUES)
shaunkrnelson 0:08689149c8e3 48 {
shaunkrnelson 0:08689149c8e3 49 sensorValue[position].type = type;
shaunkrnelson 0:08689149c8e3 50 sensorValue[position].value = value;
shaunkrnelson 0:08689149c8e3 51 sensorValue[position].isSet = true;
shaunkrnelson 0:08689149c8e3 52 return true;
shaunkrnelson 0:08689149c8e3 53 }
shaunkrnelson 0:08689149c8e3 54 else
shaunkrnelson 0:08689149c8e3 55 return false;
shaunkrnelson 0:08689149c8e3 56 }
shaunkrnelson 0:08689149c8e3 57
shaunkrnelson 0:08689149c8e3 58 int32_t SensorPacket::serializeData(uint8_t *buffer, int32_t len)
shaunkrnelson 0:08689149c8e3 59 {
shaunkrnelson 0:08689149c8e3 60 int32_t bytes = 0;
shaunkrnelson 0:08689149c8e3 61 int32_t dataLen = 0;
shaunkrnelson 0:08689149c8e3 62
shaunkrnelson 0:08689149c8e3 63 for(int32_t i = 0; i < MAX_SENSOR_VALUES; i++)
shaunkrnelson 0:08689149c8e3 64 {
shaunkrnelson 0:08689149c8e3 65 if(sensorValue[i].isSet == true)
shaunkrnelson 0:08689149c8e3 66 {
shaunkrnelson 0:08689149c8e3 67 dataLen = sensorValue[i].serialize(buffer+bytes, len - bytes);
shaunkrnelson 0:08689149c8e3 68 if(dataLen == -1)
shaunkrnelson 0:08689149c8e3 69 return -1;
shaunkrnelson 0:08689149c8e3 70 bytes += dataLen;
shaunkrnelson 0:08689149c8e3 71 }
shaunkrnelson 0:08689149c8e3 72 }
shaunkrnelson 0:08689149c8e3 73 return bytes;
shaunkrnelson 0:08689149c8e3 74 }
shaunkrnelson 0:08689149c8e3 75
shaunkrnelson 0:08689149c8e3 76 bool SelfIdPacket::setDeviceType(uint32_t model, uint8_t revision)
shaunkrnelson 0:08689149c8e3 77 {
shaunkrnelson 0:08689149c8e3 78 if((model & 0x00FFFFFF) != model)
shaunkrnelson 0:08689149c8e3 79 return false;
shaunkrnelson 0:08689149c8e3 80
shaunkrnelson 0:08689149c8e3 81 deviceType = (model<<8)|revision;
shaunkrnelson 0:08689149c8e3 82 return true;
shaunkrnelson 0:08689149c8e3 83 }
shaunkrnelson 0:08689149c8e3 84
shaunkrnelson 0:08689149c8e3 85 bool SelfIdPacket::setSwVersion(uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developerId)
shaunkrnelson 0:08689149c8e3 86 {
shaunkrnelson 0:08689149c8e3 87 uint8_t _major = major & 0xf;
shaunkrnelson 0:08689149c8e3 88 uint8_t _minor = minor & 0xf;
shaunkrnelson 0:08689149c8e3 89 uint8_t _point = point & 0x3f;
shaunkrnelson 0:08689149c8e3 90 uint16_t _build = build & 0x3ff;
shaunkrnelson 0:08689149c8e3 91 uint8_t _devid = developerId & 0xff;
shaunkrnelson 0:08689149c8e3 92
shaunkrnelson 0:08689149c8e3 93 if((_major != major) || (_minor != minor) || (_point != point) || (_build != build) || (_devid != developerId))
shaunkrnelson 0:08689149c8e3 94 return false;
shaunkrnelson 0:08689149c8e3 95
shaunkrnelson 0:08689149c8e3 96 swVersion = (_major << 28) | (_minor << 24) | (_point << 18) | (_build << 8) | _devid;
shaunkrnelson 0:08689149c8e3 97 return true;
shaunkrnelson 0:08689149c8e3 98 }
shaunkrnelson 0:08689149c8e3 99
shaunkrnelson 0:08689149c8e3 100 void SelfIdPacket::setBatteryFailState(bool failed)
shaunkrnelson 0:08689149c8e3 101 {
shaunkrnelson 0:08689149c8e3 102 if(failed == true)
shaunkrnelson 0:08689149c8e3 103 powerMask |= 1 << 3;
shaunkrnelson 0:08689149c8e3 104 else
shaunkrnelson 0:08689149c8e3 105 powerMask &= ~(1 << 3);
shaunkrnelson 0:08689149c8e3 106 }
shaunkrnelson 0:08689149c8e3 107
shaunkrnelson 0:08689149c8e3 108 bool SelfIdPacket::setBatteryLevel(uint8_t level)
shaunkrnelson 0:08689149c8e3 109 {
shaunkrnelson 0:08689149c8e3 110 uint8_t _level = level & 0x7;
shaunkrnelson 0:08689149c8e3 111
shaunkrnelson 0:08689149c8e3 112 if(level != _level)
shaunkrnelson 0:08689149c8e3 113 return false;
shaunkrnelson 0:08689149c8e3 114
shaunkrnelson 0:08689149c8e3 115 powerMask &= 0xf8;
shaunkrnelson 0:08689149c8e3 116 powerMask |= _level;
shaunkrnelson 0:08689149c8e3 117
shaunkrnelson 0:08689149c8e3 118 return true;
shaunkrnelson 0:08689149c8e3 119 }
shaunkrnelson 0:08689149c8e3 120
shaunkrnelson 0:08689149c8e3 121 bool SelfIdPacket::setExtPowerSupplyState(uint8_t id, bool isPresent)
shaunkrnelson 0:08689149c8e3 122 {
shaunkrnelson 0:08689149c8e3 123 bool retVal = false;
shaunkrnelson 0:08689149c8e3 124 if(id == EXT_POWER_SUPPLY_1)
shaunkrnelson 0:08689149c8e3 125 {
shaunkrnelson 0:08689149c8e3 126 powerMask &= 0x7F;
shaunkrnelson 0:08689149c8e3 127 if(isPresent)
shaunkrnelson 0:08689149c8e3 128 powerMask |= 0x80;
shaunkrnelson 0:08689149c8e3 129 retVal = true;
shaunkrnelson 0:08689149c8e3 130 }
shaunkrnelson 0:08689149c8e3 131 else if(id == EXT_POWER_SUPPLY_2)
shaunkrnelson 0:08689149c8e3 132 {
shaunkrnelson 0:08689149c8e3 133 powerMask &= 0xBF;
shaunkrnelson 0:08689149c8e3 134 if(isPresent)
shaunkrnelson 0:08689149c8e3 135 powerMask |= 0x40;
shaunkrnelson 0:08689149c8e3 136 retVal = true;
shaunkrnelson 0:08689149c8e3 137 }
shaunkrnelson 0:08689149c8e3 138 return retVal;
shaunkrnelson 0:08689149c8e3 139 }
shaunkrnelson 0:08689149c8e3 140
shaunkrnelson 0:08689149c8e3 141 int32_t SelfIdPacket::serializeData(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 142 {
shaunkrnelson 0:08689149c8e3 143 #define SELFID_PACKET_LEN 8
shaunkrnelson 0:08689149c8e3 144
shaunkrnelson 0:08689149c8e3 145 ASSERT(SELFID_PACKET_LEN <= len);
shaunkrnelson 0:08689149c8e3 146
shaunkrnelson 0:08689149c8e3 147 frame[0] = (deviceType>>16) & 0xff;
shaunkrnelson 0:08689149c8e3 148 frame[1] = (deviceType>>8) & 0xff;
shaunkrnelson 0:08689149c8e3 149 frame[2] = deviceType & 0xff;
shaunkrnelson 0:08689149c8e3 150
shaunkrnelson 0:08689149c8e3 151 frame[3] = (swVersion >> 24) & 0xff;
shaunkrnelson 0:08689149c8e3 152 frame[4] = (swVersion >> 16) & 0xff;
shaunkrnelson 0:08689149c8e3 153 frame[5] = (swVersion >> 8) & 0xff;
shaunkrnelson 0:08689149c8e3 154 frame[6] = swVersion & 0xff;
shaunkrnelson 0:08689149c8e3 155
shaunkrnelson 0:08689149c8e3 156 frame[7] = powerMask;
shaunkrnelson 0:08689149c8e3 157
shaunkrnelson 0:08689149c8e3 158 return SELFID_PACKET_LEN;
shaunkrnelson 0:08689149c8e3 159 }
shaunkrnelson 0:08689149c8e3 160
shaunkrnelson 0:08689149c8e3 161 int32_t ConfigWordPacket::serializeData(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 162 {
shaunkrnelson 0:08689149c8e3 163 #define CONTROL_PACKET_LENGTH 9
shaunkrnelson 0:08689149c8e3 164
shaunkrnelson 0:08689149c8e3 165 ASSERT(CONTROL_PACKET_LENGTH <= len);
shaunkrnelson 0:08689149c8e3 166
shaunkrnelson 0:08689149c8e3 167 frame[0] = (config>>24) & 0xff;
shaunkrnelson 0:08689149c8e3 168 frame[1] = (config>>16) & 0xff;
shaunkrnelson 0:08689149c8e3 169 frame[2] = (config>>8) & 0xff;
shaunkrnelson 0:08689149c8e3 170 frame[3] = config & 0xff;
shaunkrnelson 0:08689149c8e3 171
shaunkrnelson 0:08689149c8e3 172 frame[4] = (mask>>24) & 0xff;
shaunkrnelson 0:08689149c8e3 173 frame[5] = (mask>>16) & 0xff;
shaunkrnelson 0:08689149c8e3 174 frame[6] = (mask>>8) & 0xff;
shaunkrnelson 0:08689149c8e3 175 frame[7] = mask & 0xff;
shaunkrnelson 0:08689149c8e3 176
shaunkrnelson 0:08689149c8e3 177 frame[8] = authKey;
shaunkrnelson 0:08689149c8e3 178
shaunkrnelson 0:08689149c8e3 179 return CONTROL_PACKET_LENGTH;
shaunkrnelson 0:08689149c8e3 180
shaunkrnelson 0:08689149c8e3 181 }
shaunkrnelson 0:08689149c8e3 182
shaunkrnelson 0:08689149c8e3 183 int32_t BootInfoPacket::serializeData(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 184 {
shaunkrnelson 0:08689149c8e3 185 #define BOOT_PACKET_LENGTH 8
shaunkrnelson 0:08689149c8e3 186
shaunkrnelson 0:08689149c8e3 187 ASSERT(BOOT_PACKET_LENGTH <= len);
shaunkrnelson 0:08689149c8e3 188
shaunkrnelson 0:08689149c8e3 189 frame[0] = (bootCount<<8) & 0xff;
shaunkrnelson 0:08689149c8e3 190 frame[1] = bootCount & 0xff;
shaunkrnelson 0:08689149c8e3 191
shaunkrnelson 0:08689149c8e3 192 frame[2] = (resetCount<<8) & 0xff;
shaunkrnelson 0:08689149c8e3 193 frame[3] = resetCount & 0xff;
shaunkrnelson 0:08689149c8e3 194
shaunkrnelson 0:08689149c8e3 195 frame[4] = (lastBootReason<<24) & 0xff;
shaunkrnelson 0:08689149c8e3 196 frame[5] = (lastBootReason<<16) & 0xff;
shaunkrnelson 0:08689149c8e3 197 frame[6] = (lastBootReason<<8) & 0xff;
shaunkrnelson 0:08689149c8e3 198
shaunkrnelson 0:08689149c8e3 199 return BOOT_PACKET_LENGTH;
shaunkrnelson 0:08689149c8e3 200 }
shaunkrnelson 0:08689149c8e3 201
shaunkrnelson 0:08689149c8e3 202 bool GpsPacket::setCoordinates(uint32_t _latitude, uint32_t _longitude, uint16_t _elevation)
shaunkrnelson 0:08689149c8e3 203 {
shaunkrnelson 0:08689149c8e3 204
shaunkrnelson 0:08689149c8e3 205 if(((_latitude & 0x00ffffff) != _latitude) || ((_longitude & 0x00ffffff) != _longitude))
shaunkrnelson 0:08689149c8e3 206 return false;
shaunkrnelson 0:08689149c8e3 207
shaunkrnelson 0:08689149c8e3 208 latitude = _latitude;
shaunkrnelson 0:08689149c8e3 209 longitude = _longitude;
shaunkrnelson 0:08689149c8e3 210 elevation = _elevation;
shaunkrnelson 0:08689149c8e3 211 }
shaunkrnelson 0:08689149c8e3 212
shaunkrnelson 0:08689149c8e3 213 int32_t GpsPacket::serializeData(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 214 {
shaunkrnelson 0:08689149c8e3 215 #define GPS_PACKET_LENGTH 7
shaunkrnelson 0:08689149c8e3 216
shaunkrnelson 0:08689149c8e3 217 if(len < GPS_PACKET_LENGTH )
shaunkrnelson 0:08689149c8e3 218 return -1;
shaunkrnelson 0:08689149c8e3 219
shaunkrnelson 0:08689149c8e3 220 frame[0] = (latitude>>16) & 0xff;
shaunkrnelson 0:08689149c8e3 221 frame[1] = (latitude>>8) & 0xff;
shaunkrnelson 0:08689149c8e3 222 frame[2] = latitude & 0xff;
shaunkrnelson 0:08689149c8e3 223
shaunkrnelson 0:08689149c8e3 224 frame[3] = (longitude>>16) & 0xff;
shaunkrnelson 0:08689149c8e3 225 frame[4] = (longitude>>8) & 0xff;
shaunkrnelson 0:08689149c8e3 226 frame[5] = longitude & 0xff;
shaunkrnelson 0:08689149c8e3 227
shaunkrnelson 0:08689149c8e3 228 frame[6] = txPower;
shaunkrnelson 0:08689149c8e3 229
shaunkrnelson 0:08689149c8e3 230 return GPS_PACKET_LENGTH;
shaunkrnelson 0:08689149c8e3 231 }
shaunkrnelson 0:08689149c8e3 232
shaunkrnelson 0:08689149c8e3 233 int32_t RFDataPacket::serializeData(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 234 {
shaunkrnelson 0:08689149c8e3 235 #define RFDATA_PACKET_LEN 8
shaunkrnelson 0:08689149c8e3 236
shaunkrnelson 0:08689149c8e3 237 ASSERT(len > RFDATA_PACKET_LEN);
shaunkrnelson 0:08689149c8e3 238
shaunkrnelson 0:08689149c8e3 239 frame[0] = channel;
shaunkrnelson 0:08689149c8e3 240 frame[1] = txpower;
shaunkrnelson 0:08689149c8e3 241 frame[2] = datarate;
shaunkrnelson 0:08689149c8e3 242 frame[3] = snr;
shaunkrnelson 0:08689149c8e3 243 frame[4] = rssi;
shaunkrnelson 0:08689149c8e3 244 frame[5] = (timestamp >> 16)& 0xff;
shaunkrnelson 0:08689149c8e3 245 frame[6] = (timestamp >> 8)& 0xff;
shaunkrnelson 0:08689149c8e3 246 frame[7] = timestamp & 0xff;
shaunkrnelson 0:08689149c8e3 247 return RFDATA_PACKET_LEN;
shaunkrnelson 0:08689149c8e3 248
shaunkrnelson 0:08689149c8e3 249 }
shaunkrnelson 0:08689149c8e3 250
shaunkrnelson 0:08689149c8e3 251 bool OctetStringPacket::setOctetString(uint8_t *os, uint8_t len)
shaunkrnelson 0:08689149c8e3 252 {
shaunkrnelson 0:08689149c8e3 253 if(len > maxSize)
shaunkrnelson 0:08689149c8e3 254 return false;
shaunkrnelson 0:08689149c8e3 255
shaunkrnelson 0:08689149c8e3 256 memcpy(osptr, os, len);
shaunkrnelson 0:08689149c8e3 257 oslen = len;
shaunkrnelson 0:08689149c8e3 258 return true;
shaunkrnelson 0:08689149c8e3 259 }
shaunkrnelson 0:08689149c8e3 260
shaunkrnelson 0:08689149c8e3 261 int32_t OctetStringPacket::serializeData(uint8_t *frame, int32_t len)
shaunkrnelson 0:08689149c8e3 262 {
shaunkrnelson 0:08689149c8e3 263 if(len < oslen)
shaunkrnelson 0:08689149c8e3 264 oslen = len;
shaunkrnelson 0:08689149c8e3 265 memcpy(frame, osptr, oslen);
shaunkrnelson 0:08689149c8e3 266 return oslen;
shaunkrnelson 0:08689149c8e3 267 }
shaunkrnelson 0:08689149c8e3 268