summaryrefslogtreecommitdiffstats
blob: 5c73362049af4384403a3648b4a874b110511e70 (plain) (blame)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (C) 2016 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.
 */
#include "wificond/scanning/offload/offload_scan_manager.h"

#include <vector>

#include <android-base/logging.h>

#include "wificond/scanning/offload/hidl_call_util.h"
#include "wificond/scanning/offload/offload_scan_utils.h"
#include "wificond/scanning/offload/offload_service_utils.h"
#include "wificond/scanning/offload/scan_stats.h"
#include "wificond/scanning/scan_result.h"

using ::android::hardware::hidl_vec;
using android::hardware::wifi::offload::V1_0::IOffload;
using android::hardware::wifi::offload::V1_0::ScanResult;
using android::hardware::wifi::offload::V1_0::ScanFilter;
using android::hardware::wifi::offload::V1_0::ScanParam;
using android::hardware::wifi::offload::V1_0::ScanStats;
using android::hardware::wifi::offload::V1_0::OffloadStatus;
using android::hardware::wifi::offload::V1_0::OffloadStatusCode;

using android::wificond::OffloadCallback;
using ::com::android::server::wifi::wificond::NativeScanResult;
using ::com::android::server::wifi::wificond::NativeScanStats;
using std::vector;
using std::weak_ptr;
using std::shared_ptr;

using namespace std::placeholders;

namespace {
const uint32_t kSubscriptionDelayMs = 5000;
}

namespace android {
namespace wificond {

OffloadCallbackHandlersImpl::OffloadCallbackHandlersImpl(
    OffloadScanManager* offload_scan_manager)
    : offload_scan_manager_(offload_scan_manager) {}

OffloadCallbackHandlersImpl::~OffloadCallbackHandlersImpl() {}

void OffloadCallbackHandlersImpl::OnScanResultHandler(
    const vector<ScanResult>& scanResult) {
  if (offload_scan_manager_ != nullptr) {
    offload_scan_manager_->ReportScanResults(scanResult);
  }
}

void OffloadCallbackHandlersImpl::OnErrorHandler(const OffloadStatus& status) {
  if (offload_scan_manager_ != nullptr) {
    offload_scan_manager_->ReportError(status);
  }
}

OffloadScanManager::OffloadScanManager(
    weak_ptr<OffloadServiceUtils> utils,
    shared_ptr<OffloadScanCallbackInterface> callback)
    : wifi_offload_hal_(nullptr),
      wifi_offload_callback_(nullptr),
      offload_status_(OffloadScanManager::kError),
      offload_callback_handlers_(new OffloadCallbackHandlersImpl(this)),
      event_callback_(callback) {
  auto offload_scan_utils = utils.lock();
  wifi_offload_hal_ = offload_scan_utils->GetOffloadService();
  if (wifi_offload_hal_ == nullptr) {
    LOG(ERROR) << "No Offload Service available";
    return;
  }

  death_recipient_ = offload_scan_utils->GetOffloadDeathRecipient(
      std::bind(&OffloadScanManager::OnObjectDeath, this, _1));
  uint64_t cookie = reinterpret_cast<uint64_t>(wifi_offload_hal_.get());
  auto link_to_death_status =
      wifi_offload_hal_->linkToDeath(death_recipient_, cookie);
  if (!link_to_death_status.isOk()) {
    LOG(ERROR) << "Unable to register death handler "
               << link_to_death_status.description();
    return;
  }

  wifi_offload_callback_ =
      offload_scan_utils->GetOffloadCallback(offload_callback_handlers_.get());
  if (wifi_offload_callback_ == nullptr) {
    LOG(ERROR) << "Invalid Offload callback object";
    return;
  }
  wifi_offload_hal_->setEventCallback(wifi_offload_callback_);
  offload_status_ = OffloadScanManager::kNoError;
}

bool OffloadScanManager::stopScan(OffloadScanManager::ReasonCode* reason_code) {
  if (getOffloadStatus() != OffloadScanManager::kNoError) {
    *reason_code = OffloadScanManager::kNotAvailable;
    return false;
  }
  const auto& res = wifi_offload_hal_->unsubscribeScanResults();
  if (!res.isOk()) {
    *reason_code = OffloadScanManager::kTransactionFailed;
    LOG(WARNING) << "unsubscribeScanResults() failed " << res.description();
    return false;
  }
  *reason_code = OffloadScanManager::kNone;
  return true;
}

bool OffloadScanManager::GetScanStats(NativeScanStats* native_scan_stats) {
  const auto& result = HIDL_INVOKE(wifi_offload_hal_, getScanStats);
  const auto& offload_status_and_scan_stats = result.first;
  bool transport_status = result.second;
  if (!transport_status) {
    return false;
  }
  OffloadStatus offload_status = offload_status_and_scan_stats.first;
  ScanStats scan_stats = offload_status_and_scan_stats.second;
  if (offload_status.code != OffloadStatusCode::OK) {
    LOG(WARNING) << offload_status.description;
    return false;
  }
  *native_scan_stats = OffloadScanUtils::convertToNativeScanStats(scan_stats);
  return true;
}

bool OffloadScanManager::VerifyAndConvertHIDLStatus(
    std::pair<OffloadStatus, bool> result,
    OffloadScanManager::ReasonCode* reason_code) {
  const auto& offload_status = result.first;
  bool transport_status = result.second;
  if (!transport_status) {
    *reason_code = OffloadScanManager::kTransactionFailed;
    return false;
  }
  if (offload_status.code != OffloadStatusCode::OK) {
    LOG(WARNING) << offload_status.description;
    *reason_code = OffloadScanManager::kOperationFailed;
    return false;
  }
  return true;
}

bool OffloadScanManager::startScan(
    uint32_t interval_ms, int32_t rssi_threshold,
    const vector<vector<uint8_t>>& scan_ssids,
    const vector<vector<uint8_t>>& match_ssids,
    const vector<uint8_t>& match_security, const vector<uint32_t>& freqs,
    OffloadScanManager::ReasonCode* reason_code) {
  if (getOffloadStatus() != OffloadScanManager::kNoError) {
    *reason_code = OffloadScanManager::kNotAvailable;
    LOG(WARNING) << "Offload HAL scans are not available";
    return false;
  }
  ScanParam param =
      OffloadScanUtils::createScanParam(scan_ssids, freqs, interval_ms);
  ScanFilter filter = OffloadScanUtils::createScanFilter(
      match_ssids, match_security, rssi_threshold);

  if (!ConfigureScans(param, filter, reason_code)) {
    return false;
  }

  if (!SubscribeScanResults(reason_code)) {
    return false;
  }

  *reason_code = OffloadScanManager::kNone;
  /* Clear up the scan cache every time a new scan is requested */
  cached_scan_results_.clear();
  return true;
}

bool OffloadScanManager::ConfigureScans(
    ScanParam param, ScanFilter filter,
    OffloadScanManager::ReasonCode* reason_code) {
  const auto& result =
      HIDL_INVOKE(wifi_offload_hal_, configureScans, param, filter);
  if (!VerifyAndConvertHIDLStatus(result, reason_code)) {
    return false;
  }
  return true;
}

bool OffloadScanManager::SubscribeScanResults(
    OffloadScanManager::ReasonCode* reason_code) {
  const auto& result = HIDL_INVOKE(wifi_offload_hal_, subscribeScanResults,
                                   kSubscriptionDelayMs);
  if (!VerifyAndConvertHIDLStatus(result, reason_code)) {
    return false;
  }
  return true;
}

OffloadScanManager::StatusCode OffloadScanManager::getOffloadStatus() const {
  if (wifi_offload_hal_ == nullptr) {
    return OffloadScanManager::kNoService;
  }
  return offload_status_;
}

bool OffloadScanManager::getScanResults(
    std::vector<NativeScanResult>* out_scan_results) {
  for (auto scan_result : cached_scan_results_) {
    out_scan_results->push_back(scan_result);
  }
  return true;
}

bool OffloadScanManager::getScanStats(NativeScanStats* native_scan_stats) {
  if (getOffloadStatus() != OffloadScanManager::kNoError) {
    LOG(WARNING) << "Unable to get scan stats due to Wifi Offload HAL error";
    return false;
  }
  return GetScanStats(native_scan_stats);
}

OffloadScanManager::~OffloadScanManager() {
  if (wifi_offload_hal_ != nullptr) {
    wifi_offload_hal_->unlinkToDeath(death_recipient_);
  }
}

void OffloadScanManager::ReportScanResults(
    const vector<ScanResult>& scanResult) {
  cached_scan_results_ =
      OffloadScanUtils::convertToNativeScanResults(scanResult);
  if (event_callback_ != nullptr) {
    event_callback_->OnOffloadScanResult();
  } else {
    LOG(WARNING)
        << "No callback to report Offload HAL's scan results to wificond";
  }
}

void OffloadScanManager::ReportError(const OffloadStatus& status) {
  OffloadStatusCode status_code = status.code;
  OffloadScanManager::StatusCode status_result = OffloadScanManager::kNoError;
  switch (status_code) {
    case OffloadStatusCode::OK:
      status_result = OffloadScanManager::kNoError;
      break;
    case OffloadStatusCode::TIMEOUT:
      status_result = OffloadScanManager::kTimeOut;
      break;
    case OffloadStatusCode::NO_CONNECTION:
      status_result = OffloadScanManager::kNotConnected;
      break;
    case OffloadStatusCode::ERROR:
      status_result = OffloadScanManager::kError;
      break;
    default:
      LOG(WARNING) << "Invalid Offload Error reported";
      return;
  }
  if (status_result != OffloadScanManager::kNoError) {
    LOG(WARNING) << "Offload Error reported " << status.description;
    if (event_callback_ != nullptr) {
      event_callback_->OnOffloadError(
          OffloadScanCallbackInterface::REMOTE_FAILURE);
    } else {
      LOG(WARNING) << "No callback to report Offload HAL Errors to wificond";
    }
  }
  offload_status_ = status_result;
}

void OffloadScanManager::OnObjectDeath(uint64_t cookie) {
  if (wifi_offload_hal_ == reinterpret_cast<IOffload*>(cookie)) {
    wifi_offload_hal_.clear();
    if (event_callback_ != nullptr) {
      event_callback_->OnOffloadError(
          OffloadScanCallbackInterface::BINDER_DEATH);
    } else {
      LOG(WARNING)
          << "No callback to report Offload HAL Binder death to wificond";
    }

    LOG(ERROR) << "Death Notification for Wifi Offload HAL";
  }
}

}  // namespace wificond
}  // namespace android