Senet Packet API
Dependents: MTDOT-UDKDemo_Senet Senet NAMote mDot-IKS01A1 unh-hackathon-example ... more
senet_packet.cpp@0:cc9f4010bba6, 2016-03-08 (annotated)
- Committer:
- shaunkrnelson
- Date:
- Tue Mar 08 09:08:44 2016 -0500
- Revision:
- 0:cc9f4010bba6
- Child:
- 1:3e0cc8ad24f1
- Child:
- 2:9c971be7692b
Initial commit
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 |
0:cc9f4010bba6 | 26 | int32_t serializedLen = 0; |
shaunkrnelson |
0:cc9f4010bba6 | 27 | |
shaunkrnelson |
0:cc9f4010bba6 | 28 | if(len >= PacketHeader::HEADER_SIZE) |
shaunkrnelson |
0:cc9f4010bba6 | 29 | { |
shaunkrnelson |
0:cc9f4010bba6 | 30 | frame[0] = version; |
shaunkrnelson |
0:cc9f4010bba6 | 31 | frame[1] = type; |
shaunkrnelson |
0:cc9f4010bba6 | 32 | serializedLen = PacketHeader::HEADER_SIZE; |
shaunkrnelson |
0:cc9f4010bba6 | 33 | } |
shaunkrnelson |
0:cc9f4010bba6 | 34 | |
shaunkrnelson |
0:cc9f4010bba6 | 35 | return serializedLen; |
shaunkrnelson |
0:cc9f4010bba6 | 36 | } |
shaunkrnelson |
0:cc9f4010bba6 | 37 | |
shaunkrnelson |
0:cc9f4010bba6 | 38 | bool SenetPacket:: |
shaunkrnelson |
0:cc9f4010bba6 | 39 | PacketHeader::deserialize(uint8_t *frame, int32_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 40 | { |
shaunkrnelson |
0:cc9f4010bba6 | 41 | if((frame != NULL) && (len >= PacketHeader::HEADER_SIZE)) |
shaunkrnelson |
0:cc9f4010bba6 | 42 | { |
shaunkrnelson |
0:cc9f4010bba6 | 43 | version = frame[0]; |
shaunkrnelson |
0:cc9f4010bba6 | 44 | type = frame[1]; |
shaunkrnelson |
0:cc9f4010bba6 | 45 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 46 | } |
shaunkrnelson |
0:cc9f4010bba6 | 47 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 48 | } |
shaunkrnelson |
0:cc9f4010bba6 | 49 | |
shaunkrnelson |
0:cc9f4010bba6 | 50 | |
shaunkrnelson |
0:cc9f4010bba6 | 51 | SenetPacket::SenetPacket(uint8_t senetPktType, uint8_t *_buffer, uint8_t _buflen) |
shaunkrnelson |
0:cc9f4010bba6 | 52 | { |
shaunkrnelson |
0:cc9f4010bba6 | 53 | header.type = senetPktType; |
shaunkrnelson |
0:cc9f4010bba6 | 54 | header.version = VERSION; |
shaunkrnelson |
0:cc9f4010bba6 | 55 | pktLen = 0; |
shaunkrnelson |
0:cc9f4010bba6 | 56 | |
shaunkrnelson |
0:cc9f4010bba6 | 57 | if(_buffer != NULL) |
shaunkrnelson |
0:cc9f4010bba6 | 58 | { |
shaunkrnelson |
0:cc9f4010bba6 | 59 | buffer = _buffer; |
shaunkrnelson |
0:cc9f4010bba6 | 60 | bufferLen = _buflen; |
shaunkrnelson |
0:cc9f4010bba6 | 61 | ownBuffer = false; |
shaunkrnelson |
0:cc9f4010bba6 | 62 | } |
shaunkrnelson |
0:cc9f4010bba6 | 63 | else |
shaunkrnelson |
0:cc9f4010bba6 | 64 | { |
shaunkrnelson |
0:cc9f4010bba6 | 65 | if(_buflen != 0) |
shaunkrnelson |
0:cc9f4010bba6 | 66 | bufferLen = _buflen; |
shaunkrnelson |
0:cc9f4010bba6 | 67 | else |
shaunkrnelson |
0:cc9f4010bba6 | 68 | bufferLen = MAX_FRAME_SIZE; |
shaunkrnelson |
0:cc9f4010bba6 | 69 | |
shaunkrnelson |
0:cc9f4010bba6 | 70 | buffer = new uint8_t[bufferLen]; |
shaunkrnelson |
0:cc9f4010bba6 | 71 | ASSERT(buffer != NULL); |
shaunkrnelson |
0:cc9f4010bba6 | 72 | ownBuffer = true; |
shaunkrnelson |
0:cc9f4010bba6 | 73 | } |
shaunkrnelson |
0:cc9f4010bba6 | 74 | memset(buffer, 0, bufferLen); |
shaunkrnelson |
0:cc9f4010bba6 | 75 | } |
shaunkrnelson |
0:cc9f4010bba6 | 76 | |
shaunkrnelson |
0:cc9f4010bba6 | 77 | |
shaunkrnelson |
0:cc9f4010bba6 | 78 | SenetPacket::~SenetPacket() |
shaunkrnelson |
0:cc9f4010bba6 | 79 | { |
shaunkrnelson |
0:cc9f4010bba6 | 80 | if(ownBuffer == true) |
shaunkrnelson |
0:cc9f4010bba6 | 81 | delete buffer; |
shaunkrnelson |
0:cc9f4010bba6 | 82 | } |
shaunkrnelson |
0:cc9f4010bba6 | 83 | |
shaunkrnelson |
0:cc9f4010bba6 | 84 | |
shaunkrnelson |
0:cc9f4010bba6 | 85 | int32_t SenetPacket::serialize() |
shaunkrnelson |
0:cc9f4010bba6 | 86 | { |
shaunkrnelson |
0:cc9f4010bba6 | 87 | pktLen = header.serialize(buffer, bufferLen); |
shaunkrnelson |
0:cc9f4010bba6 | 88 | ASSERT(pktLen > 0); |
shaunkrnelson |
0:cc9f4010bba6 | 89 | |
shaunkrnelson |
0:cc9f4010bba6 | 90 | if(pktLen > 0) |
shaunkrnelson |
0:cc9f4010bba6 | 91 | { |
shaunkrnelson |
0:cc9f4010bba6 | 92 | int32_t payloadLen = serializePayload(buffer + pktLen, bufferLen - pktLen); |
shaunkrnelson |
0:cc9f4010bba6 | 93 | |
shaunkrnelson |
0:cc9f4010bba6 | 94 | ASSERT(payloadLen > 0); |
shaunkrnelson |
0:cc9f4010bba6 | 95 | |
shaunkrnelson |
0:cc9f4010bba6 | 96 | if(payloadLen > 0) |
shaunkrnelson |
0:cc9f4010bba6 | 97 | { |
shaunkrnelson |
0:cc9f4010bba6 | 98 | pktLen += payloadLen; |
shaunkrnelson |
0:cc9f4010bba6 | 99 | return pktLen; |
shaunkrnelson |
0:cc9f4010bba6 | 100 | } |
shaunkrnelson |
0:cc9f4010bba6 | 101 | } |
shaunkrnelson |
0:cc9f4010bba6 | 102 | |
shaunkrnelson |
0:cc9f4010bba6 | 103 | return -1; |
shaunkrnelson |
0:cc9f4010bba6 | 104 | } |
shaunkrnelson |
0:cc9f4010bba6 | 105 | |
shaunkrnelson |
0:cc9f4010bba6 | 106 | |
shaunkrnelson |
0:cc9f4010bba6 | 107 | bool SensorPacket::addSensorValue(uint8_t position, uint8_t type, uint16_t value) |
shaunkrnelson |
0:cc9f4010bba6 | 108 | { |
shaunkrnelson |
0:cc9f4010bba6 | 109 | if (position < MAX_SENSOR_VALUES) |
shaunkrnelson |
0:cc9f4010bba6 | 110 | { |
shaunkrnelson |
0:cc9f4010bba6 | 111 | sensorValue[position].type = type; |
shaunkrnelson |
0:cc9f4010bba6 | 112 | sensorValue[position].value = value; |
shaunkrnelson |
0:cc9f4010bba6 | 113 | sensorValue[position].isSet = true; |
shaunkrnelson |
0:cc9f4010bba6 | 114 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 115 | } |
shaunkrnelson |
0:cc9f4010bba6 | 116 | else |
shaunkrnelson |
0:cc9f4010bba6 | 117 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 118 | } |
shaunkrnelson |
0:cc9f4010bba6 | 119 | |
shaunkrnelson |
0:cc9f4010bba6 | 120 | int32_t SensorPacket::serializePayload(uint8_t *buffer, int32_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 121 | { |
shaunkrnelson |
0:cc9f4010bba6 | 122 | int32_t bytes = 0; |
shaunkrnelson |
0:cc9f4010bba6 | 123 | int32_t dataLen = 0; |
shaunkrnelson |
0:cc9f4010bba6 | 124 | |
shaunkrnelson |
0:cc9f4010bba6 | 125 | for(int32_t i = 0; i < MAX_SENSOR_VALUES; i++) |
shaunkrnelson |
0:cc9f4010bba6 | 126 | { |
shaunkrnelson |
0:cc9f4010bba6 | 127 | if(sensorValue[i].isSet == true) |
shaunkrnelson |
0:cc9f4010bba6 | 128 | { |
shaunkrnelson |
0:cc9f4010bba6 | 129 | dataLen = sensorValue[i].serialize(buffer+bytes, len - bytes); |
shaunkrnelson |
0:cc9f4010bba6 | 130 | if(dataLen == -1) |
shaunkrnelson |
0:cc9f4010bba6 | 131 | return -1; |
shaunkrnelson |
0:cc9f4010bba6 | 132 | bytes += dataLen; |
shaunkrnelson |
0:cc9f4010bba6 | 133 | } |
shaunkrnelson |
0:cc9f4010bba6 | 134 | } |
shaunkrnelson |
0:cc9f4010bba6 | 135 | return bytes; |
shaunkrnelson |
0:cc9f4010bba6 | 136 | } |
shaunkrnelson |
0:cc9f4010bba6 | 137 | |
shaunkrnelson |
0:cc9f4010bba6 | 138 | bool SelfIdPacket::setDeviceType(uint32_t model, uint8_t revision) |
shaunkrnelson |
0:cc9f4010bba6 | 139 | { |
shaunkrnelson |
0:cc9f4010bba6 | 140 | if((model & 0x00FFFFFF) != model) |
shaunkrnelson |
0:cc9f4010bba6 | 141 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 142 | |
shaunkrnelson |
0:cc9f4010bba6 | 143 | deviceType = (model<<8)|revision; |
shaunkrnelson |
0:cc9f4010bba6 | 144 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 145 | } |
shaunkrnelson |
0:cc9f4010bba6 | 146 | |
shaunkrnelson |
0:cc9f4010bba6 | 147 | bool SelfIdPacket::setSwVersion(uint8_t major, uint8_t minor, uint8_t point, uint16_t build, uint8_t developerId) |
shaunkrnelson |
0:cc9f4010bba6 | 148 | { |
shaunkrnelson |
0:cc9f4010bba6 | 149 | uint8_t _major = major & 0xf; |
shaunkrnelson |
0:cc9f4010bba6 | 150 | uint8_t _minor = minor & 0xf; |
shaunkrnelson |
0:cc9f4010bba6 | 151 | uint8_t _point = point & 0x3f; |
shaunkrnelson |
0:cc9f4010bba6 | 152 | uint16_t _build = build & 0x3ff; |
shaunkrnelson |
0:cc9f4010bba6 | 153 | uint8_t _devid = developerId & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 154 | |
shaunkrnelson |
0:cc9f4010bba6 | 155 | if((_major != major) || (_minor != minor) || (_point != point) || (_build != build) || (_devid != developerId)) |
shaunkrnelson |
0:cc9f4010bba6 | 156 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 157 | |
shaunkrnelson |
0:cc9f4010bba6 | 158 | swVersion = (_major << 28) | (_minor << 24) | (_point << 18) | (_build << 8) | _devid; |
shaunkrnelson |
0:cc9f4010bba6 | 159 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 160 | } |
shaunkrnelson |
0:cc9f4010bba6 | 161 | |
shaunkrnelson |
0:cc9f4010bba6 | 162 | void SelfIdPacket::setBatteryFailState(bool failed) |
shaunkrnelson |
0:cc9f4010bba6 | 163 | { |
shaunkrnelson |
0:cc9f4010bba6 | 164 | if(failed == true) |
shaunkrnelson |
0:cc9f4010bba6 | 165 | powerMask |= 1 << 3; |
shaunkrnelson |
0:cc9f4010bba6 | 166 | else |
shaunkrnelson |
0:cc9f4010bba6 | 167 | powerMask &= ~(1 << 3); |
shaunkrnelson |
0:cc9f4010bba6 | 168 | } |
shaunkrnelson |
0:cc9f4010bba6 | 169 | |
shaunkrnelson |
0:cc9f4010bba6 | 170 | bool SelfIdPacket::setBatteryLevel(uint8_t level) |
shaunkrnelson |
0:cc9f4010bba6 | 171 | { |
shaunkrnelson |
0:cc9f4010bba6 | 172 | uint8_t _level = level & 0x7; |
shaunkrnelson |
0:cc9f4010bba6 | 173 | |
shaunkrnelson |
0:cc9f4010bba6 | 174 | if(level != _level) |
shaunkrnelson |
0:cc9f4010bba6 | 175 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 176 | |
shaunkrnelson |
0:cc9f4010bba6 | 177 | powerMask &= 0xf8; |
shaunkrnelson |
0:cc9f4010bba6 | 178 | powerMask |= _level; |
shaunkrnelson |
0:cc9f4010bba6 | 179 | |
shaunkrnelson |
0:cc9f4010bba6 | 180 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 181 | } |
shaunkrnelson |
0:cc9f4010bba6 | 182 | |
shaunkrnelson |
0:cc9f4010bba6 | 183 | bool SelfIdPacket::setExtPowerSupplyState(uint8_t id, bool isPresent) |
shaunkrnelson |
0:cc9f4010bba6 | 184 | { |
shaunkrnelson |
0:cc9f4010bba6 | 185 | bool retVal = false; |
shaunkrnelson |
0:cc9f4010bba6 | 186 | if(id == EXT_POWER_SUPPLY_1) |
shaunkrnelson |
0:cc9f4010bba6 | 187 | { |
shaunkrnelson |
0:cc9f4010bba6 | 188 | powerMask &= 0x7F; |
shaunkrnelson |
0:cc9f4010bba6 | 189 | if(isPresent) |
shaunkrnelson |
0:cc9f4010bba6 | 190 | powerMask |= 0x80; |
shaunkrnelson |
0:cc9f4010bba6 | 191 | retVal = true; |
shaunkrnelson |
0:cc9f4010bba6 | 192 | } |
shaunkrnelson |
0:cc9f4010bba6 | 193 | else if(id == EXT_POWER_SUPPLY_2) |
shaunkrnelson |
0:cc9f4010bba6 | 194 | { |
shaunkrnelson |
0:cc9f4010bba6 | 195 | powerMask &= 0xBF; |
shaunkrnelson |
0:cc9f4010bba6 | 196 | if(isPresent) |
shaunkrnelson |
0:cc9f4010bba6 | 197 | powerMask |= 0x40; |
shaunkrnelson |
0:cc9f4010bba6 | 198 | retVal = true; |
shaunkrnelson |
0:cc9f4010bba6 | 199 | } |
shaunkrnelson |
0:cc9f4010bba6 | 200 | return retVal; |
shaunkrnelson |
0:cc9f4010bba6 | 201 | } |
shaunkrnelson |
0:cc9f4010bba6 | 202 | |
shaunkrnelson |
0:cc9f4010bba6 | 203 | int32_t SelfIdPacket::serializePayload(uint8_t *frame, int32_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 204 | { |
shaunkrnelson |
0:cc9f4010bba6 | 205 | int32_t out = -1; |
shaunkrnelson |
0:cc9f4010bba6 | 206 | |
shaunkrnelson |
0:cc9f4010bba6 | 207 | if(SELFID_PAYLOAD_LEN <= len) |
shaunkrnelson |
0:cc9f4010bba6 | 208 | { |
shaunkrnelson |
0:cc9f4010bba6 | 209 | frame[0] = (deviceType>>16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 210 | frame[1] = (deviceType>>8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 211 | frame[2] = deviceType & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 212 | |
shaunkrnelson |
0:cc9f4010bba6 | 213 | frame[3] = (swVersion >> 24) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 214 | frame[4] = (swVersion >> 16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 215 | frame[5] = (swVersion >> 8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 216 | frame[6] = swVersion & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 217 | |
shaunkrnelson |
0:cc9f4010bba6 | 218 | frame[7] = powerMask; |
shaunkrnelson |
0:cc9f4010bba6 | 219 | |
shaunkrnelson |
0:cc9f4010bba6 | 220 | out = SELFID_PAYLOAD_LEN; |
shaunkrnelson |
0:cc9f4010bba6 | 221 | } |
shaunkrnelson |
0:cc9f4010bba6 | 222 | |
shaunkrnelson |
0:cc9f4010bba6 | 223 | return out; |
shaunkrnelson |
0:cc9f4010bba6 | 224 | } |
shaunkrnelson |
0:cc9f4010bba6 | 225 | |
shaunkrnelson |
0:cc9f4010bba6 | 226 | int32_t ConfigWordPacket::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(CONTROL_PAYLOAD_LENGTH <= len) |
shaunkrnelson |
0:cc9f4010bba6 | 231 | { |
shaunkrnelson |
0:cc9f4010bba6 | 232 | frame[0] = (config>>24) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 233 | frame[1] = (config>>16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 234 | frame[2] = (config>>8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 235 | frame[3] = config & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 236 | |
shaunkrnelson |
0:cc9f4010bba6 | 237 | frame[4] = (mask>>24) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 238 | frame[5] = (mask>>16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 239 | frame[6] = (mask>>8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 240 | frame[7] = mask & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 241 | |
shaunkrnelson |
0:cc9f4010bba6 | 242 | frame[8] = authKey; |
shaunkrnelson |
0:cc9f4010bba6 | 243 | out = CONTROL_PAYLOAD_LENGTH; |
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 | |
shaunkrnelson |
0:cc9f4010bba6 | 250 | int32_t BootInfoPacket::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(BOOT_PAYLOAD_LENGTH <= len) |
shaunkrnelson |
0:cc9f4010bba6 | 255 | { |
shaunkrnelson |
0:cc9f4010bba6 | 256 | frame[0] = (bootCount<<8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 257 | frame[1] = bootCount & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 258 | |
shaunkrnelson |
0:cc9f4010bba6 | 259 | frame[2] = (resetCount<<8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 260 | frame[3] = resetCount & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 261 | |
shaunkrnelson |
0:cc9f4010bba6 | 262 | frame[4] = (lastBootReason<<24) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 263 | frame[5] = (lastBootReason<<16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 264 | frame[6] = (lastBootReason<<8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 265 | |
shaunkrnelson |
0:cc9f4010bba6 | 266 | out = BOOT_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 | bool GpsPacket::setCoordinates(uint32_t _latitude, uint32_t _longitude, uint16_t _elevation) |
shaunkrnelson |
0:cc9f4010bba6 | 273 | { |
shaunkrnelson |
0:cc9f4010bba6 | 274 | |
shaunkrnelson |
0:cc9f4010bba6 | 275 | if(((_latitude & 0x00ffffff) != _latitude) || ((_longitude & 0x00ffffff) != _longitude)) |
shaunkrnelson |
0:cc9f4010bba6 | 276 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 277 | |
shaunkrnelson |
0:cc9f4010bba6 | 278 | latitude = _latitude; |
shaunkrnelson |
0:cc9f4010bba6 | 279 | longitude = _longitude; |
shaunkrnelson |
0:cc9f4010bba6 | 280 | elevation = _elevation; |
shaunkrnelson |
0:cc9f4010bba6 | 281 | |
shaunkrnelson |
0:cc9f4010bba6 | 282 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 283 | } |
shaunkrnelson |
0:cc9f4010bba6 | 284 | |
shaunkrnelson |
0:cc9f4010bba6 | 285 | int32_t GpsPacket::serializePayload(uint8_t *frame, int32_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 286 | { |
shaunkrnelson |
0:cc9f4010bba6 | 287 | int32_t out = -1; |
shaunkrnelson |
0:cc9f4010bba6 | 288 | |
shaunkrnelson |
0:cc9f4010bba6 | 289 | if(GPS_PAYLOAD_LENGTH <= len) |
shaunkrnelson |
0:cc9f4010bba6 | 290 | { |
shaunkrnelson |
0:cc9f4010bba6 | 291 | frame[0] = (latitude>>16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 292 | frame[1] = (latitude>>8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 293 | frame[2] = latitude & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 294 | |
shaunkrnelson |
0:cc9f4010bba6 | 295 | frame[3] = (longitude>>16) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 296 | frame[4] = (longitude>>8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 297 | frame[5] = longitude & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 298 | |
shaunkrnelson |
0:cc9f4010bba6 | 299 | frame[6] = (elevation>>8) & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 300 | frame[7] = elevation & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 301 | |
shaunkrnelson |
0:cc9f4010bba6 | 302 | frame[8] = txPower; |
shaunkrnelson |
0:cc9f4010bba6 | 303 | |
shaunkrnelson |
0:cc9f4010bba6 | 304 | out = GPS_PAYLOAD_LENGTH; |
shaunkrnelson |
0:cc9f4010bba6 | 305 | } |
shaunkrnelson |
0:cc9f4010bba6 | 306 | |
shaunkrnelson |
0:cc9f4010bba6 | 307 | return out; |
shaunkrnelson |
0:cc9f4010bba6 | 308 | } |
shaunkrnelson |
0:cc9f4010bba6 | 309 | |
shaunkrnelson |
0:cc9f4010bba6 | 310 | int32_t RFDataPacket::serializePayload(uint8_t *frame, int32_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 311 | { |
shaunkrnelson |
0:cc9f4010bba6 | 312 | int32_t out = -1; |
shaunkrnelson |
0:cc9f4010bba6 | 313 | |
shaunkrnelson |
0:cc9f4010bba6 | 314 | if(RFDATA_PAYLOAD_LEN <= len) |
shaunkrnelson |
0:cc9f4010bba6 | 315 | { |
shaunkrnelson |
0:cc9f4010bba6 | 316 | frame[0] = channel; |
shaunkrnelson |
0:cc9f4010bba6 | 317 | frame[1] = txpower; |
shaunkrnelson |
0:cc9f4010bba6 | 318 | frame[2] = datarate; |
shaunkrnelson |
0:cc9f4010bba6 | 319 | frame[3] = snr; |
shaunkrnelson |
0:cc9f4010bba6 | 320 | frame[4] = rssi; |
shaunkrnelson |
0:cc9f4010bba6 | 321 | frame[5] = (timestamp >> 16)& 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 322 | frame[6] = (timestamp >> 8)& 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 323 | frame[7] = timestamp & 0xff; |
shaunkrnelson |
0:cc9f4010bba6 | 324 | out = RFDATA_PAYLOAD_LEN; |
shaunkrnelson |
0:cc9f4010bba6 | 325 | } |
shaunkrnelson |
0:cc9f4010bba6 | 326 | return out; |
shaunkrnelson |
0:cc9f4010bba6 | 327 | } |
shaunkrnelson |
0:cc9f4010bba6 | 328 | |
shaunkrnelson |
0:cc9f4010bba6 | 329 | |
shaunkrnelson |
0:cc9f4010bba6 | 330 | OctetStringPacket::OctetStringPacket(uint8_t size) : |
shaunkrnelson |
0:cc9f4010bba6 | 331 | SenetPacket(OCTET_STRING_PACKET, NULL, size + SenetPacket::PacketHeader::HEADER_SIZE) |
shaunkrnelson |
0:cc9f4010bba6 | 332 | { |
shaunkrnelson |
0:cc9f4010bba6 | 333 | max = size; |
shaunkrnelson |
0:cc9f4010bba6 | 334 | oslen = 0; |
shaunkrnelson |
0:cc9f4010bba6 | 335 | } |
shaunkrnelson |
0:cc9f4010bba6 | 336 | |
shaunkrnelson |
0:cc9f4010bba6 | 337 | bool OctetStringPacket::setOctetString(uint8_t *os, uint8_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 338 | { |
shaunkrnelson |
0:cc9f4010bba6 | 339 | if(len > max) |
shaunkrnelson |
0:cc9f4010bba6 | 340 | return false; |
shaunkrnelson |
0:cc9f4010bba6 | 341 | |
shaunkrnelson |
0:cc9f4010bba6 | 342 | oslen = len; |
shaunkrnelson |
0:cc9f4010bba6 | 343 | memcpy(buffer+PacketHeader::HEADER_SIZE, os, oslen); |
shaunkrnelson |
0:cc9f4010bba6 | 344 | return true; |
shaunkrnelson |
0:cc9f4010bba6 | 345 | } |
shaunkrnelson |
0:cc9f4010bba6 | 346 | |
shaunkrnelson |
0:cc9f4010bba6 | 347 | int32_t OctetStringPacket::serializePayload(uint8_t *frame, int32_t len) |
shaunkrnelson |
0:cc9f4010bba6 | 348 | { |
shaunkrnelson |
0:cc9f4010bba6 | 349 | int32_t out = -1; |
shaunkrnelson |
0:cc9f4010bba6 | 350 | |
shaunkrnelson |
0:cc9f4010bba6 | 351 | if(oslen >= len) |
shaunkrnelson |
0:cc9f4010bba6 | 352 | { |
shaunkrnelson |
0:cc9f4010bba6 | 353 | memcpy(frame, buffer + PacketHeader::HEADER_SIZE, oslen); |
shaunkrnelson |
0:cc9f4010bba6 | 354 | out = oslen; |
shaunkrnelson |
0:cc9f4010bba6 | 355 | } |
shaunkrnelson |
0:cc9f4010bba6 | 356 | |
shaunkrnelson |
0:cc9f4010bba6 | 357 | return out; |
shaunkrnelson |
0:cc9f4010bba6 | 358 | } |
shaunkrnelson |
0:cc9f4010bba6 | 359 |