Open Lighting Architecture: libs/usb/JaRuleWidgetPort.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
JaRuleWidgetPort.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 * JaRuleWidgetPort.h
17 * A Ja Rule widget port.
18 * Copyright (C) 2015 Simon Newton
19 */
20
21#ifndef LIBS_USB_JARULEWIDGETPORT_H_
22#define LIBS_USB_JARULEWIDGETPORT_H_
23
24#include <libusb.h>
25#include <ola/Callback.h>
26#include <ola/io/ByteString.h>
27#include <ola/thread/ExecutorInterface.h>
28#include <ola/thread/Mutex.h>
29#include <ola/util/SequenceNumber.h>
30
31#include <map>
32#include <queue>
33
34#include "libs/usb/JaRulePortHandle.h"
35#include "libs/usb/LibUsbAdaptor.h"
36#include "libs/usb/JaRuleWidget.h"
37
38namespace ola {
39namespace usb {
40
47 class JaRuleWidgetPort {
48 public:
58 JaRuleWidgetPort(ola::thread::ExecutorInterface *executor,
59 LibUsbAdaptor *adaptor,
60 libusb_device_handle *usb_handle,
61 uint8_t endpoint_number,
62 const ola::rdm::UID &uid,
63 uint8_t physical_port);
64
68 ~JaRuleWidgetPort();
69
75 JaRulePortHandle* ClaimPort();
76
81 void ReleasePort();
82
86 void CancelAll();
87
99 void SendCommand(CommandClass command,
100 const uint8_t *data,
101 unsigned int size,
102 CommandCompleteCallback *callback);
103
108 void _OutTransferComplete();
109
114 void _InTransferComplete();
115
116 private:
117 // This must be a multiple of the USB packet size otherwise we can experience
118 // overflows. A message can be a maximum of 640 bytes, so we'll use 1k here
119 // to be safe.
120 enum { IN_BUFFER_SIZE = 1024 };
121 enum { OUT_BUFFER_SIZE = 1024 };
122
123 // The arguments passed to the user supplied callback.
124 typedef struct {
125 USBCommandResult result;
126 JaRuleReturnCode return_code;
127 uint8_t status_flags;
128 const ola::io::ByteString payload;
129 } CallbackArgs;
130
131 class PendingCommand {
132 public:
133 PendingCommand(CommandClass command,
134 CommandCompleteCallback *callback,
135 const ola::io::ByteString &payload)
136 : command(command),
137 callback(callback),
138 payload(payload) {
139 }
140
141 CommandClass command;
142 CommandCompleteCallback *callback;
143 ola::io::ByteString payload;
144 TimeStamp out_time; // When this cmd was sent
145 };
146
147 typedef std::map<uint8_t, PendingCommand*> PendingCommandMap;
148 typedef std::queue<PendingCommand*> CommandQueue;
149
150 ola::Clock m_clock;
151 ola::thread::ExecutorInterface* const m_executor;
152 LibUsbAdaptor* const m_adaptor;
153 libusb_device_handle* const m_usb_handle;
154 const uint8_t m_endpoint_number;
155 const ola::rdm::UID m_uid;
156 const uint8_t m_physical_port;
157 JaRulePortHandle *m_handle; // NULL if the port isn't claimed
158
159 ola::SequenceNumber<uint8_t> m_token;
160
161 ola::thread::Mutex m_mutex;
162 CommandQueue m_queued_commands; // GUARDED_BY(m_mutex);
163 PendingCommandMap m_pending_commands; // GUARDED_BY(m_mutex);
164
165 libusb_transfer *m_out_transfer; // GUARDED_BY(m_mutex);
166 bool m_out_in_progress; // GUARDED_BY(m_mutex);
167
168 uint8_t m_in_buffer[IN_BUFFER_SIZE]; // GUARDED_BY(m_mutex);
169 libusb_transfer *m_in_transfer; // GUARDED_BY(m_mutex);
170 bool m_in_in_progress; // GUARDED_BY(m_mutex);
171
172 void MaybeSendCommand(); // LOCK_REQUIRED(m_mutex);
173 bool SubmitInTransfer(); // LOCK_REQUIRED(m_mutex);
174 void HandleResponse(const uint8_t *data,
175 unsigned int size); // LOCK_REQUIRED(m_mutex);
176
177 void ScheduleCallback(CommandCompleteCallback *callback,
178 USBCommandResult result,
179 JaRuleReturnCode return_code,
180 uint8_t status_flags,
181 const ola::io::ByteString &payload);
182 void RunCallback(CommandCompleteCallback *callback,
183 CallbackArgs args);
184
185 static const uint8_t EOF_IDENTIFIER = 0xa5;
186 static const uint8_t SOF_IDENTIFIER = 0x5a;
187 static const unsigned int MAX_PAYLOAD_SIZE = 513;
188 static const unsigned int MIN_RESPONSE_SIZE = 9;
189 static const unsigned int USB_PACKET_SIZE = 64;
190 static const unsigned int MAX_IN_FLIGHT = 2;
191 static const unsigned int MAX_QUEUED_MESSAGES = 10;
192
193 static const unsigned int ENDPOINT_TIMEOUT_MS = 1000;
194
195 JaRuleWidgetPort(const JaRuleWidgetPort &) = delete;
196 const JaRuleWidgetPort &operator=(const JaRuleWidgetPort &) = delete;
197};
198} // namespace usb
199} // namespace ola
200#endif // LIBS_USB_JARULEWIDGETPORT_H_
Callback.h
ola::Clock
Used to get the current time.
Definition Clock.h:230
ola::SequenceNumber
SequenceNumber, this ensures that we increment the sequence number whenever we go to use it.
Definition SequenceNumber.h:30
ola::TimeStamp
Definition Clock.h:179
ola::rdm::UID
Represents a RDM UID.
Definition UID.h:57
ola::thread::ExecutorInterface
Defer execution of a callback.
Definition ExecutorInterface.h:35
ola::thread::Mutex
Definition Mutex.h:41
ola::usb::JaRulePortHandle
Represents a DMX/RDM port on a Ja Rule device.
Definition JaRulePortHandle.h:41
ola::usb::JaRuleWidgetPort::ClaimPort
JaRulePortHandle * ClaimPort()
Claim the handle to this port.
Definition JaRuleWidgetPort.cpp:133
ola::usb::JaRuleWidgetPort::SendCommand
void SendCommand(CommandClass command, const uint8_t *data, unsigned int size, CommandCompleteCallback *callback)
Send a command on this port.
Definition JaRuleWidgetPort.cpp:188
ola::usb::JaRuleWidgetPort::_OutTransferComplete
void _OutTransferComplete()
Called by the libusb callback when the transfer completes or is cancelled.
Definition JaRuleWidgetPort.cpp:247
ola::usb::JaRuleWidgetPort::ReleasePort
void ReleasePort()
Release a handle to a port.
Definition JaRuleWidgetPort.cpp:142
ola::usb::JaRuleWidgetPort::~JaRuleWidgetPort
~JaRuleWidgetPort()
Destructor.
Definition JaRuleWidgetPort.cpp:87
ola::usb::JaRuleWidgetPort::JaRuleWidgetPort
JaRuleWidgetPort(ola::thread::ExecutorInterface *executor, LibUsbAdaptor *adaptor, libusb_device_handle *usb_handle, uint8_t endpoint_number, const ola::rdm::UID &uid, uint8_t physical_port)
Create a new JaRuleWidgetPort.
Definition JaRuleWidgetPort.cpp:68
ola::usb::JaRuleWidgetPort::CancelAll
void CancelAll()
Cancel all commands for this port.
Definition JaRuleWidgetPort.cpp:149
ola::usb::JaRuleWidgetPort::_InTransferComplete
void _InTransferComplete()
Called by the libusb callback when the transfer completes or is cancelled.
Definition JaRuleWidgetPort.cpp:263
ola::usb::LibUsbAdaptor
Wraps calls to libusb so we can test the code.
Definition LibUsbAdaptor.h:36
ola::usb
USB Device drivers.
ola::usb::CommandCompleteCallback
ola::BaseCallback4< void, USBCommandResult, JaRuleReturnCode, uint8_t, const ola::io::ByteString & > CommandCompleteCallback
A command completion callback.
Definition JaRuleConstants.h:167
ola::usb::CommandClass
CommandClass
The Ja Rule command set.
Definition JaRuleConstants.h:101
ola::usb::USBCommandResult
USBCommandResult
Indicates the eventual state of a Ja Rule command.
Definition JaRuleConstants.h:53
ola::usb::JaRuleReturnCode
JaRuleReturnCode
JaRule command return codes.
Definition JaRuleConstants.h:133
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44