rxw的RK3568的evb1公板,有2个以太口,
默认UI界面只能配置eth0,无法配置eth1,
实际应用中,有时需要一旦有网线插入,就需要该地址设置为指定IP地址。
本文介绍2个最简单的方法实现固定IP。
一、通过修改ipconfig.txt
安卓设备以太口的配置信息保存在以下文件中:
/data/misc/ethernet/ipconfig.txt
该文件是二进制文件,而且默认只有eth0的配置信息,一口君通过一些特殊方法,生成了包含eth0、eth1的两个网口配置的配置文件,
并且rxw原厂的ui界面可以识别该配置文件。
配置文件用16进制格式打开内容如下:
0000000000000003000C697041737369676E6D65......ipAssignme 000000106E740006535441544943000B6C696E6Bnt..STATIC..link 0000002041646472657373000D3139322E313638Address..192.168 000000302E34302E333400000018000767617465.40.34......gate 000000407761790000000000000001000C313932way..........192 000000502E3136382E34302E310003646E730007.168.40.1..dns.. 00000060302E302E302E300003646E730007302E0.0.0.0..dns..0. 00000070302E302E30000D70726F7879536574740.0.0..proxySett 00000080696E677300044E4F4E45000269640004ings..NONE..id.. 00000090657468300003656F73000C6970417373eth0..eos..ipAss 000000A069676E6D656E74000653544154494300ignment..STATIC. 000000B00B6C696E6B41646472657373000D3139.linkAddress..19 000000C0322E3136382E322E31323500000018002.168.2.125..... 000000D007676174657761790000000000000001.gateway........ 000000E0000B3139322E3136382E322E31000364..192.168.2.1..d 000000F06E730007302E302E302E300003646E73ns..0.0.0.0..dns 000001000007302E302E302E30000D70726F7879..0.0.0.0..proxy 0000011053657474696E677300044E4F4E450002Settings..NONE.. 0000012069640004657468310003656F73------id..eth1..eos
用 ascii格式打开如下【因为是二进制文件,会有部分内容是乱码】:
ipAssignmentSTATIClinkAddress 192.168.40.34gateway192.168.40.1dns0.0.0.0dns0.0.0.0 proxySettingsNONEideth0eosipAssignmentSTATIClinkAddress 192.168.2.125gateway192.168.2.1dns0.0.0.0dns0.0.0.0 proxySettingsNONEideth1eos
总结一下,主要配置信息如下:
|网口|ip|网关| |-----------------------------------------| |eth0|192.168.40.34|192.168.40.1| |eth1|192.168.2.125|192.168.2.1|
将该文件push进开发板,重启即可
adbroot adbremount adbpull/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.mk 2directories,29files
2)修改配置文件
编译描述信息位于文件YROS.mk中,
【读者根据自己工程,文件会有所不同】
YROS.mk修改如下:
LOCAL_PATH:=$(callmy-dir) include$(CLEAR_VARS) LOCAL_SRC_FILES:= ifplugd.cinterface.cdexec.cdfork.cdlog.cdnonblock.cdpid.cdsignal.c LOCAL_SHARED_LIBRARIES:= LOCAL_MODULE_TAGS:=optional LOCAL_CLANG:=true LOCAL_MODULE:=ethcheckd include$(BUILD_EXECUTABLE)
3)编译
编译错误1
λbuild.bat ndk-build.cmd [armeabi-v7a]Compilethumb:ifplugd<= ifplugd.c src/ifplugd.c13: error: use of undeclared identifier 'SYSCONFDIR' char *run = SYSCONFDIR"/ifplugd/ifplugd.action"; ^ src/ifplugd.c23: error: expected ';' after top level declarator char *run = SYSCONFDIR"/ifplugd/ifplugd.action"; ^ ; src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^ H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: 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/includesys/select.h23: note: expanded from macro '__FDELT' #define __FDELT(fd) ((fd) / NFDBITS) ^ src/ifplugd.c36: error: expected ')' daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); ^ src/ifplugd.c15: note: to match this '(' daemon_log(LOG_INFO, "ifplugd "VERSION" successfully initialized, link beat %sdetected.", status == IFSTATUS_UP ? "" : "not "); ^ src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^ H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: 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/includesys/select.h23: note: expanded from macro '__FDELT' #define __FDELT(fd) ((fd) / NFDBITS) ^ src/ifplugd.c26: error: expected ')' printf("ifplugd "VERSION" (SVN: "SVN_REVISION") "); ^ src/ifplugd.c15: note: to match this '(' printf("ifplugd "VERSION" (SVN: "SVN_REVISION") "); ^ 2 warnings and 4 errors generated. make: *** [obj/local/armeabi-v7a/objs/ifplugd/ifplugd.o] Error 1请添加图片描述
修改文件ifplugd.c
59#defineVARRUN"/system" 63#defineSYSCONFDIR"" 64#defineVERSION"yikoulinux"
编译错误2
λbuild.bat ndk-build.cmd [armeabi-v7a]Compilethumb: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.c43: 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.c16: 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#defineLOCALSTATEDIR
4) 编译成功
编译成功log如下:
H:compileforandroid λbuild.bat ndk-build.cmd [armeabi-v7a]Compilethumb:ifplugd<= ifplugd.c src/ifplugd.c53: warning: missing sentinel in function call [-Wsentinel] execl(run, run, interface, arg, extra_arg, 0); ^ , NULL H:/yros-ndk-windows/build//../sysroot/usr/includeunistd.h5: note: function has been explicitly marked sentinel here int execl(const char* __path, const char* __arg0, ...) __attribute__((__sentinel__)); ^ src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^ H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: 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/includesys/select.h23: note: expanded from macro '__FDELT' #define __FDELT(fd) ((fd) / NFDBITS) ^ src/ifplugd.c18: warning: multiple unsequenced modifications to 'sigfd' [-Wunsequenced] FD_SET(sigfd = daemon_signal_fd(), &rfds); ^ H:/yros-ndk-windows/build//../sysroot/usr/includesys/select.h50: 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/includesys/select.h23: 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在这里插入图片描述
程序位置如下:
libsarmeabi-v7aifplugd
测试
1. 查看ifplugd参数
ifplugd-h
rk3568_r:/#ifplugd-h ifplugd[options] -a--no-autoDonotenableinterfaceautomatically(off) -n--no-daemonDonotdaemonize(fordebugging)(off) -s--no-syslogDonotusesyslog,usestderrinstead(fordebugging)(off) -b--no-beepDonotbeep(off) -f--ignore-failIgnoredetectionfailure,retryinstead(failureistreatedasDOWN)(off) -F--ignore-fail-positiveIgnoredetectionfailure,retryinstead(failureistreatedasUP)(off) -i--iface=IFACESpecifyethernetinterface(eth0) -r--run=EXECSpecifyprogramtoexecute(/ifplugd/ifplugd.action) -I--ignore-retvalDon'texitonnonzeroreturnvalueofprogramexecuted(off) -t--poll-time=SECSSpecifypolltimeinseconds(1) -u--delay-up=SECSSpecifydelayforconfiguringinterface(0) -d--delay-down=SECSSpecifydelayfordeconfiguringinterface(5) -m--api-mode=MODEForceAPImode(mii,priv,ethtool,wlan,auto)(auto) -q--no-shutdownDon'trunscriptondaemonquit(off) -w--wait-on-forkWaituntildaemonforkfinished(off) -x--extra-argSpecifyanextraargumentforactionscript -h--helpShowthishelp -k--killKillarunningdaemon -c--check-runningCheckifadaemoniscurrentlyrunning -v--versionShowversion -S--suspendSuspendrunningdaemon -R--resumeResumerunningdaemon -z--infoWritestatusofrunningdaemontosyslog
2. 增加脚本if.sh
当网口eth1 up后,ifplugd会执行如下命令【下面动作由ifplugd自动执行】:
/system/if.sheth1up
根据参数顺序,编写脚本if.sh如下:
#!/bin/bash IPADDR=192.168.40.8 ETHPORT=eth1 echo"danielpengset"$ETHPORT$IPADDR echo$# echo$0 echo$1 echo$2 if[$#-eq2];then if[$1=$ETHPORT];then echo$ETHPORT if[$2="up"];then ifconfig$ETHPORT$IPADDR sleep1 ipruleaddfromalllookupmainpref9000 sleep1 echo1>/proc/sys/net/ipv4/ip_forward iptables-F echo"set"$ETHPORT"done" elif[$2="down"];then echo"down" elif[$2="disable"];then echo"disable" elif[$2="error"];then echo"error" fi fi fi
功能:
网口eth1 up后,设置该网口地址为IPADDR,即:192.168.40.8
读者可以根据自己的需要编写相应的脚本。
3. 运行ifplugd
ifplugd监听网口eth1并且执行后面的脚本文件
在板子上输入以下命令:
ifplugd-ieth1-r"sh/system/if.sh"
查看该守护进程:
H:compileforandroid λadbshell rk3568_r:/#ifplugd-ieth1-r"sh/system/if.sh" rk3568_r:/#ps-ef|grepif wifi280100952?0000android.hardware.wifi@1.0-service wifi385100953?0000wificond root1826101006?0000ifplugd-ieth1-rsh/system/if.sh root1834181921017pts/00000grepif
一旦网口插入网线后,地址就会被自动设置。
补充
ifplugd的确可以实现监测网卡的状态,并执行相应脚本,
但是有个前提,就是网口已经注册到系统中,即用ifconfig -a能查看到
如何是usb网口这种设备,在插入usb口之后网口设备才会注册,
那么这种情况下,要想设置usb网卡,那么就就需要修改ifplugd程序。
下文,给大家讲解如何自己实现一个简单的网口检测并设置ip的小程序。
审核编辑:汤梓红
-
接口
+关注
关注
33文章
8588浏览量
151095 -
二进制
+关注
关注
2文章
795浏览量
41650 -
ip地址
+关注
关注
0文章
302浏览量
17048 -
开发板
+关注
关注
25文章
5040浏览量
97412
原文标题:如何固定以太口地址为指定ip?
文章出处:【微信号:良许Linux,微信公众号:良许Linux】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论