mmu2_protocol.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }
  112. default: //case error:
  113. if (IsNewLine(c)) {
  114. rqState = RequestStates::Code;
  115. return DecodeStatus::MessageCompleted;
  116. } else {
  117. requestMsg.code = RequestMsgCodes::unknown;
  118. rqState = RequestStates::Error;
  119. return DecodeStatus::Error;
  120. }
  121. }
  122. }
  123. uint8_t Protocol::EncodeRequest(const RequestMsg &msg, uint8_t *txbuff) {
  124. txbuff[0] = (uint8_t)msg.code;
  125. uint8_t i = 1 + UInt8ToHex(msg.value, txbuff + 1);
  126. i += AppendCRC(msg.CRC(), txbuff + i);
  127. txbuff[i] = '\n';
  128. ++i;
  129. return i;
  130. static_assert(7 <= MaxRequestSize(), "Request message length exceeded the maximum size, increase the magic constant in MaxRequestSize()");
  131. }
  132. uint8_t Protocol::EncodeWriteRequest(uint8_t address, uint16_t value, uint8_t *txbuff) {
  133. const RequestMsg msg(RequestMsgCodes::Write, address, value);
  134. uint8_t i = BeginEncodeRequest(msg, txbuff);
  135. // dump the value
  136. i += UInt16ToHex(value, txbuff + i);
  137. i += AppendCRC(msg.CRC(), txbuff + i);
  138. txbuff[i] = '\n';
  139. ++i;
  140. return i;
  141. }
  142. DecodeStatus Protocol::DecodeResponse(uint8_t c) {
  143. switch (rspState) {
  144. case ResponseStates::RequestCode:
  145. switch (c) {
  146. case 'Q':
  147. case 'T':
  148. case 'L':
  149. case 'M':
  150. case 'U':
  151. case 'X':
  152. case 'P':
  153. case 'S':
  154. case 'B':
  155. case 'E':
  156. case 'W':
  157. case 'K':
  158. case 'F':
  159. case 'f':
  160. case 'H':
  161. case 'R':
  162. responseMsg.request.code = (RequestMsgCodes)c;
  163. responseMsg.request.value = 0;
  164. responseMsg.request.value2 = 0;
  165. responseMsg.request.crc8 = 0;
  166. rspState = ResponseStates::RequestValue;
  167. return DecodeStatus::NeedMoreData;
  168. case 0x0a:
  169. case 0x0d:
  170. // skip leading whitespace if any (makes integration with other SW easier/tolerant)
  171. return DecodeStatus::NeedMoreData;
  172. default:
  173. rspState = ResponseStates::Error;
  174. return DecodeStatus::Error;
  175. }
  176. case ResponseStates::RequestValue:
  177. if (IsHexDigit(c)) {
  178. responseMsg.request.value <<= 4U;
  179. responseMsg.request.value += Char2Nibble(c);
  180. return DecodeStatus::NeedMoreData;
  181. } else if (c == ' ') {
  182. rspState = ResponseStates::ParamCode;
  183. return DecodeStatus::NeedMoreData;
  184. } else {
  185. rspState = ResponseStates::Error;
  186. return DecodeStatus::Error;
  187. }
  188. case ResponseStates::ParamCode:
  189. switch (c) {
  190. case 'P':
  191. case 'E':
  192. case 'F':
  193. case 'A':
  194. case 'R':
  195. case 'B':
  196. rspState = ResponseStates::ParamValue;
  197. responseMsg.paramCode = (ResponseMsgParamCodes)c;
  198. responseMsg.paramValue = 0;
  199. return DecodeStatus::NeedMoreData;
  200. default:
  201. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  202. rspState = ResponseStates::Error;
  203. return DecodeStatus::Error;
  204. }
  205. case ResponseStates::ParamValue:
  206. if (IsHexDigit(c)) {
  207. responseMsg.paramValue <<= 4U;
  208. responseMsg.paramValue += Char2Nibble(c);
  209. return DecodeStatus::NeedMoreData;
  210. } else if (IsCRCSeparator(c)) {
  211. rspState = ResponseStates::CRC;
  212. return DecodeStatus::NeedMoreData;
  213. } else {
  214. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  215. rspState = ResponseStates::Error;
  216. return DecodeStatus::Error;
  217. }
  218. case ResponseStates::CRC:
  219. if (IsHexDigit(c)) {
  220. responseMsg.request.crc8 <<= 4U;
  221. responseMsg.request.crc8 += Char2Nibble(c);
  222. return DecodeStatus::NeedMoreData;
  223. } else if (IsNewLine(c)) {
  224. // check CRC at this spot
  225. if (responseMsg.request.crc8 != responseMsg.ComputeCRC8()) {
  226. // CRC mismatch
  227. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  228. rspState = ResponseStates::Error;
  229. return DecodeStatus::Error;
  230. } else {
  231. rspState = ResponseStates::RequestCode;
  232. return DecodeStatus::MessageCompleted;
  233. }
  234. } else {
  235. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  236. rspState = ResponseStates::Error;
  237. return DecodeStatus::Error;
  238. }
  239. default: //case error:
  240. if (IsNewLine(c)) {
  241. rspState = ResponseStates::RequestCode;
  242. return DecodeStatus::MessageCompleted;
  243. } else {
  244. responseMsg.paramCode = ResponseMsgParamCodes::unknown;
  245. return DecodeStatus::Error;
  246. }
  247. }
  248. }
  249. uint8_t Protocol::EncodeResponseCmdAR(const RequestMsg &msg, ResponseMsgParamCodes ar, uint8_t *txbuff) {
  250. const ResponseMsg rsp(msg, ar, 0); // this needs some cleanup @@TODO - check assembly how bad is it
  251. uint8_t i = BeginEncodeRequest(rsp.request, txbuff);
  252. txbuff[i] = (uint8_t)ar;
  253. ++i;
  254. i += AppendCRC(rsp.CRC(), txbuff + i);
  255. txbuff[i] = '\n';
  256. ++i;
  257. return i;
  258. }
  259. uint8_t Protocol::EncodeResponseReadFINDA(const RequestMsg &msg, uint8_t findaValue, uint8_t *txbuff) {
  260. return EncodeResponseRead(msg, true, findaValue, txbuff);
  261. }
  262. uint8_t Protocol::EncodeResponseQueryOperation(const RequestMsg &msg, ResponseCommandStatus rcs, uint8_t *txbuff) {
  263. const ResponseMsg rsp(msg, rcs.code, rcs.value);
  264. uint8_t i = BeginEncodeRequest(msg, txbuff);
  265. txbuff[i] = (uint8_t)rsp.paramCode;
  266. ++i;
  267. i += UInt16ToHex(rsp.paramValue, txbuff + i);
  268. i += AppendCRC(rsp.CRC(), txbuff + i);
  269. txbuff[i] = '\n';
  270. return i + 1;
  271. }
  272. uint8_t Protocol::EncodeResponseRead(const RequestMsg &msg, bool accepted, uint16_t value2, uint8_t *txbuff) {
  273. const ResponseMsg rsp(msg,
  274. accepted ? ResponseMsgParamCodes::Accepted : ResponseMsgParamCodes::Rejected,
  275. 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
  276. );
  277. uint8_t i = BeginEncodeRequest(msg, txbuff);
  278. txbuff[i] = (uint8_t)rsp.paramCode;
  279. ++i;
  280. if (accepted) {
  281. // dump the value
  282. i += UInt16ToHex(value2, txbuff + i);
  283. }
  284. i += AppendCRC(rsp.CRC(), txbuff + i);
  285. txbuff[i] = '\n';
  286. return i + 1;
  287. }
  288. uint8_t Protocol::UInt8ToHex(uint8_t value, uint8_t *dst) {
  289. if (value == 0) {
  290. *dst = '0';
  291. return 1;
  292. }
  293. uint8_t v = value >> 4U;
  294. uint8_t charsOut = 1;
  295. if (v != 0) { // skip the first '0' if any
  296. *dst = Nibble2Char(v);
  297. ++dst;
  298. charsOut = 2;
  299. }
  300. v = value & 0xfU;
  301. *dst = Nibble2Char(v);
  302. return charsOut;
  303. }
  304. uint8_t Protocol::UInt16ToHex(uint16_t value, uint8_t *dst) {
  305. constexpr uint16_t topNibbleMask = 0xf000;
  306. if (value == 0) {
  307. *dst = '0';
  308. return 1;
  309. }
  310. // skip initial zeros
  311. uint8_t charsOut = 4;
  312. while ((value & topNibbleMask) == 0) {
  313. value <<= 4U;
  314. --charsOut;
  315. }
  316. for (uint8_t i = 0; i < charsOut; ++i) {
  317. uint8_t n = (value & topNibbleMask) >> (8U + 4U);
  318. value <<= 4U;
  319. *dst = Nibble2Char(n);
  320. ++dst;
  321. }
  322. return charsOut;
  323. }
  324. uint8_t Protocol::BeginEncodeRequest(const RequestMsg &msg, uint8_t *dst) {
  325. dst[0] = (uint8_t)msg.code;
  326. uint8_t i = 1 + UInt8ToHex(msg.value, dst + 1);
  327. dst[i] = ' ';
  328. return i + 1;
  329. }
  330. uint8_t Protocol::AppendCRC(uint8_t crc, uint8_t *dst) {
  331. dst[0] = '*'; // reprap-style separator of CRC
  332. return 1 + UInt8ToHex(crc, dst + 1);
  333. }
  334. } // namespace protocol
  335. } // namespace modules