Android 11 HAL 回调函数

创建hal目录

mkdir -p hardware/interfaces/hal/1.0
 

创建hal文件

cat > hardware/interfaces/hal/1.0/types.hal << EOF
package android.hardware.hal@1.0;

enum HalStatus : uint32_t {
    OK = 0,
    NG = 1,
};
EOF

cat > hardware/interfaces/hal/1.0/IHalClientCallback.hal << EOF
package android.hardware.hal@1.0;

interface IHalClientCallback {
    getCounter(uint32_t counter) generates (HalStatus status);
};
EOF

cat > hardware/interfaces/hal/1.0/IHal.hal << EOF
package android.hardware.hal@1.0;

import IHalClientCallback;

interface IHal {
    setHalClientCallback(IHalClientCallback clientCallback) generates (HalStatus status);
};
EOF

生成Android.bp

 ./hardware/interfaces/update-makefiles.sh

生成cpp文件

PACKAGE=android.hardware.hal@1.0
LOC=./hardware/interfaces/hal/1.0/default
hidl-gen -o $LOC -L c++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
hidl-gen -o $LOC -L androidbp-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
 

hardware/interfaces/hal/1.0/default/Hal.h

cat > hardware/interfaces/hal/1.0/default/Hal.h << EOF

// FIXME: your file license if you have one

#pragma once

#include <android/hardware/hal/1.0/IHal.h>
#include <android/hardware/hal/1.0/IHalClientCallback.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

namespace android::hardware::hal::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 Hal : public V1_0::IHal {
    // Methods from ::android::hardware::hal::V1_0::IHal follow.
    Hal(void);
    Return<::android::hardware::hal::V1_0::HalStatus> setHalClientCallback(const sp<::android::hardware::hal::V1_0::IHalClientCallback>& clientCallback) override;


private:
    static sp<V1_0::IHalClientCallback> mHalClientCallback;
    static Return<void> mLoop(void);
    // Methods from ::android::hidl::base::V1_0::IBase follow.

};

// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHal* HIDL_FETCH_IHal(const char* name);

}  // namespace android::hardware::hal::implementation
EOF

 hardware/interfaces/hal/1.0/default/Hal.cpp

cat >  hardware/interfaces/hal/1.0/default/Hal.cpp << EOF        

// FIXME: your file license if you have one

#include "Hal.h"
#include <iostream>
#include <thread>


namespace android::hardware::hal::implementation {
using namespace std;

sp<V1_0::IHalClientCallback> Hal::mHalClientCallback = nullptr;
// Methods from ::android::hardware::hal::V1_0::IHal follow.
Return<::android::hardware::hal::V1_0::HalStatus> Hal::setHalClientCallback(const sp<::android::hardware::hal::V1_0::IHalClientCallback>& clientCallback) {
    // TODO implement
    mHalClientCallback = clientCallback;
    return ::android::hardware::hal::V1_0::HalStatus::OK;
}

Return<void> Hal::mLoop(void)
{
    uint32_t counter = 0;

    while(1)
    {
        counter++;
        if(mHalClientCallback == nullptr) {
            //std::cout<<__func__<<":mHalClientCallback null"<<std::endl;
        } else {
            mHalClientCallback->getCounter(counter);
        }
        sleep(1);
    }
}

Hal::Hal(void)
{
   thread *tid = new thread(mLoop);
   (void)tid;
   std::cout<<__func__<<"init done\n"<<std::endl;
}

// Methods from ::android::hidl::base::V1_0::IBase follow.

//IHal* HIDL_FETCH_IHal(const char* /* name */) {
    //return new Hal();
//}
//
}  // namespace android::hardware::hal::implementation
EOF

 hardware/interfaces/hal/1.0/default/HalClientCallback.h

cat >  hardware/interfaces/hal/1.0/default/HalClientCallback.h << EOF

// FIXME: your file license if you have one

#pragma once

#include <android/hardware/hal/1.0/IHalClientCallback.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

namespace android::hardware::hal::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 HalClientCallback : public V1_0::IHalClientCallback {
    // Methods from ::android::hardware::hal::V1_0::IHalClientCallback follow.
    Return<::android::hardware::hal::V1_0::HalStatus> getCounter(uint32_t counter) override;

    // Methods from ::android::hidl::base::V1_0::IBase follow.

};

// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHalClientCallback* HIDL_FETCH_IHalClientCallback(const char* name);

}  // namespace android::hardware::hal::implementation
EOF

  hardware/interfaces/hal/1.0/default/HalClientCallback.cpp

cat >   hardware/interfaces/hal/1.0/default/HalClientCallback.cpp << EOF

// FIXME: your file license if you have one

#pragma once

#include <android/hardware/hal/1.0/IHalClientCallback.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

namespace android::hardware::hal::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 HalClientCallback : public V1_0::IHalClientCallback {
    // Methods from ::android::hardware::hal::V1_0::IHalClientCallback follow.
    Return<::android::hardware::hal::V1_0::HalStatus> getCounter(uint32_t counter) override;

    // Methods from ::android::hidl::base::V1_0::IBase follow.

};

// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHalClientCallback* HIDL_FETCH_IHalClientCallback(const char* name);

}  // namespace android::hardware::hal::implementation
EOF

 hardware/interfaces/hal/1.0/default/service.cpp

cat >  hardware/interfaces/hal/1.0/default/service.cpp << EOF

# define LOG_TAG "android.hardware.hal@1.0-service"
#include <hidl/HidlTransportSupport.h>
#include "Hal.h"
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardware::hal::V1_0::IHal;
using android::hardware::hal::implementation::Hal;
using android::sp;
using android::OK;

int main() {
    configureRpcThreadpool(4, true);
    sp<IHal> hal = new Hal();

    if(hal->registerAsService() != OK) {
        printf("%s register ng\n", LOG_TAG);
    } else {
        printf("%s register ok\n", LOG_TAG);
        joinRpcThreadpool();
    }
    return 0;
}
EOF

 hardware/interfaces/hal/1.0/default/test.cpp

cat >  hardware/interfaces/hal/1.0/default/test.cpp << EOF

#define LOG_TAG "android.hardware.hello@1.0-test"
#include <hidl/LegacySupport.h>
#include <hidl/HidlSupport.h>
#include "Hal.h"
#include "HalClientCallback.h"

using ::android::hardware::hal::V1_0::IHal;
using ::android::hardware::hal::V1_0::IHalClientCallback;
using ::android::hardware::hal::implementation::HalClientCallback;
using ::android::sp;
using ::android::hardware::Return;
using ::android::hardware::Void;

int main()
{
    int ret = 0;

    sp<IHal> service = IHal::getService();
    if(service == nullptr) {

    } else {

    }
    sp<IHalClientCallback> cb = new HalClientCallback();
    service->setHalClientCallback(cb);

    while(1) {
        sleep(1);
    };
    return ret;
}
 

 hardware/interfaces/hal/1.0/default/Android.bp

cat >  hardware/interfaces/hal/1.0/default/Android.bp << EOF

cc_binary {
    name: "android.hardware.hal@1.0-service",
    init_rc: ["android.hardware.hal@1.0-service.rc"],
    vintf_fragments: ["hal_manifest.xml"],
    relative_install_path: "hw",
    vendor: true,

    srcs: [
        "Hal.cpp",
        "service.cpp",
    ],

    cflags: [
        "-Wall",
        "-Werror",
        "-Wno-error",
    ],

    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "android.hardware.hal@1.0",
    ],
}

cc_binary {
    name: "android.hardware.hal@1.0-test",
    vendor: true,
    srcs: [
        "HalClientCallback.cpp",
        "test.cpp",
    ],

    cflags: [
        "-Wall",
        "-Werror",
        "-Wno-error",
    ],

    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "android.hardware.hal@1.0",
    ],
}
EOF

hardware/interfaces/hal/1.0/default/hal_manifest.xml 

<manifest version="1.0" type="device">
    <hal format="hidl">
        <name>android.hardware.hal</name>
        <transport>hwbinder</transport>
        <version>1.0</version>
        <interface>
            <name>IHal</name>
            <instance>default</instance>
        </interface>
    </hal>
</manifest>
 

编译

mmm hardware/interfaces/hal/1.0/default/ 

 拷贝生成的文件

rm -rf o
mkdir o
cp --parents out/target/product/msmnile_gvmq/vendor/bin/android.hardware.hal@1.0-test o
cp --parents out/target/product/msmnile_gvmq/vendor/bin/hw/android.hardware.hal@1.0-service o
cp --parents out/target/product/msmnile_gvmq/system/lib/android.hardware.hal@1.0.so o
cp --parents out/target/product/msmnile_gvmq/system/lib64/android.hardware.hal@1.0.so o
 

 push文件到板子

adb push out/target/product/msmnile_gvmq/vendor/bin/android.hardware.hal@1.0-test       /vendor/bin
adb push out/target/product/msmnile_gvmq/vendor/bin/hw/android.hardware.hal@1.0-service /vendor/bin/hw/
adb push out/target/product/msmnile_gvmq/system/lib/android.hardware.hal        /vendor/lib
adb push out/target/product/msmnile_gvmq/system/lib64/android.hardware.hal@1.0.so       /vendor/lib64
adb push out/target/product/msmnile_gvmq/system/lib/android.hardware.hal        /system/lib
adb push out/target/product/msmnile_gvmq/system/lib64/android.hardware.hal@1.0.so       /system/lib64
 

 板子添加到/vendor/etc/vintf/manifest.xml

     <hal format="hidl">
        <name>android.hardware.hal</name>
        <transport>hwbinder</transport>
        <version>1.0</version>
        <interface>
            <name>IHal</name>
            <instance>default</instance>
        </interface>
        <fqname>@1.0::IHal/default</fqname>
    </hal>

重启板子,运行service

msmnile_gvmq:/ # /vendor/bin/hw/android.hardware.hal@1.0-service &
[1] 6940
msmnile_gvmq:/ # Halinit done

运行测试程序

msmnile_gvmq:/ # /vendor/bin/android.hardware.hal@1.0-test
counter is 45
counter is 46
counter is 47
counter is 48
counter is 49
counter is 50
counter is 51
^C

修改30.txt current.txt

diff --git a/target/product/gsi/30.txt b/target/product/gsi/30.txt
index 0589517711..24c49d8cbe 100644
--- a/target/product/gsi/30.txt
+++ b/target/product/gsi/30.txt
@@ -146,6 +146,7 @@ VNDK-core: android.hardware.graphics.composer@2.1.so
 VNDK-core: android.hardware.graphics.composer@2.2.so
 VNDK-core: android.hardware.graphics.composer@2.3.so
 VNDK-core: android.hardware.graphics.composer@2.4.so
+VNDK-core: android.hardware.hal@1.0.so
 VNDK-core: android.hardware.health.storage@1.0.so
 VNDK-core: android.hardware.health@1.0.so
 VNDK-core: android.hardware.health@2.0.so
diff --git a/target/product/gsi/current.txt b/target/product/gsi/current.txt
index 0589517711..24c49d8cbe 100644
--- a/target/product/gsi/current.txt
+++ b/target/product/gsi/current.txt
@@ -146,6 +146,7 @@ VNDK-core: android.hardware.graphics.composer@2.1.so
 VNDK-core: android.hardware.graphics.composer@2.2.so
 VNDK-core: android.hardware.graphics.composer@2.3.so
 VNDK-core: android.hardware.graphics.composer@2.4.so
+VNDK-core: android.hardware.hal@1.0.so
 VNDK-core: android.hardware.health.storage@1.0.so
 VNDK-core: android.hardware.health@1.0.so
 VNDK-core: android.hardware.health@2.0.so
 

删除vndk.libraries.txt 

rm out/soong/vndk/vndk.libraries.txt 

整包编译

make -j 64







创建apk调用Hal接口


新建apk工程

参考:Android 11 Android Studio APK响应按钮事件_蓝牙先生的博客-CSDN博客

导入framework.jar和classes.jar库到apk工程

out/target/common/obj/JAVA_LIBRARIES/android.hardware.hal-V1.0-java_intermediates/classes.jar

out/soong/.intermediates/frameworks/base/framework/android_common/turbine-combined/framework.jar

参考:Android Studio 导入libs库文件_蓝牙先生的博客-CSDN博客

修改apk工程文件build.gradle

    implementation files('libs/classes.jar')
    compileOnly files('libs/framework.jar')

修改layout,添加一个TextView控件,id命名为counter

修改MainActivity.java保存counter,设置Hal回调

package com.example.hal;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.hal.V1_0.IHal;
import android.hardware.hal.V1_0.IHalClientCallback;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    final String TAG = "com.example.hal";
    IHal hal = null;
    public static TextView tv = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.counter);
        try {
            hal = IHal.getService(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(hal == null) {
            Log.d(TAG, "IHal.getService fail");
        } else {
            IHalClientCallback cb = new HalClientCallback();
            try {
                hal.setHalClientCallback(cb);
            } catch (Exception e) {
                Log.d(TAG, "hal.setHalClientCallback fail");
            }
        }
    }
}

实现创建HalClientCallback.java class文件实现IHalClientCallback接口,并把hal的回调counter值设置到TextView counter上

package com.example.hal;

import static com.example.hal.MainActivity.tv;

import android.hardware.hal.V1_0.HalStatus;
import android.hardware.hal.V1_0.IHalClientCallback;
import android.os.RemoteException;
import android.util.Log;

import javax.net.ssl.SSLEngineResult;

public class HalClientCallback extends IHalClientCallback.Stub {
    final String TAG = "com.example.hal";
    @Override
    public int getCounter(int var1) {
        Log.d(TAG, "counter is " + var1);
        tv.setText("counter is " + var1);
        return HalStatus.OK;
    }
}

 临时关闭Selinux

setenforce 0

编译运行,看到counter一直在变化 

 

<完> 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值