Skip to content

Commit

Permalink
1.0.26 (2024-02-05)
Browse files Browse the repository at this point in the history
-------------------
- API partially updated from 1.0.27 but still supports only 1.0.26.
- Improvements and some little bugfixes.
- Examples and tests are upgraded from 1.0.27.
  • Loading branch information
Adam Karpierz committed Feb 5, 2024
1 parent ee7a22b commit 1d5e73e
Show file tree
Hide file tree
Showing 37 changed files with 3,782 additions and 367 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

1.0.26 (2024-02-05)
-------------------
- API partially updated from 1.0.27 but still supports only 1.0.26.
- Improvements and some little bugfixes.
- Examples and tests are upgraded from 1.0.27.

1.0.26rc4 (2024-01-25)
----------------------
- Setup update (now based on tox >= 4.0).
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,4 @@ Authors
.. _Development page: https://github.com/karpierz/libusb
.. _PyPI record: https://pypi.org/project/libusb/
.. _Documentation: https://libusb.readthedocs.io/
.. _USB Vendors: https://devicehunt.com/all-usb-vendors
27 changes: 17 additions & 10 deletions examples/dpfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@
from libusb._platform import defined, is_posix, is_windows
if is_windows: import win32

usb_strerror = lambda r: usb.strerror(r).decode("utf-8")

if defined("DPFP_THREADED"):

if is_posix:

THREAD_RETURN_VALUE = NULL
semaphore_t = ct.POINTER(sem_t)
thread_t = pthread_t
thread_return_t = ct.c_void_p
THREAD_RETURN_VALUE = NULL

def semaphore_create() -> semaphore_t:
name = "/org.libusb.example.dpfp_threaded:%d" % int(getpid())
name = "/org.libusb.example.dpfp_threaded:{:d}".format(int(getpid()))
semaphore: semaphore_t = sem_open(name, O_CREAT | O_EXCL, 0, 0)
if semaphore == SEM_FAILED:
return NULL;
Expand All @@ -61,7 +63,7 @@ def semaphore_take(semaphore: semaphore_t):
def semaphore_destroy(semaphore: semaphore_t):
sem_close(semaphore)

# void *(*thread_entry)(arg: ct.c_void_p)
# thread_return_t (*thread_entry)(arg: ct.c_void_p)
def thread_create(thread: ct.POINTER(thread_t),
thread_entry,
arg: ct.c_void_p) -> int:
Expand All @@ -73,14 +75,14 @@ def thread_join(thread: thread_t):

elif is_windows:

THREAD_RETURN_VALUE = 0
semaphore_t = win32.HANDLE
thread_t = win32.HANDLE
if defined("__CYGWIN__"):
thread_return_t = win32.DWORD
else:
#thread_return_t = ct.c_uint
thread_return_t = win32.DWORD
THREAD_RETURN_VALUE = 0

def semaphore_create() -> semaphore_t:
return win32.CreateSemaphore(None, 0, 1, None)
Expand Down Expand Up @@ -543,7 +545,6 @@ def alloc_transfers() -> int:

#static
def sighandler(signum, frame):

request_exit(1)


Expand Down Expand Up @@ -571,9 +572,12 @@ def main(argv=sys.argv[1:]):
global irq_transfer
global do_exit

r = usb.init(None)
r = (usb.init_context(None, None, 0)
if hasattr(usb, "init_context") else
usb.init(None))
if r < 0:
print("failed to initialise libusb {} - {}".format(r, usb.strerror(r)), file=sys.stderr)
print("failed to initialise libusb {} - {}".format(r, usb_strerror(r)),
file=sys.stderr)
sys.exit(1)

r = find_dpfp_device()
Expand All @@ -584,7 +588,8 @@ def main(argv=sys.argv[1:]):

r = usb.claim_interface(devh, 0)
if r < 0:
print("claim interface error {} - {}".format(r, usb.strerror(r)), file=sys.stderr)
print("claim interface error {} - {}".format(r, usb_strerror(r)),
file=sys.stderr)
return abs(r)
print("claimed interface")

Expand Down Expand Up @@ -642,12 +647,14 @@ def main(argv=sys.argv[1:]):
if img_transfer:
r = usb.cancel_transfer(img_transfer)
if r < 0:
print("failed to cancel transfer {} - {}".format(r, usb.strerror(r)), file=sys.stderr)
print("failed to cancel transfer {} - {}".format(r, usb_strerror(r)),
file=sys.stderr)

if irq_transfer:
r = usb.cancel_transfer(irq_transfer)
if r < 0:
print("failed to cancel transfer {} - {}".format(r, usb.strerror(r)), file=sys.stderr)
print("failed to cancel transfer {} - {}".format(r, usb_strerror(r)),
file=sys.stderr)

while img_transfer or irq_transfer:
if usb.handle_events(None) < 0:
Expand Down
4 changes: 2 additions & 2 deletions examples/examples.c/dpfp.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static inline semaphore_t semaphore_create(void)
sem_t *semaphore;
char name[50];

sprintf(name, "/org.libusb.example.dpfp_threaded:%d", (int)getpid());
snprintf(name, sizeof(name), "/org.libusb.example.dpfp_threaded:%d", (int)getpid());
semaphore = sem_open(name, O_CREAT | O_EXCL, 0, 0);
if (semaphore == SEM_FAILED)
return NULL;
Expand Down Expand Up @@ -601,7 +601,7 @@ int main(void)
{
int r;

r = libusb_init(NULL);
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0) {
fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r));
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions examples/examples.c/fxload.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ int main(int argc, char*argv[])
}

/* open the device using libusb */
status = libusb_init(NULL);
status = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (status < 0) {
logerror("libusb_init() failed: %s\n", libusb_error_name(status));
logerror("libusb_init_context() failed: %s\n", libusb_error_name(status));
return -1;
}
libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, verbose);
Expand Down
41 changes: 28 additions & 13 deletions examples/examples.c/hotplugtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev,
(void)user_data;

rc = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error getting device descriptor\n");
if (LIBUSB_SUCCESS == rc) {
printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
} else {
printf ("Device attached\n");
fprintf (stderr, "Error getting device descriptor: %s\n",
libusb_strerror((enum libusb_error)rc));
}

printf ("Device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);

if (handle) {
libusb_close (handle);
handle = NULL;
}

rc = libusb_open (dev, &handle);
if (LIBUSB_SUCCESS != rc) {
fprintf (stderr, "Error opening device\n");
fprintf (stderr, "No access to device: %s\n",
libusb_strerror((enum libusb_error)rc));
}

done++;
Expand All @@ -60,12 +63,22 @@ static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev,

static int LIBUSB_CALL hotplug_callback_detach(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
int rc;

(void)ctx;
(void)dev;
(void)event;
(void)user_data;

printf ("Device detached\n");
rc = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS == rc) {
printf ("Device detached: %04x:%04x\n", desc.idVendor, desc.idProduct);
} else {
printf ("Device detached\n");
fprintf (stderr, "Error getting device descriptor: %s\n",
libusb_strerror((enum libusb_error)rc));
}

if (handle) {
libusb_close (handle);
Expand All @@ -83,14 +96,15 @@ int main(int argc, char *argv[])
int product_id, vendor_id, class_id;
int rc;

vendor_id = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : 0x045a;
product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : 0x5005;
vendor_id = (argc > 1) ? (int)strtol (argv[1], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
class_id = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;

rc = libusb_init (NULL);
if (rc < 0)
rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (LIBUSB_SUCCESS != rc)
{
printf("failed to initialise libusb: %s\n", libusb_error_name(rc));
printf ("failed to initialise libusb: %s\n",
libusb_strerror((enum libusb_error)rc));
return EXIT_FAILURE;
}

Expand Down Expand Up @@ -118,8 +132,9 @@ int main(int argc, char *argv[])

while (done < 2) {
rc = libusb_handle_events (NULL);
if (rc < 0)
printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
if (LIBUSB_SUCCESS != rc)
printf ("libusb_handle_events() failed: %s\n",
libusb_strerror((enum libusb_error)rc));
}

if (handle) {
Expand Down
2 changes: 1 addition & 1 deletion examples/examples.c/listdevs.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(void)
int r;
ssize_t cnt;

r = libusb_init(NULL);
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0)
return r;

Expand Down
2 changes: 1 addition & 1 deletion examples/examples.c/sam3u_benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ int main(void)
(void)signal(SIGINT, sig_hdlr);
#endif

rc = libusb_init(NULL);
rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (rc < 0) {
fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc));
exit(1);
Expand Down
2 changes: 1 addition & 1 deletion examples/examples.c/testlibusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ int main(int argc, char *argv[])
}
}

r = libusb_init(NULL);
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0)
return r;

Expand Down

0 comments on commit 1d5e73e

Please sign in to comment.