如何检测属于gsm/3g调制解调器的tty是数据端口还是控制端口? [英] How to I detect whether a tty belonging to a gsm/3g-modem is a data or control port?
问题描述
我目前正在为Linux路由器编写一个小工具,当我将适当的调制解调器插入其USB端口时,该工具将建立wwan(gsm/3g)连接.插入设备后,将注册多个tty,而我目前维护着制造商和设备的列表,以及已注册的哪个tty是控制/数据端口.
I'm currently writing a small tool for a linux router that sets up a wwan (gsm/3g) connection when I plug an appropriate modem into its USB port. When the device is plugged in several ttys are registered and I currently maintain a list of manufacturers and devices and which of their registered ttys is the control / data port.
如果可能的话,我想摆脱此列表,找到一种方法以某种方式直接探测已注册的tty,以检查它们是控制端口还是数据端口.
If possible I want to get rid of this list and find a way to somehow probe the registered ttys directly to check if they are a control port or a data port.
我检查了wvdial和modem-manager的源代码,以查看这些工具如何检测正确的端口,但无法找到合适的信息.我还尝试在sysfs中查找信息以区分端口,但这也不成功.
I examined the sourcecode of wvdial and modem-manager to see how these tools detect the right port but was unable to find suitable information. I also tried to look for information in sysfs to distinguish the ports but this wasn't successful either.
推荐答案
我使用此脚本获取3g usb加密狗的数据和控制端口.
I use this script to get Data and Control ports for 3g usb dongle.
#!/bin/sh
. /usr/share/libubox/jshn.sh
for a in `ls /sys/bus/usb/devices`; do
local vendor product
[ -z "$usb" -a -f /sys/bus/usb/devices/$a/idVendor -a -f /sys/bus/usb/devices/$a/idProduct ] || continue
vendor=$(cat /sys/bus/usb/devices/$a/idVendor)
product=$(cat /sys/bus/usb/devices/$a/idProduct)
echo Vendor $vendor, Product $product
[ -f /lib/network/wwan/$vendor:$product ] && {
usb=/lib/network/wwan/$vendor:$product
devicename=$a
echo usb: $usb devicename: $devicename
}
done
[ -n "$usb" ] && {
local old_cb control data
json_set_namespace wwan old_cb
json_init
json_load "$(cat $usb)"
echo "$(cat $usb)"
json_select
json_get_vars desc control data
json_set_namespace $old_cb
[ -n "$control" -a -n "$data" ] && {
ttys=$(ls -d /sys/bus/usb/devices/$devicename/${devicename}*/tty* | sed "s/.*\///g" | tr "\n" " ")
ctl_device=$(echo $ttys | cut -d" " -f $((control + 1)))
[ -n "$ctl_device" ] && ctl_device=/dev/$ctl_device
dat_device=$(echo $ttys | cut -d" " -f $((data + 1)))
[ -n "$dat_device" ] && dat_device=/dev/$dat_device
echo control_device: $ctl_device, data_device: $dat_device
}
}
样本输出:
已连接中兴MF667
Connected ZTE MF667
Vendor 1a40, Product 0101 #this is usb hub
Vendor 19d2, Product 0016
usb: /lib/network/wwan/19d2:0016 devicename: 1-1.2
{
"desc": "ONDA MF110/ZTE",
"control": 1,
"data": 2
}}
control_device: /dev/ttyUSB1, data_device: /dev/ttyUSB2
已连接华为E3131
Connected Huawei E3131
Vendor 1a40, Product 0101 #this is usb hub
Vendor 12d1, Product 1506
usb: /lib/network/wwan/12d1:1506 devicename: 1-1.2
{
"desc": "Huawei E367/E398",
"control": 2,
"data": 0
}}
control_device: /dev/ttyUSB2, data_device: /dev/ttyUSB0
这篇关于如何检测属于gsm/3g调制解调器的tty是数据端口还是控制端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!