[asycxx-devel] SF.net SVN: asycxx:[56] trunk
Status: Alpha
Brought to you by:
joe_steeve
From: <viv...@us...> - 2009-04-16 13:25:04
|
Revision: 56 http://asycxx.svn.sourceforge.net/asycxx/?rev=56&view=rev Author: vivekanand83 Date: 2009-04-16 13:24:59 +0000 (Thu, 16 Apr 2009) Log Message: ----------- Added Datagram support. Modified Paths: -------------- trunk/include/Makefile.am trunk/include/asycxx/MsgHandler.h trunk/src/Makefile.am Added Paths: ----------- trunk/include/asycxx/DatagramProtocol.h trunk/include/asycxx/DatagramProtocolFactory.h trunk/include/asycxx/DatagramTransport.h trunk/include/asycxx/RawCANListener.h trunk/include/asycxx/RawCANProtocol.h trunk/include/asycxx/RawCANProtocolFactory.h trunk/include/asycxx/RawCANTransport.h trunk/src/RawCANListener.cxx trunk/src/RawCANTransport.cxx Removed Paths: ------------- trunk/include/asycxx/RAWCANListener.h trunk/include/asycxx/RAWCANTransport.h trunk/src/RAWCANListener.cxx trunk/src/RAWCANTransport.cxx Modified: trunk/include/Makefile.am =================================================================== --- trunk/include/Makefile.am 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/include/Makefile.am 2009-04-16 13:24:59 UTC (rev 56) @@ -23,8 +23,13 @@ asycxx/MsgHandler.h \ asycxx/GimpleMsgBus.h \ asycxx/_asycxx.h \ - asycxx/asycxx.h + asycxx/asycxx.h \ + asycxx/Selectable.h \ + asycxx/RawCANTransport.h \ + asycxx/RawCANListener.h \ + asycxx/RawCANProtocol.h \ + asycxx/RawCANProtocolFactory.h \ + asycxx/DatagramProtocolFactory.h \ + asycxx/DatagramProtocol.h \ + asycxx/DatagramTransport.h -# asycxx/RAWCANTransport.h \ -# asycxx/RAWCANListener.h - Added: trunk/include/asycxx/DatagramProtocol.h =================================================================== --- trunk/include/asycxx/DatagramProtocol.h (rev 0) +++ trunk/include/asycxx/DatagramProtocol.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,45 @@ + +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__DATAGRAM_PROTOCOL_H__ +#define __HIPRO_ASYCXX__DATAGRAM_PROTOCOL_H__ + +#include "Error.h" +#include "Reactor.h" +#include "DataBuffer.h" +#include "Protocol.h" + + +namespace asycxx +{ + class DatagramProtocolFactory; + class DatagramProtocol : public Protocol + { + public: + DatagramProtocol (Reactor *reactor, DatagramProtocolFactory *factory) + :Protocol(reactor, (ProtocolFactory*)factory) {} + virtual ~DatagramProtocol () {} + + }; +} + +#endif /* __HIPRO_ASYCXX__DATAGRAM_PROTOCOL_H__*/ + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Added: trunk/include/asycxx/DatagramProtocolFactory.h =================================================================== --- trunk/include/asycxx/DatagramProtocolFactory.h (rev 0) +++ trunk/include/asycxx/DatagramProtocolFactory.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,41 @@ + +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__DATAGRAM_PROTOCOL_FACTORY_H__ +#define __HIPRO_ASYCXX__DATAGRAM_PROTOCOL_FACTORY_H__ + +#include "Error.h" +#include "Reactor.h" +#include "ProtocolFactory.h" +#include "DatagramProtocol.h" +#include "DatagramTransport.h" + +namespace asycxx +{ + class DatagramProtocolFactory : public ProtocolFactory + { + public: + DatagramProtocolFactory (Reactor *reactor) : ProtocolFactory (reactor) {} + virtual ~DatagramProtocolFactory () {} + }; +} +#endif /* __HIPRO_ASYCXX__DATAGRAM_PROTOCOL_FACTORY_H__ */ + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Added: trunk/include/asycxx/DatagramTransport.h =================================================================== --- trunk/include/asycxx/DatagramTransport.h (rev 0) +++ trunk/include/asycxx/DatagramTransport.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,49 @@ + +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__DATAGRAM_TRANSPORT_H__ +#define __HIPRO_ASYCXX__DATAGRAM_TRANSPORT_H__ + +#include "Error.h" +#include "Transport.h" +#include "DataBuffer.h" +#include "DatagramProtocol.h" + +namespace asycxx +{ + class DatagramTransport : public Transport + { + public: + /** + * \brief CTOR + * + * \param[in] reactor Pointer to a Reactor + * \param[in] fd A valid file-descriptor to build the transport on + */ + DatagramTransport (Reactor *reactor, int fd) + : Transport (reactor, fd) {} + + virtual ~DatagramTransport () {} + }; +} +#endif /* __HIPRO_ASYCXX__DATAGRAM_TRANSPORT_H__*/ + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ + Modified: trunk/include/asycxx/MsgHandler.h =================================================================== --- trunk/include/asycxx/MsgHandler.h 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/include/asycxx/MsgHandler.h 2009-04-16 13:24:59 UTC (rev 56) @@ -36,7 +36,7 @@ void SetMsgBus (MsgBus *bus) { - ASSERT ((bus != NULL), "cannot use a NULL MsgBus"); +// ASSERT ((bus != NULL), "cannot use a NULL MsgBus"); m_Bus = bus; } Deleted: trunk/include/asycxx/RAWCANListener.h =================================================================== --- trunk/include/asycxx/RAWCANListener.h 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/include/asycxx/RAWCANListener.h 2009-04-16 13:24:59 UTC (rev 56) @@ -1,57 +0,0 @@ - -/******************************************************************** - * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All - * rights reserved. - * - * This program and the accompanying materials are made available - * under the terms described in the LICENSE file which accompanies - * this distribution. If the LICENSE file was not attached to this - * distribution or for further clarifications, please contact - * le...@hi.... - * - *******************************************************************/ - -#ifndef __HIPRO_ASYCXX__RAW_CAN_LISTENER_H__ -#define __HIPRO_ASYCXX__RAW_CAN_LISTENER_H__ - - -#include <sys/types.h> -#include <arpa/inet.h> -#include <linux/can.h> -#include <linux/can/raw.h> - -#include "Error.h" -#include "Listener.h" -#include "Factory.h" -#include "Deferred.h" -#include "Reactor.h" - -class RAWCANListener : public Listener -{ - public: - - RAWCANListener (Reactor *reactor, Factory *factory, const char *interface); - virtual ~RAWCANListener (); - - protected: - Reactor *m_Reactor; - Factory *m_Factory; - Deferred *m_ReactorDeferred; - - int m_Socket; - char m_Interface[8]; - bool m_bSetupNoDelay; - protected: - void Init (Reactor *reactor, Factory *factory, const char *interface); -}; - -#endif /* __HIPRO_ASYCXX__RAW_CAN_LISTENER_H__*/ - -/* - Local Variables: - mode: c++ - indent-tabs-mode: nil - tab-width: 4 - c-file-style: "gnu" - End: -*/ Deleted: trunk/include/asycxx/RAWCANTransport.h =================================================================== --- trunk/include/asycxx/RAWCANTransport.h 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/include/asycxx/RAWCANTransport.h 2009-04-16 13:24:59 UTC (rev 56) @@ -1,45 +0,0 @@ - -/******************************************************************** - * Copyright (C), 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All - * rights reserved. - * - * This program and the accompanying materials are made available - * under the terms described in the LICENSE file which accompanies - * this distribution. If the LICENSE file was not attached to this - * distribution or for further clarifications, please contact - * le...@hi.... - * - *******************************************************************/ - -#ifndef __HIPRO_ASYCXX__CAN_TRANSPORT_H__ -#define __HIPRO_ASYCXX__CAN_TRANSPORT_H__ - - -#include "Error.h" -#include "Reactor.h" -#include "DataBuffer.h" -#include "Transport.h" - -class RAWCANTransport : public Transport -{ - public: - RAWCANTransport (Reactor *reactor, int fd); - virtual ~RAWCANTransport (); - - protected: - Result doWrite (void); - Result doRead (DataBuffer *data); - void doDisconnect (void); - -}; - -#endif /* __HIPRO_ASYCXX__CAN_TRANSPORT_H__ */ - -/* - Local Variables: - mode: c++ - indent-tabs-mode: nil - tab-width: 4 - c-file-style: "gnu" - End: -*/ Added: trunk/include/asycxx/RawCANListener.h =================================================================== --- trunk/include/asycxx/RawCANListener.h (rev 0) +++ trunk/include/asycxx/RawCANListener.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,58 @@ + +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__RAW_CAN_LISTENER_H__ +#define __HIPRO_ASYCXX__RAW_CAN_LISTENER_H__ + + +#include <sys/types.h> +#include <arpa/inet.h> +#include <linux/can.h> +#include <linux/can/raw.h> + +#include "Error.h" +#include "Listener.h" +#include "Reactor.h" +#include "RawCANProtocolFactory.h" + +namespace asycxx +{ + class RawCANListener : public Listener + { + public: + RawCANListener(Reactor *reactor, RawCANProtocolFactory *factory, + const char *interface); + virtual ~RawCANListener (); + virtual void Readable (void){}; + + virtual void Writable (void) {} + protected: + Reactor *m_Reactor; + RawCANProtocolFactory *m_Factory; + int m_Socket; + char m_Interface[8]; + bool m_bSetupNoDelay; + protected: + void Init (Reactor *reactor, RawCANProtocolFactory *factory, const char *interface); + }; +} +#endif /* __HIPRO_ASYCXX__RAW_CAN_LISTENER_H__*/ + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Added: trunk/include/asycxx/RawCANProtocol.h =================================================================== --- trunk/include/asycxx/RawCANProtocol.h (rev 0) +++ trunk/include/asycxx/RawCANProtocol.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,54 @@ +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__RAWCAN_PROTOCOL_H__ +#define __HIPRO_ASYCXX__RAWCAN_PROTOCOL_H__ + +#include "Error.h" +#include "Reactor.h" +#include "DataBuffer.h" +#include "DatagramProtocol.h" +#include "DatagramTransport.h" +#include "RawCANTransport.h" +// #include "RawCANProtocolFactory.h" + +namespace asycxx +{ + class RawCANProtocolFactory; + class RawCANProtocol : public DatagramProtocol + { + public: + RawCANProtocol (Reactor *reactor, RawCANProtocolFactory *factory, + RawCANTransport *transport): + DatagramProtocol (reactor, + (DatagramProtocolFactory *)factory) + { + m_Transport = transport; + m_Transport->startReading(); + /*If we are not on the reactor's writers list, then add ourselves + to the reactor's writers list */ + m_Transport->startWriting(); + } + virtual ~RawCANProtocol () {} + + /** + * \brief get the current Transport + */ + virtual RawCANTransport* GetTransport (void) { return m_Transport; } + virtual void DataAvailable(DataBuffer *data) = 0; + + private: + RawCANTransport * m_Transport; + }; +} + +#endif //__HIPRO_ASYCXX__RAWCAN_PROTOCOL_H__ Added: trunk/include/asycxx/RawCANProtocolFactory.h =================================================================== --- trunk/include/asycxx/RawCANProtocolFactory.h (rev 0) +++ trunk/include/asycxx/RawCANProtocolFactory.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,44 @@ + +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__RAWCAN_PROTOCOL_FACTORY_H__ +#define __HIPRO_ASYCXX__RAWCAN_PROTOCOL_FACTORY_H__ + +#include "Error.h" +#include "Reactor.h" +#include "ProtocolFactory.h" +#include "StreamProtocol.h" +#include "StreamTransport.h" +#include "DatagramProtocolFactory.h" +#include "RawCANProtocol.h" + +namespace asycxx +{ + class RawCANProtocolFactory : public DatagramProtocolFactory + { + public: + RawCANProtocolFactory (Reactor *reactor) : DatagramProtocolFactory (reactor) {} + virtual ~RawCANProtocolFactory () {} + virtual RawCANProtocol *GetProtocol (RawCANTransport *trans) = 0; + }; +} +#endif /* __HIPRO_ASYCXX__RAWCAN_PROTOCOL_FACTORY_H__*/ + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Added: trunk/include/asycxx/RawCANTransport.h =================================================================== --- trunk/include/asycxx/RawCANTransport.h (rev 0) +++ trunk/include/asycxx/RawCANTransport.h 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,70 @@ + +/******************************************************************** + * Copyright (C), 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifndef __HIPRO_ASYCXX__CAN_TRANSPORT_H__ +#define __HIPRO_ASYCXX__CAN_TRANSPORT_H__ + + +#include "Error.h" +#include "Reactor.h" +#include "DataBuffer.h" +#include "DatagramTransport.h" +// #include "RawCANProtocol.h" + +namespace asycxx +{ + class RawCANProtocol; + class RawCANTransport : public DatagramTransport + { + public: + RawCANTransport (Reactor *reactor, int fd); + virtual ~RawCANTransport (); + + /* methods to satisfy the 'Transport' interface */ + virtual void Readable (void); + virtual void Writable (void); + virtual void Close(void) {} + virtual void closeReader(){} + virtual void closeWriter(){} + + void Send (DataBuffer *data, uint32_t can_id); + virtual RawCANProtocol* getProtocol (void) + { + return (RawCANProtocol*) Transport::getProtocol(); + } + + /** + * \brief Get the currently attached protocol + */ + virtual void setProtocol (RawCANProtocol *protocol) + { + Transport::setProtocol ((Protocol *)protocol); + } + + protected: + std::list<DataBuffer *> m_writeBuffers; + RetCode doRead (DataBuffer *data); + RetCode doWrite (void); + }; +} + +#endif /* __HIPRO_ASYCXX__CAN_TRANSPORT_H__ */ + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Modified: trunk/src/Makefile.am =================================================================== --- trunk/src/Makefile.am 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/src/Makefile.am 2009-04-16 13:24:59 UTC (rev 56) @@ -20,12 +20,11 @@ StreamProtocol.cxx \ MsgBus.cxx \ GimpleMsgBus.cxx \ - core.cxx + core.cxx \ + RawCANTransport.cxx \ + RawCANListener.cxx -# RAWCANTransport.cxx \ -# RAWCANListener.cxx - libasycxx_0_1_la_CPPFLAGS = -I$(top_srcdir)/include libasycxx_0_1_la_CXXFLAGS = -O2 libasycxx_0_1_la_LDFLAGS = -version-info 0:1:0 Deleted: trunk/src/RAWCANListener.cxx =================================================================== --- trunk/src/RAWCANListener.cxx 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/src/RAWCANListener.cxx 2009-04-16 13:24:59 UTC (rev 56) @@ -1,156 +0,0 @@ -/******************************************************************** - * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All - * rights reserved. - * - * This program and the accompanying materials are made available - * under the terms described in the LICENSE file which accompanies - * this distribution. If the LICENSE file was not attached to this - * distribution or for further clarifications, please contact - * le...@hi.... - * - *******************************************************************/ - -#ifdef HAVE_CONFIG_H -#include <asycxx-config.h> -#endif - -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <netinet/tcp.h> -#include <net/if.h> -#include <linux/can.h> -#include <linux/can/raw.h> - -#include <string.h> -#include <errno.h> -#include <unistd.h> - -#include "asycxx-common.h" -#include <asycxx/Error.h> -#include <asycxx/Factory.h> -#include <asycxx/Protocol.h> -#include <asycxx/RAWCANTransport.h> -#include <asycxx/RAWCANListener.h> - - -#include <sys/ioctl.h> - -/* At time of writing, these constants are not defined in the headers */ -#ifndef PF_CAN -#define PF_CAN 29 -#endif - -#ifndef AF_CAN -#define AF_CAN PF_CAN -#endif - -/** - * \brief ctor to put RAWCANListener on interface - * - * \param[in] reactor The reactor to hook to - * \param[in] factory The factory to use to produce protocols to - * handle new connections - * \param[in] interface The CAN interface to listen to - * - * \details This constructor puts the RAWCANListener object to listen on - * the specified interface. - */ -RAWCANListener::RAWCANListener (Reactor *reactor, Factory *factory, - const char *interface) -{ - Init (reactor, factory, interface); -} - - -/* - * \brief dtor - */ -RAWCANListener::~RAWCANListener () -{ - if (m_Socket != -1) - { - close (m_Socket); - } -} - - -/** - * \brief Initialize the RAWCANListener - * - * \param[in] reactor The reactor to hook to - * \param[in] factory The factory to use to produce protocols - * \param[in] interface The CAN interface to listen to - - * \details This method is the backend method for all the - * constructors. This creates a socketbinds it to the given interface and - * hooks it up to the reactor. - */ -void -RAWCANListener::Init (Reactor *reactor, Factory *factory, const char *interface) -{ - int iret; - - ASSERT ((factory != NULL), "checking parameters"); - ASSERT ((reactor != NULL), "checking parameters"); - ASSERT ((interface != NULL), "checking parameters"); - - m_Factory = factory; - m_Reactor = reactor; - strncpy(m_Interface, interface, strlen(interface) + 1); - - /* Create the socket */ - m_Socket = socket( PF_CAN, SOCK_RAW, CAN_RAW ); - if (m_Socket == -1) - { - THROW (RunTimeError, - "RAWCANListener: %s : creating socket to listen on %s", - strerror (errno), m_Interface); - } - - /* Locate the interface we wish to use */ - struct ifreq ifr; - strcpy(ifr.ifr_name, m_Interface); - iret = ioctl(m_Socket, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled - * with that device's index */ - if (iret < 0) - { - close(m_Socket); - THROW (RunTimeError, - "RAWCANListener: %s : locating interface (ioctl) %s", - strerror (errno), m_Interface); - } - - /* Select that CAN interface, and bind the socket to it. */ - struct sockaddr_can addr; - addr.can_family = AF_CAN; - addr.can_ifindex = ifr.ifr_ifindex; - - iret = bind(m_Socket, (struct sockaddr*)&addr, sizeof(addr)); - if (iret < 0) - { - close(m_Socket); - THROW (RunTimeError, - "RAWCANListener: %s : binding CAN to %s", - strerror (errno), m_Interface); - } - - /* register the non-blocking FD with the reactor */ - SetFDAsNonBlocking (m_Socket); - Protocol *proto; - /* create a protocol using the factory */ - RAWCANTransport *trans; - trans = new RAWCANTransport (m_Reactor, m_Socket); - proto = m_Factory->GetProtocol (trans); - proto->BuildProtocol (); -} - -/* - Local Variables: - mode: c++ - indent-tabs-mode: nil - tab-width: 4 - c-file-style: "gnu" - End: -*/ Deleted: trunk/src/RAWCANTransport.cxx =================================================================== --- trunk/src/RAWCANTransport.cxx 2009-04-16 13:21:54 UTC (rev 55) +++ trunk/src/RAWCANTransport.cxx 2009-04-16 13:24:59 UTC (rev 56) @@ -1,138 +0,0 @@ -/******************************************************************** - * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All - * rights reserved. - * - * This program and the accompanying materials are made available - * under the terms described in the LICENSE file which accompanies - * this distribution. If the LICENSE file was not attached to this - * distribution or for further clarifications, please contact - * le...@hi.... - * - *******************************************************************/ - -#ifdef HAVE_CONFIG_H -#include <asycxx-config.h> -#endif - -#include <errno.h> -#include <unistd.h> -#include <string.h> -#include <deque> - -#include <sys/socket.h> -#include <errno.h> - -#include "asycxx-common.h" -#include <asycxx/Error.h> -#include <asycxx/Protocol.h> -#include <asycxx/RAWCANTransport.h> - -#define RAW_CAN_FRAME_SIZE 16 - -RAWCANTransport::RAWCANTransport (Reactor *reactor, int fd) - : Transport (reactor, fd) -{ - ctorLOG (__FUNCTION__); - configureReadBufferSize (RAW_CAN_FRAME_SIZE); -} - - -RAWCANTransport::~RAWCANTransport () -{ - dtorLOG(__FUNCTION__); -} - - -Result -RAWCANTransport::doRead (DataBuffer *data) -{ - ASSERT ((data != NULL), "checking parameters"); - ssize_t rlen; - - __doRead__again: - rlen = read (m_Fd, data->Data(), data->BufferLen()); - if (rlen == -1) - { - if (errno == EINTR) - { - goto __doRead__again; - } - else if (errno == EAGAIN) - { - data->Len(0); - return Result_Success; - } - else - { - ERR ("%s: reading from fd=%d", strerror(errno), m_Fd); - return Result_Error; - } - } - - /* check for end-of-file */ - if (rlen == 0) - { - ERR ("EoF on fd=%d", m_Fd); - return Result_Error; - } - data->Len(rlen); - - return Result_Success; -} - -Result -RAWCANTransport::doWrite (void) -{ - ssize_t wrote; - ssize_t wlen, to_wlen; - DataBuffer *data; - void *bfr; - - wrote = 0; - while (m_writeBuffers.size() > 0) - { - data = m_writeBuffers[0]; - to_wlen = data->Len() - data->Processed(); - bfr = data->UnProcessed (); - - wlen = write (m_Fd, bfr, to_wlen); - if (wlen == -1) - { - if (errno == EINTR) { continue; } - else if (errno == EAGAIN) { return Result_Pending; } - else - { - ERR ("%s: writing DataBuffer<%p>::data<%p>,len=%d to fd=%d", - strerror (errno), data, bfr, to_wlen, m_Fd); - return Result_Error; - } - } - - /* update pointers */ - data->Processed((data->Processed() + wlen)); - wrote += wlen; - - if (data->Processed() == data->Len()) - { - m_writeBuffers.pop_front(); - data->DisOwn(); - } - } - - return Result_Success; -} - -void -RAWCANTransport::doDisconnect (void) -{ - -} - -/* - Local Variables: - mode: c++ - indent-tabs-mode: nil - tab-width: 4 - c-file-style: "gnu" - End: -*/ Added: trunk/src/RawCANListener.cxx =================================================================== --- trunk/src/RawCANListener.cxx (rev 0) +++ trunk/src/RawCANListener.cxx 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,166 @@ +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifdef HAVE_CONFIG_H +#include <asycxx-config.h> +#endif + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <netinet/tcp.h> +#include <net/if.h> +#include <linux/can.h> +#include <linux/can/raw.h> + +#include <string.h> +#include <errno.h> +#include <unistd.h> + +#include "asycxx-common.h" +#include <asycxx/Error.h> +#include <asycxx/Protocol.h> +#include <asycxx/RawCANTransport.h> +#include <asycxx/RawCANListener.h> + + +#include <sys/ioctl.h> + +/* At time of writing, these constants are not defined in the headers */ +#ifndef PF_CAN +#define PF_CAN 29 +#endif + +#ifndef AF_CAN +#define AF_CAN PF_CAN +#endif + +using namespace asycxx; +/** + * \brief ctor to put RawCANListener on interface + * + * \param[in] reactor The reactor to hook to + * \param[in] factory The factory to use to produce protocols to + * handle new connections + * \param[in] interface The CAN interface to listen to + * + * \details This constructor puts the RawCANListener object to listen on + * the specified interface. + */ +RawCANListener::RawCANListener (Reactor *reactor, RawCANProtocolFactory *factory, + const char *interface) +{ + Init (reactor, factory, interface); +} + + +/* + * \brief dtor + */ +RawCANListener::~RawCANListener () +{ + if (m_Socket != -1) + { + close (m_Socket); + } +} + + +/** + * \brief Initialize the RawCANListener + * + * \param[in] reactor The reactor to hook to + * \param[in] factory The factory to use to produce protocols + * \param[in] interface The CAN interface to listen to + + * \details This method is the backend method for all the + * constructors. This creates a socketbinds it to the given interface and + * hooks it up to the reactor. + */ +void +RawCANListener::Init (Reactor *reactor, RawCANProtocolFactory *factory, const char *interface) +{ + LOG1("%s", __FUNCTION__); + int iret; + + ASSERT ((factory != NULL), "checking parameters"); + ASSERT ((reactor != NULL), "checking parameters"); + ASSERT ((interface != NULL), "checking parameters"); + + m_Factory = factory; + m_Reactor = reactor; + strncpy(m_Interface, interface, strlen(interface) + 1); + + /* Create the socket */ + m_Socket = socket( PF_CAN, SOCK_RAW, CAN_RAW ); + if (m_Socket == -1) + { + THROW (RunTimeError, + "RawCANListener: %s : creating socket to listen on %s", + strerror (errno), m_Interface); + } + + /* Locate the interface we wish to use */ + struct ifreq ifr; + strcpy(ifr.ifr_name, m_Interface); + iret = ioctl(m_Socket, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled + * with that device's index */ + if (iret < 0) + { + close(m_Socket); + THROW (RunTimeError, + "RawCANListener: %s : locating interface (ioctl) %s", + strerror (errno), m_Interface); + } + + /* Select that CAN interface, and bind the socket to it. */ + struct sockaddr_can addr; + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + +// int newMaxBuff=512000; +// iret = setsockopt(m_Socket, SOL_SOCKET, SO_RCVBUF, &newMaxBuff, sizeof(newMaxBuff)); +// if (iret < 0) +// { +// LOG1("Cannot set socket buffer size to %d", newMaxBuff); +// } + + iret = bind(m_Socket, (struct sockaddr*)&addr, sizeof(addr)); + if (iret < 0) + { + close(m_Socket); + THROW (RunTimeError, + "RawCANListener: %s : binding CAN to %s", + strerror (errno), m_Interface); + } + + /* register the non-blocking FD with the reactor */ + SetFDAsNonBlocking (m_Socket); + Fd (m_Socket); + /* hook ourselves to the reactor */ + RawCANProtocol *proto; + /* create a protocol using the factory */ + RawCANTransport *trans; + trans = new RawCANTransport (m_Reactor, m_Socket); + proto = m_Factory->GetProtocol (trans); + trans->setProtocol (proto); +} + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Added: trunk/src/RawCANTransport.cxx =================================================================== --- trunk/src/RawCANTransport.cxx (rev 0) +++ trunk/src/RawCANTransport.cxx 2009-04-16 13:24:59 UTC (rev 56) @@ -0,0 +1,241 @@ +/******************************************************************** + * Copyright (C) 2008,2009 HiPro IT Solutions Pvt. Ltd., Chennai. All + * rights reserved. + * + * This program and the accompanying materials are made available + * under the terms described in the LICENSE file which accompanies + * this distribution. If the LICENSE file was not attached to this + * distribution or for further clarifications, please contact + * le...@hi.... + * + *******************************************************************/ + +#ifdef HAVE_CONFIG_H +#include <asycxx-config.h> +#endif + +#include <errno.h> +#include <unistd.h> +#include <string.h> +#include <deque> + +#include <sys/socket.h> +#include<linux/can.h> +#include <errno.h> + +#include "asycxx-common.h" +#include <asycxx/Error.h> +#include <asycxx/RawCANProtocol.h> +#include <asycxx/RawCANTransport.h> + +#define RAW_CAN_FRAME_SIZE 16 + +using namespace asycxx; + +RawCANTransport::RawCANTransport (Reactor *reactor, int fd) + : DatagramTransport (reactor, fd) +{ + ctorLOG (__FUNCTION__); +} + + +RawCANTransport::~RawCANTransport () +{ + DataBuffer *data; + + /* discard the DataBuffers in the pending queue */ + std::list<DataBuffer *>::iterator it; + for (it = m_writeBuffers.begin(); it != m_writeBuffers.end(); it++) + { + data = *it; + ERR ("Transport<%p> dumping write-buffer <%p>, len=%d", + this, data->Data(), data->Len()); + data->DisOwn (); + } +} + +/* Method to Write a buffer. This method should queue the buffer for + * writing. The actual writing should happen later when the + * file-handle becomes writable. */ +void +RawCANTransport::Send (DataBuffer *data, uint32_t can_id) +{ +// LOG1("%s", __FUNCTION__); + ASSERT ((data != NULL), "given data-buffer is NULL"); + ASSERT ((data->Len() <= 8), + "frame data greater than 8 bytes"); + DataBuffer *canframe = new DataBuffer(sizeof(struct can_frame)); + struct can_frame *frame= (struct can_frame *) canframe->Data(); + memcpy((void*)frame->data, (void*) data->Data(), data->Len()); + frame->can_dlc = data->Len(); + frame->can_id = can_id; + canframe->Len(sizeof(struct can_frame)); + + /* add the DataBuffer to the queue */ + m_writeBuffers.push_back (canframe); + /* assume ownership of the given data-buffer. we do this last so + that any exception in the middle does not cause a leak */ + +} + +/* This method is called by the reactor when the transport becomes + readable */ +void +RawCANTransport::Readable (void) +{ + DataBuffer *data; + RetCode ret; + RawCANProtocol *proto; + proto = getProtocol (); + + /** \todo: should use ioctl(m_Fd, SIOCINQ) to find the amount of + * unread data in the socket and then construct the DataBuffer. This + * should be done in the 'doRead' coz, Transport does not know what + * kind of file-descriptor we have with us. */ + + /* Read till there is no more */ + while (1) + { + data = new DataBuffer (sizeof(struct can_frame)); + + /* call the sub-classes read-method to fill up the + data-buffer */ + ret = doRead (data); + if (ret != RetCode_Success) + { + /* reading from transport failed */ + data->DisOwn (); + proto->ReadError (); + break; + } + + if (data->Len() == 0) + { + /* If there is no more to read, then get out */ + data->DisOwn (); + break; + } + + /* give data-buffer to the protocol. after this point, the + Protocol takes responsibility for the data-buffer object */ + proto->DataAvailable (data); + data->DisOwn (); + } + return; +} + + +/* This method is called by the reactor when a 'Selectable' becomes + * writable. */ +void +RawCANTransport::Writable (void) +{ + DatagramProtocol *proto; + proto = getProtocol (); + RetCode ret; + + /* call the base-class method to write the pending buffers */ + ret = doWrite (); + if (ret == RetCode_Error) + { + /* writing to transport failed */ + proto->WriteError (); + } + if (ret == RetCode_Success) + { + /* nothing more to write */ + stopWriting(); + } + return; +} + +RetCode +RawCANTransport::doRead (DataBuffer *data) +{ + ASSERT ((data != NULL), "checking parameters"); + ssize_t rlen; + + __doRead__again: + rlen = read (Fd(), data->Data(), data->BufferLen()); + if (rlen == -1) + { + if (errno == EINTR) + { + goto __doRead__again; + } + else if (errno == EAGAIN) + { + data->Len(0); + return RetCode_Success; + } + else + { + ERR ("%s: reading from fd=%d", strerror(errno), Fd()); + return RetCode_Error; + } + } + + /* check for end-of-file */ + if (rlen == 0) + { + ERR ("EoF on fd=%d", Fd()); + return RetCode_Error; + } + data->Len(rlen); + + return RetCode_Success; +} + +RetCode +RawCANTransport::doWrite (void) +{ + ssize_t wrote; + ssize_t wlen, to_wlen; + DataBuffer *data; + void *bfr; + std::list<DataBuffer *>::iterator it; + + wrote = 0; + it = m_writeBuffers.begin(); + while (it != m_writeBuffers.end()) + { + data = *it; + to_wlen = data->Len() - data->Processed(); + bfr = data->UnProcessed (); + + wlen = write (Fd(), bfr, to_wlen); + if (wlen == -1) + { + if (errno == EINTR) { continue; } + else if (errno == EAGAIN) { return RetCode_Pending; } + else + { + ERR ("%s: writing DataBuffer<%p>::data<%p>,len=%d to fd=%d", + strerror (errno), data, bfr, to_wlen, Fd()); + return RetCode_Error; + } + } + + /* update pointers */ + data->Processed((data->Processed() + wlen)); + wrote += wlen; + + if (data->Processed() == data->Len()) + { + it = m_writeBuffers.erase(it); + data->DisOwn(); + } + } + + return RetCode_Success; +} + + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |