Android 9 HAL 回调函数


mkdir -p hardware/interfaces/hal/1.0

cat > hardware/interfaces/hal/1.0/IHal.hal << EOF
package [email protected];

import IHalClientCallback;

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

cat > hardware/interfaces/hal/1.0/types.hal << EOF
package [email protected];

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

cat > hardware/interfaces/hal/1.0/IHalClientCallback.hal << EOF
package [email protected];

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

./hardware/interfaces/update-makefiles.sh

[email protected]
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


cat > hardware/interfaces/hal/1.0/default/Hal.cpp << EOF
#include "Hal.h"
#include
#include

namespace android {
namespace hardware {
namespace hal {
namespace V1_0 {
namespace implementation {

using namespace std;
sp 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
    HalStatus status = HalStatus::NG;

    if(mHalClientCallback == nullptr) {
        mHalClientCallback = clientCallback;
        status = HalStatus::OK;
    } else {
        status = HalStatus::NG;
    }
    return ::android::hardware::hal::V1_0::HalStatus {};
}
Return Hal::mLoop(void)
{
    uint32_t counter = 0;

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

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

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

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

EOF

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

#ifndef ANDROID_HARDWARE_HAL_V1_0_HAL_H
#define ANDROID_HARDWARE_HAL_V1_0_HAL_H

#include
#include
#include

namespace android {
namespace hardware {
namespace hal {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hal::V1_0::IHal;
using ::android::hardware::hal::V1_0::IHalClientCallback;

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;

using ::android::hardware::hal::V1_0::IHalClientCallback;

struct Hal : public IHal {
     Hal(void);
    // Methods from ::android::hardware::hal::V1_0::IHal follow.
    Return<::android::hardware::hal::V1_0::HalStatus> setHalClientCallback(const sp<::android::hardware::hal::V1_0::IHalClientCallback>& clientCallback) override;

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

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

}  // namespace implementation
}  // namespace V1_0
}  // namespace hal
}  // namespace hardware
}  // namespace android

#endif  // ANDROID_HARDWARE_HAL_V1_0_HAL_H
EOF


cat > hardware/interfaces/hal/1.0/default/HalClientCallback.cpp << EOF
#include "HalClientCallback.h"
#include

namespace android {
namespace hardware {
namespace hal {
namespace V1_0 {
namespace implementation {
using namespace std;
// Methods from ::android::hardware::hal::V1_0::IHalClientCallback follow.
Return<::android::hardware::hal::V1_0::HalStatus> HalClientCallback::getCounter(uint32_t counter) {
    // TODO implement
    std::cout<<"counter = " << counter << std::endl;
    return ::android::hardware::hal::V1_0::HalStatus::OK;
}


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

//IHalClientCallback* HIDL_FETCH_IHalClientCallback(const char* /* name */) {
    //return new HalClientCallback();
//}
//
}  // namespace implementation
}  // namespace V1_0
}  // namespace hal
}  // namespace hardware
}  // namespace android

EOF

cat > hardware/interfaces/hal/1.0/default/HalClientCallback.h << EOF
#ifndef ANDROID_HARDWARE_HAL_V1_0_HALCLIENTCALLBACK_H
#define ANDROID_HARDWARE_HAL_V1_0_HALCLIENTCALLBACK_H

#include
#include
#include

namespace android {
namespace hardware {
namespace hal {
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 HalClientCallback : public 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 implementation
}  // namespace V1_0
}  // namespace hal
}  // namespace hardware
}  // namespace android

#endif  // ANDROID_HARDWARE_HAL_V1_0_HALCLIENTCALLBACK_H

EOF

cat > hardware/interfaces/hal/1.0/default/service.cpp << EOF
# define LOG_TAG "[email protected]"
#include
#include "Hal.h"
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardware::hal::V1_0::IHal;
using android::hardware::hal::V1_0::implementation::Hal;
using android::sp;
using android::OK;

int main() {
    configureRpcThreadpool(4, true);
    sp 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

cat > hardware/interfaces/hal/1.0/default/test.cpp << EOF
#define LOG_TAG "[email protected]"
#include
#include
#include "Hal.h"
#include "HalClientCallback.h"

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

int main()
{
    int ret = 0;

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

    } else {

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

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

EOF

cat > hardware/interfaces/hal/1.0/default/Android.bp << EOF
cc_binary {
    name: "[email protected]",
    init_rc: ["[email protected]"],
    relative_install_path: "hw",
    vendor: true,

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

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

    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "[email protected]",
    ],
}

cc_binary {
    name: "[email protected]",
    vendor: true,
    srcs: [
        "HalClientCallback.cpp",
        "test.cpp",
    ],

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

    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "[email protected]",
    ],
}

EOF

cat > hardware/interfaces/hal/1.0/default/[email protected] << EOF
service hal_service /vendor/bin/hw/[email protected]
class hal
user system
group system
EOF


#<9> let service can find [email protected]
build/make/target/product/vndk/28.txt
build/make/target/product/vndk/current.txt
out/target/product/generic/obj/PACKAGING/vndk_intermediates/libs.txt
严格按字母顺序加入VNDK-core: [email protected]
三个文件一样,若编译报错,按lib.txt重新添加

#<10> let client can get services
device/generic/goldfish/manifest.xml(/vendor/manifest.xml)文件加入client才能拿到services
   
        android.hardware.hal
        hwbinder
        1.0
       
            IHal
            default
       

   

#<11>Add hal HIDL *-impl and -service libraries to product packages
build/make/target/product/emulator.mk文件加入
PRODUCT_PACKAGES += [email protected]


#<12>
mm hardware/interfaces/hal/1.0/
mmm hardware/interfaces/hal/1.0/default/
make -j 8

你可能感兴趣的:(android,HAL)