Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 1 | #ifndef GET_H_ |
| 2 | #define GET_H_ |
| 3 | |
| 4 | #include <sys/socket.h> |
| 5 | #include <linux/can.h> |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 6 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
| 9 | #include "aos/common/util/thread.h" |
| 10 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 11 | #include "libusb_wrap.h" |
| 12 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 13 | class DbgReader; |
| 14 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 15 | class 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 Silverman | c3c0629 | 2013-03-30 18:54:31 -0700 | [diff] [blame] | 39 | void Start(); |
| 40 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 41 | 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 Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 50 | |
| 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 Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 62 | class DbgReader : public ::aos::util::Thread { |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 63 | public: |
| 64 | explicit DbgReader(GyroDriver *dev_handle); |
| 65 | |
| 66 | // Serve. |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 67 | virtual void Run(); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | GyroDriver *gyro_; |
| 71 | }; |
| 72 | |
| 73 | #endif // GET_H_ |