Update tests and example to use the new libusb_init_context() function

This commit updates all test and example code to use the newer
libusb_init_context() function instead of libusb_init().

Signed-off-by: Nathan Hjelm <hjelmn@google.com>
[Tormod: Update umockdev.c as well]
Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
This commit is contained in:
Nathan Hjelm 2021-12-31 10:21:38 -07:00 committed by Tormod Volden
parent 6622f386f5
commit 3c33e499a0
11 changed files with 27 additions and 24 deletions

View file

@ -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);

View file

@ -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);

View file

@ -87,7 +87,7 @@ int main(int argc, char *argv[])
product_id = (argc > 2) ? (int)strtol (argv[2], NULL, 0) : 0x5005;
class_id = (argc > 3) ? (int)strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;
rc = libusb_init (NULL);
rc = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (rc < 0)
{
printf("failed to initialise libusb: %s\n", libusb_error_name(rc));

View file

@ -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;

View file

@ -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);

View file

@ -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;

View file

@ -1153,7 +1153,7 @@ int main(int argc, char** argv)
version = libusb_get_version();
printf("Using libusb v%d.%d.%d.%d\n\n", version->major, version->minor, version->micro, version->nano);
r = libusb_init(NULL);
r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
if (r < 0)
return r;

View file

@ -1 +1 @@
#define LIBUSB_NANO 11768
#define LIBUSB_NANO 11769

View file

@ -32,7 +32,7 @@ static libusb_testlib_result test_init_and_exit(void)
libusb_context *ctx = NULL;
int r;
r = libusb_init(&ctx);
r = libusb_init_context(&ctx, /*options=*/NULL, /*num_options=*/0);
if (r != LIBUSB_SUCCESS) {
libusb_testlib_logf(
"Failed to init libusb on iteration %d: %d",
@ -51,7 +51,7 @@ static libusb_testlib_result test_get_device_list(void)
libusb_context *ctx;
int r;
r = libusb_init(&ctx);
r = libusb_init_context(&ctx, /*options=*/NULL, /*num_options=*/0);
if (r != LIBUSB_SUCCESS) {
libusb_testlib_logf("Failed to init libusb: %d", r);
return TEST_STATUS_FAILURE;
@ -83,7 +83,7 @@ static libusb_testlib_result test_many_device_lists(void)
libusb_device **device_lists[LIST_COUNT];
int r;
r = libusb_init(&ctx);
r = libusb_init_context(&ctx, /*options=*/NULL, /*num_options=*/0);
if (r != LIBUSB_SUCCESS) {
libusb_testlib_logf("Failed to init libusb: %d", r);
return TEST_STATUS_FAILURE;
@ -123,22 +123,25 @@ static libusb_testlib_result test_default_context_change(void)
libusb_context *ctx = NULL;
int r;
/* Enable debug output on new context, to be sure to use the context */
struct libusb_init_option options[] = {
{
.option = LIBUSB_OPTION_LOG_LEVEL,
.value = {.ival = LIBUSB_LOG_LEVEL_DEBUG},
},
};
int num_options = 1;
/* First create a new context */
r = libusb_init(&ctx);
r = libusb_init_context(&ctx, options, num_options);
if (r != LIBUSB_SUCCESS) {
libusb_testlib_logf("Failed to init libusb: %d", r);
return TEST_STATUS_FAILURE;
}
/* Enable debug output on new context, to be sure to use the context */
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
/* Enable debug output on the default context. This should work even before
* the context has been created. */
libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
/* Now create a reference to the default context */
r = libusb_init(NULL);
r = libusb_init_context(/*ctx=*/NULL, options, num_options);
if (r != LIBUSB_SUCCESS) {
libusb_testlib_logf("Failed to init libusb: %d", r);
libusb_exit(ctx);

View file

@ -16,7 +16,7 @@ static void *test_init_and_exit(void * arg)
libusb_context *ctx = NULL;
int r;
r = libusb_init(&ctx);
r = libusb_init_context(&ctx, /*options=*/NULL, /*num_options=*/0);
if (r != LIBUSB_SUCCESS) {
printf("Failed to init libusb on iteration %d: %d", i, r);
return NULL;

View file

@ -430,7 +430,7 @@ test_fixture_setup_libusb(UMockdevTestbedFixture * fixture, int devcount)
{
libusb_device **devs = NULL;
libusb_init (&fixture->ctx);
libusb_init_context(/*ctx=*/&fixture->ctx, /*options=*/NULL, /*num_options=*/0);
/* Supress global log messages completely
* (though, in some tests it might be interesting to check there are no real ones).
@ -573,7 +573,7 @@ test_implicit_default(UMockdevTestbedFixture * fixture, UNUSED_DATA)
libusb_free_device_list(devs, TRUE);
clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_INFO);
libusb_init(NULL);
libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
g_assert_cmpint(libusb_get_device_list(NULL, &devs), ==, 1);
libusb_exit(NULL);