Note: Goby version 1 (shown here) is now considered obsolete. Please use version 2 for new projects, and consider upgrading old projects.

Goby Underwater Autonomy Project  Series: 1.1, revision: 163, released on 2013-02-06 14:23:27 -0500
acomms/libmodemdriver/driver_base.cpp
00001 // copyright 2009-2011 t. schneider tes@mit.edu
00002 // 
00003 // this file is part of the goby-acomms acoustic modem driver.
00004 // goby-acomms is a collection of libraries 
00005 // for acoustic underwater networking
00006 //
00007 // This program is free software: you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation, either version 3 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // This software is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this software.  If not, see <http://www.gnu.org/licenses/>.
00019 
00020 #include <boost/thread/mutex.hpp>
00021 
00022 #include "goby/util/logger.h"
00023 
00024 #include "driver_base.h"
00025 #include "driver_exception.h"
00026 
00027 goby::acomms::ModemDriverBase::ModemDriverBase(std::ostream* log /* = 0 */)
00028     : log_(log),
00029       modem_(0)
00030 { }
00031 
00032 goby::acomms::ModemDriverBase::~ModemDriverBase()
00033 {
00034     if(modem_) delete modem_;
00035 }
00036 
00037 void goby::acomms::ModemDriverBase::modem_write(const std::string& out)
00038 {
00039     while(!modem_->active())
00040     {
00041         if(log_) *log_ << group("modem_out") << warn << "modem is closed! (check physical connection)" << std::endl;
00042         sleep(1);
00043     }
00044     
00045     
00046     modem_->write(out);
00047 }
00048 
00049 bool goby::acomms::ModemDriverBase::modem_read(std::string* in)
00050 {
00051     while(!modem_->active())
00052     {
00053         if(log_) *log_ << group("modem_in") << warn << "modem is closed! (check physical connection)" << std::endl;
00054         sleep(1);
00055     }
00056 
00057     return modem_->readline(in);
00058 }
00059 
00060 void goby::acomms::ModemDriverBase::modem_close()
00061 {
00062     modem_->close();    
00063 }
00064 
00065 
00066 
00067 void goby::acomms::ModemDriverBase::modem_start(const protobuf::DriverConfig& cfg)
00068 {        
00069     if(!cfg.has_modem_id())
00070         throw(driver_exception("missing modem_id in configuration"));
00071     
00072     switch(cfg.connection_type())
00073     {
00074         case protobuf::DriverConfig::CONNECTION_SERIAL:
00075             if(log_) *log_ << group("modem_out") << "opening serial port " << cfg.serial_port() << " @ " << cfg.serial_baud() << std::endl;
00076 
00077             if(!cfg.has_serial_port())
00078                 throw(driver_exception("missing serial port in configuration"));
00079             if(!cfg.has_serial_baud())
00080                 throw(driver_exception("missing serial baud in configuration"));
00081             
00082             modem_ = new util::SerialClient(cfg.serial_port(), cfg.serial_baud(), cfg.line_delimiter());
00083             break;
00084             
00085         case protobuf::DriverConfig::CONNECTION_TCP_AS_CLIENT:
00086             if(log_) *log_ << group("modem_out") << "opening tcp client: " << cfg.tcp_server() << ":" << cfg.tcp_port() << std::endl;
00087             if(!cfg.has_tcp_server())
00088                 throw(driver_exception("missing tcp server address in configuration"));
00089             if(!cfg.has_tcp_port())
00090                 throw(driver_exception("missing tcp port in configuration"));
00091 
00092             modem_ = new util::TCPClient(cfg.tcp_server(), cfg.tcp_port(), cfg.line_delimiter());
00093             break;
00094             
00095         case protobuf::DriverConfig::CONNECTION_TCP_AS_SERVER:
00096             if(log_) *log_ << group("modem_out") << "opening tcp server on port" << cfg.tcp_port() << std::endl;
00097 
00098             if(!cfg.has_tcp_port())
00099                 throw(driver_exception("missing tcp port in configuration"));
00100 
00101             modem_ = new util::TCPServer(cfg.tcp_port(), cfg.line_delimiter());
00102 
00103         case protobuf::DriverConfig::CONNECTION_DUAL_UDP_BROADCAST:
00104             throw(driver_exception("unimplemented connection type"));
00105     }    
00106 
00107     modem_->start();
00108 }
00109 
00110 void goby::acomms::ModemDriverBase::add_flex_groups(util::FlexOstream* tout)
00111 {
00112     tout->add_group("modem_out", util::Colors::lt_magenta, "outgoing micromodem messages (goby_modemdriver)");
00113     tout->add_group("modem_in", util::Colors::lt_blue, "incoming micromodem messages (goby_modemdriver)");
00114 }
00115 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends