blob: c0ed6a6c81d8cf2156fd19cbc834d91e2cc6a8f3 [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>
6#include <memory>
7#include <boost/thread/locks.hpp>
8#include <boost/thread.hpp>
9
10#include "thread.h"
11#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_;
64 // Thread handle for the debug thread.
65 std::unique_ptr<boost::thread> debug_thread_;
66
67 // Handle for the transmit object which runs in the transmit thread and also
68 // handles priority queueing of packets.
69 //std::unique_ptr<PacketTransmitter> tx_;
70 // Thread handle for the transmit thread.
71 //std::unique_ptr<boost::thread> tx_thread_;
72
73 // Handle for the rx object which runs in the rx thread and also
74 // handles priority queueing of packets.
75 std::unique_ptr<PacketReceiver> rx_;
76 // Thread handle for the receive thread.
77 std::unique_ptr<boost::thread> rx_thread_;
78};
79
80class DbgReader : public Thread {
81 public:
82 explicit DbgReader(GyroDriver *dev_handle);
83
84 // Serve.
85 void operator()();
86
87 private:
88 GyroDriver *gyro_;
89};
90
91#endif // GET_H_