解决硬编码导致不兼容hv问题

This commit is contained in:
ling 2024-03-20 18:47:29 +08:00
parent 945723e869
commit adb173a6ca

View File

@ -43,10 +43,10 @@
#define CM_MAX_PATHLEN 256
#define CM_INVALID_VAL (~((int)0))
/* get first line from file 'fname'
* And convert the content into a hex number, then return this number */
static int file_get_value(const char *fname, int base) {
static int file_get_value(const char *fname, int base)
{
FILE *fp = NULL;
long num;
char buff[32 + 1] = {'\0'};
@ -78,7 +78,8 @@ static int file_get_value(const char *fname, int base) {
* This function will search the directory 'dirname' and return the first child.
* '.' and '..' is ignored by default
*/
static int dir_get_child(const char *dirname, char *buff, unsigned bufsize, const char *prefix) {
static int dir_get_child(const char *dirname, char *buff, unsigned bufsize, const char *prefix)
{
struct dirent *entptr = NULL;
DIR *dirptr;
@ -101,7 +102,8 @@ static int dir_get_child(const char *dirname, char *buff, unsigned bufsize, cons
return 0;
}
static int conf_get_val(const char *fname, const char *key) {
static int conf_get_val(const char *fname, const char *key)
{
char buff[128] = {'\0'};
FILE *fp = fopen(fname, "r");
if (!fp)
@ -194,7 +196,8 @@ static void query_usb_interface_info(char *path, struct usb_interface_info *p) {
path[offset] = '\0';
}
static int detect_path_cdc_wdm_or_qcqmi(char *path, char *devname, size_t bufsize) {
static int detect_path_cdc_wdm_or_qcqmi(char *path, char *devname, size_t bufsize)
{
size_t offset = strlen(path);
char tmp[32];
@ -229,7 +232,8 @@ static int detect_path_cdc_wdm_or_qcqmi(char *path, char *devname, size_t bufsiz
/* There is a chance that, no device(qcqmiX|cdc-wdmX) is generated. We should warn user about that! */
snprintf(devname, bufsize, "/dev/%s", tmp);
if (access(devname, R_OK | F_OK) && errno == ENOENT) {
if (access(devname, R_OK | F_OK) && errno == ENOENT)
{
int major, minor;
dbg_time("access %s failed, errno: %d (%s)", devname, errno, strerror(errno));
@ -330,7 +334,9 @@ BOOL qmidevice_detect(char *qmichannel, char *usbnet_adapter, unsigned bufsize,
if (driver_type == SOFTWARE_QMI || driver_type == SOFTWARE_MBIM) {
detect_path_cdc_wdm_or_qcqmi(pl->path, devname, sizeof(devname));
} else if (driver_type == SOFTWARE_ECM_RNDIS_NCM) {
}
else if (driver_type == SOFTWARE_ECM_RNDIS_NCM)
{
int atIntf = -1;
if (profile->usb_dev.idVendor == 0x2c7c) { //Quectel
@ -388,26 +394,6 @@ BOOL qmidevice_detect(char *qmichannel, char *usbnet_adapter, unsigned bufsize,
return FALSE;
}
int isContain(const char *str1, const char *str2) {
if (strstr(str1, str2) != NULL) {
return 1;
} else {
return 0;
}
}
void replaceStr(char *str, char *orig, char *rep) {
char buffer[1024];
char *p;
while (p = strstr(str, orig)) {
memcpy(buffer, str, p - str);
buffer[p - str] = '\0';
sprintf(buffer + strlen(buffer), "%s%s", rep, p + strlen(orig));
strcpy(str, buffer);
}
}
int mhidevice_detect(char *qmichannel, char *usbnet_adapter, PROFILE_T *profile) {
struct dirent* ent = NULL;
DIR *pDir;
@ -415,39 +401,35 @@ int mhidevice_detect(char *qmichannel, char *usbnet_adapter, PROFILE_T *profile)
int i = 0;
char path[256];
int find = 0;
while (rootdir_mhi[i]) {
const char *rootdir = rootdir_mhi[i++];
dbg_time("rootdir : %s ", rootdir);
pDir = opendir(rootdir);
if (!pDir) {
if (errno != ENOENT) {
if (errno != ENOENT)
dbg_time("opendir %s failed: %s", rootdir, strerror(errno));
}
continue;
}
while ((ent = readdir(pDir)) != NULL) {
char netcard[32] = {'\0'};
char devname[32] = {'\0'};
int software_interface = SOFTWARE_QMI;
char *pNode = NULL;
pNode = strstr(ent->d_name, "_IP_HW0"); //0306_00.01.00_IP_HW0
if (!pNode) {
continue;
}
char *d_name[32];
strcpy(d_name, ent->d_name);
replaceStr(d_name, "_IP_HW0", "");
dbg_time("while -> pcie_name = : %s ", d_name);
pNode = strstr(ent->d_name, "_IP_HW0"); //0306_00.01.00_IP_HW0
if (!pNode)
continue;
snprintf(path, sizeof(path), "%s/%.32s/net", rootdir, ent->d_name);
dir_get_child(path, netcard, sizeof(netcard), NULL);
if (!netcard[0]) {
if (!netcard[0])
continue;
}
if (usbnet_adapter[0] && strcmp(netcard, usbnet_adapter)) { //not '-i x
if (usbnet_adapter[0] && strcmp(netcard, usbnet_adapter)) //not '-i x'
continue;
}
if (!strcmp(rootdir, "/sys/bus/mhi/devices")) {
snprintf(path, sizeof(path), "%s/%.13s_IPCR", rootdir, ent->d_name); // 13 is sizeof(0306_00.01.00)
if (!access(path, F_OK)) {
@ -462,51 +444,46 @@ int mhidevice_detect(char *qmichannel, char *usbnet_adapter, PROFILE_T *profile)
continue;
}
//修改为动态获取
// 解决硬编码导致不兼容hv问题
snprintf(path, sizeof(path), "%s/%s_IPCR", rootdir, d_name);
if (access(path, F_OK)) {
snprintf(path, sizeof(path), "%s/%s_QMI0", rootdir, d_name);
if (access(path, F_OK)) {
snprintf(path, sizeof(path), "%s/%s_MBIM", rootdir, d_name);
if (!access(path, F_OK)) {
if (!access(path, F_OK))
software_interface = SOFTWARE_MBIM;
}
}
}
// 原代码
// snprintf(path, sizeof(path), "%s/%.14s_IPCR", rootdir, ent->d_name);
// dbg_time("while -> path3 : %s ", path);
// snprintf(path, sizeof(path), "%s/%.13s_IPCR", rootdir, ent->d_name);
// if (access(path, F_OK)) {
// snprintf(path, sizeof(path), "%s/%.14s_QMI0", rootdir, ent->d_name);
// dbg_time("while -> path4 : %s ", path);
// snprintf(path, sizeof(path), "%s/%.13s_QMI0", rootdir, ent->d_name);
// if (access(path, F_OK)) {
// snprintf(path, sizeof(path), "%s/%.14s_MBIM", rootdir, ent->d_name);
// dbg_time("while -> path5 : %s ", path);
// if (!access(path, F_OK)) {
// snprintf(path, sizeof(path), "%s/%.13s_MBIM", rootdir, ent->d_name);
// if (!access(path, F_OK))
// software_interface = SOFTWARE_MBIM;
// }
// }
// }
dbg_time("while -> path_final : %s ", path);
if (access(path, F_OK)) {
if (access(path, F_OK))
continue;
}
strncat(path, "/mhi_uci_q", sizeof(path)-1);
dir_get_child(path, devname, sizeof(devname), NULL);
if (!devname[0]) {
if (!devname[0])
continue;
}
dbg_time("while -> path_final_sub : %s ,dev_name: %s ", path, devname);
sprintf(usbnet_adapter, "%s", netcard);
sprintf(qmichannel, "/dev/%s", devname);
profile->software_interface = software_interface;
find = 1;
break;
}
closedir(pDir);
}
return find;
}
@ -514,7 +491,8 @@ int atdevice_detect(char *atchannel, char *usbnet_adapter, PROFILE_T *profile) {
if (!access("/sys/class/net/sipa_dummy0", F_OK)) {
strcpy(usbnet_adapter, "sipa_dummy0");
snprintf(profile->qmapnet_adapter, sizeof(profile->qmapnet_adapter), "%s%d", "pcie", profile->pdp - 1);
} else {
}
else {
dbg_time("atdevice_detect failed");
goto error;
}
@ -522,7 +500,8 @@ int atdevice_detect(char *atchannel, char *usbnet_adapter, PROFILE_T *profile) {
if (!access("/dev/stty_nr31", F_OK)) {
strcpy(atchannel, "/dev/stty_nr31");
profile->software_interface = SOFTWARE_ECM_RNDIS_NCM;
} else {
}
else {
goto error;
}
@ -532,11 +511,13 @@ int atdevice_detect(char *atchannel, char *usbnet_adapter, PROFILE_T *profile) {
}
int get_driver_type(PROFILE_T *profile) {
int get_driver_type(PROFILE_T *profile)
{
/* QMI_WWAN */
if (profile->usb_intf.bInterfaceClass == USB_CLASS_VENDOR_SPEC) {
return SOFTWARE_QMI;
} else if (profile->usb_intf.bInterfaceClass == USB_CLASS_COMM) {
}
else if (profile->usb_intf.bInterfaceClass == USB_CLASS_COMM) {
switch (profile->usb_intf.bInterfaceSubClass) {
case USB_CDC_SUBCLASS_MBIM:
return SOFTWARE_MBIM;
@ -548,7 +529,8 @@ int get_driver_type(PROFILE_T *profile) {
default:
break;
}
} else if (profile->usb_intf.bInterfaceClass == USB_CLASS_WIRELESS_CONTROLLER) {
}
else if (profile->usb_intf.bInterfaceClass == USB_CLASS_WIRELESS_CONTROLLER) {
if (profile->usb_intf.bInterfaceSubClass == 1 && profile->usb_intf.bInterfaceProtocol == 3)
return SOFTWARE_ECM_RNDIS_NCM;
}
@ -558,12 +540,14 @@ int get_driver_type(PROFILE_T *profile) {
return DRV_INVALID;
}
struct usbfs_getdriver {
struct usbfs_getdriver
{
unsigned int interface;
char driver[255 + 1];
};
struct usbfs_ioctl {
struct usbfs_ioctl
{
int ifno; /* interface 0..N ; negative numbers reserved */
int ioctl_code; /* MUST encode size + direction of data so the
* macros in <asm/ioctl.h> give correct values */
@ -573,7 +557,8 @@ struct usbfs_ioctl {
#define IOCTL_USBFS_DISCONNECT _IO('U', 22)
#define IOCTL_USBFS_CONNECT _IO('U', 23)
int usbfs_is_kernel_driver_alive(int fd, int ifnum) {
int usbfs_is_kernel_driver_alive(int fd, int ifnum)
{
struct usbfs_getdriver getdrv;
getdrv.interface = ifnum;
if (ioctl(fd, USBDEVFS_GETDRIVER, &getdrv) < 0) {
@ -584,7 +569,8 @@ int usbfs_is_kernel_driver_alive(int fd, int ifnum) {
return 1;
}
void usbfs_detach_kernel_driver(int fd, int ifnum) {
void usbfs_detach_kernel_driver(int fd, int ifnum)
{
struct usbfs_ioctl operate;
operate.data = NULL;
operate.ifno = ifnum;
@ -596,7 +582,8 @@ void usbfs_detach_kernel_driver(int fd, int ifnum) {
}
}
void usbfs_attach_kernel_driver(int fd, int ifnum) {
void usbfs_attach_kernel_driver(int fd, int ifnum)
{
struct usbfs_ioctl operate;
operate.data = NULL;
operate.ifno = ifnum;
@ -608,13 +595,15 @@ void usbfs_attach_kernel_driver(int fd, int ifnum) {
}
}
int reattach_driver(PROFILE_T *profile) {
int reattach_driver(PROFILE_T *profile)
{
int ifnum = 4;
int fd;
char devpath[128] = {'\0'};
snprintf(devpath, sizeof(devpath), "/dev/bus/usb/%03d/%03d", profile->usb_dev.busnum, profile->usb_dev.devnum);
fd = open(devpath, O_RDWR | O_NOCTTY);
if (fd < 0) {
if (fd < 0)
{
dbg_time("%s fail to open %s", __func__, devpath);
return -1;
}
@ -625,8 +614,8 @@ int reattach_driver(PROFILE_T *profile) {
}
#define SIOCETHTOOL 0x8946
int ql_get_netcard_driver_info(const char *devname) {
int ql_get_netcard_driver_info(const char *devname)
{
int fd = -1;
struct ethtool_drvinfo drvinfo;
struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
@ -656,7 +645,8 @@ int ql_get_netcard_driver_info(const char *devname) {
return 0;
}
int ql_get_netcard_carrier_state(const char *devname) {
int ql_get_netcard_carrier_state(const char *devname)
{
int fd = -1;
struct ethtool_value edata;
struct ifreq ifr; /* ifreq suitable for ethtool ioctl */
@ -687,7 +677,8 @@ int ql_get_netcard_carrier_state(const char *devname) {
return edata.data;
}
static void *catch_log(void *arg) {
static void *catch_log(void *arg)
{
PROFILE_T *profile = (PROFILE_T *)arg;
int nreads = 0;
char tbuff[256+32];
@ -718,7 +709,8 @@ static void *catch_log(void *arg) {
return NULL;
}
int ql_capture_usbmon_log(PROFILE_T *profile, const char *log_path) {
int ql_capture_usbmon_log(PROFILE_T *profile, const char *log_path)
{
char usbmon_path[256];
pthread_t pt;
pthread_attr_t attr;