android filter

1. Define the .hal interface file, such as:

Create a new directory hello in vendor/sprd/interface, and define the hidl interface, such as:

1 package [email protected]1.0;

2
3 interface IHello {
4
5 helloWorld(string name) generates (string result);
6
7 };

2, use hidl-gen tool to generate communication framework and business template p>

# [email protected]
# LOC=vendor/sprd/interfaces/hello/1.0/default/
# hidl-gen -o $LOC -Lc++-impl -r vendor.sprd.hardware :vendor/sprd/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
# hidl-gen -o $LOC -Landroidbp-impl -r vendor.sprd.hardware:vendor/sprd/interfaces -r android .hidl:system/libhidl/transport $PACKAGE

In this way, under default, you can see the newly generated Hello.cpp Hello.h file, which is the business logic class we will implement

 1 struct Hello: public IHello {

2 // Methods from ::vendor::sprd::hardware::hello::V1_0::IHello follow.
3 Return<void> helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) override;
4
5 // Methods from ::android::hidl::base::V1_0::IBase follow.
6
7 };
8
9 // FIXME: most likely delete, this is only for passthrough implementations
10 // extern "C" IHello* HIDL_FETCH_IHello(const char* name);

Implemented in Hello.cpp

3, To run the business logic, the Hello.cpp code above needs to run in the process, so you need to create a new one

Create a file service.cpp, [email protected],

The business class above can be Compile into so and load it into this process, or you can directly compile the code into a bin file to run. The former is taken here.

The bp file is as follows:

 1 cc_binary {

2 name: "[email protected]", 3 init_rc: ["[email protected]"], 4 relative_install_path: "hw", 5 vendor: true, 6 7 srcs: [8 "Hello.cpp", 9 "service.cpp" 10 ], 11 12 shared_libs: [13 "libcutils", 14 "liblog" , 15 "libhidlbase", 16 "libhidltransport", 17 "libhardware", 18 "libutils", 19 "[email Protected]", 20 ], 21 }< /span>

sevice.cpp:

 1 int main () {

2 ALOGD("Hello hidl main()...");
3
4 android::sp hello = new Hello();
5
6 configureRpcThreadpool(1, true /*callerWillJoin*/);
7
8 if (hello != nullptr) {
9 if (::android::OK! = hello->registerAsService()) {
10 ALOGE("Error while registering IHello service");
11 return 1;
12 }
13} else {
14 ALOGE("Can't create instance of Hello, nullptr");
15 }
16
17 joinRpcThreadpool();
18
19 return 0; // should never get here
20 }

4. So far, start the [email protected] executable file under the command line, namely The hidl service can be run

5. Test file:

 1 < span style="color: #0000ff;">int main() {

2 int ret;
3
4 android::sp service = IHello::getService();
5 if (service == nullptr) {
6 printf("Failed to get service ");
7 return -1;
8 }
9
10 service->helloWorld("Trump", [&](hidl_string result) {
11 printf("%s ", result.c_str()) ;
12 });
13
14 return 0;
15 }

1 package [email protected]1.0;

2
3 interface IHello {
4
5 helloWorld(string name) generates (string result);
6
7 };

 1  struct Hello: public IHello {

2 // Methods from ::vendor::sprd::hardware::hello::V1_0::IHello follow.
3 Return<void> helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) override;
4
5 // Methods from ::android::hidl::base::V1_0::IBase follow.
6
7 };
8
9 // FIXME: most likely delete, this is only for passthrough implementations
10 // extern "C" IHello* HIDL_FETCH_IHello(const char* name);

 1 cc_binary {

2 name: "[email protected]", 3 init_rc: ["[email protected]"], 4 relative_install_path: "hw", 5 vendor: true, 6 7 srcs: [8 "Hello.cpp", 9 "service.cpp" 10 ], 11 12 shared_libs: [13 "libcutils", 14 "liblog" , 15 "libhidlbase", 16 "libhidltransport", 17 "libhardware", 18 "libutils", 19 "[email Protected]", 20 ], 21 }< /span>

 1  int main() {

2 ALOGD("Hello hidl main()...");
3
4 android::sp hello = new Hello();
5
6 configureRpcThreadpool(1, true /*callerWillJoin*/);
7
8 if (hello != nullptr) {
9 if (::android::OK! = hello->registerAsService()) {
10 ALOGE("Error while registering IHello service");
11 return 1;
12 }
13} else {
14 ALOGE("Can't create instance of Hello, nullptr");
15 }
16
17 joinRpcThreadpool();
18
19 return 0; // should never get here
20 }

 1< /span> int main() {

2 int ret;
3
4 android::sp service = IHello::getService();
5 if (service == nullptr) {
6 printf("Failed to get service ");
7 return -1;
8 }
9
10 service->helloWorld("Trump", [&](hidl_string result) {
11 printf("%s ", result.c_str()) ;
12 });
13
14 return 0;
15 }

Leave a Comment

Your email address will not be published.