xref: /aosp_15_r20/frameworks/native/libs/binder/RpcWireFormat.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker #pragma once
17*38e8c45fSAndroid Build Coastguard Worker 
18*38e8c45fSAndroid Build Coastguard Worker namespace android {
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
21*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic error "-Wpadded"
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker constexpr uint8_t RPC_CONNECTION_OPTION_INCOMING = 0x1; // default is outgoing
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker constexpr uint32_t RPC_WIRE_ADDRESS_OPTION_CREATED = 1 << 0; // distinguish from '0' address
26*38e8c45fSAndroid Build Coastguard Worker constexpr uint32_t RPC_WIRE_ADDRESS_OPTION_FOR_SERVER = 1 << 1;
27*38e8c45fSAndroid Build Coastguard Worker 
28*38e8c45fSAndroid Build Coastguard Worker struct RpcWireAddress {
29*38e8c45fSAndroid Build Coastguard Worker     uint32_t options;
30*38e8c45fSAndroid Build Coastguard Worker     uint32_t address;
31*38e8c45fSAndroid Build Coastguard Worker 
fromRawRpcWireAddress32*38e8c45fSAndroid Build Coastguard Worker     static inline RpcWireAddress fromRaw(uint64_t raw) {
33*38e8c45fSAndroid Build Coastguard Worker         return *reinterpret_cast<RpcWireAddress*>(&raw);
34*38e8c45fSAndroid Build Coastguard Worker     }
toRawRpcWireAddress35*38e8c45fSAndroid Build Coastguard Worker     static inline uint64_t toRaw(RpcWireAddress addr) {
36*38e8c45fSAndroid Build Coastguard Worker         return *reinterpret_cast<uint64_t*>(&addr);
37*38e8c45fSAndroid Build Coastguard Worker     }
38*38e8c45fSAndroid Build Coastguard Worker };
39*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcWireAddress) == sizeof(uint64_t));
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker /**
42*38e8c45fSAndroid Build Coastguard Worker  * This is sent to an RpcServer in order to request a new connection is created,
43*38e8c45fSAndroid Build Coastguard Worker  * either as part of a new session or an existing session
44*38e8c45fSAndroid Build Coastguard Worker  */
45*38e8c45fSAndroid Build Coastguard Worker struct RpcConnectionHeader {
46*38e8c45fSAndroid Build Coastguard Worker     uint32_t version; // maximum supported by caller
47*38e8c45fSAndroid Build Coastguard Worker     uint8_t options;
48*38e8c45fSAndroid Build Coastguard Worker     uint8_t fileDescriptorTransportMode;
49*38e8c45fSAndroid Build Coastguard Worker     uint8_t reservered[8];
50*38e8c45fSAndroid Build Coastguard Worker     // Follows is sessionIdSize bytes.
51*38e8c45fSAndroid Build Coastguard Worker     // if size is 0, this is requesting a new session.
52*38e8c45fSAndroid Build Coastguard Worker     uint16_t sessionIdSize;
53*38e8c45fSAndroid Build Coastguard Worker };
54*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcConnectionHeader) == 16);
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker /**
57*38e8c45fSAndroid Build Coastguard Worker  * In response to an RpcConnectionHeader which corresponds to a new session,
58*38e8c45fSAndroid Build Coastguard Worker  * this returns information to the server.
59*38e8c45fSAndroid Build Coastguard Worker  */
60*38e8c45fSAndroid Build Coastguard Worker struct RpcNewSessionResponse {
61*38e8c45fSAndroid Build Coastguard Worker     uint32_t version; // maximum supported by callee <= maximum supported by caller
62*38e8c45fSAndroid Build Coastguard Worker     uint8_t reserved[4];
63*38e8c45fSAndroid Build Coastguard Worker };
64*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcNewSessionResponse) == 8);
65*38e8c45fSAndroid Build Coastguard Worker 
66*38e8c45fSAndroid Build Coastguard Worker #define RPC_CONNECTION_INIT_OKAY "cci"
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker /**
69*38e8c45fSAndroid Build Coastguard Worker  * Whenever a client connection is setup, this is sent as the initial
70*38e8c45fSAndroid Build Coastguard Worker  * transaction. The main use of this is in order to control the timing for when
71*38e8c45fSAndroid Build Coastguard Worker  * an incoming connection is setup.
72*38e8c45fSAndroid Build Coastguard Worker  */
73*38e8c45fSAndroid Build Coastguard Worker struct RpcOutgoingConnectionInit {
74*38e8c45fSAndroid Build Coastguard Worker     char msg[4];
75*38e8c45fSAndroid Build Coastguard Worker     uint8_t reserved[4];
76*38e8c45fSAndroid Build Coastguard Worker };
77*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcOutgoingConnectionInit) == 8);
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker enum : uint32_t {
80*38e8c45fSAndroid Build Coastguard Worker     /**
81*38e8c45fSAndroid Build Coastguard Worker      * follows is RpcWireTransaction, if flags != oneway, reply w/ RPC_COMMAND_REPLY expected
82*38e8c45fSAndroid Build Coastguard Worker      */
83*38e8c45fSAndroid Build Coastguard Worker     RPC_COMMAND_TRANSACT = 0,
84*38e8c45fSAndroid Build Coastguard Worker     /**
85*38e8c45fSAndroid Build Coastguard Worker      * follows is RpcWireReply
86*38e8c45fSAndroid Build Coastguard Worker      */
87*38e8c45fSAndroid Build Coastguard Worker     RPC_COMMAND_REPLY,
88*38e8c45fSAndroid Build Coastguard Worker     /**
89*38e8c45fSAndroid Build Coastguard Worker      * follows is RpcDecStrong
90*38e8c45fSAndroid Build Coastguard Worker      *
91*38e8c45fSAndroid Build Coastguard Worker      * note - this in the protocol directly instead of as a 'special
92*38e8c45fSAndroid Build Coastguard Worker      * transaction' in order to keep it as lightweight as possible (we don't
93*38e8c45fSAndroid Build Coastguard Worker      * want to create a 'Parcel' object for every decref)
94*38e8c45fSAndroid Build Coastguard Worker      */
95*38e8c45fSAndroid Build Coastguard Worker     RPC_COMMAND_DEC_STRONG,
96*38e8c45fSAndroid Build Coastguard Worker };
97*38e8c45fSAndroid Build Coastguard Worker 
98*38e8c45fSAndroid Build Coastguard Worker /**
99*38e8c45fSAndroid Build Coastguard Worker  * These commands are used when the address in an RpcWireTransaction is zero'd
100*38e8c45fSAndroid Build Coastguard Worker  * out (no address). This allows the transact/reply flow to be used for
101*38e8c45fSAndroid Build Coastguard Worker  * additional server commands, without making the protocol for
102*38e8c45fSAndroid Build Coastguard Worker  * transactions/replies more complicated.
103*38e8c45fSAndroid Build Coastguard Worker  */
104*38e8c45fSAndroid Build Coastguard Worker enum : uint32_t {
105*38e8c45fSAndroid Build Coastguard Worker     RPC_SPECIAL_TRANSACT_GET_ROOT = 0,
106*38e8c45fSAndroid Build Coastguard Worker     RPC_SPECIAL_TRANSACT_GET_MAX_THREADS = 1,
107*38e8c45fSAndroid Build Coastguard Worker     RPC_SPECIAL_TRANSACT_GET_SESSION_ID = 2,
108*38e8c45fSAndroid Build Coastguard Worker };
109*38e8c45fSAndroid Build Coastguard Worker 
110*38e8c45fSAndroid Build Coastguard Worker // serialization is like:
111*38e8c45fSAndroid Build Coastguard Worker // |RpcWireHeader|struct desginated by 'command'| (over and over again)
112*38e8c45fSAndroid Build Coastguard Worker //
113*38e8c45fSAndroid Build Coastguard Worker // When file descriptors are included in out-of-band data (e.g. in unix domain
114*38e8c45fSAndroid Build Coastguard Worker // sockets), they are always paired with the RpcWireHeader bytes of the
115*38e8c45fSAndroid Build Coastguard Worker // transaction or reply the file descriptors belong to.
116*38e8c45fSAndroid Build Coastguard Worker 
117*38e8c45fSAndroid Build Coastguard Worker struct RpcWireHeader {
118*38e8c45fSAndroid Build Coastguard Worker     uint32_t command; // RPC_COMMAND_*
119*38e8c45fSAndroid Build Coastguard Worker     uint32_t bodySize;
120*38e8c45fSAndroid Build Coastguard Worker 
121*38e8c45fSAndroid Build Coastguard Worker     uint32_t reserved[2];
122*38e8c45fSAndroid Build Coastguard Worker };
123*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcWireHeader) == 16);
124*38e8c45fSAndroid Build Coastguard Worker 
125*38e8c45fSAndroid Build Coastguard Worker struct RpcDecStrong {
126*38e8c45fSAndroid Build Coastguard Worker     RpcWireAddress address;
127*38e8c45fSAndroid Build Coastguard Worker     uint32_t amount;
128*38e8c45fSAndroid Build Coastguard Worker     uint32_t reserved;
129*38e8c45fSAndroid Build Coastguard Worker };
130*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcDecStrong) == 16);
131*38e8c45fSAndroid Build Coastguard Worker 
132*38e8c45fSAndroid Build Coastguard Worker struct RpcWireTransaction {
133*38e8c45fSAndroid Build Coastguard Worker     RpcWireAddress address;
134*38e8c45fSAndroid Build Coastguard Worker     uint32_t code;
135*38e8c45fSAndroid Build Coastguard Worker     uint32_t flags;
136*38e8c45fSAndroid Build Coastguard Worker 
137*38e8c45fSAndroid Build Coastguard Worker     uint64_t asyncNumber;
138*38e8c45fSAndroid Build Coastguard Worker 
139*38e8c45fSAndroid Build Coastguard Worker     // The size of the Parcel data directly following RpcWireTransaction.
140*38e8c45fSAndroid Build Coastguard Worker     uint32_t parcelDataSize;
141*38e8c45fSAndroid Build Coastguard Worker 
142*38e8c45fSAndroid Build Coastguard Worker     uint32_t reserved[3];
143*38e8c45fSAndroid Build Coastguard Worker 
144*38e8c45fSAndroid Build Coastguard Worker     uint8_t data[];
145*38e8c45fSAndroid Build Coastguard Worker };
146*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcWireTransaction) == 40);
147*38e8c45fSAndroid Build Coastguard Worker 
148*38e8c45fSAndroid Build Coastguard Worker struct RpcWireReply {
149*38e8c45fSAndroid Build Coastguard Worker     int32_t status; // transact return
150*38e8c45fSAndroid Build Coastguard Worker 
151*38e8c45fSAndroid Build Coastguard Worker     // -- Fields below only transmitted starting at protocol version 1 --
152*38e8c45fSAndroid Build Coastguard Worker 
153*38e8c45fSAndroid Build Coastguard Worker     // The size of the Parcel data directly following RpcWireReply.
154*38e8c45fSAndroid Build Coastguard Worker     uint32_t parcelDataSize;
155*38e8c45fSAndroid Build Coastguard Worker 
156*38e8c45fSAndroid Build Coastguard Worker     uint32_t reserved[3];
157*38e8c45fSAndroid Build Coastguard Worker 
158*38e8c45fSAndroid Build Coastguard Worker     // Byte size of RpcWireReply in the wire protocol.
wireSizeRpcWireReply159*38e8c45fSAndroid Build Coastguard Worker     static size_t wireSize(uint32_t protocolVersion) {
160*38e8c45fSAndroid Build Coastguard Worker         if (protocolVersion == 0) {
161*38e8c45fSAndroid Build Coastguard Worker             return sizeof(int32_t);
162*38e8c45fSAndroid Build Coastguard Worker         }
163*38e8c45fSAndroid Build Coastguard Worker         return sizeof(RpcWireReply);
164*38e8c45fSAndroid Build Coastguard Worker     }
165*38e8c45fSAndroid Build Coastguard Worker };
166*38e8c45fSAndroid Build Coastguard Worker static_assert(sizeof(RpcWireReply) == 20);
167*38e8c45fSAndroid Build Coastguard Worker 
168*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop
169*38e8c45fSAndroid Build Coastguard Worker 
170*38e8c45fSAndroid Build Coastguard Worker } // namespace android
171