[asycxx-devel] SF.net SVN: asycxx:[18] trunk
Status: Alpha
Brought to you by:
joe_steeve
From: <viv...@us...> - 2009-03-23 08:45:21
|
Revision: 18 http://asycxx.svn.sourceforge.net/asycxx/?rev=18&view=rev Author: vivekanand83 Date: 2009-03-23 08:45:18 +0000 (Mon, 23 Mar 2009) Log Message: ----------- Raw CAN support added. Modified Paths: -------------- trunk/configure.ac trunk/examples/Makefile.am trunk/src/Makefile.am trunk/src/asycxx-common.h Added Paths: ----------- trunk/examples/canserver/ trunk/examples/canserver/Canserver.cxx trunk/examples/canserver/Makefile.am trunk/include/asycxx/RAWCANListener.h trunk/include/asycxx/RAWCANTransport.h trunk/src/RAWCANListener.cxx trunk/src/RAWCANTransport.cxx Modified: trunk/configure.ac =================================================================== --- trunk/configure.ac 2009-03-17 09:24:06 UTC (rev 17) +++ trunk/configure.ac 2009-03-23 08:45:18 UTC (rev 18) @@ -70,6 +70,7 @@ include/Makefile examples/Makefile examples/echoserver/Makefile +examples/canserver/Makefile asycxx-0.1.pc ]) AC_OUTPUT Modified: trunk/examples/Makefile.am =================================================================== --- trunk/examples/Makefile.am 2009-03-17 09:24:06 UTC (rev 17) +++ trunk/examples/Makefile.am 2009-03-23 08:45:18 UTC (rev 18) @@ -1,2 +1,2 @@ -SUBDIRS = echoserver +SUBDIRS = echoserver canserver Added: trunk/examples/canserver/Canserver.cxx =================================================================== --- trunk/examples/canserver/Canserver.cxx (rev 0) +++ trunk/examples/canserver/Canserver.cxx 2009-03-23 08:45:18 UTC (rev 18) @@ -0,0 +1,79 @@ + +/******************************************************************** + * 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.... + * + *******************************************************************/ + +#include <memory> + +#include <asycxx/Logger.h> +#include <asycxx/Error.h> +#include <asycxx/Timer.h> +#include <asycxx/SelectReactor.h> +#include <asycxx/Protocol.h> +#include <asycxx/Factory.h> +#include <asycxx/RAWCANListener.h> + +Logger *Log; + +class RAWCANServerProtocol : public Protocol +{ + public: + RAWCANServerProtocol (Reactor *reactor, Factory *factory, Transport +*transport) + : Protocol (reactor, factory, transport) + {} + ~RAWCANServerProtocol () {} + + void DataAvailable (DataBuffer *data) + { + printf ("CANSERVER: Got data of size %d\n", data->Len()); + } +}; + + +class RAWCANServerProtocolFactory : public Factory +{ + public: + RAWCANServerProtocolFactory (Reactor *reactor) : Factory (reactor) + {} + ~RAWCANServerProtocolFactory () {} + + RAWCANServerProtocol * GetProtocol (Transport *trans) + { + return new RAWCANServerProtocol (m_Reactor, this, trans); + } +}; + + +int +main () +{ + Reactor *reactor; + + Log = new Logger(); + Log->EnableConsole(); + + reactor = new SelectReactor (); + Timer::Init (reactor); + + RAWCANServerProtocolFactory CANfactory (reactor); + RAWCANListener RAWCANlistener (reactor, &CANfactory, "vcan0"); + reactor->Run (); +} + +/* + Local Variables: + mode: c++ + indent-tabs-mode: nil + tab-width: 4 + c-file-style: "gnu" + End: +*/ Added: trunk/examples/canserver/Makefile.am =================================================================== --- trunk/examples/canserver/Makefile.am (rev 0) +++ trunk/examples/canserver/Makefile.am 2009-03-23 08:45:18 UTC (rev 18) @@ -0,0 +1,14 @@ + +noinst_PROGRAMS = CanServer + +CanServer_SOURCES = Canserver.cxx +CanServer_LDADD = $(top_srcdir)/src/libasycxx-0.1.la +CanServer_CPPFLAGS = -I$(top_srcdir)/include +CanServer_LDFLAGS = -L$(top_srcdir)/src + + +# Local Variables: +# mode: makefile +# indent-tabs-mode: nil +# tab-width: 4 +# End: Added: trunk/include/asycxx/RAWCANListener.h =================================================================== --- trunk/include/asycxx/RAWCANListener.h (rev 0) +++ trunk/include/asycxx/RAWCANListener.h 2009-03-23 08:45:18 UTC (rev 18) @@ -0,0 +1,60 @@ + +/******************************************************************** + * 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__ + +#ifdef HAVE_CONFIG_H +#include <asycxx-config.h> +#endif + +#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: +*/ Added: trunk/include/asycxx/RAWCANTransport.h =================================================================== --- trunk/include/asycxx/RAWCANTransport.h (rev 0) +++ trunk/include/asycxx/RAWCANTransport.h 2009-03-23 08:45:18 UTC (rev 18) @@ -0,0 +1,48 @@ + +/******************************************************************** + * 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__ + +#ifdef HAVE_CONFIG_H +#include <asycxx-config.h> +#endif + +#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: +*/ Modified: trunk/src/Makefile.am =================================================================== --- trunk/src/Makefile.am 2009-03-17 09:24:06 UTC (rev 17) +++ trunk/src/Makefile.am 2009-03-23 08:45:18 UTC (rev 18) @@ -21,8 +21,11 @@ MsgBus.cxx \ MsgHandler.cxx \ GimpleMsgBus.cxx \ - core.cxx + core.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 Added: trunk/src/RAWCANListener.cxx =================================================================== --- trunk/src/RAWCANListener.cxx (rev 0) +++ trunk/src/RAWCANListener.cxx 2009-03-23 08:45:18 UTC (rev 18) @@ -0,0 +1,156 @@ +/******************************************************************** + * 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: +*/ Added: trunk/src/RAWCANTransport.cxx =================================================================== --- trunk/src/RAWCANTransport.cxx (rev 0) +++ trunk/src/RAWCANTransport.cxx 2009-03-23 08:45:18 UTC (rev 18) @@ -0,0 +1,138 @@ +/******************************************************************** + * 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: +*/ Modified: trunk/src/asycxx-common.h =================================================================== --- trunk/src/asycxx-common.h 2009-03-17 09:24:06 UTC (rev 17) +++ trunk/src/asycxx-common.h 2009-03-23 08:45:18 UTC (rev 18) @@ -35,19 +35,19 @@ #define dummyLog(fmt, ...) -#if HMI_CTRL_VERBOSE_LEVEL >= 1 +#if ASYCXX_VERBOSE_LEVEL >= 1 #define LOG1 LOG #else #define LOG1 dummyLog #endif -#if HMI_CTRL_VERBOSE_LEVEL >= 2 +#if ASYCXX_VERBOSE_LEVEL >= 2 #define LOG2 LOG #else #define LOG2 dummyLog #endif -#if HMI_CTRL_VERBOSE_LEVEL >= 3 +#if ASYCXX_VERBOSE_LEVEL >= 3 #define LOG3 LOG #else #define LOG3 dummyLog This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |