Skip to content

Commit 3ef9bad

Browse files
committed
Support homebrew builds better, add additional methods
1 parent 298e9d4 commit 3ef9bad

File tree

8 files changed

+250
-1
lines changed

8 files changed

+250
-1
lines changed

CMakeLists.txt

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
cmake_minimum_required(VERSION 2.8.4)
2+
cmake_policy(SET CMP0042 NEW)
3+
cmake_policy(SET CMP0079 NEW)
24
project(usbpp C CXX)
5+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/" ${CMAKE_MODULE_PATH})
6+
LINK_DIRECTORIES(/usr/local/lib /usr/local/opt)
7+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
8+
39

410
if (NOT BINDIR)
511
set(BINDIR "bin")
@@ -15,18 +21,27 @@ if (NOT INSTALL_PKGCONFIG_DIR)
1521
endif()
1622

1723
find_package(PkgConfig REQUIRED)
24+
find_package(libusb-1.0)
1825
pkg_check_modules(LIBUSB REQUIRED libusb-1.0>=1.0.16)
1926
find_package(Threads)
2027

2128
set(CMAKE_CXX_FLAGS "-std=c++11")
2229
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wstrict-aliasing -Wextra")
23-
include_directories(${LIBUSB_INCLUDE_DIRS} include)
30+
include_directories(SYSTEM ${LIBUSB_1_INCLUDE_DIRS})
31+
include_directories(include)
32+
2433

2534
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2635

2736
add_subdirectory(examples)
2837
add_subdirectory(include)
2938
add_subdirectory(src)
3039

40+
include(CMakePackageConfigHelpers)
41+
write_basic_package_version_file(
42+
"${CMAKE_CURRENT_BINARY_DIR}/usbpp-config.cmake"
43+
VERSION 1.0
44+
COMPATIBILITY AnyNewerVersion
45+
)
3146
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/usbpp.pc.in ${CMAKE_CURRENT_BINARY_DIR}/usbpp.pc @ONLY)
3247
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/usbpp.pc DESTINATION "${INSTALL_PKGCONFIG_DIR}")

cmake/Findlibusb-1.0.cmake

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# https://github.com/myriadrf/LimeSuite/blob/master/cmake/Modules/Findlibusb-1.0.cmake
2+
# - Try to find libusb-1.0
3+
# Once done this will define
4+
#
5+
# LIBUSB_1_FOUND - system has libusb
6+
# LIBUSB_1_INCLUDE_DIRS - the libusb include directory
7+
# LIBUSB_1_LIBRARIES - Link these to use libusb
8+
# LIBUSB_1_DEFINITIONS - Compiler switches required for using libusb
9+
#
10+
# Adapted from cmake-modules Google Code project
11+
#
12+
# Copyright (c) 2006 Andreas Schneider <[email protected]>
13+
#
14+
# (Changes for libusb) Copyright (c) 2008 Kyle Machulis <[email protected]>
15+
#
16+
# Redistribution and use is allowed according to the terms of the New BSD license.
17+
#
18+
# CMake-Modules Project New BSD License
19+
#
20+
# Redistribution and use in source and binary forms, with or without
21+
# modification, are permitted provided that the following conditions are met:
22+
#
23+
# * Redistributions of source code must retain the above copyright notice, this
24+
# list of conditions and the following disclaimer.
25+
#
26+
# * Redistributions in binary form must reproduce the above copyright notice,
27+
# this list of conditions and the following disclaimer in the
28+
# documentation and/or other materials provided with the distribution.
29+
#
30+
# * Neither the name of the CMake-Modules Project nor the names of its
31+
# contributors may be used to endorse or promote products derived from this
32+
# software without specific prior written permission.
33+
#
34+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
35+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
36+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
38+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44+
#
45+
46+
47+
if (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS)
48+
# in cache already
49+
set(LIBUSB_FOUND TRUE)
50+
else (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS)
51+
find_path(LIBUSB_1_INCLUDE_DIR
52+
NAMES
53+
libusb.h
54+
PATHS
55+
/usr/include
56+
/usr/local/include
57+
/opt/local/include
58+
/sw/include
59+
PATH_SUFFIXES
60+
libusb-1.0
61+
)
62+
63+
set(LIBUSB_1_LIBRARY_NAME usb-1.0)
64+
if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
65+
set(LIBUSB_1_LIBRARY_NAME usb)
66+
endif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
67+
68+
if(NOT bananapi-r2)
69+
find_library(LIBUSB_1_LIBRARY
70+
NAMES ${LIBUSB_1_LIBRARY_NAME}
71+
PATHS
72+
/usr/lib
73+
/usr/local/lib
74+
/opt/local/lib
75+
/sw/lib
76+
)
77+
endif(NOT bananapi-r2)
78+
79+
#bananapi-r2 ubuntu 16.04 has wrong libusb-1.0.so path
80+
#when compiling on bananapi-r2 run cmake with -DBananapi-r2=YES argument like so:
81+
# $(builddir)/cmake -Dbananapi-r2=YES ../
82+
if(bananapi-r2)
83+
message(STATUS "DEBUG --->>>> BANANAPI")
84+
find_library(LIBUSB_1_LIBRARY
85+
NAMES ${LIBUSB_1_LIBRARY_NAME}
86+
PATHS
87+
/lib/arm-linux-gnueabihf/
88+
/usr/lib/arm-linux-gnueabihf/
89+
NO_DEFAULT_PATH
90+
)
91+
endif(bananapi-r2)
92+
93+
set(LIBUSB_1_INCLUDE_DIRS
94+
${LIBUSB_1_INCLUDE_DIR}
95+
)
96+
set(LIBUSB_1_LIBRARIES
97+
${LIBUSB_1_LIBRARY}
98+
)
99+
100+
if (LIBUSB_1_INCLUDE_DIRS AND LIBUSB_1_LIBRARIES)
101+
set(LIBUSB_1_FOUND TRUE)
102+
endif (LIBUSB_1_INCLUDE_DIRS AND LIBUSB_1_LIBRARIES)
103+
104+
if (LIBUSB_1_FOUND)
105+
if (NOT libusb_1_FIND_QUIETLY)
106+
message(STATUS "Found libusb-1.0:")
107+
message(STATUS " - Includes: ${LIBUSB_1_INCLUDE_DIRS}")
108+
message(STATUS " - Libraries: ${LIBUSB_1_LIBRARIES}")
109+
endif (NOT libusb_1_FIND_QUIETLY)
110+
else (LIBUSB_1_FOUND)
111+
if (libusb_1_FIND_REQUIRED)
112+
message(FATAL_ERROR "Could not find libusb")
113+
endif (libusb_1_FIND_REQUIRED)
114+
endif (LIBUSB_1_FOUND)
115+
116+
# show the LIBUSB_1_INCLUDE_DIRS and LIBUSB_1_LIBRARIES variables only in the advanced view
117+
mark_as_advanced(LIBUSB_1_INCLUDE_DIRS LIBUSB_1_LIBRARIES)
118+
119+
endif (LIBUSB_1_LIBRARIES AND LIBUSB_1_INCLUDE_DIRS)

include/context.h

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <functional>
2222
#include <memory>
2323
#include <vector>
24+
#include <libusb.h>
2425

2526
#include "device.h"
2627
#include "exception.h"
@@ -101,6 +102,9 @@ class Context {
101102
*/
102103
Context& operator=(Context&& other) noexcept;
103104

105+
template <typename ... Ts>
106+
void setOption(libusb_option option, Ts ... args);
107+
104108
/**
105109
* Get list of currently attached USB devices.
106110
*

include/device.h

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <exception>
2222
#include <memory>
2323
#include <string>
24+
#include <vector>
2425

2526
#include "buffer.h"
2627
#include "exception.h"
@@ -157,6 +158,21 @@ class Device {
157158
*/
158159
libusb_device_descriptor getDescriptor();
159160

161+
/**
162+
*
163+
* @param desc
164+
* @param key
165+
* @return
166+
*/
167+
std::string* getStringDescriptor(int key);
168+
169+
uint8_t getBusNumber() const;
170+
171+
uint8_t getDeviceAddress() const;
172+
173+
static const int MAX_PORT_NUMBERS = 255;
174+
std::vector<uint8_t> *getPortNumbers();
175+
160176
/**
161177
* Get the device configuration.
162178
*

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_library(usbpp SHARED
55
hiddevice.cpp hidreport.cpp # HID support
66
mscbw.cpp mscsw.cpp msdevice.cpp msscsiinquiry.cpp msscsiinquiryresponse.cpp # mass storage
77
)
8+
find_package(libusb-1.0)
89
target_link_libraries(usbpp ${LIBUSB_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
910

1011
install(TARGETS usbpp DESTINATION ${LIBDIR})

src/context.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ Context& Context::operator=(Context&& other) noexcept {
254254
return *this;
255255
}
256256

257+
template <typename ... Ts>
258+
void Context::setOption(libusb_option option, Ts ... args) {
259+
libusb_set_option( pimpl->m_ctx, option, args...);
260+
}
261+
257262
std::vector<Device> Context::getDevices() {
258263
libusb_device** devices;
259264
int count = libusb_get_device_list(pimpl->m_ctx, &devices);

src/device.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
#include "device.h"
1919

20+
#include <cstring>
2021
#include <cassert>
2122
#include <libusb.h>
2223
#include <unistd.h>
2324
#include <unordered_map>
2425
#include <unordered_set>
2526
#include <sstream>
27+
#include <vector>
2628

2729
#include "deviceimpl.h"
2830

@@ -212,6 +214,56 @@ libusb_device_descriptor Device::getDescriptor() {
212214
return desc;
213215
}
214216

217+
std::string* Device::getStringDescriptor(const int key) {
218+
/** Pre-allocated maximum length for getStringDescriptor calls. Calls with maxlen > n will throw */
219+
const int STRING_DESCRIPTOR_MAXLEN = 1024;
220+
/** Pre-allocated buffer for libusb_get_string_descriptor_ascii, using above constant */
221+
unsigned char data[STRING_DESCRIPTOR_MAXLEN];
222+
if (pimpl->m_handle == NULL) {
223+
return nullptr;
224+
}
225+
// pre-zero
226+
std::memset(data, 0, STRING_DESCRIPTOR_MAXLEN);
227+
libusb_get_string_descriptor_ascii(pimpl->m_handle, key, data, STRING_DESCRIPTOR_MAXLEN);
228+
uint16_t data_length;
229+
for (data_length=0; data_length<STRING_DESCRIPTOR_MAXLEN; data_length++) {
230+
if (data[data_length] == 0) break;
231+
}
232+
// return null when no result, don't bother allocating an empty string
233+
if (data_length == 0) {
234+
return nullptr;
235+
}
236+
return new std::string(reinterpret_cast<const char *>(data));
237+
}
238+
239+
uint8_t Device::getBusNumber() const{
240+
if (pimpl->m_device == NULL) {
241+
return 0;
242+
}
243+
244+
return libusb_get_bus_number(pimpl->m_device);
245+
}
246+
247+
uint8_t Device::getDeviceAddress() const {
248+
return libusb_get_device_address(pimpl->m_device);
249+
}
250+
251+
std::vector<uint8_t> *Device::getPortNumbers() {
252+
uint8_t ports[Usbpp::Device::MAX_PORT_NUMBERS]; // these are not chars, actually uints- these are port numbers (or something similar)
253+
std::vector<uint8_t> *ports_vec = new std::vector<uint8_t>();
254+
255+
if (pimpl->m_device != NULL) {
256+
int count = libusb_get_port_numbers(pimpl->m_device, ports, Usbpp::Device::MAX_PORT_NUMBERS);
257+
if (count > 0) {
258+
ports_vec->reserve(count);
259+
for (int i = 0; i < count; i++) {
260+
ports_vec->push_back(ports[i]);
261+
}
262+
}
263+
}
264+
return ports_vec;
265+
}
266+
215267
int Device::getConfiguration() {
216268
int config;
217269
int res = libusb_get_configuration(pimpl->m_handle, &config);

usbpp-config.cmake

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is a basic version file for the Config-mode of find_package().
2+
# It is used by write_basic_package_version_file() as input file for configure_file()
3+
# to create a version-file which can be installed along a config.cmake file.
4+
#
5+
# The created file sets PACKAGE_VERSION_EXACT if the current version string and
6+
# the requested version string are exactly the same and it sets
7+
# PACKAGE_VERSION_COMPATIBLE if the current version is >= requested version.
8+
# The variable CVF_VERSION must be set before calling configure_file().
9+
10+
set(PACKAGE_VERSION "1.0")
11+
12+
if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
13+
set(PACKAGE_VERSION_COMPATIBLE FALSE)
14+
else()
15+
set(PACKAGE_VERSION_COMPATIBLE TRUE)
16+
if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
17+
set(PACKAGE_VERSION_EXACT TRUE)
18+
endif()
19+
endif()
20+
21+
22+
# if the installed project requested no architecture check, don't perform the check
23+
if("FALSE")
24+
return()
25+
endif()
26+
27+
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
28+
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "4" STREQUAL "")
29+
return()
30+
endif()
31+
32+
# check that the installed version has the same 32/64bit-ness as the one which is currently searching:
33+
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "4")
34+
math(EXPR installedBits "4 * 8")
35+
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
36+
set(PACKAGE_VERSION_UNSUITABLE TRUE)
37+
endif()

0 commit comments

Comments
 (0)