1 /*
2  * Copyright (C) 2018 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 <shared/test_success_marker.h>
18 
19 #include <chre/re.h>
20 #include <shared/send_message.h>
21 
22 namespace nanoapp_testing {
23 
TestSuccessMarker(uint32_t numStages)24 TestSuccessMarker::TestSuccessMarker(uint32_t numStages) {
25   if (numStages > 32) {
26     sendFatalFailureToHost(
27         "Total number of stage should be less than 33, got %d", &numStages);
28   }
29   mAllFinished = (UINT64_C(1) << numStages) - 1;
30 }
31 
markStage(uint32_t stage)32 void TestSuccessMarker::markStage(uint32_t stage) {
33   uint32_t finishedBit = (1 << stage);
34   if ((mAllFinished & finishedBit) == 0) {
35     sendFatalFailureToHost("markSuccess invalid stage", &stage);
36   }
37   if ((mFinishedBitmask & finishedBit) == 0) {
38     chreLog(CHRE_LOG_DEBUG, "Stage %" PRIu32 " succeeded", stage);
39     mFinishedBitmask |= finishedBit;
40   }
41 }
42 
isAllFinished()43 bool TestSuccessMarker::isAllFinished() {
44   return (mFinishedBitmask == mAllFinished);
45 }
46 
isStageMarked(uint32_t stage)47 bool TestSuccessMarker::isStageMarked(uint32_t stage) {
48   bool marked = false;
49   if (stage <= 32) {
50     uint32_t finishedBit = (1 << stage);
51     marked = ((mFinishedBitmask & finishedBit) != 0);
52   }
53   return marked;
54 }
55 
markStageAndSuccessOnFinish(uint32_t stage)56 void TestSuccessMarker::markStageAndSuccessOnFinish(uint32_t stage) {
57   if (!isStageMarked(stage)) {
58     markStage(stage);
59     if (isAllFinished()) {
60       sendSuccessToHost();
61     }
62   }
63 }
64 
65 }  // namespace nanoapp_testing
66