USBUART
A library for reading/wring data via USB-UART adapters
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules
generic.cpp
Go to the documentation of this file.
1 
5 /* This file is part of USBUART Library. http://usbuart.info/
6  *
7  * Copyright (C) 2016 Eugene Hutorny <eugene@hutorny.in.ua>
8  *
9  * The USBUART Library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License v2
11  * as published by the Free Software Foundation;
12  *
13  * The USBUART Library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with the USBUART Library; if not, see
20  * <http://www.gnu.org/licenses/gpl-2.0.html>.
21  */
22 
23 #include <cerrno>
24 #include <cstring>
25 #include <endian.h>
26 #include <libusb.h>
27 #include "usbuart.hpp"
28 
29 namespace usbuart {
30 static constexpr uint8_t vendor_reqo =
31  (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT);
32 static constexpr uint8_t vendor_reqi =
33  (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN);
34 
35 void generic::write_cv(uint8_t req, uint16_t val, uint16_t index)
36  const throw(error_t) {
37  if( int r = libusb_control_transfer(dev,
38  vendor_reqo, req, val, index, nullptr, 0, timeout) < 0) {
39  log.e(__, "control transfer %02x,%02x,%04x,%04x "
40  "fail with error %d: %s\n", vendor_reqo, req, val, index, r,
41  libusb_error_name(r));
43  }
44 }
45 
46 void generic::control(uint8_t reqtype, uint8_t req, void* data, size_t size)
47  const throw(error_t) {
48  if( int r = libusb_control_transfer(dev,
49  reqtype, req, 0, 0, (unsigned char*)data, size, timeout) < 0 ) {
50  log.e(__,"control transfer %02x,%02x,%04x,%04x "
51  "fail with error %d: %s\n", reqtype, req,
52  0, 0, r, libusb_error_name(r));
54  }
55 }
56 
57 
58 void generic::read_cv(uint8_t req, uint16_t val, uint8_t& dst)
59  const throw(error_t) {
60  if( int r = libusb_control_transfer(dev,
61  vendor_reqi, req, val, 0, &dst, 1, timeout) != 1 ) {
62  log.e(__,"control transfer %02x,%02x,%04x,%04x "
63  "fail with error %d: %s\n", vendor_reqi, req,
64  val, 0, r, libusb_error_name(r));
66  }
67 }
68 
69 void generic::read_cv(uint8_t req, uint16_t val, uint16_t& dst)
70  const throw(error_t) {
71  if( int r = libusb_control_transfer(dev,
72  vendor_reqi, req, val, 0, (unsigned char*)&dst, 1, timeout) != 2 ) {
73  log.e(__,"control transfer %02x,%02x,%04x,%04x "
74  "fail with error %d: %s\n", vendor_reqi, req,
75  val, 0, r, libusb_error_name(r));
77  }
78  dst = le16toh(dst);
79 }
80 
81 void generic::claim_interface() const throw(error_t) {
82  int r = libusb_claim_interface(dev, ifcnum);
83  if( r == 0 ) return;
84  int err = errno;
85  log.e(__,"claim interface %d fail %d: %s\n", ifcnum, r, libusb_error_name(r));
86  if( err )
87  log.e(__,"%s\n", strerror(err));
88  //FIXME try to release kernel driver
89  //
90  switch( r ) {
91  case LIBUSB_ERROR_NO_DEVICE: throw error_t::no_device;
92  case LIBUSB_ERROR_NOT_FOUND: throw error_t::no_interface;
93  case LIBUSB_ERROR_BUSY: throw error_t::interface_busy;
94  }
95  switch( err ) {
96  case EACCES:
97  case -EACCES: throw error_t::no_access;
98  }
99  throw error_t::usb_error;
100 }
101 
102 generic::~generic() noexcept {
103  libusb_release_interface(dev, ifcnum);
104  /* libusb_close(dev); must not be here because dev should survive probe */
105 }
106 
107 }
requested interface busy
implementation header USBUART Library
device does not exist
USBUART namespace.
Definition: usbuart.h:29
control transfer error
claim interface failed
error_t
API Error codes.
Definition: usbuart.h:177
access permission denied