Skip to content

Commit

Permalink
Add HIDAPI support
Browse files Browse the repository at this point in the history
not work with -m option.
Add -hidapi option.

Signed-off-by: Frank Li <frank.li@nxp.com>
  • Loading branch information
nxpfrankli committed Jul 24, 2023
1 parent d669525 commit 6742d0b
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "zstd"]
path = zstd
url = https://github.com/facebook/zstd.git
[submodule "hidapi"]
path = hidapi
url = https://github.com/libusb/hidapi.git
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Note that, since uuu is an OSI compliant Open Source project, you are entitled t
## Linux
- `git clone https://github.com/nxp-imx/mfgtools.git`
- `cd mfgtools`
- `sudo apt-get install libusb-1.0-0-dev libbz2-dev libzstd-dev pkg-config cmake libssl-dev g++`
- `sudo apt-get install libusb-1.0-0-dev libbz2-dev libzstd-dev libhid-dev pkg-config cmake libssl-dev g++`
- `cmake . && make`

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

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.
Expand Down
7 changes: 6 additions & 1 deletion libuuu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ set(CMAKE_SKIP_RPATH ON)

find_package(BZip2 REQUIRED)
find_package(PkgConfig REQUIRED)
if(APPLE)
pkg_check_modules(LIBHID REQUIRED hidapi)
else()
pkg_check_modules(LIBHID REQUIRED hidapi-libusb)
endif()
pkg_check_modules(LIBUSB REQUIRED libusb-1.0>=1.0.16)
pkg_check_modules(LIBZSTD REQUIRED libzstd)
find_package(Threads)
Expand All @@ -21,7 +26,7 @@ set(UUUSSL "-DUUUSSL")
set(UUUOPENSLL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
endif()

include_directories(${LIBUSB_INCLUDE_DIRS} ${LIBZSTD_INCLUDE_DIRS} ${UUUOPENSLL_INCLUDE_DIR} include)
include_directories(${LIBUSB_INCLUDE_DIRS} ${LIBZSTD_INCLUDE_DIRS} ${UUUOPENSLL_INCLUDE_DIR} ${LIBHID_INCLUDE_DIRS} include)


set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wstrict-aliasing -Wextra ${UUUSSL}")
Expand Down
4 changes: 3 additions & 1 deletion libuuu/cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class CmdCtx
ConfigItem *m_config_item = nullptr;
void *m_dev = nullptr;
short m_current_bcd;
bool m_bHIDAPI = false;
};

class CmdUsbCtx : public CmdCtx
Expand Down Expand Up @@ -219,4 +220,5 @@ int run_cmd(CmdCtx *pCtx, const char * cmd, int dry);
int insert_env_variable(std::string key, std::string value);
std::string get_env_variable(std::string key);
int clear_env();
bool is_evn_exist(std::string key);
bool is_evn_exist(std::string key);
bool is_using_hidapi();
6 changes: 6 additions & 0 deletions libuuu/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <cstdint>
#include <string>
#include <vector>
#include <string.h>

class ConfigItem
{
Expand All @@ -48,6 +49,10 @@ class ConfigItem
m_chip = chip;
if (comp)
m_compatible = comp;

m_bHID = (strcmp(pro, "SDP:") == 0)
|| (strcmp(pro, "SDPS:") == 0)
|| (strcmp(pro, "SDPV:") == 0);
}
std::string m_protocol;
std::string m_chip;
Expand All @@ -56,6 +61,7 @@ class ConfigItem
uint16_t m_vid = 0;
uint16_t m_bcdVerMin = 0;
uint16_t m_bcdVerMax = UINT16_MAX;
bool m_bHID;
};

class Config :public std::vector<ConfigItem>
Expand Down
3 changes: 3 additions & 0 deletions libuuu/libuuu.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ void uuu_set_debug_level(uint32_t mask);
*/
void uuu_set_small_mem(uint32_t val);


void uuu_set_using_hidapi(uint32_t val);

#define MAX_USER_LEN 128
typedef int (*uuu_askpasswd)(char* prompt, char user[MAX_USER_LEN], char passwd[MAX_USER_LEN]);
int uuu_set_askpasswd(uuu_askpasswd ask);
Expand Down
30 changes: 30 additions & 0 deletions libuuu/trans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "liberror.h"
#include "libusb.h"
#include "zip.h"
#include "cmd.h"
#include "hidapi.h"

extern "C"
{
Expand Down Expand Up @@ -105,6 +107,11 @@ int USBTrans::close()

int HIDTrans::open(void *p)
{
m_devhandle = p;

if (is_using_hidapi())
return 0;

if (USBTrans::open(p))
return -1;

Expand All @@ -120,6 +127,16 @@ int HIDTrans::open(void *p)
int HIDTrans::write(void *buff, size_t size)
{
int ret;

if (is_using_hidapi())
{
ret = hid_write((hid_device*)m_devhandle, (const unsigned char *)buff, size);
if (ret < 0)
set_last_err_string("hid_write() error");

return ret;
}

uint8_t *p = (uint8_t *)buff;
int actual_size;

Expand Down Expand Up @@ -163,6 +180,19 @@ int HIDTrans::write(void *buff, size_t size)
int HIDTrans::read(void *buff, size_t size, size_t *rsize)
{
int ret;

if (is_using_hidapi())
{
ret = hid_read_timeout((hid_device*)m_devhandle, (unsigned char*)buff, size, m_timeout);
if (ret < 0)
{
set_last_err_string("hid_read() error");
return ret;
}
*rsize = ret;
return 0;
}

int actual;
ret = libusb_interrupt_transfer(
(libusb_device_handle *)m_devhandle,
Expand Down
Loading

0 comments on commit 6742d0b

Please sign in to comment.