mmu2_protocol_logic.cpp 22 KB

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