Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
AlohaFrame.cpp
00001 #include "AlohaFrame.h" 00002 #include "crc.h" 00003 #include <string.h> 00004 00005 #ifdef _DEBUG 00006 #include <cassert> 00007 #endif 00008 00009 #define CRC_BASE_IDX 3 00010 00011 AlohaFrame::AlohaFrame() 00012 { 00013 // init variables 00014 memset(_buffer, 0x0, sizeof(_buffer)); 00015 _isVerified = false; 00016 } 00017 00018 AlohaFrame::AlohaFrame(uint8_t *data, uint8_t sz) 00019 { 00020 // the user takes the responsibility of making sure 00021 // they have sufficient memory for data buffer 00022 00023 // copy data to internal buffer 00024 memcpy(_buffer, data, sz); 00025 00026 // set verify flag 00027 _isVerified = false; 00028 } 00029 00030 00031 AlohaFrame::~AlohaFrame() 00032 { 00033 } 00034 00035 void AlohaFrame::generateCrc() 00036 { 00037 uint8_t crc_idx = CRC_BASE_IDX + getPayloadLength(); 00038 00039 _buffer[crc_idx] = crc8(_buffer, crc_idx); 00040 00041 // set verify flag 00042 _isVerified = true; 00043 } 00044 00045 bool AlohaFrame::verify() 00046 { 00047 uint8_t crc_idx = CRC_BASE_IDX + getPayloadLength(); 00048 00049 if (_isVerified) 00050 { 00051 return true; 00052 } 00053 else 00054 { 00055 return _buffer[crc_idx] == crc8(_buffer, crc_idx); 00056 } 00057 } 00058 00059 uint8_t AlohaFrame::serialize(uint8_t *buffer) 00060 { 00061 uint8_t total_length = FIXED_BYTE + getPayloadLength(); 00062 00063 memcpy(buffer, _buffer, total_length); 00064 00065 return total_length; 00066 } 00067 00068 void AlohaFrame::setType(uint8_t type) 00069 { 00070 // clear upper 4 bits 00071 _buffer[0] &= 0x0f; 00072 00073 // set bits 00074 _buffer[0] |= type << 4; 00075 00076 // set verify flag 00077 _isVerified = false; 00078 } 00079 00080 void AlohaFrame::setPayloadLength(uint8_t length) 00081 { 00082 // clear lower 4 bits 00083 _buffer[0] &= 0xf0; 00084 00085 // set bits 00086 _buffer[0] |= length & 0x0f; 00087 00088 // set verify flag 00089 _isVerified = false; 00090 } 00091 00092 void AlohaFrame::setSourceAddress(uint8_t sa) 00093 { 00094 // clear upper 4 bits 00095 _buffer[1] &= 0x0f; 00096 00097 // set bits 00098 _buffer[1] |= sa << 4; 00099 00100 // set verify flag 00101 _isVerified = false; 00102 } 00103 00104 void AlohaFrame::setDestinationAddress(uint8_t da) 00105 { 00106 // clear lower 4 bits 00107 _buffer[1] &= 0xf0; 00108 00109 // set bits 00110 _buffer[1] |= da & 0x0f; 00111 00112 // set verify flag 00113 _isVerified = false; 00114 } 00115 00116 void AlohaFrame::setFullMessageFlag(uint8_t fmf) 00117 { 00118 // clear upper 1 bits 00119 _buffer[2] &= 0x7f; 00120 00121 // set bits 00122 _buffer[2] |= fmf << 7; 00123 00124 // set verify flag 00125 _isVerified = false; 00126 } 00127 00128 void AlohaFrame::setSequenceID(uint8_t seqid) 00129 { 00130 // clear lower 7 bits 00131 _buffer[2] &= 0x80; 00132 00133 // set bits 00134 _buffer[2] |= seqid & 0x7f; 00135 00136 // set verify flag 00137 _isVerified = false; 00138 } 00139 00140 void AlohaFrame::setPayload(uint8_t idx, uint8_t payload) 00141 { 00142 // set payload based on index 00143 _buffer[CRC_BASE_IDX + idx] = payload; 00144 00145 // set verify flag 00146 _isVerified = false; 00147 } 00148 00149 uint8_t AlohaFrame::getType() 00150 { 00151 return (_buffer[0] & 0xf0) >> 4; 00152 } 00153 00154 uint8_t AlohaFrame::getPayloadLength() 00155 { 00156 return _buffer[0] & 0x0f; 00157 } 00158 00159 uint8_t AlohaFrame::getSourceAddress() 00160 { 00161 return (_buffer[1] & 0xf0) >> 4; 00162 } 00163 00164 uint8_t AlohaFrame::getDestinationAddress() 00165 { 00166 return _buffer[1] & 0x0f; 00167 } 00168 00169 uint8_t AlohaFrame::getFullMessageFlag() 00170 { 00171 return (_buffer[2] & 0x80) >> 7; 00172 } 00173 00174 uint8_t AlohaFrame::getSequenceID() 00175 { 00176 return _buffer[2] & 0x7f; 00177 } 00178 00179 uint8_t AlohaFrame::getPayload(uint8_t idx) 00180 { 00181 return _buffer[CRC_BASE_IDX + idx]; 00182 } 00183 00184 uint8_t AlohaFrame::getCrc() 00185 { 00186 uint8_t crc_idx = CRC_BASE_IDX + getPayloadLength(); 00187 00188 if (_isVerified) 00189 { 00190 return _buffer[crc_idx]; 00191 } 00192 else 00193 { 00194 return crc8(_buffer, crc_idx); 00195 } 00196 } 00197 00198 #ifdef _DEBUG 00199 void AlohaFrame::unit_test() 00200 { 00201 // value test 00202 AlohaFrame testObject; 00203 00204 testObject.setType(0x0); 00205 testObject.setPayloadLength(0x3); 00206 testObject.setSourceAddress(0x2); 00207 testObject.setDestinationAddress(0x3); 00208 testObject.setFullMessageFlag(0x1); 00209 testObject.setSequenceID(0x4); 00210 testObject.setPayload(0x56, 0); 00211 testObject.setPayload(0x67, 1); 00212 testObject.setPayload(0x89, 2); 00213 00214 assert(0x0 == testObject.getType()); 00215 assert(0x3 == testObject.getPayloadLength()); 00216 assert(0x2 == testObject.getSourceAddress()); 00217 assert(0x3 == testObject.getDestinationAddress()); 00218 assert(0x1 == testObject.getFullMessageFlag()); 00219 assert(0x4 == testObject.getSequenceID()); 00220 assert(0x56 == testObject.getPayload(0)); 00221 assert(0x67 == testObject.getPayload(1)); 00222 assert(0x89 == testObject.getPayload(2)); 00223 00224 assert(false == testObject.verify()); 00225 testObject.generateCrc(); 00226 assert(true == testObject.verify()); 00227 assert(0xa1 == testObject.getCrc()); 00228 00229 // value test round 2 00230 uint8_t testString[] = { 0x03, 0x23, 0x84, 0x56, 0x67, 0x89, 0xa1 }; 00231 AlohaFrame testObject2(testString, 7); 00232 00233 assert(0x0 == testObject2.getType()); 00234 assert(0x3 == testObject2.getPayloadLength()); 00235 assert(0x2 == testObject2.getSourceAddress()); 00236 assert(0x3 == testObject2.getDestinationAddress()); 00237 assert(0x1 == testObject2.getFullMessageFlag()); 00238 assert(0x4 == testObject2.getSequenceID()); 00239 assert(0x56 == testObject2.getPayload(0)); 00240 assert(0x67 == testObject2.getPayload(1)); 00241 assert(0x89 == testObject2.getPayload(2)); 00242 00243 assert(true == testObject2.verify()); 00244 testObject2.generateCrc(); 00245 assert(true == testObject2.verify()); 00246 assert(0xa1 == testObject2.getCrc()); 00247 } 00248 #endif
Generated on Wed Jul 20 2022 10:55:18 by
1.7.2