Senet Packet API

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

Committer:
shaunkrnelson
Date:
Thu Aug 11 12:48:46 2016 +0000
Revision:
4:7d43feca0e3d
Parent:
3:b6823438e893
Child:
6:17e4e5f99d0c
remove calls to assert()  as it is not defined in the mbed toolchain

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shaunkrnelson 0:cc9f4010bba6 1 /*
shaunkrnelson 0:cc9f4010bba6 2 * =====================================================================================
shaunkrnelson 0:cc9f4010bba6 3 *
shaunkrnelson 0:cc9f4010bba6 4 * Filename: senet_packet.cpp
shaunkrnelson 0:cc9f4010bba6 5 *
shaunkrnelson 0:cc9f4010bba6 6 * Description: Senet Packet Types implementation file
shaunkrnelson 0:cc9f4010bba6 7 *
shaunkrnelson 0:cc9f4010bba6 8 * Version: 1.0
shaunkrnelson 0:cc9f4010bba6 9 * Created: 03/05/2016 04:23:40 PM
shaunkrnelson 0:cc9f4010bba6 10 *
shaunkrnelson 0:cc9f4010bba6 11 * Author: S. Nelson
shaunkrnelson 0:cc9f4010bba6 12 * Company: Senet, Inc
shaunkrnelson 0:cc9f4010bba6 13 *
shaunkrnelson 0:cc9f4010bba6 14 * =====================================================================================
shaunkrnelson 0:cc9f4010bba6 15 */
shaunkrnelson 0:cc9f4010bba6 16 #include "senet_packet.h"
shaunkrnelson 0:cc9f4010bba6 17 #include <stdio.h>
shaunkrnelson 0:cc9f4010bba6 18
shaunkrnelson 4:7d43feca0e3d 19 #define ASSERT(_expr)
shaunkrnelson 0:cc9f4010bba6 20
shaunkrnelson 0:cc9f4010bba6 21
shaunkrnelson 0:cc9f4010bba6 22 int32_t SenetPacket::
shaunkrnelson 0:cc9f4010bba6 23 PacketHeader::serialize(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 24 {
shaunkrnelson 1:3e0cc8ad24f1 25 int32_t serializedLen = -1;
shaunkrnelson 0:cc9f4010bba6 26
shaunkrnelson 0:cc9f4010bba6 27 if(len >= PacketHeader::HEADER_SIZE)
shaunkrnelson 0:cc9f4010bba6 28 {
shaunkrnelson 1:3e0cc8ad24f1 29 serializedLen = 0;
shaunkrnelson 1:3e0cc8ad24f1 30 frame[serializedLen++] = version;
shaunkrnelson 1:3e0cc8ad24f1 31 frame[serializedLen++] = type;
shaunkrnelson 1:3e0cc8ad24f1 32
shaunkrnelson 1:3e0cc8ad24f1 33 ASSERT(serializedLen == PacketHeader::HEADER_SIZE);
shaunkrnelson 0:cc9f4010bba6 34 }
shaunkrnelson 0:cc9f4010bba6 35
shaunkrnelson 0:cc9f4010bba6 36 return serializedLen;
shaunkrnelson 0:cc9f4010bba6 37 }
shaunkrnelson 0:cc9f4010bba6 38
shaunkrnelson 1:3e0cc8ad24f1 39 int32_t SenetPacket::
shaunkrnelson 0:cc9f4010bba6 40 PacketHeader::deserialize(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 41 {
shaunkrnelson 0:cc9f4010bba6 42 if((frame != NULL) && (len >= PacketHeader::HEADER_SIZE))
shaunkrnelson 0:cc9f4010bba6 43 {
shaunkrnelson 1:3e0cc8ad24f1 44 int32_t offset = 0;
shaunkrnelson 1:3e0cc8ad24f1 45 version = frame[offset++];
shaunkrnelson 1:3e0cc8ad24f1 46 type = frame[offset++];
shaunkrnelson 1:3e0cc8ad24f1 47
shaunkrnelson 1:3e0cc8ad24f1 48 ASSERT(offset == PacketHeader::HEADER_SIZE);
shaunkrnelson 1:3e0cc8ad24f1 49
shaunkrnelson 1:3e0cc8ad24f1 50 return PacketHeader::HEADER_SIZE;
shaunkrnelson 0:cc9f4010bba6 51 }
shaunkrnelson 0:cc9f4010bba6 52 return false;
shaunkrnelson 0:cc9f4010bba6 53 }
shaunkrnelson 0:cc9f4010bba6 54
shaunkrnelson 0:cc9f4010bba6 55
shaunkrnelson 0:cc9f4010bba6 56 SenetPacket::SenetPacket(uint8_t senetPktType, uint8_t *_buffer, uint8_t _buflen)
shaunkrnelson 0:cc9f4010bba6 57 {
shaunkrnelson 0:cc9f4010bba6 58 header.type = senetPktType;
shaunkrnelson 0:cc9f4010bba6 59 header.version = VERSION;
shaunkrnelson 0:cc9f4010bba6 60 pktLen = 0;
shaunkrnelson 0:cc9f4010bba6 61
shaunkrnelson 0:cc9f4010bba6 62 if(_buffer != NULL)
shaunkrnelson 0:cc9f4010bba6 63 {
shaunkrnelson 0:cc9f4010bba6 64 buffer = _buffer;
shaunkrnelson 0:cc9f4010bba6 65 bufferLen = _buflen;
shaunkrnelson 0:cc9f4010bba6 66 ownBuffer = false;
shaunkrnelson 0:cc9f4010bba6 67 }
shaunkrnelson 0:cc9f4010bba6 68 else
shaunkrnelson 0:cc9f4010bba6 69 {
shaunkrnelson 0:cc9f4010bba6 70 if(_buflen != 0)
shaunkrnelson 0:cc9f4010bba6 71 bufferLen = _buflen;
shaunkrnelson 0:cc9f4010bba6 72 else
shaunkrnelson 0:cc9f4010bba6 73 bufferLen = MAX_FRAME_SIZE;
shaunkrnelson 0:cc9f4010bba6 74
shaunkrnelson 0:cc9f4010bba6 75 buffer = new uint8_t[bufferLen];
shaunkrnelson 0:cc9f4010bba6 76 ASSERT(buffer != NULL);
shaunkrnelson 0:cc9f4010bba6 77 ownBuffer = true;
shaunkrnelson 0:cc9f4010bba6 78 }
shaunkrnelson 0:cc9f4010bba6 79 memset(buffer, 0, bufferLen);
shaunkrnelson 0:cc9f4010bba6 80 }
shaunkrnelson 0:cc9f4010bba6 81
shaunkrnelson 0:cc9f4010bba6 82
shaunkrnelson 0:cc9f4010bba6 83 SenetPacket::~SenetPacket()
shaunkrnelson 0:cc9f4010bba6 84 {
shaunkrnelson 0:cc9f4010bba6 85 if(ownBuffer == true)
shaunkrnelson 0:cc9f4010bba6 86 delete buffer;
shaunkrnelson 0:cc9f4010bba6 87 }
shaunkrnelson 0:cc9f4010bba6 88
shaunkrnelson 0:cc9f4010bba6 89
shaunkrnelson 0:cc9f4010bba6 90 int32_t SenetPacket::serialize()
shaunkrnelson 0:cc9f4010bba6 91 {
shaunkrnelson 0:cc9f4010bba6 92 pktLen = header.serialize(buffer, bufferLen);
shaunkrnelson 0:cc9f4010bba6 93 ASSERT(pktLen > 0);
shaunkrnelson 0:cc9f4010bba6 94
shaunkrnelson 0:cc9f4010bba6 95 if(pktLen > 0)
shaunkrnelson 0:cc9f4010bba6 96 {
shaunkrnelson 0:cc9f4010bba6 97 int32_t payloadLen = serializePayload(buffer + pktLen, bufferLen - pktLen);
shaunkrnelson 0:cc9f4010bba6 98
shaunkrnelson 0:cc9f4010bba6 99 ASSERT(payloadLen > 0);
shaunkrnelson 0:cc9f4010bba6 100
shaunkrnelson 0:cc9f4010bba6 101 if(payloadLen > 0)
shaunkrnelson 0:cc9f4010bba6 102 {
shaunkrnelson 0:cc9f4010bba6 103 pktLen += payloadLen;
shaunkrnelson 0:cc9f4010bba6 104 return pktLen;
shaunkrnelson 0:cc9f4010bba6 105 }
shaunkrnelson 0:cc9f4010bba6 106 }
shaunkrnelson 0:cc9f4010bba6 107
shaunkrnelson 0:cc9f4010bba6 108 return -1;
shaunkrnelson 0:cc9f4010bba6 109 }
shaunkrnelson 0:cc9f4010bba6 110
shaunkrnelson 1:3e0cc8ad24f1 111 int32_t SenetPacket::deserialize(uint8_t *frame, int32_t len)
shaunkrnelson 1:3e0cc8ad24f1 112 {
shaunkrnelson 1:3e0cc8ad24f1 113 int32_t bytes = 0;
shaunkrnelson 1:3e0cc8ad24f1 114
shaunkrnelson 1:3e0cc8ad24f1 115 bytes = header.deserialize(frame, len);
shaunkrnelson 1:3e0cc8ad24f1 116 if(bytes > 0)
shaunkrnelson 1:3e0cc8ad24f1 117 {
shaunkrnelson 1:3e0cc8ad24f1 118 int32_t payloadLen = deserializePayload(frame, len - bytes);
shaunkrnelson 1:3e0cc8ad24f1 119 if(payloadLen > 0)
shaunkrnelson 1:3e0cc8ad24f1 120 bytes += payloadLen;
shaunkrnelson 1:3e0cc8ad24f1 121 else
shaunkrnelson 1:3e0cc8ad24f1 122 bytes = payloadLen;
shaunkrnelson 1:3e0cc8ad24f1 123
shaunkrnelson 1:3e0cc8ad24f1 124 }
shaunkrnelson 1:3e0cc8ad24f1 125 return bytes;
shaunkrnelson 1:3e0cc8ad24f1 126 }
shaunkrnelson 1:3e0cc8ad24f1 127
shaunkrnelson 0:cc9f4010bba6 128
shaunkrnelson 0:cc9f4010bba6 129 bool SensorPacket::addSensorValue(uint8_t position, uint8_t type, uint16_t value)
shaunkrnelson 0:cc9f4010bba6 130 {
shaunkrnelson 0:cc9f4010bba6 131 if (position < MAX_SENSOR_VALUES)
shaunkrnelson 0:cc9f4010bba6 132 {
shaunkrnelson 0:cc9f4010bba6 133 sensorValue[position].type = type;
shaunkrnelson 0:cc9f4010bba6 134 sensorValue[position].value = value;
shaunkrnelson 0:cc9f4010bba6 135 sensorValue[position].isSet = true;
shaunkrnelson 0:cc9f4010bba6 136 return true;
shaunkrnelson 0:cc9f4010bba6 137 }
shaunkrnelson 0:cc9f4010bba6 138 else
shaunkrnelson 0:cc9f4010bba6 139 return false;
shaunkrnelson 0:cc9f4010bba6 140 }
shaunkrnelson 0:cc9f4010bba6 141
shaunkrnelson 0:cc9f4010bba6 142 int32_t SensorPacket::serializePayload(uint8_t *buffer, int32_t len)
shaunkrnelson 0:cc9f4010bba6 143 {
shaunkrnelson 0:cc9f4010bba6 144 int32_t bytes = 0;
shaunkrnelson 0:cc9f4010bba6 145 int32_t dataLen = 0;
shaunkrnelson 0:cc9f4010bba6 146
shaunkrnelson 0:cc9f4010bba6 147 for(int32_t i = 0; i < MAX_SENSOR_VALUES; i++)
shaunkrnelson 0:cc9f4010bba6 148 {
shaunkrnelson 0:cc9f4010bba6 149 if(sensorValue[i].isSet == true)
shaunkrnelson 0:cc9f4010bba6 150 {
shaunkrnelson 0:cc9f4010bba6 151 dataLen = sensorValue[i].serialize(buffer+bytes, len - bytes);
shaunkrnelson 0:cc9f4010bba6 152 if(dataLen == -1)
shaunkrnelson 0:cc9f4010bba6 153 return -1;
shaunkrnelson 0:cc9f4010bba6 154 bytes += dataLen;
shaunkrnelson 0:cc9f4010bba6 155 }
shaunkrnelson 0:cc9f4010bba6 156 }
shaunkrnelson 0:cc9f4010bba6 157 return bytes;
shaunkrnelson 0:cc9f4010bba6 158 }
shaunkrnelson 0:cc9f4010bba6 159
shaunkrnelson 0:cc9f4010bba6 160 bool SelfIdPacket::setDeviceType(uint32_t model, uint8_t revision)
shaunkrnelson 0:cc9f4010bba6 161 {
shaunkrnelson 0:cc9f4010bba6 162 if((model & 0x00FFFFFF) != model)
shaunkrnelson 0:cc9f4010bba6 163 return false;
shaunkrnelson 0:cc9f4010bba6 164
shaunkrnelson 0:cc9f4010bba6 165 deviceType = (model<<8)|revision;
shaunkrnelson 0:cc9f4010bba6 166 return true;
shaunkrnelson 0:cc9f4010bba6 167 }
shaunkrnelson 0:cc9f4010bba6 168
shaunkrnelson 0:cc9f4010bba6 169 bool SelfIdPacket::setSwVersion(uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developerId)
shaunkrnelson 0:cc9f4010bba6 170 {
shaunkrnelson 0:cc9f4010bba6 171 uint8_t _major = major & 0xf;
shaunkrnelson 0:cc9f4010bba6 172 uint8_t _minor = minor & 0xf;
shaunkrnelson 0:cc9f4010bba6 173 uint8_t _point = point & 0x3f;
shaunkrnelson 0:cc9f4010bba6 174 uint16_t _build = build & 0x3ff;
shaunkrnelson 0:cc9f4010bba6 175 uint8_t _devid = developerId & 0xff;
shaunkrnelson 0:cc9f4010bba6 176
shaunkrnelson 0:cc9f4010bba6 177 if((_major != major) || (_minor != minor) || (_point != point) || (_build != build) || (_devid != developerId))
shaunkrnelson 0:cc9f4010bba6 178 return false;
shaunkrnelson 0:cc9f4010bba6 179
shaunkrnelson 0:cc9f4010bba6 180 swVersion = (_major << 28) | (_minor << 24) | (_point << 18) | (_build << 8) | _devid;
shaunkrnelson 0:cc9f4010bba6 181 return true;
shaunkrnelson 0:cc9f4010bba6 182 }
shaunkrnelson 0:cc9f4010bba6 183
shaunkrnelson 0:cc9f4010bba6 184 void SelfIdPacket::setBatteryFailState(bool failed)
shaunkrnelson 0:cc9f4010bba6 185 {
shaunkrnelson 0:cc9f4010bba6 186 if(failed == true)
shaunkrnelson 0:cc9f4010bba6 187 powerMask |= 1 << 3;
shaunkrnelson 0:cc9f4010bba6 188 else
shaunkrnelson 0:cc9f4010bba6 189 powerMask &= ~(1 << 3);
shaunkrnelson 0:cc9f4010bba6 190 }
shaunkrnelson 0:cc9f4010bba6 191
shaunkrnelson 0:cc9f4010bba6 192 bool SelfIdPacket::setBatteryLevel(uint8_t level)
shaunkrnelson 0:cc9f4010bba6 193 {
shaunkrnelson 0:cc9f4010bba6 194 uint8_t _level = level & 0x7;
shaunkrnelson 0:cc9f4010bba6 195
shaunkrnelson 0:cc9f4010bba6 196 if(level != _level)
shaunkrnelson 0:cc9f4010bba6 197 return false;
shaunkrnelson 0:cc9f4010bba6 198
shaunkrnelson 0:cc9f4010bba6 199 powerMask &= 0xf8;
shaunkrnelson 0:cc9f4010bba6 200 powerMask |= _level;
shaunkrnelson 0:cc9f4010bba6 201
shaunkrnelson 0:cc9f4010bba6 202 return true;
shaunkrnelson 0:cc9f4010bba6 203 }
shaunkrnelson 0:cc9f4010bba6 204
shaunkrnelson 0:cc9f4010bba6 205 bool SelfIdPacket::setExtPowerSupplyState(uint8_t id, bool isPresent)
shaunkrnelson 0:cc9f4010bba6 206 {
shaunkrnelson 0:cc9f4010bba6 207 bool retVal = false;
shaunkrnelson 0:cc9f4010bba6 208 if(id == EXT_POWER_SUPPLY_1)
shaunkrnelson 0:cc9f4010bba6 209 {
shaunkrnelson 0:cc9f4010bba6 210 powerMask &= 0x7F;
shaunkrnelson 0:cc9f4010bba6 211 if(isPresent)
shaunkrnelson 0:cc9f4010bba6 212 powerMask |= 0x80;
shaunkrnelson 0:cc9f4010bba6 213 retVal = true;
shaunkrnelson 0:cc9f4010bba6 214 }
shaunkrnelson 0:cc9f4010bba6 215 else if(id == EXT_POWER_SUPPLY_2)
shaunkrnelson 0:cc9f4010bba6 216 {
shaunkrnelson 0:cc9f4010bba6 217 powerMask &= 0xBF;
shaunkrnelson 0:cc9f4010bba6 218 if(isPresent)
shaunkrnelson 0:cc9f4010bba6 219 powerMask |= 0x40;
shaunkrnelson 0:cc9f4010bba6 220 retVal = true;
shaunkrnelson 0:cc9f4010bba6 221 }
shaunkrnelson 0:cc9f4010bba6 222 return retVal;
shaunkrnelson 0:cc9f4010bba6 223 }
shaunkrnelson 0:cc9f4010bba6 224
shaunkrnelson 0:cc9f4010bba6 225 int32_t SelfIdPacket::serializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 226 {
shaunkrnelson 0:cc9f4010bba6 227 int32_t out = -1;
shaunkrnelson 0:cc9f4010bba6 228
shaunkrnelson 0:cc9f4010bba6 229 if(SELFID_PAYLOAD_LEN <= len)
shaunkrnelson 0:cc9f4010bba6 230 {
shaunkrnelson 2:9c971be7692b 231 frame[0] = (deviceType>>24) & 0xff;
shaunkrnelson 2:9c971be7692b 232 frame[1] = (deviceType>>16) & 0xff;
shaunkrnelson 2:9c971be7692b 233 frame[2] = (deviceType>>8) & 0xff;
shaunkrnelson 2:9c971be7692b 234 frame[3] = deviceType & 0xff;
shaunkrnelson 0:cc9f4010bba6 235
shaunkrnelson 2:9c971be7692b 236 frame[4] = (swVersion >> 24) & 0xff;
shaunkrnelson 2:9c971be7692b 237 frame[5] = (swVersion >> 16) & 0xff;
shaunkrnelson 2:9c971be7692b 238 frame[6] = (swVersion >> 8) & 0xff;
shaunkrnelson 2:9c971be7692b 239 frame[7] = swVersion & 0xff;
shaunkrnelson 0:cc9f4010bba6 240
shaunkrnelson 2:9c971be7692b 241 frame[8] = powerMask;
shaunkrnelson 0:cc9f4010bba6 242
shaunkrnelson 0:cc9f4010bba6 243 out = SELFID_PAYLOAD_LEN;
shaunkrnelson 0:cc9f4010bba6 244 }
shaunkrnelson 0:cc9f4010bba6 245
shaunkrnelson 0:cc9f4010bba6 246 return out;
shaunkrnelson 0:cc9f4010bba6 247 }
shaunkrnelson 0:cc9f4010bba6 248
shaunkrnelson 0:cc9f4010bba6 249 int32_t ConfigWordPacket::serializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 250 {
shaunkrnelson 0:cc9f4010bba6 251 int32_t out = -1;
shaunkrnelson 0:cc9f4010bba6 252
shaunkrnelson 0:cc9f4010bba6 253 if(CONTROL_PAYLOAD_LENGTH <= len)
shaunkrnelson 0:cc9f4010bba6 254 {
shaunkrnelson 0:cc9f4010bba6 255 frame[0] = (config>>24) & 0xff;
shaunkrnelson 0:cc9f4010bba6 256 frame[1] = (config>>16) & 0xff;
shaunkrnelson 0:cc9f4010bba6 257 frame[2] = (config>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 258 frame[3] = config & 0xff;
shaunkrnelson 0:cc9f4010bba6 259
shaunkrnelson 0:cc9f4010bba6 260 frame[4] = (mask>>24) & 0xff;
shaunkrnelson 0:cc9f4010bba6 261 frame[5] = (mask>>16) & 0xff;
shaunkrnelson 0:cc9f4010bba6 262 frame[6] = (mask>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 263 frame[7] = mask & 0xff;
shaunkrnelson 0:cc9f4010bba6 264
shaunkrnelson 0:cc9f4010bba6 265 frame[8] = authKey;
shaunkrnelson 0:cc9f4010bba6 266 out = CONTROL_PAYLOAD_LENGTH;
shaunkrnelson 0:cc9f4010bba6 267 }
shaunkrnelson 0:cc9f4010bba6 268
shaunkrnelson 0:cc9f4010bba6 269 return out;
shaunkrnelson 0:cc9f4010bba6 270
shaunkrnelson 0:cc9f4010bba6 271 }
shaunkrnelson 0:cc9f4010bba6 272
shaunkrnelson 1:3e0cc8ad24f1 273 int32_t ConfigWordPacket::deserializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 1:3e0cc8ad24f1 274 {
shaunkrnelson 1:3e0cc8ad24f1 275 if(CONTROL_PAYLOAD_LENGTH <= len)
shaunkrnelson 1:3e0cc8ad24f1 276 {
shaunkrnelson 1:3e0cc8ad24f1 277 int32_t offset = 0;
shaunkrnelson 1:3e0cc8ad24f1 278
shaunkrnelson 1:3e0cc8ad24f1 279 config = frame[offset++]<<24;
shaunkrnelson 1:3e0cc8ad24f1 280 config |= frame[offset++]<<16;
shaunkrnelson 1:3e0cc8ad24f1 281 config |= frame[offset++]<<8;
shaunkrnelson 1:3e0cc8ad24f1 282 config |= frame[offset++];
shaunkrnelson 1:3e0cc8ad24f1 283
shaunkrnelson 1:3e0cc8ad24f1 284 mask = frame[offset++]<<24;
shaunkrnelson 1:3e0cc8ad24f1 285 mask |= frame[offset++]<<16;
shaunkrnelson 1:3e0cc8ad24f1 286 mask |= frame[offset++]<<8;
shaunkrnelson 1:3e0cc8ad24f1 287 mask |= frame[offset++];
shaunkrnelson 1:3e0cc8ad24f1 288
shaunkrnelson 1:3e0cc8ad24f1 289 authKey = frame[offset++];
shaunkrnelson 1:3e0cc8ad24f1 290
shaunkrnelson 1:3e0cc8ad24f1 291 return offset;
shaunkrnelson 1:3e0cc8ad24f1 292 }
shaunkrnelson 1:3e0cc8ad24f1 293 return -1;
shaunkrnelson 1:3e0cc8ad24f1 294 }
shaunkrnelson 1:3e0cc8ad24f1 295
shaunkrnelson 1:3e0cc8ad24f1 296
shaunkrnelson 0:cc9f4010bba6 297 int32_t BootInfoPacket::serializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 298 {
shaunkrnelson 0:cc9f4010bba6 299 int32_t out = -1;
shaunkrnelson 0:cc9f4010bba6 300
shaunkrnelson 0:cc9f4010bba6 301 if(BOOT_PAYLOAD_LENGTH <= len)
shaunkrnelson 0:cc9f4010bba6 302 {
shaunkrnelson 2:9c971be7692b 303 frame[0] = (bootCount>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 304 frame[1] = bootCount & 0xff;
shaunkrnelson 0:cc9f4010bba6 305
shaunkrnelson 2:9c971be7692b 306 frame[2] = (resetCount>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 307 frame[3] = resetCount & 0xff;
shaunkrnelson 0:cc9f4010bba6 308
shaunkrnelson 2:9c971be7692b 309 frame[4] = (lastBootReason>>24) & 0xff;
shaunkrnelson 2:9c971be7692b 310 frame[5] = (lastBootReason>>16) & 0xff;
shaunkrnelson 2:9c971be7692b 311 frame[6] = (lastBootReason>>8) & 0xff;
shaunkrnelson 2:9c971be7692b 312 frame[7] = lastBootReason & 0xff;
shaunkrnelson 2:9c971be7692b 313 frame[8] = authKey;
shaunkrnelson 0:cc9f4010bba6 314
shaunkrnelson 0:cc9f4010bba6 315 out = BOOT_PAYLOAD_LENGTH;
shaunkrnelson 0:cc9f4010bba6 316 }
shaunkrnelson 0:cc9f4010bba6 317
shaunkrnelson 0:cc9f4010bba6 318 return out;
shaunkrnelson 0:cc9f4010bba6 319 }
shaunkrnelson 0:cc9f4010bba6 320
shaunkrnelson 2:9c971be7692b 321
shaunkrnelson 2:9c971be7692b 322 int32_t BootInfoPacket::deserializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 323 {
shaunkrnelson 2:9c971be7692b 324 if(BOOT_PAYLOAD_LENGTH <= len)
shaunkrnelson 2:9c971be7692b 325 {
shaunkrnelson 2:9c971be7692b 326 int32_t offset = 0;
shaunkrnelson 2:9c971be7692b 327
shaunkrnelson 2:9c971be7692b 328 bootCount = frame[offset++]<<8;
shaunkrnelson 2:9c971be7692b 329 bootCount |= frame[offset++];
shaunkrnelson 2:9c971be7692b 330
shaunkrnelson 2:9c971be7692b 331 resetCount = frame[offset++]<<8;
shaunkrnelson 2:9c971be7692b 332 resetCount |= frame[offset++];
shaunkrnelson 0:cc9f4010bba6 333
shaunkrnelson 2:9c971be7692b 334 lastBootReason = frame[offset++] << 24;
shaunkrnelson 2:9c971be7692b 335 lastBootReason |= frame[offset++] << 16;
shaunkrnelson 2:9c971be7692b 336 lastBootReason |= frame[offset++] << 8;
shaunkrnelson 2:9c971be7692b 337 lastBootReason |= frame[offset++];
shaunkrnelson 2:9c971be7692b 338
shaunkrnelson 2:9c971be7692b 339 authKey = frame[offset++];
shaunkrnelson 0:cc9f4010bba6 340
shaunkrnelson 2:9c971be7692b 341 return offset;
shaunkrnelson 2:9c971be7692b 342 }
shaunkrnelson 2:9c971be7692b 343 return -1;
shaunkrnelson 2:9c971be7692b 344 }
shaunkrnelson 2:9c971be7692b 345
shaunkrnelson 1:3e0cc8ad24f1 346 bool GpsPacket::setCoordinates(int32_t _latitude, int32_t _longitude, uint16_t _elevation)
shaunkrnelson 0:cc9f4010bba6 347 {
shaunkrnelson 0:cc9f4010bba6 348 latitude = _latitude;
shaunkrnelson 0:cc9f4010bba6 349 longitude = _longitude;
shaunkrnelson 0:cc9f4010bba6 350 elevation = _elevation;
shaunkrnelson 0:cc9f4010bba6 351
shaunkrnelson 0:cc9f4010bba6 352 return true;
shaunkrnelson 0:cc9f4010bba6 353 }
shaunkrnelson 0:cc9f4010bba6 354
shaunkrnelson 0:cc9f4010bba6 355 int32_t GpsPacket::serializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 356 {
shaunkrnelson 0:cc9f4010bba6 357 int32_t out = -1;
shaunkrnelson 0:cc9f4010bba6 358
shaunkrnelson 0:cc9f4010bba6 359 if(GPS_PAYLOAD_LENGTH <= len)
shaunkrnelson 0:cc9f4010bba6 360 {
shaunkrnelson 0:cc9f4010bba6 361 frame[0] = (latitude>>16) & 0xff;
shaunkrnelson 0:cc9f4010bba6 362 frame[1] = (latitude>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 363 frame[2] = latitude & 0xff;
shaunkrnelson 0:cc9f4010bba6 364
shaunkrnelson 0:cc9f4010bba6 365 frame[3] = (longitude>>16) & 0xff;
shaunkrnelson 0:cc9f4010bba6 366 frame[4] = (longitude>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 367 frame[5] = longitude & 0xff;
shaunkrnelson 0:cc9f4010bba6 368
shaunkrnelson 0:cc9f4010bba6 369 frame[6] = (elevation>>8) & 0xff;
shaunkrnelson 0:cc9f4010bba6 370 frame[7] = elevation & 0xff;
shaunkrnelson 0:cc9f4010bba6 371
shaunkrnelson 0:cc9f4010bba6 372 frame[8] = txPower;
shaunkrnelson 0:cc9f4010bba6 373
shaunkrnelson 0:cc9f4010bba6 374 out = GPS_PAYLOAD_LENGTH;
shaunkrnelson 0:cc9f4010bba6 375 }
shaunkrnelson 0:cc9f4010bba6 376
shaunkrnelson 0:cc9f4010bba6 377 return out;
shaunkrnelson 0:cc9f4010bba6 378 }
shaunkrnelson 0:cc9f4010bba6 379
shaunkrnelson 0:cc9f4010bba6 380 int32_t RFDataPacket::serializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 381 {
shaunkrnelson 0:cc9f4010bba6 382 int32_t out = -1;
shaunkrnelson 0:cc9f4010bba6 383
shaunkrnelson 0:cc9f4010bba6 384 if(RFDATA_PAYLOAD_LEN <= len)
shaunkrnelson 0:cc9f4010bba6 385 {
shaunkrnelson 0:cc9f4010bba6 386 frame[0] = channel;
shaunkrnelson 0:cc9f4010bba6 387 frame[1] = txpower;
shaunkrnelson 0:cc9f4010bba6 388 frame[2] = datarate;
shaunkrnelson 0:cc9f4010bba6 389 frame[3] = snr;
shaunkrnelson 2:9c971be7692b 390 frame[4] = (rssi >>8) & 0xff;
shaunkrnelson 2:9c971be7692b 391 frame[5] = rssi & 0xff;
shaunkrnelson 2:9c971be7692b 392 frame[6] = (timestamp >> 16) & 0xff;
shaunkrnelson 2:9c971be7692b 393 frame[7] = (timestamp >> 8) & 0xff;
shaunkrnelson 2:9c971be7692b 394 frame[8] = timestamp & 0xff;
shaunkrnelson 0:cc9f4010bba6 395 out = RFDATA_PAYLOAD_LEN;
shaunkrnelson 0:cc9f4010bba6 396 }
shaunkrnelson 0:cc9f4010bba6 397 return out;
shaunkrnelson 0:cc9f4010bba6 398 }
shaunkrnelson 0:cc9f4010bba6 399
shaunkrnelson 0:cc9f4010bba6 400
shaunkrnelson 0:cc9f4010bba6 401 OctetStringPacket::OctetStringPacket(uint8_t size) :
shaunkrnelson 0:cc9f4010bba6 402 SenetPacket(OCTET_STRING_PACKET, NULL, size + SenetPacket::PacketHeader::HEADER_SIZE)
shaunkrnelson 0:cc9f4010bba6 403 {
shaunkrnelson 0:cc9f4010bba6 404 max = size;
shaunkrnelson 0:cc9f4010bba6 405 oslen = 0;
shaunkrnelson 0:cc9f4010bba6 406 }
shaunkrnelson 0:cc9f4010bba6 407
shaunkrnelson 0:cc9f4010bba6 408 bool OctetStringPacket::setOctetString(uint8_t *os, uint8_t len)
shaunkrnelson 0:cc9f4010bba6 409 {
shaunkrnelson 0:cc9f4010bba6 410 if(len > max)
shaunkrnelson 0:cc9f4010bba6 411 return false;
shaunkrnelson 0:cc9f4010bba6 412
shaunkrnelson 0:cc9f4010bba6 413 oslen = len;
shaunkrnelson 0:cc9f4010bba6 414 memcpy(buffer+PacketHeader::HEADER_SIZE, os, oslen);
shaunkrnelson 0:cc9f4010bba6 415 return true;
shaunkrnelson 0:cc9f4010bba6 416 }
shaunkrnelson 0:cc9f4010bba6 417
shaunkrnelson 0:cc9f4010bba6 418 int32_t OctetStringPacket::serializePayload(uint8_t *frame, int32_t len)
shaunkrnelson 0:cc9f4010bba6 419 {
shaunkrnelson 0:cc9f4010bba6 420 int32_t out = -1;
shaunkrnelson 0:cc9f4010bba6 421
shaunkrnelson 0:cc9f4010bba6 422 if(oslen >= len)
shaunkrnelson 0:cc9f4010bba6 423 {
shaunkrnelson 0:cc9f4010bba6 424 memcpy(frame, buffer + PacketHeader::HEADER_SIZE, oslen);
shaunkrnelson 0:cc9f4010bba6 425 out = oslen;
shaunkrnelson 0:cc9f4010bba6 426 }
shaunkrnelson 0:cc9f4010bba6 427
shaunkrnelson 0:cc9f4010bba6 428 return out;
shaunkrnelson 0:cc9f4010bba6 429 }
shaunkrnelson 0:cc9f4010bba6 430