先贴最后的文件目录:
aidl/android/hardware/demo/IFoo.aidl:
package android.hardware.demo;import android.hardware.demo.IFooCallback;@VintfStability
interface IFoo {void doFoo();int doFooWithParameter(int param);void registerCallback(IFooCallback callback);}
aidl/android/hardware/demo/IFooCallback.aidl:
package android.hardware.demo;@VintfStability
interface IFooCallback {int onTest(int param);
}
aidl/Android.bp:
aidl_interface {name: "android.hardware.demo",vendor_available: true,srcs: ["android/hardware/demo/*.aidl"],stability: "vintf",owner: "demo",imports: ["android.hardware.common-V2",],backend: {cpp: {enabled: false,},java: {enabled: false,sdk_version: "module_current",},},
}
以上三个文件写完以后就可以运行mm先处理aidl文件了。会提示你运行m android.hardware.demo-update-api来自动生成(更新)aidl/aidl_api文件夹。
aidl/default目录下的文件内容:
Foo.h:
#ifndef _FOO_H_
#define _FOO_H_#include <aidl/android/hardware/demo/BnFoo.h>namespace aidl {
namespace android {
namespace hardware {
namespace demo {
class Foo: public BnFoo {public:Foo(/* args */);~Foo();::ndk::ScopedAStatus doFoo(); ::ndk::ScopedAStatus doFooWithParameter(int32_t in_param, int32_t* _aidl_return);::ndk::ScopedAStatus registerCallback(const std::shared_ptr<::aidl::android::hardware::demo::IFooCallback>& in_callback);
};
} // namespace demo
} // namespace hardware
} // namespace android
} // namespace aidl#endif
Foo.cpp:
#include <iostream>
#include "Foo.h"namespace aidl {
namespace android {
namespace hardware {
namespace demo {Foo::Foo(/* args */) {std::cout << "Foo::Foo!" << std::endl;
}Foo::~Foo() {std::cout << "Foo::~Foo!" << std::endl;
}::ndk::ScopedAStatus Foo::doFoo()
{std::cout << "doFoo!" << std::endl;return ndk::ScopedAStatus::ok();
}::ndk::ScopedAStatus Foo::doFooWithParameter(int32_t in_param, int32_t* _aidl_return)
{std::cout << "doFooWithParameter!" << std::endl;*_aidl_return = in_param + 39;return ndk::ScopedAStatus::ok();
}::ndk::ScopedAStatus Foo::registerCallback(const std::shared_ptr<::aidl::android::hardware::demo::IFooCallback>& in_callback){int i = 0;in_callback->onTest(39, &i);std::cout << "FooCallback::onTest called on Bp side, returns : " << i << std::endl;return ndk::ScopedAStatus::ok();
}}
}
}
}
service.cpp:
#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <binder/ProcessState.h>#include "Foo.h"using aidl::android::hardware::demo::Foo;int main() {// Enable vndbinder to allow vendor-to-venfor binder callandroid::ProcessState::initWithDriver("/dev/vndbinder");auto binder = ::ndk::SharedRefBase::make<Foo>();const std::string name = std::string() + Foo::descriptor + "/default";CHECK_EQ(STATUS_OK, AServiceManager_addService(binder->asBinder().get(), name.c_str()))<< "Failed to register " << name;ABinderProcess_setThreadPoolMaxThreadCount(64);ABinderProcess_startThreadPool();ABinderProcess_joinThreadPool();return EXIT_FAILURE; // should not reached
}
Android.bp:(里面可能很多库是不需要的)
cc_binary {name: "android.hardware.demo-service",vendor: true,relative_install_path: "hw",init_rc: ["android.hardware.demo-service.rc"],vintf_fragments: ["android.hardware.demo-service.xml"],srcs: ["Foo.cpp","service.cpp",],local_include_dirs: [],cflags: ["-Wall","-Werror",],cppflags: ["-std=c++17","-Wno-sign-compare","-fexceptions",],shared_libs: ["libbase","liblog","libhardware","libbinder_ndk","libbinder","libutils","libnativewindow","libui","libdrm","libdmabufheap","android.hardware.demo-V1-ndk_platform","libhidlbase","libutilscallstack","libcutils","libfmq",],static_libs: ["libaidlcommonsupport","libpolym",],
}
android.hardware.demo-service.xml:(最后要push到/vendor/etc/vintf/manifest目录下)
<manifest version="1.0" type="device"><hal format="aidl"><name>android.hardware.demo</name><version>1</version><fqname>IFoo/default</fqname></hal>
</manifest>
最后一个文件不是必须要加,也可以手动起service:
android.hardware.demo-service.rc:
service android.hardware.demo-service /vendor/bin/hw/android.hardware.demo-serviceinterface aidl android.hardware.demo.IFoo/defaultclass haluser systemgroup system
记得有一个自动生成的库要push进去:
adb push .\r12\out\target\product\xxx\vendor\lib64\android.hardware.demo-V1-ndk_platform.so /vendor/lib64/
完毕。
push进去手动运行/vendor/bin/hw/android.hardware.demo-service,这个时候用service list命令可以看到自己service了。
使用的代码就比较简单:
aidl/client目录下:
main.cpp:
#include <iostream>
#include <android/binder_manager.h>
#include <aidl/android/hardware/demo/IFoo.h>
#include <aidl/android/hardware/demo/BnFooCallback.h>using ::aidl::android::hardware::demo::IFoo;
using ::aidl::android::hardware::demo::BnFooCallback;
using ::ndk::SpAIBinder;class FooCallback : public BnFooCallback {public:::ndk::ScopedAStatus onTest(int32_t in_param, int32_t* _aidl_return) override;
};::ndk::ScopedAStatus FooCallback::onTest(int32_t in_param, int32_t* _aidl_return) {int i = in_param*2;*_aidl_return = i;std::cout << "FooCallback::onTest called on Bn side, will return : " << i << std::endl;return ndk::ScopedAStatus::ok();
}int main()
{std::shared_ptr<IFoo> mFoo;mFoo = IFoo::fromBinder(SpAIBinder(AServiceManager_waitForService("android.hardware.demo.IFoo/default")));mFoo->doFoo();int i = 0;mFoo->doFooWithParameter(-1, &i);std::cout << "Foo::doFooWithParameter called, returns : " << i << std::endl;std::shared_ptr<FooCallback> mFooCallback;mFooCallback = ndk::SharedRefBase::make<FooCallback>();mFoo->registerCallback(mFooCallback);return 0;}
Android.bp:
cc_binary {name: "demotest",vendor: true,srcs: ["main.cpp",],shared_libs: ["libbase","libutils","libbinder_ndk","liblog","libnativewindow","android.hardware.demo-V1-ndk_platform","libutilscallstack","libcutils","libfmq",],static_libs: ["libarect","libaidlcommonsupport",],
}
可能有多余的库也拷进来的,可以自己试着删掉一些没关系的。