simple serial protocol

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 0:21e4e3afa5e6 1 // SerialInterfaceProtocol (SIP) is a protocol which help to exchange
rba90 0:21e4e3afa5e6 2 // information between host and slave devices
rba90 0:21e4e3afa5e6 3 #ifndef SERIALINTERFACEPROTOCOL_H_
rba90 0:21e4e3afa5e6 4 #define SERIALINTERFACEPROTOCOL_H_
rba90 0:21e4e3afa5e6 5
rba90 0:21e4e3afa5e6 6 #include "mbed.h"
rba90 0:21e4e3afa5e6 7 #include "stdint.h"
rba90 6:d70709307c5a 8 #include "RingBuffer.h"
rba90 11:390476907bfc 9 #include "CommandPacket2.h"
rba90 0:21e4e3afa5e6 10
rba90 0:21e4e3afa5e6 11 #define SIP_CMD_VECTOR_TABLE_SZ 256
rba90 0:21e4e3afa5e6 12 #define SIP_MAX_RESP_LEN 256
rba90 0:21e4e3afa5e6 13 #define SIP_MAX_PAYLOAD_LEN 256
rba90 0:21e4e3afa5e6 14
rba90 0:21e4e3afa5e6 15 typedef int (*callback_func)(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length);
rba90 0:21e4e3afa5e6 16 typedef CircularBuffer<uint8_t> SerialBuffer_t;
rba90 0:21e4e3afa5e6 17
rba90 11:390476907bfc 18 uint8_t hexchar_to_uint8(uint8_t ch);
rba90 11:390476907bfc 19
rba90 0:21e4e3afa5e6 20 class SerialInterfaceProtocol
rba90 0:21e4e3afa5e6 21 {
rba90 0:21e4e3afa5e6 22 public:
rba90 0:21e4e3afa5e6 23 // namespace
rba90 11:390476907bfc 24 typedef enum
rba90 11:390476907bfc 25 {
rba90 11:390476907bfc 26 NONE = 0,
rba90 11:390476907bfc 27 SFLAG,
rba90 11:390476907bfc 28 COMMAND_H,
rba90 11:390476907bfc 29 COMMAND_L,
rba90 11:390476907bfc 30 LENGTH_H,
rba90 11:390476907bfc 31 LENGTH_L,
rba90 11:390476907bfc 32 PAYLOAD_H,
rba90 11:390476907bfc 33 PAYLOAD_L,
rba90 11:390476907bfc 34 CHECKSUM_H,
rba90 11:390476907bfc 35 CHECKSUM_L,
rba90 11:390476907bfc 36 EFLAG
rba90 11:390476907bfc 37 } State_t;
rba90 11:390476907bfc 38
rba90 11:390476907bfc 39 typedef enum
rba90 11:390476907bfc 40 {
rba90 11:390476907bfc 41 NO_ERROR = 0,
rba90 11:390476907bfc 42 INVALID_SFLAG_ERROR,
rba90 11:390476907bfc 43 INVALID_EFLAG_ERROR,
rba90 11:390476907bfc 44 INVALID_CMD_ERROR,
rba90 11:390476907bfc 45 INVALID_CS_ERROR,
rba90 11:390476907bfc 46 INVALID_EXEC_ERROR
rba90 11:390476907bfc 47 } Error_t;
rba90 0:21e4e3afa5e6 48
rba90 0:21e4e3afa5e6 49 protected:
rba90 0:21e4e3afa5e6 50 // internal variable
rba90 0:21e4e3afa5e6 51 callback_func CommandVectorTable[SIP_CMD_VECTOR_TABLE_SZ];
rba90 0:21e4e3afa5e6 52
rba90 0:21e4e3afa5e6 53 // buffered interface
rba90 0:21e4e3afa5e6 54 SerialBuffer_t *SerialInputBuffer;
rba90 0:21e4e3afa5e6 55 SerialBuffer_t *SerialOutputBuffer;
rba90 11:390476907bfc 56
rba90 11:390476907bfc 57 // internal packet buffer (the SIP could issue one command at a time)
rba90 11:390476907bfc 58 CommandPacket2 PacketBuffer;
rba90 0:21e4e3afa5e6 59
rba90 0:21e4e3afa5e6 60 // state machine
rba90 11:390476907bfc 61 State_t state;
rba90 11:390476907bfc 62
rba90 11:390476907bfc 63 // error code
rba90 11:390476907bfc 64 Error_t errno;
rba90 0:21e4e3afa5e6 65
rba90 0:21e4e3afa5e6 66 // internal state
rba90 0:21e4e3afa5e6 67 bool isChecksumEnabled;
rba90 0:21e4e3afa5e6 68
rba90 0:21e4e3afa5e6 69 protected:
rba90 0:21e4e3afa5e6 70 // internal methods
rba90 0:21e4e3afa5e6 71 int execute(uint8_t *response, uint8_t *response_length);
rba90 0:21e4e3afa5e6 72
rba90 0:21e4e3afa5e6 73 int assemble(uint8_t *response, uint8_t response_length);
rba90 0:21e4e3afa5e6 74
rba90 0:21e4e3afa5e6 75
rba90 0:21e4e3afa5e6 76 public:
rba90 0:21e4e3afa5e6 77 // public methods
rba90 7:100865963801 78 int respond(uint8_t command, uint8_t *response, uint8_t response_length);
rba90 0:21e4e3afa5e6 79
rba90 0:21e4e3afa5e6 80 // constructor
rba90 0:21e4e3afa5e6 81 SerialInterfaceProtocol(SerialBuffer_t *in, SerialBuffer_t *out);
rba90 0:21e4e3afa5e6 82 ~SerialInterfaceProtocol();
rba90 0:21e4e3afa5e6 83
rba90 0:21e4e3afa5e6 84 // member function
rba90 0:21e4e3afa5e6 85 void poll();
rba90 0:21e4e3afa5e6 86 void registerCommand(uint8_t command, callback_func f);
rba90 0:21e4e3afa5e6 87 void deRegisterCommand(uint8_t command);
rba90 0:21e4e3afa5e6 88
rba90 0:21e4e3afa5e6 89 void disableChecksum();
rba90 0:21e4e3afa5e6 90 void enableChecksum();
rba90 0:21e4e3afa5e6 91
rba90 0:21e4e3afa5e6 92 };
rba90 0:21e4e3afa5e6 93
rba90 0:21e4e3afa5e6 94
rba90 0:21e4e3afa5e6 95
rba90 0:21e4e3afa5e6 96
rba90 0:21e4e3afa5e6 97 #endif