00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArSickPacketReceiver.h"
00029 #include "ArLog.h"
00030 #include "ariaUtil.h"
00031
00032
00039 AREXPORT ArSickPacketReceiver::ArSickPacketReceiver(
00040 unsigned char receivingAddress, bool allocatePackets,
00041 bool useBase0Address)
00042 {
00043 myAllocatePackets = allocatePackets;
00044 myReceivingAddress = receivingAddress;
00045 myDeviceConn = NULL;
00046 myUseBase0Address = useBase0Address;
00047 }
00048
00056 AREXPORT ArSickPacketReceiver::ArSickPacketReceiver(
00057 ArDeviceConnection *deviceConnection,
00058 unsigned char receivingAddress, bool allocatePackets,
00059 bool useBase0Address)
00060 {
00061 myDeviceConn = deviceConnection;
00062 myAllocatePackets = allocatePackets;
00063 myReceivingAddress = receivingAddress;
00064 myUseBase0Address = useBase0Address;
00065 }
00066
00067 AREXPORT ArSickPacketReceiver::~ArSickPacketReceiver()
00068 {
00069
00070 }
00071
00072 AREXPORT void ArSickPacketReceiver::setDeviceConnection(
00073 ArDeviceConnection *deviceConnection)
00074 {
00075 myDeviceConn = deviceConnection;
00076 }
00077
00078 AREXPORT ArDeviceConnection *ArSickPacketReceiver::getDeviceConnection(void)
00079 {
00080 return myDeviceConn;
00081 }
00082
00092 AREXPORT ArSickPacket *ArSickPacketReceiver::receivePacket(
00093 unsigned int msWait)
00094 {
00095 ArSickPacket *packet;
00096 unsigned char c;
00097 char buf[2048];
00098 long count = 0;
00099
00100 int state = STATE_START;
00101
00102
00103 long timeToRunFor;
00104 long packetLength;
00105 ArTime timeDone;
00106 ArTime lastDataRead;
00107 ArTime packetReceived;
00108 int numRead;
00109
00110
00111 if (myDeviceConn == NULL ||
00112 myDeviceConn->getStatus() != ArDeviceConnection::STATUS_OPEN)
00113 {
00114 return NULL;
00115 }
00116
00117 timeDone.setToNow();
00118 timeDone.addMSec(msWait);
00119 do
00120 {
00121 timeToRunFor = timeDone.mSecTo();
00122 if (timeToRunFor < 0)
00123 timeToRunFor = 0;
00124
00125 if (myDeviceConn->read((char *)&c, 1, timeToRunFor) == 0)
00126 {
00127 if (state == STATE_START)
00128 {
00129 return NULL;
00130 }
00131 else
00132 {
00133
00134 continue;
00135 }
00136 }
00137
00138
00139 switch (state) {
00140 case STATE_START:
00141 if (c == 0x02)
00142 {
00143
00144 state = STATE_ADDR;
00145 myPacket.empty();
00146 myPacket.setLength(0);
00147 myPacket.uByteToBuf(c);
00148 packetReceived = myDeviceConn->getTimeRead(0);
00149 myPacket.setTimeReceived(packetReceived);
00150 }
00151
00152
00153
00154
00155 break;
00156 case STATE_ADDR:
00157
00158
00159
00160
00161
00162 if (!myUseBase0Address && c >= 0x80 && c <= 0x84)
00163 {
00164 state = STATE_START_COUNT;
00165 myPacket.uByteToBuf(c);
00166 }
00167
00168 else if (myUseBase0Address && c <= 0x4)
00169 {
00170 state = STATE_START_COUNT;
00171 myPacket.uByteToBuf(c);
00172 }
00173 else
00174 {
00175 ArLog::log(ArLog::Terse,
00176 "ArSickPacketReceiver::receivePacket: wrong address (0x%x instead of 0x%x)", c, (unsigned) 0x80 + myReceivingAddress);
00177 state = STATE_START;
00178 }
00179 break;
00180 case STATE_START_COUNT:
00181 packetLength = c;
00182 myPacket.uByteToBuf(c);
00183 state = STATE_ACQUIRE_DATA;
00184 break;
00185 case STATE_ACQUIRE_DATA:
00186
00187
00188
00189 myPacket.uByteToBuf(c);
00190 packetLength = packetLength | (c << 8);
00191 count = 0;
00192
00193
00194 if (packetLength > ((long)myPacket.getMaxLength() -
00195 (long)myPacket.getHeaderLength()))
00196 {
00197 ArLog::log(ArLog::Normal,
00198 "ArSickPacketReceiver::receivePacket: packet too long, it is %d long while the maximum is %d.", packetLength, myPacket.getMaxLength());
00199 state = STATE_START;
00200
00201 break;
00202 }
00203
00204
00205
00206 lastDataRead.setToNow();
00207 while (count < packetLength + 2)
00208 {
00209 numRead = myDeviceConn->read(buf + count, packetLength + 2- count, 1);
00210 if (numRead > 0)
00211 lastDataRead.setToNow();
00212 if (lastDataRead.mSecTo() < -100)
00213 {
00214 return NULL;
00215 }
00216 count += numRead;
00217 }
00218 myPacket.dataToBuf(buf, packetLength + 2);
00219 if (myPacket.verifyCRC())
00220 {
00221 myPacket.resetRead();
00222
00223
00224 if (myAllocatePackets)
00225 {
00226 packet = new ArSickPacket;
00227 packet->duplicatePacket(&myPacket);
00228 return packet;
00229 }
00230 else
00231 return &myPacket;
00232 }
00233 else
00234 {
00235 ArLog::log(ArLog::Normal,
00236 "ArSickPacketReceiver::receivePacket: bad packet, bad checksum");
00237 state = STATE_START;
00238
00239 break;
00240 }
00241 break;
00242 default:
00243 break;
00244 }
00245 } while (timeDone.mSecTo() >= 0 || state != STATE_START);
00246
00247
00248 return NULL;
00249
00250 }
00251
00252