Android系统hal实现 —(以应用层app操作gpio为例)

上一章我们讲了如何用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 android.hardware.testgpio@1.0;

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 PACKAGE=android.hardware.testgpio@1.0
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
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值