aboutsummaryrefslogtreecommitdiff
path: root/apps/test/common/chre_cross_validator_wifi/inc/chre_cross_validator_wifi_manager.h
blob: 313d1f5f5975392459f58f524ab087c7f02b8eb6 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * 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_CROSS_VALIDATOR_WIFI_MANAGER_H_
#define CHRE_CROSS_VALIDATOR_WIFI_MANAGER_H_

#include <cinttypes>
#include <cstdint>

#include <chre.h>
#include <pb_common.h>
#include <pb_decode.h>
#include <pb_encode.h>

#include "chre/util/singleton.h"

#include "chre_cross_validation_wifi.nanopb.h"
#include "chre_test_common.nanopb.h"

#include "wifi_scan_result.h"

namespace chre {

namespace cross_validator_wifi {

/**
 * Class to manage a CHRE cross validator wifi nanoapp.
 */
class Manager {
 public:
  /**
   * Handle a CHRE event.
   *
   * @param senderInstanceId The instand ID that sent the event.
   * @param eventType The type of the event.
   * @param eventData The data for the event.
   */
  void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
                   const void *eventData);

 private:
  struct CrossValidatorState {
    uint16_t hostEndpoint;
  };

  chre_cross_validation_wifi_Step mStep = chre_cross_validation_wifi_Step_INIT;

  //! Struct that holds some information about the state of the cross validator
  CrossValidatorState mCrossValidatorState;

  // TODO: Find a better max scan results val
  static constexpr uint8_t kMaxScanResults = UINT8_MAX;
  WifiScanResult mApScanResults[kMaxScanResults];
  WifiScanResult mChreScanResults[kMaxScanResults];

  //! The current index that cross validator should assign to when a new scan
  //! result comes in.
  uint8_t mChreScanResultsI = 0;

  uint8_t mChreScanResultsSize = 0;
  uint8_t mApScanResultsSize = 0;

  //! The number of wifi scan results processed from CHRE apis
  uint8_t mNumResultsProcessed = 0;

  //! Bools indicating that data collection is complete for each side
  bool mApDataCollectionDone = false;
  bool mChreDataCollectionDone = false;

  /**
   * This is the fraction of the number of results in the greater set of
   * scan results between the AP and CHRE that the lesser set can differ by.
   * Increasing this value will increase the relative amount that AP and CHRE
   * results sizes can differ by.
   *
   * Ex: AP_results_size = 8
   *     CHRE_results_size = 7
   *     kMaxDiffNumResultsFraction = 0.25
   *
   *     CHRE_results_size is valid because it is >= 8 - 8 * 0.25 = 6
   */
  // TODO(b/185026344): Perfect this number. Consider using an abolute
  // difference instead of a percentage difference also.
  static constexpr float kMaxDiffNumResultsFraction = 0.25f;

  /**
   * Handle a message from the host.
   * @param senderInstanceId The instance id of the sender.
   * @param data The message from the host's data.
   */
  void handleMessageFromHost(uint32_t senderInstanceId,
                             const chreMessageFromHostData *data);

  /**
   * Handle a step start message from the host.
   *
   * @param stepStartCommand The step start command proto.
   */
  void handleStepStartMessage(
      chre_cross_validation_wifi_StepStartCommand stepStartCommand);

  /**
   * @param success true if the result was success.
   * @param errMessage The error message that should be sent to host with
   * failure.
   *
   * @return The TestResult proto message that is encoded with these fields.
   */
  chre_test_common_TestResult makeTestResultProtoMessage(
      bool success, const char *errMessage = nullptr);

  /**
   * @param capabilitiesFromChre The number with flags that represent the
   *        different wifi capabilities.
   * @return The wifi capabilities proto message for the host.
   */
  chre_cross_validation_wifi_WifiCapabilities makeWifiCapabilitiesMessage(
      uint32_t capabilitiesFromChre);

  /**
   * Encode the proto message and send to host.
   *
   * @param message The proto message struct pointer.
   * @param fields The fields descriptor of the proto message to encode.
   * @param messageType The message type of the message.
   */
  void encodeAndSendMessageToHost(const void *message, const pb_field_t *fields,
                                  uint32_t messageType);
  /**
   * Handle a wifi scan result data message sent from AP.
   *
   * @param hostData The message.
   */
  void handleDataMessage(const chreMessageFromHostData *hostData);

  /**
   * Handle a wifi scan result event from a CHRE event.
   *
   * @param event the wifi scan event from CHRE api.
   */
  void handleWifiScanResult(const chreWifiScanEvent *event);

  /**
   * Compare the AP and CHRE wifi scan results and send test result to host.
   */
  void compareAndSendResultToHost();

  /**
   * Verify the wifi scan results are matching between AP and CHRE.
   *
   * @param testResultOut Pointer to the test result proto message which will be
   *                      sent back to host, whose bool and message depends on
   *                      the checks inside this method.
   */
  void verifyScanResults(chre_test_common_TestResult *testResultOut);

  /**
   * Get the scan result that has the same bssid as the scan result passed.
   *
   * @param results        The array of scan results to search through.
   * @param resultsSize    The number of valid scan result objects in the array
   *                       passed.
   * @param queryResult    The result to search with.
   * @param resultIndexOut The pointer where the scan result index will be
   *                       copied to if the result was found.
   * @return true if the scan result was found.
   */
  bool getMatchingScanResult(WifiScanResult *results, uint8_t resultsSize,
                             const WifiScanResult &queryResult,
                             uint8_t *resultIndexOut);

  /**
   * Setup WiFi scan monitoring from CHRE apis.
   *
   * @return true if chreWifiConfigureScanMonitorAsync() returns true
   */
  bool setupWifiScanMonitoring();

  /**
   * Handle wifi async result event with event data.
   *
   * @param result The data for the event.
   */
  void handleWifiAsyncResult(const chreAsyncResult *result);

  /**
   * The function to pass as the encode function pointer for the errorMessage
   * field of the TestResult message.
   *
   * @param stream The stream to write bytes to.
   * @param field The field that should be encoded. Unused by us.
   * @param arg The argument that will be set to a pointer to the string to
   * encode as error message.
   */
  static bool encodeErrorMessage(pb_ostream_t *stream,
                                 const pb_field_t * /*field*/,
                                 void *const *arg);
};

// The chre cross validator manager singleton.
typedef chre::Singleton<Manager> ManagerSingleton;

}  // namespace cross_validator_wifi

}  // namespace chre

#endif  // CHRE_CROSS_VALIDATOR_WIFI_MANAGER_H_