OpcUaCanOpen
CANopen OPC-UA server
AnaCanScan Class Reference

#include <AnaCanScan.h>

Inherits CanModule::CCanAccess.

Public Member Functions

 AnaCanScan ()
 
 AnaCanScan (AnaCanScan const &other)=delete
 
AnaCanScanoperator= (AnaCanScan const &other)=delete
 
virtual ~AnaCanScan ()
 Shut down can scan thread. More...
 
virtual bool createBus (const string name, const string parameters)
 Method that initialises a CAN bus channel for anagate. More...
 
virtual bool sendMessage (short cobID, unsigned char len, unsigned char *message, bool rtr=false)
 send a CAN message frame (8 byte) for anagate Method that sends a message through the can bus channel. More...
 
virtual bool sendMessage (CanMessage *)
 
virtual bool sendRemoteRequest (short cobID)
 Method that sends a remote request trough the can bus channel. More...
 
virtual void getStatistics (CanStatistics &result)
 
void statisticsOnRecieve (int)
 callback API More...
 
void callbackOnRecieve (CanMessage &)
 callback API More...
 
void startAlive (int aliveTime_sec)
 starts explicitly the anagate keepalive mechanism More...
 
void setConnectWaitTime (int timeout_ms)
 set a connection specific timeout value. More...
 
- Public Member Functions inherited from CanModule::CCanAccess
 CCanAccess ()
 
std::string & getBusName ()
 
virtual ~CCanAccess ()
 
bool initialiseLogging (LogItInstance *remoteInstance)
 
LogItInstancegetLogItInstance ()
 the LogIt instance is NOT shared by inheritance in windows, the instance has to be passed explicitly from the parent More...
 
vector< string > parseNameAndParameters (string name, string parameters)
 

Static Public Member Functions

static std::string canMessageToString (CanMessage &f)
 

Static Public Attributes

static Log::LogComponentHandle st_logItHandleAnagate = 0
 

Private Member Functions

bool sendErrorCode (AnaInt32)
 
string ipAdress ()
 
int canPortNumber ()
 
int handle ()
 
int configureCanBoard (const string name, const string parameters)
 decode the name, parameter and return the port of the configured module More...
 
int connectReceptionHandler ()
 connect the reception handler and only AFTER that register the handle in the global obj. More...
 
int openCanPort ()
 Obtains an Anagate canport and opens it. More...
 
int reconnect ()
 we try to reconnect one port after a power loss, and we should do this for all ports returns: 0 = OK -1 = could not CANOpenDevice device -2 = could not connect callback CANSetCallbackEx -3 = device was found open, tried to close but did not succeed (maybe you should just try sending again) More...
 
bool errorCodeToString (long error, char message[])
 Provides textual representation of an error code. More...
 
void stopBus (void)
 
void eraseReceptionHandlerFromMap (AnaInt32 h)
 

Static Private Member Functions

static void objectMapSize ()
 
static int reconnectAllPorts (string ip)
 reconnects all ports of a given ip adress=one module. More...
 
static void setCanHandleInUse (int handle, bool flag)
 we have one object for each CAN port, each object has an unique handle. More...
 
static bool isCanHandleInUse (int handle)
 
static void setCanHandleOfPort (int port, AnaInt32 handle)
 
static AnaInt32 getCanHandleFromPort (int n)
 
static void setIpReconnectInProgress (string ip, bool flag)
 
static bool isIpReconnectInProgress (string ip)
 

Private Attributes

int m_canPortNumber
 
string m_canIPAddress
 
unsigned int m_baudRate
 
DWORD m_idCanScanThread
 
bool m_canCloseDevice
 
string m_busName
 
string m_busParameters
 
AnaInt32 m_UcanHandle
 
CanStatistics m_statistics
 
AnaInt32 m_timeout
 

Static Private Attributes

static AnaInt32 s_canHandleArray [256]
 we would like to keep logging from a few static methods as well, since one IP number corresponds to many ports and some class-level management is going on for the connect-reconnect behavior. More...
 
static bool s_isCanHandleInUseArray [256]
 
static std::map< string, bool > reconnectInProgress_map
 

Additional Inherited Members

- Public Attributes inherited from CanModule::CCanAccess
boost::signals2::signal< void(const CanMessage &) > canMessageCame
 
boost::signals2::signal< void(const int, const char *, timeval &) > canMessageError
 
- Protected Attributes inherited from CanModule::CCanAccess
string m_sBusName
 
CanParameters m_CanParameters
 

Detailed Description

Definition at line 57 of file AnaCanScan.h.

Constructor & Destructor Documentation

◆ AnaCanScan() [1/2]

AnaCanScan::AnaCanScan ( )

Definition at line 73 of file AnaCanScan.cpp.

◆ AnaCanScan() [2/2]

AnaCanScan::AnaCanScan ( AnaCanScan const &  other)
delete

◆ ~AnaCanScan()

AnaCanScan::~AnaCanScan ( )
virtual

Shut down can scan thread.

Definition at line 90 of file AnaCanScan.cpp.

Member Function Documentation

◆ callbackOnRecieve()

void AnaCanScan::callbackOnRecieve ( CanMessage msg)

callback API

Definition at line 202 of file AnaCanScan.cpp.

◆ canMessageToString()

std::string AnaCanScan::canMessageToString ( CanMessage f)
static

Definition at line 176 of file AnaCanScan.cpp.

◆ canPortNumber()

int AnaCanScan::canPortNumber ( )
inlineprivate

Definition at line 125 of file AnaCanScan.h.

◆ configureCanBoard()

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

decode the name, parameter and return the port of the configured module

Definition at line 264 of file AnaCanScan.cpp.

◆ connectReceptionHandler()

AnaInt32 AnaCanScan::connectReceptionHandler ( )
private

connect the reception handler and only AFTER that register the handle in the global obj.

map, since reception is asynchron. Finally, in the caller, the stale handle is deleted from the map.

Definition at line 631 of file AnaCanScan.cpp.

◆ createBus()

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

Method that initialises a CAN bus channel for anagate.

All following methods called on the same object will be using this initialized channel.

Parameters
name3 parameters separated by ":" like "n0:n1:n2" n0 = "an" for anagate n1 = port number on the module, 0=A, 1=B, etc etc n2 = ip number ex.: "an:can1:137.138.12.99" speaks to port B (=1) on anagate module at the ip ex.: "an:1:137.138.12.99" works as well
parametersup to 5 parameters separated by whitespaces : "p0 p1 p2 p3 p4" in THAT order, positive integers
  • "Unspecified" (or empty): using defaults = "125000 0 0 0 0" // all params missing
  • p0: bitrate: 10000, 20000, 50000, 62000, 100000, 125000, 250000, 500000, 800000, 1000000 bit/s
  • p1: operatingMode: 0=default mode, values 1 (loop back) and 2 (listen) are not supported by CanModule
  • p2: termination: 0=not terminated (default), 1=terminated (120 Ohm for CAN bus)
  • p3: highSpeed: 0=deactivated (default), 1=activated. If activated, confirmation and filtering of CAN traffic are switched off
  • p4: TimeStamp: 0=deactivated (default), 1=activated. If activated, a timestamp is added to the CAN frame. Not all modules support this. i.e. "250000 0 1 0 0" (see anagate manual for more details)
Returns
was the initialisation process successful?

Implements CanModule::CCanAccess.

Definition at line 230 of file AnaCanScan.cpp.

◆ eraseReceptionHandlerFromMap()

void AnaCanScan::eraseReceptionHandlerFromMap ( AnaInt32  h)
private

Definition at line 680 of file AnaCanScan.cpp.

◆ errorCodeToString()

bool AnaCanScan::errorCodeToString ( long  error,
char  message[] 
)
private

Provides textual representation of an error code.

error return from module

Todo:
: Fix AnaCanScan::errorCodeToString method, this doesn't do anything!!

Definition at line 836 of file AnaCanScan.cpp.

◆ getCanHandleFromPort()

static AnaInt32 AnaCanScan::getCanHandleFromPort ( int  n)
inlinestaticprivate

Definition at line 116 of file AnaCanScan.h.

◆ getStatistics()

void AnaCanScan::getStatistics ( CanStatistics result)
virtual

Implements CanModule::CCanAccess.

Definition at line 846 of file AnaCanScan.cpp.

◆ handle()

int AnaCanScan::handle ( )
inlineprivate

Definition at line 126 of file AnaCanScan.h.

◆ ipAdress()

string AnaCanScan::ipAdress ( )
inlineprivate

Definition at line 124 of file AnaCanScan.h.

◆ isCanHandleInUse()

static bool AnaCanScan::isCanHandleInUse ( int  handle)
inlinestaticprivate

Definition at line 114 of file AnaCanScan.h.

◆ isIpReconnectInProgress()

bool AnaCanScan::isIpReconnectInProgress ( string  ip)
staticprivate

Definition at line 130 of file AnaCanScan.cpp.

◆ objectMapSize()

void AnaCanScan::objectMapSize ( )
staticprivate

Definition at line 482 of file AnaCanScan.cpp.

◆ openCanPort()

int AnaCanScan::openCanPort ( )
private

Obtains an Anagate canport 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

communicate with the hardware using the CanOpen interface: open anagate port, attach reply handler. No message queuing. CANSetMaxSizePerQueue not called==default==0

Definition at line 331 of file AnaCanScan.cpp.

◆ operator=()

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

◆ reconnect()

int AnaCanScan::reconnect ( )
private

we try to reconnect one port after a power loss, and we should do this for all ports returns: 0 = OK -1 = could not CANOpenDevice device -2 = could not connect callback CANSetCallbackEx -3 = device was found open, tried to close but did not succeed (maybe you should just try sending again)


• 1 = DISCONNECTED : The connection to the AnaGate is disconnected. • 2 = CONNECTING : The connection is connecting. • 3 = CONNECTED : The connection is established. • 4 = DISCONNECTING : The connection is disonnecting. • 5 = NOT_INITIALIZED : The network protocol is not successfully initialized.

Definition at line 698 of file AnaCanScan.cpp.

◆ reconnectAllPorts()

AnaInt32 AnaCanScan::reconnectAllPorts ( string  ip)
staticprivate

reconnects all ports of a given ip adress=one module.

We have one object per CAN port, so i.e. for an anagate duo (ports A and B) we have two objects with the same IP but with port 0 and 1.So we will have to scan over all objects there because we do not know how many CANports a given anagate bridge has. We also allow globally only one ongoing reconnect to one ip at a time. Theoretically we could have a separate reconnection thread per ip, but that looks like overcomplicating the code and the issue. If several anagate modules fail they will all reconnect one after the other. That is OK since if all of these modules loose power and reboot at the same moment they will also be ready at roughly the same moment for reconnect. That means that we have to wait for the first ip to reconnect for a while but then all other ips will reconnect rather quickly in the most likely case. returns: 0 = OK -1 = cant open / reconnect CAN ports +1 = ignore, since another thread is doing the reconnect already

delete the map elements where the key is different from the handle, since we reassigned handles in the living objects like: g_AnaCanScanPointerMap[ m_UcanHandle ] = this;

Definition at line 513 of file AnaCanScan.cpp.

◆ sendErrorCode()

bool AnaCanScan::sendErrorCode ( AnaInt32  status)
private

Definition at line 403 of file AnaCanScan.cpp.

◆ sendMessage() [1/2]

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

send a CAN message frame (8 byte) for anagate Method that sends a message through 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 trough.

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 sending process successful?

todo anyway extended can messages are not treated here yet

a sending on one port failed miserably. we are pessimistic and assume that the whole module (=ip) with all ports has lost power. Therefore we try to reconnect all used ports of this ip. This is a bit brute, but it will work. Nevertheless we should limit our efforts to this module=ip only and not reconnect all other modules managed by this task. we also avoid reconnecting all ports of an ip several times (for each failed send on a port)

Implements CanModule::CCanAccess.

Definition at line 432 of file AnaCanScan.cpp.

◆ sendMessage() [2/2]

bool AnaCanScan::sendMessage ( CanMessage canm)
virtual

Reimplemented from CanModule::CCanAccess.

Definition at line 807 of file AnaCanScan.cpp.

◆ sendRemoteRequest()

bool AnaCanScan::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?

Implements CanModule::CCanAccess.

Definition at line 819 of file AnaCanScan.cpp.

◆ setCanHandleInUse()

static void AnaCanScan::setCanHandleInUse ( int  handle,
bool  flag 
)
inlinestaticprivate

we have one object for each CAN port, each object has an unique handle.

Several objects thus share the same ip. CAN ports and object handles are therefore equivalent, except that the handle value is assigned by the OS at CANopen.

Definition at line 113 of file AnaCanScan.h.

◆ setCanHandleOfPort()

static void AnaCanScan::setCanHandleOfPort ( int  port,
AnaInt32  handle 
)
inlinestaticprivate

Definition at line 115 of file AnaCanScan.h.

◆ setConnectWaitTime()

void AnaCanScan::setConnectWaitTime ( int  timeout_ms)

set a connection specific timeout value.

Global timeout is 6000, set by CanOpenDevice In order to make this specific timeout apply we have to reconnect

Definition at line 788 of file AnaCanScan.cpp.

◆ setIpReconnectInProgress()

void AnaCanScan::setIpReconnectInProgress ( string  ip,
bool  flag 
)
staticprivate

Definition at line 113 of file AnaCanScan.cpp.

◆ startAlive()

void AnaCanScan::startAlive ( int  aliveTime_sec)

starts explicitly the anagate keepalive mechanism

Todo:
not sure we need this actually

Definition at line 798 of file AnaCanScan.cpp.

◆ statisticsOnRecieve()

void AnaCanScan::statisticsOnRecieve ( int  bytes)

callback API

Definition at line 194 of file AnaCanScan.cpp.

◆ stopBus()

void AnaCanScan::stopBus ( void  )
private

Definition at line 95 of file AnaCanScan.cpp.

Member Data Documentation

◆ m_baudRate

unsigned int AnaCanScan::m_baudRate
private

Definition at line 83 of file AnaCanScan.h.

◆ m_busName

string AnaCanScan::m_busName
private

Definition at line 86 of file AnaCanScan.h.

◆ m_busParameters

string AnaCanScan::m_busParameters
private

Definition at line 87 of file AnaCanScan.h.

◆ m_canCloseDevice

bool AnaCanScan::m_canCloseDevice
private

Definition at line 85 of file AnaCanScan.h.

◆ m_canIPAddress

string AnaCanScan::m_canIPAddress
private

Definition at line 82 of file AnaCanScan.h.

◆ m_canPortNumber

int AnaCanScan::m_canPortNumber
private

Definition at line 81 of file AnaCanScan.h.

◆ m_idCanScanThread

DWORD AnaCanScan::m_idCanScanThread
private

Definition at line 84 of file AnaCanScan.h.

◆ m_statistics

CanStatistics AnaCanScan::m_statistics
private

Definition at line 89 of file AnaCanScan.h.

◆ m_timeout

AnaInt32 AnaCanScan::m_timeout
private

Definition at line 90 of file AnaCanScan.h.

◆ m_UcanHandle

AnaInt32 AnaCanScan::m_UcanHandle
private

Definition at line 88 of file AnaCanScan.h.

◆ reconnectInProgress_map

std::map< string, bool > AnaCanScan::reconnectInProgress_map
staticprivate

Definition at line 119 of file AnaCanScan.h.

◆ s_canHandleArray

AnaInt32 AnaCanScan::s_canHandleArray
staticprivate

we would like to keep logging from a few static methods as well, since one IP number corresponds to many ports and some class-level management is going on for the connect-reconnect behavior.

We definitely need these to be static. So since we need the logit handle static there is no point in having it also private. At least the cross-thread references to the handler are within this class only. It is all re-entrant code, and the handler does not change during runtime any more, so no mutex protection for now.

Definition at line 102 of file AnaCanScan.h.

◆ s_isCanHandleInUseArray

bool AnaCanScan::s_isCanHandleInUseArray
staticprivate

Definition at line 103 of file AnaCanScan.h.

◆ st_logItHandleAnagate

Log::LogComponentHandle AnaCanScan::st_logItHandleAnagate = 0
static

Definition at line 78 of file AnaCanScan.h.


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