1 /*
2  * Copyright (C) 2016 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 #ifndef _GTS_NANOAPPS_GENERAL_TEST_TIMER_SET_TEST_H_
18 #define _GTS_NANOAPPS_GENERAL_TEST_TIMER_SET_TEST_H_
19 
20 #include <general_test/test.h>
21 
22 namespace general_test {
23 
24 /**
25  * Checks that chreTimerSet() works by trying various timers.
26  *
27  * Simple Protocol.
28  */
29 class TimerSetTest : public Test {
30  public:
31   TimerSetTest();
32   ~TimerSetTest();
33 
34   void markSuccess(uint32_t stage);
35 
36  protected:
37   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
38                    const void *eventData) override;
39   void setUp(uint32_t messageSize, const void *message) override;
40 
41  private:
42   class Stage {
43    public:
44     Stage(uint32_t stage, uint64_t duration, const void *cookie, bool oneShot);
45     void start();
46     // We take 'test' so we can call markSuccess if appropriate.
47     void processEvent(uint64_t timestamp, TimerSetTest *test);
48 
getCookie()49     const void *getCookie() const {
50       return mCookie;
51     }
52 
53    private:
54     uint64_t mSetTime;
55     uint64_t mDuration;
56     uint32_t mStage;
57     uint32_t mEventCount;
58     const void *mCookie;
59     bool mOneShot;
60     uint32_t mTimerHandle;
61   };
62   // In the interest in keeping our static memory usage low (since we
63   // are just one of many, many tests in this nanoapp), we get this
64   // memory from the heap instead of statically declaring an array here.
65   Stage *mStages;
66   static constexpr size_t kStageCount = 6;
67 
68   bool mInMethod;
69   static constexpr uint32_t kAllFinished = (1 << kStageCount) - 1;
70   uint32_t mFinishedBitmask;
71 
72   void initStages();
73   Stage *getStageFromCookie(const void *cookie);
74 };
75 
76 }  // namespace general_test
77 
78 #endif  // _GTS_NANOAPPS_GENERAL_TEST_TIMER_SET_TEST_H_
79