Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00029
00030 #include <driver_svh/SVHReceiveThread.h>
00031 #include <driver_svh/Logging.h>
00032
00033
00034 using icl_comm::ArrayBuilder;
00035
00036 namespace driver_svh {
00037
00038 SVHReceiveThread::SVHReceiveThread(const TimeSpan& period, boost::shared_ptr<Serial> device,
00039 ReceivedPacketCallback const & received_callback)
00040 : PeriodicThread("SVHReceiveThread", period),
00041 m_serial_device(device),
00042 m_received_state(eRS_HEADER1),
00043 m_length(0),
00044 m_data(0, 0),
00045 m_ab(0),
00046 m_packets_received(0),
00047 m_skipped_bytes(0),
00048 m_received_callback(received_callback)
00049 {}
00050
00051 void SVHReceiveThread::run()
00052 {
00053 while (execute())
00054 {
00055 if (m_serial_device)
00056 {
00057 if (m_serial_device->IsOpen())
00058 {
00059
00060 if(!receiveData())
00061 {
00062 waitPeriod();
00063 }
00064 }
00065 else
00066 {
00067 LOGGING_WARNING_C(DriverSVH, SVHReceiveThread, "Cannot read data from serial device. It is not opened!" << endl);
00068 waitPeriod();
00069 }
00070 }
00071
00072 else
00073 {
00074
00075 waitPeriod();
00076 }
00077 }
00078 }
00079
00080 bool SVHReceiveThread::receiveData()
00081 {
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093 uint8_t data_byte;
00094 int bytes = m_serial_device->Read(&data_byte, sizeof(uint8_t));
00095 if (bytes < 0)
00096 {
00097 LOGGING_TRACE_C(DriverSVH, SVHReceiveThread, "Serial read error:" << bytes << endl );
00098 return false;
00099 }
00100 if (bytes < 1)
00101 {
00102 return false;
00103 }
00104
00105 switch (m_received_state)
00106 {
00107 case eRS_HEADER1:
00108 {
00109 if (data_byte == PACKET_HEADER1)
00110 {
00111 m_received_state = eRS_HEADER2;
00112 }
00113 else
00114 {
00115 m_skipped_bytes++;
00116 }
00117 break;
00118 }
00119 case eRS_HEADER2:
00120 {
00121 switch (data_byte)
00122 {
00123 case PACKET_HEADER2:
00124 {
00125 m_received_state = eRS_INDEX;
00126 break;
00127 }
00128 case PACKET_HEADER1:
00129 {
00130 m_received_state = eRS_HEADER2;
00131 m_skipped_bytes++;
00132 break;
00133 }
00134 default:
00135 {
00136 m_received_state = eRS_HEADER1;
00137 m_skipped_bytes+=2;
00138 break;
00139 }
00140 }
00141 break;
00142 }
00143 case eRS_INDEX:
00144 {
00145
00146 m_ab.reset(0);
00147
00148
00149 m_ab.appendWithoutConversion(data_byte);
00150 m_received_state = eRS_ADDRESS;
00151 break;
00152 }
00153 case eRS_ADDRESS:
00154 {
00155
00156 m_ab.appendWithoutConversion(data_byte);
00157 m_received_state = eRS_LENGTH1;
00158 break;
00159 }
00160 case eRS_LENGTH1:
00161 {
00162
00163 m_ab.appendWithoutConversion(data_byte);
00164 m_received_state = eRS_LENGTH2;
00165 break;
00166 }
00167 case eRS_LENGTH2:
00168 {
00169
00170 m_ab.appendWithoutConversion(data_byte);
00171 m_length = m_ab.readBack<uint16_t>();
00172 m_received_state = eRS_DATA;
00173 m_data.clear();
00174 m_data.reserve(m_length);
00175 break;
00176 }
00177 case eRS_DATA:
00178 {
00179
00180
00181 m_data.push_back(data_byte);
00182 m_ab.appendWithoutConversion(data_byte);
00183 if(m_data.size()>=m_length)
00184 {
00185 m_received_state = eRS_CHECKSUM1;
00186 }
00187 break;
00188 }
00189 case eRS_CHECKSUM1:
00190 {
00191 m_checksum1 = data_byte;
00192 m_checksum2 = 0;
00193 m_received_state = eRS_CHECKSUM2;
00194 break;
00195 }
00196 case eRS_CHECKSUM2:
00197 {
00198 m_checksum2=data_byte;
00199 uint8_t checksum1=m_checksum1;
00200 uint8_t checksum2=m_checksum2;
00201
00202 for (size_t i = 0; i < m_data.size(); ++i)
00203 {
00204 checksum1 -= m_data[i];
00205 checksum2 ^= m_data[i];
00206 }
00207
00208 if ((checksum1 == 0) && (checksum2 == 0))
00209 {
00210
00211
00212 SVHSerialPacket received_packet(m_length);
00213 m_ab >> received_packet;
00214
00215 m_packets_received++;
00216
00217 if(m_skipped_bytes>0)LOGGING_TRACE_C(DriverSVH, SVHReceiveThread, "Skipped "<<m_skipped_bytes<<" bytes "<< endl);
00218 LOGGING_TRACE_C(DriverSVH, SVHReceiveThread, "Received packet index:" << received_packet.index <<", address:"<<received_packet.address<<", size:"<<received_packet.data.size() << endl);
00219 m_skipped_bytes=0;
00220
00221 if (m_received_callback)
00222 {
00223 m_received_callback(received_packet, m_packets_received);
00224 }
00225
00226 m_received_state = eRS_HEADER1;
00227 }
00228 else
00229 {
00230 m_received_state = eRS_HEADER1;
00231
00232 SVHSerialPacket received_packet(m_length);
00233 m_ab >> received_packet;
00234
00235 if(m_skipped_bytes>0)LOGGING_TRACE_C(DriverSVH, SVHReceiveThread, "Skipped "<<m_skipped_bytes<<" bytes: "<< endl);
00236 LOGGING_TRACE_C(DriverSVH, SVHReceiveThread, "Checksum error: "<< (int)checksum1<<","<<(int)checksum2<<"!=0, skipping "<<m_length+8<<"bytes, packet index:" << received_packet.index <<", address:"<<received_packet.address<<", size:"<<received_packet.data.size() << endl);
00237 m_skipped_bytes=0;
00238 if (m_received_callback)
00239 {
00240 m_received_callback(received_packet, m_packets_received);
00241 }
00242 }
00243 break;
00244 }
00245 }
00246
00247 return true;
00248 }
00249
00250 }