簡単な説明
Android システムでさまざまなハードウェア デバイス (カメラ、センサー、オーディオ デバイスなど) を使用する場合、HAL
ハードウェア抽象化レイヤー ( ) と通信する必要があります。HAL
ハードウェアとアプリケーションの間のブリッジとして機能する中間層です。ただし、ハードウェア デバイスが異なるため、HAL インターフェイスもハードウェアによって異なります。ここで、Android HAL
Interface Definition Language ( HIDL
) の出番です。
HIDL
開発者は、ハードウェア デバイスごとに独自のインターフェイスを定義できます。これらのインターフェイスは、入力パラメーター、戻り値、例外など、ハードウェア デバイスの機能と特性を定義します。これらのインターフェイスを使用すると、アプリケーションは、特定のハードウェアの詳細を知らなくても、ハードウェア デバイスと直接通信できます。ではAndroid
、との通信HIDL
に広く使用されていますHAL
。
このメカニズムを設計する目的はAndroid Project Treble
、 framework( )を から分離して、再コンパイルせずにフレームワーク部分を直接上書きおよび更新できるようにすることです。android O
HIDL
framework
HAL
HAL
HIDL
実際にはプロセス間通信に使用されます ( Inter-process Communication,IPC
)。プロセス間通信はBinder
call ( Binderized
) と呼ぶことができます。プロセスにリンクする必要があるライブラリの場合、passthough
モード(ただし、Java ではサポートされていません)。
コードを書く
Aosp
コード ディレクトリにディレクトリを作成します(バージョン関連 hardware/interfaces/stksensor/1.0
については、公式 Web サイトでバージョン管理の概念を1.0
確認できます)。
1. 新しいファイルを作成するIStksensor.hal
package android,[email protected];
improt IStksensorCallBack;
interface IStksensor {
setCallBack(IStksensorCallBack callback);
write(string data) generates (bool res);
init() generates (MyResult result);
};
2.作成UDT
(ユーザー定義型)、types.hal
package [email protected];
enum ResultCode : int32_t {
UNKNOWN = -1,
ERROR = 0,
OK = 1,
};
struct MyResult{
ResultCode resultCode;
string msg;
};
3. 新しいファイル コールバック ファイルを作成するIStksensorCallBack.hal
package [email protected];
interface IStksensorCallBack {
passData(uint32_t data) generates (MyResult result);
};
hidl-gen を使用して関連ファイルを生成する
ターミナルで次のコマンドを実行して、一時変数を設定します
PACKAGE=[email protected]
LOC=hardware/interfaces/stksensor/1.0/default
ソースコードのルートディレクトリに生成されたディレクトリ内のファイルAOSP
を実行しますhidl-gen
default
c++
hidl-gen -o $LOC -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
ソースコードのルートディレクトリに生成されたディレクトリ内のファイルAOSP
を実行しますhidl-gen
default
Android.bp
hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
AOSP
ソースコードのルートディレクトリで実行すると、ディレクトリにファイルが./hardware/interfaces/update-makefiles.sh
生成されますhardware/interfaces/stksensor/1.0/
Android.bp
インターフェイス コードを実装する
変更hardware/interfaces/stksensor/1.0/default/Stksensor.cpp
、ここでの実装は単にログを出力することです
// FIXME: your file license if you have one
#include "Stksensor.h"
#include <log/log.h>
namespace android::hardware::stksensor::implementation {
// Methods from ::android::hardware::stksensor::V1_0::IStksensor follow.
Return<void> Stksensor::setCallBack(const sp<::android::hardware::stksensor::V1_0::IStksensorCallBack>& callback) {
// TODO implement
ALOGE("stksensor service have called the funtion of setCallBack");
return Void();
}
Return<::android::hardware::stksensor::V1_0::ResultCode> Stksensor::write(const hidl_string& data) {
// TODO implement
ALOGE("stksensor service have called the funtion of write");
return ::android::hardware::stksensor::V1_0::ResultCode {};
}
Return<void> Stksensor::init(init_cb _hidl_cb) {
// TODO implement
ALOGE("stksensor service have called the funtion of init");
return Void();
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
//IStksensor* HIDL_FETCH_IStksensor(const char* /* name */) {
//return new Stksensor();
//}
//
} // namespace android::hardware::stksensor::implementation
起動サービスを追加
1.hardware/interfaces/stksensor/1.0/default/
作成時service.cpp
#define LOG_TAG "[email protected]"
#include <hidl/HidlTransportSupport.h>
#include <log/log.h>
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardwara::stksensor::V1_0::IStksensor;
using android::hardware::stksensor::V1_0::implementation::Stksensor;
using namespace android;
int main() {
configureRpcThreadpool(1, true);
sp<IStksensor> service = new Stksensor();
status_t status = service->registerAsService("stksensor");
if (status != OK) {
ALOGE("Error registering stksensor as service: %d", status);
return status;
}
ALOGE("Ok to registering stksensor as service");
joinRpcThreadpool();
return 1;
}
2.起動スクリプトhardware/interfaces/stksensor/1.0/default/
を作成する[email protected]
service stksensor-hal-1-0 /vendor/bin/hw/[email protected]
class hal
user system
group system
3.hardware/interfaces/stksensor/1.0/default/
作成され[email protected]
たvintf
<manifest version="1.0" type="device">
<hal format="hidl">
<name>android.hardware.stksensor</name>
<transport>hwbinder</transport>
<version>1.0</version>
<interface>
<name>IStksensor</name>
<instance>stksensor</instance>
</interface>
</hal>
</manifest>
コンパイル スクリプトを変更する
1.再度修正hardware/interfaces/stksensor/1.0/default/Android.bp
// FIXME: your file license if you have one
cc_library_shared {
// FIXME: this should only be -impl for a passthrough hal.
// In most cases, to convert this to a binderized implementation, you should:
// - change '-impl' to '-service' here and make it a cc_binary instead of a
// cc_library_shared.
// - add a *.rc file for this module.
// - delete HIDL_FETCH_I* functions.
// - call configureRpcThreadpool and registerAsService on the instance.
// You may also want to append '-impl/-service' with a specific identifier like
// '-vendor' or '-<hardware identifier>' etc to distinguish it.
name: "[email protected]",
relative_install_path: "hw",
// FIXME: this should be 'vendor: true' for modules that will eventually be
// on AOSP.
proprietary: true,
srcs: [
"Stksensor.cpp",
"StksensorCallBack.cpp",
],
shared_libs: [
"libhidlbase",
"libutils",
"[email protected]",
],
}
cc_binary {
name: "[email protected]",
relative_install_path: "hw",
defaults: ["hidl_defaults"],
proprietary: true,
init_rc: ["[email protected]"],
vintf_fragments: ["[email protected]"],
srcs: [
"SerialPort.cpp",
"service.cpp",
],
shared_libs: [
"libbase",
"liblog",
"libdl",
"libutils",
"libhardware",
"libhidlbase",
"libhidltransport",
"[email protected]",
],
}
2.device/$your_company/$SOC/device.mk
追加
PRODUCT_PACKAGES += \
[email protected] \
[email protected]
現在のstksensor
ディレクトリ構造
stksensor/
└── 1.0
├── Android.bp
├── default
│ ├── Android.bp
│ ├── [email protected]
│ ├── [email protected]
│ ├── service.cpp
│ ├── StksensorCallBack.cpp
│ ├── StksensorCallBack.h
│ ├── Stksensor.cpp
│ └── Stksensor.h
├── IStksensorCallBack.hal
├── IStksensor.hal
└── types.hal
注: 一部のavc
権限が報告されるため、自分で追加する必要があります