blob: e48075e1ee586cdd9f16101763e6c17c01248564 [file] [log] [blame]
xf.libdd93d52023-05-12 07:10:14 -07001/*******************************************************************************
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#include "devUsb.h"
32
33/*******************************************************************************
34* 宏定义 *
35*******************************************************************************/
36#define USB_DIR_FILE_NAME_SIZE (512)
37#define USB_DIR_BASE ("/sys/bus/usb/devices")
38#define SUCCESS (0)
39#define FAIL (-1)
40
41/*******************************************************************************
42* 变量定义 *
43*******************************************************************************/
44
45/*******************************************************************************
46* 数据类型定义 *
47*******************************************************************************/
48
49/*******************************************************************************
50* 函数声明 *
51*******************************************************************************/
52static int dev_get_usbsys_val(const char *sys_filename, int base);
53static int dev_strStartsWith(const char *line, const char *src);
54static int dev_get_ttyport_by_syspath(char *syspath);
55
56/*******************************************************************************
57* 局部静态变量声明 *
58*******************************************************************************/
59static char sys_filename[USB_DIR_FILE_NAME_SIZE] = {0};
60
61/*******************************************************************************
62* 全局变量定义 *
63*******************************************************************************/
64int g_usb_dev = -1;
65
66/*******************************************************************************
67* 局部函数实现 *
68*******************************************************************************/
69static int dev_get_usbsys_val(const char *sys_filename, int base)
70{
71 char buff[64] = {0};
72 int ret_val = -1;
73 int fd = -1;
74
75 fd = open(sys_filename, O_RDONLY);
76 if (fd < 0) {
77 // printf("failed to open usbsys, error is %s\n", strerror(errno));
78 return FAIL;
79 }
80
81 if (read(fd, buff, sizeof(buff)) <= 0) {
82 printf("[%s] read:%s failed\n", __func__, sys_filename);
83 }
84 else {
85 ret_val = strtoul(buff, NULL, base);
86 }
87 close(fd);
88
89 return ret_val;
90}
91
92static int dev_strStartsWith(const char *line, const char *src)
93{
94 int ret = -1;
95
96 for ( ; *line != '\0' && *src != '\0'; line++, src++) {
97 if (*line != *src) {
98 return FAIL;
99 }
100 }
101 ret = atoi(line);
102 return ret;
103}
104
105static int dev_get_ttyport_by_syspath(char *syspath)
106{
107 DIR *usbdir = NULL;
108 struct dirent *dent = NULL;
109 int usb_port = -1;
110
111 usbdir = opendir(syspath);
112 if (usbdir == NULL) {
113 printf("%s: open [%s] busdir failed\n", __func__, syspath);
114 return FAIL;
115 }
116
117 while ((dent = readdir(usbdir)) != NULL)
118 {
119 usb_port = dev_strStartsWith(dent->d_name, "ttyUSB");
120 if ( usb_port >= 0) {
121 closedir(usbdir);
122 usbdir = NULL;
123 return usb_port;
124 }
125 }
126
127 if (usbdir) {
128 closedir(usbdir);
129 usbdir = NULL;
130 }
131 return FAIL;
132}
133
134int dev_get_device(int *pPortInfo)
135{
136 DIR *usbdir = NULL;
137 struct dirent *dent = NULL;
138 int idVendor = 0, idProduct = 0;
139 int bConfigurationValue = 0;
140 int num = 0;
141 usbdir = opendir(USB_DIR_BASE);
142 if (usbdir == NULL)
143 return -1;
144
145 while ((dent = readdir(usbdir)) != NULL){
146 if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
147 continue;
148 }
149
150 snprintf(sys_filename, sizeof(sys_filename), "%s/%s/idVendor", USB_DIR_BASE, dent->d_name);
151 if ((idVendor = dev_get_usbsys_val(sys_filename, 16)) <= 0) {
152 continue;
153 }
154
155 snprintf(sys_filename, sizeof(sys_filename), "%s/%s/idProduct", USB_DIR_BASE, dent->d_name);
156 if ((idProduct = dev_get_usbsys_val(sys_filename, 16)) <= 0) {
157 continue;
158 }
159
160 snprintf(sys_filename, sizeof(sys_filename), "%s/%s/bConfigurationValue", USB_DIR_BASE, dent->d_name);
161 if ((bConfigurationValue = dev_get_usbsys_val(sys_filename, 10)) <= 0) {
162 continue;
163 }
164
165 if (idVendor == pPortInfo[0] && idProduct == pPortInfo[1]){
166 snprintf(sys_filename, sizeof(sys_filename), "%s/%s/%s:%d.%d", USB_DIR_BASE, dent->d_name, dent->d_name, bConfigurationValue, pPortInfo[2]);
167 g_usb_dev = dev_get_ttyport_by_syspath(sys_filename);
168 printf("port found: /dev/ttyUSB%d -- %s\n", g_usb_dev, sys_filename);
169 closedir(usbdir);
170 usbdir = NULL;
171 return 0;
172 }
173 usleep(10000);
174 }
175 if (usbdir) {
176 closedir(usbdir);
177 usbdir = NULL;
178 }
179 printf("Can not find port.\n");
180
181 return -1;
182}
183