Utilize Avahi Libraries in Android for mDNS Discovering
16lz
2021-01-23
零.
Continue my previous avahi article, here, i would port the same example code to Android platform.
Download and unravel Android NDK
for me, the untarred NDK root locates in ~/Android/AndroidNDK/android-ndk-r10e
And install the android tools in the linux system. (yum for fedora, apt-get for ubunu/debian)
sudo apt-get install gcc-arm-linux-androideabi
sudo apt-get install android-tools-adb
I strongly suggest you DO NOT install the others android-tool, header and libraries via apt-get/yum.
If you need those, just download from Android website and decompress them.
一.
Download and untar the avahi libraries, in here I use version 0.6.31.
Download 2 patches and place them in the untarred avahi folder:
avahi.org/raw-attachment/ticket/354/0001-Add-Android-support.patch
avahi.org/raw-attachment/ticket/354/0002-Add-uninstalled.pc.in-files.patch
apply the patches.
patch -p1 0001-Add-Android-support.patch
You would encounter a warning here, just ignore it. it is for there is no .git folder inside the avahi folder.
patch -p1 0002-Add-uninstalled.pc.in-files.patch
For the old configuration tools does not support androideabi, you need to upgrade the config.guess and config.sub from here. it is, to overwrite these 2 files inside the avahi root folder.
二.
The struct in6_pktinfo has been added in Android 4.0(2011/10). When the avahi 0.6.31 was released (2012/2), the BioniC (the position of glibC for Android) libraries on most populace's Android phone was not implemented the whole IVP6. Therefore, the structure ha been declare inside avahi. In present(2015/12), I thought there is no one set build-target version before 4.0. It is requisite to modify code to remove the obsolete lines in avahi-core/socket.c , about line 67:
#endif#if(0)#ifdef __BIONIC__struct in6_pktinfo { struct in6_addr ipi6_addr; int ipi6_ifindex;};#endif#endifstatic void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) {
The in6_pktinfo has been declared in NDKfolder/platforms/android- version/ arch/usr/include/linux/ipv6.h, to comment out it to avoid compilation error.
Optional:
if your application would be integrated web-browser and avahi disovering functions, you should modify the code avahi-core/netlink.c, about line 160:
memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = groups;#if(1) addr.nl_pid = 0;#else addr.nl_pid = getpid();#endif if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
It is, the add.nl_pid value would not be conflicted with browser's. More detail of this change is in here.
三.
The Avahi configure arguments for me be:
./configure --prefix=$PWD/built --disable-dbus --disable-mono --disable-monodoc --disable-qt4 --disable-qt3 --disable-glib --disable-gobject --disable-gtk --disable-gtk3 --disable-gdbm --disable-dbm --disable-python --disable-pygtk -disable-libdaemon --disable-nls --disable-xmltoman --with-xml=none --disable-core-docs --disable-doxygen-doc CPPFLAGS="-I/home/gaiger/Android/AndroidNDK/android-ndk-r10e/platforms/android-14/arch-arm/usr/include -mcpu=arm926ej-s -mfloat-abi=soft" LDFLAGS=-L/home/gaiger/Android/AndroidNDK/android-ndk-r10e/platforms/android-14/arch-arm/usr/lib --host=arm-linux-androideabi --disable-stack-protector --with-distro=debian
NOTE :
Zero:
I disable most functions of Avahi. Because those are useless for the goal of discovering mDNS service and dependent on dbus, gtk, qt and so forth libraries. To building those depended libraries are difficult for Android.
One:
The path, /home/gaiger/Android/AndroidNDK/android-ndk-r10e/platforms/android-14/arch-arm/usr, is where the Android system libraries locates, those are being part of Android NDK.
/home/gaiger/Android/AndroidNDK/android-ndk-r10e : where AndroidNDK root locates.
platforms/android-14/arch-arm : I chose to android 4.0 with arm-architecture as my target system.
it is very natural to add include path (-I usr_path/include) and linking path(-I usr_path/lib) as arguments in cross-compilation.
TWO:
-mcpu=arm926ej-s -mfloat-abi=softfp : To avoid crash in some (mostly those are not existed ) cheap Android device CPU without hardware floating computing. Besides, Avahi do not involve in heavy floating point computing, So adapt pure soft-float computing would not lead mDNS discovering sluggish.
arm926ej-s means, assuming the CPU does not support hardware floating but supports Jazelle ( for java VM).
More detail of CPU architecture, you could refer to this article.
THREE :
--with-distro=debian : for me , I use ubuntu. If you use fedora/CentOS, you use set it as fedora.
FOUR:
--disable-stack-protector : there is no libssp (stack smash protector) in current BioniC defaultly. To advert configuring error, one should disable this function. The detail about libssp is in here.
四.
After configuring done, it is very instinctive to press make and make install.
To this step, the avahi libraries for Android has been built, in the avahi_root/built
五.
Create a folder and a sub-folder in avahi root: sandyboxcode/jni
Copy the code sandbox-discover-standalone.c of my last article in folder sandyboxcode/jni.
And Create two files, Application.mk and Android.mk in the same folder ( sandyboxcode/jni).
The content of file Application.mk be:
#APP_ABI := armeabi-v7aAPP_ABI := armeabi
The Android.mk file be:
LOCAL_PATH := $(my-dir)ifneq ($(DYNAMIC_LINK),true)DYNAMIC_LINK = falseinclude $(CLEAR_VARS)LOCAL_MODULE := libavahi-coreLOCAL_SRC_FILES := ../../built/lib/libavahi-core.ainclude $(PREBUILT_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := libavahi-commonLOCAL_SRC_FILES := ../../built/lib/libavahi-common.ainclude $(PREBUILT_STATIC_LIBRARY)elseinclude $(CLEAR_VARS)LOCAL_MODULE := libavahi-coreLOCAL_SRC_FILES := ../../built/lib/libavahi-core.soinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := libavahi-commonLOCAL_SRC_FILES := ../../built/lib/libavahi-common.soinclude $(PREBUILT_SHARED_LIBRARY)endif #if $DYNAMIC_LINK .neq. trueinclude $(CLEAR_VARS)LOCAL_MODULE := sandbox-discover-standaloneLOCAL_SRC_FILES := sandbox-discover-standalone.c LOCAL_CFLAGS := -O2 -mcpu=arm926ej-s -mfloat-abi=softfpLOCAL_C_INCLUDES := $(LOCAL_PATH)/../../built/include ifeq ($(DYNAMIC_LINK),false)LOCAL_STATIC_LIBRARIES := libavahi-core libavahi-commonelseLOCAL_SHARED_LIBRARIES += libavahi-common libavahi-coreendif #if $(DYNAMIC_LINK) eq falseLOCAL_LDLIBS += -lloginclude $(BUILD_EXECUTABLE)
Then to the sandbox folder, type :
$your_ndk_path/ndk-build -B V=1
Now you have gotten binary of sandbox-discover-standalone for Android, under the folder libs/armeabi .
六.
Push the built binary to a android device, which might need to be rooted to execute a binary in shell.
For me, I use the development board tiny210 on Android 4.0, which was rooted natively.
check if the device connected to working computer indeed:
$ adb devicesList of devices attached 0123456789ABCDEF device
push the binary to the android :
$ adb push libs/armeabi/sandbox-discover-standalone sandbox-discover-standalone2291 KB/s (202300 bytes in 0.086s)
execute it in the shell:
$ adb shell / # chmod 777 sandbox-discover-standalone/ # ./sandbox-discover-standalone 1 services have been foundService Type: _http._tcpService Name: GAIGER NETEYE CS2230Domain Name: localInterface: eth0 IPv4Address: 192.168.13.107/NETEYECS2230-001A97018882.local:80TEXT Data: NULLdiscover cast 1002 msec
It is successful totally.
Known insufficiency :
if you encounter Segmentation fault when you execute the binary in Android,
you should correct the code in sandbox-discover-standalone.c, of function ServiceResolverCallback:
pServiceInfo->pServiceName = strndup(pServiceName, MAX_STR_LEN); pServiceInfo->pDomainName = strndup(pDomain, MAX_STR_LEN);#ifndef __ANDROID__ pServiceInfo->pInterface = strndup(if_indextoname(interface, (char*)pServiceName), MAX_STR_LEN);#endif pServiceInfo->pProtocolNumber = strndup(avahi_proto_to_string(protocol), MAX_STR_LEN);
It could avoid the crashing: the function if_indextoname of bionic libraries is not as stable as libc.
Extra:
It is passible to write a stand Makefile for Android ndk build :
i assure the Makefile be in sandyboxcode/ Makefile (NOT inside the jni folder), for the sandbox-discover-sandalone.c be :
CC := arm-linux-androideabi-gccCXX := arm-linux-androideabi-g++INC := -I~/Android/AndroidNDK/android-ndk-r10e/platforms/android-14/arch-arm/usr/include INC += -I../built/include -IjniAVAHI_LIB_PATH := ../built/libLIB := ~/Android/AndroidNDK/android-ndk-r10e/platforms/android-14/arch-arm/usr/lib/liblog.soLIB += $(AVAHI_LIB_PATH)/libavahi-core.aLIB += $(AVAHI_LIB_PATH)/libavahi-common.aCFLAGS := -g all : $(CC) $(CFLAGS) $(INC) jni/sandbox-discover-standalone.c $(LIB) -o sandbox-discover-standaloneclean : rm sandbox-discover-standalone -rf
Note : the shared libraries liblog.so be linking directly!.
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer