Android板上检测4G模块
Android版本:4.4.4
Linux内核版本:3.0.6
近来工作上有个需求:在网络不可用的时候,重启4G模块。这个功能可以理解为断网自动重连。
开机过程中,4G模块转出为ttyUSB0~ttyUSB5等虚拟串口,串口数不定。可通过 cat /proc/kmsg 命令,查看内核打印信息
root@rk3188:/ # cat /proc/kmsg
...
...
...
...
<6>[ 8.882529] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB0 <6>[ 8.882904] option 2-1.4:1.1: GSM modem (1-port) converter detected <6>[ 8.883192] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB1 <6>[ 8.883577] option 2-1.4:1.2: GSM modem (1-port) converter detected <6>[ 8.883845] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB2 <6>[ 8.884276] option 2-1.4:1.3: GSM modem (1-port) converter detected <6>[ 8.885092] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB3 <6>[ 8.885784] option 2-1.4:1.4: GSM modem (1-port) converter detected <6>[ 8.887226] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB4 <4>[ 9.821694] ==> rtl8188e_iol_efuse_patch <6>[ 10.181269] ADDRCONF(NETDEV_UP): wlan0: link is not ready
虚拟串口的数目不定,但可以确定,4G模块初始化必会转出ttyUSB0串口。只需检查ttyUSB0的存在,就可以判断4G模块是否初始化。
filter = new IntentFilter(); filter.addAction(Intent.ACTION_BOOT_COMPLETED);//监听开机广播 mContext.registerReceiver( new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { File serial_4g = new File("/dev/ttyUSB0");//判断4G模块是否存在 if( serial_4g.exists() ){//若初始化了,则开启检测网络的线程 mRestartHandler.postDelayed(mRunnable, 3000); } } }, filter );
在检测不到网络的情况下,重启4G模块。需要注意的是,gpio 在 /sys/class/ 目录下。/sys/ 不太确定,但可以确定,/sys/class/ 下的文件是驱动代码导出来的。所以,对gpio的复位置位,不能简单地用java里的文件操作函数来实现,需要用到JNI。JNI,Java Native Interface,是联系上层java代码和底层C/C++代码的接口。
private Runnable mRunnable = new Runnable() { @Override public void run() { loge("XYP_DEBUG ====================I will check network===================="); NetworkInfo mNetworkInfo = getActiveNetworkInfo(); if(mNetworkInfo != null && mNetworkInfo.isAvailable()) { loge("XYP_DEBUG ======== net is available ========type = " + mNetworkInfo.getType() + " ==== typeName = " + mNetworkInfo.getTypeName() + " ===="); BrokenNetRestartCount = 0; } else {//网络不可用,对4G模块的reset复位后置位 try{ Gpio.SetGpioOutputLow("gpio_3g_reset");//调用了JNI方法 Thread.currentThread(); Thread.sleep(500);//休眠一段时间,保证复位完成 Gpio.SetGpioOutputHigh("gpio_3g_reset"); }catch(InterruptedException e){ Log.e( "InterruptedException",e.getMessage() ); } } //网络可能不稳定,每隔60S检查一下网络 mRestartHandler.postDelayed(mRunnable, 1000 * 60); } };
我用板子测的,确保它必定没连上网,以此检测4G模块是否重启。
还是用 cat /proc/kmsg 命令查看内核打印信息,也可以用dmesg命令,但 cat /proc/kmsg 才能实时打印。4G模块隔一段时间重启,cmd窗口也就隔段时间会输出ttyUSB0等虚拟串口的信息。这说明4G模块重启的功能已实现。
root@rk3188:/ # cat /proc/kmsg
...
...
...
...
<6>[ 13.999017] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB0 <6>[ 13.999340] option 2-1.4:1.1: GSM modem (1-port) converter detected <6>[ 13.999616] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB1 <6>[ 13.999951] option 2-1.4:1.2: GSM modem (1-port) converter detected <6>[ 14.000188] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB2 <6>[ 14.000474] option 2-1.4:1.3: GSM modem (1-port) converter detected <6>[ 14.000694] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB3 <6>[ 14.001012] option 2-1.4:1.4: GSM modem (1-port) converter detected <6>[ 14.001268] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB4 <6>[ 17.206536] DWC_OTG: ********vbus detect********************************************* <6>[ 17.408847] DWC_OTG: ********soft connect!!!***************************************** <6>[ 17.414856] DWC_OTG: USB SUSPEND <6>[ 17.521191] DWC_OTG: USB RESET <6>[ 17.561161] android_work: sent uevent USB_STATE=CONNECTED <6>[ 17.564801] DWC_OTG: USB RESET <6>[ 17.647356] android_usb gadget: high speed config #1: android <6>[ 17.647405] DWC_OTG: do_gadget_setup warning: pcd->lock locked without unlock <6>[ 17.647509] android_work: sent uevent USB_STATE=CONFIGURED <6>[ 23.544892] warning: `zygote' uses 32-bit capabilities (legacy support in use) <6>[ 30.373765] EXT4-fs (mmcblk0p10): re-mounted. Opts: (null) <6>[ 31.550755] healthd: battery l=50 v=0 t=0.0 h=2 st=3 chg=a <4>[ 44.744474] wifi_driver_write: wifi driver already rmmod <4>[ 44.762519] enter func vmac_open... <4>[ 44.866590] power on phy <6>[ 45.917707] vmac_mii_bus: probed <6>[ 45.917895] rk29 vmac rk29 vmac.0: PHY driver [Generic PHY] (mii_bus:phy_addr=0:00, irq=-1) <6>[ 45.919116] ADDRCONF(NETDEV_UP): eth0: link is not ready <6>[ 46.093472] acc_open <6>[ 46.093499] acc_release <6>[ 47.916994] PHY: 0:00 - Link is Down <7>[ 48.018648] SELinux: initialized (dev mmcblk0p11, type vfat), uses genfs_contexts <4>[ 48.070390] avc: denied { set } for property=sys.vold.hasAsec scontext=u:r:vold:s0 tcontext=u:object_r:system_prop:s0 tclass=property_service <3>[ 48.070460] init: sys_prop: permission denied uid:0 name:sys.vold.hasAsec <3>[ 48.070960] init: no such service 'fuse_internal_sd' <5>[ 50.078679] type=1400 audit(50.060:4): avc: denied { getattr } for pid=885 comm="zygote" path="socket:[5713]" dev=sockfs ino=5713 scontext=u:r:untrust ed_app:s0 tcontext=u:r:zygote:s0 tclass=unix_stream_socket <5>[ 50.078858] type=1400 audit(50.060:5): avc: denied { getopt } for pid=885 comm="zygote" path="/dev/socket/zygote" scontext=u:r:untrusted_app:s0 tconte xt=u:r:zygote:s0 tclass=unix_stream_socket <3>[ 51.133715] init: sys_prop: permission denied uid:1003 name:service.bootanim.exit <6>[ 52.473320] alarm_set_rtc: no RTC, time will be lost on reboot <6>[ 55.328510] usb 2-1.4: USB disconnect, device number 5 <6>[ 55.328967] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0 <6>[ 55.329094] option 2-1.4:1.0: device disconnected <3>[ 55.329312] option: option_instat_callback : error -2 <6>[ 55.329602] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1 <6>[ 55.329724] option 2-1.4:1.1: device disconnected <3>[ 55.345337] option: option_instat_callback : error -71 <3>[ 55.345827] option: option_instat_callback : error -71 <3>[ 55.346327] option: option_instat_callback : error -71 <6>[ 55.436820] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2 <6>[ 55.436909] option 2-1.4:1.2: device disconnected <6>[ 55.437268] option1 ttyUSB3: GSM modem (1-port) converter now disconnected from ttyUSB3 <6>[ 55.437363] option 2-1.4:1.3: device disconnected <6>[ 55.437660] option1 ttyUSB4: GSM modem (1-port) converter now disconnected from ttyUSB4 <6>[ 55.437740] option 2-1.4:1.4: device disconnected <6>[ 63.496831] usb 2-1.4: new high speed USB device number 6 using usb20_host <6>[ 63.612508] usb 2-1.4: New USB device found, idVendor=1c9e, idProduct=9b3c <6>[ 63.612537] usb 2-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3 <6>[ 63.612572] usb 2-1.4: Product: USB Modem <6>[ 63.612590] usb 2-1.4: Manufacturer: LONGSUNG <6>[ 63.658409] option 2-1.4:1.0: GSM modem (1-port) converter detected <6>[ 63.659242] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB0 <6>[ 63.660367] option 2-1.4:1.1: GSM modem (1-port) converter detected <6>[ 63.661184] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB1 <6>[ 63.662499] option 2-1.4:1.2: GSM modem (1-port) converter detected <6>[ 63.663283] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB2 <6>[ 63.664638] option 2-1.4:1.3: GSM modem (1-port) converter detected <6>[ 63.665446] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB3 <6>[ 63.667054] option 2-1.4:1.4: GSM modem (1-port) converter detected <6>[ 63.669260] usb 2-1.4: GSM modem (1-port) converter now attached to ttyUSB4 <6>[ 69.245090] healthd: battery l=50 v=0 t=0.0 h=2 st=3 chg=a