blob: f6b8011b235d4e5422587891da8ae9ccf563e91d [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
Austin Schuh9b360e92013-03-27 04:47:04 +000013class DbgReader;
14
Austin Schuh9b360e92013-03-27 04:47:04 +000015class GyroDriver {
16 public:
17 // Constructs a GyroDriver.
18 // GyroDriver takes ownership of the device handle.
19 explicit GyroDriver(LibUSBDeviceHandle *dev_handle);
20 virtual ~GyroDriver();
21
22 // Queues a CAN frame. The driver will copy the frame to reduce memory churn
23 // and non-realtime behavior associated with allocating and deallocating the
24 // messages.
25 // Returns true if the message was queued and false otherwise.
26 bool QueueCanFrame(const struct can_frame &msg);
27
28 // Returns 1 packet worth of debug data from the bulk debug channel.
29 std::string GetDebugData();
30
31 // Waits until a CAN frame is available and returns it.
32 // Returns true if we got a frame and false if we were interrupted.
33 bool GetCanFrame(struct can_frame *msg);
34
35 // Terminates the rx and tx threads in preperation for shutdown.
36 // Not safe for use in signal handlers.
37 void Terminate();
38
Brian Silvermanc3c06292013-03-30 18:54:31 -070039 void Start();
40
Austin Schuh9b360e92013-03-27 04:47:04 +000041 private:
42 // Class that runs in a seperate thread and receives and queues all messages.
43 class PacketReceiver;
44
45 // USB Device handle.
46 std::unique_ptr<LibUSBDeviceHandle> dev_handle_;
47 // Handle for the object run in the debug thread which prints out debug
48 // information.
49 std::unique_ptr<DbgReader> dbg_;
Austin Schuh9b360e92013-03-27 04:47:04 +000050
51 // Handle for the transmit object which runs in the transmit thread and also
52 // handles priority queueing of packets.
53 //std::unique_ptr<PacketTransmitter> tx_;
54 // Thread handle for the transmit thread.
55 //std::unique_ptr<boost::thread> tx_thread_;
56
57 // Handle for the rx object which runs in the rx thread and also
58 // handles priority queueing of packets.
59 std::unique_ptr<PacketReceiver> rx_;
Austin Schuh9b360e92013-03-27 04:47:04 +000060};
61
Brian Silverman798c7782013-03-28 16:48:02 -070062class DbgReader : public ::aos::util::Thread {
Austin Schuh9b360e92013-03-27 04:47:04 +000063 public:
64 explicit DbgReader(GyroDriver *dev_handle);
65
66 // Serve.
Brian Silverman798c7782013-03-28 16:48:02 -070067 virtual void Run();
Austin Schuh9b360e92013-03-27 04:47:04 +000068
69 private:
70 GyroDriver *gyro_;
71};
72
73#endif // GET_H_