• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/macros.h>
20 #include <shared/nano_endian.h>
21 #include <shared/send_message.h>
22 
23 #include "chre_api/chre.h"
24 
25 using nanoapp_testing::sendFailureToHost;
26 
27 namespace general_test {
28 
29 struct AppInfo {
30   uint64_t appId;
31   uint32_t instanceId;
32 } __attribute__((packed));
33 
NanoappInfo()34 NanoappInfo::NanoappInfo()
35     : mAppId(chreGetAppId()), mInstanceId(chreGetInstanceId()) {
36   if (mInstanceId == CHRE_INSTANCE_ID) {
37     EXPECT_FAIL_RETURN("Given CHRE_INSTANCE_ID for my instance ID");
38   }
39 }
40 
sendToHost()41 void NanoappInfo::sendToHost() {
42   AppInfo info = {
43       .appId = nanoapp_testing::hostToLittleEndian(mAppId),
44       .instanceId = nanoapp_testing::hostToLittleEndian(mInstanceId),
45   };
46 
47   sendMessageToHost(nanoapp_testing::MessageType::kContinue, &info,
48                     sizeof(info));
49 }
50 
validate(uint64_t appId,uint32_t instanceId)51 bool NanoappInfo::validate(uint64_t appId, uint32_t instanceId) {
52   bool result = true;
53   if (appId != mAppId) {
54     sendFailureToHost("app IDs do not match");
55     result = false;
56   } else if (instanceId != mInstanceId) {
57     sendFailureToHost("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     sendFailureToHost("Unable to get nanoapp info by app ID");
69   }
70 
71   return result;
72 }
73 
queryByInstanceId(struct chreNanoappInfo * info)74 bool NanoappInfo::queryByInstanceId(struct chreNanoappInfo *info) {
75   bool result = chreGetNanoappInfoByInstanceId(mInstanceId, info);
76 
77   if (!result) {
78     sendFailureToHost("Unable to get nanoapp info by instance ID");
79   }
80 
81   return result;
82 }
83 
84 }  // namespace general_test
85