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