mmu2_protocol.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /// @file
  2. #include "mmu2_protocol.h"
  3. // protocol definition
  4. // command: Q0
  5. // meaning: query operation status
  6. // Query/command: query
  7. // Expected reply from the MMU:
  8. // any of the running operation statuses: OID: [T|L|U|E|C|W|K][0-4]
  9. // <OID> P[0-9] : command being processed i.e. operation running, may contain a state number
  10. // <OID> E[0-9][0-9] : error 1-9 while doing a tool change
  11. // <OID> F[0-9] : operation finished - will be repeated to "Q" messages until a new command is issued
  12. namespace modules {
  13. namespace protocol {
  14. // decoding automaton
  15. // states: input -> transition into state
  16. // Code QTLMUXPSBEWK -> msgcode
  17. // \n ->start
  18. // * ->error
  19. // error \n ->start
  20. // * ->error
  21. // msgcode 0-9 ->msgvalue
  22. // * ->error
  23. // msgvalue 0-9 ->msgvalue
  24. // \n ->start successfully accepted command
  25. DecodeStatus Protocol::DecodeRequest(uint8_t c) {
  26. switch (rqState) {
  27. case RequestStates::Code:
  28. switch (c) {
  29. case 'Q':
  30. case 'T':
  31. case 'L':
  32. case 'M':
  33. case 'U':
  34. case 'X':
  35. case 'P':
  36. case 'S':
  37. case 'B':
  38. case 'E':
  39. case 'W': // write is gonna be a special one
  40. case 'K':
  41. case 'F':
  42. case 'f':
  43. case 'H':
  44. case 'R':
  45. requestMsg.code = (RequestMsgCodes)c;
  46. requestMsg.value = 0;
  47. requestMsg.value2 = 0;
  48. requestMsg.crc8 = 0;
  49. rqState = (c == 'W') ? RequestStates::Address : RequestStates::Value; // prepare special automaton path for Write commands
  50. return DecodeStatus::NeedMoreData;
  51. default:
  52. requestMsg.code = RequestMsgCodes::unknown;
  53. rqState = RequestStates::Error;
  54. return DecodeStatus::Error;
  55. }
  56. case RequestStates::Value:
  57. if (IsHexDigit(c)) {
  58. requestMsg.value <<= 4U;
  59. requestMsg.value |= Char2Nibble(c);
  60. return DecodeStatus::NeedMoreData;
  61. } else if (IsCRCSeparator(c)) {
  62. rqState = RequestStates::CRC;
  63. return DecodeStatus::NeedMoreData;
  64. } else {
  65. requestMsg.code = RequestMsgCodes::unknown;
  66. rqState = RequestStates::Error;
  67. return DecodeStatus::Error;
  68. }
  69. case RequestStates::Address:
  70. if (IsHexDigit(c)) {
  71. requestMsg.value <<= 4U;
  72. requestMsg.value |= Char2Nibble(c);
  73. return DecodeStatus::NeedMoreData;
  74. } else if (c == ' ') { // end of address, value coming
  75. rqState = RequestStates::WriteValue;
  76. return DecodeStatus::NeedMoreData;
  77. } else {
  78. requestMsg.code = RequestMsgCodes::unknown;
  79. rqState = RequestStates::Error;
  80. return DecodeStatus::Error;
  81. }
  82. case RequestStates::WriteValue:
  83. if (IsHexDigit(c)) {
  84. requestMsg.value2 <<= 4U;
  85. requestMsg.value2 |= Char2Nibble(c);
  86. return DecodeStatus::NeedMoreData;
  87. } else if (IsCRCSeparator(c)) {
  88. rqState = RequestStates::CRC;
  89. return DecodeStatus::NeedMoreData;
  90. } else {
  91. requestMsg.code = RequestMsgCodes::unknown;
  92. rqState = RequestStates::Error;
  93. return DecodeStatus::Error;
  94. }
  95. case RequestStates::CRC:
  96. if (IsHexDigit(c)) {
  97. requestMsg.crc8 <<= 4U;
  98. requestMsg.crc8 |= Char2Nibble(c);
  99. return DecodeStatus::NeedMoreData;
  100. } else if (IsNewLine(c)) {
  101. // check CRC at this spot
  102. if (requestMsg.crc8 != requestMsg.ComputeCRC8()) {
  103. // CRC mismatch
  104. requestMsg.code = RequestMsgCodes::unknown;
  105. rqState = RequestStates::Error;
  106. return DecodeStatus::Error;
  107. } else {
  108. rqState = RequestStates::Code;
  109. return DecodeStatus::MessageCompleted;
  110. }
  111. } else {
  112. requestMsg.code = RequestMsgCodes::unknown;
  113. rqState = RequestStates::Error;
  114. return DecodeStatus::Error;
  115. }
  116. default: //case error:
  117. if (IsNewLine(c)) {
  118. rqState = RequestStates::Code;
  119. return DecodeStatus::MessageCompleted;
  120. } else {
  121. requestMsg.code = RequestMsgCodes::unknown;
  122. rqState = RequestStates::Error;
  123. return DecodeStatus::Error;
  124. }
  125. }
  126. }
  127. uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) {
  128. txbuff[0] = (uint8_t)msg.code;
  129. uint8_t i = 1 + UInt8ToHex(msg.value, txbuff + 1);
  130. i += AppendCRC(msg.CRC(), txbuff + i);
  131. txbuff[i] = '\n';
  132. ++i;
  133. return i;
  134. static_assert(7 <= MaxRequestSize(), "Request message length exceeded the maximum size, increase the magic constant in MaxRequestSize()");
  135. }
  136. uint8_t Protocol::EncodeWriteRequest(uint8_t address, uint16_t value, uint8_t *txbuff) {
  137. const RequestMsg msg(RequestMsgCodes::Write, address, value);
  138. uint8_t i = BeginEncodeRequest(msg, txbuff);
  139. // dump the value
  140. i += UInt16ToHex(value, txbuff + i);
  141. i += AppendCRC(msg.CRC(), txbuff + i);
  142. txbuff[i] = '\n';
  143. ++i;
  144. return i;
  145. }
  146. DecodeStatus Protocol::DecodeResponse(uint8_t c) {
  147. switch (rspState) {
  148. case ResponseStates::RequestCode:
  149. switch (c) {
  150. case 'Q':
  151. case 'T':
  152. case 'L':
  153. case 'M':
  154. case 'U':
  155. case 'X':
  156. case 'P':
  157. case 'S':
  158. case 'B':
  159. case 'E':
  160. case 'W':
  161. case 'K':
  162. case 'F':
  163. case 'f':
  164. case 'H':
  165. case 'R':
  166. responseMsg.request.code = (RequestMsgCodes)c;
  167. responseMsg.request.value = 0;
  168. responseMsg.request.value2 = 0;
  169. responseMsg.request.crc8 = 0;
  170. rspState = ResponseStates::RequestValue;
  171. return DecodeStatus::NeedMoreData;
  172. case 0x0a:
  173. case 0x0d:
  174. // skip leading whitespace if any (makes integration with other SW easier/tolerant)
  175. return DecodeStatus::NeedMoreData;
  176. default:
  177. rspState = ResponseStates::Error;
  178. return DecodeStatus::Error;
  179. }
  180. case ResponseStates::RequestValue:
  181. if (IsHexDigit(c)) {
  182. responseMsg.request.value <<= 4U;
  183. responseMsg.request.value += Char2Nibble(c);
  184. return DecodeStatus::NeedMoreData;
  185. } else if (c == ' ') {
  186. rspState = ResponseStates::ParamCode;
  187. return DecodeStatus::NeedMoreData;
  188. } else {
  189. rspState = ResponseStates::Error;
  190. return DecodeStatus::Error;
  191. }
  192. case ResponseStates::ParamCode:
  193. switch (c) {
  194. case 'P':
  195. case 'E':
  196. case 'F':
  197. case 'A':
  198. case 'R':
  199. case 'B':
  200. rspState = ResponseStates::ParamValue;
  201. responseMsg.paramCode = (ResponseMsgParamCodes)c;
  202. responseMsg.paramValue = 0;
  203. return DecodeStatus::NeedMoreData;
  204. default:
  205. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  206. rspState = ResponseStates::Error;
  207. return DecodeStatus::Error;
  208. }
  209. case ResponseStates::ParamValue:
  210. if (IsHexDigit(c)) {
  211. responseMsg.paramValue <<= 4U;
  212. responseMsg.paramValue += Char2Nibble(c);
  213. return DecodeStatus::NeedMoreData;
  214. } else if (IsCRCSeparator(c)) {
  215. rspState = ResponseStates::CRC;
  216. return DecodeStatus::NeedMoreData;
  217. } else {
  218. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  219. rspState = ResponseStates::Error;
  220. return DecodeStatus::Error;
  221. }
  222. case ResponseStates::CRC:
  223. if (IsHexDigit(c)) {
  224. responseMsg.request.crc8 <<= 4U;
  225. responseMsg.request.crc8 += Char2Nibble(c);
  226. return DecodeStatus::NeedMoreData;
  227. } else if (IsNewLine(c)) {
  228. // check CRC at this spot
  229. if (responseMsg.request.crc8 != responseMsg.ComputeCRC8()) {
  230. // CRC mismatch
  231. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  232. rspState = ResponseStates::Error;
  233. return DecodeStatus::Error;
  234. } else {
  235. rspState = ResponseStates::RequestCode;
  236. return DecodeStatus::MessageCompleted;
  237. }
  238. } else {
  239. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  240. rspState = ResponseStates::Error;
  241. return DecodeStatus::Error;
  242. }
  243. default: //case error:
  244. if (IsNewLine(c)) {
  245. rspState = ResponseStates::RequestCode;
  246. return DecodeStatus::MessageCompleted;
  247. } else {
  248. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  249. return DecodeStatus::Error;
  250. }
  251. }
  252. }
  253. uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) {
  254. // BEWARE:
  255. // ResponseMsg rsp(RequestMsg(msg.code, msg.value), ar, 0);
  256. // ... is NOT the same as:
  257. // ResponseMsg rsp(msg, ar, 0);
  258. // ... because of the usually unused parameter value2 (which only comes non-zero in write requests).
  259. // It took me a few hours to find out why the CRC from the MMU never matched all the other sides (unit tests and the MK3S)
  260. // It is because this was the only place where the original request kept its value2 non-zero.
  261. // In the response, we must make sure value2 is actually zero unless being sent along with it (which is not right now)
  262. const ResponseMsg rsp(RequestMsg(msg.code, msg.value), ar, 0); // this needs some cleanup @@TODO - check assembly how bad is it
  263. uint8_t i = BeginEncodeRequest(rsp.request, txbuff);
  264. txbuff[i] = (uint8_t)ar;
  265. ++i;
  266. i += AppendCRC(rsp.CRC(), txbuff + i);
  267. txbuff[i] = '\n';
  268. ++i;
  269. return i;
  270. }
  271. uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) {
  272. return EncodeResponseRead(msg, true, findaValue, txbuff);
  273. }
  274. uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff) {
  275. const ResponseMsg rsp(msg, rcs.code, rcs.value);
  276. uint8_t i = BeginEncodeRequest(msg, txbuff);
  277. txbuff[i] = (uint8_t)rsp.paramCode;
  278. ++i;
  279. i += UInt16ToHex(rsp.paramValue, txbuff + i);
  280. i += AppendCRC(rsp.CRC(), txbuff + i);
  281. txbuff[i] = '\n';
  282. return i + 1;
  283. }
  284. uint8_t Protocol::EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff) {
  285. const ResponseMsg rsp(msg,
  286. accepted ? ResponseMsgParamCodes::Accepted : ResponseMsgParamCodes::Rejected,
  287. accepted ? value2 : 0 // be careful about this value for CRC computation - rejected status doesn't have any meaningful value which could be reconstructed from the textual form of the message
  288. );
  289. uint8_t i = BeginEncodeRequest(msg, txbuff);
  290. txbuff[i] = (uint8_t)rsp.paramCode;
  291. ++i;
  292. if (accepted) {
  293. // dump the value
  294. i += UInt16ToHex(value2, txbuff + i);
  295. }
  296. i += AppendCRC(rsp.CRC(), txbuff + i);
  297. txbuff[i] = '\n';
  298. return i + 1;
  299. }
  300. uint8_t Protocol::UInt8ToHex(uint8_t value, uint8_t *dst) {
  301. if (value == 0) {
  302. *dst = '0';
  303. return 1;
  304. }
  305. uint8_t v = value >> 4U;
  306. uint8_t charsOut = 1;
  307. if (v != 0) { // skip the first '0' if any
  308. *dst = Nibble2Char(v);
  309. ++dst;
  310. charsOut = 2;
  311. }
  312. v = value & 0xfU;
  313. *dst = Nibble2Char(v);
  314. return charsOut;
  315. }
  316. uint8_t Protocol::UInt16ToHex(uint16_t value, uint8_t *dst) {
  317. constexpr uint16_t topNibbleMask = 0xf000;
  318. if (value == 0) {
  319. *dst = '0';
  320. return 1;
  321. }
  322. // skip initial zeros
  323. uint8_t charsOut = 4;
  324. while ((value & topNibbleMask) == 0) {
  325. value <<= 4U;
  326. --charsOut;
  327. }
  328. for (uint8_t i = 0; i < charsOut; ++i) {
  329. uint8_t n = (value & topNibbleMask) >> (8U + 4U);
  330. value <<= 4U;
  331. *dst = Nibble2Char(n);
  332. ++dst;
  333. }
  334. return charsOut;
  335. }
  336. uint8_t Protocol::BeginEncodeRequest(const RequestMsg &msg, uint8_t *dst) {
  337. dst[0] = (uint8_t)msg.code;
  338. uint8_t i = 1 + UInt8ToHex(msg.value, dst + 1);
  339. dst[i] = ' ';
  340. return i + 1;
  341. }
  342. uint8_t Protocol::AppendCRC(uint8_t crc, uint8_t *dst) {
  343. dst[0] = '*'; // reprap-style separator of CRC
  344. return 1 + UInt8ToHex(crc, dst + 1);
  345. }
  346. } // namespace protocol
  347. } // namespace modules