Layer 2 Aloha packet
AlohaFrame.cpp@3:9bc861001717, 2016-08-11 (annotated)
- Committer:
- rba90
- Date:
- Thu Aug 11 02:34:23 2016 +0000
- Revision:
- 3:9bc861001717
- Parent:
- 2:849128cfddb8
move some #define from .c to .h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 0:bd0c4fdc6833 | 1 | #include "AlohaFrame.h" |
rba90 | 0:bd0c4fdc6833 | 2 | #include "crc.h" |
rba90 | 0:bd0c4fdc6833 | 3 | #include <string.h> |
rba90 | 0:bd0c4fdc6833 | 4 | |
rba90 | 0:bd0c4fdc6833 | 5 | #ifdef _DEBUG |
rba90 | 0:bd0c4fdc6833 | 6 | #include <cassert> |
rba90 | 0:bd0c4fdc6833 | 7 | #endif |
rba90 | 0:bd0c4fdc6833 | 8 | |
rba90 | 0:bd0c4fdc6833 | 9 | #define CRC_BASE_IDX 3 |
rba90 | 0:bd0c4fdc6833 | 10 | |
rba90 | 0:bd0c4fdc6833 | 11 | AlohaFrame::AlohaFrame() |
rba90 | 0:bd0c4fdc6833 | 12 | { |
rba90 | 0:bd0c4fdc6833 | 13 | // init variables |
rba90 | 0:bd0c4fdc6833 | 14 | memset(_buffer, 0x0, sizeof(_buffer)); |
rba90 | 0:bd0c4fdc6833 | 15 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 16 | } |
rba90 | 0:bd0c4fdc6833 | 17 | |
rba90 | 0:bd0c4fdc6833 | 18 | AlohaFrame::AlohaFrame(uint8_t *data, uint8_t sz) |
rba90 | 0:bd0c4fdc6833 | 19 | { |
rba90 | 0:bd0c4fdc6833 | 20 | // the user takes the responsibility of making sure |
rba90 | 0:bd0c4fdc6833 | 21 | // they have sufficient memory for data buffer |
rba90 | 0:bd0c4fdc6833 | 22 | |
rba90 | 0:bd0c4fdc6833 | 23 | // copy data to internal buffer |
rba90 | 0:bd0c4fdc6833 | 24 | memcpy(_buffer, data, sz); |
rba90 | 0:bd0c4fdc6833 | 25 | |
rba90 | 0:bd0c4fdc6833 | 26 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 27 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 28 | } |
rba90 | 0:bd0c4fdc6833 | 29 | |
rba90 | 0:bd0c4fdc6833 | 30 | |
rba90 | 0:bd0c4fdc6833 | 31 | AlohaFrame::~AlohaFrame() |
rba90 | 0:bd0c4fdc6833 | 32 | { |
rba90 | 0:bd0c4fdc6833 | 33 | } |
rba90 | 0:bd0c4fdc6833 | 34 | |
rba90 | 0:bd0c4fdc6833 | 35 | void AlohaFrame::generateCrc() |
rba90 | 0:bd0c4fdc6833 | 36 | { |
rba90 | 0:bd0c4fdc6833 | 37 | uint8_t crc_idx = CRC_BASE_IDX + getPayloadLength(); |
rba90 | 0:bd0c4fdc6833 | 38 | |
rba90 | 0:bd0c4fdc6833 | 39 | _buffer[crc_idx] = crc8(_buffer, crc_idx); |
rba90 | 0:bd0c4fdc6833 | 40 | |
rba90 | 0:bd0c4fdc6833 | 41 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 42 | _isVerified = true; |
rba90 | 0:bd0c4fdc6833 | 43 | } |
rba90 | 0:bd0c4fdc6833 | 44 | |
rba90 | 0:bd0c4fdc6833 | 45 | bool AlohaFrame::verify() |
rba90 | 0:bd0c4fdc6833 | 46 | { |
rba90 | 0:bd0c4fdc6833 | 47 | uint8_t crc_idx = CRC_BASE_IDX + getPayloadLength(); |
rba90 | 0:bd0c4fdc6833 | 48 | |
rba90 | 0:bd0c4fdc6833 | 49 | if (_isVerified) |
rba90 | 0:bd0c4fdc6833 | 50 | { |
rba90 | 0:bd0c4fdc6833 | 51 | return true; |
rba90 | 0:bd0c4fdc6833 | 52 | } |
rba90 | 0:bd0c4fdc6833 | 53 | else |
rba90 | 0:bd0c4fdc6833 | 54 | { |
rba90 | 0:bd0c4fdc6833 | 55 | return _buffer[crc_idx] == crc8(_buffer, crc_idx); |
rba90 | 0:bd0c4fdc6833 | 56 | } |
rba90 | 0:bd0c4fdc6833 | 57 | } |
rba90 | 0:bd0c4fdc6833 | 58 | |
rba90 | 0:bd0c4fdc6833 | 59 | uint8_t AlohaFrame::serialize(uint8_t *buffer) |
rba90 | 0:bd0c4fdc6833 | 60 | { |
rba90 | 0:bd0c4fdc6833 | 61 | uint8_t total_length = FIXED_BYTE + getPayloadLength(); |
rba90 | 0:bd0c4fdc6833 | 62 | |
rba90 | 0:bd0c4fdc6833 | 63 | memcpy(buffer, _buffer, total_length); |
rba90 | 0:bd0c4fdc6833 | 64 | |
rba90 | 0:bd0c4fdc6833 | 65 | return total_length; |
rba90 | 0:bd0c4fdc6833 | 66 | } |
rba90 | 0:bd0c4fdc6833 | 67 | |
rba90 | 0:bd0c4fdc6833 | 68 | void AlohaFrame::setType(uint8_t type) |
rba90 | 0:bd0c4fdc6833 | 69 | { |
rba90 | 0:bd0c4fdc6833 | 70 | // clear upper 4 bits |
rba90 | 0:bd0c4fdc6833 | 71 | _buffer[0] &= 0x0f; |
rba90 | 0:bd0c4fdc6833 | 72 | |
rba90 | 0:bd0c4fdc6833 | 73 | // set bits |
rba90 | 0:bd0c4fdc6833 | 74 | _buffer[0] |= type << 4; |
rba90 | 0:bd0c4fdc6833 | 75 | |
rba90 | 0:bd0c4fdc6833 | 76 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 77 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 78 | } |
rba90 | 0:bd0c4fdc6833 | 79 | |
rba90 | 0:bd0c4fdc6833 | 80 | void AlohaFrame::setPayloadLength(uint8_t length) |
rba90 | 0:bd0c4fdc6833 | 81 | { |
rba90 | 0:bd0c4fdc6833 | 82 | // clear lower 4 bits |
rba90 | 0:bd0c4fdc6833 | 83 | _buffer[0] &= 0xf0; |
rba90 | 0:bd0c4fdc6833 | 84 | |
rba90 | 0:bd0c4fdc6833 | 85 | // set bits |
rba90 | 0:bd0c4fdc6833 | 86 | _buffer[0] |= length & 0x0f; |
rba90 | 0:bd0c4fdc6833 | 87 | |
rba90 | 0:bd0c4fdc6833 | 88 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 89 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 90 | } |
rba90 | 0:bd0c4fdc6833 | 91 | |
rba90 | 0:bd0c4fdc6833 | 92 | void AlohaFrame::setSourceAddress(uint8_t sa) |
rba90 | 0:bd0c4fdc6833 | 93 | { |
rba90 | 0:bd0c4fdc6833 | 94 | // clear upper 4 bits |
rba90 | 0:bd0c4fdc6833 | 95 | _buffer[1] &= 0x0f; |
rba90 | 0:bd0c4fdc6833 | 96 | |
rba90 | 0:bd0c4fdc6833 | 97 | // set bits |
rba90 | 0:bd0c4fdc6833 | 98 | _buffer[1] |= sa << 4; |
rba90 | 0:bd0c4fdc6833 | 99 | |
rba90 | 0:bd0c4fdc6833 | 100 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 101 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 102 | } |
rba90 | 0:bd0c4fdc6833 | 103 | |
rba90 | 0:bd0c4fdc6833 | 104 | void AlohaFrame::setDestinationAddress(uint8_t da) |
rba90 | 0:bd0c4fdc6833 | 105 | { |
rba90 | 0:bd0c4fdc6833 | 106 | // clear lower 4 bits |
rba90 | 0:bd0c4fdc6833 | 107 | _buffer[1] &= 0xf0; |
rba90 | 0:bd0c4fdc6833 | 108 | |
rba90 | 0:bd0c4fdc6833 | 109 | // set bits |
rba90 | 0:bd0c4fdc6833 | 110 | _buffer[1] |= da & 0x0f; |
rba90 | 0:bd0c4fdc6833 | 111 | |
rba90 | 0:bd0c4fdc6833 | 112 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 113 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 114 | } |
rba90 | 0:bd0c4fdc6833 | 115 | |
rba90 | 0:bd0c4fdc6833 | 116 | void AlohaFrame::setFullMessageFlag(uint8_t fmf) |
rba90 | 0:bd0c4fdc6833 | 117 | { |
rba90 | 0:bd0c4fdc6833 | 118 | // clear upper 1 bits |
rba90 | 0:bd0c4fdc6833 | 119 | _buffer[2] &= 0x7f; |
rba90 | 0:bd0c4fdc6833 | 120 | |
rba90 | 0:bd0c4fdc6833 | 121 | // set bits |
rba90 | 0:bd0c4fdc6833 | 122 | _buffer[2] |= fmf << 7; |
rba90 | 0:bd0c4fdc6833 | 123 | |
rba90 | 0:bd0c4fdc6833 | 124 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 125 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 126 | } |
rba90 | 0:bd0c4fdc6833 | 127 | |
rba90 | 0:bd0c4fdc6833 | 128 | void AlohaFrame::setSequenceID(uint8_t seqid) |
rba90 | 0:bd0c4fdc6833 | 129 | { |
rba90 | 0:bd0c4fdc6833 | 130 | // clear lower 7 bits |
rba90 | 0:bd0c4fdc6833 | 131 | _buffer[2] &= 0x80; |
rba90 | 0:bd0c4fdc6833 | 132 | |
rba90 | 0:bd0c4fdc6833 | 133 | // set bits |
rba90 | 0:bd0c4fdc6833 | 134 | _buffer[2] |= seqid & 0x7f; |
rba90 | 0:bd0c4fdc6833 | 135 | |
rba90 | 0:bd0c4fdc6833 | 136 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 137 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 138 | } |
rba90 | 0:bd0c4fdc6833 | 139 | |
rba90 | 2:849128cfddb8 | 140 | void AlohaFrame::setPayload(uint8_t idx, uint8_t payload) |
rba90 | 0:bd0c4fdc6833 | 141 | { |
rba90 | 0:bd0c4fdc6833 | 142 | // set payload based on index |
rba90 | 0:bd0c4fdc6833 | 143 | _buffer[CRC_BASE_IDX + idx] = payload; |
rba90 | 0:bd0c4fdc6833 | 144 | |
rba90 | 0:bd0c4fdc6833 | 145 | // set verify flag |
rba90 | 0:bd0c4fdc6833 | 146 | _isVerified = false; |
rba90 | 0:bd0c4fdc6833 | 147 | } |
rba90 | 0:bd0c4fdc6833 | 148 | |
rba90 | 0:bd0c4fdc6833 | 149 | uint8_t AlohaFrame::getType() |
rba90 | 0:bd0c4fdc6833 | 150 | { |
rba90 | 0:bd0c4fdc6833 | 151 | return (_buffer[0] & 0xf0) >> 4; |
rba90 | 0:bd0c4fdc6833 | 152 | } |
rba90 | 0:bd0c4fdc6833 | 153 | |
rba90 | 0:bd0c4fdc6833 | 154 | uint8_t AlohaFrame::getPayloadLength() |
rba90 | 0:bd0c4fdc6833 | 155 | { |
rba90 | 0:bd0c4fdc6833 | 156 | return _buffer[0] & 0x0f; |
rba90 | 0:bd0c4fdc6833 | 157 | } |
rba90 | 0:bd0c4fdc6833 | 158 | |
rba90 | 0:bd0c4fdc6833 | 159 | uint8_t AlohaFrame::getSourceAddress() |
rba90 | 0:bd0c4fdc6833 | 160 | { |
rba90 | 0:bd0c4fdc6833 | 161 | return (_buffer[1] & 0xf0) >> 4; |
rba90 | 0:bd0c4fdc6833 | 162 | } |
rba90 | 0:bd0c4fdc6833 | 163 | |
rba90 | 0:bd0c4fdc6833 | 164 | uint8_t AlohaFrame::getDestinationAddress() |
rba90 | 0:bd0c4fdc6833 | 165 | { |
rba90 | 0:bd0c4fdc6833 | 166 | return _buffer[1] & 0x0f; |
rba90 | 0:bd0c4fdc6833 | 167 | } |
rba90 | 0:bd0c4fdc6833 | 168 | |
rba90 | 0:bd0c4fdc6833 | 169 | uint8_t AlohaFrame::getFullMessageFlag() |
rba90 | 0:bd0c4fdc6833 | 170 | { |
rba90 | 0:bd0c4fdc6833 | 171 | return (_buffer[2] & 0x80) >> 7; |
rba90 | 0:bd0c4fdc6833 | 172 | } |
rba90 | 0:bd0c4fdc6833 | 173 | |
rba90 | 0:bd0c4fdc6833 | 174 | uint8_t AlohaFrame::getSequenceID() |
rba90 | 0:bd0c4fdc6833 | 175 | { |
rba90 | 0:bd0c4fdc6833 | 176 | return _buffer[2] & 0x7f; |
rba90 | 0:bd0c4fdc6833 | 177 | } |
rba90 | 0:bd0c4fdc6833 | 178 | |
rba90 | 0:bd0c4fdc6833 | 179 | uint8_t AlohaFrame::getPayload(uint8_t idx) |
rba90 | 0:bd0c4fdc6833 | 180 | { |
rba90 | 0:bd0c4fdc6833 | 181 | return _buffer[CRC_BASE_IDX + idx]; |
rba90 | 0:bd0c4fdc6833 | 182 | } |
rba90 | 0:bd0c4fdc6833 | 183 | |
rba90 | 0:bd0c4fdc6833 | 184 | uint8_t AlohaFrame::getCrc() |
rba90 | 0:bd0c4fdc6833 | 185 | { |
rba90 | 0:bd0c4fdc6833 | 186 | uint8_t crc_idx = CRC_BASE_IDX + getPayloadLength(); |
rba90 | 0:bd0c4fdc6833 | 187 | |
rba90 | 0:bd0c4fdc6833 | 188 | if (_isVerified) |
rba90 | 0:bd0c4fdc6833 | 189 | { |
rba90 | 0:bd0c4fdc6833 | 190 | return _buffer[crc_idx]; |
rba90 | 0:bd0c4fdc6833 | 191 | } |
rba90 | 0:bd0c4fdc6833 | 192 | else |
rba90 | 0:bd0c4fdc6833 | 193 | { |
rba90 | 0:bd0c4fdc6833 | 194 | return crc8(_buffer, crc_idx); |
rba90 | 0:bd0c4fdc6833 | 195 | } |
rba90 | 0:bd0c4fdc6833 | 196 | } |
rba90 | 0:bd0c4fdc6833 | 197 | |
rba90 | 0:bd0c4fdc6833 | 198 | #ifdef _DEBUG |
rba90 | 0:bd0c4fdc6833 | 199 | void AlohaFrame::unit_test() |
rba90 | 0:bd0c4fdc6833 | 200 | { |
rba90 | 0:bd0c4fdc6833 | 201 | // value test |
rba90 | 0:bd0c4fdc6833 | 202 | AlohaFrame testObject; |
rba90 | 0:bd0c4fdc6833 | 203 | |
rba90 | 0:bd0c4fdc6833 | 204 | testObject.setType(0x0); |
rba90 | 0:bd0c4fdc6833 | 205 | testObject.setPayloadLength(0x3); |
rba90 | 0:bd0c4fdc6833 | 206 | testObject.setSourceAddress(0x2); |
rba90 | 0:bd0c4fdc6833 | 207 | testObject.setDestinationAddress(0x3); |
rba90 | 0:bd0c4fdc6833 | 208 | testObject.setFullMessageFlag(0x1); |
rba90 | 0:bd0c4fdc6833 | 209 | testObject.setSequenceID(0x4); |
rba90 | 0:bd0c4fdc6833 | 210 | testObject.setPayload(0x56, 0); |
rba90 | 0:bd0c4fdc6833 | 211 | testObject.setPayload(0x67, 1); |
rba90 | 0:bd0c4fdc6833 | 212 | testObject.setPayload(0x89, 2); |
rba90 | 0:bd0c4fdc6833 | 213 | |
rba90 | 0:bd0c4fdc6833 | 214 | assert(0x0 == testObject.getType()); |
rba90 | 0:bd0c4fdc6833 | 215 | assert(0x3 == testObject.getPayloadLength()); |
rba90 | 0:bd0c4fdc6833 | 216 | assert(0x2 == testObject.getSourceAddress()); |
rba90 | 0:bd0c4fdc6833 | 217 | assert(0x3 == testObject.getDestinationAddress()); |
rba90 | 0:bd0c4fdc6833 | 218 | assert(0x1 == testObject.getFullMessageFlag()); |
rba90 | 0:bd0c4fdc6833 | 219 | assert(0x4 == testObject.getSequenceID()); |
rba90 | 0:bd0c4fdc6833 | 220 | assert(0x56 == testObject.getPayload(0)); |
rba90 | 0:bd0c4fdc6833 | 221 | assert(0x67 == testObject.getPayload(1)); |
rba90 | 0:bd0c4fdc6833 | 222 | assert(0x89 == testObject.getPayload(2)); |
rba90 | 0:bd0c4fdc6833 | 223 | |
rba90 | 0:bd0c4fdc6833 | 224 | assert(false == testObject.verify()); |
rba90 | 0:bd0c4fdc6833 | 225 | testObject.generateCrc(); |
rba90 | 0:bd0c4fdc6833 | 226 | assert(true == testObject.verify()); |
rba90 | 0:bd0c4fdc6833 | 227 | assert(0xa1 == testObject.getCrc()); |
rba90 | 0:bd0c4fdc6833 | 228 | |
rba90 | 0:bd0c4fdc6833 | 229 | // value test round 2 |
rba90 | 0:bd0c4fdc6833 | 230 | uint8_t testString[] = { 0x03, 0x23, 0x84, 0x56, 0x67, 0x89, 0xa1 }; |
rba90 | 0:bd0c4fdc6833 | 231 | AlohaFrame testObject2(testString, 7); |
rba90 | 0:bd0c4fdc6833 | 232 | |
rba90 | 0:bd0c4fdc6833 | 233 | assert(0x0 == testObject2.getType()); |
rba90 | 0:bd0c4fdc6833 | 234 | assert(0x3 == testObject2.getPayloadLength()); |
rba90 | 0:bd0c4fdc6833 | 235 | assert(0x2 == testObject2.getSourceAddress()); |
rba90 | 0:bd0c4fdc6833 | 236 | assert(0x3 == testObject2.getDestinationAddress()); |
rba90 | 0:bd0c4fdc6833 | 237 | assert(0x1 == testObject2.getFullMessageFlag()); |
rba90 | 0:bd0c4fdc6833 | 238 | assert(0x4 == testObject2.getSequenceID()); |
rba90 | 0:bd0c4fdc6833 | 239 | assert(0x56 == testObject2.getPayload(0)); |
rba90 | 0:bd0c4fdc6833 | 240 | assert(0x67 == testObject2.getPayload(1)); |
rba90 | 0:bd0c4fdc6833 | 241 | assert(0x89 == testObject2.getPayload(2)); |
rba90 | 0:bd0c4fdc6833 | 242 | |
rba90 | 0:bd0c4fdc6833 | 243 | assert(true == testObject2.verify()); |
rba90 | 0:bd0c4fdc6833 | 244 | testObject2.generateCrc(); |
rba90 | 0:bd0c4fdc6833 | 245 | assert(true == testObject2.verify()); |
rba90 | 0:bd0c4fdc6833 | 246 | assert(0xa1 == testObject2.getCrc()); |
rba90 | 0:bd0c4fdc6833 | 247 | } |
rba90 | 0:bd0c4fdc6833 | 248 | #endif |