Open Lighting Architecture  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JaRuleEndpoint.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * JaRuleEndpoint.h
17  * Handles the communication with a Ja Rule USB endpoint.
18  * Copyright (C) 2015 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBDMX_JARULEENDPOINT_H_
22 #define PLUGINS_USBDMX_JARULEENDPOINT_H_
23 
24 #include <libusb.h>
25 #include <ola/io/ByteString.h>
26 #include <ola/thread/ExecutorInterface.h>
27 #include <ola/thread/Mutex.h>
28 #include <ola/util/SequenceNumber.h>
29 
30 #include <map>
31 #include <queue>
32 
33 #include "plugins/usbdmx/LibUsbAdaptor.h"
34 
35 namespace ola {
36 namespace plugin {
37 namespace usbdmx {
38 
48  public:
49  typedef enum {
53  } StatusFlags;
54 
60  typedef enum {
65 
73 
78 
83 
88 
93 
98  } CommandResult;
99 
110  typedef ola::BaseCallback4<
111  void, CommandResult, uint8_t, uint8_t, const ola::io::ByteString&>
113 
117  typedef enum {
118  RESET_DEVICE = 0x00,
119  SET_BREAK_TIME = 0x10,
120  GET_BREAK_TIME = 0x11,
121  SET_MAB_TIME = 0x12,
122  GET_MAB_TIME = 0x13,
123  SET_RDM_BROADCAST_LISTEN = 0x20,
124  GET_RDM_BROADCAST_LISTEN = 0x21,
125  SET_RDM_WAIT_TIME = 0x22,
126  GET_RDM_WAIT_TIME = 0x23,
127  TX_DMX = 0x30,
128  RDM_DUB = 0x40,
129  RDM_REQUEST = 0x41,
130  RDM_BROADCAST_REQUEST = 0x42,
131  ECHO_COMMAND = 0xf0,
132  GET_LOG = 0xf1,
133  GET_FLAGS = 0xf2,
134  WRITE_LOG = 0xf3,
135  } CommandClass;
136 
144  AsyncronousLibUsbAdaptor *adaptor,
145  libusb_device *device);
146 
150  ~JaRuleEndpoint();
151 
157  bool Init();
158 
164  void CancelAll();
165 
177  void SendCommand(CommandClass command, const uint8_t *data, unsigned int size,
178  CommandCompleteCallback *callback);
179 
184  void _OutTransferComplete();
185 
190  void _InTransferComplete();
191 
192  private:
193  // This must be a multiple of the USB packet size otherwise we can experience
194  // overflows. A message can be a maximum of 640 bytes, so we'll use 1k here
195  // to be safe.
196  enum {
197  IN_BUFFER_SIZE = 1024
198  };
199 
200  enum {
201  OUT_BUFFER_SIZE = 1024
202  };
203 
204  // A command that is in the send queue.
205  typedef struct {
206  CommandClass command;
207  CommandCompleteCallback *callback;
208  ola::io::ByteString payload;
209  } QueuedCommand;
210 
211  // A command that has been sent, and is waiting on a response.
212  typedef struct {
213  CommandClass command;
214  CommandCompleteCallback *callback;
215  // TODO(simon): we probably need a counter here to detect timeouts.
216  } PendingCommand;
217 
218  // The arguments passed to the user supplied callback.
219  typedef struct {
220  CommandResult result;
221  uint8_t return_code;
222  uint8_t status_flags;
223  const ola::io::ByteString payload;
224  } CallbackArgs;
225 
226  typedef std::map<uint8_t, PendingCommand> PendingCommandMap;
227  typedef std::queue<QueuedCommand> CommandQueue;
228 
229  ola::thread::ExecutorInterface *m_executor;
230  LibUsbAdaptor *m_adaptor;
231  libusb_device *m_device;
232  libusb_device_handle *m_usb_handle;
234 
235  ola::thread::Mutex m_mutex;
236  CommandQueue m_queued_commands; // GUARDED_BY(m_mutex);
237  PendingCommandMap m_pending_commands; // GUARDED_BY(m_mutex);
238 
239  uint8_t m_out_buffer[OUT_BUFFER_SIZE]; // GUARDED_BY(m_mutex);
240  libusb_transfer *m_out_transfer; // GUARDED_BY(m_mutex);
241  bool m_out_in_progress; // GUARDED_BY(m_mutex);
242 
243  uint8_t m_in_buffer[IN_BUFFER_SIZE]; // GUARDED_BY(m_mutex);
244  libusb_transfer *m_in_transfer; // GUARDED_BY(m_mutex);
245  bool m_in_in_progress; // GUARDED_BY(m_mutex);
246 
247  void MaybeSendCommand(); // LOCK_REQUIRED(m_mutex);
248  bool SubmitInTransfer(); // LOCK_REQUIRED(m_mutex);
249  void HandleResponse(const uint8_t *data,
250  unsigned int size); // LOCK_REQUIRED(m_mutex);
251 
252  void ScheduleCallback(CommandCompleteCallback *callback,
253  CommandResult result,
254  uint8_t return_code,
255  uint8_t status_flags,
256  const ola::io::ByteString &payload);
257  void RunCallback(CommandCompleteCallback *callback, CallbackArgs args);
258 
259  static const uint8_t EOF_IDENTIFIER = 0xa5;
260  static const uint8_t SOF_IDENTIFIER = 0x5a;
261  static const unsigned int MAX_PAYLOAD_SIZE = 513;
262  static const unsigned int MIN_RESPONSE_SIZE = 9;
263  static const unsigned int USB_PACKET_SIZE = 64;
264  static const unsigned int MAX_IN_FLIGHT = 2;
265  static const unsigned int MAX_QUEUED_MESSAGES = 10;
266  static const unsigned int INTERFACE_OFFSET = 2;
267 
268  static const uint8_t IN_ENDPOINT = 0x81;
269  static const uint8_t OUT_ENDPOINT = 0x01;
270  static const unsigned int ENDPOINT_TIMEOUT_MS = 1000;
271 
272  DISALLOW_COPY_AND_ASSIGN(JaRuleEndpoint);
273 };
274 
275 } // namespace usbdmx
276 } // namespace plugin
277 } // namespace ola
278 
279 #endif // PLUGINS_USBDMX_JARULEENDPOINT_H_