blob: 62462d318dc2fa7e033235bbe2e29b892051b384 [file] [log] [blame]
Austin Schuh9b360e92013-03-27 04:47:04 +00001#ifndef GET_H_
2#define GET_H_
3
4#include <sys/socket.h>
5#include <linux/can.h>
Austin Schuh9b360e92013-03-27 04:47:04 +00006
Brian Silverman798c7782013-03-28 16:48:02 -07007#include <memory>
8
9#include "aos/common/util/thread.h"
10
Austin Schuh9b360e92013-03-27 04:47:04 +000011#include "libusb_wrap.h"
12
13// Use a packed version of can_frame.
14// This allows for us to just transfer the data over the wire trivially.
15struct packed_can_frame {
16 canid_t can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
17 uint8_t can_dlc; /* data length code: 0 .. 8 */
18 uint8_t data[8];
19} __attribute__((packed));
20
21class DbgReader;
22
23// Compare function for CAN IDs.
24struct can_frame_priority_comparator
25 : std::binary_function <struct can_frame,
26 struct can_frame, bool> {
27 bool operator() (const struct can_frame& x,
28 const struct can_frame& y) const;
29};
30
31class GyroDriver {
32 public:
33 // Constructs a GyroDriver.
34 // GyroDriver takes ownership of the device handle.
35 explicit GyroDriver(LibUSBDeviceHandle *dev_handle);
36 virtual ~GyroDriver();
37
38 // Queues a CAN frame. The driver will copy the frame to reduce memory churn
39 // and non-realtime behavior associated with allocating and deallocating the
40 // messages.
41 // Returns true if the message was queued and false otherwise.
42 bool QueueCanFrame(const struct can_frame &msg);
43
44 // Returns 1 packet worth of debug data from the bulk debug channel.
45 std::string GetDebugData();
46
47 // Waits until a CAN frame is available and returns it.
48 // Returns true if we got a frame and false if we were interrupted.
49 bool GetCanFrame(struct can_frame *msg);
50
51 // Terminates the rx and tx threads in preperation for shutdown.
52 // Not safe for use in signal handlers.
53 void Terminate();
54
55 private:
56 // Class that runs in a seperate thread and receives and queues all messages.
57 class PacketReceiver;
58
59 // USB Device handle.
60 std::unique_ptr<LibUSBDeviceHandle> dev_handle_;
61 // Handle for the object run in the debug thread which prints out debug
62 // information.
63 std::unique_ptr<DbgReader> dbg_;
Austin Schuh9b360e92013-03-27 04:47:04 +000064
65 // Handle for the transmit object which runs in the transmit thread and also
66 // handles priority queueing of packets.
67 //std::unique_ptr<PacketTransmitter> tx_;
68 // Thread handle for the transmit thread.
69 //std::unique_ptr<boost::thread> tx_thread_;
70
71 // Handle for the rx object which runs in the rx thread and also
72 // handles priority queueing of packets.
73 std::unique_ptr<PacketReceiver> rx_;
Austin Schuh9b360e92013-03-27 04:47:04 +000074};
75
Brian Silverman798c7782013-03-28 16:48:02 -070076class DbgReader : public ::aos::util::Thread {
Austin Schuh9b360e92013-03-27 04:47:04 +000077 public:
78 explicit DbgReader(GyroDriver *dev_handle);
79
80 // Serve.
Brian Silverman798c7782013-03-28 16:48:02 -070081 virtual void Run();
Austin Schuh9b360e92013-03-27 04:47:04 +000082
83 private:
84 GyroDriver *gyro_;
85};
86
87#endif // GET_H_