1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker *
4*84e33947SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker *
8*84e33947SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker *
10*84e33947SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker */
16*84e33947SAndroid Build Coastguard Worker
17*84e33947SAndroid Build Coastguard Worker #include "chre/util/pigweed/rpc_helper.h"
18*84e33947SAndroid Build Coastguard Worker
19*84e33947SAndroid Build Coastguard Worker #include <cinttypes>
20*84e33947SAndroid Build Coastguard Worker
21*84e33947SAndroid Build Coastguard Worker #include "chre/util/nanoapp/log.h"
22*84e33947SAndroid Build Coastguard Worker
23*84e33947SAndroid Build Coastguard Worker #ifndef LOG_TAG
24*84e33947SAndroid Build Coastguard Worker #define LOG_TAG "[RpcHelper]"
25*84e33947SAndroid Build Coastguard Worker #endif // LOG_TAG
26*84e33947SAndroid Build Coastguard Worker
27*84e33947SAndroid Build Coastguard Worker namespace chre {
28*84e33947SAndroid Build Coastguard Worker
rpcEndpointsMatch(uint32_t expectedId,uint32_t actualId)29*84e33947SAndroid Build Coastguard Worker bool rpcEndpointsMatch(uint32_t expectedId, uint32_t actualId) {
30*84e33947SAndroid Build Coastguard Worker if ((expectedId & kRpcClientIdMask) != (actualId & kRpcClientIdMask)) {
31*84e33947SAndroid Build Coastguard Worker LOGE("Invalid endpoint 0x%04" PRIx32 " expected 0x%04" PRIx32, actualId,
32*84e33947SAndroid Build Coastguard Worker expectedId);
33*84e33947SAndroid Build Coastguard Worker return false;
34*84e33947SAndroid Build Coastguard Worker }
35*84e33947SAndroid Build Coastguard Worker
36*84e33947SAndroid Build Coastguard Worker return true;
37*84e33947SAndroid Build Coastguard Worker }
38*84e33947SAndroid Build Coastguard Worker
isRpcChannelIdHost(uint32_t id)39*84e33947SAndroid Build Coastguard Worker bool isRpcChannelIdHost(uint32_t id) {
40*84e33947SAndroid Build Coastguard Worker return id >> 16 == 1;
41*84e33947SAndroid Build Coastguard Worker }
42*84e33947SAndroid Build Coastguard Worker
isRpcChannelIdNanoapp(uint32_t id)43*84e33947SAndroid Build Coastguard Worker bool isRpcChannelIdNanoapp(uint32_t id) {
44*84e33947SAndroid Build Coastguard Worker return id >> 16 == 0;
45*84e33947SAndroid Build Coastguard Worker }
46*84e33947SAndroid Build Coastguard Worker
validateHostChannelId(const chreMessageFromHostData * msg,uint32_t channelId)47*84e33947SAndroid Build Coastguard Worker bool validateHostChannelId(const chreMessageFromHostData *msg,
48*84e33947SAndroid Build Coastguard Worker uint32_t channelId) {
49*84e33947SAndroid Build Coastguard Worker struct chreHostEndpointInfo info;
50*84e33947SAndroid Build Coastguard Worker
51*84e33947SAndroid Build Coastguard Worker if (!isRpcChannelIdHost(channelId) ||
52*84e33947SAndroid Build Coastguard Worker !chreGetHostEndpointInfo(msg->hostEndpoint, &info)) {
53*84e33947SAndroid Build Coastguard Worker LOGE("Invalid channelId for a host client 0x%08" PRIx32, channelId);
54*84e33947SAndroid Build Coastguard Worker return false;
55*84e33947SAndroid Build Coastguard Worker }
56*84e33947SAndroid Build Coastguard Worker
57*84e33947SAndroid Build Coastguard Worker return rpcEndpointsMatch(channelId, static_cast<uint32_t>(msg->hostEndpoint));
58*84e33947SAndroid Build Coastguard Worker }
59*84e33947SAndroid Build Coastguard Worker
validateNanoappChannelId(uint32_t nappId,uint32_t channelId)60*84e33947SAndroid Build Coastguard Worker bool validateNanoappChannelId(uint32_t nappId, uint32_t channelId) {
61*84e33947SAndroid Build Coastguard Worker if (nappId > kRpcNanoappMaxId) {
62*84e33947SAndroid Build Coastguard Worker LOGE("Invalid nanoapp Id 0x%08" PRIx32, nappId);
63*84e33947SAndroid Build Coastguard Worker return false;
64*84e33947SAndroid Build Coastguard Worker }
65*84e33947SAndroid Build Coastguard Worker
66*84e33947SAndroid Build Coastguard Worker if (!isRpcChannelIdNanoapp(channelId)) {
67*84e33947SAndroid Build Coastguard Worker LOGE("Invalid channelId for a nanoapp 0x%08" PRIx32, channelId);
68*84e33947SAndroid Build Coastguard Worker return false;
69*84e33947SAndroid Build Coastguard Worker }
70*84e33947SAndroid Build Coastguard Worker
71*84e33947SAndroid Build Coastguard Worker return rpcEndpointsMatch(channelId, nappId);
72*84e33947SAndroid Build Coastguard Worker }
73*84e33947SAndroid Build Coastguard Worker
74*84e33947SAndroid Build Coastguard Worker } // namespace chre