SDDC_Driver
Loading...
Searching...
No Matches
usb_device.h
1/*
2 * usb_device.h - Basic USB and USB control functions
3 *
4 * Copyright (C) 2020 by Franco Venturi
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GPL-3.0-or-later
20 */
21
22#pragma once
23
24#include "../config.h"
25#include <libusb.h>
26#include <vector>
27#include <string>
28
29typedef struct USBDeviceInfo {
30 // uint8_t index;
31 uint16_t usb_vendor_id;
32 uint16_t usb_product_id;
33 bool need_firmware;
34 std::string manufacturer;
35 std::string product;
36 std::string serial_number;
37 uint8_t usb_bus_number;
38 uint8_t usb_device_address;
40
41enum StreamingStatus {
42 STREAMING_STATUS_OFF,
43 STREAMING_STATUS_READY,
44 STREAMING_STATUS_STREAMING,
45 STREAMING_STATUS_CANCELLED,
46 STREAMING_STATUS_FAILED = 0xff
47};
48
49typedef struct streaming streaming_t;
50
51typedef void (*streaming_read_async_cb_t)(uint32_t data_size, uint8_t *data,
52 void *context);
53
54#define MAX_ENDPOINTS (16)
55#define BULK_XFER_TIMEOUT 5000 // timeout (in ms) for each bulk transfer
56
57
59{
60 public:
61 USBDevice();
62 ~USBDevice();
63
64 std::vector<USBDeviceInfo> getDeviceList();
65
66 sddc_err_t open(USBDeviceInfo dev_select, const char* image, uint32_t size);
67 void close();
68 int control(uint8_t request, uint16_t value, uint16_t index, uint8_t *data, uint16_t length, bool read);
69 int handleEvents();
70
71 int streaming_open_sync();
72 int streaming_open_async(uint32_t frame_size,
73 uint32_t num_frames, streaming_read_async_cb_t callback,
74 void *callback_context);
75 int streaming_framesize();
76 void streaming_close();
77 int streaming_start();
78 int streaming_stop();
79 int streaming_reset_status();
80 int streaming_read_sync(uint8_t *data, int length,
81 int *transferred);
82
83 private:
84 // --- USB --- //
85 libusb_context *usb_ctx = nullptr;
86 libusb_device_handle *dev_handle = nullptr;
87
88 libusb_device* findUSBDevice(USBDeviceInfo device_def, bool strict = true);
89 libusb_device_handle* initializeUSBDevice(libusb_device *device);
90 // --- //
91
92
93 uint8_t bulk_in_endpoint_address = 0;
94 uint16_t bulk_in_max_packet_size = 0;
95 uint8_t bulk_in_max_burst = 0;
96
97 int list_endpoints(struct libusb_endpoint_descriptor endpoints[],
98 struct libusb_ss_endpoint_companion_descriptor ss_endpoints[],
99 libusb_device *device);
100
101 // --- Streaming --- //
102 uint32_t concurrent_transfers;
103 std::vector<struct libusb_transfer*> transfers;
104 std::vector<uint8_t*> transfer_buffers;
105 streaming_t *streaming_obj = nullptr;
106
107 StreamingStatus streaming_status;
108
109 streaming_read_async_cb_t stream_callback;
110 void *stream_callback_context;
111
112 static void LIBUSB_CALL streaming_read_async_callback(struct libusb_transfer *transfer);
113};
Definition usb_device.h:59
int control(uint8_t request, uint16_t value, uint16_t index, uint8_t *data, uint16_t length, bool read)
Send a request to the USB device.
Definition usb_device.cpp:331
Definition usb_device.h:29
Definition streaming.cpp:56