Android HAL 모듈 작성 방법
Android의 HAL(Hardware Abstraction Layer)은 하드웨어와 프레임워크 사이의 추상화 계층을 제공하여, 특정 하드웨어 구현이 Android 시스템과 연동될 수 있도록 합니다. 본 포스팅에서는 새로운 HAL 모듈을 작성하는 방법에 대해 설명하며, 필요한 경우 AOSP(Android Open Source Project) 코드와 함께 설명하겠습니다.
1. HAL 개요
HAL은 Android의 하드웨어 접근을 추상화하는 계층으로, Android 프레임워크가 하드웨어의 구체적인 구현을 알 필요 없이 표준 인터페이스를 통해 하드웨어를 제어할 수 있도록 합니다. HAL은 보통 .so
(shared object) 라이브러리 형태로 제공되며, hwservicemanager
및 hidl-gen
을 이용해 HIDL(HAL Interface Definition Language) 기반으로 정의될 수도 있습니다.
HAL은 다음과 같은 계층을 포함합니다:
- HAL Interface:
hardware/interfaces/
에 정의된 인터페이스 - HAL Implementation:
hardware/libhardware
나hardware/vendor_name/
에서 구현 - Binder IPC 또는 HIDL IPC: HAL과 Android 서비스 사이의 통신
2. 새로운 HAL 모듈 작성 절차
2.1 HAL 인터페이스 정의
HAL 모듈을 작성하기 위해서는 먼저 인터페이스를 정의해야 합니다. Android 8.0 이후부터는 HIDL을 사용하여 인터페이스를 정의하는 것이 권장됩니다. 예를 들어, hardware/interfaces/example/1.0/
디렉토리에 새로운 HAL 인터페이스를 정의할 수 있습니다.
package android.hardware.example@1.0;
interface IExample {
string getExampleData();
void setExampleData(string data);
};
2.2 HAL 구현 작성
인터페이스를 정의한 후, 해당 인터페이스를 구현하는 HAL 모듈을 작성해야 합니다. 일반적으로 HAL 구현은 hardware/vendor_name/
디렉토리에 작성됩니다. 예를 들어, hardware/example/
디렉토리에 다음과 같은 구현을 추가할 수 있습니다.
#include <android/hardware/example/1.0/IExample.h>
#include <hidl/Status.h>
#include <utils/Log.h>
using android::hardware::example::V1_0::IExample;
using android::hardware::Return;
using android::hardware::Void;
using android::sp;
class Example : public IExample {
public:
Return<void> setExampleData(const hidl_string& data) override {
ALOGI("Setting example data: %s", data.c_str());
return Void();
}
Return<hidl_string> getExampleData() override {
return hidl_string("Example Data");
}
};
extern "C" IExample* HIDL_FETCH_IExample(const char* name) {
return new Example();
}
2.3 Android.mk 또는 Android.bp 설정
HAL 모듈이 정상적으로 빌드될 수 있도록 Android.bp
또는 Android.mk
파일을 설정해야 합니다. Android.bp 예제는 다음과 같습니다.
hidl_interface {
name: "android.hardware.example",
root: "hardware/interfaces/example",
vendor: true,
}
cc_library_shared {
name: "android.hardware.example@1.0-impl",
srcs: ["Example.cpp"],
shared_libs: ["liblog"],
vendor: true,
}
2.4 서비스 등록
HAL 모듈을 Android 시스템이 인식할 수 있도록 서비스 등록을 수행해야 합니다. 서비스는 registerAsService()
함수를 통해 등록할 수 있습니다.
#include <android/hardware/example/1.0/IExample.h>
#include <hidl/HidlTransportSupport.h>
using android::hardware::example::V1_0::IExample;
using android::hardware::defaultPassthroughServiceImplementation;
int main() {
return defaultPassthroughServiceImplementation<IExample>();
}
위 코드를 example-service.cpp
파일로 저장하고, Android.bp
에 해당 서비스를 빌드하도록 추가합니다.
cc_binary {
name: "example-service",
srcs: ["example-service.cpp"],
shared_libs: ["libhidlbase", "libhidltransport", "liblog"],
vendor: true,
}
2.5 빌드 및 테스트
새로운 HAL 모듈을 빌드하려면 lunch
명령어로 타겟을 설정한 후, 다음 명령어를 실행합니다.
m -j$(nproc) example-service
adb push out/target/product/<device>/system/bin/example-service /vendor/bin/
adb shell chmod +x /vendor/bin/example-service
adb shell /vendor/bin/example-service
서비스가 정상적으로 실행되었는지 확인하기 위해 logcat
을 모니터링할 수도 있습니다.
adb logcat -s example-service
3. 정리
본 포스팅에서는 새로운 Android HAL 모듈을 작성하는 방법에 대해 설명하였습니다. HAL 인터페이스를 정의하고, 구현을 작성한 후, 이를 시스템에 등록하여 서비스로 실행하는 과정까지 살펴보았습니다. AOSP 코드를 참고하여 HAL 모듈을 작성할 때, HIDL을 사용하는 것이 최신 Android 버전에서는 권장됩니다. 새로운 HAL 모듈을 개발할 때 위 내용을 참고하면 도움이 될 것입니다.
'Android > Framework' 카테고리의 다른 글
HAL 로그 확인 및 디버깅 (logcat, dmesg), HAL이 정상적으로 동작하지 않을 때 문제 해결 방법 (0) | 2025.03.28 |
---|---|
Android HAL 레이어에서 Native Library 및 커널 드라이버 연동 (0) | 2025.03.27 |
Android HAL 모듈 분석: Camera HAL과 Audio HAL (0) | 2025.03.25 |
Android HAL과 Binder IPC 이해하기 (0) | 2025.03.24 |
HIDL과 AIDL의 차이 (0) | 2025.03.23 |