1 /* 2 * Copyright (C) 2022 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 17 #ifndef CHRE_UTIL_PIGWEED_RPC_HELPER_H_ 18 #define CHRE_UTIL_PIGWEED_RPC_HELPER_H_ 19 20 #include <cstdint> 21 22 #include "chre_api/chre.h" 23 24 namespace chre { 25 26 /** The upper 16b of a channel ID are set to 1 for host clients. */ 27 constexpr uint32_t kChannelIdHostClient = 1 << 16; 28 29 /** Mask to extract the host ID / nanoapp ID from a channel ID. */ 30 constexpr uint32_t kRpcClientIdMask = 0xffff; 31 32 /** Maximum ID for a nanoapp as the value is encoded on 16b. */ 33 constexpr uint32_t kRpcNanoappMaxId = 0xffff; 34 35 /** 36 * Returns whether the endpoint matches. 37 * 38 * Only the lower 16 bits are considered. 39 * 40 * @param expectedId Expected channel ID. 41 * @param actualId Actual channel ID. 42 * @return whether the nanoapp IDs match (lower 16b). 43 */ 44 bool rpcEndpointsMatch(uint32_t expectedId, uint32_t actualId); 45 46 /** 47 * @param id Channel ID. 48 * @return whether the channel ID is a host client. 49 */ 50 bool isRpcChannelIdHostClient(uint32_t id); 51 52 /** 53 * @param id Channel ID. 54 * @return whether the channel ID is a nanoapp client. 55 */ 56 bool isRpcChannelIdNanoappClient(uint32_t id); 57 58 /** 59 * Validates that the host client sending the message matches the expected 60 * channel ID. 61 * 62 * @param msg Message received from the host client. 63 * @param channelId Channel ID extracted from the received packet. 64 * @return Whether the IDs match. 65 */ 66 bool validateHostChannelId(const chreMessageFromHostData *msg, 67 uint32_t channelId); 68 69 /** 70 * Validates that the nanoapp sending the message matches the expected 71 * channel ID. 72 * 73 * @param senderInstanceId ID of the nanoapp sending the message. 74 * @param channelId Channel ID extracted from the received packet. 75 * @return Whether the IDs match. 76 */ 77 bool validateNanoappChannelId(uint32_t nappId, uint32_t channelId); 78 79 } // namespace chre 80 81 #endif // CHRE_UTIL_PIGWEED_RPC_HELPER_H_ 82