• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chre/util/pigweed/rpc_helper.h"
18 
19 #include <cinttypes>
20 
21 #include "chre/util/nanoapp/log.h"
22 
23 #ifndef LOG_TAG
24 #define LOG_TAG "[RpcHelper]"
25 #endif  // LOG_TAG
26 
27 namespace chre {
28 
rpcEndpointsMatch(uint32_t expectedId,uint32_t actualId)29 bool rpcEndpointsMatch(uint32_t expectedId, uint32_t actualId) {
30   if ((expectedId & kRpcClientIdMask) != (actualId & kRpcClientIdMask)) {
31     LOGE("Invalid endpoint 0x%04" PRIx32 " expected 0x%04" PRIx32, actualId,
32          expectedId);
33     return false;
34   }
35 
36   return true;
37 }
38 
isRpcChannelIdHost(uint32_t id)39 bool isRpcChannelIdHost(uint32_t id) {
40   return id >> 16 == 1;
41 }
42 
isRpcChannelIdNanoapp(uint32_t id)43 bool isRpcChannelIdNanoapp(uint32_t id) {
44   return id >> 16 == 0;
45 }
46 
validateHostChannelId(const chreMessageFromHostData * msg,uint32_t channelId)47 bool validateHostChannelId(const chreMessageFromHostData *msg,
48                            uint32_t channelId) {
49   struct chreHostEndpointInfo info;
50 
51   if (!isRpcChannelIdHost(channelId) ||
52       !chreGetHostEndpointInfo(msg->hostEndpoint, &info)) {
53     LOGE("Invalid channelId for a host client 0x%08" PRIx32, channelId);
54     return false;
55   }
56 
57   return rpcEndpointsMatch(channelId, static_cast<uint32_t>(msg->hostEndpoint));
58 }
59 
validateNanoappChannelId(uint32_t nappId,uint32_t channelId)60 bool validateNanoappChannelId(uint32_t nappId, uint32_t channelId) {
61   if (nappId > kRpcNanoappMaxId) {
62     LOGE("Invalid nanoapp Id 0x%08" PRIx32, nappId);
63     return false;
64   }
65 
66   if (!isRpcChannelIdNanoapp(channelId)) {
67     LOGE("Invalid channelId for a nanoapp 0x%08" PRIx32, channelId);
68     return false;
69   }
70 
71   return rpcEndpointsMatch(channelId, nappId);
72 }
73 
74 }  // namespace chre