美文网首页
Android hal服务搭建

Android hal服务搭建

作者: AnterC | 来源:发表于2022-12-07 18:21 被阅读0次

第一部分:编写hal服务

1.先全编译源码:

source build/envset.sh

lunch

make

2.安装hidl-gen工具:

make hidl-gen

3.在hardware/interfaces/automotive目录下新建mcuupdate/1.0目录,并在1.0目录中创建接口IMcuUpdate.hal。

4.执行下面命令会自动生成对应的C++文件:

hidl-gen -o hardware/interfaces/automotive/mcuupdate/1.0/default -Lc++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.automotive.mcuupdate@1.0

执行后会在1.0文件夹下新建default文件夹,此文件夹中会生成McuUpdate.h和McuUpdate.cpp文件,打开McuUpdate.h,去掉下面的注释,使用直通式HAL(Passthrough模式)来通信:

McuUpdate.cpp也进行对应修改:

5.执行下面命令,会在hardware/interfaces/automotive/mcuupdate/1.0/default目录中生成Android.bp文件:

hidl-gen -o hardware/interfaces/automotive/mcuupdate/1.0/default -Landroidbp-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.automotive.mcuupdate@1.0

6.执行下面命令,来更新Makefile:

./hardware/interfaces/update-makefiles.sh

执行完之后会在hardware/interfaces/automotive/mcuupdate/1.0目录中生成Android.bp文件

7.在hardware/interfaces/automotive/mcuupdate/1.0/default目录下新建android.hardware.automotive.mcuupdate@1.0-service.rc,此为程序的入口函数,文件内容如下:

8.在hardware/interfaces/automotive/mcuupdate/1.0/default目录下新建service.cpp,里面添加入口main函数:

9.打开hardware/interfaces/automotive/mcuupdate/1.0/default/Android.bp,添加编译service.cpp成为可执行文件的代码:

10.编译生成hidl服务端跟客户端要用的各种库文件:

编译完生成下列文件:

out\target\product\spm8666p1_64_car\system\lib64\android.hardware.automotive.mcuupdate@1.0.so

out\target\product\spm8666p1_64_car\system\lib64\android.hardware.automotive.mcuupdate@1.0-adapter-helper.so

out\target\product\spm8666p1_64_car\vendor\lib64\android.hardware.automotive.mcuupdate@1.0-adapter-helper.so

out\target\product\spm8666p1_64_car\vendor\etc\init\android.hardware.automotive.mcuupdate@1.0-service.rc

out\target\product\spm8666p1_64_car\vendor\bin\hw\android.hardware.automotive.mcuupdate@1.0-service

11.在device/mediatek/mt6771/manifest.xml文件中添加以下内容,不然客户端无法拿到服务端service:

12.增加hash值

HIDL 接口层是 Treble 架构的基础,原则上发布后不能变动,新接口应升级版本号。系统编译时会检查已发布接口 hash 值,防止篡改。已有接口对应 hash 值详见hardware/interfaces/current.txt。新增扩展接口,可在 Android 根目录执行命令生成 hash 值:

hidl-gen -L hash -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.automotive.mcuupdate@1.0 >> hardware/interfaces/current.txt

13.device\mediatek\mt6771\device.mk在尾部添加PRODUCT_PACKAGES += android.hardware.automotive.mcuupdate@1.0-service,以便启动service。

14.修改VNDK相关部分:

    在build\make\target\product\vndk\current.txt和build\make\target\product\vndk\28.txt里按字母顺序添加VNDK-core: android.hardware.automotive.mcuupdate@1.0.so,否则编译不过。

15.全编译时如果出现 error: VNDK library list has been changed错误,解决办法是把out\target\product\spm8666p1_64_car\obj\PACKAGING\vndk_intermediates\libs.txt的内容对比到build\make\target\product\vndk\current.txt和build\make\target\product\vndk\28.txt

16.切记:如果hal文件变更了,重新生成h和cpp时会覆盖,所以每次更改hal前把default目录改下名字,等生成完了,再把老的代码手工移到新生成的文件里面。

第二部分:配置selinux,以便service能在rc里面启动

1.在system\sepolicy\vendor\file_contexts尾部添加

/(vendor|system/vendor)/bin/hw/android\.hardware\.automotive\.mcuupdate@1\.0-service u:object_r:hal_mcuupdate_default_exec:s0

2.新建system\sepolicy\vendor\hal_mcuupdate_default.te,内容如下:

type hal_mcuupdate_default, domain;

hal_server_domain(hal_mcuupdate_default, hal_mcuupdate)

type hal_mcuupdate_default_exec, exec_type, vendor_file_type, file_type;

init_daemon_domain(hal_mcuupdate_default)

3.在system\sepolicy\public\attributes中添加hal_attribute(mcuupdate);

4.在system\sepolicy\public\hwservice.te尾部添加type hal_mcuupdate_hwservice, hwservice_manager_type;

5.新建system\sepolicy\public\hal_mcuupdate.te,内容如下:

#HwBinder IPC from client to server, and callbacks

binder_call(hal_mcuupdate_client, hal_mcuupdate_server)

binder_call(hal_mcuupdate_server, hal_mcuupdate_client)

add_hwservice(hal_mcuupdate_server, hal_mcuupdate_hwservice)

allow hal_mcuupdate_client hal_mcuupdate_hwservice:hwservice_manager find;

6.同步system\sepolicy\public\attributes的内容hal_attribute(mcuupdate);到system\sepolicy\prebuilts\api\28.0\public\attributes

   同步system\sepolicy\public\hwservice.te的内容type hal_mcuupdate_hwservice, hwservice_manager_type;到system\sepolicy\prebuilts\api\28.0\public\hwservice.te

   同步system\sepolicy\public\hal_mcuupdate.te文件到system\sepolicy\prebuilts\api\28.0\public\

7.在system\sepolicy\private\hwservice_contexts中按字母顺序添加

android.hardware.automotive.mcuupdate::IMcuUpdate                    u:object_r:hal_mcuupdate_hwservice:s0

android.hardware.automotive.mcuupdate::IMcuUpdateCallback       u:object_r:hal_mcuupdate_hwservice:s0

需要跨进程使用的都要加上

8.在system\sepolicy\private\compat\26.0\26.0.ignore.cil和system\sepolicy\private\compat\27.0\27.0.ignore.cil的new_objects中添加hal_mcuupdate_hwservice

9.同步system\sepolicy\private\compat\26.0\26.0.ignore.cil和system\sepolicy\private\compat\27.0\27.0.ignore.cil的new_objects中hal_mcuupdate_hwservice到

system\sepolicy\prebuilts\api\28.0\private\compat\26.0\26.0.ignore.cil和system\sepolicy\prebuilts\api\28.0\private\compat\27.0\27.0.ignore.cil的new_objects中,

同步第7步的内容到system\sepolicy\prebuilts\api\28.0\private\hwservice_contexts中

10.编译中间接口:

mmm hardware/interfaces/automotive/mcuupdate/1.0/

11.编译出最终的库和service:

mmm hardware/interfaces/automotive/mcuupdate/1.0/default/

因为修改了很多selinux相关的东西,所以最终需要整个android源码编译一次。烧录到机器,就可以看到service起来了

第三部分:与上层app建立连接

1.前面android源码编译完之后,将在out\target\common\obj\JAVA_LIBRARIES\生成android.hardware.automotive.mcuupdate-V1.0-java_intermediates文件夹,拷贝里面的classes.jar到AndroidStudio工程的libs中,implementation files('libs/classes.jar')引入;

2.在AndroidStudio工程的Android.mk中加入LOCAL_STATIC_JAVA_LIBRARIES := android.hardware.automotive.mcuupdate-V1.0-java-static

3.源码进行一次编译,烧录进机器

4.IMcuUpdate service = IMcuUpdate.getService();获取到IMcuUpdate后就可以调用它里面的方法了。

第四部分:与HIDL Client建立连接

1.在LOCAL_SHARED_LIBRARIES中添加android.hardware.automotive.mcuupdate@1.0 \

2.在Client中添加#include <android/hardware/automotive/mcuupdate/1.0/IMcuUpdate.h>

然后在函数中使用sp<IMcuUpdate> pMcuUpdate = IMcuUpdate::getService();即可绑定上服务。

相关文章

网友评论

      本文标题:Android hal服务搭建

      本文链接:https://www.haomeiwen.com/subject/dranfdtx.html