Skip to content

Commit 6742d0b

Browse files
committed
Add HIDAPI support
not work with -m option. Add -hidapi option. Signed-off-by: Frank Li <frank.li@nxp.com>
1 parent d669525 commit 6742d0b

12 files changed

Lines changed: 300 additions & 16 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "zstd"]
1111
path = zstd
1212
url = https://github.com/facebook/zstd.git
13+
[submodule "hidapi"]
14+
path = hidapi
15+
url = https://github.com/libusb/hidapi.git

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Note that, since uuu is an OSI compliant Open Source project, you are entitled t
7272
## Linux
7373
- `git clone https://github.com/nxp-imx/mfgtools.git`
7474
- `cd mfgtools`
75-
- `sudo apt-get install libusb-1.0-0-dev libbz2-dev libzstd-dev pkg-config cmake libssl-dev g++`
75+
- `sudo apt-get install libusb-1.0-0-dev libbz2-dev libzstd-dev libhid-dev pkg-config cmake libssl-dev g++`
7676
- `cmake . && make`
7777

7878
The above commands build mfgtools in source. To build it out of source
@@ -87,7 +87,7 @@ For cmake prior 3.13:
8787
## macOS
8888
- `git clone https://github.com/nxp-imx/mfgtools.git`
8989
- `cd mfgtools`
90-
- `brew install cmake libusb openssl pkg-config`
90+
- `brew install cmake libusb openssl hidapi pkg-config`
9191
- `cmake -DOPENSSL_ROOT_DIR=$(brew --prefix)/opt/openssl . && make`
9292

9393
Note that we assume [brew](https://brew.sh) is installed and can be used to resolve dependencies as shown above. The remaining dependency `libbz2` can be resolved via the XCode supplied libraries.

libuuu/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ set(CMAKE_SKIP_RPATH ON)
66

77
find_package(BZip2 REQUIRED)
88
find_package(PkgConfig REQUIRED)
9+
if(APPLE)
10+
pkg_check_modules(LIBHID REQUIRED hidapi)
11+
else()
12+
pkg_check_modules(LIBHID REQUIRED hidapi-libusb)
13+
endif()
914
pkg_check_modules(LIBUSB REQUIRED libusb-1.0>=1.0.16)
1015
pkg_check_modules(LIBZSTD REQUIRED libzstd)
1116
find_package(Threads)
@@ -21,7 +26,7 @@ set(UUUSSL "-DUUUSSL")
2126
set(UUUOPENSLL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
2227
endif()
2328

24-
include_directories(${LIBUSB_INCLUDE_DIRS} ${LIBZSTD_INCLUDE_DIRS} ${UUUOPENSLL_INCLUDE_DIR} include)
29+
include_directories(${LIBUSB_INCLUDE_DIRS} ${LIBZSTD_INCLUDE_DIRS} ${UUUOPENSLL_INCLUDE_DIR} ${LIBHID_INCLUDE_DIRS} include)
2530

2631

2732
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wstrict-aliasing -Wextra ${UUUSSL}")

libuuu/cmd.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class CmdCtx
5151
ConfigItem *m_config_item = nullptr;
5252
void *m_dev = nullptr;
5353
short m_current_bcd;
54+
bool m_bHIDAPI = false;
5455
};
5556

5657
class CmdUsbCtx : public CmdCtx
@@ -219,4 +220,5 @@ int run_cmd(CmdCtx *pCtx, const char * cmd, int dry);
219220
int insert_env_variable(std::string key, std::string value);
220221
std::string get_env_variable(std::string key);
221222
int clear_env();
222-
bool is_evn_exist(std::string key);
223+
bool is_evn_exist(std::string key);
224+
bool is_using_hidapi();

libuuu/config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <cstdint>
3535
#include <string>
3636
#include <vector>
37+
#include <string.h>
3738

3839
class ConfigItem
3940
{
@@ -48,6 +49,10 @@ class ConfigItem
4849
m_chip = chip;
4950
if (comp)
5051
m_compatible = comp;
52+
53+
m_bHID = (strcmp(pro, "SDP:") == 0)
54+
|| (strcmp(pro, "SDPS:") == 0)
55+
|| (strcmp(pro, "SDPV:") == 0);
5156
}
5257
std::string m_protocol;
5358
std::string m_chip;
@@ -56,6 +61,7 @@ class ConfigItem
5661
uint16_t m_vid = 0;
5762
uint16_t m_bcdVerMin = 0;
5863
uint16_t m_bcdVerMax = UINT16_MAX;
64+
bool m_bHID;
5965
};
6066

6167
class Config :public std::vector<ConfigItem>

libuuu/libuuu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ void uuu_set_debug_level(uint32_t mask);
144144
*/
145145
void uuu_set_small_mem(uint32_t val);
146146

147+
148+
void uuu_set_using_hidapi(uint32_t val);
149+
147150
#define MAX_USER_LEN 128
148151
typedef int (*uuu_askpasswd)(char* prompt, char user[MAX_USER_LEN], char passwd[MAX_USER_LEN]);
149152
int uuu_set_askpasswd(uuu_askpasswd ask);

libuuu/trans.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "liberror.h"
3535
#include "libusb.h"
3636
#include "zip.h"
37+
#include "cmd.h"
38+
#include "hidapi.h"
3739

3840
extern "C"
3941
{
@@ -105,6 +107,11 @@ int USBTrans::close()
105107

106108
int HIDTrans::open(void *p)
107109
{
110+
m_devhandle = p;
111+
112+
if (is_using_hidapi())
113+
return 0;
114+
108115
if (USBTrans::open(p))
109116
return -1;
110117

@@ -120,6 +127,16 @@ int HIDTrans::open(void *p)
120127
int HIDTrans::write(void *buff, size_t size)
121128
{
122129
int ret;
130+
131+
if (is_using_hidapi())
132+
{
133+
ret = hid_write((hid_device*)m_devhandle, (const unsigned char *)buff, size);
134+
if (ret < 0)
135+
set_last_err_string("hid_write() error");
136+
137+
return ret;
138+
}
139+
123140
uint8_t *p = (uint8_t *)buff;
124141
int actual_size;
125142

@@ -163,6 +180,19 @@ int HIDTrans::write(void *buff, size_t size)
163180
int HIDTrans::read(void *buff, size_t size, size_t *rsize)
164181
{
165182
int ret;
183+
184+
if (is_using_hidapi())
185+
{
186+
ret = hid_read_timeout((hid_device*)m_devhandle, (unsigned char*)buff, size, m_timeout);
187+
if (ret < 0)
188+
{
189+
set_last_err_string("hid_read() error");
190+
return ret;
191+
}
192+
*rsize = ret;
193+
return 0;
194+
}
195+
166196
int actual;
167197
ret = libusb_interrupt_transfer(
168198
(libusb_device_handle *)m_devhandle,

0 commit comments

Comments
 (0)