aux.c: fixed bug with choosing device by path

This commit is contained in:
Edward Emelianov 2021-09-10 12:13:07 +03:00
parent 18d489d84f
commit 01e5b454e2

View File

@ -20,13 +20,13 @@
#include "aux.h" #include "aux.h"
#include <fcntl.h>
#include <libudev.h> #include <libudev.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <usefull_macros.h> #include <usefull_macros.h>
static char *VID = NULL, *PID = NULL;
// try to find serial device with given vid/pid/filepath // try to find serial device with given vid/pid/filepath
// @return device filepath (allocated here!) or NULL if not found // @return device filepath (allocated here!) or NULL if not found
char *find_device(){ char *find_device(){
@ -64,33 +64,29 @@ char *find_device(){
udev_device_unref(dev); udev_device_unref(dev);
continue; continue;
} }
int found = 0; int found = FALSE;
do{ do{
// 1st try to check recently found VID/PID
int good = 0;
if(VID){ if(strcmp(VID, vid)) break; else ++good;}
if(PID){ if(strcmp(PID, pid)) break; else ++good;}
if(good == 2){
DBG("Use recently found VID/PID");
found = 1; break;
}
// user give us VID and/or PID? Check them // user give us VID and/or PID? Check them
if(GP->vid){ if(strcmp(GP->vid, vid)) break;} // user give another vid if(GP->vid){ if(strcmp(GP->vid, vid)) break;} // user give another vid
if(GP->pid){ if(strcmp(GP->vid, vid)) break;} // user give another pid if(GP->pid){ if(strcmp(GP->vid, vid)) break;} // user give another pid
// now try to check by user given device name // now try to check by user given device name
if(GP->device){ if(GP->device){
if(strcmp(GP->device, devpath) == 0) found = 1; DBG("User gives device name: %s", GP->device);
if(strcmp(GP->device, devpath) == 0) found = TRUE;
break; break;
} }
// still didn't found? OK, take the first comer! // still didn't found? OK, take the first comer!
DBG("The first comer"); DBG("The first comer");
found = 1; found = TRUE;
}while(0); }while(0);
if(found){ if(found){
FREE(VID); FREE(PID); if(open(devpath, O_RDWR) < 0){
VID = strdup(vid); PID = strdup(pid); WARN("open()");
path = strdup(devpath); found = FALSE;
DBG("Found: VID=%s, PID=%s, path=%s", VID, PID, path); }else{
path = strdup(devpath);
DBG("Found: VID=%s, PID=%s, path=%s", vid, pid, path);
}
} }
/*printf("\tman: %s, prod: %s\n", udev_device_get_sysattr_value(dev,"manufacturer"), /*printf("\tman: %s, prod: %s\n", udev_device_get_sysattr_value(dev,"manufacturer"),
udev_device_get_sysattr_value(dev,"product")); udev_device_get_sysattr_value(dev,"product"));