simple serial protocol

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

Committer:
rba90
Date:
Fri Sep 02 02:39:04 2016 +0000
Revision:
11:390476907bfc
The CommandPacket is reconstructed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 11:390476907bfc 1 #include "CommandPacket2.h"
rba90 11:390476907bfc 2 #include <stdlib.h>
rba90 11:390476907bfc 3 #include <stdint.h>
rba90 11:390476907bfc 4 #include <string.h>
rba90 11:390476907bfc 5 #include <stdio.h>
rba90 11:390476907bfc 6
rba90 11:390476907bfc 7 CommandPacket2::CommandPacket2()
rba90 11:390476907bfc 8 {
rba90 11:390476907bfc 9 // initialize payload
rba90 11:390476907bfc 10 memset(_payload, 0x0, sizeof(_payload));
rba90 11:390476907bfc 11
rba90 11:390476907bfc 12 // reset is verified state
rba90 11:390476907bfc 13 _isVerified = false;
rba90 11:390476907bfc 14 }
rba90 11:390476907bfc 15
rba90 11:390476907bfc 16 CommandPacket2::~CommandPacket2()
rba90 11:390476907bfc 17 {
rba90 11:390476907bfc 18
rba90 11:390476907bfc 19 }
rba90 11:390476907bfc 20
rba90 11:390476907bfc 21 void CommandPacket2::setSFlag(uint8_t sflag)
rba90 11:390476907bfc 22 {
rba90 11:390476907bfc 23 _sflag = sflag;
rba90 11:390476907bfc 24 _isVerified = false;
rba90 11:390476907bfc 25 }
rba90 11:390476907bfc 26
rba90 11:390476907bfc 27 void CommandPacket2::setCommand(uint8_t command)
rba90 11:390476907bfc 28 {
rba90 11:390476907bfc 29 _command = command;
rba90 11:390476907bfc 30 _isVerified = false;
rba90 11:390476907bfc 31 }
rba90 11:390476907bfc 32
rba90 11:390476907bfc 33 void CommandPacket2::setLength(uint8_t length)
rba90 11:390476907bfc 34 {
rba90 11:390476907bfc 35 _length = length;
rba90 11:390476907bfc 36 _isVerified = false;
rba90 11:390476907bfc 37 }
rba90 11:390476907bfc 38
rba90 11:390476907bfc 39 void CommandPacket2::setPayload(uint8_t idx, uint8_t payload)
rba90 11:390476907bfc 40 {
rba90 11:390476907bfc 41 _payload[idx] = payload;
rba90 11:390476907bfc 42 _isVerified = false;
rba90 11:390476907bfc 43 }
rba90 11:390476907bfc 44
rba90 11:390476907bfc 45 void CommandPacket2::setChecksum(uint8_t checksum)
rba90 11:390476907bfc 46 {
rba90 11:390476907bfc 47 _checksum = checksum;
rba90 11:390476907bfc 48 _isVerified = false;
rba90 11:390476907bfc 49 }
rba90 11:390476907bfc 50
rba90 11:390476907bfc 51 void CommandPacket2::setEFlag(uint8_t eflag)
rba90 11:390476907bfc 52 {
rba90 11:390476907bfc 53 _eflag = eflag;
rba90 11:390476907bfc 54 _isVerified = false;
rba90 11:390476907bfc 55 }
rba90 11:390476907bfc 56
rba90 11:390476907bfc 57 uint8_t CommandPacket2::getSFlag()
rba90 11:390476907bfc 58 {
rba90 11:390476907bfc 59 return _sflag;
rba90 11:390476907bfc 60 }
rba90 11:390476907bfc 61
rba90 11:390476907bfc 62 uint8_t CommandPacket2::getCommand()
rba90 11:390476907bfc 63 {
rba90 11:390476907bfc 64 return _command;
rba90 11:390476907bfc 65 }
rba90 11:390476907bfc 66
rba90 11:390476907bfc 67 uint8_t CommandPacket2::getLength()
rba90 11:390476907bfc 68 {
rba90 11:390476907bfc 69 return _length;
rba90 11:390476907bfc 70 }
rba90 11:390476907bfc 71
rba90 11:390476907bfc 72 uint8_t CommandPacket2::getPayload(uint8_t idx)
rba90 11:390476907bfc 73 {
rba90 11:390476907bfc 74 return _payload[idx];
rba90 11:390476907bfc 75 }
rba90 11:390476907bfc 76
rba90 11:390476907bfc 77 uint8_t CommandPacket2::getEFlag()
rba90 11:390476907bfc 78 {
rba90 11:390476907bfc 79 return _eflag;
rba90 11:390476907bfc 80 }
rba90 11:390476907bfc 81
rba90 11:390476907bfc 82 uint8_t CommandPacket2::getChecksum()
rba90 11:390476907bfc 83 {
rba90 11:390476907bfc 84 return _checksum;
rba90 11:390476907bfc 85 }
rba90 11:390476907bfc 86
rba90 11:390476907bfc 87 bool CommandPacket2::verify()
rba90 11:390476907bfc 88 {
rba90 11:390476907bfc 89 if (_isVerified)
rba90 11:390476907bfc 90 {
rba90 11:390476907bfc 91 return true;
rba90 11:390476907bfc 92 }
rba90 11:390476907bfc 93 else
rba90 11:390476907bfc 94 {
rba90 11:390476907bfc 95 return _checksum == calculate_checksum();
rba90 11:390476907bfc 96 }
rba90 11:390476907bfc 97 }
rba90 11:390476907bfc 98
rba90 11:390476907bfc 99 uint8_t CommandPacket2::calculate_checksum()
rba90 11:390476907bfc 100 {
rba90 11:390476907bfc 101 // take the sum of all user defined data bytes preceding checksum field
rba90 11:390476907bfc 102 uint8_t s = 0;
rba90 11:390476907bfc 103
rba90 11:390476907bfc 104 // add command
rba90 11:390476907bfc 105 s += _command;
rba90 11:390476907bfc 106
rba90 11:390476907bfc 107 // add length
rba90 11:390476907bfc 108 s += _length;
rba90 11:390476907bfc 109
rba90 11:390476907bfc 110 // add payload
rba90 11:390476907bfc 111 for (uint8_t i = 0; i < _length; i++)
rba90 11:390476907bfc 112 {
rba90 11:390476907bfc 113 s += _payload[i];
rba90 11:390476907bfc 114 }
rba90 11:390476907bfc 115
rba90 11:390476907bfc 116 // calculate two's complement of the remainder
rba90 11:390476907bfc 117 s = 0xff - s + 1;
rba90 11:390476907bfc 118
rba90 11:390476907bfc 119 return s;
rba90 11:390476907bfc 120 }
rba90 11:390476907bfc 121
rba90 11:390476907bfc 122 void CommandPacket2::generateChecksum()
rba90 11:390476907bfc 123 {
rba90 11:390476907bfc 124 _checksum = calculate_checksum();
rba90 11:390476907bfc 125
rba90 11:390476907bfc 126 _isVerified = true;
rba90 11:390476907bfc 127 }
rba90 11:390476907bfc 128
rba90 11:390476907bfc 129 int CommandPacket2::serialize(uint8_t *buffer)
rba90 11:390476907bfc 130 {
rba90 11:390476907bfc 131 // the user need to make sure that they have enough space for buffer
rba90 11:390476907bfc 132 if (!buffer) return 0;
rba90 11:390476907bfc 133
rba90 11:390476907bfc 134 char octet[3];
rba90 11:390476907bfc 135 int offset = 0;
rba90 11:390476907bfc 136
rba90 11:390476907bfc 137 // cast content to output buffer
rba90 11:390476907bfc 138 // sflag
rba90 11:390476907bfc 139 buffer[offset] = _sflag;
rba90 11:390476907bfc 140 offset += 1;
rba90 11:390476907bfc 141
rba90 11:390476907bfc 142 // command
rba90 11:390476907bfc 143 memset(octet, 0x0, sizeof(octet));
rba90 11:390476907bfc 144 sprintf(octet, "%02X", _command);
rba90 11:390476907bfc 145 memcpy(buffer + offset, octet, 2);
rba90 11:390476907bfc 146 offset += 2;
rba90 11:390476907bfc 147
rba90 11:390476907bfc 148 // length
rba90 11:390476907bfc 149 memset(octet, 0x0, sizeof(octet));
rba90 11:390476907bfc 150 sprintf(octet, "%02X", _length);
rba90 11:390476907bfc 151 memcpy(buffer + offset, octet, 2);
rba90 11:390476907bfc 152 offset += 2;
rba90 11:390476907bfc 153
rba90 11:390476907bfc 154 // payload
rba90 11:390476907bfc 155 for (uint8_t i = 0; i < _length; i++)
rba90 11:390476907bfc 156 {
rba90 11:390476907bfc 157 memset(octet, 0x0, sizeof(octet));
rba90 11:390476907bfc 158 sprintf(octet, "%02X", _payload[i]);
rba90 11:390476907bfc 159 memcpy(buffer + offset, octet, 2);
rba90 11:390476907bfc 160 offset += 2;
rba90 11:390476907bfc 161 }
rba90 11:390476907bfc 162
rba90 11:390476907bfc 163 // checksum
rba90 11:390476907bfc 164 memset(octet, 0x0, sizeof(octet));
rba90 11:390476907bfc 165 sprintf(octet, "%02X", _checksum);
rba90 11:390476907bfc 166 memcpy(buffer + offset, octet, 2);
rba90 11:390476907bfc 167 offset += 2;
rba90 11:390476907bfc 168
rba90 11:390476907bfc 169 // eflag
rba90 11:390476907bfc 170 buffer[offset] = _eflag;
rba90 11:390476907bfc 171 offset += 1;
rba90 11:390476907bfc 172
rba90 11:390476907bfc 173 return offset;
rba90 11:390476907bfc 174 }
rba90 11:390476907bfc 175