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