3 * Copyright (C) 2005-2007 Takahiro Hirofuchi
8 int read_integer(char *path)
14 bzero(buff, sizeof(buff));
16 fd = open(path, O_RDONLY);
20 ret = read(fd, buff, sizeof(buff));
26 sscanf(buff, "%d", &ret);
33 int read_string(char *path, char *string, size_t len)
41 fd = open(path, O_RDONLY);
47 ret = read(fd, string, len-1);
54 p = strchr(string, '\n');
62 int write_integer(char *path, int value)
68 snprintf(buff, sizeof(buff), "%d", value);
70 fd = open(path, O_WRONLY);
74 ret = write(fd, buff, strlen(buff));
85 int read_bConfigurationValue(char *busid)
89 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
91 return read_integer(path);
94 int write_bConfigurationValue(char *busid, int config)
98 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bConfigurationValue", busid);
100 return write_integer(path, config);
103 int read_bNumInterfaces(char *busid)
107 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bNumInterfaces", busid);
109 return read_integer(path);
112 int read_bDeviceClass(char *busid)
116 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/bDeviceClass", busid);
118 return read_integer(path);
121 int getdriver(char *busid, int conf, int infnum, char *driver, size_t len)
124 char linkto[PATH_MAX];
127 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s:%d.%d/driver", busid, conf, infnum);
129 /* readlink does not add NULL */
130 bzero(linkto, sizeof(linkto));
131 ret = readlink(path, linkto, sizeof(linkto)-1);
133 strncpy(driver, "none", len);
136 strncpy(driver, basename(linkto), len);
141 int getdevicename(char *busid, char *name, size_t len)
144 char idProduct[10], idVendor[10];
146 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idVendor", busid);
147 read_string(path, idVendor, sizeof(idVendor));
149 snprintf(path, PATH_MAX, "/sys/bus/usb/devices/%s/idProduct", busid);
150 read_string(path, idProduct, sizeof(idProduct));
152 if (!idVendor[0] || !idProduct[0])
155 snprintf(name, len, "%s:%s", idVendor, idProduct);
162 /* if this cannot read a whole line, return -1 */
163 int readline(int sockfd, char *buff, int bufflen)
170 while (index < bufflen) {
171 ret = read(sockfd, &c, sizeof(c));
172 if (ret < 0 && errno == EINTR)
180 if ( index > 0 && buff[index-1] == '\r' && buff[index] == '\n') {
182 buff[index-1] = '\0'; /* get rid of delimitor */
192 int writeline(int sockfd, char *str, int strlen)
199 if (strlen + 3 > MAXLINE)
202 strncpy(buff, str, strlen);
203 buff[strlen+1] = '\r';
204 buff[strlen+2] = '\n';
205 buff[strlen+3] = '\0';
210 ret = write(sockfd, buff+index, len);
223 int writeline(int sockfd, char *str, int strlen)
230 len = strnlen(str, strlen);
232 if (strlen + 2 > MAXLINE)
235 memcpy(buff, str, strlen);
237 buff[strlen+1] = '\n'; /* strlen+1 <= MAXLINE-1 */
242 ret = write(sockfd, buff+index, len);
243 if (ret < 0 && errno == EINTR)