aboutsummaryrefslogtreecommitdiff
path: root/apps/test/common/chre_settings_test/inc/chre_settings_test_manager.h
blob: 2c25c8d80a3d1274d5f39ef386cc9f38c1474170 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef CHRE_SETTINGS_TEST_MANAGER_H_
#define CHRE_SETTINGS_TEST_MANAGER_H_

#include "chre_settings_test.nanopb.h"

#include <chre.h>
#include <cinttypes>

#include "chre/util/optional.h"
#include "chre/util/singleton.h"

namespace chre {

namespace settings_test {

/**
 * A class to manage a CHRE settings test session.
 */
class Manager {
 public:
  enum class Feature : uint8_t {
    WIFI_SCANNING = 0,
    WIFI_RTT,
    GNSS_LOCATION,
    GNSS_MEASUREMENT,
    WWAN_CELL_INFO,
  };

  enum class FeatureState : uint8_t {
    DISABLED = 0,
    ENABLED,
  };

  enum class TestStep : uint8_t {
    SETUP = 0,
    START,
  };

  /**
   * Handles an event from CHRE. Semantics are the same as nanoappHandleEvent.
   */
  void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
                   const void *eventData);

 private:
  struct TestSession {
    uint16_t hostEndpointId;
    Feature feature;
    FeatureState featureState;
    TestStep step;

    TestSession(uint16_t id, Feature feature, FeatureState state,
                TestStep step) {
      this->hostEndpointId = id;
      this->feature = feature;
      this->featureState = state;
      this->step = step;
    }
  };

  /**
   * @return true if the provided feature is supported by CHRE.
   */
  bool isFeatureSupported(Feature feature);

  /**
   * Handles a message from the host.
   *
   * @param senderInstanceId The sender instance ID of this message.
   * @param hostData The data from the host.
   */
  void handleMessageFromHost(uint32_t senderInstanceId,
                             const chreMessageFromHostData *hostData);

  /**
   * Initiates the test given a start command from the host. If a test was
   * already in progress, a new start message will override and start a new
   * test.
   *
   * @param hostEndpointId The test host endpoint ID.
   * @param feature The feature to test.
   * @param state The feature state.
   * @param step The test step.
   */
  void handleStartTestMessage(uint16_t hostEndpointId, Feature feature,
                              FeatureState state, TestStep step);

  /**
   * Processes data from CHRE.
   *
   * @param eventType The event type as defined by CHRE.
   * @param eventData A pointer to the data.
   */
  void handleDataFromChre(uint16_t eventType, const void *eventData);

  /**
   * Starts a test for a given feature.
   *
   * @param feature The feature to test.
   *
   * @return true if the test successfully began.
   */
  bool startTestForFeature(Feature feature);

  /**
   * @param result The async result.
   * @param expectedCookie The expected cookie value.
   *
   * @return true if the async result matches expected values.
   */
  bool validateAsyncResult(const chreAsyncResult *result,
                           const void *expectedCookie);

  /**
   * @param result The async result provided by CHRE.
   */
  void handleWifiAsyncResult(const chreAsyncResult *result);
  void handleGnssAsyncResult(const chreAsyncResult *result);

  /**
   * @param result The cell info result from CHRE.
   */
  void handleWwanCellInfoResult(const chreWwanCellInfoResult *result);

  /**
   * @param result The WiFi scan event result.
   */
  void handleWifiScanResult(const chreWifiScanEvent *result);

  /**
   * End the current test session and sends result to host.
   *
   * @param hostEndpointId The host to send the result to.
   * @param success True if the test succeeded.
   */
  void sendTestResult(uint16_t hostEndpointId, bool success);

  //! The current test session.
  chre::Optional<TestSession> mTestSession;

  //! The cached target to issue an RTT ranging request.
  chre::Optional<chreWifiRangingTarget> mCachedRangingTarget;
};

// The settings test manager singleton.
typedef chre::Singleton<Manager> ManagerSingleton;

}  // namespace settings_test

}  // namespace chre

#endif  // CHRE_SETTINGS_TEST_MANAGER_H_