23 #include "benthos_atm900_driver.h" 24 #include "driver_exception.h" 26 #include "goby/common/logger.h" 27 #include "goby/util/binary.h" 30 using goby::util::hex_decode;
31 using goby::util::hex_encode;
35 const std::string goby::acomms::BenthosATM900Driver::SERIAL_DELIMITER =
"\r\n";
37 boost::shared_ptr<dccl::Codec> goby::acomms::benthos_header_dccl_;
39 goby::acomms::BenthosATM900Driver::BenthosATM900Driver() : fsm_(driver_cfg_), next_frame_(0)
48 if (!cfg.has_line_delimiter())
49 driver_cfg_.set_line_delimiter(SERIAL_DELIMITER);
51 if (!driver_cfg_.has_serial_baud())
52 driver_cfg_.set_serial_baud(DEFAULT_BAUD);
54 glog.is(DEBUG1) &&
glog << group(glog_out_group()) <<
"BenthosATM900Driver: Starting modem..." 64 const int pause_ms = 10;
65 usleep(pause_ms * 1000);
68 const int start_timeout =
69 driver_cfg_.GetExtension(benthos::protobuf::BenthosATM900DriverConfig::start_timeout);
70 if (i / (1000 / pause_ms) > start_timeout)
72 protobuf::ModemDriverStatus::STARTUP_FAILED));
106 case protobuf::ModemTransmission::DATA:
109 if (!msg.has_max_frame_bytes() ||
110 msg.max_frame_bytes() >
111 driver_cfg_.GetExtension(
112 benthos::protobuf::BenthosATM900DriverConfig::max_frame_size))
113 msg.set_max_frame_bytes(driver_cfg_.GetExtension(
114 benthos::protobuf::BenthosATM900DriverConfig::max_frame_size));
118 if (!(msg.frame_size() == 0 || msg.frame(0).empty()))
123 case protobuf::ModemTransmission::DRIVER_SPECIFIC:
125 switch (msg.GetExtension(benthos::protobuf::type))
127 case benthos::protobuf::BENTHOS_TWO_WAY_PING:
132 glog << group(glog_out_group()) << warn
133 <<
"Not initiating transmission because we were given an invalid " 134 "DRIVER_SPECIFIC transmission type for the Benthos ATM-900:" 142 glog.is(DEBUG1) &&
glog << group(glog_out_group()) << warn
143 <<
"Not initiating transmission because we were given an " 144 "invalid transmission type for the base Driver:" 158 data_event.line = in;
161 glog << group(glog_in_group())
162 << (boost::algorithm::all(in, boost::is_print() || boost::is_any_of(
"\r\n"))
163 ? boost::trim_copy(in)
164 : goby::util::hex_encode(in))
167 fsm_.process_event(data_event);
170 while (!fsm_.received().empty())
172 receive(fsm_.received().front());
173 fsm_.received().pop_front();
181 glog.is(DEBUG2) &&
glog << group(glog_in_group()) << msg << std::endl;
183 if (msg.type() == protobuf::ModemTransmission::DATA && msg.ack_requested() &&
184 msg.dest() == driver_cfg_.modem_id())
188 ack.set_type(goby::acomms::protobuf::ModemTransmission::ACK);
189 ack.set_src(msg.dest());
190 ack.set_dest(msg.src());
191 ack.set_rate(msg.rate());
192 for (
int i = msg.frame_start(), n = msg.frame_size() + msg.frame_start(); i < n; ++i)
193 ack.add_acked_frame(i);
202 glog.is(DEBUG2) &&
glog << group(glog_out_group()) << msg << std::endl;
203 fsm_.buffer_data_out(msg);
207 void goby::acomms::BenthosATM900Driver::try_serial_tx()
211 while (!fsm_.serial_tx_buffer().empty())
213 const std::string& line = fsm_.serial_tx_buffer().front();
216 glog << group(glog_out_group())
217 << (boost::algorithm::all(line, boost::is_print() || boost::is_any_of(
"\r\n"))
218 ? boost::trim_copy(line)
219 : goby::util::hex_encode(line))
224 fsm_.serial_tx_buffer().pop_front();
void shutdown()
Shuts down the modem driver.
void modem_write(const std::string &out)
write a line to the serial port.
void handle_initiate_transmission(const protobuf::ModemTransmission &m)
Virtual initiate_transmission method. Typically connected to MACManager::signal_initiate_transmission...
void modem_start(const protobuf::DriverConfig &cfg)
start the physical connection to the modem (serial port, TCP, etc.). must be called before ModemDrive...
void do_work()
Allows the modem driver to do its work.
ReturnType goby_time()
Returns current UTC time as a boost::posix_time::ptime.
boost::signals2::signal< void(const protobuf::ModemTransmission &message)> signal_receive
Called when a binary data transmission is received from the modem.
void do_work()
Allows the modem driver to do its work.
common::FlexOstream glog
Access the Goby logger through this object.
void startup(const protobuf::DriverConfig &cfg)
Starts the modem driver. Must be called before poll().
bool modem_read(std::string *in)
read a line from the serial port, including end-of-line character(s)
boost::signals2::signal< void(protobuf::ModemTransmission *msg)> signal_data_request
Called when the modem or modem driver needs data to send. The returned data should be stored in Modem...
void modem_close()
closes the serial port. Use modem_start to reopen the port.
boost::signals2::signal< void(protobuf::ModemTransmission *msg_request)> signal_modify_transmission
Called before the modem driver begins processing a transmission. This allows a third party to modify ...