Home | History | Annotate | Download | only in general_test
      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 
     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 
     39 void NanoappInfo::sendToHost() {
     40   AppInfo info;
     41   info.appId = mAppId;
     42   info.instanceId = mInstanceId;
     43 
     44   nanoapp_testing::hostToLittleEndian(&info.appId);
     45   nanoapp_testing::hostToLittleEndian(&info.instanceId);
     46 
     47   sendMessageToHost(nanoapp_testing::MessageType::kContinue,
     48                     &info, sizeof(info));
     49 }
     50 
     51 bool NanoappInfo::validate(uint64_t appId, uint32_t instanceId) {
     52   bool result = true;
     53   if (appId != mAppId) {
     54     nanoapp_testing::sendFatalFailureToHost(
     55         "app IDs do not match");
     56     result = false;
     57   } else if (instanceId != mInstanceId) {
     58     nanoapp_testing::sendFatalFailureToHost(
     59         "instance IDs do not match");
     60     result = false;
     61   }
     62 
     63   return result;
     64 }
     65 
     66 bool NanoappInfo::queryByAppId(struct chreNanoappInfo *info) {
     67   bool result = chreGetNanoappInfoByAppId(mAppId, info);
     68 
     69   if (!result) {
     70     nanoapp_testing::sendFatalFailureToHost(
     71         "Unable to get nanoapp info by app ID");
     72   }
     73 
     74   return result;
     75 }
     76 
     77 bool NanoappInfo::queryByInstanceId(struct chreNanoappInfo *info) {
     78   bool result = chreGetNanoappInfoByInstanceId(mInstanceId, info);
     79 
     80   if (!result) {
     81     nanoapp_testing::sendFatalFailureToHost(
     82         "Unable to get nanoapp info by instance ID");
     83   }
     84 
     85   return result;
     86 }
     87 
     88 } // namespace general_test
     89