15 #include "../../include/kobuki_driver/packet_handler/packet_finder.hpp"
28 state(waitingForStx), verbose(false)
37 void PacketFinderBase::configure(
const std::string &sigslots_namespace,
39 unsigned int sizeMaxPayload,
unsigned int sizeChecksumField,
bool variableSizePayload)
42 size_etx = putEtx.
size();
43 size_length_field = sizeLengthField;
44 variable_size_payload = variableSizePayload;
45 size_max_payload = sizeMaxPayload;
46 size_payload = variable_size_payload ? 0 : sizeMaxPayload;
47 size_checksum_field = sizeChecksumField;
50 buffer =
BufferType(size_stx + size_length_field + size_max_payload + size_checksum_field + size_etx);
51 state = waitingForStx;
53 sig_warn.connect(sigslots_namespace + std::string(
"/ros_warn"));
54 sig_error.connect(sigslots_namespace + std::string(
"/ros_error"));
62 void PacketFinderBase::clear()
64 state = waitingForStx;
68 void PacketFinderBase::enableVerbose()
73 bool PacketFinderBase::checkSum()
78 unsigned int PacketFinderBase::numberOfDataToRead()
88 case waitingForPayloadToEtx:
89 num = size_payload + size_etx + size_checksum_field;
92 case waitingForPayloadSize:
93 num = size_checksum_field;
105 printf(
"[state(%d):%02d]", state, num);
110 void PacketFinderBase::getBuffer(BufferType & bufferRef)
115 void PacketFinderBase::getPayload(
BufferType & bufferRef)
118 bufferRef.resize( buffer.size() - size_stx - size_etx - size_length_field - size_checksum_field );
119 for (
unsigned int i = size_stx + size_length_field; i < buffer.size() - size_etx - size_checksum_field; i++) {
131 bool PacketFinderBase::update(
const unsigned char * incoming,
unsigned int numberOfIncoming)
135 if (!(numberOfIncoming > 0))
138 bool found_packet(
false);
140 if ( state == clearBuffer ) {
142 state = waitingForStx;
147 if (WaitForStx(incoming[0]))
149 if (size_length_field)
151 state = waitingForPayloadSize;
155 if (variable_size_payload)
158 state = waitingForEtx;
164 state = waitingForPayloadToEtx;
170 if (waitForEtx(incoming[0], found_packet))
176 case waitingForPayloadSize:
177 if (waitForPayloadSize(incoming, numberOfIncoming))
179 state = waitingForPayloadToEtx;
183 case waitingForPayloadToEtx:
184 if (waitForPayloadAndEtx(incoming, numberOfIncoming, found_packet))
191 state = waitingForStx;
194 if ( found_packet ) {
204 bool PacketFinderBase::WaitForStx(
const unsigned char datum)
206 bool found_stx(
true);
209 buffer.push_back(datum);
212 for (
unsigned int i = 0; i < buffer.size() && i < STX.size(); i++)
214 if (buffer[i] != STX[i])
222 return (found_stx && buffer.size() == STX.size());
225 bool PacketFinderBase::waitForPayloadSize(
const unsigned char * incoming,
unsigned int numberOfIncoming)
228 unsigned char first_byte;
229 for (
unsigned int i = 0; i < numberOfIncoming; i++) {
230 first_byte = incoming[i];
231 buffer.push_back(incoming[i]);
236 for (
unsigned int i = 0; i < buffer.size(); i++)
237 printf(
"%02x ", buffer[i]);
242 if (buffer.size() < size_stx + size_length_field)
248 switch (size_length_field)
251 size_payload = buffer[size_stx];
254 size_payload = buffer[size_stx];
255 size_payload |= buffer[size_stx + 1] << 8;
258 size_payload = buffer[size_stx];
259 size_payload |= buffer[size_stx + 1] << 8;
260 size_payload |= buffer[size_stx + 2] << 16;
261 size_payload |= buffer[size_stx + 3] << 24;
271 printf(
"[payloadSize: %d]\n", size_payload);
278 bool PacketFinderBase::waitForEtx(
const unsigned char incoming,
bool & foundPacket)
281 buffer.push_back(incoming);
285 if (buffer.size() < size_stx + size_etx + 1)
291 unsigned int number_of_match(0);
292 for (
unsigned int i = 0; i < ETX.size(); i++)
294 if (buffer[buffer.size() - ETX.size() + i] == ETX[i])
300 if (number_of_match == ETX.size())
306 if (buffer.size() >= size_stx + size_max_payload + size_etx)
313 bool PacketFinderBase::waitForPayloadAndEtx(
const unsigned char * incoming,
unsigned int numberOfIncoming,
bool & foundPacket)
316 for (
unsigned int i = 0; i < numberOfIncoming; i++)
318 buffer.push_back(incoming[i]);
323 if ( size_payload > size_max_payload ) {
325 std::ostringstream ostream;
326 ostream <<
"abnormally sized payload retrieved, clearing [" << size_max_payload <<
"][" << size_payload <<
"]";
328 ostream << std::setfill(
'0') << std::uppercase;
340 ostream <<
", buffer: [" << std::setw(2) << buffer.size() <<
"][";
341 for (
unsigned int i = 0; i < buffer.size(); ++i ) {
342 ostream << std::setw(2) << std::hex << static_cast<int>(buffer[i]) <<
" " << std::dec;
346 sig_error.emit(ostream.str());
350 if (buffer.size() < size_stx + size_length_field + size_payload + size_checksum_field + size_etx)
357 std::cout <<
"Start check etx " << std::endl;
358 for (
unsigned int i = 0; i < numberOfIncoming; ++i ) {
359 std::cout << std::hex << static_cast<int>(*(incoming+i)) <<
" ";
361 std::cout << std::dec << std::endl;
365 for (
unsigned int i = (size_stx + size_length_field + size_payload + size_checksum_field);
366 i < (size_stx + size_length_field + size_payload + size_checksum_field + size_etx); i++)
368 if (buffer[i] != ETX[i])
374 std::cout <<
"End of checking etx " << std::endl;