/*! \file piusb.h
* \ingroup USB
* \~\brief
* \~english USB device
* \~russian Устройство USB
*/
/*
PIP - Platform Independent Primitives
USB, based on libusb
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see .
*/
//! \defgroup USB USB
//! \~\brief
//! \~english USB support via libusb
//! \~russian Поддержка USB с помощью libusb
//!
//! \~\details
//! \~english \section cmake_module_USB Building with CMake
//! \~russian \section cmake_module_USB Сборка с использованием CMake
//!
//! \~\code
//! find_package(PIP REQUIRED)
//! target_link_libraries([target] PIP::USB)
//! \endcode
//!
//! \~english \par Common
//! \~russian \par Общее
//!
//! \~english
//! These files provides works with raw USB device
//!
//! \~russian
//! Эти файлы обеспечивают работу с сырым USB устройством
//!
//! \~\authors
//! \~english
//! Ivan Pelipenko peri4ko@yandex.ru;
//! Andrey Bychkov work.a.b@yandex.ru;
//! \~russian
//! Иван Пелипенко peri4ko@yandex.ru;
//! Андрей Бычков work.a.b@yandex.ru;
//!
#ifndef PIUSB_H
#define PIUSB_H
#include "piiodevice.h"
#include "pip_usb_export.h"
struct usb_dev_handle;
class PIP_USB_EXPORT PIUSB: public PIIODevice {
PIIODEVICE(PIUSB, "usb");
public:
explicit PIUSB(ushort vid = 0, ushort pid = 0);
virtual ~PIUSB();
struct PIP_USB_EXPORT Endpoint {
Endpoint(uchar a = 0, uchar at = 0, ushort mps = 0) {
address = a;
attributes = at;
max_packet_size = mps;
parse();
}
enum Direction {
Write = 0,
Read = 1
};
enum TransferType {
Control = 0,
Isochronous = 1,
Bulk = 2,
Interrupt = 3
};
enum SynchronisationType {
NoSynchonisation = 0,
Asynchronous = 2,
Adaptive = 1,
Synchronous = 3
};
enum UsageType {
DataEndpoint = 0,
FeedbackEndpoint = 2,
ExplicitFeedbackDataEndpoint = 1
};
void parse();
bool isNull() const { return address == 0; }
uchar address;
uchar attributes;
ushort max_packet_size;
Direction direction;
TransferType transfer_type;
SynchronisationType synchronisation_type = NoSynchonisation;
UsageType usage_type = DataEndpoint;
};
struct PIP_USB_EXPORT Interface {
uchar index = 0;
uchar value_to_select = 0;
ushort class_code = 0;
ushort subclass_code = 0;
ushort protocol_code = 0;
PIVector endpoints;
};
struct PIP_USB_EXPORT Configuration {
uchar index = 0;
uchar value_to_select = 0;
uchar attributes = 0;
ushort max_power = 0; // mA
bool self_powered = false;
bool remote_wakeup = false;
PIVector interfaces;
};
struct PIP_USB_EXPORT Descriptor {
ushort usb_spec_number = 0;
uchar device_class = 0;
uchar device_subclass = 0;
uchar device_protocol = 0;
uchar max_packet_size = 0;
ushort id_vendor = 0;
ushort id_product = 0;
ushort id_device_release = 0;
uchar index_manufacturer = 0;
uchar index_product = 0;
uchar index_serial = 0;
PIVector configurations;
};
const PIUSB::Descriptor & currentDescriptor() const { return desc_; }
const PIUSB::Configuration & currentConfiguration() const { return conf_; }
const PIUSB::Interface & currentInterface() const { return iface_; }
ushort vendorID() const { return vid_; }
ushort productID() const { return pid_; }
int deviceNumber() const { return property("deviceNumber").toInt(); }
int timeoutRead() const { return property("timeoutRead").toInt(); }
int timeoutWrite() const { return property("timeoutWrite").toInt(); }
const PIUSB::Endpoint & endpointRead() const { return ep_read; }
const PIUSB::Endpoint & endpointWrite() const { return ep_write; }
const PIVector & endpoints() const { return eps; }
PIVector endpointsRead();
PIVector endpointsWrite();
PIUSB::Endpoint getEndpointByAddress(uchar address);
void setVendorID(ushort vid);
void setProductID(ushort pid);
bool setConfiguration(uchar value);
bool setInterface(uchar value);
void setEndpointRead(const PIUSB::Endpoint & ep) { ep_read = ep; }
void setEndpointWrite(const PIUSB::Endpoint & ep) { ep_write = ep; }
void setDeviceNumber(int dn) { setProperty("deviceNumber", dn); }
void setTimeoutRead(int t) { setProperty("timeoutRead", t); }
void setTimeoutWrite(int t) { setProperty("timeoutWrite", t); }
int controlWrite(const void * data, int max_size);
virtual void flush() override;
protected:
bool configureDevice(const void * e_main, const void * e_parent = 0) override;
PIString constructFullPathDevice() const override;
void configureFromFullPathDevice(const PIString & full_path) override;
ssize_t readDevice(void * read_to, ssize_t max_size) override;
ssize_t writeDevice(const void * data, ssize_t max_size) override;
bool openDevice() override;
bool closeDevice() override;
DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Reliable; }
PIVector eps;
ushort vid_, pid_;
int intefrace_, timeout_r, timeout_w;
int interface_claimed;
PIUSB::Endpoint ep_read, ep_write;
Descriptor desc_;
Configuration conf_;
Interface iface_;
usb_dev_handle * hdev;
};
PIP_USB_EXPORT PICout operator<<(PICout s, const PIUSB::Endpoint & v);
#endif