add example: lsdev
authorStanislaw Wadas <s.wadas@samsung.com>
Fri, 22 May 2015 08:33:00 +0000 (10:33 +0200)
committerStanislaw Wadas <s.wadas@samsung.com>
Wed, 2 Dec 2015 12:45:16 +0000 (13:45 +0100)
this program list all usb devices, and perform
basic operations on them using libhusb CAPI

Signed-off-by: Stanislaw Wadas <s.wadas@samsung.com>
Rebase example onto current HEAD

Change-Id: I6bf6eb14cdfe1062aa6fb739fdd03ddb6e849862
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
CMakeLists.txt
example/CMakeLists.txt [new file with mode: 0644]
example/lsdev.c [new file with mode: 0644]

index 1f97b601f2997c499f7f9eb2c86503dd588b1ed8..281678ab1985ec0a0ab13f76f925b81a21dc622f 100644 (file)
@@ -4,6 +4,7 @@ PROJECT(libhusb)
 # Generation options:
 # -DBUILD_DOC_ONLY - only doxygen documentation is build
 # -DBUILD_DOC - build also doxygen documentation
+# -DBUILD_EXAMPLES - build also sample applications
 ########################################################
 
 ########################################################
@@ -17,6 +18,7 @@ IF(BUILD_DOC_ONLY)
        SET(BUILD_DOC TRUE)
 ELSE(BUILD_DOC_ONLY)
        SET(BUILD_SHARED_LIBS TRUE)
+       SET(BUILD_EXAMPLES TRUE)
 ENDIF(BUILD_DOC_ONLY)
 
 IF(BUILD_SHARED_LIBS)
@@ -66,6 +68,9 @@ IF(BUILD_SHARED_LIBS)
                COMMAND ${CMAKE_COMMAND}
                -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
 
+       IF(BUILD_EXAMPLES)
+               ADD_SUBDIRECTORY(example)
+       ENDIF(BUILD_EXAMPLES)
 ENDIF(BUILD_SHARED_LIBS)
 
 IF(BUILD_DOC)
diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt
new file mode 100644 (file)
index 0000000..06f6e00
--- /dev/null
@@ -0,0 +1,17 @@
+MESSAGE("Building sample applications")
+
+SET(LSDEV_EXAMPLE_SRC
+      lsdev.c
+      )
+
+INCLUDE(FindPkgConfig)
+pkg_check_modules(LIBUSB REQUIRED
+     libusb-1.0
+     )
+
+SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -g -O0 -Wall -lusb-1.0 ")
+
+ADD_EXECUTABLE(lsdev ${LSDEV_EXAMPLE_SRC})
+TARGET_LINK_LIBRARIES(lsdev husb usb-1.0)
+
+
diff --git a/example/lsdev.c b/example/lsdev.c
new file mode 100644 (file)
index 0000000..97369fc
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * lsdev.c
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include <../include/libhusb.h>
+
+static void print_devs(libhusb_device **devs, libhusb_context *ctx)
+{
+       libhusb_device *dev;
+       int i = 0, j = 0;
+       uint8_t path[8];
+       int r;
+
+       int data1;
+       libhusb_device_handle *dev_handle;
+       libhusb_device *dev_vp;
+       char str1[256];
+       struct libhusb_cfg_descriptor *config;
+       struct libhusb_device_descriptor desc;
+       int conf;
+
+       printf("Getting device list: \n");
+
+       while ((dev = devs[i++]) != NULL) {
+               r = libhusb_get_device_descriptor(dev, &desc);
+               if (r < 0) {
+                       fprintf(stderr, "failed to get device descriptor");
+                       return;
+               }
+
+               printf("%04x:%04x (bus %d, device %d)",
+                       desc.idVendor, desc.idProduct,
+                       libhusb_get_bus_number(dev), libhusb_get_address(dev));
+
+               r = libhusb_get_port_numbers(dev, path, sizeof(path));
+               if (r < 0)
+                       printf("cant get port numbers\n");
+               else if (r > 0) {
+                       printf(" path: %d", path[0]);
+                       for (j = 1; j < r; j++)
+                               printf(".%d", path[j]);
+               }
+               printf("\n");
+       }
+
+       int vid, pid;
+       printf("provide vid and pid of device which you want to operate on\n");
+       printf("\n enter device vid and hit enter(with 0x):");
+       scanf("%x", &vid);
+       printf("\n enter device pid and hit enter(with 0x):");
+       scanf("%x", &pid);
+
+       printf("\nOpening device vid:0x%x  pid:0x%x\n", vid, pid);
+
+       dev_handle = libhusb_device_open_with_vid_pid(ctx, vid, pid);
+       if (dev_handle == NULL) {
+               printf("cant get dev with vid pid\n");
+               return;
+       }
+       if (dev_handle != NULL) printf("opened\n");
+
+       dev_vp = libhusb_get_device(dev_handle);
+       if (dev_vp == NULL) {
+               printf("ERROR\n");
+               return;
+       }
+
+       data1 = libhusb_get_max_packet_size(dev_vp, 1);
+       if (data1 < 0) {
+               printf("ERROR: %s", libhusb_error_name(data1));
+               return;
+       }
+
+       printf("\nmax packet size: %d\n", data1);
+
+       r = libhusb_get_active_config(dev_handle, &conf);
+       if (r < 0) {
+               printf("ERROR: %s", libhusb_error_name(r));
+               return;
+       }
+
+       printf("\nbconfig: %d\n", conf);
+
+       printf("\ntrying to set 2nd config\n");
+       r = libhusb_set_config(dev_handle, 1);
+       if (r < 0) printf("failure\n");
+
+       printf("claiming iface\n");
+       r = libhusb_claim_interface(dev_handle, 0, 0);
+       if (r < 0) printf("failure\n");
+
+       printf("releasing iface\n");
+       r = libhusb_release_interface(dev_handle, 0);
+       if (r < 0) printf("failure\n");
+
+/*
+       commented because sometime after reset device require hardware restart
+       printf("reseting device \n");
+       r = libhusb_reset_device(dev_handle);
+       if (r < 0) printf("failure\n");
+*/
+
+       printf("is kernel driver active on iface 1? \n");
+       r = libhusb_kernel_driver_active(dev_handle, 0);
+       if (r == 0)
+               printf("no active driver\n");
+       else if (r == 1)
+               printf("kernel driver is active\n");
+       else if (r != 0 || r != 1)
+               printf("ERROR\n");
+
+       printf("detaching kernel driver\n");
+       r = libhusb_detach_kernel_driver(dev_handle, 0);
+       if (r < 0) printf("failure\n");
+
+       printf("attaching kernel driver\n");
+       r = libhusb_attach_kernel_driver(dev_handle, 0);
+       if (r < 0) printf("failure\n");
+
+       r = libhusb_get_string_descriptor_ascii(dev_handle, desc.iProduct,
+                                               (unsigned char *) str1, sizeof(str1));
+       if (r < 0) printf("failure\n");
+       else printf("Product name: %s\n", str1);
+
+       r = libhusb_get_config_descriptor(dev_vp, 0, &config);
+       if (r < 0) printf("failure\n");
+
+       libhusb_free_config_descriptor(config);
+       libhusb_close(dev_handle);
+}
+
+int main(void)
+{
+       struct libhusb_context *ctx;
+       struct libhusb_device **devs;
+       int r;
+       ssize_t cnt;
+
+       r = libhusb_init(&ctx);
+       if (r < 0) {
+               printf("ERROR: %s", libhusb_error_name(r));
+               return r;
+       }
+
+       cnt = libhusb_get_devices(ctx, &devs);
+       if (cnt < 0)
+               return (int) cnt;
+
+       print_devs(devs, ctx);
+       libhusb_free_devices(devs, 1);
+
+       libhusb_exit(ctx);
+
+       return 0;
+}