mmu2_protocol.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /// @file protocol.h
  2. #pragma once
  3. #include <stdint.h>
  4. namespace modules {
  5. /// @brief The MMU communication protocol implementation and related stuff.
  6. ///
  7. /// See description of the new protocol in the MMU 2021 doc
  8. /// @@TODO possibly add some checksum to verify the correctness of messages
  9. namespace protocol {
  10. /// Definition of request message codes
  11. enum class RequestMsgCodes : uint8_t {
  12. unknown = 0,
  13. Query = 'Q',
  14. Tool = 'T',
  15. Load = 'L',
  16. Mode = 'M',
  17. Unload = 'U',
  18. Reset = 'X',
  19. Finda = 'P',
  20. Version = 'S',
  21. Button = 'B',
  22. Eject = 'E',
  23. Wait = 'W',
  24. Cut = 'K',
  25. FilamentType = 'F',
  26. FilamentSensor = 'f',
  27. Home = 'H'
  28. };
  29. /// Definition of response message parameter codes
  30. enum class ResponseMsgParamCodes : uint8_t {
  31. unknown = 0,
  32. Processing = 'P',
  33. Error = 'E',
  34. Finished = 'F',
  35. Accepted = 'A',
  36. Rejected = 'R',
  37. Button = 'B' // the MMU registered a button press and is sending it to the printer for processing
  38. };
  39. /// A request message - requests are being sent by the printer into the MMU.
  40. struct RequestMsg {
  41. RequestMsgCodes code; ///< code of the request message
  42. uint8_t value; ///< value of the request message
  43. /// @param code of the request message
  44. /// @param value of the request message
  45. inline RequestMsg(RequestMsgCodes code, uint8_t value)
  46. : code(code)
  47. , value(value) {}
  48. };
  49. /// A response message - responses are being sent from the MMU into the printer as a response to a request message.
  50. struct ResponseMsg {
  51. RequestMsg request; ///< response is always preceeded by the request message
  52. ResponseMsgParamCodes paramCode; ///< code of the parameter
  53. uint16_t paramValue; ///< value of the parameter
  54. /// @param request the source request message this response is a reply to
  55. /// @param paramCode code of the parameter
  56. /// @param paramValue value of the parameter
  57. inline ResponseMsg(RequestMsg request, ResponseMsgParamCodes paramCode, uint16_t paramValue)
  58. : request(request)
  59. , paramCode(paramCode)
  60. , paramValue(paramValue) {}
  61. };
  62. /// Message decoding return values
  63. enum class DecodeStatus : uint_fast8_t {
  64. MessageCompleted, ///< message completed and successfully lexed
  65. NeedMoreData, ///< message incomplete yet, waiting for another byte to come
  66. Error, ///< input character broke message decoding
  67. };
  68. /// Protocol class is responsible for creating/decoding messages in Rx/Tx buffer
  69. ///
  70. /// Beware - in the decoding more, it is meant to be a statefull instance which works through public methods
  71. /// processing one input byte per call.
  72. class Protocol {
  73. public:
  74. inline Protocol()
  75. : rqState(RequestStates::Code)
  76. , requestMsg(RequestMsgCodes::unknown, 0)
  77. , rspState(ResponseStates::RequestCode)
  78. , responseMsg(RequestMsg(RequestMsgCodes::unknown, 0), ResponseMsgParamCodes::unknown, 0) {
  79. }
  80. /// Takes the input byte c and steps one step through the state machine
  81. /// @returns state of the message being decoded
  82. DecodeStatus DecodeRequest(uint8_t c);
  83. /// Decodes response message in rxbuff
  84. /// @returns decoded response message structure
  85. DecodeStatus DecodeResponse(uint8_t c);
  86. /// Encodes request message msg into txbuff memory
  87. /// It is expected the txbuff is large enough to fit the message
  88. /// @returns number of bytes written into txbuff
  89. static uint8_t EncodeRequest(const RequestMsg &msg, uint8_t *txbuff);
  90. /// @returns the maximum byte length necessary to encode a request message
  91. /// Beneficial in case of pre-allocating a buffer for enconding a RequestMsg.
  92. static constexpr uint8_t MaxRequestSize() { return 3; }
  93. /// Encode generic response Command Accepted or Rejected
  94. /// @param msg source request message for this response
  95. /// @param ar code of response parameter
  96. /// @param txbuff where to format the message
  97. /// @returns number of bytes written into txbuff
  98. static uint8_t EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff);
  99. /// Encode response to Read FINDA query
  100. /// @param msg source request message for this response
  101. /// @param findaValue 1/0 (on/off) status of FINDA
  102. /// @param txbuff where to format the message
  103. /// @returns number of bytes written into txbuff
  104. static uint8_t EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff);
  105. /// Encode response to Version query
  106. /// @param msg source request message for this response
  107. /// @param value version number (0-255)
  108. /// @param txbuff where to format the message
  109. /// @returns number of bytes written into txbuff
  110. static uint8_t EncodeResponseVersion(const RequestMsg &msg, uint8_t value, uint8_t *txbuff);
  111. /// Encode response to Query operation status
  112. /// @param msg source request message for this response
  113. /// @param code status of operation (Processing, Error, Finished)
  114. /// @param value related to status of operation(e.g. error code or progress)
  115. /// @param txbuff where to format the message
  116. /// @returns number of bytes written into txbuff
  117. static uint8_t EncodeResponseQueryOperation(const RequestMsg &msg, ResponseMsgParamCodes code, uint16_t value, uint8_t *txbuff);
  118. /// @returns the most recently lexed request message
  119. inline const RequestMsg GetRequestMsg() const { return requestMsg; }
  120. /// @returns the most recently lexed response message
  121. inline const ResponseMsg GetResponseMsg() const { return responseMsg; }
  122. /// resets the internal request decoding state (typically after an error)
  123. void ResetRequestDecoder() {
  124. rqState = RequestStates::Code;
  125. }
  126. /// resets the internal response decoding state (typically after an error)
  127. void ResetResponseDecoder() {
  128. rspState = ResponseStates::RequestCode;
  129. }
  130. private:
  131. enum class RequestStates : uint8_t {
  132. Code, ///< starting state - expects message code
  133. Value, ///< expecting code value
  134. Error ///< automaton in error state
  135. };
  136. RequestStates rqState;
  137. RequestMsg requestMsg;
  138. enum class ResponseStates : uint8_t {
  139. RequestCode, ///< starting state - expects message code
  140. RequestValue, ///< expecting code value
  141. ParamCode, ///< expecting param code
  142. ParamValue, ///< expecting param value
  143. Error ///< automaton in error state
  144. };
  145. ResponseStates rspState;
  146. ResponseMsg responseMsg;
  147. static bool IsNewLine(uint8_t c) {
  148. return c == '\n' || c == '\r';
  149. }
  150. static bool IsDigit(uint8_t c) {
  151. return c >= '0' && c <= '9';
  152. }
  153. };
  154. } // namespace protocol
  155. } // namespace modules
  156. namespace mp = modules::protocol;