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/util/pigweed/rpc_common.h"
23 #include "chre_api/chre.h"
24 
25 namespace chre {
26 
27 /**
28  * Returns whether the endpoint matches.
29  *
30  * Only the lower 16 bits are considered.
31  *
32  * @param expectedId Expected channel ID.
33  * @param actualId Actual channel ID.
34  * @return whether the nanoapp IDs match (lower 16b).
35  */
36 bool rpcEndpointsMatch(uint32_t expectedId, uint32_t actualId);
37 
38 /**
39  * @param id Channel ID.
40  * @return whether the channel ID is a host client.
41  */
42 bool isRpcChannelIdHostClient(uint32_t id);
43 
44 /**
45  * @param id Channel ID.
46  * @return whether the channel ID is a nanoapp client.
47  */
48 bool isRpcChannelIdNanoappClient(uint32_t id);
49 
50 /**
51  * Validates that the host client sending the message matches the expected
52  * channel ID.
53  *
54  * @param msg Message received from the host client.
55  * @param channelId Channel ID extracted from the received packet.
56  * @return Whether the IDs match.
57  */
58 bool validateHostChannelId(const chreMessageFromHostData *msg,
59                            uint32_t channelId);
60 
61 /**
62  * Validates that the nanoapp sending the message matches the expected
63  * channel ID.
64  *
65  * @param senderInstanceId ID of the nanoapp sending the message.
66  * @param channelId Channel ID extracted from the received packet.
67  * @return Whether the IDs match.
68  */
69 bool validateNanoappChannelId(uint32_t nappId, uint32_t channelId);
70 
71 }  // namespace chre
72 
73 #endif  // CHRE_UTIL_PIGWEED_RPC_HELPER_H_
74