mmu2_protocol_logic.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #include "mmu2_protocol_logic.h"
  2. #include "mmu2_log.h"
  3. #include "mmu2_fsensor.h"
  4. #include "system_timer.h"
  5. #include <string.h>
  6. namespace MMU2 {
  7. static const uint8_t supportedMmuFWVersion[3] PROGMEM = { 2, 1, 3 };
  8. void ProtocolLogic::CheckAndReportAsyncEvents() {
  9. // even when waiting for a query period, we need to report a change in filament sensor's state
  10. // - it is vital for a precise synchronization of moves of the printer and the MMU
  11. uint8_t fs = (uint8_t)WhereIsFilament();
  12. if (fs != lastFSensor) {
  13. SendAndUpdateFilamentSensor();
  14. }
  15. }
  16. void ProtocolLogic::SendQuery() {
  17. SendMsg(RequestMsg(RequestMsgCodes::Query, 0));
  18. scopeState = ScopeState::QuerySent;
  19. }
  20. void ProtocolLogic::SendFINDAQuery() {
  21. SendMsg(RequestMsg(RequestMsgCodes::Finda, 0));
  22. scopeState = ScopeState::FINDAReqSent;
  23. }
  24. void ProtocolLogic::SendAndUpdateFilamentSensor() {
  25. SendMsg(RequestMsg(RequestMsgCodes::FilamentSensor, lastFSensor = (uint8_t)WhereIsFilament()));
  26. scopeState = ScopeState::FilamentSensorStateSent;
  27. }
  28. void ProtocolLogic::SendButton(uint8_t btn) {
  29. SendMsg(RequestMsg(RequestMsgCodes::Button, btn));
  30. scopeState = ScopeState::ButtonSent;
  31. }
  32. void ProtocolLogic::SendVersion(uint8_t stage) {
  33. SendMsg(RequestMsg(RequestMsgCodes::Version, stage));
  34. scopeState = (ScopeState)((uint_fast8_t)ScopeState::S0Sent + stage);
  35. }
  36. void ProtocolLogic::SendReadRegister(uint8_t index, ScopeState nextState) {
  37. SendMsg(RequestMsg(RequestMsgCodes::Read, index));
  38. scopeState = nextState;
  39. }
  40. void ProtocolLogic::SendWriteRegister(uint8_t index, uint16_t value, ScopeState nextState){
  41. SendWriteMsg(RequestMsg(RequestMsgCodes::Write, index, value));
  42. scopeState = nextState;
  43. }
  44. // searches for "ok\n" in the incoming serial data (that's the usual response of the old MMU FW)
  45. struct OldMMUFWDetector {
  46. uint8_t ok;
  47. inline constexpr OldMMUFWDetector():ok(0) { }
  48. enum class State : uint8_t { MatchingPart, SomethingElse, Matched };
  49. /// @returns true when "ok\n" gets detected
  50. State Detect(uint8_t c){
  51. // consume old MMU FW's data if any -> avoid confusion of protocol decoder
  52. if(ok == 0 && c == 'o'){
  53. ++ok;
  54. return State::MatchingPart;
  55. } else if(ok == 1 && c == 'k'){
  56. ++ok;
  57. return State::MatchingPart;
  58. } else if(ok == 2 && c == '\n'){
  59. return State::Matched;
  60. }
  61. return State::SomethingElse;
  62. }
  63. };
  64. StepStatus ProtocolLogic::ExpectingMessage() {
  65. int bytesConsumed = 0;
  66. int c = -1;
  67. OldMMUFWDetector oldMMUh4x0r; // old MMU FW hacker ;)
  68. // try to consume as many rx bytes as possible (until a message has been completed)
  69. while ((c = uart->read()) >= 0) {
  70. ++bytesConsumed;
  71. RecordReceivedByte(c);
  72. switch (protocol.DecodeResponse(c)) {
  73. case DecodeStatus::MessageCompleted:
  74. rsp = protocol.GetResponseMsg();
  75. LogResponse();
  76. RecordUARTActivity(); // something has happened on the UART, update the timeout record
  77. return MessageReady;
  78. case DecodeStatus::NeedMoreData:
  79. break;
  80. case DecodeStatus::Error:{
  81. // consume old MMU FW's data if any -> avoid confusion of protocol decoder
  82. auto old = oldMMUh4x0r.Detect(c);
  83. if( old == OldMMUFWDetector::State::Matched ){
  84. // hack bad FW version - BEWARE - we silently assume that the first query is an "S0"
  85. // The old MMU FW responds with "ok\n" and we fake the response to a bad FW version at this spot
  86. rsp = ResponseMsg(RequestMsg(RequestMsgCodes::Version, 0), ResponseMsgParamCodes::Accepted, 0);
  87. return MessageReady;
  88. } else if( old == OldMMUFWDetector::State::MatchingPart ){
  89. break;
  90. }
  91. }
  92. [[fallthrough]]; // otherwise
  93. default:
  94. RecordUARTActivity(); // something has happened on the UART, update the timeout record
  95. return ProtocolError;
  96. }
  97. }
  98. if (bytesConsumed != 0) {
  99. RecordUARTActivity(); // something has happened on the UART, update the timeout record
  100. return Processing; // consumed some bytes, but message still not ready
  101. } else if (Elapsed(linkLayerTimeout)) {
  102. return CommunicationTimeout;
  103. }
  104. return Processing;
  105. }
  106. void ProtocolLogic::SendMsg(RequestMsg rq) {
  107. uint8_t txbuff[Protocol::MaxRequestSize()];
  108. uint8_t len = Protocol::EncodeRequest(rq, txbuff);
  109. uart->write(txbuff, len);
  110. LogRequestMsg(txbuff, len);
  111. RecordUARTActivity();
  112. }
  113. void ProtocolLogic::SendWriteMsg(RequestMsg rq){
  114. uint8_t txbuff[Protocol::MaxRequestSize()];
  115. uint8_t len = Protocol::EncodeWriteRequest(rq.value, rq.value2, txbuff);
  116. uart->write(txbuff, len);
  117. LogRequestMsg(txbuff, len);
  118. RecordUARTActivity();
  119. }
  120. void ProtocolLogic::StartSeqRestart() {
  121. retries = maxRetries;
  122. SendVersion(0);
  123. }
  124. void ProtocolLogic::DelayedRestartRestart() {
  125. scopeState = ScopeState::RecoveringProtocolError;
  126. }
  127. void ProtocolLogic::CommandRestart() {
  128. scopeState = ScopeState::CommandSent;
  129. SendMsg(rq);
  130. }
  131. void ProtocolLogic::IdleRestart() {
  132. scopeState = ScopeState::Ready;
  133. }
  134. StepStatus ProtocolLogic::ProcessVersionResponse(uint8_t stage) {
  135. if (rsp.request.code != RequestMsgCodes::Version || rsp.request.value != stage) {
  136. // got a response to something else - protocol corruption probably, repeat the query OR restart the comm by issuing S0?
  137. SendVersion(stage);
  138. } else {
  139. mmuFwVersion[stage] = rsp.paramValue;
  140. if (mmuFwVersion[stage] != pgm_read_byte(supportedMmuFWVersion + stage)) {
  141. if (--retries == 0) {
  142. return VersionMismatch;
  143. } else {
  144. SendVersion(stage);
  145. }
  146. } else {
  147. dataTO.Reset(); // got a meaningful response from the MMU, stop data layer timeout tracking
  148. SendVersion(stage + 1);
  149. }
  150. }
  151. return Processing;
  152. }
  153. StepStatus ProtocolLogic::ScopeStep() {
  154. if ( ! ExpectsResponse() ) {
  155. // we are waiting for something
  156. switch (currentScope) {
  157. case Scope::DelayedRestart:
  158. return DelayedRestartWait();
  159. case Scope::Idle:
  160. return IdleWait();
  161. case Scope::Command:
  162. return CommandWait();
  163. case Scope::Stopped:
  164. return StoppedStep();
  165. default:
  166. break;
  167. }
  168. } else {
  169. // we are expecting a message
  170. if (auto expmsg = ExpectingMessage(); expmsg != MessageReady) // this whole statement takes 12B
  171. return expmsg;
  172. // process message
  173. switch (currentScope) {
  174. case Scope::StartSeq:
  175. return StartSeqStep(); // ~270B
  176. case Scope::Idle:
  177. return IdleStep(); // ~300B
  178. case Scope::Command:
  179. return CommandStep(); // ~430B
  180. case Scope::Stopped:
  181. return StoppedStep();
  182. default:
  183. break;
  184. }
  185. }
  186. return Finished;
  187. }
  188. StepStatus ProtocolLogic::StartSeqStep() {
  189. // solve initial handshake
  190. switch (scopeState) {
  191. case ScopeState::S0Sent: // received response to S0 - major
  192. case ScopeState::S1Sent: // received response to S1 - minor
  193. case ScopeState::S2Sent: // received response to S2 - minor
  194. return ProcessVersionResponse((uint8_t)scopeState - (uint8_t)ScopeState::S0Sent);
  195. case ScopeState::S3Sent: // received response to S3 - revision
  196. if (rsp.request.code != RequestMsgCodes::Version || rsp.request.value != 3) {
  197. // got a response to something else - protocol corruption probably, repeat the query OR restart the comm by issuing S0?
  198. SendVersion(3);
  199. } else {
  200. mmuFwVersionBuild = rsp.paramValue; // just register the build number
  201. // Start General Interrogation after line up.
  202. // For now we just send the state of the filament sensor, but we may request
  203. // data point states from the MMU as well. TBD in the future, especially with another protocol
  204. SendAndUpdateFilamentSensor();
  205. }
  206. return Processing;
  207. case ScopeState::FilamentSensorStateSent:
  208. SwitchFromStartToIdle();
  209. return Processing; // Returning Finished is not a good idea in case of a fast error recovery
  210. // - it tells the printer, that the command which experienced a protocol error and recovered successfully actually terminated.
  211. // In such a case we must return "Processing" in order to keep the MMU state machine running and prevent the printer from executing next G-codes.
  212. break;
  213. default:
  214. return VersionMismatch;
  215. }
  216. return Finished;
  217. }
  218. StepStatus ProtocolLogic::DelayedRestartWait() {
  219. if (Elapsed(heartBeatPeriod)) { // this basically means, that we are waiting until there is some traffic on
  220. while (uart->read() != -1)
  221. ; // clear the input buffer
  222. // switch to StartSeq
  223. Start();
  224. }
  225. return Processing;
  226. }
  227. StepStatus ProtocolLogic::CommandWait() {
  228. if (Elapsed(heartBeatPeriod)) {
  229. SendQuery();
  230. } else {
  231. // even when waiting for a query period, we need to report a change in filament sensor's state
  232. // - it is vital for a precise synchronization of moves of the printer and the MMU
  233. CheckAndReportAsyncEvents();
  234. }
  235. return Processing;
  236. }
  237. StepStatus ProtocolLogic::ProcessCommandQueryResponse() {
  238. switch (rsp.paramCode) {
  239. case ResponseMsgParamCodes::Processing:
  240. progressCode = static_cast<ProgressCode>(rsp.paramValue);
  241. errorCode = ErrorCode::OK;
  242. SendAndUpdateFilamentSensor(); // keep on reporting the state of fsensor regularly
  243. return Processing;
  244. case ResponseMsgParamCodes::Error:
  245. // in case of an error the progress code remains as it has been before
  246. errorCode = static_cast<ErrorCode>(rsp.paramValue);
  247. // keep on reporting the state of fsensor regularly even in command error state
  248. // - the MMU checks FINDA and fsensor even while recovering from errors
  249. SendAndUpdateFilamentSensor();
  250. return CommandError;
  251. case ResponseMsgParamCodes::Button:
  252. // The user pushed a button on the MMU. Save it, do what we need to do
  253. // to prepare, then pass it back to the MMU so it can work its magic.
  254. buttonCode = static_cast<Buttons>(rsp.paramValue);
  255. SendAndUpdateFilamentSensor();
  256. return ButtonPushed;
  257. case ResponseMsgParamCodes::Finished:
  258. progressCode = ProgressCode::OK;
  259. scopeState = ScopeState::Ready;
  260. return Finished;
  261. default:
  262. return ProtocolError;
  263. }
  264. }
  265. StepStatus ProtocolLogic::CommandStep() {
  266. switch (scopeState) {
  267. case ScopeState::CommandSent: {
  268. switch (rsp.paramCode) { // the response should be either accepted or rejected
  269. case ResponseMsgParamCodes::Accepted:
  270. progressCode = ProgressCode::OK;
  271. errorCode = ErrorCode::RUNNING;
  272. scopeState = ScopeState::Wait;
  273. break;
  274. case ResponseMsgParamCodes::Rejected:
  275. // rejected - should normally not happen, but report the error up
  276. progressCode = ProgressCode::OK;
  277. errorCode = ErrorCode::PROTOCOL_ERROR;
  278. return CommandRejected;
  279. default:
  280. return ProtocolError;
  281. }
  282. } break;
  283. case ScopeState::QuerySent:
  284. return ProcessCommandQueryResponse();
  285. case ScopeState::FilamentSensorStateSent:
  286. SendFINDAQuery();
  287. return Processing;
  288. case ScopeState::FINDAReqSent:
  289. findaPressed = rsp.paramValue;
  290. SendReadRegister(4, ScopeState::StatisticsSent);
  291. return Processing;
  292. case ScopeState::StatisticsSent:
  293. scopeState = ScopeState::Wait;
  294. return Processing;
  295. case ScopeState::ButtonSent:
  296. if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
  297. // Button was accepted, decrement the retry.
  298. mmu2.DecrementRetryAttempts();
  299. }
  300. SendAndUpdateFilamentSensor();
  301. break;
  302. default:
  303. return ProtocolError;
  304. }
  305. return Processing;
  306. }
  307. StepStatus ProtocolLogic::IdleWait() {
  308. if (scopeState == ScopeState::Ready) { // check timeout
  309. if (Elapsed(heartBeatPeriod)) {
  310. SendQuery();
  311. return Processing;
  312. }
  313. }
  314. return Finished;
  315. }
  316. StepStatus ProtocolLogic::IdleStep() {
  317. switch (scopeState) {
  318. case ScopeState::QuerySent: // check UART
  319. // If we are accidentally in Idle and we receive something like "T0 P1" - that means the communication dropped out while a command was in progress.
  320. // That causes no issues here, we just need to switch to Command processing and continue there from now on.
  321. // The usual response in this case should be some command and "F" - finished - that confirms we are in an Idle state even on the MMU side.
  322. switch (rsp.request.code) {
  323. case RequestMsgCodes::Cut:
  324. case RequestMsgCodes::Eject:
  325. case RequestMsgCodes::Load:
  326. case RequestMsgCodes::Mode:
  327. case RequestMsgCodes::Tool:
  328. case RequestMsgCodes::Unload:
  329. if (rsp.paramCode != ResponseMsgParamCodes::Finished) {
  330. return SwitchFromIdleToCommand();
  331. }
  332. break;
  333. case RequestMsgCodes::Reset:
  334. // this one is kind of special
  335. // we do not transfer to any "running" command (i.e. we stay in Idle),
  336. // but in case there is an error reported we must make sure it gets propagated
  337. switch (rsp.paramCode) {
  338. case ResponseMsgParamCodes::Button:
  339. // The user pushed a button on the MMU. Save it, do what we need to do
  340. // to prepare, then pass it back to the MMU so it can work its magic.
  341. buttonCode = static_cast<Buttons>(rsp.paramValue);
  342. SendFINDAQuery();
  343. return ButtonPushed;
  344. case ResponseMsgParamCodes::Processing:
  345. // @@TODO we may actually use this branch to report progress of manual operation on the MMU
  346. // The MMU sends e.g. X0 P27 after its restart when the user presses an MMU button to move the Selector
  347. // For now let's behave just like "finished"
  348. case ResponseMsgParamCodes::Finished:
  349. errorCode = ErrorCode::OK;
  350. break;
  351. default:
  352. errorCode = static_cast<ErrorCode>(rsp.paramValue);
  353. SendFINDAQuery(); // continue Idle state without restarting the communication
  354. return CommandError;
  355. }
  356. break;
  357. default:
  358. return ProtocolError;
  359. }
  360. SendFINDAQuery();
  361. return Processing;
  362. case ScopeState::FINDAReqSent:
  363. findaPressed = rsp.paramValue;
  364. SendReadRegister(4, ScopeState::StatisticsSent);
  365. return Processing;
  366. case ScopeState::StatisticsSent:
  367. failStatistics = rsp.paramValue;
  368. scopeState = ScopeState::Ready;
  369. return Finished;
  370. case ScopeState::ButtonSent:
  371. if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
  372. // Button was accepted, decrement the retry.
  373. mmu2.DecrementRetryAttempts();
  374. }
  375. SendFINDAQuery();
  376. return Processing;
  377. case ScopeState::ReadRegisterSent:
  378. if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
  379. // @@TODO just dump the value onto the serial
  380. }
  381. return Finished;
  382. case ScopeState::WriteRegisterSent:
  383. if (rsp.paramCode == ResponseMsgParamCodes::Accepted) {
  384. // @@TODO do something? Retry if not accepted?
  385. }
  386. return Finished;
  387. default:
  388. return ProtocolError;
  389. }
  390. // The "return Finished" in this state machine requires a bit of explanation:
  391. // The Idle state either did nothing (still waiting for the heartbeat timeout)
  392. // or just successfully received the answer to Q0, whatever that was.
  393. // In both cases, it is ready to hand over work to a command or something else,
  394. // therefore we are returning Finished (also to exit mmu_loop() and unblock Marlin's loop!).
  395. // If there is no work, we'll end up in the Idle state again
  396. // and we'll send the heartbeat message after the specified timeout.
  397. return Finished;
  398. }
  399. ProtocolLogic::ProtocolLogic(MMU2Serial *uart)
  400. : currentScope(Scope::Stopped)
  401. , scopeState(ScopeState::Ready)
  402. , plannedRq(RequestMsgCodes::unknown, 0)
  403. , lastUARTActivityMs(0)
  404. , dataTO()
  405. , rsp(RequestMsg(RequestMsgCodes::unknown, 0), ResponseMsgParamCodes::unknown, 0)
  406. , state(State::Stopped)
  407. , lrb(0)
  408. , uart(uart)
  409. , errorCode(ErrorCode::OK)
  410. , progressCode(ProgressCode::OK)
  411. , buttonCode(NoButton)
  412. , lastFSensor((uint8_t)WhereIsFilament())
  413. , findaPressed(false)
  414. , failStatistics(0)
  415. , mmuFwVersion { 0, 0, 0 }
  416. {}
  417. void ProtocolLogic::Start() {
  418. state = State::InitSequence;
  419. currentScope = Scope::StartSeq;
  420. protocol.ResetResponseDecoder(); // important - finished delayed restart relies on this
  421. StartSeqRestart();
  422. }
  423. void ProtocolLogic::Stop() {
  424. state = State::Stopped;
  425. currentScope = Scope::Stopped;
  426. }
  427. void ProtocolLogic::ToolChange(uint8_t slot) {
  428. PlanGenericRequest(RequestMsg(RequestMsgCodes::Tool, slot));
  429. }
  430. void ProtocolLogic::Statistics() {
  431. PlanGenericRequest(RequestMsg(RequestMsgCodes::Version, 3));
  432. }
  433. void ProtocolLogic::UnloadFilament() {
  434. PlanGenericRequest(RequestMsg(RequestMsgCodes::Unload, 0));
  435. }
  436. void ProtocolLogic::LoadFilament(uint8_t slot) {
  437. PlanGenericRequest(RequestMsg(RequestMsgCodes::Load, slot));
  438. }
  439. void ProtocolLogic::EjectFilament(uint8_t slot) {
  440. PlanGenericRequest(RequestMsg(RequestMsgCodes::Eject, slot));
  441. }
  442. void ProtocolLogic::CutFilament(uint8_t slot) {
  443. PlanGenericRequest(RequestMsg(RequestMsgCodes::Cut, slot));
  444. }
  445. void ProtocolLogic::ResetMMU() {
  446. PlanGenericRequest(RequestMsg(RequestMsgCodes::Reset, 0));
  447. }
  448. void ProtocolLogic::Button(uint8_t index) {
  449. PlanGenericRequest(RequestMsg(RequestMsgCodes::Button, index));
  450. }
  451. void ProtocolLogic::Home(uint8_t mode) {
  452. PlanGenericRequest(RequestMsg(RequestMsgCodes::Home, mode));
  453. }
  454. void ProtocolLogic::ReadRegister(uint8_t address){
  455. PlanGenericRequest(RequestMsg(RequestMsgCodes::Read, address));
  456. }
  457. void ProtocolLogic::WriteRegister(uint8_t address, uint16_t data){
  458. PlanGenericRequest(RequestMsg(RequestMsgCodes::Write, address, data));
  459. }
  460. void ProtocolLogic::PlanGenericRequest(RequestMsg rq) {
  461. plannedRq = rq;
  462. if (!ExpectsResponse()) {
  463. ActivatePlannedRequest();
  464. } // otherwise wait for an empty window to activate the request
  465. }
  466. bool ProtocolLogic::ActivatePlannedRequest() {
  467. switch(plannedRq.code){
  468. case RequestMsgCodes::Button:
  469. // only issue the button to the MMU and do not restart the state machines
  470. SendButton(plannedRq.value);
  471. plannedRq = RequestMsg(RequestMsgCodes::unknown, 0);
  472. return true;
  473. case RequestMsgCodes::Read:
  474. SendReadRegister(plannedRq.value, ScopeState::ReadRegisterSent );
  475. plannedRq = RequestMsg(RequestMsgCodes::unknown, 0);
  476. return true;
  477. case RequestMsgCodes::Write:
  478. SendWriteRegister(plannedRq.value, plannedRq.value2, ScopeState::WriteRegisterSent );
  479. plannedRq = RequestMsg(RequestMsgCodes::unknown, 0);
  480. return true;
  481. case RequestMsgCodes::unknown:
  482. return false;
  483. default:// commands
  484. currentScope = Scope::Command;
  485. SetRequestMsg(plannedRq);
  486. plannedRq = RequestMsg(RequestMsgCodes::unknown, 0);
  487. CommandRestart();
  488. return true;
  489. }
  490. }
  491. StepStatus ProtocolLogic::SwitchFromIdleToCommand() {
  492. currentScope = Scope::Command;
  493. SetRequestMsg(rsp.request);
  494. // we are recovering from a communication drop out, the command is already running
  495. // and we have just received a response to a Q0 message about a command progress
  496. return ProcessCommandQueryResponse();
  497. }
  498. void ProtocolLogic::SwitchToIdle() {
  499. state = State::Running;
  500. currentScope = Scope::Idle;
  501. IdleRestart();
  502. }
  503. void ProtocolLogic::SwitchFromStartToIdle() {
  504. state = State::Running;
  505. currentScope = Scope::Idle;
  506. IdleRestart();
  507. SendQuery(); // force sending Q0 immediately
  508. }
  509. bool ProtocolLogic::Elapsed(uint32_t timeout) const {
  510. return _millis() >= (lastUARTActivityMs + timeout);
  511. }
  512. void ProtocolLogic::RecordUARTActivity() {
  513. lastUARTActivityMs = _millis();
  514. }
  515. void ProtocolLogic::RecordReceivedByte(uint8_t c) {
  516. lastReceivedBytes[lrb] = c;
  517. lrb = (lrb + 1) % lastReceivedBytes.size();
  518. }
  519. constexpr char NibbleToChar(uint8_t c) {
  520. switch (c) {
  521. case 0:
  522. case 1:
  523. case 2:
  524. case 3:
  525. case 4:
  526. case 5:
  527. case 6:
  528. case 7:
  529. case 8:
  530. case 9:
  531. return c + '0';
  532. case 10:
  533. case 11:
  534. case 12:
  535. case 13:
  536. case 14:
  537. case 15:
  538. return (c - 10) + 'a';
  539. default:
  540. return 0;
  541. }
  542. }
  543. void ProtocolLogic::FormatLastReceivedBytes(char *dst) {
  544. for (uint8_t i = 0; i < lastReceivedBytes.size(); ++i) {
  545. uint8_t b = lastReceivedBytes[(lrb - i - 1) % lastReceivedBytes.size()];
  546. dst[i * 3] = NibbleToChar(b >> 4);
  547. dst[i * 3 + 1] = NibbleToChar(b & 0xf);
  548. dst[i * 3 + 2] = ' ';
  549. }
  550. dst[(lastReceivedBytes.size() - 1) * 3 + 2] = 0; // terminate properly
  551. }
  552. void ProtocolLogic::FormatLastResponseMsgAndClearLRB(char *dst) {
  553. *dst++ = '<';
  554. for (uint8_t i = 0; i < lrb; ++i) {
  555. uint8_t b = lastReceivedBytes[i];
  556. if (b < 32)
  557. b = '.';
  558. if (b > 127)
  559. b = '.';
  560. *dst++ = b;
  561. }
  562. *dst = 0; // terminate properly
  563. lrb = 0; // reset the input buffer index in case of a clean message
  564. }
  565. void ProtocolLogic::LogRequestMsg(const uint8_t *txbuff, uint8_t size) {
  566. constexpr uint_fast8_t rqs = modules::protocol::Protocol::MaxRequestSize() + 2;
  567. char tmp[rqs] = ">";
  568. static char lastMsg[rqs] = "";
  569. for (uint8_t i = 0; i < size; ++i) {
  570. uint8_t b = txbuff[i];
  571. if (b < 32)
  572. b = '.';
  573. if (b > 127)
  574. b = '.';
  575. tmp[i + 1] = b;
  576. }
  577. tmp[size + 1] = '\n';
  578. tmp[size + 2] = 0;
  579. if (!strncmp_P(tmp, PSTR(">S0*99.\n"), rqs) && !strncmp(lastMsg, tmp, rqs)) {
  580. // @@TODO we skip the repeated request msgs for now
  581. // to avoid spoiling the whole log just with ">S0" messages
  582. // especially when the MMU is not connected.
  583. // We'll lose the ability to see if the printer is actually
  584. // trying to find the MMU, but since it has been reliable in the past
  585. // we can live without it for now.
  586. } else {
  587. MMU2_ECHO_MSG(tmp);
  588. }
  589. memcpy(lastMsg, tmp, rqs);
  590. }
  591. void ProtocolLogic::LogError(const char *reason_P) {
  592. char lrb[lastReceivedBytes.size() * 3];
  593. FormatLastReceivedBytes(lrb);
  594. MMU2_ERROR_MSGRPGM(reason_P);
  595. SERIAL_ECHOPGM(", last bytes: ");
  596. SERIAL_ECHOLN(lrb);
  597. }
  598. void ProtocolLogic::LogResponse() {
  599. char lrb[lastReceivedBytes.size()];
  600. FormatLastResponseMsgAndClearLRB(lrb);
  601. MMU2_ECHO_MSG(lrb);
  602. SERIAL_ECHOLN();
  603. }
  604. StepStatus ProtocolLogic::SuppressShortDropOuts(const char *msg_P, StepStatus ss) {
  605. if (dataTO.Record(ss)) {
  606. LogError(msg_P);
  607. return dataTO.InitialCause();
  608. } else {
  609. return Processing; // suppress short drop outs of communication
  610. }
  611. }
  612. StepStatus ProtocolLogic::HandleCommunicationTimeout() {
  613. uart->flush(); // clear the output buffer
  614. protocol.ResetResponseDecoder();
  615. Start();
  616. return SuppressShortDropOuts(PSTR("Communication timeout"), CommunicationTimeout);
  617. }
  618. StepStatus ProtocolLogic::HandleProtocolError() {
  619. uart->flush(); // clear the output buffer
  620. state = State::InitSequence;
  621. currentScope = Scope::DelayedRestart;
  622. DelayedRestartRestart();
  623. return SuppressShortDropOuts(PSTR("Protocol Error"), ProtocolError);
  624. }
  625. StepStatus ProtocolLogic::Step() {
  626. if (!ExpectsResponse()) { // if not waiting for a response, activate a planned request immediately
  627. ActivatePlannedRequest();
  628. }
  629. auto currentStatus = ScopeStep();
  630. switch (currentStatus) {
  631. case Processing:
  632. // we are ok, the state machine continues correctly
  633. break;
  634. case Finished: {
  635. // We are ok, switching to Idle if there is no potential next request planned.
  636. // But the trouble is we must report a finished command if the previous command has just been finished
  637. // i.e. only try to find some planned command if we just finished the Idle cycle
  638. bool previousCommandFinished = currentScope == Scope::Command; // @@TODO this is a nasty hack :(
  639. if (!ActivatePlannedRequest()) { // if nothing is planned, switch to Idle
  640. SwitchToIdle();
  641. } else {
  642. // if the previous cycle was Idle and now we have planned a new command -> avoid returning Finished
  643. if (!previousCommandFinished && currentScope == Scope::Command) {
  644. currentStatus = Processing;
  645. }
  646. }
  647. } break;
  648. case CommandRejected:
  649. // we have to repeat it - that's the only thing we can do
  650. // no change in state
  651. // @@TODO wait until Q0 returns command in progress finished, then we can send this one
  652. LogError(PSTR("Command rejected"));
  653. CommandRestart();
  654. break;
  655. case CommandError:
  656. LogError(PSTR("Command Error"));
  657. // we shall probably transfer into the Idle state and await further instructions from the upper layer
  658. // Idle state may solve the problem of keeping up the heart beat running
  659. break;
  660. case VersionMismatch:
  661. LogError(PSTR("Version mismatch"));
  662. Stop(); // cannot continue
  663. break;
  664. case ProtocolError:
  665. currentStatus = HandleProtocolError();
  666. break;
  667. case CommunicationTimeout:
  668. currentStatus = HandleCommunicationTimeout();
  669. break;
  670. default:
  671. break;
  672. }
  673. return currentStatus;
  674. }
  675. uint8_t ProtocolLogic::CommandInProgress() const {
  676. if (currentScope != Scope::Command)
  677. return 0;
  678. return (uint8_t)ReqMsg().code;
  679. }
  680. bool DropOutFilter::Record(StepStatus ss) {
  681. if (occurrences == maxOccurrences) {
  682. cause = ss;
  683. }
  684. --occurrences;
  685. return occurrences == 0;
  686. }
  687. } // namespace MMU2