add sync transfer test program
authorStanislaw Wadas <s.wadas@samsung.com>
Wed, 24 Jun 2015 10:22:43 +0000 (12:22 +0200)
committerStanislaw Wadas <s.wadas@samsung.com>
Wed, 2 Dec 2015 12:45:16 +0000 (13:45 +0100)
program allow to test sync transfer.
this program need to be run on host, on device side
there is need to create gadget with FunctionFS function
and start test daemon. source can be found :
https://github.com/torvalds/linux/blob/master/tools/usb/ffs-aio-example/simple/device_app/aio_simple.c

Change-Id: If9805cf8a0bcf58288dc2d58aca7bdb3a0ae4e65
Signed-off-by: Stanislaw Wadas <s.wadas@samsung.com>
example/CMakeLists.txt
example/simple_sync.c [new file with mode: 0644]

index 06f6e00c766c3ae3c5488584022d5450dfc06ca1..20a0186eaf1ce6d70566cbba66f756bcbe7a6d08 100644 (file)
@@ -4,6 +4,10 @@ SET(LSDEV_EXAMPLE_SRC
       lsdev.c
       )
 
+SET(SIMPLE_SYNC_EXAMPLE_SRC
+      simple_sync.c
+      )
+
 INCLUDE(FindPkgConfig)
 pkg_check_modules(LIBUSB REQUIRED
      libusb-1.0
@@ -14,4 +18,6 @@ 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)
 
+ADD_EXECUTABLE(simple_sync ${SIMPLE_SYNC_EXAMPLE_SRC})
+TARGET_LINK_LIBRARIES(simple_sync husb usb-1.0)
 
diff --git a/example/simple_sync.c b/example/simple_sync.c
new file mode 100644 (file)
index 0000000..be6f00b
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * simple_sync.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 <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <../include/libhusb.h>
+
+#define BUF_LEN        8192
+
+libhusb_device_handle *dev_handle;
+
+static void print_devs(libhusb_device **devs, libhusb_context *ctx)
+{
+       libhusb_device *dev;
+       int i = 0, j = 0;
+       uint8_t path[8];
+       int r; 
+       struct libhusb_device_descriptor desc;
+
+       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 id %d, device id %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(" path: %d", path[0]);
+                       for (j = 1; j < r; j++)
+                               printf(".%d", path[j]);
+               }
+               printf("\n");
+       }
+}
+
+static int connect_to_dev_interface(int vid, int pid, struct libhusb_context *ctx)
+{
+       int r;
+
+       dev_handle = libhusb_device_open_with_vid_pid(ctx, vid, pid);
+       if (dev_handle == NULL) {
+               printf("cant get dev with vid pid\n");
+               return -1;
+       }
+
+       printf("claiming iface\n");
+       if (libhusb_claim_interface(dev_handle, 0, 0)){
+               r = libhusb_detach_kernel_driver(dev_handle, 0);
+               if (r) {
+                       printf("unable to detach kernel driver: %s\n",
+                              libhusb_error_name(r));
+                       goto out2;
+               }
+               r = libhusb_claim_interface(dev_handle, 0, 0);
+               if (r) {
+                       printf("can't claim interface: %s\n",
+                              libhusb_error_name(r));
+                       goto out3;
+               }
+       }
+
+       return 0;
+
+out3:
+       libhusb_attach_kernel_driver(dev_handle, 0);
+
+out2:
+       libhusb_close(dev_handle);
+
+       libhusb_exit(ctx);
+       return 1;
+}
+
+int main(void)
+{
+       struct libhusb_context *ctx;
+       struct libhusb_device **devs;
+       int r;
+       ssize_t cnt;
+       int vid,pid;
+       libhusb_device *dev_vp;
+       unsigned char in_addr, out_addr;
+       struct libhusb_cfg_descriptor *config;
+       struct libhusb_interface_descriptor const *iface;
+       int n;
+
+       r = libhusb_init(&ctx);
+       if (r < 0)
+               return r;
+
+       cnt = libhusb_get_devices(ctx, &devs);
+       if (cnt < 0)
+               return (int) cnt;
+
+       printf("Getting dev list: \n");
+
+       print_devs(devs, ctx);
+
+       printf("provide vid and pid of device which you want to operate on\n");
+       printf("\n enter device vid and hit enter:");
+       scanf ("%x",&vid);
+       printf("\n enter device vid and hit enter:");
+       scanf ("%x",&pid);
+
+       r = connect_to_dev_interface(vid, pid, ctx);
+       if (r < 0) {
+               printf("failure %s\n",libhusb_error_name(r));
+               return r;
+       }
+       dev_vp = libhusb_get_device(dev_handle);
+       r = libhusb_get_config_descriptor(dev_vp, 0, &config);
+       if (r < 0) {
+               printf("failure\n");
+               return r;
+       }
+
+       iface = &config->interface[0].altsetting[0];
+       in_addr = iface->endpoint[0].bEndpointAddress;
+       out_addr = iface->endpoint[1].bEndpointAddress;
+
+       for(n = 0; n < 100; n++) {
+               static unsigned char buffer[BUF_LEN];
+               int bytes;
+               libhusb_bulk_transfer(dev_handle, in_addr, buffer, BUF_LEN,
+                                    &bytes, 500);
+               libhusb_bulk_transfer(dev_handle, out_addr, buffer, BUF_LEN,
+                                    &bytes, 500);
+       }
+       printf("EXITING\n\n");
+
+       libhusb_release_interface(dev_handle, 0);
+       libhusb_attach_kernel_driver(dev_handle, 0);
+
+       libhusb_free_config_descriptor(config);
+       libhusb_free_devices(devs, 1);
+
+       libhusb_close(dev_handle);
+       libhusb_exit(ctx);
+
+       return 0;
+}