HIDL第一個Hello World(待續)
阿新 • • 發佈:2019-01-08
# source build/envsetup.sh
# lunch
# make hidl-gen -j4
一、server端
1.建立INaruto.hal檔案
# cd hardware
# mkdir -p ~/android/hardware/interfaces/naruto/1.0/default
# cd hardware/interfaces/naruto/1.0
# emacs INaruto.hal
package [email protected];
interface INaruto {
helloWorld(string name) generates (string result);
};
2.使用hidl-gen工具編譯INaruto.hal自動生成.mk,.bp,.cpp檔案
# cd ~/android
# [email protected]
# LOC=hardware/interfaces/naruto/1.0/default/
# make hidl-gen -j16
# hidl-gen -o $LOC -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
# hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
在default下生成Android.bp,Naruto.cpp,Naruto.h檔案
# ./hardware/interfaces/update-makefiles.sh
在1.0目錄下生成Android.bp,Android.mk檔案
# touch hardware/interfaces/naruto/1.0/default/ [email protected]
# touch hardware/interfaces/naruto/1.0/default/service.cpp
3.修改原始碼,然後編譯自動生成檔案
# mmm hardware/interfaces/naruto/1.0/default/
4.啟動server端程序
# emacs hardware/interfaces/naruto/1.0/default/[email protected]
service naruto_hal_service /vendor/bin/hw/[email protected]
class hal
user system
group system
5.server端程序原始碼
# emacs hardware/interfaces/naruto/1.0/default/service.cpp
#define LOG_TAG " [email protected]"
#include <android/hardware/naruto/1.0/INaruto.h>
#include <hidl/LegacySupport.h>
using android::hardware::naruto::V1_0::INaruto;
using android::hardware::defaultPassthroughServiceImplementation;
int main() {
return defaultPassthroughServiceImplementation<INaruto>();
}
6.編譯server端程式碼
# mmm hardware/interfaces/naruto/1.0/default
7.編譯vendor.img,刷機
# make vendorimage -j16
二、client端
1.測試程式碼: naruto_test
# emacs test.cpp
#include <android/hardware/naruto/1.0/INaruto.h>
#include <hidl/Status.h>
#include <hidl/LegacySupport.h>
#include <utils/misc.h>
#include <hidl/HidlSupport.h>
#include <stdio.h>
using android::hardware::naruto::V1_0::INaruto;
using android::sp;
using android::hardware::hidl_string;
int main()
{
int ret;
android::sp<INaruto> service = INaruto::getService();
if(service == nullptr) {
printf("Failed to get service\n");
return -1;
}
service->helloWorld("JayZhang", [&](hidl_string result) {
printf("%s\n", result.c_str());
});
return 0;
}
2.Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PROPRIETARY_MODULE := true
LOCAL_MODULE := naruto_test
LOCAL_SRC_FILES := \
client.cpp \
LOCAL_SHARED_LIBRARIES := \
liblog \
libhidlbase \
libutils \
[email protected] \
include $(BUILD_EXECUTABLE)
3.在manifest檔案裡新增vendor介面的定義,不然在client端是沒法拿到service的,在相應的manifest.xml裡面加入
# emacs manifest.xml
…
…
<hal format="hidl">
<name>android.hardware.naruto</name>
<transport>hwbinder</transport>
<version>1.0</version>
<interface>
<name>INaruto</name>
<instance>default</instance>
</interface>
</hal>
# adb push manifest.xml /vendor
三、燒寫vendor.img測試
# adb shell
# /vendor/bin/hw/[email protected]
# ./naruto_test