simple serial protocol

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

Committer:
rba90
Date:
Sun Jul 17 08:35:56 2016 +0000
Revision:
4:4a92da3f19c0
Parent:
3:22bb958f0eb0
Child:
5:d39ee3fc4f4a
response no error if return 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 0:21e4e3afa5e6 1 #include "SerialInterfaceProtocol.h"
rba90 1:03208c983c8b 2 #include "mbed.h"
rba90 0:21e4e3afa5e6 3
rba90 0:21e4e3afa5e6 4 // #define DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 5
rba90 0:21e4e3afa5e6 6 SerialInterfaceProtocol::SerialInterfaceProtocol(SerialBuffer_t *in, SerialBuffer_t *out)
rba90 0:21e4e3afa5e6 7 {
rba90 0:21e4e3afa5e6 8 // assign input and output buffers
rba90 0:21e4e3afa5e6 9 SerialInputBuffer = in;
rba90 0:21e4e3afa5e6 10 SerialOutputBuffer = out;
rba90 0:21e4e3afa5e6 11
rba90 0:21e4e3afa5e6 12 // init command vector table
rba90 0:21e4e3afa5e6 13 for (int i = 0; i < SIP_CMD_VECTOR_TABLE_SZ; i++)
rba90 0:21e4e3afa5e6 14 {
rba90 0:21e4e3afa5e6 15 CommandVectorTable[i] = NULL;
rba90 0:21e4e3afa5e6 16 }
rba90 0:21e4e3afa5e6 17
rba90 0:21e4e3afa5e6 18 // init internal state machine
rba90 0:21e4e3afa5e6 19 state = CommandPacket::NONE;
rba90 0:21e4e3afa5e6 20
rba90 0:21e4e3afa5e6 21 // init internal state
rba90 3:22bb958f0eb0 22 isChecksumEnabled = false;
rba90 0:21e4e3afa5e6 23 }
rba90 0:21e4e3afa5e6 24
rba90 0:21e4e3afa5e6 25 SerialInterfaceProtocol::~SerialInterfaceProtocol()
rba90 0:21e4e3afa5e6 26 {
rba90 0:21e4e3afa5e6 27
rba90 0:21e4e3afa5e6 28 }
rba90 0:21e4e3afa5e6 29
rba90 0:21e4e3afa5e6 30 void SerialInterfaceProtocol::registerCommand(uint8_t command, callback_func f)
rba90 0:21e4e3afa5e6 31 {
rba90 0:21e4e3afa5e6 32 CommandVectorTable[command] = f;
rba90 0:21e4e3afa5e6 33 }
rba90 0:21e4e3afa5e6 34
rba90 0:21e4e3afa5e6 35 void SerialInterfaceProtocol::deRegisterCommand(uint8_t command)
rba90 0:21e4e3afa5e6 36 {
rba90 0:21e4e3afa5e6 37 CommandVectorTable[command] = NULL;
rba90 0:21e4e3afa5e6 38 }
rba90 0:21e4e3afa5e6 39
rba90 0:21e4e3afa5e6 40 int SerialInterfaceProtocol::execute(uint8_t *response, uint8_t *response_length)
rba90 0:21e4e3afa5e6 41 {
rba90 0:21e4e3afa5e6 42
rba90 0:21e4e3afa5e6 43 // execute the command if it's already been registered
rba90 0:21e4e3afa5e6 44 if (CommandVectorTable[PacketBuffer.command] != NULL)
rba90 0:21e4e3afa5e6 45 {
rba90 0:21e4e3afa5e6 46 return CommandVectorTable[PacketBuffer.command](
rba90 0:21e4e3afa5e6 47 PacketBuffer.payload,
rba90 0:21e4e3afa5e6 48 PacketBuffer.length,
rba90 0:21e4e3afa5e6 49 response,
rba90 0:21e4e3afa5e6 50 response_length
rba90 0:21e4e3afa5e6 51 );
rba90 0:21e4e3afa5e6 52 }
rba90 0:21e4e3afa5e6 53
rba90 0:21e4e3afa5e6 54 printf("Callback function not registered\r\n");
rba90 0:21e4e3afa5e6 55
rba90 0:21e4e3afa5e6 56 return -1;
rba90 0:21e4e3afa5e6 57 }
rba90 0:21e4e3afa5e6 58
rba90 0:21e4e3afa5e6 59 int SerialInterfaceProtocol::assemble(uint8_t *response, uint8_t response_length)
rba90 0:21e4e3afa5e6 60 {
rba90 0:21e4e3afa5e6 61 // prepare for packet buffer
rba90 0:21e4e3afa5e6 62 PacketBuffer.sflag = CommandPacket::CP_SFLAG;
rba90 0:21e4e3afa5e6 63
rba90 0:21e4e3afa5e6 64 PacketBuffer.command = 0xE0 | ((uint8_t) PacketBuffer.errno & 0x0f);
rba90 0:21e4e3afa5e6 65
rba90 0:21e4e3afa5e6 66 PacketBuffer.length = response_length;
rba90 0:21e4e3afa5e6 67
rba90 0:21e4e3afa5e6 68 memcpy(PacketBuffer.payload, response, response_length);
rba90 0:21e4e3afa5e6 69
rba90 0:21e4e3afa5e6 70 PacketBuffer.checksum = PacketBuffer.generate_checksum();
rba90 0:21e4e3afa5e6 71
rba90 0:21e4e3afa5e6 72 PacketBuffer.eflag = CommandPacket::CP_EFLAG;
rba90 0:21e4e3afa5e6 73
rba90 0:21e4e3afa5e6 74
rba90 0:21e4e3afa5e6 75 // serialize the packet
rba90 0:21e4e3afa5e6 76 int total_len = PacketBuffer.length * 2 + 8;
rba90 0:21e4e3afa5e6 77 uint8_t buffer[total_len];
rba90 0:21e4e3afa5e6 78 memset(buffer, 0x0, total_len);
rba90 0:21e4e3afa5e6 79
rba90 0:21e4e3afa5e6 80 PacketBuffer.serialize(buffer);
rba90 1:03208c983c8b 81
rba90 0:21e4e3afa5e6 82 // add to ring buffer
rba90 0:21e4e3afa5e6 83 for (int i = 0; i < total_len; i++)
rba90 0:21e4e3afa5e6 84 {
rba90 0:21e4e3afa5e6 85 SerialOutputBuffer->enqueue(buffer[i]);
rba90 0:21e4e3afa5e6 86 }
rba90 0:21e4e3afa5e6 87
rba90 0:21e4e3afa5e6 88 return 0;
rba90 0:21e4e3afa5e6 89
rba90 0:21e4e3afa5e6 90 }
rba90 0:21e4e3afa5e6 91
rba90 0:21e4e3afa5e6 92 void SerialInterfaceProtocol::poll()
rba90 0:21e4e3afa5e6 93 {
rba90 0:21e4e3afa5e6 94 static uint8_t payload_counter = 0;
rba90 0:21e4e3afa5e6 95 uint8_t response[SIP_MAX_RESP_LEN];
rba90 0:21e4e3afa5e6 96 uint8_t response_length;
rba90 0:21e4e3afa5e6 97
rba90 0:21e4e3afa5e6 98 // fetch data from ring buffer
rba90 0:21e4e3afa5e6 99 while (SerialInputBuffer->getCounter() > 0)
rba90 0:21e4e3afa5e6 100 {
rba90 0:21e4e3afa5e6 101 uint8_t ch;
rba90 0:21e4e3afa5e6 102 ch = SerialInputBuffer->dequeue();
rba90 0:21e4e3afa5e6 103
rba90 0:21e4e3afa5e6 104 // reset state to keep sync
rba90 0:21e4e3afa5e6 105 if (ch == CommandPacket::CP_SFLAG)
rba90 0:21e4e3afa5e6 106 {
rba90 0:21e4e3afa5e6 107 state = CommandPacket::SFLAG;
rba90 0:21e4e3afa5e6 108
rba90 0:21e4e3afa5e6 109 // reset variable
rba90 0:21e4e3afa5e6 110 payload_counter = 0;
rba90 0:21e4e3afa5e6 111 memset(PacketBuffer.payload, 0x0, sizeof(PacketBuffer.payload));
rba90 0:21e4e3afa5e6 112 PacketBuffer.errno = CommandPacket::NO_ERROR;
rba90 0:21e4e3afa5e6 113 }
rba90 0:21e4e3afa5e6 114
rba90 0:21e4e3afa5e6 115 switch (state)
rba90 0:21e4e3afa5e6 116 {
rba90 0:21e4e3afa5e6 117 case CommandPacket::SFLAG:
rba90 0:21e4e3afa5e6 118 PacketBuffer.sflag = ch;
rba90 0:21e4e3afa5e6 119 state = CommandPacket::COMMAND_H;
rba90 0:21e4e3afa5e6 120 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 121 printf("CommandPacket::SFLAG: 0x%x\r\n", PacketBuffer.sflag);
rba90 0:21e4e3afa5e6 122 #endif
rba90 0:21e4e3afa5e6 123 break;
rba90 0:21e4e3afa5e6 124
rba90 0:21e4e3afa5e6 125 case CommandPacket::COMMAND_H:
rba90 0:21e4e3afa5e6 126 PacketBuffer.command = hexchar_to_uint8(ch) << 4;
rba90 0:21e4e3afa5e6 127 state = CommandPacket::COMMAND_L;
rba90 0:21e4e3afa5e6 128 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 129 printf("CommandPacket::COMMAND_H: 0x%x\r\n", PacketBuffer.command);
rba90 0:21e4e3afa5e6 130 #endif
rba90 0:21e4e3afa5e6 131 break;
rba90 0:21e4e3afa5e6 132
rba90 0:21e4e3afa5e6 133 case CommandPacket::COMMAND_L:
rba90 0:21e4e3afa5e6 134 PacketBuffer.command |= (hexchar_to_uint8(ch) & 0x0f);
rba90 0:21e4e3afa5e6 135 state = CommandPacket::LENGTH_H;
rba90 0:21e4e3afa5e6 136 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 137 printf("CommandPacket::COMMAND_L: 0x%x\r\n", PacketBuffer.command);
rba90 0:21e4e3afa5e6 138 #endif
rba90 0:21e4e3afa5e6 139 break;
rba90 0:21e4e3afa5e6 140
rba90 0:21e4e3afa5e6 141 case CommandPacket::LENGTH_H:
rba90 0:21e4e3afa5e6 142 PacketBuffer.length = hexchar_to_uint8(ch) << 4;
rba90 0:21e4e3afa5e6 143 state = CommandPacket::LENGTH_L;
rba90 0:21e4e3afa5e6 144 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 145 printf("CommandPacket::LENGTH_H: 0x%x\r\n", PacketBuffer.length);
rba90 0:21e4e3afa5e6 146 #endif
rba90 0:21e4e3afa5e6 147 break;
rba90 0:21e4e3afa5e6 148
rba90 0:21e4e3afa5e6 149 case CommandPacket::LENGTH_L:
rba90 0:21e4e3afa5e6 150 PacketBuffer.length |= (hexchar_to_uint8(ch) & 0x0f);
rba90 0:21e4e3afa5e6 151 if (PacketBuffer.length != 0) // if the length is not zero, then proceed to payload state
rba90 0:21e4e3afa5e6 152 {
rba90 0:21e4e3afa5e6 153 state = CommandPacket::PAYLOAD_H;
rba90 0:21e4e3afa5e6 154 }
rba90 0:21e4e3afa5e6 155 else // otherwise proceed to checksum state
rba90 0:21e4e3afa5e6 156 {
rba90 0:21e4e3afa5e6 157 state = CommandPacket::CHECKSUM_H;
rba90 0:21e4e3afa5e6 158 }
rba90 0:21e4e3afa5e6 159 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 160 printf("CommandPacket::LENGTH_L: 0x%x\r\n", PacketBuffer.length);
rba90 0:21e4e3afa5e6 161 #endif
rba90 0:21e4e3afa5e6 162 break;
rba90 0:21e4e3afa5e6 163
rba90 0:21e4e3afa5e6 164 case CommandPacket::PAYLOAD_H:
rba90 0:21e4e3afa5e6 165 PacketBuffer.payload[payload_counter] = hexchar_to_uint8(ch) << 4; // store higher 4 bits of payload
rba90 0:21e4e3afa5e6 166 state = CommandPacket::PAYLOAD_L;
rba90 0:21e4e3afa5e6 167 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 168 printf("CommandPacket::PAYLOAD_H: 0x%x\r\n", PacketBuffer.payload[payload_counter]);
rba90 0:21e4e3afa5e6 169 #endif
rba90 0:21e4e3afa5e6 170 break;
rba90 0:21e4e3afa5e6 171
rba90 0:21e4e3afa5e6 172 case CommandPacket::PAYLOAD_L:
rba90 0:21e4e3afa5e6 173 PacketBuffer.payload[payload_counter++] |= (hexchar_to_uint8(ch) & 0x0f); // store lower 4 bits of payload
rba90 0:21e4e3afa5e6 174 if (payload_counter < PacketBuffer.length) // append ch to payload until reach the length
rba90 0:21e4e3afa5e6 175 {
rba90 0:21e4e3afa5e6 176 state = CommandPacket::PAYLOAD_H;
rba90 0:21e4e3afa5e6 177 }
rba90 0:21e4e3afa5e6 178 else
rba90 0:21e4e3afa5e6 179 {
rba90 0:21e4e3afa5e6 180 state = CommandPacket::CHECKSUM_H;
rba90 0:21e4e3afa5e6 181 }
rba90 0:21e4e3afa5e6 182 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 183 printf("CommandPacket::PAYLOAD_L: 0x%x\r\n", PacketBuffer.payload[payload_counter - 1]);
rba90 0:21e4e3afa5e6 184 #endif
rba90 0:21e4e3afa5e6 185 break;
rba90 0:21e4e3afa5e6 186
rba90 0:21e4e3afa5e6 187 case CommandPacket::CHECKSUM_H:
rba90 0:21e4e3afa5e6 188 PacketBuffer.checksum = hexchar_to_uint8(ch) << 4;
rba90 0:21e4e3afa5e6 189 state = CommandPacket::CHECKSUM_L;
rba90 0:21e4e3afa5e6 190 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 191 printf("CommandPacket::CHECKSUM_H: 0x%x\r\n", PacketBuffer.checksum);
rba90 0:21e4e3afa5e6 192 #endif
rba90 0:21e4e3afa5e6 193 break;
rba90 0:21e4e3afa5e6 194
rba90 0:21e4e3afa5e6 195 case CommandPacket::CHECKSUM_L:
rba90 0:21e4e3afa5e6 196 PacketBuffer.checksum |= (hexchar_to_uint8(ch) & 0x0f);
rba90 0:21e4e3afa5e6 197
rba90 0:21e4e3afa5e6 198 // checksum can be turned off
rba90 0:21e4e3afa5e6 199 if (isChecksumEnabled)
rba90 0:21e4e3afa5e6 200 {
rba90 0:21e4e3afa5e6 201 if (PacketBuffer.verify()) // checksum match
rba90 0:21e4e3afa5e6 202 {
rba90 0:21e4e3afa5e6 203 state = CommandPacket::EFLAG;
rba90 0:21e4e3afa5e6 204 }
rba90 0:21e4e3afa5e6 205 else // checksum mismatch
rba90 0:21e4e3afa5e6 206 {
rba90 1:03208c983c8b 207 // clear response and response length
rba90 1:03208c983c8b 208 response_length = 0;
rba90 1:03208c983c8b 209 memset(response, 0x0, sizeof(response));
rba90 0:21e4e3afa5e6 210
rba90 0:21e4e3afa5e6 211 // prepare for checksum error response
rba90 0:21e4e3afa5e6 212 PacketBuffer.errno = CommandPacket::INVALID_CS_ERROR;
rba90 0:21e4e3afa5e6 213 assemble(response, response_length);
rba90 0:21e4e3afa5e6 214
rba90 0:21e4e3afa5e6 215 state = CommandPacket::NONE;
rba90 0:21e4e3afa5e6 216 }
rba90 0:21e4e3afa5e6 217 }
rba90 0:21e4e3afa5e6 218 else
rba90 0:21e4e3afa5e6 219 {
rba90 0:21e4e3afa5e6 220 state = CommandPacket::EFLAG;
rba90 0:21e4e3afa5e6 221 }
rba90 0:21e4e3afa5e6 222
rba90 0:21e4e3afa5e6 223
rba90 0:21e4e3afa5e6 224 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 225 printf("CommandPacket::CHECKSUM_L: 0x%x\r\n", PacketBuffer.checksum);
rba90 0:21e4e3afa5e6 226 #endif
rba90 0:21e4e3afa5e6 227 break;
rba90 0:21e4e3afa5e6 228
rba90 0:21e4e3afa5e6 229 case CommandPacket::EFLAG:
rba90 0:21e4e3afa5e6 230 if (ch == CommandPacket::CP_EFLAG)
rba90 0:21e4e3afa5e6 231 {
rba90 0:21e4e3afa5e6 232 PacketBuffer.eflag = ch;
rba90 0:21e4e3afa5e6 233
rba90 0:21e4e3afa5e6 234 // clear response and response length
rba90 0:21e4e3afa5e6 235 response_length = 0;
rba90 0:21e4e3afa5e6 236 memset(response, 0x0, sizeof(response));
rba90 0:21e4e3afa5e6 237
rba90 0:21e4e3afa5e6 238 // execute command
rba90 0:21e4e3afa5e6 239 int ret = execute(response, &response_length);
rba90 0:21e4e3afa5e6 240
rba90 0:21e4e3afa5e6 241 if (ret < 0) // command not registered
rba90 0:21e4e3afa5e6 242 {
rba90 0:21e4e3afa5e6 243 PacketBuffer.errno = CommandPacket::INVALID_CMD_ERROR;
rba90 0:21e4e3afa5e6 244 }
rba90 0:21e4e3afa5e6 245 else if (ret != 0) // error to execute
rba90 0:21e4e3afa5e6 246 {
rba90 0:21e4e3afa5e6 247 PacketBuffer.errno = CommandPacket::INVALID_EXEC_ERROR;
rba90 0:21e4e3afa5e6 248 }
rba90 4:4a92da3f19c0 249 else
rba90 4:4a92da3f19c0 250 {
rba90 4:4a92da3f19c0 251 PacketBuffer.errno = CommandPacket::NO_ERROR;
rba90 4:4a92da3f19c0 252 }
rba90 0:21e4e3afa5e6 253
rba90 0:21e4e3afa5e6 254 assemble(response, response_length);
rba90 0:21e4e3afa5e6 255 }
rba90 0:21e4e3afa5e6 256 state = CommandPacket::NONE;
rba90 0:21e4e3afa5e6 257 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 258 printf("CommandPacket::EFLAG: 0x%x\r\n", PacketBuffer.eflag);
rba90 0:21e4e3afa5e6 259 #endif
rba90 0:21e4e3afa5e6 260 break;
rba90 0:21e4e3afa5e6 261
rba90 0:21e4e3afa5e6 262 case CommandPacket::NONE:
rba90 1:03208c983c8b 263 // clear response and response length
rba90 1:03208c983c8b 264 response_length = 0;
rba90 1:03208c983c8b 265 memset(response, 0x0, sizeof(response));
rba90 0:21e4e3afa5e6 266
rba90 1:03208c983c8b 267 // Execute error generator
rba90 0:21e4e3afa5e6 268 PacketBuffer.errno = CommandPacket::INVALID_SFLAG_ERROR;
rba90 0:21e4e3afa5e6 269 assemble(response, response_length);
rba90 0:21e4e3afa5e6 270
rba90 0:21e4e3afa5e6 271 #ifdef DEBUG_MESSAGE
rba90 0:21e4e3afa5e6 272 printf("CommandPacket::NONE\r\n");
rba90 0:21e4e3afa5e6 273 #endif
rba90 0:21e4e3afa5e6 274 break;
rba90 0:21e4e3afa5e6 275
rba90 0:21e4e3afa5e6 276 default:
rba90 0:21e4e3afa5e6 277 break;
rba90 0:21e4e3afa5e6 278 }
rba90 0:21e4e3afa5e6 279 }
rba90 0:21e4e3afa5e6 280 }
rba90 0:21e4e3afa5e6 281
rba90 0:21e4e3afa5e6 282 void SerialInterfaceProtocol::disableChecksum()
rba90 0:21e4e3afa5e6 283 {
rba90 0:21e4e3afa5e6 284 isChecksumEnabled = false;
rba90 0:21e4e3afa5e6 285 }
rba90 0:21e4e3afa5e6 286
rba90 0:21e4e3afa5e6 287 void SerialInterfaceProtocol::enableChecksum()
rba90 0:21e4e3afa5e6 288 {
rba90 0:21e4e3afa5e6 289 isChecksumEnabled = true;
rba90 0:21e4e3afa5e6 290 }