Skip to content

Commit

Permalink
Merge pull request #710 from davidgiven/usb
Browse files Browse the repository at this point in the history
Make work on FreeBSD
  • Loading branch information
davidgiven authored Aug 19, 2023
2 parents 90da6b1 + 95b703b commit 0d495ed
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 20 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ ifeq ($(shell uname),Darwin)
-framework Foundation
endif

ifeq ($(shell uname),FreeBSD)
PLATFORM = FreeBSD
CFLAGS += -I/usr/local/include
endif

#Check the Make version.


Expand Down
7 changes: 6 additions & 1 deletion dep/libusbp/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LIBUSBP_SRCS += \
dep/libusbp/src/mac/list_mac.c \
dep/libusbp/src/mac/serial_port_mac.c \

else
else ifeq ($(shell uname),Linux)

LIBUSBP_CFLAGS += $(shell pkg-config --cflags libudev)
LIBUSBP_LDFLAGS += $(shell pkg-config --libs libudev)
Expand All @@ -48,6 +48,11 @@ LIBUSBP_SRCS += \
dep/libusbp/src/linux/udev_linux.c \
dep/libusbp/src/linux/usbfd_linux.c \

else

LIBUSBP_SRCS += \
dep/libusbp/src/dummy.c

endif

LIBUSBP_OBJS = $(patsubst %.c, $(OBJDIR)/%.o, $(LIBUSBP_SRCS))
Expand Down
103 changes: 103 additions & 0 deletions dep/libusbp/src/dummy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

// This file contains failing place-holders to make things compile
// on otherwise unsupported platforms.

#include <libusbp_internal.h>

struct libusbp_device
{
char* syspath;
char* serial_number; // may be NULL
uint16_t product_id;
uint16_t vendor_id;
uint16_t revision;
};

static libusbp_error* fail()
{
return error_create("USB hardware is not supported on this platform");
}

libusbp_error* libusbp_device_copy(
const libusbp_device* source, libusbp_device** dest)
{
return fail();
}

libusbp_error* libusbp_generic_interface_create(const libusbp_device* device,
uint8_t interface_number,
bool composite __attribute__((unused)),
libusbp_generic_interface** gi)
{
return fail();
}

libusbp_error* libusbp_generic_handle_open(
const libusbp_generic_interface* gi, libusbp_generic_handle** handle)
{
return fail();
}

void libusbp_device_free(libusbp_device* device) {}

void libusbp_generic_handle_close(libusbp_generic_handle* handle) {}

void libusbp_generic_interface_free(libusbp_generic_interface* gi) {}

libusbp_error* libusbp_device_get_vendor_id(
const libusbp_device* device, uint16_t* vendor_id)
{
return fail();
}

libusbp_error* libusbp_device_get_product_id(
const libusbp_device* device, uint16_t* product_id)
{
return fail();
}

libusbp_error* libusbp_device_get_serial_number(
const libusbp_device* device, char** serial_number)
{
return fail();
}

libusbp_error* libusbp_write_pipe(libusbp_generic_handle* handle,
uint8_t pipe_id,
const void* data,
size_t size,
size_t* transferred)
{
return fail();
}

libusbp_error* libusbp_read_pipe(libusbp_generic_handle* handle,
uint8_t pipe_id,
void* data,
size_t size,
size_t* transferred)
{
return fail();
}

libusbp_error* libusbp_serial_port_create(const libusbp_device* device,
uint8_t interface_number,
bool composite,
libusbp_serial_port** port)
{
return fail();
}

libusbp_error* libusbp_serial_port_get_name(
const libusbp_serial_port* port, char** name)
{
return fail();
}

void libusbp_serial_port_free(libusbp_serial_port* port) {}

libusbp_error* libusbp_list_connected_devices(
libusbp_device*** device_list, size_t* device_count)
{
return fail();
}
45 changes: 26 additions & 19 deletions lib/usb/usbfinder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,39 @@ static const std::string get_serial_number(const libusbp::device& device)

std::vector<std::shared_ptr<CandidateDevice>> findUsbDevices()
{
std::vector<std::shared_ptr<CandidateDevice>> candidates;
for (const auto& it : libusbp::list_connected_devices())
try
{
auto candidate = std::make_unique<CandidateDevice>();
candidate->device = it;

uint32_t id = (it.get_vendor_id() << 16) | it.get_product_id();
if (VALID_DEVICES.find(id) != VALID_DEVICES.end())
std::vector<std::shared_ptr<CandidateDevice>> candidates;
for (const auto& it : libusbp::list_connected_devices())
{
candidate->id = id;
candidate->serial = get_serial_number(it);
auto candidate = std::make_unique<CandidateDevice>();
candidate->device = it;

if (id == GREASEWEAZLE_ID)
uint32_t id = (it.get_vendor_id() << 16) | it.get_product_id();
if (VALID_DEVICES.find(id) != VALID_DEVICES.end())
{
libusbp::serial_port port(candidate->device);
candidate->serialPort = port.get_name();
candidate->type = DEVICE_GREASEWEAZLE;
}
else if (id == FLUXENGINE_ID)
candidate->type = DEVICE_FLUXENGINE;
candidate->id = id;
candidate->serial = get_serial_number(it);

candidates.push_back(std::move(candidate));
if (id == GREASEWEAZLE_ID)
{
libusbp::serial_port port(candidate->device);
candidate->serialPort = port.get_name();
candidate->type = DEVICE_GREASEWEAZLE;
}
else if (id == FLUXENGINE_ID)
candidate->type = DEVICE_FLUXENGINE;

candidates.push_back(std::move(candidate));
}
}
}

return candidates;
return candidates;
}
catch (const libusbp::error& e)
{
error("USB error: {}", e.message());
}
}

std::string getDeviceName(DeviceType type)
Expand Down

0 comments on commit 0d495ed

Please sign in to comment.