| 162 | |
| 163 | |
| 164 | To programmatically detect what is plugged into each port a structure such as this may be created |
| 165 | {{{ |
| 166 | typedef struct |
| 167 | { |
| 168 | const char* portName; |
| 169 | const char* sysfsPath; |
| 170 | const char* usb2sysfsPath; |
| 171 | |
| 172 | } USBPortInfo_t; |
| 173 | |
| 174 | const USBPortInfo_t usbPortInfo[] = { |
| 175 | { "USB2", "/sys/bus/usb/devices/1-1.4", NULL }, |
| 176 | { "USB3-1", "/sys/bus/usb/devices/2-1.1", "/sys/bus/usb/devices/1-1.1" }, |
| 177 | { "USB3-2", "/sys/bus/usb/devices/2-1.2", "/sys/bus/usb/devices/1-1.2" }, |
| 178 | { "USB-C", "/sys/bus/usb/devices/4-1", NULL }, |
| 179 | }; |
| 180 | |
| 181 | void ScanUSBports(void) |
| 182 | { |
| 183 | int n; |
| 184 | char tmp[100]; |
| 185 | struct stat st; |
| 186 | FILE* file; |
| 187 | const char* pathFound; |
| 188 | |
| 189 | typedef enum { noUsb, usbFound, usb2Found } USBFound_t; |
| 190 | |
| 191 | USBFound_t found = noUsb; |
| 192 | |
| 193 | for(n = 0; n < ARRAY_ELEMS(usbPortInfo); n++) { |
| 194 | if(stat(usbPortInfo[n].sysfsPath, &st) == 0) { |
| 195 | found = usbFound; |
| 196 | pathFound = usbPortInfo[n].sysfsPath; |
| 197 | } else if((usbPortInfo[n].usb2sysfsPath != NULL) && (stat(usbPortInfo[n].usb2sysfsPath, &st) == 0)) { |
| 198 | found = usb2Found; |
| 199 | pathFound = usbPortInfo[n].usb2sysfsPath; |
| 200 | } else { |
| 201 | found = noUsb; |
| 202 | } |
| 203 | |
| 204 | if(found != noUsb) { |
| 205 | sprintf_s(tmp, "%s/product", pathFound); |
| 206 | file = fopen(tmp, "r"); |
| 207 | if(file != NULL) { |
| 208 | if(found == usb2Found) { |
| 209 | strcpy(tmp, "*USB2* "); |
| 210 | fgets(tmp + 7, sizeof(tmp) - 7, file); |
| 211 | } else { |
| 212 | fgets(tmp, sizeof(tmp), file); |
| 213 | } |
| 214 | printf("%s [%s]\r\n", usbPortInfo[n].portName, tmp); |
| 215 | fclose(file); |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | }}} |