• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

HIDL第一个Hello World

相关文章 海叔叔 4年前 (2021-10-28) 45次浏览 已收录 0个评论
# 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

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:HIDL第一个Hello World
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址