Skip to content

Commit

Permalink
Swdisconnect (#33)
Browse files Browse the repository at this point in the history
* protect global connection maps with mutexes against parallel access
* correct software close/open behaviour as possible without reloading the library. Delays are needed
 *  prefer reloading the library when software close/open
 *  works in general, but differences between modules/OS are to be noted, see Documentation/sphinx-result html tree
*   some bugs corrected related to reconnect
*   tested thouroughly with extra test suite (simpletest)
*    lots of commits as always because of X-build chain ;-)
*    shall become 1.1.9.5
  • Loading branch information
meeludwig authored Oct 23, 2019
1 parent 1f19c3b commit add0648
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 117 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## 1.1.9.5
- disconnect and reconnect on the same bus by software: connection map management corrected
- double-create of one bus protected
- double-close of one bus protected
- works for systec, peak and anagate @linux
- increased anagate open can bus timeout from 6 to 12 seconds
- force a delay of 7 sec. after anagate close bus to avoid fw crash too soon

## 1.1.9.4 [14.oct.2019]
- sync flag suppressed
- hi speed to true as default for anagate
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
#
cmake_minimum_required(VERSION 3.0)
project( CanModule LANGUAGES C CXX VERSION 1.1.9.4 ) # sets PROJECT_VERSION etc etc
project( CanModule LANGUAGES C CXX VERSION 1.1.9.5 ) # sets PROJECT_VERSION etc etc
message(STATUS " [${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_LIST_LINE}]: CanModule version= ${PROJECT_VERSION}" )
file(WRITE CanInterface/include/VERSION.h "// VERSION.h - do not edit\n#define CanModule_VERSION \"${PROJECT_VERSION}\"" )

Expand Down
49 changes: 38 additions & 11 deletions CanInterfaceImplementations/anagate/AnaCanScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

#include <time.h>
#include <string.h>

#include <map>
#include <LogIt.h>
#include <sstream>
#include <iostream>
#include <boost/thread/thread.hpp>

#include "CanModuleUtils.h"

#ifdef _WIN32
Expand Down Expand Up @@ -79,7 +80,7 @@ AnaCanScan::AnaCanScan():
m_busName(""),
m_busParameters(""),
m_UcanHandle(0),
m_timeout ( 6000 )
m_timeout ( 12000 )
{
m_statistics.beginNewRun();
}
Expand All @@ -89,13 +90,29 @@ AnaCanScan::AnaCanScan():
*/
AnaCanScan::~AnaCanScan()
{
LOG(Log::TRC, AnaCanScan::s_logItHandleAnagate ) << "Closing down Anagate Can Scan component";
stopBus();
}

void AnaCanScan::stopBus ()
{
MLOGANA(TRC,this) << __FUNCTION__ << " m_busName= " << m_busName << " m_canPortNumber= " << m_canPortNumber;
CANSetCallback(m_UcanHandle, 0);
CANCloseDevice(m_UcanHandle);
LOG(Log::TRC, AnaCanScan::s_logItHandleAnagate ) << "Anagate Can Scan component closed successfully";

setCanHandleInUse(m_canPortNumber,false);
eraseReceptionHandlerFromMap( m_UcanHandle );

#ifdef _WIN32
#else
MLOGANA(TRC, this ) << " imposing a delay of 7 seconds before continuing";
boost::this_thread::sleep_for(boost::chrono::milliseconds( 7000 ));
#endif
MLOGANA(TRC,this) << " finished";
}


/* static */ void AnaCanScan::setIpReconnectInProgress( string ip, bool flag ){
anagateReconnectMutex.lock();
std::map<string,bool>::iterator it = AnaCanScan::reconnectInProgress_map.find( ip );

if ( flag ){
Expand All @@ -109,14 +126,17 @@ AnaCanScan::~AnaCanScan()
AnaCanScan::reconnectInProgress_map.erase( it );
}
}
anagateReconnectMutex.unlock();
}

/* static */ bool AnaCanScan::isIpReconnectInProgress( string ip ){
anagateReconnectMutex.lock();
std::map<string,bool>::iterator it = AnaCanScan::reconnectInProgress_map.find( ip );
if ( it == AnaCanScan::reconnectInProgress_map.end() )
return( false );
else
return( true );
anagateReconnectMutex.unlock();
}


Expand Down Expand Up @@ -218,7 +238,6 @@ bool AnaCanScan::createBus(const string name,const string parameters)
//LOG(Log::TRC, myHandle) << __FUNCTION__ << " " __FILE__ << " " << __LINE__;
AnaCanScan::s_logItHandleAnagate = myHandle;

m_sBusName = name;
MLOGANA(DBG, this) << " parameters= " << parameters;
int returnCode = configureCanBoard(name, parameters);
if ( returnCode < 0 ) {
Expand All @@ -230,7 +249,7 @@ bool AnaCanScan::createBus(const string name,const string parameters)


/**
* decode the name, parameter and return the port to the configured module
* decode the name, parameter and return the port of the configured module
*/
int AnaCanScan::configureCanBoard(const string name,const string parameters)
{
Expand Down Expand Up @@ -276,7 +295,7 @@ int AnaCanScan::configureCanBoard(const string name,const string parameters)
// or by decoding. They are always used.
} else {
MLOGANA(ERR, this) << "Error while parsing parameters: this syntax is incorrect: [" << parameters << "]";
MLOGANA(ERR, this) << "you need up to 5 numbers separated by whitespaces, i.e. \"125000 0 0 0 0 0\" \"p0 p1 p2 p3 p4\"";
MLOGANA(ERR, this) << "you need up to 5 numbers separated by whitespaces, i.e. \"125000 0 0 0 0\" \"p0 p1 p2 p3 p4\"";
MLOGANA(ERR, this) << " p0 = baud rate, 125000 or whatever the module supports";
MLOGANA(ERR, this) << " p1 = operation mode";
MLOGANA(ERR, this) << " p2 = termination";
Expand All @@ -291,8 +310,7 @@ int AnaCanScan::configureCanBoard(const string name,const string parameters)
}

/**
*
* Obtains a Anagate canport and opens it.
* 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
*
Expand All @@ -312,7 +330,7 @@ int AnaCanScan::openCanPort()
} else {
//Otherwise we create it.
MLOGANA(DBG, this) << "Will call CANOpenDevice with parameters m_canHandleNumber:[" << m_canPortNumber << "], m_canIPAddress:[" << m_canIPAddress << "]";
anaCallReturn = CANOpenDevice(&canModuleHandle, FALSE, TRUE, m_canPortNumber, m_canIPAddress.c_str(),m_timeout);
anaCallReturn = CANOpenDevice(&canModuleHandle, FALSE, TRUE, m_canPortNumber, m_canIPAddress.c_str(), m_timeout);
if (anaCallReturn != 0) {
// fill out initialisation struct
MLOGANA(ERR,this) << "Error in CANOpenDevice, return code = [" << anaCallReturn << "]";
Expand All @@ -323,7 +341,6 @@ int AnaCanScan::openCanPort()
setCanHandleInUse(m_canPortNumber,true);

// initialize CAN interface

MLOGANA(TRC,this) << "calling CANSetGlobals with m_lBaudRate= "
<< m_CanParameters.m_lBaudRate
<< " m_iOperationMode= " << m_CanParameters.m_iOperationMode
Expand Down Expand Up @@ -650,6 +667,16 @@ AnaInt32 AnaCanScan::connectReceptionHandler(){
}


void AnaCanScan::eraseReceptionHandlerFromMap( AnaInt32 h ){
std::map<AnaInt32, AnaCanScan *>::iterator it = g_AnaCanScanPointerMap.find( h );
if (it != g_AnaCanScanPointerMap.end()) {
g_AnaCanScanPointerMap.erase ( it );
m_busName = "nobus";
} else {
MLOGANA(DBG,this) << " handler " << h << " not found in map, not erased";
}
}

/**
* we try to reconnect one port after a power loss, and we should do this for all ports
* returns:
Expand Down
4 changes: 3 additions & 1 deletion CanInterfaceImplementations/anagate/AnaCanScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#ifndef CCANANASCAN_H_
#define CCANANASCAN_H_

#include <boost/thread/thread.hpp> // just needed for the boost sleep

#include <string>
#include "CanStatistics.h"
Expand Down Expand Up @@ -118,6 +117,9 @@ class AnaCanScan: public CanModule::CCanAccess
int openCanPort();
int reconnect();
bool errorCodeToString(long error, char message[]);
void stopBus( void );
void eraseReceptionHandlerFromMap( AnaInt32 h );

};

#endif //CCANANASCAN_H_
Loading

0 comments on commit add0648

Please sign in to comment.