上一章我们讲了如何用JNI实现Android 应用层app操作节点:https://blog.csdn.net/weixin_41486477/article/details/110791176
上一章提到需要过CTS认证的是不能使用JNI方法的,所以本章整理如何构建一个hal实现Android 应用层app操作节点。
驱动和hal(testgpio.default.so)部分等同JNI实现的部分,下面重点分享如何构建自己的hal,并在framework中调用它。
一.构建hal
新建hardware\interfaces\testgpio\1.0\ITestgpio.hal,generates表示生成的函数类型,如例就是int32_t类型。
package [email protected];
interface Itestgpio {
set_gpio_up(int32_t gpio_index) generates (int32_t result);
set_gpio_down(int32_t gpio_index) generates (int32_t result);
testgpio_open()generates (int32_t result);
};
借助hidl-gen工具生成目录文件:
1.执行make hidl-gen 先安装工具
2.依次执行
export [email protected]
export LOC=hardware/interfaces/testgpio/1.0/default/
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
./hardware/interfaces/update-makefiles.sh
生成的文件仅仅是一个模板,需要按照我们的需求自行修改
修改hardware\interfaces\testgpio\1.0\default\Testgpio.h
#ifndef ANDROID_HARDWARE_TESTGPIO_V1_0_TESTGPIO_H
#define ANDROID_HARDWARE_TESTGPIO_V1_0_TESTGPIO_H
#include <android/hardware/testgpio/1.0/ITestgpio.h>
#include <hidl/MQDescriptor.h>
#include <hardware/hardware.h>
#include <hidl/Status.h>
#include "hardware/testgpio.h"
namespace android {
namespace hardware {
namespace testgpio {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
struct Testgpio : public ITestgpio {
Testgpio();
~Testgpio();
// Methods from ::android::hardware::testgpio::V1_0::ITestgpio follow.
Return<int32_t> set_gpio_up(int32_t gpio_index) override;
Return<int32_t> set_gpio_down(int32_t gpio_index) override;
Return<int32_t> testgpio_open() override;
private:
int openHal();
testgpio_device_t *mDevice;
int ret;
// Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" ITestgpio* HIDL_FETCH_ITestgpio(const char* name);
} // namespace implementation
} // namespace V1_0
} // namespace testgpio
} // namespace hardware
} // namespace android
#endif
修改hardware\interfaces\testgpio\1.0\default\Testgpio.cpp
#include "Testgpio.h"
#include <utils/Log.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "Tes