1 /*
2  * Copyright (C) 2020 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 CHRE_AUDIO_CONCURRENCY_TEST_MANAGER_H_
18 #define CHRE_AUDIO_CONCURRENCY_TEST_MANAGER_H_
19 
20 #include <chre.h>
21 #include <cinttypes>
22 
23 #include "chre/util/optional.h"
24 #include "chre/util/singleton.h"
25 
26 namespace chre {
27 
28 namespace audio_concurrency_test {
29 
30 /**
31  * A class to manage a CHRE audio concurrency test session.
32  */
33 class Manager {
34  public:
35   enum class TestStep : uint8_t {
36     ENABLE_AUDIO = 0,
37     VERIFY_AUDIO_RESUME,
38   };
39 
40   ~Manager();
41 
42   /**
43    * Handles an event from CHRE. Semantics are the same as nanoappHandleEvent.
44    */
45   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
46                    const void *eventData);
47 
48  private:
49   struct TestSession {
50     uint16_t hostEndpointId;
51     TestStep step;
52 
TestSessionTestSession53     TestSession(uint16_t id, TestStep step) {
54       this->hostEndpointId = id;
55       this->step = step;
56     }
57   };
58 
59   /**
60    * Handles a message from the host.
61    *
62    * @param senderInstanceId The sender instance ID of this message.
63    * @param hostData The data from the host.
64    */
65   void handleMessageFromHost(uint32_t senderInstanceId,
66                              const chreMessageFromHostData *hostData);
67 
68   /**
69    * Initiates the test given a test command from the host.
70    *
71    * @param hostEndpointId The test host endpoint ID.
72    * @param step The test step.
73    *
74    * @return true if the message was handled correctly.
75    */
76   bool handleTestCommandMessage(uint16_t hostEndpointId, TestStep step);
77 
78   /**
79    * Processes data from CHRE.
80    *
81    * @param eventType The event type as defined by CHRE.
82    * @param eventData A pointer to the data.
83    */
84   void handleDataFromChre(uint16_t eventType, const void *eventData);
85 
86   /**
87    * Handles a CHRE timer event.
88    */
89   void handleTimer();
90 
91   /**
92    * @param durationSeconds The duration of the timeout timer.
93    *
94    * @return True if the timer was set successfully.
95    */
96   bool setTimeoutTimer(uint32_t durationSeconds);
97 
98   /**
99    * Cancels the timeout timer, if pending.
100    */
101   void cancelTimeoutTimer();
102 
103   /**
104    * Performs a check on the audio data.
105    *
106    * @param data The audio data.
107    *
108    * @return true if the audio data is valid.
109    */
110   bool validateAudioDataEvent(const chreAudioDataEvent *data);
111 
112   /**
113    * @param data The audio data.
114    */
115   void handleAudioDataEvent(const chreAudioDataEvent *data);
116 
117   // Use the first audio source available for this test.
118   static constexpr uint32_t kAudioHandle = 0;
119 
120   //! The audio source to use for this test.
121   struct chreAudioSource mAudioSource;
122 
123   //! The current test session.
124   Optional<TestSession> mTestSession;
125 
126   //! The handle of the timer to check timeout.
127   uint32_t mTimerHandle = CHRE_TIMER_INVALID;
128 
129   //! True if CHRE audio is enabled for this nanoapp.
130   bool mAudioEnabled = false;
131 };
132 
133 // The audio concurrency test manager singleton.
134 typedef chre::Singleton<Manager> ManagerSingleton;
135 
136 }  // namespace audio_concurrency_test
137 
138 }  // namespace chre
139 
140 #endif  // CHRE_AUDIO_CONCURRENCY_TEST_MANAGER_H_
141