文章目录
注:本文为个人学习记录,可能存在个别或多处错误,欢迎指正和讨论。
参考:
https://www.jianshu.com/p/ca6823b897b5
https://source.android.com/devices/architecture
进行学习。
接android9.0 从driver到APP(2)–hardware
一、 接口描述文件创建
1.1 创建HIDL目录
mkdir -p hardware/interfaces/sample/1.0
1.2 创建接口描述文件sample.hal,
/*
* Copyright (C) 2019 Alex
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.sample@1.0;
interface ISample {
init();
close();
Read(vec<int32_t> val);
Write(int32_t val);
getVal(vec<int32_t> val);
setVal(int32_t val);
};
1.3 生成HAL 相关文件
在Android根目录,使用hidl-gen工具生成HAL 相关文件
hidl-gen -o hardware/interfaces/sample/1.0/default -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport android.hardware.sample@1.0
二 、更新Makefile
然后使用脚本来更新Makefile,自动生成 Android.bp
./hardware/interfaces/update-makefiles.sh
update-makefiles.sh脚本是SDK自带的。
目录结构:
./
├── Android.bp
├── default
│ ├── Sample.cpp
│ └── Sample.h
└── ISample.hal
在1.0的目录的Android.bp内容如下:
// This file is autogenerated by hidl-gen -Landroidbp.
hidl_interface {
name: "android.hardware.sample@1.0",
root: "android.hardware",
vndk: {
enabled: true,
},
srcs: [
"ISample.hal",
],
interfaces: [
"android.hidl.base@1.0",
],
gen_java: true,
}
三 、 service.cpp相关
3.1 service.cpp创建
在default目录创建 service.cpp
内容可以从其他文件夹中复制过来再修改:
改完之后的内容如下:
/*
* Copyright (C) 2019 Alex
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "android.hardware.sample@1.0-service"
#include <android/hardware/sample/1.0/ISample.h>
#include <hidl/LegacySupport.h>
using android::hardware::sample::V1_0::ISample;
using android::hardware::defaultPassthroughServiceImplementation;
int main() {
return defaultPassthroughServiceImplementation<ISample>();
}
在default 目录创建一个
android.hardware.sample@1.0-service.rc
这个文件是自动启动的时候需要使用。
3.2 Android.bp
default目录没有自动生成Android.bp。
从其他地方拷贝个修改吧
cc_library_shared {
name: "android.hardware.sample@1.0-impl",
defaults: ["hidl_defaults"],
proprietary: true,
relative_install_path: "hw",
srcs: ["Sample.cpp"],
cflags: [
"-Wall",
"-Werror",
],
shared_libs