lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /******************************************************************************* |
| 2 | * °æÈ¨ËùÓÐ (C)2016, ÖÐÐËͨѶ¹É·ÝÓÐÏÞ¹«Ë¾¡£ |
| 3 | * |
| 4 | * ÎļþÃû³Æ: usb_dev.c |
| 5 | * Îļþ±êʶ: usb_dev.c |
| 6 | * ÄÚÈÝÕªÒª: usbÉ豸·ÃÎʹ¤¾ß |
| 7 | * |
| 8 | * ÐÞ¸ÄÈÕÆÚ °æ±¾ºÅ Ð޸ıê¼Ç ÐÞ¸ÄÈË ÐÞ¸ÄÄÚÈÝ |
| 9 | * ------------------------------------------------------------------------------ |
| 10 | * 2016/3/10 V1.0 Create ´´½¨ |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | /******************************************************************************* |
| 15 | * Í·Îļþ * |
| 16 | *******************************************************************************/ |
| 17 | #include<stdlib.h> |
| 18 | #include<stdio.h> |
| 19 | #include<string.h> |
| 20 | #include<sys/types.h> |
| 21 | #include<sys/stat.h> |
| 22 | #include<fcntl.h> |
| 23 | #include<errno.h> |
| 24 | #include<getopt.h> |
| 25 | #include<stdarg.h> |
| 26 | #include<termios.h> |
| 27 | #include<stddef.h> |
| 28 | #include<dirent.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | /******************************************************************************* |
| 32 | * ºê¶¨Òå * |
| 33 | *******************************************************************************/ |
| 34 | #define USB_DIR_FILE_NAME_SIZE 1024 |
| 35 | #define USB_DIR_BASE "/sys/bus/usb/devices" |
| 36 | #define USB_PID 0x0197 |
| 37 | #define SUCCESS 0 |
| 38 | #define FAIL -1 |
| 39 | |
| 40 | /******************************************************************************* |
| 41 | * ³£Á¿¶¨Òå * |
| 42 | *******************************************************************************/ |
| 43 | char sys_filename[USB_DIR_FILE_NAME_SIZE] = {0}; |
| 44 | int g_usb_dev = -1; |
| 45 | |
| 46 | /******************************************************************************* |
| 47 | * Êý¾ÝÀàÐͶ¨Òå * |
| 48 | *******************************************************************************/ |
| 49 | |
| 50 | /******************************************************************************* |
| 51 | * º¯ÊýÉùÃ÷ * |
| 52 | *******************************************************************************/ |
| 53 | |
| 54 | /******************************************************************************* |
| 55 | * ¾Ö²¿¾²Ì¬±äÁ¿¶¨Òå * |
| 56 | *******************************************************************************/ |
| 57 | |
| 58 | /******************************************************************************* |
| 59 | * È«¾Ö±äÁ¿¶¨Òå * |
| 60 | *******************************************************************************/ |
| 61 | |
| 62 | /******************************************************************************* |
| 63 | * ¾Ö²¿º¯ÊýʵÏÖ * |
| 64 | *******************************************************************************/ |
| 65 | int dev_get_usbsys_val(const char *sys_filename, int base) |
| 66 | { |
| 67 | char buff[64] = {0}; |
| 68 | int ret_val = -1; |
| 69 | int fd = -1; |
| 70 | |
| 71 | fd = open(sys_filename, O_RDONLY); |
| 72 | if (fd < 0) { |
| 73 | printf("failed to open usbsys, error is %s\n", strerror(errno)); |
| 74 | return FAIL; |
| 75 | } |
| 76 | |
| 77 | if (read(fd, buff, sizeof(buff)) <= 0) { |
| 78 | printf("[%s] read:%s failed\n", __func__, sys_filename); |
| 79 | } |
| 80 | else { |
| 81 | ret_val = strtoul(buff, NULL, base); |
| 82 | } |
| 83 | close(fd); |
| 84 | |
| 85 | return ret_val; |
| 86 | } |
| 87 | |
| 88 | int dev_strStartsWith(const char *line, const char *src) |
| 89 | { |
| 90 | int ret = -1; |
| 91 | |
| 92 | for ( ; *line != '\0' && *src != '\0'; line++, src++) { |
| 93 | if (*line != *src) { |
| 94 | return FAIL; |
| 95 | } |
| 96 | } |
| 97 | ret = atoi(line); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | int dev_get_ttyport_by_syspath(char *syspath) |
| 102 | { |
| 103 | DIR *usbdir = NULL; |
| 104 | struct dirent *dent = NULL; |
| 105 | int usb_port = -1; |
| 106 | |
| 107 | usbdir = opendir(syspath); |
| 108 | if (usbdir == NULL) { |
| 109 | printf("%s: open [%s] busdir failed\n", __func__, syspath); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | while ((dent = readdir(usbdir)) != NULL) |
| 114 | { |
| 115 | usb_port = dev_strStartsWith(dent->d_name, "ttyUSB"); |
| 116 | if ( usb_port >= 0) { |
| 117 | closedir(usbdir); |
| 118 | usbdir = NULL; |
| 119 | return usb_port; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (usbdir) { |
| 124 | closedir(usbdir); |
| 125 | usbdir = NULL; |
| 126 | } |
| 127 | return FAIL; |
| 128 | } |
| 129 | |
| 130 | int dev_get_device(void) |
| 131 | { |
| 132 | DIR *usbdir = NULL; |
| 133 | struct dirent *dent = NULL; |
| 134 | int idProduct = 0; |
| 135 | int bConfigurationValue = 0; |
| 136 | int num = 0; |
| 137 | usbdir = opendir(USB_DIR_BASE); |
| 138 | if (usbdir == NULL) |
| 139 | return FAIL; |
| 140 | |
| 141 | while ((dent = readdir(usbdir)) != NULL){ |
| 142 | if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) { |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | snprintf(sys_filename, sizeof(sys_filename), "%s/%s/idProduct", USB_DIR_BASE, dent->d_name); |
| 147 | if ((idProduct = dev_get_usbsys_val(sys_filename, 16)) <= 0) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | snprintf(sys_filename, sizeof(sys_filename), "%s/%s/bConfigurationValue", USB_DIR_BASE, dent->d_name); |
| 152 | if ((bConfigurationValue = dev_get_usbsys_val(sys_filename, 10)) <= 0) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | if (idProduct == USB_PID){ |
| 157 | snprintf(sys_filename, sizeof(sys_filename), "%s/%s/%s:%d.%d", USB_DIR_BASE, dent->d_name, dent->d_name, bConfigurationValue, 0); |
| 158 | g_usb_dev = dev_get_ttyport_by_syspath(sys_filename); |
| 159 | closedir(usbdir); |
| 160 | usbdir = NULL; |
| 161 | return SUCCESS; |
| 162 | } |
| 163 | } |
| 164 | if (usbdir) { |
| 165 | closedir(usbdir); |
| 166 | usbdir = NULL; |
| 167 | } |
| 168 | return FAIL; |
| 169 | } |
| 170 | |