51 #pragma warning(disable: 4996)
52 #pragma warning(disable: 4267)
53 #pragma warning(disable: 4101) // C4101: "e" : Unreferenzierte lokale Variable
54 #define _WIN32_WINNT 0x0501
62 #include <boost/asio.hpp>
63 #include <boost/lambda/lambda.hpp>
66 #include <boost/lexical_cast.hpp>
71 #include "sick_scan/rosconsole_simu.hpp"
84 return (diagnostic_msgs::DiagnosticStatus::ERROR);
90 bool emulateReply(
UINT8 *requestData,
int requestLen, std::vector<unsigned char> *replyVector)
94 std::vector<std::string> keyWordList;
95 std::vector<std::string> answerList;
97 keyWordList.push_back(
"sMN SetAccessMode");
98 answerList.push_back(
"sAN SetAccessMode 1");
100 keyWordList.push_back(
"sWN EIHstCola");
101 answerList.push_back(
"sWA EIHstCola");
103 keyWordList.push_back(
"sRN FirmwareVersion");
104 answerList.push_back(
"sRA FirmwareVersion 8 1.0.0.0R");
106 keyWordList.push_back(
"sRN OrdNum");
107 answerList.push_back(
"sRA OrdNum 7 1234567");
109 keyWordList.push_back(
"sWN TransmitTargets 1");
110 answerList.push_back(
"sWA TransmitTargets");
112 keyWordList.push_back(
"sWN TransmitObjects 1");
113 answerList.push_back(
"sWA TransmitObjects");
115 keyWordList.push_back(
"sWN TCTrackingMode 0");
116 answerList.push_back(
"sWA TCTrackingMode");
118 keyWordList.push_back(
"sRN SCdevicestate");
119 answerList.push_back(
"sRA SCdevicestate 1");
121 keyWordList.push_back(
"sRN DItype");
122 answerList.push_back(
"sRA DItype D RMS3xx-xxxxxx");
124 keyWordList.push_back(
"sRN ODoprh");
125 answerList.push_back(
"sRA ODoprh 451");
127 keyWordList.push_back(
"sMN mSCloadappdef");
128 answerList.push_back(
"sAN mSCloadappdef");
131 keyWordList.push_back(
"sRN SerialNumber");
132 answerList.push_back(
"sRA SerialNumber 8 18020073");
134 keyWordList.push_back(
"sMN Run");
135 answerList.push_back(
"sAN Run 1s");
137 keyWordList.push_back(
"sRN ODpwrc");
138 answerList.push_back(
"sRA ODpwrc 20");
140 keyWordList.push_back(
"sRN LocationName");
141 answerList.push_back(
"sRA LocationName B not defined");
143 keyWordList.push_back(
"sEN LMDradardata 1");
144 answerList.push_back(
"sEA LMDradardata 1");
146 for (
int i = 0; i < requestLen; i++)
148 request += (char) requestData[i];
150 for (
int i = 0; i < keyWordList.size(); i++)
152 if (request.find(keyWordList[i]) != std::string::npos)
155 reply += answerList[i];
156 reply += (char) 0x03;
160 replyVector->clear();
161 for (
int i = 0; i < reply.length(); i++)
163 replyVector->push_back((
unsigned char) reply[i]);
175 socket_(io_service_),
176 deadline_(io_service_),
179 timelimit_(timelimit)
183 if ((cola_dialect_id ==
'a') || (cola_dialect_id ==
'A'))
188 if ((cola_dialect_id ==
'b') || (cola_dialect_id ==
'B'))
203 deadline_.expires_at(boost::posix_time::pos_infin);
214 using boost::asio::ip::tcp;
215 using boost::lambda::var;
216 using boost::lambda::_1;
251 if ((cola_dialect_id ==
'a') || (cola_dialect_id ==
'A'))
253 this->m_protocol =
CoLa_A;
257 this->m_protocol =
CoLa_B;
358 if (magicWord != 0x02020202)
365 if (magicWord == 0x02020202)
392 printInfoMessage(
"SickScanCommonNw::findFrameInReceiveBuffer: Frame cannot be decoded yet, only " +
399 payloadlength = colab::getIntegerFromBuffer<UINT32>(
m_receiveBuffer, pos);
401 "SickScanCommonNw::findFrameInReceiveBuffer: Decoded payload length is " + ::
toString(payloadlength) +
409 "SickScanCommonNw::findFrameInReceiveBuffer: Frame too big for receive buffer. Frame discarded with length:"
418 "SickScanCommonNw::findFrameInReceiveBuffer: Frame not complete yet. Waiting for the rest of it (" +
424 frameLen = payloadlength + 9;
438 for (
UINT16 i = 8; i < (frameLen - 1); i++)
442 temp_xor = temp_xor ^ temp;
446 if (temp_xor != checkSum)
448 printWarning(
"SickScanCommonNw::findFrameInReceiveBuffer: Wrong checksum, Frame discarded.");
472 "SickScanCommonNw::processFrame: Calling processFrame_CoLa_A() with " + ::
toString(frame.
size()) +
" bytes.",
479 "SickScanCommonNw::processFrame: Calling processFrame_CoLa_B() with " + ::
toString(frame.
size()) +
" bytes.",
496 bool beVerboseHere =
false;
498 "SickScanCommonNw::readCallbackFunction(): Called with " +
toString(numOfBytes) +
" available bytes.",
503 UINT32 bytesToBeTransferred = numOfBytes;
504 if (remainingSpace < numOfBytes)
506 bytesToBeTransferred = remainingSpace;
516 if (bytesToBeTransferred > 0)
534 printInfoMessage(
"SickScanCommonNw::readCallbackFunction(): No complete frame in input buffer, we are done.",
544 "SickScanCommonNw::readCallbackFunction(): Processing a frame of length " + ::
toString(frame.
size()) +
545 " bytes.", beVerboseHere);
567 sscanf(
port_.c_str(),
"%d", &portInt);
572 ROS_INFO(
"Sensor emulation is switched on - network traffic is switched off.");
583 ROS_WARN(
"Disconnecting TCP-Connection.");
609 deadline_.expires_at(boost::posix_time::pos_infin);
620 ret = this->
recvQueue.getNumberOfEntriesInQueue();
625 bool *exception_occured,
bool isBinary)
628 deadline_.expires_from_now(boost::posix_time::milliseconds(timeout_ms));
629 const char end_delim =
static_cast<char>(0x03);
631 ec_ = boost::asio::error::would_block;
638 int waitingTimeInMs = 1;
640 for (i = 0; i < timeout_ms; i += waitingTimeInMs)
642 if (
false == this->
recvQueue.isQueueEmpty())
650 ROS_ERROR(
"no answer received after %zu ms. Maybe sopas mode is wrong.\n", timeout_ms);
653 boost::condition_variable cond_;
656 *bytes_read = datagramWithTimeStamp.
datagram.size();
657 memcpy(buffer, &(datagramWithTimeStamp.
datagram[0]), datagramWithTimeStamp.
datagram.size());
668 ROS_ERROR(
"sendSOPASCommand: socket not open");
675 bool cmdIsBinary =
false;
683 for (
int i = 0; i < 4; i++)
685 if (request[i] == 0x02)
701 if (cmdIsBinary ==
false)
703 msgLen = strlen(request);
708 for (
int i = 4; i < 8; i++)
710 dataLen |= ((
unsigned char) request[i] << (7 - i) * 8);
712 msgLen = 8 + dataLen + 1;
721 bool debugBinCmd =
false;
724 printf(
"=== START HEX DUMP ===\n");
725 for (
int i = 0; i < msgLen; i++)
727 unsigned char *ptr = (
UINT8 *) request;
728 printf(
"%02x ", ptr[i]);
730 printf(
"\n=== END HEX DUMP ===\n");
741 boost::asio::write(
socket_, boost::asio::buffer(request, msgLen));
743 catch (boost::system::system_error &e)
745 ROS_ERROR(
"write error for command: %s", request);
753 const int BUF_SIZE = 65536;
754 char buffer[BUF_SIZE];
767 "sendSOPASCommand: no full reply available for read after timeout.");
774 reply->resize(bytes_read);
776 std::copy(buffer, buffer + bytes_read, &(*reply)[0]);
785 bool isBinaryProtocol,
int *numberOfRemainingFifoEntries)
787 if (NULL != numberOfRemainingFifoEntries)
789 *numberOfRemainingFifoEntries = 0;
798 double waitTime10Hz = 10.0 * (double) nanoSec / 1E9;
800 uint32_t waitTime = (int) waitTime10Hz;
802 double waitTimeUntilNextTime10Hz = 1 / 10.0 * (1.0 - (waitTime10Hz - waitTime));
814 std::vector<unsigned char> dataBuffer;
815 #if 1 // prepared for reconnect
816 bool retVal = this->
recvQueue.waitForIncomingObject(maxWaitInMs);
819 ROS_WARN(
"Timeout during waiting for new datagram");
828 if (NULL != numberOfRemainingFifoEntries)
830 *numberOfRemainingFifoEntries = this->
recvQueue.getNumberOfEntriesInQueue();
832 recvTimeStamp = datagramWithTimeStamp.
timeStamp;
833 dataBuffer = datagramWithTimeStamp.
datagram;
838 long size = dataBuffer.size();
839 memcpy(receiveBuffer, &(dataBuffer[0]), size);
840 *actual_length = size;
845 char szFileName[255];
846 sprintf(szFileName,
"/tmp/dg%06d.bin", cnt++);
850 fout = fopen(szFileName,
"wb");
853 fwrite(receiveBuffer, *actual_length, 1, fout);
861 ROS_ERROR(
"get_datagram: socket not open");
869 std::vector<unsigned char> reply;
873 bool exception_occured =
false;
875 char *buffer =
reinterpret_cast<char *
>(receiveBuffer);
891 ROS_INFO(
"Failure - attempting to reconnect");