serial_query.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 
4 #include "create/serial_query.h"
5 #include "create/types.h"
6 
7 #define SENSORS_RESPONSE_LENGTH 20
8 
9 namespace create {
10 
11  SerialQuery::SerialQuery(std::shared_ptr<Data> d) : Serial(d),
12  streamRecoveryTimer(io),
13  packetID(ID_BUMP_WHEELDROP),
14  packetByte(0),
15  packetData(0),
16  maxPacketID(ID_CAPACITY) {
17  }
18 
20  if (!started) {
22  started = true;
23  }
24  return true;
25  }
26 
28  static const uint8_t requestPacket[2] = { OC_SENSORS, ID_GROUP_0 };
29  // Prevents previous packet from corrupting next one
30  flushInput();
31  send(requestPacket, 2);
32  // Automatically resend request if no response is received
33  streamRecoveryTimer.expires_from_now(boost::posix_time::milliseconds(50));
34  streamRecoveryTimer.async_wait(
35  std::bind(&SerialQuery::restartSensorStream, this, std::placeholders::_1));
36  }
37 
38  void SerialQuery::restartSensorStream(const boost::system::error_code& err) {
39  if (err != boost::asio::error::operation_aborted) {
40  if (packetID != ID_BUMP_WHEELDROP) {
42  }
44  }
45  }
46 
48  // Only works with POSIX support
49  tcflush(port.lowest_layer().native_handle(), TCIFLUSH);
50  }
51 
53  packetData |= (static_cast<uint16_t>(byteRead) << (8 * packetByte));
54 
55  if (packetByte > 0) {
56  --packetByte;
57  } else if (packetID < maxPacketID) {
58  // New packet
59  data->getPacket(packetID)->setDataToValidate(packetData);
60  packetData = 0;
61  ++packetID;
62  packetByte = data->getPacket(packetID)->nbytes - 1;
63  } else {
64  // Response finished
66  packetByte = 0;
67  packetData = 0;
70  }
71  }
72 } // namespace create
uint8_t byteRead
Definition: serial.h:65
boost::asio::serial_port port
Definition: serial.h:56
void notifyDataReady()
Definition: serial.cpp:142
Definition: create.h:47
boost::asio::deadline_timer streamRecoveryTimer
Definition: serial_query.h:52
std::shared_ptr< Data > data
Definition: serial.h:76
bool send(const uint8_t *bytes, const uint32_t numBytes)
Definition: serial.cpp:179
void restartSensorStream(const boost::system::error_code &err)
void processByte(uint8_t byteRead)
SerialQuery(std::shared_ptr< Data > data)
const uint8_t maxPacketID
Definition: serial_query.h:56
uint64_t corruptPackets
Definition: serial.h:78


libcreate
Author(s): Jacob Perron
autogenerated on Mon Nov 2 2020 03:59:34