1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_UTILS_NDK_BINDER_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_UTILS_NDK_BINDER_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
21 
22 #include <assert.h>
23 #include <jni.h>
24 
25 #include <memory>
26 
27 // This file defines NdkBinder functions, variables, and types in
28 // grpc_binder::ndk_util namespace. This allows us to dynamically load
29 // libbinder_ndk at runtime, and make it possible to compile the code without
30 // the library present at compile time.
31 
32 // TODO(mingcl): Consider if we want to check API level and include NDK headers
33 // normally if the level is high enough
34 
35 namespace grpc_binder {
36 namespace ndk_util {
37 
38 struct AIBinder;
39 struct AParcel;
40 struct AIBinder_Class;
41 
42 // Only enum values used by the project is defined here
43 enum {
44   FLAG_ONEWAY = 0x01,
45 };
46 enum {
47   STATUS_OK = 0,
48   STATUS_UNKNOWN_ERROR = (-2147483647 - 1),
49 };
50 
51 typedef int32_t binder_status_t;
52 typedef uint32_t binder_flags_t;
53 typedef uint32_t transaction_code_t;
54 
55 typedef bool (*AParcel_byteArrayAllocator)(void* arrayData, int32_t length,
56                                            int8_t** outBuffer);
57 typedef bool (*AParcel_stringAllocator)(void* stringData, int32_t length,
58                                         char** buffer);
59 typedef void* (*AIBinder_Class_onCreate)(void* args);
60 typedef void (*AIBinder_Class_onDestroy)(void* userData);
61 typedef binder_status_t (*AIBinder_Class_onTransact)(AIBinder* binder,
62                                                      transaction_code_t code,
63                                                      const AParcel* in,
64                                                      AParcel* out);
65 
66 void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz);
67 void* AIBinder_getUserData(AIBinder* binder);
68 uid_t AIBinder_getCallingUid();
69 AIBinder* AIBinder_fromJavaBinder(JNIEnv* env, jobject binder);
70 AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
71                                       AIBinder_Class_onCreate onCreate,
72                                       AIBinder_Class_onDestroy onDestroy,
73                                       AIBinder_Class_onTransact onTransact);
74 AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args);
75 bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz);
76 void AIBinder_incStrong(AIBinder* binder);
77 void AIBinder_decStrong(AIBinder* binder);
78 binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code,
79                                   AParcel** in, AParcel** out,
80                                   binder_flags_t flags);
81 binder_status_t AParcel_readByteArray(const AParcel* parcel, void* arrayData,
82                                       AParcel_byteArrayAllocator allocator);
83 void AParcel_delete(AParcel* parcel);
84 int32_t AParcel_getDataSize(const AParcel* parcel);
85 binder_status_t AParcel_writeInt32(AParcel* parcel, int32_t value);
86 binder_status_t AParcel_writeInt64(AParcel* parcel, int64_t value);
87 binder_status_t AParcel_writeStrongBinder(AParcel* parcel, AIBinder* binder);
88 binder_status_t AParcel_writeString(AParcel* parcel, const char* string,
89                                     int32_t length);
90 binder_status_t AParcel_readInt32(const AParcel* parcel, int32_t* value);
91 binder_status_t AParcel_readInt64(const AParcel* parcel, int64_t* value);
92 binder_status_t AParcel_readString(const AParcel* parcel, void* stringData,
93                                    AParcel_stringAllocator allocator);
94 binder_status_t AParcel_readStrongBinder(const AParcel* parcel,
95                                          AIBinder** binder);
96 binder_status_t AParcel_writeByteArray(AParcel* parcel, const int8_t* arrayData,
97                                        int32_t length);
98 binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in);
99 jobject AIBinder_toJavaBinder(JNIEnv* env, AIBinder* binder);
100 
101 }  // namespace ndk_util
102 
103 }  // namespace grpc_binder
104 
105 #endif  // GPR_SUPPORT_BINDER_TRANSPORT
106 
107 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_UTILS_NDK_BINDER_H
108