Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
iridium_driver_fsm.h
Go to the documentation of this file.
1// Copyright 2013-2026:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Goby Underwater Autonomy Project Libraries
10// ("The Goby Libraries").
11//
12// The Goby Libraries are free software: you can redistribute them and/or modify
13// them under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// The Goby Libraries are distributed in the hope that they will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with Goby. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef GOBY_ACOMMS_MODEMDRIVER_IRIDIUM_DRIVER_FSM_H
26#define GOBY_ACOMMS_MODEMDRIVER_IRIDIUM_DRIVER_FSM_H
27
28#include <algorithm> // for for_each
29#include <chrono> // for seconds
30#include <iostream> // for basic_ost...
31#include <locale> // for locale
32#include <random> // for mt19937
33#include <string> // for string
34#include <utility> // for move, pair
35#include <vector> // for vector
36
37#include <boost/algorithm/string/classification.hpp> // for is_any_ofF
38#include <boost/algorithm/string/split.hpp> // for split
39#include <boost/algorithm/string/trim.hpp> // for trim
40#include <boost/bind/bind.hpp> // for bind_t, arg
41#include <boost/circular_buffer.hpp> // for circular_...
42#include <boost/lexical_cast/bad_lexical_cast.hpp> // for bad_lexic...
43#include <boost/mpl/list.hpp> // for list
44#include <boost/smart_ptr/intrusive_ptr.hpp> // for intrusive...
45#include <boost/statechart/custom_reaction.hpp>
46#include <boost/statechart/event.hpp> // for event
47#include <boost/statechart/in_state_reaction.hpp> // for in_state_...
48#include <boost/statechart/result.hpp> // for reaction_...
49#include <boost/statechart/simple_state.hpp> // for simple_st...
50#include <boost/statechart/state.hpp> // for state
51#include <boost/statechart/state_machine.hpp> // for state_mac...
52#include <boost/statechart/transition.hpp> // for transition
53
54#include "goby/acomms/modemdriver/iridium_driver_common.h" // for OnCallBase
55#include "goby/acomms/protobuf/driver_base.pb.h" // for DriverConfig
56#include "goby/acomms/protobuf/iridium_driver.pb.h" // for Config
57#include "goby/acomms/protobuf/modem_message.pb.h" // for ModemTran...
59#include "goby/util/as.h" // for as
60#include "goby/util/debug_logger/flex_ostream.h" // for FlexOstream
61#include "goby/util/debug_logger/flex_ostreambuf.h" // for DEBUG1, WARN
62#include "goby/util/debug_logger/logger_manipulators.h" // for operator<<
63
64namespace goby
65{
66namespace acomms
67{
68namespace iridium
69{
70inline unsigned sbd_csum(const std::string& data)
71{
72 unsigned int csum = 0;
73 for (char it : data) csum += it & 0xFF;
74 return csum;
75}
76
77namespace fsm
78{
80{
81 StateNotify(std::string name) : name_(std::move(name))
82 {
83 glog.is(goby::util::logger::DEBUG1) && glog << group("iridiumdriver") << name_ << std::endl;
84 }
86 {
87 glog.is(goby::util::logger::DEBUG1) && glog << group("iridiumdriver") << "~" << name_
88 << std::endl;
89 }
90
91 private:
92 std::string name_;
93};
94
95// events
97{
98 std::string line;
99};
101{
102};
103
105{
106 std::string line;
107};
109{
110};
111
113{
114 EvAck(std::string response) : response_(std::move(response)) {}
115
116 std::string response_;
117};
118
120{
121};
123{
124};
125
127{
128};
130{
131};
133{
134};
135
137{
138};
139
141{
142};
144{
145};
147{
148};
149
151{
152};
153
155{
156};
158{
159 EvSBDCheckSignal(bool in_response_to_ring_alert = false)
160 : in_response_to_ring_alert_(in_response_to_ring_alert)
161 {
162 }
164};
165
167{
168 EvSBDSignalStrength(int rssi) : rssi_(rssi) {}
169 int rssi_;
170};
171
173{
174 EvSBDBeginData(std::string data = "") : data_(std::move(data)) {}
175 std::string data_;
176};
177
179{
180};
181
182struct EvSBDSendBufferCleared : boost::statechart::event<EvSBDSendBufferCleared>
183{
184};
185
186struct EvSBDSendBufferClearFailed : boost::statechart::event<EvSBDSendBufferClearFailed>
187{
188};
189
191{
192};
194{
195};
197{
198 EvSBDWriteFailed(int result) : result_(result) {}
200};
201struct EvSBDTransmitComplete : boost::statechart::event<EvSBDTransmitComplete>
202{
203 EvSBDTransmitComplete(std::string sbdi) : sbdi_(std::move(sbdi)) {}
204 std::string sbdi_;
205};
206struct EvSBDReceiveComplete : boost::statechart::event<EvSBDReceiveComplete>
207{
208};
209
210struct EvSBDCheckWriteTimeout : boost::statechart::event<EvSBDCheckWriteTimeout>
211{
212};
213
215{
216};
217
218struct EvSBDTrafficManagementStatus : boost::statechart::event<EvSBDTrafficManagementStatus>
219{
220};
221
223{
224};
225
226// pre-declare
227struct Active;
228struct Command;
229struct Configure;
230struct Ready;
231struct FlushEeprom;
232struct EepromFlushed;
233struct Answer;
234struct Dial;
235struct Online;
236struct OnCall;
237struct NotOnCall;
238struct SBD;
239struct SBDReady;
240struct SBDCheckSignal;
241struct SBDSignalOK;
242struct SBDClearBuffers;
243struct SBDWrite;
244struct SBDTransmit;
246struct SBDReceive;
247
248// state machine
249struct IridiumDriverFSM : boost::statechart::state_machine<IridiumDriverFSM, Active>
250{
251 private:
252 struct CIEVData
253 {
254 int rssi{-1};
255 bool service_available{false};
256 };
257
258 public:
259 enum class SBDBlockReason
260 {
261 NONE,
264 };
265
267 : serial_tx_buffer_(SERIAL_BUFFER_CAPACITY),
268 received_(RECEIVED_BUFFER_CAPACITY),
269 driver_cfg_(driver_cfg),
270 data_out_(DATA_BUFFER_CAPACITY),
271 rng_(std::random_device()())
272 {
273 ++count_;
274 glog_ir_group_ = "iridiumdriver::" + goby::util::as<std::string>(count_);
275 }
276
278
279 // messages for the serial line at next opportunity
280 boost::circular_buffer<std::string>& serial_tx_buffer() { return serial_tx_buffer_; }
281
282 // received messages to be passed up out of the ModemDriver
283 boost::circular_buffer<goby::acomms::protobuf::ModemTransmission>& received()
284 {
285 return received_;
286 }
287
288 // data that should (eventually) be sent out across the Iridium connection
289 boost::circular_buffer<goby::acomms::protobuf::ModemTransmission>& data_out()
290 {
291 return data_out_;
292 }
293
294 const goby::acomms::protobuf::DriverConfig& driver_cfg() const { return driver_cfg_; }
299
300 const std::string& glog_ir_group() const { return glog_ir_group_; }
301
302 const CIEVData& ciev_data() const { return ciev_data_; }
303
304 void parse_ciev(const std::string& ciev);
305 // returns rssi (0 if invalid)
306 int parse_csq(const std::string& csq);
307 void parse_sbdloe(const std::string& sbdloe);
308
309 // gate on starting SBD sessions, per Iridium SBD retry and traffic management guidance
310 bool sbd_session_blocked() const { return goby::time::SteadyClock::now() < sbd_blocked_until_; }
311 std::chrono::seconds sbd_block_remaining() const
312 {
313 return std::chrono::ceil<std::chrono::seconds>(sbd_blocked_until_ -
315 }
317 {
318 return sbd_session_blocked() ? sbd_block_reason_ : SBDBlockReason::NONE;
319 }
321 {
322 sbd_consecutive_failures_ = 0;
323 sbd_blocked_until_ = goby::time::SteadyClock::time_point();
324 }
326 void sbd_traffic_management(std::chrono::seconds remaining);
327
328 // MT messages still queued at the GSS after the last session that received one
329 int sbd_mt_queued() const { return sbd_mt_queued_; }
330 void set_sbd_mt_queued(int n) { sbd_mt_queued_ = n; }
331
332 // set when AT commands go unanswered until the state machine resets; cleared by any serial input
333 bool modem_not_responding() const { return modem_not_responding_; }
334 void set_modem_not_responding(bool not_responding) { modem_not_responding_ = not_responding; }
335
337 {
338 return last_sbd_session_start_;
339 }
341 {
342 last_sbd_session_start_ = t;
343 }
344
345 private:
346 enum
347 {
348 SERIAL_BUFFER_CAPACITY = 10
349 };
350 enum
351 {
352 RECEIVED_BUFFER_CAPACITY = 10
353 };
354
355 boost::circular_buffer<std::string> serial_tx_buffer_;
356 boost::circular_buffer<goby::acomms::protobuf::ModemTransmission> received_;
357 const goby::acomms::protobuf::DriverConfig& driver_cfg_;
358
359 enum
360 {
361 DATA_BUFFER_CAPACITY = 5
362 };
363 boost::circular_buffer<goby::acomms::protobuf::ModemTransmission> data_out_;
364
365 std::string glog_ir_group_;
366
367 static int count_;
368
369 CIEVData ciev_data_;
370
371 goby::time::SteadyClock::time_point sbd_blocked_until_;
372 SBDBlockReason sbd_block_reason_{SBDBlockReason::NONE};
373 int sbd_consecutive_failures_{0};
374 int sbd_mt_queued_{0};
375 bool modem_not_responding_{false};
376 goby::time::SteadyClock::time_point last_sbd_session_start_;
377 std::mt19937 rng_;
378};
379
380struct Active : boost::statechart::simple_state<Active, IridiumDriverFSM,
381 boost::mpl::list<Command, NotOnCall>>
382{
383 typedef boost::mpl::list<boost::statechart::transition<EvReset, Active>> reactions;
384};
385
386// Command
387struct Command : boost::statechart::simple_state<Command, Active::orthogonal<0>,
388 boost::mpl::list<Configure, SBD>>,
390{
391 public:
394 void in_state_react(const EvAck&);
395
396 Command() : StateNotify("Command"), at_out_(AT_BUFFER_CAPACITY) {}
397 ~Command() override = default;
398
399 using reactions = boost::mpl::list<
400 boost::statechart::in_state_reaction<EvRxSerial, Command, &Command::in_state_react>,
401 boost::statechart::in_state_reaction<EvTxSerial, Command, &Command::in_state_react>,
402 boost::statechart::transition<EvOnline, Online>,
403 boost::statechart::in_state_reaction<EvAck, Command, &Command::in_state_react>>;
404
406 {
407 ATSentenceMeta() = default;
409 int tries_{0};
410 };
411
412 void push_at_command(const std::string& cmd)
413 {
414 at_out_.push_back(std::make_pair(ATSentenceMeta(), cmd));
415 }
416
417 boost::circular_buffer<std::pair<ATSentenceMeta, std::string>>& at_out() { return at_out_; }
418
419 void clear_sbd_rx_buffer() { sbd_rx_buffer_.clear(); }
420
421 // commands where "ERROR" means unsupported by this modem, not a fault
422 static bool is_optional_command(const std::string& cmd)
423 {
424 static const std::string sbdst = "+SBDST=";
425 return cmd == "+SBDLOE" || cmd == "*F" || cmd.compare(0, sbdst.size(), sbdst) == 0;
426 }
427
429 {
430 return std::max<int>(SBDIX_TIMEOUT_SECONDS,
431 cfg.sbd_session_timeout() + SBDIX_TIMEOUT_MARGIN_SECONDS);
432 }
433
434 void handle_sbd_rx(const std::string& in);
435
436 private:
437 enum
438 {
439 AT_BUFFER_CAPACITY = 100
440 };
441 boost::circular_buffer<std::pair<ATSentenceMeta, std::string>> at_out_;
442 enum
443 {
444 COMMAND_TIMEOUT_SECONDS = 2,
445 DIAL_TIMEOUT_SECONDS = 60,
446 SBDIX_TIMEOUT_SECONDS = DIAL_TIMEOUT_SECONDS,
447 // margin over the modem's own +SBDST timeout so the modem normally reports it first
448 SBDIX_TIMEOUT_MARGIN_SECONDS = 10,
449 FLUSH_EEPROM_TIMEOUT_SECONDS = 10,
450 TRIPLE_PLUS_TIMEOUT_SECONDS = 6,
451 HANGUP_TIMEOUT_SECONDS = 10,
452 ANSWER_TIMEOUT_SECONDS = 30,
453 // +CSQ and +SBDLOE may each take up to 10 seconds to respond
454 CSQ_TIMEOUT_SECONDS = 15,
455 SBDLOE_TIMEOUT_SECONDS = 15
456 };
457
458 enum
459 {
460 RETRIES_BEFORE_RESET = 3
461 };
462 std::string sbd_rx_buffer_;
463};
464
465struct Configure : boost::statechart::state<Configure, Command::orthogonal<0>>, StateNotify
466{
467 using reactions = boost::mpl::list<boost::statechart::transition<EvAtEmpty, Ready>>;
468
469 Configure(my_context ctx) : my_base(std::move(ctx)), StateNotify("Configure")
470 {
471 context<Command>().push_at_command("");
472 const auto& iridium_driver_config = context<IridiumDriverFSM>().iridium_driver_cfg();
473 for (int i = 0, n = iridium_driver_config.config_size(); i < n; ++i)
474 {
475 context<Command>().push_at_command(iridium_driver_config.config(i));
476 }
477 }
478
480};
481
482struct Ready : boost::statechart::simple_state<Ready, Command::orthogonal<0>>, StateNotify
483{
484 public:
485 Ready() : StateNotify("Ready") {}
486 ~Ready() override = default;
487
488 boost::statechart::result react(const EvDial&)
489 {
490 if (context<IridiumDriverFSM>().iridium_driver_cfg().device() ==
492 {
494 glog << group("iridiumdriver")
495 << "Not dialing since the 9602/9603 does not support calls." << std::endl;
496 return discard_event();
497 }
498 else if (state_downcast<const NotOnCall*>() != 0)
499 {
500 return transit<Dial>();
501 }
502 else
503 {
505 glog << group("iridiumdriver") << "Not dialing since we are already on a call."
506 << std::endl;
507 return discard_event();
508 }
509 }
510
511 using reactions = boost::mpl::list<boost::statechart::transition<EvRing, Answer>,
512 boost::statechart::transition<EvFlushEeprom, FlushEeprom>,
513 boost::statechart::custom_reaction<EvDial>>;
514
515 private:
516};
517
518struct FlushEeprom : boost::statechart::state<FlushEeprom, Command::orthogonal<0>>, StateNotify
519{
520 using reactions = boost::mpl::list<boost::statechart::transition<EvAtEmpty, EepromFlushed>>;
521
522 FlushEeprom(my_context ctx) : my_base(std::move(ctx)), StateNotify("FlushEeprom")
523 {
524 context<Command>().push_at_command("*F");
525 }
526 ~FlushEeprom() override = default;
527};
528
529// the modem no longer responds to commands until it is power cycled
530struct EepromFlushed : boost::statechart::simple_state<EepromFlushed, Command::orthogonal<0>>,
532{
533 EepromFlushed() : StateNotify("EepromFlushed") {}
534 ~EepromFlushed() override = default;
535};
536
537struct HangingUp : boost::statechart::state<HangingUp, Command::orthogonal<0>>, StateNotify
538{
539 public:
540 HangingUp(my_context ctx) : my_base(std::move(ctx)), StateNotify("HangingUp")
541 {
542 context<Command>().push_at_command("+++");
543 context<Command>().push_at_command("H");
544 }
545 ~HangingUp() override = default;
546
547 using reactions = boost::mpl::list<boost::statechart::transition<EvAtEmpty, Ready>>;
548
549 private:
550};
551
552struct PostDisconnected : boost::statechart::state<PostDisconnected, Command::orthogonal<0>>,
554{
555 public:
556 PostDisconnected(my_context ctx) : my_base(std::move(ctx)), StateNotify("PostDisconnected")
557 {
559 glog << group("iridiumdriver") << "Disconnected; checking error details: " << std::endl;
560 context<Command>().push_at_command("+CEER");
561 }
562 ~PostDisconnected() override = default;
563
564 using reactions = boost::mpl::list<boost::statechart::transition<EvAtEmpty, Ready>>;
565
566 private:
567};
568
569struct Dial : boost::statechart::state<Dial, Command::orthogonal<0>>, StateNotify
570{
571 using reactions = boost::mpl::list<boost::statechart::custom_reaction<EvNoCarrier>>;
572
573 Dial(my_context ctx) : my_base(std::move(ctx)), StateNotify("Dial"), dial_attempts_(0)
574 {
575 dial();
576 }
577 ~Dial() override = default;
578
579 boost::statechart::result react(const EvNoCarrier&);
580 void dial();
581
582 private:
583 int dial_attempts_;
584};
585
586struct Answer : boost::statechart::state<Answer, Command::orthogonal<0>>, StateNotify
587{
588 using reactions = boost::mpl::list<boost::statechart::transition<EvNoCarrier, Ready>>;
589
590 Answer(my_context ctx) : my_base(std::move(ctx)), StateNotify("Answer")
591 {
592 context<Command>().push_at_command("A");
593 }
594 ~Answer() override = default;
595};
596
597// Online
598struct Online : boost::statechart::simple_state<Online, Active::orthogonal<0>>, StateNotify
599{
600 Online() : StateNotify("Online") {}
601 ~Online() override = default;
602
605
606 using reactions = boost::mpl::list<
607 boost::statechart::transition<EvHangup, HangingUp>,
608 boost::statechart::transition<EvDisconnect, PostDisconnected>,
609 boost::statechart::in_state_reaction<EvRxSerial, Online, &Online::in_state_react>,
610 boost::statechart::in_state_reaction<EvTxSerial, Online, &Online::in_state_react>>;
611};
612
613// Orthogonal on-call / not-on-call
614struct NotOnCall : boost::statechart::simple_state<NotOnCall, Active::orthogonal<1>>, StateNotify
615{
616 using reactions = boost::mpl::list<boost::statechart::transition<EvConnect, OnCall>>;
617
618 NotOnCall() : StateNotify("NotOnCall") {}
619 ~NotOnCall() override = default;
620};
621
622struct OnCall : boost::statechart::state<OnCall, Active::orthogonal<1>>, StateNotify, OnCallBase
623{
624 public:
625 OnCall(my_context ctx) : my_base(std::move(ctx)), StateNotify("OnCall")
626 {
627 // add a brief identifier that is *different* than the "~" which is what PPP uses
628 // add a carriage return to clear out any garbage
629 // at the *beginning* of transmission
630 context<IridiumDriverFSM>().serial_tx_buffer().push_front("goby\r");
631
632 // connecting necessarily puts the DTE online
634 }
635 ~OnCall() override
636 {
637 // signal the disconnect event for the command state to handle
638 glog.is(goby::util::logger::DEBUG1) && glog << group("iridiumdriver") << "Sent "
639 << total_bytes_sent() << " bytes on this call."
640 << std::endl;
642 }
643
647
648 using reactions = boost::mpl::list<
649 boost::statechart::transition<EvNoCarrier, NotOnCall>,
650 boost::statechart::in_state_reaction<EvRxOnCallSerial, OnCall, &OnCall::in_state_react>,
651 boost::statechart::in_state_reaction<EvTxOnCallSerial, OnCall, &OnCall::in_state_react>,
652 boost::statechart::in_state_reaction<EvSendBye, OnCall, &OnCall::in_state_react>>;
653
654 private:
655};
656
657struct SBD : boost::statechart::simple_state<SBD, Command::orthogonal<1>, SBDReady>, StateNotify
658{
659 SBD() : StateNotify("SBD") {}
660 ~SBD() override = default;
661
662 void set_data(const EvSBDBeginData& e) { set_data(e.data_); }
663 void clear_data() { data_.clear(); }
664 const std::string& data() const { return data_; }
665
666 void set_in_response_to_ring_alert(bool b) { in_response_to_ring_alert_ = b; }
667 bool in_response_to_ring_alert() const { return in_response_to_ring_alert_; }
668
669 private:
670 void set_data(const std::string& data)
671 {
672 if (data.empty())
673 data_ = data;
674 else
675 {
676 unsigned int csum = sbd_csum(data);
677
678 const int bits_in_byte = 8;
679 data_ = data + std::string(1, (csum & 0xFF00) >> bits_in_byte) +
680 std::string(1, (csum & 0xFF));
681 }
682 }
683
684 private:
685 std::string data_;
686 bool in_response_to_ring_alert_{false};
687};
688
689/* struct SBDConfigure : boost::statechart::simple_state<SBDConfigure, SBD >, StateNotify */
690/* { */
691/* typedef boost::mpl::list< */
692/* boost::statechart::transition< EvConfigured, SBDReady > */
693/* > reactions; */
694
695/* SBDConfigure() : StateNotify("SBDConfigure") */
696/* { */
697/* } */
698/* ~SBDConfigure() { */
699/* } */
700
701/* }; */
702
704{
705 using reactions = boost::mpl::list<boost::statechart::custom_reaction<EvSBDCheckSignal>>;
706
707 SBDReady() : StateNotify("SBDReady") {}
708
709 ~SBDReady() override = default;
710
711 boost::statechart::result react(const EvSBDCheckSignal& e)
712 {
713 const auto& fsm = context<IridiumDriverFSM>();
714 if (fsm.sbd_session_blocked())
715 {
717 glog << group("iridiumdriver") << "Not starting SBD session: blocked for another "
718 << fsm.sbd_block_remaining().count() << " s" << std::endl;
719 return discard_event();
720 }
721 context<SBD>().set_in_response_to_ring_alert(e.in_response_to_ring_alert_);
722 return transit<SBDCheckSignal>();
723 }
724};
725
727{
728 using reactions = boost::mpl::list<boost::statechart::custom_reaction<EvSBDSignalStrength>>;
729
730 SBDCheckSignal(my_context ctx) : my_base(std::move(ctx)), StateNotify("SBDCheckSignal")
731 {
732 // +CIEV only reports the last known value, so query the current signal strength
733 context<Command>().push_at_command("+CSQ");
734 }
735 ~SBDCheckSignal() override = default;
736
737 boost::statechart::result react(const EvSBDSignalStrength& e)
738 {
739 const int min_rssi =
740 context<IridiumDriverFSM>().iridium_driver_cfg().sbd_min_signal_strength();
741 if (e.rssi_ >= min_rssi)
742 {
743 return transit<SBDSignalOK>();
744 }
745 else
746 {
748 glog << group("iridiumdriver") << "Not starting SBD session: signal strength "
749 << e.rssi_ << " is less than minimum of " << min_rssi << std::endl;
750 return transit<SBDReady>();
751 }
752 }
753};
754
755// waits for the driver to provide the data (if any) for this session
757{
758 using reactions = boost::mpl::list<
759 boost::statechart::transition<EvSBDBeginData, SBDClearBuffers, SBD, &SBD::set_data>,
760 boost::statechart::transition<EvSBDCancel, SBDReady>>;
761
762 SBDSignalOK(my_context ctx) : my_base(std::move(ctx)), StateNotify("SBDSignalOK")
763 {
764 if (context<SBD>().in_response_to_ring_alert())
765 post_event(EvSBDBeginData()); // mailbox check
766 }
767 ~SBDSignalOK() override = default;
768};
769
771{
772 using reactions =
773 boost::mpl::list<boost::statechart::transition<EvSBDSendBufferCleared, SBDWrite>,
774 boost::statechart::custom_reaction<EvSBDSendBufferClearFailed>>;
775
776 SBDClearBuffers(my_context ctx) : my_base(std::move(ctx)), StateNotify("SBDClearBuffers")
777 {
778 context<Command>().clear_sbd_rx_buffer();
779 context<Command>().push_at_command("+SBDD2");
780 }
781
782 ~SBDClearBuffers() override = default;
783
784 boost::statechart::result react(const EvSBDSendBufferClearFailed&)
785 {
787 glog << group("iridiumdriver")
788 << "Error clearing SBD message buffers (+SBDD2); abandoning SBD session"
789 << std::endl;
790 return transit<SBDReady>();
791 }
792};
793
795{
796 SBDWrite(my_context ctx) : my_base(std::move(ctx)), StateNotify("SBDWrite")
797 {
798 if (context<SBD>().data().empty())
799 {
800 glog.is(goby::util::logger::DEBUG1) && glog << group("iridiumdriver")
801 << "Mailbox Check." << std::endl;
802 post_event(EvSBDWriteComplete()); // Mailbox Check
803 }
804 else
805 {
806 glog.is(goby::util::logger::DEBUG1) && glog << group("iridiumdriver") << "Writing data."
807 << std::endl;
808
809 const int csum_bytes = 2;
810 context<Command>().push_at_command(
811 "+SBDWB=" + goby::util::as<std::string>(context<SBD>().data().size() - csum_bytes));
812 }
813 entry_time_ = goby::time::SteadyClock::now();
814 }
815
817 {
818 context<IridiumDriverFSM>().serial_tx_buffer().push_back(context<SBD>().data());
819 }
820
822
823 ~SBDWrite() override = default;
824
825 using reactions = boost::mpl::list<
826 boost::statechart::in_state_reaction<EvSBDWriteReady, SBDWrite, &SBDWrite::in_state_react>,
827 boost::statechart::in_state_reaction<EvSBDCheckWriteTimeout, SBDWrite,
829 boost::statechart::transition<EvSBDWriteComplete, SBDTransmit>,
830 boost::statechart::custom_reaction<EvSBDWriteFailed>>;
831
832 boost::statechart::result react(const EvSBDWriteFailed& e)
833 {
834 std::string reason;
835 switch (e.result_)
836 {
837 case 1: reason = "write timeout"; break;
838 case 2: reason = "checksum mismatch"; break;
839 case 3: reason = "invalid message size"; break;
840 default: reason = "unknown error"; break;
841 }
843 glog << group("iridiumdriver") << "Error writing SBD message (+SBDWB result "
844 << e.result_ << ": " << reason << "); abandoning SBD session" << std::endl;
845 return transit<SBDReady>();
846 }
847
848 private:
850};
851
853{
854 using reactions = boost::mpl::list<boost::statechart::custom_reaction<EvSBDTransmitComplete>,
855 boost::statechart::custom_reaction<EvSBDSessionTimeout>>;
856 SBDTransmit(my_context ctx) : my_base(std::move(ctx)), StateNotify("SBDTransmit")
857 {
858 context<IridiumDriverFSM>().set_last_sbd_session_start(goby::time::SteadyClock::now());
859 if (context<SBD>().in_response_to_ring_alert())
860 context<Command>().push_at_command("+SBDIXA");
861 else
862 context<Command>().push_at_command("+SBDIX");
863 }
864 ~SBDTransmit() override { context<SBD>().clear_data(); }
865
866 std::string mo_status_as_string(int code)
867 {
868 switch (code)
869 {
870 // success
871 case 0: return "MO message, if any, transferred successfully";
872 case 1:
873 return "MO message, if any, transferred successfully, but the MT message in the "
874 "queue was too big to be transferred";
875 case 2:
876 return "MO message, if any, transferred successfully, but the requested Location "
877 "Update was not accepted";
878 case 3:
879 case 4:
880 return "Reserved, but indicate MO session success if used";
881
882 // failure
883 case 5:
884 case 6:
885 case 7:
886 case 8:
887 case 9:
888 case 20:
889 case 21:
890 case 22:
891 case 23:
892 case 24:
893 case 25:
894 case 26:
895 case 27:
896 case 28:
897 case 29:
898 case 30:
899 case 31:
900 default: return "Reserved, but indicate MO session failure if used";
901 case 10: return "GSS reported that the call did not complete in the allowed time";
902 case 11: return "MO message queue at the GSS is full";
903 case 12: return "MO message has too many segments";
904 case 13: return "GSS reported that the session did not complete";
905 case 14: return "Invalid segment size";
906 case 15: return "Access is denied";
907 case 16: return "Modem has been locked and may not make SBD calls (see +CULK command)";
908 case 17: return "Gateway not responding (local session timeout)";
909 case 18: return "Connection lost (RF drop)";
910 case 19: return "Link failure (A protocol error caused termination of the call)";
911 case 32: return "No network service, unable to initiate call";
912 case 33: return "Antenna fault, unable to initiate call";
913 case 34: return "Radio is disabled, unable to initiate call (see *Rn command)";
914 case 35: return "Modem is busy, unable to initiate call";
915 case 36: return "Try later, must wait 3 minutes since last registration";
916 case 37: return "SBD service is temporarily disabled";
917 case 38: return "Try later, traffic management period (see +SBDLOE command)";
918 case 64: return "Band violation (attempt to transmit outside permitted frequency band)";
919 case 65: return "PLL lock failure; hardware error during attempted transmit";
920 }
921 }
922
923 boost::statechart::result react(const EvSBDTransmitComplete& e)
924 {
925 // +SBDIX:<MO status>,<MOMSN>,<MT status>,<MTMSN>,<MT length>,<MT queued>
926 std::vector<std::string> sbdi_fields;
927 boost::algorithm::split(sbdi_fields, e.sbdi_, boost::is_any_of(":,"));
928
929 std::for_each(
930 sbdi_fields.begin(), sbdi_fields.end(),
931 boost::bind(&boost::trim<std::string>, boost::placeholders::_1, std::locale()));
932
933 if (sbdi_fields.size() != 7)
934 {
935 glog.is(goby::util::logger::DEBUG1) && glog << group("iridiumdriver")
936 << "Invalid +SBDI response: " << e.sbdi_
937 << std::endl;
938 context<IridiumDriverFSM>().sbd_session_failed();
939 return transit<SBDReady>();
940 }
941 else
942 {
943 enum
944 {
945 MO_STATUS = 1,
946 MOMSN = 2,
947 MT_STATUS = 3,
948 MTMSN = 4,
949 MT_LENGTH = 5,
950 MT_QUEUED = 6
951 };
952 enum
953 {
954 MT_STATUS_NO_MESSAGE = 0,
955 MT_STATUS_RECEIVED_MESSAGE = 1,
956 MT_STATUS_ERROR = 2
957 };
958
959 // 0-4 OK, 5+ FAILURE
960 enum
961 {
962 MO_STATUS_SUCCESS_MAX = 4,
963 MO_STATUS_FAILURE_MIN = 5,
964 MO_STATUS_TRAFFIC_MANAGEMENT = 38
965 };
966
967 int mo_status = goby::util::as<int>(sbdi_fields[MO_STATUS]);
968 if (mo_status <= MO_STATUS_SUCCESS_MAX)
969 {
971 glog << group("iridiumdriver")
972 << "Success sending SBDIX: " << mo_status_as_string(mo_status)
973 << std::endl;
974 context<IridiumDriverFSM>().sbd_session_succeeded();
975 }
976 else
977 {
979 glog << group("iridiumdriver") << "Error sending SBD packet (" << mo_status
980 << "): " << mo_status_as_string(mo_status) << std::endl;
981 context<IridiumDriverFSM>().sbd_session_failed();
982 if (mo_status == MO_STATUS_TRAFFIC_MANAGEMENT)
983 return transit<SBDCheckTrafficManagement>();
984 else
985 return transit<SBDReady>();
986 }
987
988 int mt_status = goby::util::as<int>(sbdi_fields[MT_STATUS]);
989 if (mt_status == MT_STATUS_ERROR)
991 glog << group("iridiumdriver")
992 << "Error during mailbox check or MT message receive (MT status 2)"
993 << std::endl;
994 // only follow up when a message was received, so an MT that can't be
995 // delivered (e.g. too large) doesn't cause back-to-back sessions
996 context<IridiumDriverFSM>().set_sbd_mt_queued(
997 mt_status == MT_STATUS_RECEIVED_MESSAGE
998 ? goby::util::as<int>(sbdi_fields[MT_QUEUED])
999 : 0);
1000 if (mt_status == MT_STATUS_RECEIVED_MESSAGE)
1001 return transit<SBDReceive>();
1002 else
1003 return transit<SBDReady>();
1004 }
1005 }
1006
1007 boost::statechart::result react(const EvSBDSessionTimeout&)
1008 {
1010 glog << group("iridiumdriver") << "Timeout waiting for +SBDIX response" << std::endl;
1011 context<IridiumDriverFSM>().sbd_session_failed();
1012 return transit<SBDReady>();
1013 }
1014};
1015
1016struct SBDCheckTrafficManagement : boost::statechart::state<SBDCheckTrafficManagement, SBD>,
1018{
1020 boost::mpl::list<boost::statechart::transition<EvSBDTrafficManagementStatus, SBDReady>>;
1021
1023 : my_base(std::move(ctx)), StateNotify("SBDCheckTrafficManagement")
1024 {
1025 context<Command>().push_at_command("+SBDLOE");
1026 }
1027 ~SBDCheckTrafficManagement() override = default;
1028};
1029
1031{
1033 boost::mpl::list<boost::statechart::transition<EvSBDReceiveComplete, SBDReady>>;
1034 SBDReceive(my_context ctx) : my_base(std::move(ctx)), StateNotify("SBDReceive")
1035 {
1036 context<Command>().push_at_command("+SBDRB");
1037 }
1038 ~SBDReceive() override = default;
1039};
1040
1041} // namespace fsm
1042} // namespace iridium
1043} // namespace acomms
1044} // namespace goby
1045
1047
1048#endif
void post_event(const event_base_ptr_type &pEvent)
_proto_TypeTraits::Singular::ConstType GetExtension(const ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< DriverConfig, _proto_TypeTraits, _field_type, _is_packed > &id) const
bool is(goby::util::logger::Verbosity verbosity)
goby::util::logger::GroupSetter group(std::string n)
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::goby::acomms::protobuf::DriverConfig, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::acomms::iridium::protobuf::Config >, 11, false > config
unsigned sbd_csum(const std::string &data)
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
util::FlexOstream glog
Access the Goby logger through this object.
STL namespace.
boost::mpl::list< boost::statechart::transition< EvReset, Active > > reactions
boost::mpl::list< boost::statechart::transition< EvNoCarrier, Ready > > reactions
boost::circular_buffer< std::pair< ATSentenceMeta, std::string > > & at_out()
void in_state_react(const EvRxSerial &)
void in_state_react(const EvTxSerial &)
void in_state_react(const EvAck &)
void push_at_command(const std::string &cmd)
static bool is_optional_command(const std::string &cmd)
static int sbd_session_timeout_seconds(const protobuf::Config &cfg)
boost::mpl::list< boost::statechart::in_state_reaction< EvRxSerial, Command, &Command::in_state_react >, boost::statechart::in_state_reaction< EvTxSerial, Command, &Command::in_state_react >, boost::statechart::transition< EvOnline, Online >, boost::statechart::in_state_reaction< EvAck, Command, &Command::in_state_react > > reactions
void handle_sbd_rx(const std::string &in)
boost::mpl::list< boost::statechart::transition< EvAtEmpty, Ready > > reactions
boost::mpl::list< boost::statechart::custom_reaction< EvNoCarrier > > reactions
boost::statechart::result react(const EvNoCarrier &)
EvSBDCheckSignal(bool in_response_to_ring_alert=false)
boost::mpl::list< boost::statechart::transition< EvAtEmpty, EepromFlushed > > reactions
boost::mpl::list< boost::statechart::transition< EvAtEmpty, Ready > > reactions
goby::time::SteadyClock::time_point last_sbd_session_start() const
const goby::acomms::iridium::protobuf::Config & iridium_driver_cfg() const
const goby::acomms::protobuf::DriverConfig & driver_cfg() const
boost::circular_buffer< goby::acomms::protobuf::ModemTransmission > & received()
void buffer_data_out(const goby::acomms::protobuf::ModemTransmission &msg)
void set_last_sbd_session_start(goby::time::SteadyClock::time_point t)
boost::circular_buffer< std::string > & serial_tx_buffer()
boost::circular_buffer< goby::acomms::protobuf::ModemTransmission > & data_out()
void parse_ciev(const std::string &ciev)
IridiumDriverFSM(const goby::acomms::protobuf::DriverConfig &driver_cfg)
int parse_csq(const std::string &csq)
void parse_sbdloe(const std::string &sbdloe)
void sbd_traffic_management(std::chrono::seconds remaining)
boost::mpl::list< boost::statechart::transition< EvConnect, OnCall > > reactions
boost::mpl::list< boost::statechart::transition< EvNoCarrier, NotOnCall >, boost::statechart::in_state_reaction< EvRxOnCallSerial, OnCall, &OnCall::in_state_react >, boost::statechart::in_state_reaction< EvTxOnCallSerial, OnCall, &OnCall::in_state_react >, boost::statechart::in_state_reaction< EvSendBye, OnCall, &OnCall::in_state_react > > reactions
void in_state_react(const EvSendBye &)
void in_state_react(const EvRxOnCallSerial &)
void in_state_react(const EvTxOnCallSerial &)
void in_state_react(const EvTxSerial &)
boost::mpl::list< boost::statechart::transition< EvHangup, HangingUp >, boost::statechart::transition< EvDisconnect, PostDisconnected >, boost::statechart::in_state_reaction< EvRxSerial, Online, &Online::in_state_react >, boost::statechart::in_state_reaction< EvTxSerial, Online, &Online::in_state_react > > reactions
void in_state_react(const EvRxSerial &)
boost::mpl::list< boost::statechart::transition< EvAtEmpty, Ready > > reactions
boost::statechart::result react(const EvDial &)
boost::mpl::list< boost::statechart::transition< EvRing, Answer >, boost::statechart::transition< EvFlushEeprom, FlushEeprom >, boost::statechart::custom_reaction< EvDial > > reactions
boost::mpl::list< boost::statechart::custom_reaction< EvSBDSignalStrength > > reactions
boost::statechart::result react(const EvSBDSignalStrength &e)
boost::mpl::list< boost::statechart::transition< EvSBDTrafficManagementStatus, SBDReady > > reactions
boost::mpl::list< boost::statechart::transition< EvSBDSendBufferCleared, SBDWrite >, boost::statechart::custom_reaction< EvSBDSendBufferClearFailed > > reactions
boost::statechart::result react(const EvSBDSendBufferClearFailed &)
boost::statechart::result react(const EvSBDCheckSignal &e)
boost::mpl::list< boost::statechart::custom_reaction< EvSBDCheckSignal > > reactions
boost::mpl::list< boost::statechart::transition< EvSBDReceiveComplete, SBDReady > > reactions
boost::mpl::list< boost::statechart::transition< EvSBDBeginData, SBDClearBuffers, SBD, &SBD::set_data >, boost::statechart::transition< EvSBDCancel, SBDReady > > reactions
boost::mpl::list< boost::statechart::custom_reaction< EvSBDTransmitComplete >, boost::statechart::custom_reaction< EvSBDSessionTimeout > > reactions
boost::statechart::result react(const EvSBDSessionTimeout &)
boost::statechart::result react(const EvSBDTransmitComplete &e)
void in_state_react(const EvSBDCheckWriteTimeout &)
boost::statechart::result react(const EvSBDWriteFailed &e)
boost::mpl::list< boost::statechart::in_state_reaction< EvSBDWriteReady, SBDWrite, &SBDWrite::in_state_react >, boost::statechart::in_state_reaction< EvSBDCheckWriteTimeout, SBDWrite, &SBDWrite::in_state_react >, boost::statechart::transition< EvSBDWriteComplete, SBDTransmit >, boost::statechart::custom_reaction< EvSBDWriteFailed > > reactions
void in_state_react(const EvSBDWriteReady &)
const std::string & data() const
void set_data(const EvSBDBeginData &e)
std::chrono::time_point< SteadyClock > time_point
static time_point now() noexcept
Returns the current steady time unless SimulatorSettings::using_sim_time == true in which case a simu...