35 #include <boost/thread/thread.hpp> 41 #define DLLEXPORTFLAG __declspec(dllexport) 63 #define MLOGANA(LEVEL,THIS) LOG(Log::LEVEL, AnaCanScan::st_logItHandleAnagate) << __FUNCTION__ << " " << " anagate bus= " << THIS->getBusName() << " " 75 m_canIPAddress( (char *)
string(
"192.168.1.2").c_str()),
78 m_canCloseDevice(false),
106 MLOGANA(
TRC,
this ) <<
" imposing a delay of 7 seconds before continuing";
107 boost::this_thread::sleep_for(boost::chrono::milliseconds( 7000 ));
154 void WINAPI InternalCallback(AnaUInt32 nIdentifier,
const char * pcBuffer, AnaInt32 nBufferLen, AnaInt32 nFlags, AnaInt32 hHandle, AnaInt32 nSeconds, AnaInt32 nMicroseconds)
157 if (nFlags == 2)
return;
158 canMsgCopy.
c_id = nIdentifier;
159 canMsgCopy.
c_dlc = nBufferLen;
160 canMsgCopy.
c_ff = nFlags;
161 for (
int i = 0; i < nBufferLen; i++)
162 canMsgCopy.
c_data[i] = pcBuffer[i];
179 result =
"[id=0x"+CanModuleUtils::toHexString(
f.c_id, 3,
'0')+
" ";
180 if (
f.c_id &
f.c_rtr)
182 result+=
"dlc=" + CanModuleUtils::toString(
int(
f.c_dlc)) +
" data=[";
184 for (
int i=0; i<
f.c_dlc; i++)
185 result+= CanModuleUtils::toHexString((
unsigned int)
f.c_data[i], 2,
'0')+
" ";
237 LogItInstance* logItInstance = CCanAccess::getLogItInstance();
242 std::cout << __FILE__ <<
" " << __LINE__ <<
" " << __FUNCTION__
243 <<
" could not set LogIt instance" << std::endl;
247 std::cout << __FILE__ <<
" " << __LINE__ <<
" " << __FUNCTION__
253 if ( returnCode < 0 ) {
256 MLOGANA(
DBG,
this) <<
" OK, Bus created with name= " <<
name <<
" parameters= " << parameters;
266 MLOGANA(
DBG,
this) <<
"(user supplied) name= " <<
name <<
" parameters= " << parameters;
267 vector<string> stringVector;
272 if ( stringVector.size() != 3 ){
273 MLOGANA(
ERR,
this) <<
" need exactly 3 elements delimited by \":\" like \"an:0:128.141.159.194\", got " 274 << stringVector.size();
275 for (
unsigned i = 0; i < stringVector.size(); i++ ){
276 MLOGANA(
ERR,
this) <<
" stringVector[" << i <<
"]= " << stringVector[ i ];
280 for (
unsigned i = 0; i < stringVector.size(); i++ ){
281 MLOGANA(
TRC,
this) <<
"(cleaned up) stringVector[" << i <<
"]= " << stringVector[ i ];
291 if (strcmp(parameters.c_str(),
"Unspecified") != 0) {
307 MLOGANA(
ERR,
this) <<
"Error while parsing parameters: this syntax is incorrect: [" << parameters <<
"]";
308 MLOGANA(
ERR,
this) <<
"you need up to 5 numbers separated by whitespaces, i.e. \"125000 0 0 0 0\" \"p0 p1 p2 p3 p4\"";
309 MLOGANA(
ERR,
this) <<
" p0 = baud rate, 125000 or whatever the module supports";
317 MLOGANA(
INF,
this) <<
"Unspecified parameters, default values will be used.";
333 AnaInt32 anaCallReturn;
334 AnaInt32 canModuleHandle;
344 if (anaCallReturn != 0) {
346 MLOGANA(
ERR,
this) <<
"Error in CANOpenDevice, return code = [" << anaCallReturn <<
"]";
354 MLOGANA(
TRC,
this) <<
"calling CANSetGlobals with m_lBaudRate= " 363 switch ( anaCallReturn ){
366 MLOGANA(
ERR,
this) <<
"Connection to TCP/IP partner can't be established or is disconnected. Lost TCP/IP: 0x" << hex << anaCallReturn << dec;
370 MLOGANA(
ERR,
this) <<
"No answer was received from TCP/IP partner within the defined timeout. Lost TCP/IP: 0x" << hex << anaCallReturn << dec;
374 MLOGANA(
ERR,
this) <<
"Invalid device handle. Lost TCP/IP: 0x" << hex << anaCallReturn << dec;
378 MLOGANA(
ERR,
this) <<
"Other Error in CANSetGlobals: 0x" << hex << anaCallReturn << dec;
384 if (anaCallReturn != 0) {
385 MLOGANA(
ERR,
this) <<
"Error in CANSetCallbackEx, return code = [" << anaCallReturn <<
"]";
388 MLOGANA(
TRC,
this) <<
"OK CANSetCallbackEx, return code = [" << anaCallReturn <<
"]";
405 char errorMessage[120];
408 auto now = std::chrono::system_clock::now();
410 duration_cast<std::chrono::microseconds>(
411 now.time_since_epoch()
413 ftTimeStamp.tv_sec = nMicrosecs.count() / 1000000L;
414 ftTimeStamp.tv_usec = (nMicrosecs.count() % 1000000L) ;
434 MLOGANA(
DBG,
this) <<
"Sending message: [" << ( message == 0 ?
"" : (
const char *) message) <<
"], cobID: [" << cobID <<
"], Message Length: [" <<
static_cast<int>(len) <<
"]";
435 AnaInt32 anaCallReturn;
436 unsigned char *messageToBeSent[8];
437 AnaInt32
flags = 0x0;
441 int messageLengthToBeProcessed;
446 messageLengthToBeProcessed = 8;
447 MLOGANA(
DBG,
this) <<
"The length is more than 8 bytes, ignoring overhead, no extended CAN msg yet " << len;
449 messageLengthToBeProcessed = len;
452 memcpy(messageToBeSent, message, messageLengthToBeProcessed);
454 <<
" cobID= " << cobID
455 <<
" length = " << messageLengthToBeProcessed
457 anaCallReturn = CANWrite(
m_UcanHandle, cobID, reinterpret_cast<char*>(messageToBeSent), messageLengthToBeProcessed,
flags);
458 if (anaCallReturn != 0) {
459 MLOGANA(
ERR,
this) <<
"There was a problem when sending a message with CANWrite: 0x" << hex << anaCallReturn << dec;
471 while ( anaCallReturn0 < 0 ){
472 MLOGANA(
WRN,
this) <<
"failed to reconnect all module CAN ports once, keep on trying to reconnect. ip= " <<
m_canIPAddress;
487 <<
" key= " << it->first
488 <<
" ip= " << it->second->ipAdress()
489 <<
" CAN port= " << it->second->canPortNumber()
490 <<
" handle= " << it->second->handle()
491 <<
" (used?= " << it->second->isCanHandleInUse(it->second->handle()) <<
")" 492 <<
" ptr= 0x" << hex << (
unsigned long) (it->second) << dec;
521 <<
" is already in progress, skipping.";
524 boost::this_thread::sleep(boost::posix_time::microseconds( us ));
531 <<
" is now in progress.";
540 int nbCANportsOnModule = 0;
541 bool reconnectFailed =
false;
543 if ( ip == it->second->ipAdress() ){
545 <<
" key= " << it->first
546 <<
" found ip= " << ip
547 <<
" for CAN port= " << it->second->canPortNumber()
548 <<
" reconnecting...";
550 ret = it->second->reconnect();
553 <<
" key= " << it->first
554 <<
" found ip= " << ip
555 <<
" for CAN port= " << it->second->canPortNumber()
556 <<
" failed to reconnect";
557 reconnectFailed =
true;
559 nbCANportsOnModule++;
564 if ( reconnectFailed ) {
566 <<
" last ret= " << ret <<
". Just abandoning and trying again in 10 secs, module might not be ready yet.";
568 boost::this_thread::sleep(boost::posix_time::microseconds( us ));
581 <<
" receive handler map map before reconnect for ip= " << ip;
584 for (std::map<AnaInt32, AnaCanScan*>::iterator it=lmap.begin(); it!=lmap.end(); it++){
585 if ( ip == it->second->ipAdress() ){
586 anaRet = it->second->connectReceptionHandler();
589 <<
" failed to reconnect reception handler for ip= " << ip
590 <<
" handle= " << it->second->handle()
591 <<
" port= " << it->second->canPortNumber()
592 <<
" anaRet= " << anaRet;
595 <<
" reconnect reception handler for ip= " << ip
596 <<
" handle= " << it->second->handle()
597 <<
" looking good= " << anaRet;
605 if ( it->first != it->second->handle()){
607 <<
" erasing stale handler " << it->first
608 <<
" for object handle= " << it->second->handle() <<
" from obj. map";
613 <<
" is done and OK.";
617 <<
" receive handler map after reconnect for ip= " << ip;
622 boost::this_thread::sleep(boost::posix_time::microseconds( us ));
632 AnaInt32 anaCallReturn;
637 switch ( anaCallReturn ){
640 MLOGANA(
ERR,
this) <<
"Connection to TCP/IP partner can't be established or is disconnected. Lost TCP/IP: 0x" << hex << anaCallReturn << dec;
644 MLOGANA(
ERR,
this) <<
"No answer was received from TCP/IP partner within the defined timeout. Lost TCP/IP: 0x" << hex << anaCallReturn << dec;
648 MLOGANA(
ERR,
this) <<
"Invalid device handle. Lost TCP/IP: 0x" << hex << anaCallReturn << dec;
652 MLOGANA(
ERR,
this) <<
"Other Error in CANSetGlobals: 0x" << hex << anaCallReturn << dec;
660 if (anaCallReturn != 0) {
661 MLOGANA(
ERR,
this) <<
"failed CANSetCallbackEx, return code = [" << anaCallReturn <<
"]" 663 <<
" in a reconnect! Can't fix this, maybe hardware/firmware problem.";
676 return( anaCallReturn );
686 MLOGANA(
DBG,
this) <<
" handler " << h <<
" not found in map, not erased";
699 AnaInt32 anaCallReturn;
700 AnaInt32 canModuleHandle;
709 MLOGANA(
TRC,
this) <<
"CANDeviceConnectState: device connect state= 0x" << hex << state << dec;
730 MLOGANA(
INF,
this) <<
"device is in state CONNECTING, don't try to reconnect for now, skip.";
735 MLOGANA(
INF,
this) <<
"device is in state CONNECTED, don't try to reconnect, just skip.";
745 <<
" anaCallReturn= 0x" << hex << anaCallReturn << dec;
746 if ( anaCallReturn != 0 ) {
748 <<
" anaCallReturn= 0x" << hex << anaCallReturn << dec;
762 if (anaCallReturn != 0) {
763 MLOGANA(
ERR,
this) <<
"CANOpenDevice failed: 0x" << hex << anaCallReturn << dec;
768 <<
" canModuleHandle= " << canModuleHandle
779 boost::this_thread::sleep(boost::posix_time::microseconds( us ));
799 AnaInt32 anaCallReturn = CANStartAlive(
m_UcanHandle, aliveTime_sec );
800 if ( anaCallReturn != 0 ){
801 MLOGANA(
ERR,
this) <<
"could not start alive mechanism, error= 0x" << hex << anaCallReturn << dec;
821 AnaInt32 anaCallReturn;
825 if (anaCallReturn != 0) {
826 MLOGANA(
ERR,
this) <<
"There was a problem when sending a message with CANWrite";
838 char tmp[300] =
"Error";