OpcUaCanOpen
CANopen OPC-UA server
CSockCanScan Class Reference

#include <SockCanScan.h>

Inherits CCanAccess, CCanAccess, and CCanAccess.

Public Member Functions

 CSockCanScan ()
 
virtual ~CSockCanScan ()
 
virtual bool sendMessage (short cobID, unsigned char len, unsigned char *message)
 
virtual bool sendRemoteRequest (short cobID)
 Method that sends a remote request trough the can bus channel. More...
 
virtual bool createBUS (const char *name, const char *parameters)
 
string & getNamePort ()
 
int getHandler ()
 
string & getChannel ()
 
 CSockCanScan ()
 
 CSockCanScan (CSockCanScan const &other)=delete
 
CSockCanScanoperator= (CSockCanScan const &other)=delete
 
virtual ~CSockCanScan ()
 
virtual bool sendRemoteRequest (short cobID)
 
virtual bool createBus (const string name, string parameters)
 Method that initializes a can bus channel. More...
 
virtual bool sendMessage (short cobID, unsigned char len, unsigned char *message, bool rtr=false)
 Method that sends a message trough the can bus channel. More...
 
int getHandler ()
 Returns socket handler. More...
 
virtual void getStatistics (CanStatistics &result)
 Returns the instance of the CanStatistics object. More...
 
Log::LogComponentHandle logItHandle ()
 
 CSockCanScan ()
 
int FinalConstruct (const char *port, const char *parameters, CCanCallback *)
 
string & getNamePort ()
 
int getHandler ()
 
int sendErrorCode (int)
 
virtual ~CSockCanScan ()
 
virtual bool sendMessage (short cobID, unsigned char len, unsigned char *message)
 
virtual bool sendRemoteRequest (short cobID)
 
virtual bool createBUS (const char *name, const char *parameters, CCanCallback *callback)
 
- Public Member Functions inherited from CCanAccess
 CCanAccess ()
 
std::string & getBusName ()
 
void setBusName (std::string &name)
 
virtual ~CCanAccess ()
 
 CCanAccess ()
 
virtual void setCallBack (CCanCallback *callback)
 
virtual bool getErrorMessage (long, char **)
 
int getCanBusHandler ()
 
void setCanBusHandler (int h)
 
char * getBusName ()
 
virtual ~CCanAccess ()
 

Static Public Member Functions

static can_frame emptyCanFrame (void)
 produce and empty can frame More...
 

Public Attributes

int numModule
 
int numChannel
 
int numCanHandle
 
CCanCallbackcb
 
- Public Attributes inherited from CCanAccess
boost::signals2::signal< void(const canMessage &) > canMessageCame
 
boost::signals2::signal< void(const int, const char *, timeval &) > canMessageError
 

Static Public Attributes

static std::map< string, string > m_busMap
 © Copyright CERN, 2015. More...
 

Private Member Functions

void sendErrorMessage (const char *)
 
void sendErrorMessage (const struct can_frame *)
 
void clearErrorMessage ()
 
int configureCanboard (const char *, const char *)
 
int openCanPort ()
 stop, set bitrate, start a CAN port, open a socket for it, set the socket to CAN, bind it and check any errors More...
 
void waitCanDriver ()
 
bool stopBus ()
 notify the main thread to finish and delete the bus from the map of connections More...
 
void updateInitialError ()
 
void sendErrorMessage (const char *)
 
void clearErrorMessage ()
 
int configureCanBoard (const string name, const string parameters)
 
int openCanPort ()
 Obtains a SocketCAN socket and opens it. More...
 
int configureCanboard (const char *, const char *)
 

Static Private Member Functions

static void * CanScanControlThread (void *)
 reading from socket, and supervising thread for monitoring the sockets/CAN buses. More...
 
static std::string errorFrameToString (const struct can_frame &f)
 
static void * CanScanControlThread (void *)
 
static void * CanScanControlThread (void *)
 

Private Attributes

bool run_can
 
int sock
 
pthread_t m_hCanScanThread
 
int m_idCanScanThread
 
string name_of_port
 
string channelName
 
string param
 
volatile bool m_CanScanThreadRunEnableFlag
 
int m_sock
 
CanStatistics m_statistics
 
int m_errorCode
 
std::string m_channelName
 
std::string m_busName
 
Log::LogComponentHandle m_logItHandleSock
 

Additional Inherited Members

- Protected Attributes inherited from CCanAccess
char * busName
 
int handler
 
CCanCallbackcb
 

Detailed Description

Definition at line 21 of file SockCanScan.h.

Constructor & Destructor Documentation

◆ CSockCanScan() [1/4]

CSockCanScan::CSockCanScan ( )
inline

Definition at line 25 of file SockCanScan.h.

◆ ~CSockCanScan() [1/3]

CSockCanScan::~CSockCanScan ( )
virtual

Definition at line 148 of file SockCanScan.cpp.

◆ CSockCanScan() [2/4]

CSockCanScan::CSockCanScan ( )

◆ CSockCanScan() [3/4]

CSockCanScan::CSockCanScan ( CSockCanScan const &  other)
delete

◆ ~CSockCanScan() [2/3]

virtual CSockCanScan::~CSockCanScan ( )
virtual

◆ CSockCanScan() [4/4]

CSockCanScan::CSockCanScan ( )
inline

Definition at line 23 of file SockCanScan.h.

◆ ~CSockCanScan() [3/3]

virtual CSockCanScan::~CSockCanScan ( )
virtual

Member Function Documentation

◆ CanScanControlThread() [1/3]

static void* CSockCanScan::CanScanControlThread ( void *  )
staticprivate

◆ CanScanControlThread() [2/3]

void * CSockCanScan::CanScanControlThread ( void *  p_voidSockCanScan)
staticprivate

reading from socket, and supervising thread for monitoring the sockets/CAN buses.

It takes an object reference (cast) and listens with a select call on that socket/object. The select runs with 1Hz, and if there is nothing to receive it should timeout.

the select failed. that is very bad, system problem, but let's continue nevertheless. There is not much we can do

select returned, either on the timeout or with something on that socket/object. Assumption: at this moment sock holds meaningful value. Now select result >=0 so it was either nothing received (timeout) or something received

The preceding call took either 'timeout' time, or there is frame received – perfect time to attempt to clean error frame.

select reports that it has got something, so no timeout in this case

try to re-open the socket. only reopen the socket if we still need it. If we need to shutdown the thread, leave it closed and terminate main loop.

if we have an open socket again, and we can read numberOfReadBytes >=0 we should be fine again and continue the thread execution normally

we have a CAN message, error free, lets digest it

reformat and buffer the message from the socket. this actually should exclude more bits

the select got nothing to read, this was just a timeout.

Definition at line 48 of file SockCanScan.cpp.

◆ CanScanControlThread() [3/3]

static void* CSockCanScan::CanScanControlThread ( void *  )
staticprivate

◆ clearErrorMessage() [1/2]

void CSockCanScan::clearErrorMessage ( )
private

Definition at line 472 of file SockCanScan.cpp.

◆ clearErrorMessage() [2/2]

void CSockCanScan::clearErrorMessage ( )
private

◆ configureCanboard() [1/2]

int CSockCanScan::configureCanboard ( const char *  ,
const char *   
)
private

◆ configureCanboard() [2/2]

int CSockCanScan::configureCanboard ( const char *  name,
const char *  parameters 
)
private

Definition at line 161 of file SockCanScan.cpp.

◆ configureCanBoard()

int CSockCanScan::configureCanBoard ( const string  name,
const string  parameters 
)
private

Definition at line 311 of file SockCanScan.cpp.

◆ createBUS() [1/2]

bool CSockCanScan::createBUS ( const char *  name,
const char *  parameters 
)
virtual

Implements CCanAccess.

Definition at line 354 of file SockCanScan.cpp.

◆ createBUS() [2/2]

bool CSockCanScan::createBUS ( const char *  name,
const char *  parameters,
CCanCallback callback 
)
virtual

Implements CCanAccess.

Definition at line 206 of file SockCanScan.cpp.

◆ createBus()

bool CSockCanScan::createBus ( const string  name,
string  parameters 
)
virtual

Method that initializes a can bus channel.

The following methods called upon the same object will be using this initialized channel.

Parameters
name= 2 parameters separated by ":", like "n0:n1"
  • n0 = "sock" for sockets, used by systec and peak
  • n1 = CAN port number on the module, can be prefixed with "can"
  • ex.: "sock:can1" speaks to port 1 on systec or peak module
  • ex.: "sock:1" works as well
parametersone parameter: "p0", positive integer
  • "Unspecified" (or empty): using defaults = "125000" // params missing
  • p0: bitrate: 50000, 100000, 125000, 250000, 500000, 1000000 bit/s, other values might be allowed by the module
  • i.e. "250000"
Returns
Was the initialization process successful?

dont create a main thread for the same bus twice: if it exists already, just configure the board again this protects agains having multiple threads if a given port ports is opened several times: protects against erro/unusual runtime. If the connection is closed, it's main thread is stopped and joined, and the port is erased from the connection map. when the same port is opened again later on, a (new) main thread is created, and the connection is again added to the map.

Definition at line 540 of file SockCanScan.cpp.

◆ emptyCanFrame()

static can_frame CSockCanScan::emptyCanFrame ( void  )
inlinestatic

produce and empty can frame

Definition at line 72 of file SockCanScan.h.

◆ errorFrameToString()

std::string CSockCanScan::errorFrameToString ( const struct can_frame &  f)
staticprivate

Definition at line 585 of file SockCanScan.cpp.

◆ FinalConstruct()

int CSockCanScan::FinalConstruct ( const char *  port,
const char *  parameters,
CCanCallback callb 
)

Definition at line 71 of file SockCanScan.cpp.

◆ getChannel()

string& CSockCanScan::getChannel ( )
inline

Definition at line 35 of file SockCanScan.h.

◆ getHandler() [1/3]

int CSockCanScan::getHandler ( )
inline

Definition at line 26 of file SockCanScan.h.

◆ getHandler() [2/3]

int CSockCanScan::getHandler ( )
inline

Definition at line 34 of file SockCanScan.h.

◆ getHandler() [3/3]

int CSockCanScan::getHandler ( )
inline

Returns socket handler.

Definition at line 62 of file SockCanScan.h.

◆ getNamePort() [1/2]

string& CSockCanScan::getNamePort ( )
inline

Definition at line 25 of file SockCanScan.h.

◆ getNamePort() [2/2]

string& CSockCanScan::getNamePort ( )
inline

Definition at line 33 of file SockCanScan.h.

◆ getStatistics()

void CSockCanScan::getStatistics ( CanStatistics result)
virtual

Returns the instance of the CanStatistics object.

Definition at line 703 of file SockCanScan.cpp.

◆ logItHandle()

Log::LogComponentHandle CSockCanScan::logItHandle ( )
inline

Definition at line 82 of file SockCanScan.h.

◆ openCanPort() [1/2]

int CSockCanScan::openCanPort ( )
private

stop, set bitrate, start a CAN port, open a socket for it, set the socket to CAN, bind it and check any errors

Definition at line 180 of file SockCanScan.cpp.

◆ openCanPort() [2/2]

int CSockCanScan::openCanPort ( )
private

Obtains a SocketCAN socket and opens it.

The name of the port and parameters should have been specified by preceding call to configureCanboard()

Returns
less than zero in case of error, otherwise success

◆ operator=()

CSockCanScan& CSockCanScan::operator= ( CSockCanScan const &  other)
delete

◆ sendErrorCode()

int CSockCanScan::sendErrorCode ( int  status)

Definition at line 155 of file SockCanScan.cpp.

◆ sendErrorMessage() [1/3]

void CSockCanScan::sendErrorMessage ( const char *  mess)
private

Definition at line 484 of file SockCanScan.cpp.

◆ sendErrorMessage() [2/3]

void CSockCanScan::sendErrorMessage ( const struct can_frame *  errFrame)
private

Definition at line 369 of file SockCanScan.cpp.

◆ sendErrorMessage() [3/3]

void CSockCanScan::sendErrorMessage ( const char *  )
private

◆ sendMessage() [1/3]

bool CSockCanScan::sendMessage ( short  cobID,
unsigned char  len,
unsigned char *  message 
)
virtual

Implements CCanAccess.

Definition at line 271 of file SockCanScan.cpp.

◆ sendMessage() [2/3]

virtual bool CSockCanScan::sendMessage ( short  cobID,
unsigned char  len,
unsigned char *  message 
)
virtual

Implements CCanAccess.

◆ sendMessage() [3/3]

bool CSockCanScan::sendMessage ( short  cobID,
unsigned char  len,
unsigned char *  message,
bool  rtr = false 
)
virtual

Method that sends a message trough the can bus channel.

If the method createBUS was not called before this, sendMessage will fail, as there is no can bus channel to send a message through.

Parameters
cobIDIdentifier that will be used for the message.
lenLength of the message. If the message is bigger than 8 characters, it will be split into separate 8 characters messages.
messageMessage to be sent trough the can bus.
rtris the message a remote transmission request?
Returns
Was the initialisation process successful?

Definition at line 409 of file SockCanScan.cpp.

◆ sendRemoteRequest() [1/3]

bool CSockCanScan::sendRemoteRequest ( short  cobID)
virtual

Method that sends a remote request trough the can bus channel.

If the method createBUS was not called before this, sendMessage will fail, as there is no can bus channel to send the request trough. Similar to sendMessage, but it sends an special message reserved for requests.

Parameters
cobIDIdentifier that will be used for the request.
Returns
: Was the initialisation process successful?

that is not tested or used until now, and I am not sure it works

Implements CCanAccess.

Definition at line 319 of file SockCanScan.cpp.

◆ sendRemoteRequest() [2/3]

virtual bool CSockCanScan::sendRemoteRequest ( short  cobID)
virtual

Implements CCanAccess.

◆ sendRemoteRequest() [3/3]

virtual bool CSockCanScan::sendRemoteRequest ( short  cobID)
virtual

Implements CCanAccess.

◆ stopBus()

bool CSockCanScan::stopBus ( void  )
private

notify the main thread to finish and delete the bus from the map of connections

Definition at line 679 of file SockCanScan.cpp.

◆ updateInitialError()

void CSockCanScan::updateInitialError ( )
private

Definition at line 710 of file SockCanScan.cpp.

◆ waitCanDriver()

void CSockCanScan::waitCanDriver ( )
private

Definition at line 495 of file SockCanScan.cpp.

Member Data Documentation

◆ cb

CCanCallback* CSockCanScan::cb

Definition at line 40 of file SockCanScan.h.

◆ channelName

string CSockCanScan::channelName
private

Definition at line 55 of file SockCanScan.h.

◆ m_busMap

std::map< string, string > CSockCanScan::m_busMap
static

© Copyright CERN, 2015.

All rights not expressly granted are reserved.

SockCanScan.cpp

Created on: Jul 21, 2011 Based on work by vfilimon Rework and logging done by Piotr Nikiel piotr.nosp@m.@nik.nosp@m.iel.i.nosp@m.nfo mludwig at cern dot ch

This file is part of Quasar.

Quasar is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public Licence as published by the Free Software Foundation, either version 3 of the Licence.

Quasar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public Licence for more details.

You should have received a copy of the GNU Lesser General Public License along with Quasar. If not, see http://www.gnu.org/licenses/.

Definition at line 81 of file SockCanScan.h.

◆ m_busName

std::string CSockCanScan::m_busName
private

Definition at line 93 of file SockCanScan.h.

◆ m_CanScanThreadRunEnableFlag

volatile bool CSockCanScan::m_CanScanThreadRunEnableFlag
private

Definition at line 85 of file SockCanScan.h.

◆ m_channelName

std::string CSockCanScan::m_channelName
private

Definition at line 92 of file SockCanScan.h.

◆ m_errorCode

int CSockCanScan::m_errorCode
private

Definition at line 91 of file SockCanScan.h.

◆ m_hCanScanThread

pthread_t CSockCanScan::m_hCanScanThread
private

Definition at line 42 of file SockCanScan.h.

◆ m_idCanScanThread

int CSockCanScan::m_idCanScanThread
private

Definition at line 43 of file SockCanScan.h.

◆ m_logItHandleSock

Log::LogComponentHandle CSockCanScan::m_logItHandleSock
private

Definition at line 95 of file SockCanScan.h.

◆ m_sock

int CSockCanScan::m_sock
private

Definition at line 87 of file SockCanScan.h.

◆ m_statistics

CanStatistics CSockCanScan::m_statistics
private

Definition at line 88 of file SockCanScan.h.

◆ name_of_port

string CSockCanScan::name_of_port
private

Definition at line 54 of file SockCanScan.h.

◆ numCanHandle

int CSockCanScan::numCanHandle

Definition at line 37 of file SockCanScan.h.

◆ numChannel

int CSockCanScan::numChannel

Definition at line 36 of file SockCanScan.h.

◆ numModule

int CSockCanScan::numModule

Definition at line 35 of file SockCanScan.h.

◆ param

string CSockCanScan::param
private

Definition at line 56 of file SockCanScan.h.

◆ run_can

bool CSockCanScan::run_can
private

Definition at line 38 of file SockCanScan.h.

◆ sock

int CSockCanScan::sock
private

Definition at line 39 of file SockCanScan.h.


The documentation for this class was generated from the following files: