aboutsummaryrefslogtreecommitdiff
path: root/apps/test/common/chre_cross_validator_wifi/inc/chre_cross_validator_wifi_manager.h
blob: e1d5c274dec7582b85bcda173efc798cb3e896ab (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
/*
 * 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;

  /**
   * 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);

  /**
   * Encode the proto message and send to host.
   *
   * @param message The proto message struct pointer.
   * @fields The fields descriptor of the proto message to encode.
   */
  void encodeAndSendMessageToHost(const void *message,
                                  const pb_field_t *fields);
  /**
   * 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();

  /**
   * 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_