--- /dev/null
+/*
+ * 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;
+}