35 #include "usb_buffer.pb.h"
41 #include <glog/logging.h>
46 #include <linux/usb/video.h>
47 #include <linux/uvcvideo.h>
48 #include <linux/videodev2.h>
50 #include <unordered_map>
68 struct v4l2_format
fmt;
93 delete[]
it->second.cache;
94 it->second.cache =
nullptr;
126 return Status::UNREACHABLE;
131 m_implData->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
137 return Status::GENERIC_ERROR;
143 usb_payload::ClientRequest requestMsg;
146 requestMsg.SerializeToString(&requestStr);
149 LOG(
ERROR) <<
"Request to get available modes failed";
157 LOG(
ERROR) <<
"Response for get available frame types request failed";
160 usb_payload::ServerResponse responseMsg;
161 bool parsed = responseMsg.ParseFromString(
responseStr);
164 <<
"Failed to deserialize string containing UVC gadget response";
170 <<
"Get available frame types operation failed on UVC gadget";
187 LOG(
INFO) <<
"Device already started";
190 LOG(
INFO) <<
"Starting device";
192 struct v4l2_streamparm fpsControl;
193 memset(&fpsControl, 0,
sizeof(
struct v4l2_streamparm));
195 fpsControl.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
196 fpsControl.parm.capture.timeperframe.numerator = 1;
197 fpsControl.parm.capture.timeperframe.denominator =
m_fps;
204 return Status::GENERIC_ERROR;
207 enum v4l2_buf_type
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
211 return Status::GENERIC_ERROR;
223 LOG(
INFO) <<
"Device already stopped";
226 LOG(
INFO) <<
"Stopping device";
228 enum v4l2_buf_type
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
232 return Status::GENERIC_ERROR;
244 modes.emplace_back(frameType.modeNumber);
256 if (frameDetails.modeNumber ==
mode) {
257 details = frameDetails;
275 usb_payload::ClientRequest requestMsg;
276 auto frameTypeMsg = requestMsg.mutable_mode_details();
281 requestMsg.SerializeToString(&requestStr);
284 LOG(
ERROR) <<
"Set frame type operation failed on UVC gadget";
287 struct v4l2_requestbuffers
req;
290 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
291 req.memory = V4L2_MEMORY_MMAP;
295 return Status::GENERIC_ERROR;
299 struct v4l2_fmtdesc fmtdesc;
300 memset(&fmtdesc, 0,
sizeof(fmtdesc));
301 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
304 struct v4l2_frmsizeenum frmenum;
305 memset(&frmenum, 0,
sizeof frmenum);
306 frmenum.pixel_format = fmtdesc.pixelformat;
309 if ((frmenum.discrete.width ==
type.frameWidthInBytes) &&
310 (frmenum.discrete.height ==
type.frameHeightInBytes)) {
323 LOG(
WARNING) <<
"UVC does not support the requested format "
325 return Status::GENERIC_ERROR;
330 m_implData->fmt.fmt.pix.pixelformat = fmtdesc.pixelformat;
331 m_implData->fmt.fmt.pix.field = V4L2_FIELD_ANY;
337 return Status::GENERIC_ERROR;
342 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343 req.memory = V4L2_MEMORY_MMAP;
352 return Status::GENERIC_ERROR;
357 return Status::GENERIC_ERROR;
367 return Status::GENERIC_ERROR;
372 struct v4l2_buffer
buf;
376 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
377 buf.memory = V4L2_MEMORY_MMAP;
384 return Status::GENERIC_ERROR;
393 PROT_READ | PROT_WRITE,
400 return Status::GENERIC_ERROR;
404 for (
unsigned int i = 0;
i <
m_implData->buffersCount; ++
i) {
405 struct v4l2_buffer
buf;
408 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
409 buf.memory = V4L2_MEMORY_MMAP;
415 return Status::GENERIC_ERROR;
427 LOG(
WARNING) <<
"Invalid adddress to buffer provided";
443 r = select(
m_implData->fd + 1, &fds,
nullptr,
nullptr, &tv);
449 return Status::GENERIC_ERROR;
458 struct v4l2_buffer
buf;
462 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
463 buf.memory = V4L2_MEMORY_MMAP;
478 return Status::GENERIC_ERROR;
489 static_cast<const char *
>(
m_implData->buffers[
buf.index].start);
496 return Status::GENERIC_ERROR;
508 usb_payload::ClientRequest requestMsg;
513 requestMsg.SerializeToString(&requestStr);
516 LOG(
ERROR) <<
"Request to get available controls failed";
524 LOG(
ERROR) <<
"Failed to get response of the request to get controls";
527 usb_payload::ServerResponse responseMsg;
528 bool parsed = responseMsg.ParseFromString(
responseStr);
531 <<
"Failed to deserialize string containing UVC gadget response";
536 LOG(
ERROR) <<
"Get available controls operation failed on UVC gadget";
542 for (
int i = 0;
i < responseMsg.strings_payload_size();
i++) {
543 std::string controlName = responseMsg.strings_payload(
i);
544 controls.push_back(controlName);
555 if (control ==
"fps") {
561 usb_payload::ClientRequest requestMsg;
563 requestMsg.add_func_strings_param(control);
564 requestMsg.add_func_strings_param(
value);
568 requestMsg.SerializeToString(&requestStr);
571 LOG(
ERROR) <<
"Request to set control failed: " << control;
579 LOG(
ERROR) <<
"Failed to get response of the request to set control: "
583 usb_payload::ServerResponse responseMsg;
584 bool parsed = responseMsg.ParseFromString(
responseStr);
587 <<
"Failed to deserialize string containing UVC gadget response";
592 LOG(
ERROR) <<
"Set control:" << control
593 <<
" operation failed on UVC gadget";
606 usb_payload::ClientRequest requestMsg;
608 requestMsg.add_func_strings_param(control);
612 requestMsg.SerializeToString(&requestStr);
615 LOG(
ERROR) <<
"Request to get control: " << control <<
" failed";
623 LOG(
ERROR) <<
"Failed to get response of the request to get control: "
627 usb_payload::ServerResponse responseMsg;
628 bool parsed = responseMsg.ParseFromString(
responseStr);
631 <<
"Failed to deserialize string containing UVC gadget response";
636 LOG(
ERROR) <<
"Get control: " << control
637 <<
" operation failed on UVC gadget";
641 value = responseMsg.strings_payload(0);
658 LOG(
ERROR) <<
"Won't return the handle. Device hasn't been opened yet.";
670 LOG(
INFO) <<
"Function used only on target!";
675 unsigned int usDelay) {
680 usb_payload::ClientRequest requestMsg;
682 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(cmd));
683 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(usDelay));
687 requestMsg.SerializeToString(&requestStr);
690 LOG(
ERROR) <<
"Request to read " << cmd <<
" adsd3500 command failed";
698 LOG(
ERROR) <<
"Failes to get respode to " << cmd <<
" adsd3500 command";
701 usb_payload::ServerResponse responseMsg;
702 bool parsed = responseMsg.ParseFromString(
responseStr);
705 <<
"Failed to deserialize string containing UVC gadget response";
710 LOG(
ERROR) <<
"Adsd3500 command read:" << cmd
711 <<
" operation failed on UVC gadget";
716 memcpy(
data, responseMsg.bytes_payload(0).c_str(),
sizeof(uint16_t));
722 unsigned int usDelay) {
727 usb_payload::ClientRequest requestMsg;
729 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(cmd));
730 requestMsg.add_func_bytes_param(&
data,
sizeof(uint16_t));
734 requestMsg.SerializeToString(&requestStr);
737 LOG(
ERROR) <<
"Request to write " << cmd <<
" adsd3500 command failed";
745 LOG(
ERROR) <<
"Failes to get respode to " << cmd <<
" adsd3500 command";
748 usb_payload::ServerResponse responseMsg;
749 bool parsed = responseMsg.ParseFromString(
responseStr);
752 <<
"Failed to deserialize string containing UVC gadget response";
757 LOG(
ERROR) <<
"Adsd3500 command write:" << cmd
758 <<
" operation failed on UVC gadget";
766 uint8_t *readback_data,
767 uint16_t payload_len) {
772 usb_payload::ClientRequest requestMsg;
773 requestMsg.set_func_name(
775 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(cmd));
776 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(payload_len));
777 requestMsg.add_func_bytes_param(readback_data, 4 *
sizeof(uint8_t));
781 requestMsg.SerializeToString(&requestStr);
784 LOG(
ERROR) <<
"Request to read " << cmd
785 <<
" adsd3500 payload command failed";
793 LOG(
ERROR) <<
"Failed to get respode to " << cmd
794 <<
" adsd3500 payload read command";
797 usb_payload::ServerResponse responseMsg;
798 bool parsed = responseMsg.ParseFromString(
responseStr);
801 <<
"Failed to deserialize string containing UVC gadget response";
806 LOG(
ERROR) <<
"Adsd3500 payload command read:" << cmd
807 <<
" operation failed on UVC gadget";
811 memcpy((uint16_t *)readback_data, responseMsg.bytes_payload(0).c_str(),
812 payload_len *
sizeof(uint8_t));
818 uint16_t payload_len) {
823 usb_payload::ClientRequest requestMsg;
825 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(payload_len));
829 requestMsg.SerializeToString(&requestStr);
832 LOG(
ERROR) <<
"Request to read adsd3500 payload failed";
840 LOG(
ERROR) <<
"Failes to get respode to adsd3500 payload";
843 usb_payload::ServerResponse responseMsg;
844 bool parsed = responseMsg.ParseFromString(
responseStr);
847 <<
"Failed to deserialize string containing UVC gadget response";
852 LOG(
ERROR) <<
"Adsd3500 payload read: operation failed on UVC gadget";
856 memcpy(payload, responseMsg.bytes_payload(0).c_str(),
857 payload_len *
sizeof(uint8_t));
864 uint16_t payload_len) {
869 usb_payload::ClientRequest requestMsg;
870 requestMsg.set_func_name(
872 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(cmd));
873 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(payload_len));
874 requestMsg.add_func_bytes_param(payload, payload_len);
878 requestMsg.SerializeToString(&requestStr);
881 LOG(
ERROR) <<
"Request to write " << cmd
882 <<
" adsd3500 payload command failed";
890 LOG(
ERROR) <<
"Failes to get respode to " << cmd
891 <<
" adsd3500 payload write command";
894 usb_payload::ServerResponse responseMsg;
895 bool parsed = responseMsg.ParseFromString(
responseStr);
898 <<
"Failed to deserialize string containing UVC gadget response";
903 LOG(
ERROR) <<
"Adsd3500 payload command write:" << cmd
904 <<
" operation failed on UVC gadget";
912 uint16_t payload_len) {
917 usb_payload::ClientRequest requestMsg;
919 requestMsg.add_func_int32_param(
static_cast<::
google::int32>(payload_len));
920 requestMsg.add_func_bytes_param(payload, payload_len);
924 requestMsg.SerializeToString(&requestStr);
927 LOG(
ERROR) <<
"Request to write adsd3500 payload failed";
935 LOG(
ERROR) <<
"Failes to get respode to adsd3500 payload write";
939 usb_payload::ServerResponse responseMsg;
940 bool parsed = responseMsg.ParseFromString(
responseStr);
943 <<
"Failed to deserialize string containing UVC gadget response";
948 LOG(
ERROR) <<
"Adsd3500 payload write: operation failed on UVC gadget";
960 usb_payload::ClientRequest requestMsg;
961 requestMsg.set_func_name(usb_payload::FunctionName::ADSD3500_RESET);
965 requestMsg.SerializeToString(&requestStr);
968 LOG(
ERROR) <<
"Request to write adsd3500 payload failed";
976 LOG(
ERROR) <<
"Failes to get respode to adsd3500 payload write";
980 usb_payload::ServerResponse responseMsg;
981 bool parsed = responseMsg.ParseFromString(
responseStr);
984 <<
"Failed to deserialize string containing UVC gadget response";
989 LOG(
ERROR) <<
"Adsd3500 payload write: operation failed on UVC gadget";
998 LOG(
WARNING) <<
"Registering an interrupt callback on a USB connection "
999 "is not supported yet!";
1005 LOG(
WARNING) <<
"Unregistering an interrupt callback on a USB connection "
1006 "is not supported yet!";
1011 int &imagerStatus) {
1018 uint16_t iniFileLength,
1020 uint16_t calDataLength) {
1025 std::map<std::string, std::string> &
params) {
1032 const std::map<std::string, std::string> &
params) {