rxw的RK3568的evb1公板,有2个以太口,
默认UI界面只能配置eth0,无法配置eth1,
实际应用中,有时需要一旦有网线插入,就需要该地址设置为指定IP地址。
本文介绍2个最简单的方法实现固定IP。
一、通过修改ipconfig.txt
安卓设备以太口的配置信息保存在以下文件中:
/data/misc/ethernet/ipconfig.txt
该文件是二进制文件,而且默认只有eth0的配置信息,一口君通过一些特殊方法,生成了包含eth0、eth1的两个网口配置的配置文件,
并且rxw原厂的ui界面可以识别该配置文件。
配置文件用16进制格式打开内容如下:
00000000 00 00 00 03 00 0C 69 70 41 73 73 69 67 6E 6D 65 ......ipAssignme
00000010 6E 74 00 06 53 54 41 54 49 43 00 0B 6C 69 6E 6B nt..STATIC..link
00000020 41 64 64 72 65 73 73 00 0D 31 39 32 2E 31 36 38 Address..192.168
00000030 2E 34 30 2E 33 34 00 00 00 18 00 07 67 61 74 65 .40.34......gate
00000040 77 61 79 00 00 00 00 00 00 00 01 00 0C 31 39 32 way..........192
00000050 2E 31 36 38 2E 34 30 2E 31 00 03 64 6E 73 00 07 .168.40.1..dns..
00000060 30 2E 30 2E 30 2E 30 00 03 64 6E 73 00 07 30 2E 0.0.0.0..dns..0.
00000070 30 2E 30 2E 30 00 0D 70 72 6F 78 79 53 65 74 74 0.0.0..proxySett
00000080 69 6E 67 73 00 04 4E 4F 4E 45 00 02 69 64 00 04 ings..NONE..id..
00000090 65 74 68 30 00 03 65 6F 73 00 0C 69 70 41 73 73 eth0..eos..ipAss
000000A0 69 67 6E 6D 65 6E 74 00 06 53 54 41 54 49 43 00 ignment..STATIC.
000000B0 0B 6C 69 6E 6B 41 64 64 72 65 73 73 00 0D 31 39 .linkAddress..19
000000C0 32 2E 31 36 38 2E 32 2E 31 32 35 00 00 00 18 00 2.168.2.125.....
000000D0 07 67 61 74 65 77 61 79 00 00 00 00 00 00 00 01 .gateway........
000000E0 00 0B 31 39 32 2E 31 36 38 2E 32 2E 31 00 03 64 ..192.168.2.1..d
000000F0 6E 73 00 07 30 2E 30 2E 30 2E 30 00 03 64 6E 73 ns..0.0.0.0..dns
00000100 00 07 30 2E 30 2E 30 2E 30 00 0D 70 72 6F 78 79 ..0.0.0.0..proxy
00000110 53 65 74 74 69 6E 67 73 00 04 4E 4F 4E 45 00 02 Settings..NONE..
00000120 69 64 00 04 65 74 68 31 00 03 65 6F 73 -- -- -- id..eth1..eos
用 ascii格式打开如下【因为是二进制文件,会有部分内容是乱码】:
ipAssignment STATIC linkAddress
192.168.40.34 gateway 192.168.40.1 dns 0.0.0.0 dns 0.0.0.0
proxySettings NONE id eth0 eos ipAssignment STATIC linkAddress
192.168.2.125 gateway 192.168.2.1 dns 0.0.0.0 dns 0.0.0.0
proxySettings NONE id eth1 eos
总结一下,主要配置信息如下:
| 网口 | ip | 网关 |
|-----------------------------------------|
| eth0 | 192.168.40.34 | 192.168.40.1 |
| eth1 | 192.168.2.125 | 192.168.2.1 |
将该文件push进开发板,重启即可
adb root
adb remount
adb pull /data/misc/ethernet/ipconfig.txt
这样只要eth1对应的以太口up,就会自动设置ip地址192.168.2.125
同时并不影响UI配置eth0接口
注意:
这种方法仅在rxw3568+android11上测试通过,其他平台或者android版本没有测试。
二、通过开源项目ifplugd
除了第一种种方法还可以通过一个开源的项目ifplugd来实现。
通过ifplugd工具监听网口热插拔信息,然后执行指定脚本,将配置命令存放在脚本中即可。
ifplugd的移植需要用到libdaemon库,该库主要提供守护进程以及log接口功能。
1. 移植步骤
1)准备文件:
libdaemon-0.14.tar.gz
ifplugd-0.14.tar.gz
后台回复:eth
,既可以获取
没有采用最新的libdaemon、ifplugd,会有版本兼容问题
本实例基于ndk编译器编译,
为了方便起见,我把libdaemon、ifplugd源文件全部拷贝到ndk实例工程下,libdaemon的头文件,拷贝了2次,
方便c文件包含。
.....src-ifplugd$ tree ./
./
├── Application.mk
├── daemon.h
├── dexec.c
├── dexec.h
├── dfork.c
├── dfork.h
├── dlog.c
├── dlog.h
├── dnonblock.c
├── dnonblock.h
├── dpid.c
├── dpid.h
├── dsignal.c
├── dsignal.h
├── ethtool-kernel.h
├── ethtool-local.h
├── ifplugd.c
├── ifstatus.c
├── include
├── interface.c
├── interface.h
├── libdaemon
│ ├── daemon.h
│ ├── dexec.h
│ ├── dfork.h
│ ├── dlog.h
│ ├── dnonblock.h
│ ├── dpid.h
│ └── dsignal.h
├── svn-revision.h
└── YROS.mk2 directories, 29 files
2)修改配置文件
编译描述信息位于文件YROS.mk中,
【读者根据自己工程,文件会有所不同】
YROS.mk修改如下:
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \ifplugd.c interface.cdexec.c dfork.c dlog.c dnonblock.c dpid.c dsignal.c LOCAL_SHARED_LIBRARIES := \LOCAL_MODULE_TAGS := optional
LOCAL_CLANG := trueLOCAL_MODULE:= ethcheckdinclude $(BUILD_EXECUTABLE)
3)编译
- 编译错误1
λ build.bat ndk-build.cmd
[armeabi-v7a] Compile thumb : ifplugd <= ifplugd.c
src/ifplugd.c:66:13: error: use of undeclared identifier 'SYSCONFDIR'
char *run = SYSCONFDIR"/ifplugd/ifplugd.action"; ^
src/ifplugd.c:66:23: error: expected ';' after top level declarator
char *run = SYSCONFDIR"/ifplugd/ifplugd.action"; ^ ;
src/ifplugd.c:155:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: note: expanded from macro 'FD_SET'
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd)) ^ ~~
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:48:23: note: expanded from macro '__FDELT'
#define __FDELT(fd) ((fd) / NFDBITS) ^
src/ifplugd.c:374:36: error: expected ')' daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); ^
src/ifplugd.c:374:15: note: to match this '(' daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); ^
src/ifplugd.c:388:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: note: expanded from macro 'FD_SET'
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd)) ^ ~~
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:48:23: note: expanded from macro '__FDELT'
#define __FDELT(fd) ((fd) / NFDBITS) ^
src/ifplugd.c:704:26: error: expected ')' printf("ifplugd "VERSION" (SVN: "SVN_REVISION")\n"); ^
src/ifplugd.c:704:15: note: to match this '(' printf("ifplugd "VERSION" (SVN: "SVN_REVISION")\n"); ^
2 warnings and 4 errors generated.
make: *** [obj/local/armeabi-v7a/objs/ifplugd/ifplugd.o] Error 1
修改文件ifplugd.c
59 #define VARRUN "/system"
63 #define SYSCONFDIR ""
64 #define VERSION "yikoulinux"
- 编译错误2
λ build.batndk-build.cmd
[armeabi-v7a] Compile thumb : ifplugd <= dexec.c
[armeabi-v7a] Compile thumb : ifplugd <= dfork.c
[armeabi-v7a] Compile thumb : ifplugd <= dlog.c
[armeabi-v7a] Compile thumb : ifplugd <= dnonblock.c
[armeabi-v7a] Compile thumb : ifplugd <= dpid.c
src/dpid.c:63:43: error: use of undeclared identifier 'LOCALSTATEDIR'snprintf(fn, sizeof(fn), "%s/%s.pid", VARRUN, daemon_pid_file_ident ? daemon_pid_file_ident : "unknown");^
src/dpid.c:51:16: note: expanded from macro 'VARRUN'
#define VARRUN LOCALSTATEDIR "/run"^
1 error generated.
make: *** [obj/local/armeabi-v7a/objs/ifplugd/dpid.o] Error 1
修改文件dpid.c
50 #define LOCALSTATEDIR
4) 编译成功
编译成功log如下:
H:\compileforandroid
λ build.bat ndk-build.cmd
[armeabi-v7a] Compile thumb : ifplugd <= ifplugd.c
src/ifplugd.c:148:53: warning: missing sentinel in function call [-Wsentinel] execl(run, run, interface, arg, extra_arg, 0); ^ , NULL
H:/yros-ndk-windows/build//../sysroot/usr/include\unistd.h:105:5: note: function has been explicitly marked sentinel here
int execl(const char* __path, const char* __arg0, ...) __attribute__((__sentinel__)); ^
src/ifplugd.c:157:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: note: expanded from macro 'FD_SET'
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd)) ^ ~~
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:48:23: note: expanded from macro '__FDELT'
#define __FDELT(fd) ((fd) / NFDBITS) ^
src/ifplugd.c:390:18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:75:50: note: expanded from macro 'FD_SET'
#define FD_SET(fd, set) (__FDS_BITS(set)[__FDELT(fd)] |= __FDMASK(fd)) ^ ~~
H:/yros-ndk-windows/build//../sysroot/usr/include\sys/select.h:48:23: note: expanded from macro '__FDELT'
#define __FDELT(fd) ((fd) / NFDBITS) ^
3 warnings generated.
[armeabi-v7a] Compile thumb : ifplugd <= interface.c
[armeabi-v7a] Compile thumb : ifplugd <= dexec.c
[armeabi-v7a] Compile thumb : ifplugd <= dfork.c
[armeabi-v7a] Compile thumb : ifplugd <= dlog.c
[armeabi-v7a] Compile thumb : ifplugd <= dnonblock.c
[armeabi-v7a] Compile thumb : ifplugd <= dpid.c
[armeabi-v7a] Compile thumb : ifplugd <= dsignal.c
[armeabi-v7a] Executable : ifplugd
[armeabi-v7a] Install : ifplugd => libs/armeabi-v7a/ifplugd
程序位置如下:
libs\armeabi-v7a\ifplugd
测试
1. 查看ifplugd参数
ifplugd -h
rk3568_r:/ # ifplugd -h
ifplugd [options]-a --no-auto Do not enable interface automatically (off)-n --no-daemon Do not daemonize (for debugging) (off)-s --no-syslog Do not use syslog, use stderr instead (for debugging) (off)-b --no-beep Do not beep (off)-f --ignore-fail Ignore detection failure, retry instead (failure is treated as DOWN) (off)-F --ignore-fail-positive Ignore detection failure, retry instead (failure is treated as UP) (off)-i --iface=IFACE Specify ethernet interface (eth0)-r --run=EXEC Specify program to execute (/ifplugd/ifplugd.action)-I --ignore-retval Don't exit on nonzero return value of program executed (off)-t --poll-time=SECS Specify poll time in seconds (1)-u --delay-up=SECS Specify delay for configuring interface (0)-d --delay-down=SECS Specify delay for deconfiguring interface (5)-m --api-mode=MODE Force API mode (mii, priv, ethtool, wlan, auto) (auto)-q --no-shutdown Don't run script on daemon quit (off)-w --wait-on-fork Wait until daemon fork finished (off)-x --extra-arg Specify an extra argument for action script-h --help Show this help-k --kill Kill a running daemon-c --check-running Check if a daemon is currently running-v --version Show version-S --suspend Suspend running daemon-R --resume Resume running daemon-z --info Write status of running daemon to syslog
2. 增加脚本if.sh
当网口eth1 up后,ifplugd会执行如下命令【下面动作由ifplugd自动执行】:
/system/if.sh eth1 up
根据参数顺序,编写脚本if.sh如下:
#!/bin/bashIPADDR=192.168.40.8
ETHPORT=eth1
echo "daniel peng set" $ETHPORT $IPADDR
echo $#
echo $0
echo $1
echo $2
if [ $# -eq 2 ];thenif [ $1 = $ETHPORT ];then echo $ETHPORTif [ $2 = "up" ];thenifconfig $ETHPORT $IPADDRsleep 1ip rule add from all lookup main pref 9000 sleep 1echo 1 > /proc/sys/net/ipv4/ip_forward iptables -Fecho "set" $ETHPORT "done"elif [ $2 = "down" ];thenecho "down"elif [ $2 = "disable" ];thenecho "disable"elif [ $2 = "error" ];thenecho "error"fifi
fi
功能:
网口eth1 up后,设置该网口地址为IPADDR,即:192.168.40.8
读者可以根据自己的需要编写相应的脚本。
3. 运行ifplugd
ifplugd监听网口eth1并且执行后面的脚本文件
在板子上输入以下命令:
ifplugd -i eth1 -r "sh /system/if.sh"
查看该守护进程:
H:\compileforandroid
λ adb shell
rk3568_r:/ # ifplugd -i eth1 -r "sh /system/if.sh"
rk3568_r:/ # ps -ef | grep if
wifi 280 1 0 09:58:52 ? 00:00:00 android.hardware.wifi@1.0-service
wifi 385 1 0 09:58:53 ? 00:00:00 wificond
root 1826 1 0 10:18:06 ? 00:00:00 ifplugd -i eth1 -r sh /system/if.sh
root 1834 1819 2 10:18:17 pts/0 00:00:00 grep if
一旦网口插入网线后,地址就会被自动设置。
要实现开机就自动运行ifplugd,可以参考下面文章
《安卓如何设置开机自动启动某个程序?ramdisk + init.rc给你搞定》
补充
ifplugd的确可以实现监测网卡的状态,并执行相应脚本,
但是有个前提,就是网口已经注册到系统中,即用ifconfig -a能查看到
如何是usb网口这种设备,在插入usb口之后网口设备才会注册,
那么这种情况下,要想设置usb网卡,那么就就需要修改ifplugd程序。
下文,给大家讲解如何自己实现一个简单的网口检测并设置ip的小程序。
完整代码,点赞留言,后台回复:eth