23 #include <boost/format.hpp> 24 #include <boost/thread/mutex.hpp> 26 #include "goby/acomms/connect.h" 27 #include "goby/common/logger.h" 29 #include "driver_base.h" 30 #include "driver_exception.h" 35 int goby::acomms::ModemDriverBase::count_ = 0;
39 glog_out_group_ =
"goby::acomms::modemdriver::out::" + goby::util::as<std::string>(order_);
40 glog_in_group_ =
"goby::acomms::modemdriver::in::" + goby::util::as<std::string>(order_);
54 protobuf::ModemDriverStatus::CONNECTION_TO_MODEM_FAILED));
60 return modem_->readline(in);
63 protobuf::ModemDriverStatus::CONNECTION_TO_MODEM_FAILED));
70 if (!cfg.has_modem_id())
72 protobuf::ModemDriverStatus::INVALID_CONFIGURATION));
74 switch (cfg.connection_type())
76 case protobuf::DriverConfig::CONNECTION_SERIAL:
78 << cfg.serial_port() <<
" @ " << cfg.serial_baud()
81 if (!cfg.has_serial_port())
83 protobuf::ModemDriverStatus::INVALID_CONFIGURATION));
84 if (!cfg.has_serial_baud())
86 protobuf::ModemDriverStatus::INVALID_CONFIGURATION));
92 case protobuf::DriverConfig::CONNECTION_TCP_AS_CLIENT:
94 <<
"opening tcp client: " << cfg.tcp_server() <<
":" 95 << cfg.tcp_port() << std::endl;
96 if (!cfg.has_tcp_server())
98 protobuf::ModemDriverStatus::INVALID_CONFIGURATION));
99 if (!cfg.has_tcp_port())
101 protobuf::ModemDriverStatus::INVALID_CONFIGURATION));
103 modem_.reset(
new util::TCPClient(cfg.tcp_server(), cfg.tcp_port(), cfg.line_delimiter(),
104 cfg.reconnect_interval()));
107 case protobuf::DriverConfig::CONNECTION_TCP_AS_SERVER:
109 <<
"opening tcp server on port" << cfg.tcp_port()
112 if (!cfg.has_tcp_port())
114 protobuf::ModemDriverStatus::INVALID_CONFIGURATION));
116 modem_.reset(
new util::TCPServer(cfg.tcp_port(), cfg.line_delimiter()));
119 if (cfg.has_raw_log())
122 boost::format file_format(cfg.raw_log());
123 file_format.exceptions(boost::io::all_error_bits ^
124 (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
126 std::string file_name = (file_format % to_iso_string(second_clock::universal_time())).str();
128 glog.is(DEBUG1) &&
glog << group(glog_out_group_)
129 <<
"logging raw output to file: " << file_name << std::endl;
131 raw_fs_.reset(
new std::ofstream(file_name.c_str()));
133 if (raw_fs_->is_open())
135 if (!raw_fs_connections_made_)
138 boost::bind(&ModemDriverBase::write_raw,
this, _1,
true));
140 boost::bind(&ModemDriverBase::write_raw,
this, _1,
false));
141 raw_fs_connections_made_ =
true;
146 glog.is(DEBUG1) &&
glog << group(glog_out_group_) << warn <<
"Failed to open log file" 155 const int max_startup_ms = 10000;
156 int startup_elapsed_ms = 0;
157 while (!modem_->active())
160 startup_elapsed_ms += 100;
161 if (startup_elapsed_ms >= max_startup_ms)
163 protobuf::ModemDriverStatus::STARTUP_FAILED));
173 *raw_fs_ << msg.raw() << std::endl;
179 <<
"Updating configuration is not implemented in this driver." provides a basic client for line by line text based communications over a 8N1 tty (such as an RS-232 ...
void modem_write(const std::string &out)
write a line to the serial port.
void modem_start(const protobuf::DriverConfig &cfg)
start the physical connection to the modem (serial port, TCP, etc.). must be called before ModemDrive...
boost::signals2::signal< void(const protobuf::ModemRaw &msg)> signal_raw_incoming
Called after any message is received from the modem by the driver. Used by the MACManager for auto-di...
provides a basic TCP client for line by line text based communications to a remote TCP server ...
provides a basic TCP server for line by line text based communications to a one or more remote TCP cl...
void add_group(const std::string &name, Colors::Color color=Colors::nocolor, const std::string &description="")
Add another group to the logger. A group provides related manipulator for categorizing log messages...
void connect(Signal *signal, Slot slot)
connect a signal to a slot (e.g. function pointer)
common::FlexOstream glog
Access the Goby logger through this object.
virtual ~ModemDriverBase()
Public Destructor.
boost::signals2::signal< void(const protobuf::ModemRaw &msg)> signal_raw_outgoing
Called after any message is sent from the driver to the modem. Useful for higher level analysis and d...
bool modem_read(std::string *in)
read a line from the serial port, including end-of-line character(s)
void modem_close()
closes the serial port. Use modem_start to reopen the port.
virtual void update_cfg(const protobuf::DriverConfig &cfg)
Update configuration while running (not required to be implemented)
ModemDriverBase()
Constructor.