1 /*
2  * Copyright (C) 2017 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 <general_test/nanoapp_info.h>
18 
19 #include <shared/nano_endian.h>
20 #include <shared/send_message.h>
21 
22 #include <chre.h>
23 
24 namespace general_test {
25 
26 struct AppInfo {
27   uint64_t appId;
28   uint32_t instanceId;
29 } __attribute__((packed));
30 
NanoappInfo()31 NanoappInfo::NanoappInfo()
32     : mAppId(chreGetAppId()), mInstanceId(chreGetInstanceId()) {
33   if (mInstanceId == CHRE_INSTANCE_ID) {
34     nanoapp_testing::sendFatalFailureToHost(
35         "Given CHRE_INSTANCE_ID for my instance ID");
36   }
37 }
38 
sendToHost()39 void NanoappInfo::sendToHost() {
40   AppInfo info = {
41       .appId = nanoapp_testing::hostToLittleEndian(mAppId),
42       .instanceId = nanoapp_testing::hostToLittleEndian(mInstanceId),
43   };
44 
45   sendMessageToHost(nanoapp_testing::MessageType::kContinue, &info,
46                     sizeof(info));
47 }
48 
validate(uint64_t appId,uint32_t instanceId)49 bool NanoappInfo::validate(uint64_t appId, uint32_t instanceId) {
50   bool result = true;
51   if (appId != mAppId) {
52     nanoapp_testing::sendFatalFailureToHost("app IDs do not match");
53     result = false;
54   } else if (instanceId != mInstanceId) {
55     nanoapp_testing::sendFatalFailureToHost("instance IDs do not match");
56     result = false;
57   }
58 
59   return result;
60 }
61 
queryByAppId(struct chreNanoappInfo * info)62 bool NanoappInfo::queryByAppId(struct chreNanoappInfo *info) {
63   bool result = chreGetNanoappInfoByAppId(mAppId, info);
64 
65   if (!result) {
66     nanoapp_testing::sendFatalFailureToHost(
67         "Unable to get nanoapp info by app ID");
68   }
69 
70   return result;
71 }
72 
queryByInstanceId(struct chreNanoappInfo * info)73 bool NanoappInfo::queryByInstanceId(struct chreNanoappInfo *info) {
74   bool result = chreGetNanoappInfoByInstanceId(mInstanceId, info);
75 
76   if (!result) {
77     nanoapp_testing::sendFatalFailureToHost(
78         "Unable to get nanoapp info by instance ID");
79   }
80 
81   return result;
82 }
83 
84 }  // namespace general_test
85