转自https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md。

Background

“aidl” refers to several related but distinct concepts:

  • the AIDL interface definition language
  • .aidl files (which contain AIDL)
  • the aidl generator which transforms AIDL into client/server IPC interfaces

The aidl generator is a command line tool that generates client and server stubs for Binder interfaces from a specification in a file with the .aidl extension. For Java interfaces, the executable is called aidl while for C++ the binary is called aidl-cpp. In this document, we’ll use AIDL to describe the language of .aidl files and aidl generator to refer to the code generation tool that takes an .aidl file, parses the AIDL, and outputs code.

Previously, the aidl generator only generated Java interface/stub/proxy objects. C++ Binder interfaces were handcrafted with various degrees of compatibility with the Java equivalents. The Brillo project added support for generating C++ with the aidl generator. This generated C++ is cross-language compatible (e.g. Java clients are tested to interoperate with native services).

Overview

This document describes how C++ generation works with attention to:

  • build interface
  • cross-language type mapping
  • implementing a generated interface
  • C++ parcelables
  • cross-language error reporting
  • cross-language null reference handling
  • cross-language integer constants

Detailed Design

Build Interface

Write AIDL in .aidl files and add them to LOCAL_SRC_FILES in your Android.mk. If your build target is a binary (e.g. you include $(BUILD_SHARED_LIBRARY)), then the generated code will be C++, not Java.

AIDL definitions should be hosted from the same repository as the implementation. Any system that needs the definition will also need the implementation (for both parcelables and interface). If there are multiple implementations (i.e. one in Java and one in C++), keep the definition with the native implementation. Android now has systems that run the native components of the system without the Java.
If you use an import statement in your AIDL, even from the same package, you need to add a path to LOCAL_AIDL_INCLUDES. This path should be relative to the root of the Android tree. For instance, a file IFoo.aidl defining com.example.IFoo might sit in a folder hierarchy something/something-else/com/example/IFoo.aidl. Then we would write:

LOCAL_AIDL_INCLUDES := something/something-else

Generated C++ ends up in nested namespaces corresponding to the interface’s package. The generated header also corresponds to the interface package. So com.example.IFoo becomes ::com::example::IFoo in header “com/example/IFoo.h”.
Similar to how Java works, the suffix of the path to a .aidl file must match the package. So if IFoo.aidl declares itself to be in package com.example, the folder structure (as given to LOCAL_SRC_FILES
) must look like: some/prefix/com/example/IFoo.aidl
.
To generate code from .aidl files from another build target (e.g. another binary or java), just add a relative path to the .aidl files to LOCAL_SRC_FILES. Remember that importing AIDL works the same, even for code in other directory hierarchies: add the include root path relative to the checkout root to LOCAL_AIDL_INCLUDES.

Type Mapping

The following table summarizes the equivalent C++ types for common Java types and whether those types may be used as in/out/inout parameters in AIDL interfaces.

Java Type C++ Type inout Notes
boolean bool in "These 8 types are all considered primitives.
byte int8_t in  
char char16_t in  
int int32_t in  
long int64_t in  
float float in  
double double in  
String String16 in Supports null references.
android.os.Parcelable android::Parcelable inout  
T extends IBinder sp in  
Arrays (T[]) vector inout May contain only primitives, Strings and parcelables.
List vector inout  
PersistableBundle PersistableBundle inout binder/PersistableBundle.h
List vector<sp> inout  
FileDescriptor ScopedFd inout nativehelper/ScopedFd.h

Note that java.util.Map and java.utils.List are not good candidates for cross language communication because they may contain arbitrary types on the Java side. For instance, Map is cast to Map<String,Object> and then the object values dynamically inspected and serialized as type/value pairs. Support exists for sending arbitrary Java serializables, Android Bundles, etc.

Implementing a generated interface

Given an interface declaration like:

package foo;
import bar.IAnotherInterface;
interface IFoo { IAnotherInterface DoSomething(int count, out List<String> output); } 

aidl-cpp will generate a C++ interface:

namespace foo {// Some headers have been omitted for clarity.
#include <android/String16.h> #include <cstdint> #include <vector> #include <bar/IAnotherInterface.h> // Some class members have been omitted for clarity. class IFoo : public android::IInterface { public: virtual android::binder::Status DoSomething( int32_t count, std::vector<android::String16>* output, android::sp<bar::IAnotherInterface>* returned_value) = 0; }; 

Note that aidl-cpp
will import headers for types used in the interface. For imported types (e.g. parcelables and interfaces), it will import a header corresponding to the package/class name of the import. For instance, import bar.IAnotherInterface
causes aidl-cpp to generate #include <bar/IAnotherInterface.h>
.
When writing a service that implements this interface, write:

#include "foo/BnFoo.h"namespace unrelated_namespace { class MyFoo : public foo::BnFoo { public: android::binder::Status DoSomething( int32_t count, std::vector<android::String16>* output, android::sp<bar::IAnotherInterface>* returned_value) override { for (int32_t i = 0; i < count; ++i) { output->push_back(String16("...")); } *returned_value = new InstanceOfAnotherInterface; return Status::ok(); } }; // class MyFoo } // namespace unrelated_namespace 

Note that the output values, output and returned_value are passed by pointer, and that this pointer is always valid.

C++ Parcelables

In Java, a parcelable should extend android.os.Parcelable and provide a static final CREATOR field that acts as a factory for new instances/arrays of instances of the parcelable. In addition, in order to be used as an out parameter, a parcelable class must define a readFromParcel method.
In C++, parcelables must implement android::Parcelable from binder/Parcelable.h in libbinder. Parcelables must define a constructor that takes no arguments. In order to be used in arrays, a parcelable must implement a copy or move constructor (called implicitly in vector).
The C++ generator needs to know what header defines the C++ parcelable. It learns this from the cpp_header
directive shown below. The generator takes this string and uses it as the literal include statement in generated code. The idea here is that you generate your code once, link it into a library along with parcelable implementations, and export appropriate header paths. This header include must make sense in the context of the Android.mk that compiles this generated code.
// ExampleParcelable.aidlpackage com.example.android;// Native types must be aliased at their declaration in the appropriate .aidl// file. This allows multiple interfaces to use a parcelable and its C++// equivalent without duplicating the mapping between the C++ and Java types.// Generator will assume bar/foo.h declares class// com::example::android::ExampleParcelableparcelable ExampleParcelable cpp_header "bar/foo.h";

Null Reference Handling

The aidl generator for both C++ and Java languages has been expanded to understand nullable annotations.
Given an interface definition like:

interface IExample { void ReadStrings(String neverNull, in @nullable String maybeNull); }; 

the generated C++ header code looks like:

class IExample { android::binder::Status ReadStrings( const android::String16& in_neverNull, const std::unique_ptr<android::String16>& in_maybeNull); }; 

Note that by default, the generated C++ passes a const reference to the value of a parameter and rejects null references with a NullPointerException sent back the caller. Parameters marked with @nullable are passed by pointer, allowing native services to explicitly control whether they allow method overloading via null parameters. Java stubs and proxies currently do nothing with the @nullable annotation.
Consider an AIDL type in @nullable List<String> bar. This type indicates that the remote caller may pass in a list of strings, and that both the list and any string in the list may be null. This type will map to a C++ type unique_ptr<vector<unique_ptr<String16>>>* bar
. In this case:

  • bar is never null
  • *bar might be null
  • (*bar)->empty() could be true
  • (**bar)[0] could be null (and so on)

Exception Reporting

C++ methods generated by the aidl generator return android::binder::Status objects, rather than android::status_t. This Status object allows generated C++ code to send and receive exceptions (an exception type and a String16 error message) since we do not use real exceptions in C++. More background on Status objects can be found here.

For legacy support and migration ease, the Status object includes a mechanism to report a android::status_t. However, that return code is interpreted by a different code path and does not include a helpful String message.

For situations where your native service needs to throw an error code specific to the service, use Status::fromServiceSpecificError(). This kind of exception comes with a helpful message and an integer error code. Make your error codes consistent across services by using interface constants (see below).

Integer Constants

AIDL has been enhanced to support defining integer constants as part of an interface:

 interface IMyInterface {const int CONST_A = 1; const int CONST_B = 2; const int CONST_C = 3; ... } 

These map to appropriate 32 bit integer class constants in Java and C++ (e.g. IMyInterface.CONST_A
and IMyInterface::CONST_A
respectively).

转载于:https://www.cnblogs.com/zhougong/p/8930268.html

C++ 下面的AIDL相关推荐

  1. sonarqube中,分析maven聚合工程时,不必分析parent工程,只需分析下面的module子工程即可

    sonarqube中,分析maven聚合工程时,不必分析parent工程,只需分析下面的module子工程即可 cd ../../xxx-sms # mvn clean org.jacoco:jaco ...

  2. Java黑皮书课后题第8章:*8.30(代数:解答线性方程)编写一个方法,解答下面的2*2线性方程组系统

    *8.30(代数:解答线性方程)编写一个方法,解答下面的2*2线性方程组系统 题目 题目描述 代码 题目 题目描述 8.30(代数:解答线性方程)编写一个方法,解答下面的22线性方程组系统: a00 ...

  3. Java黑皮书课后题第5章:**5.35(加法)编写程序,计算下面的和:1/(1+根2) + 1/(根2+根3) + 1/(根3+根4)+……1/(根624+根625)

    @[TOC](5.35(加法)编写程序,计算下面的和:1/(1+根2) + 1/(根2+根3) + 1/(根3+根4)+--1/(根624+根625)) 题目 题目概述 5.35(加法)编写程序,计算 ...

  4. windows下面的txt在linux下面显示为乱码

    windows下面的txt正确保存操作后, 传递到linux,发现是乱码. 解决方案: linux下面,整个txt文件拖入chrome浏览器,然后把浏览器里面的内容重新保存即可

  5. Ubuntu16.04下面的vs code出现Unable to activate CppCheck analyzer

    Ubuntu16.04下面的vs code出现Unable to activate CppCheck analyzer 解决方案: synaptic管理器中安装cppcheck和cppcheck-gu ...

  6. Android之非root手机run-as命令获取debug版本apk里面的数据(shared_prefs文件,lib下面的so,数据库文件)

    1  问题 非root手机想要获取debug版本的apk里面的数据(shared_prefs文件,lib下面的so,数据库文件) 2  直接用run-as命令 adb shellrun-as pack ...

  7. 根据div 标签 查看数组@class=modulwrap 下面的/table/tbody/tr/td

    <div class="modulwrap"><div class="request_title"><span class=&qu ...

  8. java 下面的while语句退出时_java 下面的while语句退出时

    java 下面的while语句退出时 [2021-02-02 07:01:21]  简介: php中while循环语句的使用方法是:while循环语句在指定条件为true时执行代码块.while循环语 ...

  9. linux下面的j2sdk的安装和配置过程

    linux下面的j2sdk的安装和配置过程! 1.下载一个Linux Platform的JDK,建议下载RPM自解压格式的 (RPM in self-extracting file,j2sdk-1_4 ...

最新文章

  1. 干货|理解attention机制本质及self-attention
  2. 第1章、蓄势待发准备篇
  3. VS2008 快捷键大全
  4. 给定数组 求和等于固定值 算法_别人家的面试题:不可变数组快速范围求和
  5. CodeForces - 1295D Same GCDs(欧拉函数)
  6. Netty之Channel的继承关系
  7. Java和Lagom的CQRS
  8. 巧用 Img / JavaScript 采集页面数据
  9. 快准牌电脑发软件_做自媒体必备技能,视频剪辑软件排名(精品篇)
  10. Java隐含对象实验报告,JSP隐含对象response实现文件下载
  11. 计算机教育部学科评估2019,2019教育部8大学科评估公示名单,学科实力一眼较高低!...
  12. keil4内嵌汇编_keil C中嵌入汇编程序的方法
  13. Android开发笔记(六十八)工程库打包
  14. echarts x轴加箭头,ECharts X轴(xAxis)
  15. ARX帮助文档:数据库对象
  16. OpenCasCade开发环境搭建
  17. 传奇服务器+技能cd修改器,CD登陆器KEY文件修改教程+配套工具_【传奇爱好者】...
  18. 网件WNDR4300刷openwrt/LEDE固件
  19. 嗖嗖移动大厅之使用场景类
  20. 关于2010年部分节假日安排

热门文章

  1. 【Java框架】 Hibernate与Mybatis对比
  2. Hadoop集群高可用及zookeeper+kafka组件搭建
  3. 地图相关应用系统部署到现场报错原因汇总
  4. 谈谈 Tomcat 请求处理流程
  5. 动图 + 源码,演示 Java 中常用数据结构执行过程及原理
  6. 必备面试题:系统CPU飙高和GC频繁,如何排查?
  7. 干货!!!MySQL 大表优化方案(1)
  8. SaaS颠覆传统软件到底是不是个伪命题?
  9. Docker使用Link在容器之间建立连接
  10. Java注释@interface的用法【转】