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,
46                     &info, 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(
53         "app IDs do not match");
54     result = false;
55   } else if (instanceId != mInstanceId) {
56     nanoapp_testing::sendFatalFailureToHost(
57         "instance IDs do not match");
58     result = false;
59   }
60 
61   return result;
62 }
63 
queryByAppId(struct chreNanoappInfo * info)64 bool NanoappInfo::queryByAppId(struct chreNanoappInfo *info) {
65   bool result = chreGetNanoappInfoByAppId(mAppId, info);
66 
67   if (!result) {
68     nanoapp_testing::sendFatalFailureToHost(
69         "Unable to get nanoapp info by app ID");
70   }
71 
72   return result;
73 }
74 
queryByInstanceId(struct chreNanoappInfo * info)75 bool NanoappInfo::queryByInstanceId(struct chreNanoappInfo *info) {
76   bool result = chreGetNanoappInfoByInstanceId(mInstanceId, info);
77 
78   if (!result) {
79     nanoapp_testing::sendFatalFailureToHost(
80         "Unable to get nanoapp info by instance ID");
81   }
82 
83   return result;
84 }
85 
86 } // namespace general_test
87