123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #pragma once
- #include <stdint.h>
- #include "mmu2_crc.h"
- namespace modules {
- namespace protocol {
- enum class RequestMsgCodes : uint8_t {
- unknown = 0,
- Query = 'Q',
- Tool = 'T',
- Load = 'L',
- Mode = 'M',
- Unload = 'U',
- Reset = 'X',
- Finda = 'P',
- Version = 'S',
- Button = 'B',
- Eject = 'E',
- Write = 'W',
- Cut = 'K',
- FilamentType = 'F',
- FilamentSensor = 'f',
- Home = 'H',
- Read = 'R'
- };
- enum class ResponseMsgParamCodes : uint8_t {
- unknown = 0,
- Processing = 'P',
- Error = 'E',
- Finished = 'F',
- Accepted = 'A',
- Rejected = 'R',
- Button = 'B'
- };
- struct RequestMsg {
- RequestMsgCodes code;
- uint8_t value;
- uint16_t value2;
-
-
-
- uint8_t crc8;
- constexpr uint8_t ComputeCRC8() const {
- uint8_t crc = 0;
- crc = modules::crc::CRC8::CCITT_updateCX(0, (uint8_t)code);
- crc = modules::crc::CRC8::CCITT_updateCX(crc, value);
- crc = modules::crc::CRC8::CCITT_updateW(crc, value2);
- return crc;
- }
-
-
- inline constexpr RequestMsg(RequestMsgCodes code, uint8_t value)
- : code(code)
- , value(value)
- , value2(0)
- , crc8(ComputeCRC8()) {
- }
-
-
-
-
- inline constexpr RequestMsg(RequestMsgCodes code, uint8_t address, uint16_t value)
- : code(code)
- , value(address)
- , value2(value)
- , crc8(ComputeCRC8()) {
- }
- constexpr uint8_t CRC() const { return crc8; }
- };
- struct ResponseMsg {
- RequestMsg request;
- ResponseMsgParamCodes paramCode;
- uint16_t paramValue;
- constexpr uint8_t ComputeCRC8() const {
- uint8_t crc = request.ComputeCRC8();
- crc = modules::crc::CRC8::CCITT_updateCX(crc, (uint8_t)paramCode);
- crc = modules::crc::CRC8::CCITT_updateW(crc, paramValue);
- return crc;
- }
-
-
-
- inline constexpr ResponseMsg(RequestMsg request, ResponseMsgParamCodes paramCode, uint16_t paramValue)
- : request(request)
- , paramCode(paramCode)
- , paramValue(paramValue) {
- this->request.crc8 = ComputeCRC8();
- }
- constexpr uint8_t CRC() const { return request.crc8; }
- };
- struct ResponseCommandStatus {
- ResponseMsgParamCodes code;
- uint16_t value;
- inline constexpr ResponseCommandStatus(ResponseMsgParamCodes code, uint16_t value)
- : code(code)
- , value(value) {}
- };
- enum class DecodeStatus : uint_fast8_t {
- MessageCompleted,
- NeedMoreData,
- Error,
- };
- class Protocol {
- public:
- inline Protocol()
- : rqState(RequestStates::Code)
- , requestMsg(RequestMsgCodes::unknown, 0)
- , rspState(ResponseStates::RequestCode)
- , responseMsg(RequestMsg(RequestMsgCodes::unknown, 0), ResponseMsgParamCodes::unknown, 0) {
- }
-
-
- DecodeStatus DecodeRequest(uint8_t c);
-
-
- DecodeStatus DecodeResponse(uint8_t c);
-
-
-
- static uint8_t EncodeRequest(const RequestMsg &msg, uint8_t *txbuff);
-
-
-
- static uint8_t EncodeWriteRequest(uint8_t address, uint16_t value, uint8_t *txbuff);
-
-
- static constexpr uint8_t MaxRequestSize() { return 13; }
-
-
- static constexpr uint8_t MaxResponseSize() { return 14; }
-
-
-
-
-
- static uint8_t EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff);
-
-
-
-
-
- static uint8_t EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff);
-
-
-
-
-
-
- static uint8_t EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff);
-
-
-
-
-
-
- static uint8_t EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff);
-
- inline const RequestMsg GetRequestMsg() const { return requestMsg; }
-
- inline const ResponseMsg GetResponseMsg() const { return responseMsg; }
-
- void ResetRequestDecoder() {
- rqState = RequestStates::Code;
- }
-
- void ResetResponseDecoder() {
- rspState = ResponseStates::RequestCode;
- }
- #ifndef UNITTEST
- private:
- #endif
- enum class RequestStates : uint8_t {
- Code,
- Value,
- Address,
- WriteValue,
- CRC,
- Error
- };
- RequestStates rqState;
- RequestMsg requestMsg;
- enum class ResponseStates : uint8_t {
- RequestCode,
- RequestValue,
- ParamCode,
- ParamValue,
- CRC,
- Error
- };
- ResponseStates rspState;
- ResponseMsg responseMsg;
- static constexpr bool IsNewLine(uint8_t c) {
- return c == '\n' || c == '\r';
- }
- static constexpr bool IsDigit(uint8_t c) {
- return c >= '0' && c <= '9';
- }
- static constexpr bool IsCRCSeparator(uint8_t c) {
- return c == '*';
- }
- static constexpr bool IsHexDigit(uint8_t c) {
- return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
- }
- static constexpr uint8_t Char2Nibble(uint8_t c) {
- switch (c) {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- return c - '0';
- case 'a':
- case 'b':
- case 'c':
- case 'd':
- case 'e':
- case 'f':
- return c - 'a' + 10;
- default:
- return 0;
- }
- }
- static constexpr uint8_t Nibble2Char(uint8_t n) {
- switch (n) {
- case 0:
- case 1:
- case 2:
- case 3:
- case 4:
- case 5:
- case 6:
- case 7:
- case 8:
- case 9:
- return n + '0';
- case 0xa:
- case 0xb:
- case 0xc:
- case 0xd:
- case 0xe:
- case 0xf:
- return n - 10 + 'a';
- default:
- return 0;
- }
- }
-
- static uint8_t UInt8ToHex(uint8_t value, uint8_t *dst);
-
- static uint8_t UInt16ToHex(uint16_t value, uint8_t *dst);
- static uint8_t BeginEncodeRequest(const RequestMsg &msg, uint8_t *dst);
- static uint8_t AppendCRC(uint8_t crc, uint8_t *dst);
- };
- }
- }
- namespace mp = modules::protocol;
|