Quellcode durchsuchen

init version, reset usb function done

yuguorong vor 2 Jahren
Commit
faef7ce77e
5 geänderte Dateien mit 186 neuen und 0 gelöschten Zeilen
  1. 4 0
      .gitignore
  2. 56 0
      Makefile
  3. 4 0
      inc/test.h
  4. BIN
      lib/libusb-1.0.so
  5. 122 0
      src/resetusb.c

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+.vscode/
+build/
+*.o
+*.d

+ 56 - 0
Makefile

@@ -0,0 +1,56 @@
+.PHONY: all build_target clean test prebuild
+all: build_target
+
+TARGET:=reset_usb_tools
+OUTDIR:=build
+SRCDIR:=src
+LIBDIR:=lib
+INC:=-Iinc
+CDEF:=
+LIBS:=-lusb-1.0
+CFLAG := -O1 -g -Wall
+LDFLAG:=
+
+CC:=gcc
+PROJDIR := $(shell pwd -P)
+INC += -I$(SRCDIR)
+LDFLAG += -Wl,--rpath=$(LIBDIR)
+#SRC:=resetusb.c
+SRC:=$(wildcard $(SRCDIR)/*.c)
+OBJDIR=$(strip $(OUTDIR))
+TARGET:=$(OBJDIR)/$(TARGET)
+obj=$(SRC:.c=.o)
+#OBJS:=$(addprefix $(OBJDIR)/, $(obj))
+OBJS:=$(subst $(SRCDIR)/, $(OBJDIR)/, $(obj))
+PRES:=$(OBJS:.o=.p)
+
+DEPS:=$(OBJS:.o=.d)
+DEPFLAG:=-MMD -MF 
+
+$(OBJDIR):
+	mkdir -p $@
+
+include $(wildcard $(DEPS))
+
+build_target:$(TARGET)
+
+prebuild:$(OBJDIR) $(PRES)
+
+ $(TARGET):$(OBJDIR) $(OBJS)
+	@echo build target...
+	@$(CC) -o $@ $(OBJS) $(LIBS) $(LDFLAG) 
+
+$(OBJDIR)/%.o: $(SRCDIR)/%.c
+	@echo compile  $< ...
+	@$(CC) -c $(INC) $(CDEF) $(CFLAG) $< -o $@ $(DEPFLAG) $(OBJDIR)/$*.d -MP
+
+$(OBJDIR)/%.p: $(SRCDIR)/%.c
+	@$(CC) -c  -E $(INC) $(CDEF) $(CFLAG) $< -o $@ 
+
+clean:
+	rm -rf $(OBJDIR)
+
+test:
+	
+	@echo $(SRC), $(obj), $(SRCDIR), $(OBJDIR), $(OBJS)  
+

+ 4 - 0
inc/test.h

@@ -0,0 +1,4 @@
+#ifndef _TEST_H_
+#define _TEST_H_
+
+#endif

BIN
lib/libusb-1.0.so


+ 122 - 0
src/resetusb.c

@@ -0,0 +1,122 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libusb-1.0/libusb.h>
+#include "test.h"
+ 
+struct libusb_device_handle *devh;
+struct libusb_device *dev;
+struct libusb_device **devs;
+struct libusb_context *ctx;
+void resetUSB() {
+    int success;
+    int bpoint = 0;
+    do {
+        success = libusb_reset_device(devh);
+        ++bpoint;
+        if (bpoint > 3) {
+            success = 1;
+        }
+    } while (success < 0);
+    if (success) {
+        printf("  reset usb device failed:%d\n", success);
+    } else {
+        printf("  reset usb device ok\n");
+    }
+}
+struct libusb_device* search_device(int _busNum, int _devNum) { 
+    libusb_device *l_dev;
+    int i = 0;
+    int l_busNum, l_devNum;
+   
+    while ((l_dev = devs[i++]) != NULL) {
+        //printf("check against %d device\n", i);
+        l_busNum =(int) libusb_get_bus_number(l_dev);
+        l_devNum =(int) libusb_get_device_address(l_dev);
+        //printf("bus number: %d; device number: %d\n", l_busNum, l_devNum);
+        if ((l_busNum == _busNum) && (l_devNum == _devNum)) {
+            printf("  found device\n");
+            return l_dev;
+        }
+    }
+    return NULL;
+}
+
+typedef struct dev_by_bus{
+    int busnum;
+    int devnum;
+    int index;
+}DEV_BY_BUS;
+
+DEV_BY_BUS  busdev[128];
+
+int main(int argc, char **argv) {
+    //parse the input parameters to get the bus number and device number
+    int l_busNum, l_devNum;
+    int matchVID = 0x5713;
+    int matchPID = 0x0720;
+    int l_ret;
+ 
+    if (argc < 2) {
+        printf("Used defalut paramter(VID:PID = 5713:0720!\n");
+        printf("usage: ./usbreset <vid>:<pid>\n");
+    }else{
+        sscanf(argv[1], "%X:%X",&matchVID, &matchPID);
+        printf("Spec VID:PID = %X:%X\n", matchVID,matchPID);
+    }
+    l_ret = libusb_init(&ctx);
+    if (l_ret ) {
+        fprintf(stderr, "unable to initialize libusb: %i\n", l_ret);
+        return EXIT_FAILURE;
+    }
+    l_ret = libusb_get_device_list(NULL, &devs);
+    if (l_ret < 0) {
+        return (int) l_ret;
+    }
+
+    int cnt = 0;
+    libusb_device *dev;
+    for (int i=0; (dev = devs[i]) != NULL; i++) {
+		struct libusb_device_descriptor desc;
+		int r = libusb_get_device_descriptor(dev, &desc);
+		if (r < 0) {
+			fprintf(stderr, "   failed to get device descriptor");
+			continue;
+		}
+ 
+		printf("%04x:%04x (bus %d, device %d)\n",
+			desc.idVendor, desc.idProduct,
+			libusb_get_bus_number(dev), libusb_get_device_address(dev));
+
+        if( desc.idVendor != matchVID || desc.idProduct != matchPID ) 
+            continue;
+        busdev[cnt].busnum = libusb_get_bus_number(dev);
+        busdev[cnt].devnum = libusb_get_device_address(dev);
+        cnt++;
+    }
+
+    for(int i=0; i<cnt; i++){
+        printf("==>Try reset device [%d]:  \n", i+1);
+        l_busNum = busdev[i].busnum;
+        l_devNum = busdev[i].devnum;
+        printf("  bus number: %d; dev number: %d\n", l_busNum, l_devNum);
+    
+        dev = search_device(l_busNum, l_devNum);
+        if (dev == NULL) {
+            printf("device not found\n");
+            continue;
+        }
+        l_ret = libusb_open(dev, &devh);    
+        if (l_ret ) {
+            printf("error getting usb handle.\n");
+            continue;
+        }
+        //reset the usb device
+        resetUSB();
+        libusb_close(devh);
+    }
+    //free the device list
+    libusb_free_device_list(devs, 1);
+    libusb_exit(NULL);
+    return 0;
+    
+}