solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/Connections/CConnectSerialAsio.h
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  solarpowerlog
00003  Copyright (C) 2009  Tobias Frost
00004 
00005  This file is part of solarpowerlog.
00006 
00007  Solarpowerlog is free software; However, it is dual-licenced
00008  as described in the file "COPYING".
00009 
00010  For this file (ConnectionTCP.h), the license terms are:
00011 
00012  You can redistribute it and/or  modify it under the terms of the GNU Lesser
00013  General Public License (LGPL) as published by the Free Software Foundation;
00014  either version 3 of the License, or (at your option) any later version.
00015 
00016  This program is distributed in the hope that it will be useful, but
00017  WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  Lesser General Public License for more details.
00020 
00021  You should have received a copy of the GNU Library General Public
00022  License along with this proramm; if not, see
00023  <http://www.gnu.org/licenses/>.
00024  ----------------------------------------------------------------------------
00025  */
00026 
00039 #ifndef CCONNCECTSERIALASIO_H_
00040 #define CCONNCECTSERIALASIO_H_
00041 
00042 #ifdef HAVE_CONFIG_H
00043 #include "config.h"
00044 #include "porting.h"
00045 #endif
00046 
00047 #ifdef HAVE_COMMS_ASIOSERIAL
00048 
00049 #include <boost/asio/serial_port.hpp>
00050 #include <boost/asio/streambuf.hpp>
00051 #include <semaphore.h>
00052 
00053 #include "interfaces/IConnect.h"
00054 #include "interfaces/CWorkScheduler.h"
00055 #include "configuration/Registry.h"
00056 #include "patterns/ICommand.h"
00057 #include "Connections/CAsyncCommand.h"
00058 
00060 #define TCP_ASIO_DEFAULT_TIMEOUT (3000UL)
00061 #define TCP_ASIO_DEFAULT_INTERBYTETIMEOUT (50UL)
00062 
00063 using namespace std;
00064 
00072 class CConnectSerialAsio: public IConnect
00073 {
00074 protected:
00075      friend class IConnectFactory;
00076      CConnectSerialAsio(const string & configurationname);
00077 
00078 public:
00079      virtual ~CConnectSerialAsio();
00080 
00081      virtual bool Connect(ICommand *callback);
00082 
00083      virtual bool Disconnect(ICommand *callback);
00084 
00085      virtual void SetupLogger(const string& parentlogger, const string & = "")
00086      {
00087           IConnect::SetupLogger(parentlogger, "Comms_TCP_ASIO");
00088      }
00089 
00090      virtual bool Send(const char *tosend, unsigned int len, ICommand *callback =
00091                NULL);
00092 
00093      virtual bool Send(const string& tosend, ICommand *callback = NULL);
00094 
00095      virtual bool Receive(ICommand *callback);
00096 
00097      virtual bool CheckConfig(void);
00098 
00099      virtual bool IsConnected(void);
00100 
00101 private:
00102      boost::asio::io_service *ioservice;
00103      boost::asio::serial_port *port;
00104      boost::asio::streambuf *data;
00105 
00106      char characterlen;
00107      boost::asio::serial_port_base::parity parity;
00108      boost::asio::serial_port_base::stop_bits stopbits;
00109      boost::asio::serial_port_base::flow_control flowctrl;
00110      unsigned int baudrate;
00111 
00112      time_t timer;
00113 
00114      // async patch
00115      virtual void _main(void);
00116 
00127      bool PushWork(CAsyncCommand *cmd);
00128 
00130      //void CancelWork( void );
00131 
00141      bool HandleConnect(CAsyncCommand *cmd);
00142 
00149      bool HandleDisConnect(CAsyncCommand *cmd);
00150 
00151      bool HandleReceive(CAsyncCommand *cmd);
00152 
00153      bool HandleSend(CAsyncCommand *cmd);
00154 
00155      list<CAsyncCommand*> cmds;
00156      sem_t cmdsemaphore;
00157 
00158 };
00159 
00160 #endif /* HAVE_COMMS_ASIOSERIAL */
00161 
00162 #endif /* CONNECTIONTCP_H_ */