36 #include <glog/logging.h>
43 #include <linux/usb/video.h>
44 #include <linux/uvcvideo.h>
46 #include <sys/ioctl.h>
50 #include <linux/uvcvideo.h>
58 r = ioctl(fh, request, arg);
63 if (request == (
int)UVCIOC_CTRL_QUERY) {
65 struct uvc_xu_control_query *cq = (
struct uvc_xu_control_query *)arg;
67 printf(
"%d Calling IOCTL with bRequest %02x, wValue %04x, wIndex "
70 count, cq->query, cq->selector, cq->unit, cq->size);
72 printf(
"Return values: %d \n",
r);
81 uint8_t *BytesToWrite,
82 uint8_t nbBytesToWrite, uint8_t *
data,
83 uint8_t nbPacketBytes,
84 uint8_t nbBytesToRead) {
90 <<
" is greater than the maximum size for a packet which is: "
98 <<
" is greater than the maximum size for a packet which is: "
103 struct uvc_xu_control_query cq;
104 uint8_t packet[nbPacketBytes];
106 if (nbBytesToWrite > 0) {
107 memcpy(packet, BytesToWrite, nbBytesToWrite);
108 packet[nbBytesToWrite] = nbPacketBytes;
112 cq.query = UVC_SET_CUR;
113 cq.data =
static_cast<unsigned char *
>(packet);
114 cq.size = nbPacketBytes;
116 cq.selector = selector;
120 LOG(
WARNING) <<
"Error in sending address to device, error: "
128 cq.query = UVC_GET_CUR;
129 cq.data =
static_cast<unsigned char *
>(packet);
130 cq.size = nbPacketBytes;
132 cq.selector = selector;
141 memcpy(
data, packet, nbBytesToRead);
148 uint32_t bufferLength) {
154 <<
" is greater than the maximum size (255) accepted for an id";
158 uint8_t nbWrPacketBytes =
sizeof(
address) + (
id > -1 ? 1 : 0);
159 uint8_t wrPacket[nbWrPacketBytes];
160 uint32_t *crtAddress =
161 reinterpret_cast<uint32_t *
>(wrPacket + (
id > -1 ? 1 : 0));
163 uint32_t readBytes = 0;
164 uint32_t readLength = 0;
170 while (readBytes < bufferLength) {
172 ? bufferLength - readBytes
178 LOG(
WARNING) <<
"Failed to read a packet via UVC extension unit";
181 *crtAddress += readLength;
182 readBytes += readLength;
190 uint32_t bufferLength) {
196 <<
" is greater than the maximum size (255) accepted for an id";
200 uint8_t nbLeadingBytes =
sizeof(
address) + (
id > -1 ? 1 : 0);
201 struct uvc_xu_control_query cq;
203 uint32_t *crtAddress =
204 reinterpret_cast<uint32_t *
>(packet + (
id > -1 ? 1 : 0));
206 size_t writtenBytes = 0;
210 packet[0] =
static_cast<uint8_t
>(
id);
215 cq.query = UVC_SET_CUR;
216 cq.data =
static_cast<unsigned char *
>(packet);
219 cq.selector = selector;
221 while (writtenBytes < bufferLength) {
223 bufferLength - writtenBytes >
MAX_BUF_SIZE - (nbLeadingBytes + 1)
225 : bufferLength - writtenBytes;
226 packet[nbLeadingBytes] = writeLen;
227 memcpy(&packet[nbLeadingBytes + 1],
data + writtenBytes, writeLen);
231 LOG(
WARNING) <<
"Failed to write a packet via UVC extension unit";
234 writtenBytes += writeLen;
235 *crtAddress += writeLen;
243 uint16_t bufferLength;
246 fd, uvcControlId, -1, 0,
reinterpret_cast<uint8_t *
>(&bufferLength),
247 sizeof(bufferLength));
251 <<
"Failed to read size of buffer holding sensors info. Error: "
256 std::unique_ptr<uint8_t[]>
data(
new uint8_t[bufferLength + 1]);
258 fd, uvcControlId, -1,
sizeof(bufferLength),
data.get(), bufferLength);
260 LOG(
WARNING) <<
"Failed to read the content of buffer holding sensors "
266 data[bufferLength] =
'\0';
267 outStr =
reinterpret_cast<char *
>(
data.get());
273 const uint8_t *
data,
size_t nbBytes) {
274 struct uvc_xu_control_query cq;
275 uint8_t *nonConstBuffer =
const_cast<uint8_t *
>(
data);
278 cq.query = UVC_SET_CUR;
279 cq.data =
static_cast<unsigned char *
>(nonConstBuffer);
282 cq.selector = selector;
289 struct uvc_xu_control_query cq;
292 cq.query = UVC_GET_CUR;
293 cq.data =
static_cast<unsigned char *
>(
data);
296 cq.selector = selector;
303 const uint8_t uvcSendRequestControl = 1;
306 size_t stringLength = requestStr.size();
308 reinterpret_cast<uint8_t *
>(&stringLength),
312 <<
"Failed to write the length of the request string. Error: "
313 << ret <<
"(" <<
strerror(ret) <<
")";
320 size_t writtenBytes = 0;
321 const char *
data = requestStr.data();
323 while (writtenBytes < stringLength) {
326 : stringLength - writtenBytes;
327 memcpy(&packet,
data + writtenBytes, writeLen);
332 LOG(
WARNING) <<
"Failed to write a packet of the send request "
334 << ret <<
"(" <<
strerror(ret) <<
")";
337 writtenBytes += writeLen;
345 const uint8_t uvcGetRequestControl = 2;
354 <<
"Failed to read the length of the response string. Error: "
355 << ret <<
"(" <<
strerror(ret) <<
")";
358 size_t stringLength =
reinterpret_cast<size_t *
>(packet)[0];
362 size_t readBytes = 0;
363 size_t readlength = 0;
365 while (readBytes < stringLength) {
368 : stringLength - readBytes;
374 <<
"Failed to read a packet of the response string. Error: "
375 << ret <<
"(" <<
strerror(ret) <<
")";
378 responseStr.append(
reinterpret_cast<const char *
>(packet), readlength);
379 readBytes += readlength;