OSDN Git Service

Fix VTS test which turns off radio.
[android-x86/hardware-interfaces.git] / wifi / 1.0 / default / wifi_chip.cpp
1 /*
2  * Copyright (C) 2016 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 #include <android-base/logging.h>
18
19 #include "hidl_return_util.h"
20 #include "hidl_struct_util.h"
21 #include "wifi_chip.h"
22 #include "wifi_feature_flags.h"
23 #include "wifi_status_util.h"
24
25 namespace {
26 using android::sp;
27 using android::hardware::hidl_vec;
28 using android::hardware::hidl_string;
29 using android::hardware::wifi::V1_0::ChipModeId;
30 using android::hardware::wifi::V1_0::IWifiChip;
31 using android::hardware::wifi::V1_0::IfaceType;
32
33 constexpr ChipModeId kStaChipModeId = 0;
34 constexpr ChipModeId kApChipModeId = 1;
35 constexpr ChipModeId kInvalidModeId = UINT32_MAX;
36
37 template <typename Iface>
38 void invalidateAndClear(sp<Iface>& iface) {
39   if (iface.get()) {
40     iface->invalidate();
41     iface.clear();
42   }
43 }
44 }  // namepsace
45
46 namespace android {
47 namespace hardware {
48 namespace wifi {
49 namespace V1_0 {
50 namespace implementation {
51 using hidl_return_util::validateAndCall;
52
53 WifiChip::WifiChip(
54     ChipId chip_id,
55     const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
56     const std::weak_ptr<mode_controller::WifiModeController> mode_controller)
57     : chip_id_(chip_id),
58       legacy_hal_(legacy_hal),
59       mode_controller_(mode_controller),
60       is_valid_(true),
61       current_mode_id_(kInvalidModeId),
62       debug_ring_buffer_cb_registered_(false) {}
63
64 void WifiChip::invalidate() {
65   invalidateAndRemoveAllIfaces();
66   legacy_hal_.reset();
67   event_cb_handler_.invalidate();
68   is_valid_ = false;
69 }
70
71 bool WifiChip::isValid() {
72   return is_valid_;
73 }
74
75 std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
76   return event_cb_handler_.getCallbacks();
77 }
78
79 Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
80   return validateAndCall(this,
81                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
82                          &WifiChip::getIdInternal,
83                          hidl_status_cb);
84 }
85
86 Return<void> WifiChip::registerEventCallback(
87     const sp<IWifiChipEventCallback>& event_callback,
88     registerEventCallback_cb hidl_status_cb) {
89   return validateAndCall(this,
90                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
91                          &WifiChip::registerEventCallbackInternal,
92                          hidl_status_cb,
93                          event_callback);
94 }
95
96 Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
97   return validateAndCall(this,
98                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
99                          &WifiChip::getCapabilitiesInternal,
100                          hidl_status_cb);
101 }
102
103 Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
104   return validateAndCall(this,
105                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
106                          &WifiChip::getAvailableModesInternal,
107                          hidl_status_cb);
108 }
109
110 Return<void> WifiChip::configureChip(ChipModeId mode_id,
111                                      configureChip_cb hidl_status_cb) {
112   return validateAndCall(this,
113                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
114                          &WifiChip::configureChipInternal,
115                          hidl_status_cb,
116                          mode_id);
117 }
118
119 Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
120   return validateAndCall(this,
121                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
122                          &WifiChip::getModeInternal,
123                          hidl_status_cb);
124 }
125
126 Return<void> WifiChip::requestChipDebugInfo(
127     requestChipDebugInfo_cb hidl_status_cb) {
128   return validateAndCall(this,
129                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
130                          &WifiChip::requestChipDebugInfoInternal,
131                          hidl_status_cb);
132 }
133
134 Return<void> WifiChip::requestDriverDebugDump(
135     requestDriverDebugDump_cb hidl_status_cb) {
136   return validateAndCall(this,
137                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
138                          &WifiChip::requestDriverDebugDumpInternal,
139                          hidl_status_cb);
140 }
141
142 Return<void> WifiChip::requestFirmwareDebugDump(
143     requestFirmwareDebugDump_cb hidl_status_cb) {
144   return validateAndCall(this,
145                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
146                          &WifiChip::requestFirmwareDebugDumpInternal,
147                          hidl_status_cb);
148 }
149
150 Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
151   return validateAndCall(this,
152                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
153                          &WifiChip::createApIfaceInternal,
154                          hidl_status_cb);
155 }
156
157 Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
158   return validateAndCall(this,
159                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
160                          &WifiChip::getApIfaceNamesInternal,
161                          hidl_status_cb);
162 }
163
164 Return<void> WifiChip::getApIface(const hidl_string& ifname,
165                                   getApIface_cb hidl_status_cb) {
166   return validateAndCall(this,
167                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
168                          &WifiChip::getApIfaceInternal,
169                          hidl_status_cb,
170                          ifname);
171 }
172
173 Return<void> WifiChip::removeApIface(const hidl_string& ifname,
174                                      removeApIface_cb hidl_status_cb) {
175   return validateAndCall(this,
176                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
177                          &WifiChip::removeApIfaceInternal,
178                          hidl_status_cb,
179                          ifname);
180 }
181
182 Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
183   return validateAndCall(this,
184                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
185                          &WifiChip::createNanIfaceInternal,
186                          hidl_status_cb);
187 }
188
189 Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
190   return validateAndCall(this,
191                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
192                          &WifiChip::getNanIfaceNamesInternal,
193                          hidl_status_cb);
194 }
195
196 Return<void> WifiChip::getNanIface(const hidl_string& ifname,
197                                    getNanIface_cb hidl_status_cb) {
198   return validateAndCall(this,
199                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
200                          &WifiChip::getNanIfaceInternal,
201                          hidl_status_cb,
202                          ifname);
203 }
204
205 Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
206                                       removeNanIface_cb hidl_status_cb) {
207   return validateAndCall(this,
208                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
209                          &WifiChip::removeNanIfaceInternal,
210                          hidl_status_cb,
211                          ifname);
212 }
213
214 Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
215   return validateAndCall(this,
216                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
217                          &WifiChip::createP2pIfaceInternal,
218                          hidl_status_cb);
219 }
220
221 Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
222   return validateAndCall(this,
223                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
224                          &WifiChip::getP2pIfaceNamesInternal,
225                          hidl_status_cb);
226 }
227
228 Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
229                                    getP2pIface_cb hidl_status_cb) {
230   return validateAndCall(this,
231                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
232                          &WifiChip::getP2pIfaceInternal,
233                          hidl_status_cb,
234                          ifname);
235 }
236
237 Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
238                                       removeP2pIface_cb hidl_status_cb) {
239   return validateAndCall(this,
240                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
241                          &WifiChip::removeP2pIfaceInternal,
242                          hidl_status_cb,
243                          ifname);
244 }
245
246 Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
247   return validateAndCall(this,
248                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
249                          &WifiChip::createStaIfaceInternal,
250                          hidl_status_cb);
251 }
252
253 Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
254   return validateAndCall(this,
255                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
256                          &WifiChip::getStaIfaceNamesInternal,
257                          hidl_status_cb);
258 }
259
260 Return<void> WifiChip::getStaIface(const hidl_string& ifname,
261                                    getStaIface_cb hidl_status_cb) {
262   return validateAndCall(this,
263                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
264                          &WifiChip::getStaIfaceInternal,
265                          hidl_status_cb,
266                          ifname);
267 }
268
269 Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
270                                       removeStaIface_cb hidl_status_cb) {
271   return validateAndCall(this,
272                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
273                          &WifiChip::removeStaIfaceInternal,
274                          hidl_status_cb,
275                          ifname);
276 }
277
278 Return<void> WifiChip::createRttController(
279     const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
280   return validateAndCall(this,
281                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
282                          &WifiChip::createRttControllerInternal,
283                          hidl_status_cb,
284                          bound_iface);
285 }
286
287 Return<void> WifiChip::getDebugRingBuffersStatus(
288     getDebugRingBuffersStatus_cb hidl_status_cb) {
289   return validateAndCall(this,
290                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
291                          &WifiChip::getDebugRingBuffersStatusInternal,
292                          hidl_status_cb);
293 }
294
295 Return<void> WifiChip::startLoggingToDebugRingBuffer(
296     const hidl_string& ring_name,
297     WifiDebugRingBufferVerboseLevel verbose_level,
298     uint32_t max_interval_in_sec,
299     uint32_t min_data_size_in_bytes,
300     startLoggingToDebugRingBuffer_cb hidl_status_cb) {
301   return validateAndCall(this,
302                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
303                          &WifiChip::startLoggingToDebugRingBufferInternal,
304                          hidl_status_cb,
305                          ring_name,
306                          verbose_level,
307                          max_interval_in_sec,
308                          min_data_size_in_bytes);
309 }
310
311 Return<void> WifiChip::forceDumpToDebugRingBuffer(
312     const hidl_string& ring_name,
313     forceDumpToDebugRingBuffer_cb hidl_status_cb) {
314   return validateAndCall(this,
315                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
316                          &WifiChip::forceDumpToDebugRingBufferInternal,
317                          hidl_status_cb,
318                          ring_name);
319 }
320
321 Return<void> WifiChip::stopLoggingToDebugRingBuffer(
322     stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
323   return validateAndCall(this,
324                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
325                          &WifiChip::stopLoggingToDebugRingBufferInternal,
326                          hidl_status_cb);
327 }
328
329 Return<void> WifiChip::getDebugHostWakeReasonStats(
330     getDebugHostWakeReasonStats_cb hidl_status_cb) {
331   return validateAndCall(this,
332                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
333                          &WifiChip::getDebugHostWakeReasonStatsInternal,
334                          hidl_status_cb);
335 }
336
337 Return<void> WifiChip::enableDebugErrorAlerts(
338     bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
339   return validateAndCall(this,
340                          WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
341                          &WifiChip::enableDebugErrorAlertsInternal,
342                          hidl_status_cb,
343                          enable);
344 }
345
346 void WifiChip::invalidateAndRemoveAllIfaces() {
347   invalidateAndClear(ap_iface_);
348   invalidateAndClear(nan_iface_);
349   invalidateAndClear(p2p_iface_);
350   invalidateAndClear(sta_iface_);
351   // Since all the ifaces are invalid now, all RTT controller objects
352   // using those ifaces also need to be invalidated.
353   for (const auto& rtt : rtt_controllers_) {
354     rtt->invalidate();
355   }
356   rtt_controllers_.clear();
357 }
358
359 std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
360   return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
361 }
362
363 WifiStatus WifiChip::registerEventCallbackInternal(
364     const sp<IWifiChipEventCallback>& event_callback) {
365   if (!event_cb_handler_.addCallback(event_callback)) {
366     return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
367   }
368   return createWifiStatus(WifiStatusCode::SUCCESS);
369 }
370
371 std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
372   legacy_hal::wifi_error legacy_status;
373   uint32_t legacy_logger_feature_set;
374   std::tie(legacy_status, legacy_logger_feature_set) =
375       legacy_hal_.lock()->getLoggerSupportedFeatureSet();
376   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
377     return {createWifiStatusFromLegacyError(legacy_status), 0};
378   }
379   uint32_t hidl_caps;
380   if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
381           legacy_logger_feature_set, &hidl_caps)) {
382     return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
383   }
384   return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
385 }
386
387 std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
388 WifiChip::getAvailableModesInternal() {
389   // The chip combination supported for current devices is fixed for now with
390   // 2 separate modes of operation:
391   // Mode 1 (STA mode): Will support 1 STA and 1 P2P or NAN iface operations
392   // concurrently [NAN conditional on wifiHidlFeatureAware]
393   // Mode 2 (AP mode): Will support 1 AP iface operations.
394   // TODO (b/32997844): Read this from some device specific flags in the
395   // makefile.
396   // STA mode iface combinations.
397   const IWifiChip::ChipIfaceCombinationLimit
398       sta_chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
399   IWifiChip::ChipIfaceCombinationLimit sta_chip_iface_combination_limit_2;
400   if (WifiFeatureFlags::wifiHidlFeatureAware) {
401     sta_chip_iface_combination_limit_2 = {{IfaceType::P2P, IfaceType::NAN},
402                                           1};
403   } else {
404     sta_chip_iface_combination_limit_2 = {{IfaceType::P2P},
405                                           1};
406   }
407   const IWifiChip::ChipIfaceCombination sta_chip_iface_combination = {
408       {sta_chip_iface_combination_limit_1, sta_chip_iface_combination_limit_2}};
409   const IWifiChip::ChipMode sta_chip_mode = {kStaChipModeId,
410                                              {sta_chip_iface_combination}};
411   // AP mode iface combinations.
412   const IWifiChip::ChipIfaceCombinationLimit ap_chip_iface_combination_limit = {
413       {IfaceType::AP}, 1};
414   const IWifiChip::ChipIfaceCombination ap_chip_iface_combination = {
415       {ap_chip_iface_combination_limit}};
416   const IWifiChip::ChipMode ap_chip_mode = {kApChipModeId,
417                                             {ap_chip_iface_combination}};
418   return {createWifiStatus(WifiStatusCode::SUCCESS),
419           {sta_chip_mode, ap_chip_mode}};
420 }
421
422 WifiStatus WifiChip::configureChipInternal(ChipModeId mode_id) {
423   if (mode_id != kStaChipModeId && mode_id != kApChipModeId) {
424     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
425   }
426   if (mode_id == current_mode_id_) {
427     LOG(DEBUG) << "Already in the specified mode " << mode_id;
428     return createWifiStatus(WifiStatusCode::SUCCESS);
429   }
430   WifiStatus status = handleChipConfiguration(mode_id);
431   if (status.code != WifiStatusCode::SUCCESS) {
432     for (const auto& callback : event_cb_handler_.getCallbacks()) {
433       if (!callback->onChipReconfigureFailure(status).isOk()) {
434         LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
435       }
436     }
437     return status;
438   }
439   for (const auto& callback : event_cb_handler_.getCallbacks()) {
440     if (!callback->onChipReconfigured(mode_id).isOk()) {
441       LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
442     }
443   }
444   current_mode_id_ = mode_id;
445   return status;
446 }
447
448 std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
449   if (current_mode_id_ == kInvalidModeId) {
450     return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
451             current_mode_id_};
452   }
453   return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
454 }
455
456 std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
457 WifiChip::requestChipDebugInfoInternal() {
458   IWifiChip::ChipDebugInfo result;
459   legacy_hal::wifi_error legacy_status;
460   std::string driver_desc;
461   std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion();
462   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
463     LOG(ERROR) << "Failed to get driver version: "
464                << legacyErrorToString(legacy_status);
465     WifiStatus status = createWifiStatusFromLegacyError(
466         legacy_status, "failed to get driver version");
467     return {status, result};
468   }
469   result.driverDescription = driver_desc.c_str();
470
471   std::string firmware_desc;
472   std::tie(legacy_status, firmware_desc) =
473       legacy_hal_.lock()->getFirmwareVersion();
474   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
475     LOG(ERROR) << "Failed to get firmware version: "
476                << legacyErrorToString(legacy_status);
477     WifiStatus status = createWifiStatusFromLegacyError(
478         legacy_status, "failed to get firmware version");
479     return {status, result};
480   }
481   result.firmwareDescription = firmware_desc.c_str();
482
483   return {createWifiStatus(WifiStatusCode::SUCCESS), result};
484 }
485
486 std::pair<WifiStatus, std::vector<uint8_t>>
487 WifiChip::requestDriverDebugDumpInternal() {
488   legacy_hal::wifi_error legacy_status;
489   std::vector<uint8_t> driver_dump;
490   std::tie(legacy_status, driver_dump) =
491       legacy_hal_.lock()->requestDriverMemoryDump();
492   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
493     LOG(ERROR) << "Failed to get driver debug dump: "
494                << legacyErrorToString(legacy_status);
495     return {createWifiStatusFromLegacyError(legacy_status),
496             std::vector<uint8_t>()};
497   }
498   return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
499 }
500
501 std::pair<WifiStatus, std::vector<uint8_t>>
502 WifiChip::requestFirmwareDebugDumpInternal() {
503   legacy_hal::wifi_error legacy_status;
504   std::vector<uint8_t> firmware_dump;
505   std::tie(legacy_status, firmware_dump) =
506       legacy_hal_.lock()->requestFirmwareMemoryDump();
507   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
508     LOG(ERROR) << "Failed to get firmware debug dump: "
509                << legacyErrorToString(legacy_status);
510     return {createWifiStatusFromLegacyError(legacy_status), {}};
511   }
512   return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
513 }
514
515 std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
516   if (current_mode_id_ != kApChipModeId || ap_iface_.get()) {
517     return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
518   }
519   std::string ifname = legacy_hal_.lock()->getApIfaceName();
520   ap_iface_ = new WifiApIface(ifname, legacy_hal_);
521   for (const auto& callback : event_cb_handler_.getCallbacks()) {
522     if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
523       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
524     }
525   }
526   return {createWifiStatus(WifiStatusCode::SUCCESS), ap_iface_};
527 }
528
529 std::pair<WifiStatus, std::vector<hidl_string>>
530 WifiChip::getApIfaceNamesInternal() {
531   if (!ap_iface_.get()) {
532     return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
533   }
534   return {createWifiStatus(WifiStatusCode::SUCCESS),
535           {legacy_hal_.lock()->getApIfaceName()}};
536 }
537
538 std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
539     const std::string& ifname) {
540   if (!ap_iface_.get() || (ifname != legacy_hal_.lock()->getApIfaceName())) {
541     return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
542   }
543   return {createWifiStatus(WifiStatusCode::SUCCESS), ap_iface_};
544 }
545
546 WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
547   if (!ap_iface_.get() || (ifname != legacy_hal_.lock()->getApIfaceName())) {
548     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
549   }
550   invalidateAndClear(ap_iface_);
551   for (const auto& callback : event_cb_handler_.getCallbacks()) {
552     if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
553       LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
554     }
555   }
556   return createWifiStatus(WifiStatusCode::SUCCESS);
557 }
558
559 std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
560   // Only 1 of NAN or P2P iface can be active at a time.
561   if (WifiFeatureFlags::wifiHidlFeatureAware) {
562     if (current_mode_id_ != kStaChipModeId || nan_iface_.get() ||
563         p2p_iface_.get()) {
564       return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
565     }
566     std::string ifname = legacy_hal_.lock()->getNanIfaceName();
567     nan_iface_ = new WifiNanIface(ifname, legacy_hal_);
568     for (const auto& callback : event_cb_handler_.getCallbacks()) {
569       if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
570         LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
571       }
572     }
573     return {createWifiStatus(WifiStatusCode::SUCCESS), nan_iface_};
574   } else {
575     return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
576   }
577 }
578
579 std::pair<WifiStatus, std::vector<hidl_string>>
580 WifiChip::getNanIfaceNamesInternal() {
581   if (!nan_iface_.get()) {
582     return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
583   }
584   return {createWifiStatus(WifiStatusCode::SUCCESS),
585           {legacy_hal_.lock()->getNanIfaceName()}};
586 }
587
588 std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
589     const std::string& ifname) {
590   if (!nan_iface_.get() || (ifname != legacy_hal_.lock()->getNanIfaceName())) {
591     return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
592   }
593   return {createWifiStatus(WifiStatusCode::SUCCESS), nan_iface_};
594 }
595
596 WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
597   if (!nan_iface_.get() || (ifname != legacy_hal_.lock()->getNanIfaceName())) {
598     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
599   }
600   invalidateAndClear(nan_iface_);
601   for (const auto& callback : event_cb_handler_.getCallbacks()) {
602     if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
603       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
604     }
605   }
606   return createWifiStatus(WifiStatusCode::SUCCESS);
607 }
608
609 std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
610   // Only 1 of NAN or P2P iface can be active at a time.
611   if (current_mode_id_ != kStaChipModeId || p2p_iface_.get() ||
612       nan_iface_.get()) {
613     return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
614   }
615   std::string ifname = legacy_hal_.lock()->getP2pIfaceName();
616   p2p_iface_ = new WifiP2pIface(ifname, legacy_hal_);
617   for (const auto& callback : event_cb_handler_.getCallbacks()) {
618     if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
619       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
620     }
621   }
622   return {createWifiStatus(WifiStatusCode::SUCCESS), p2p_iface_};
623 }
624
625 std::pair<WifiStatus, std::vector<hidl_string>>
626 WifiChip::getP2pIfaceNamesInternal() {
627   if (!p2p_iface_.get()) {
628     return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
629   }
630   return {createWifiStatus(WifiStatusCode::SUCCESS),
631           {legacy_hal_.lock()->getP2pIfaceName()}};
632 }
633
634 std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
635     const std::string& ifname) {
636   if (!p2p_iface_.get() || (ifname != legacy_hal_.lock()->getP2pIfaceName())) {
637     return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
638   }
639   return {createWifiStatus(WifiStatusCode::SUCCESS), p2p_iface_};
640 }
641
642 WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
643   if (!p2p_iface_.get() || (ifname != legacy_hal_.lock()->getP2pIfaceName())) {
644     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
645   }
646   invalidateAndClear(p2p_iface_);
647   for (const auto& callback : event_cb_handler_.getCallbacks()) {
648     if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
649       LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
650     }
651   }
652   return createWifiStatus(WifiStatusCode::SUCCESS);
653 }
654
655 std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
656   if (current_mode_id_ != kStaChipModeId || sta_iface_.get()) {
657     return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
658   }
659   std::string ifname = legacy_hal_.lock()->getStaIfaceName();
660   sta_iface_ = new WifiStaIface(ifname, legacy_hal_);
661   for (const auto& callback : event_cb_handler_.getCallbacks()) {
662     if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
663       LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
664     }
665   }
666   return {createWifiStatus(WifiStatusCode::SUCCESS), sta_iface_};
667 }
668
669 std::pair<WifiStatus, std::vector<hidl_string>>
670 WifiChip::getStaIfaceNamesInternal() {
671   if (!sta_iface_.get()) {
672     return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
673   }
674   return {createWifiStatus(WifiStatusCode::SUCCESS),
675           {legacy_hal_.lock()->getStaIfaceName()}};
676 }
677
678 std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
679     const std::string& ifname) {
680   if (!sta_iface_.get() || (ifname != legacy_hal_.lock()->getStaIfaceName())) {
681     return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
682   }
683   return {createWifiStatus(WifiStatusCode::SUCCESS), sta_iface_};
684 }
685
686 WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
687   if (!sta_iface_.get() || (ifname != legacy_hal_.lock()->getStaIfaceName())) {
688     return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
689   }
690   invalidateAndClear(sta_iface_);
691   for (const auto& callback : event_cb_handler_.getCallbacks()) {
692     if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
693       LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
694     }
695   }
696   return createWifiStatus(WifiStatusCode::SUCCESS);
697 }
698
699 std::pair<WifiStatus, sp<IWifiRttController>>
700 WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
701   sp<WifiRttController> rtt = new WifiRttController(bound_iface, legacy_hal_);
702   rtt_controllers_.emplace_back(rtt);
703   return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
704 }
705
706 std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
707 WifiChip::getDebugRingBuffersStatusInternal() {
708   legacy_hal::wifi_error legacy_status;
709   std::vector<legacy_hal::wifi_ring_buffer_status>
710       legacy_ring_buffer_status_vec;
711   std::tie(legacy_status, legacy_ring_buffer_status_vec) =
712       legacy_hal_.lock()->getRingBuffersStatus();
713   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
714     return {createWifiStatusFromLegacyError(legacy_status), {}};
715   }
716   std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
717   if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
718           legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
719     return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
720   }
721   return {createWifiStatus(WifiStatusCode::SUCCESS),
722           hidl_ring_buffer_status_vec};
723 }
724
725 WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
726     const hidl_string& ring_name,
727     WifiDebugRingBufferVerboseLevel verbose_level,
728     uint32_t max_interval_in_sec,
729     uint32_t min_data_size_in_bytes) {
730   WifiStatus status = registerDebugRingBufferCallback();
731   if (status.code != WifiStatusCode::SUCCESS) {
732     return status;
733   }
734   legacy_hal::wifi_error legacy_status =
735       legacy_hal_.lock()->startRingBufferLogging(
736           ring_name,
737           static_cast<
738               std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
739               verbose_level),
740           max_interval_in_sec,
741           min_data_size_in_bytes);
742   return createWifiStatusFromLegacyError(legacy_status);
743 }
744
745 WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
746     const hidl_string& ring_name) {
747   WifiStatus status = registerDebugRingBufferCallback();
748   if (status.code != WifiStatusCode::SUCCESS) {
749     return status;
750   }
751   legacy_hal::wifi_error legacy_status =
752       legacy_hal_.lock()->getRingBufferData(ring_name);
753   return createWifiStatusFromLegacyError(legacy_status);
754 }
755
756 WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
757   legacy_hal::wifi_error legacy_status =
758       legacy_hal_.lock()->deregisterRingBufferCallbackHandler();
759   return createWifiStatusFromLegacyError(legacy_status);
760 }
761
762 std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
763 WifiChip::getDebugHostWakeReasonStatsInternal() {
764   legacy_hal::wifi_error legacy_status;
765   legacy_hal::WakeReasonStats legacy_stats;
766   std::tie(legacy_status, legacy_stats) =
767       legacy_hal_.lock()->getWakeReasonStats();
768   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
769     return {createWifiStatusFromLegacyError(legacy_status), {}};
770   }
771   WifiDebugHostWakeReasonStats hidl_stats;
772   if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
773                                                             &hidl_stats)) {
774     return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
775   }
776   return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
777 }
778
779 WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
780   legacy_hal::wifi_error legacy_status;
781   if (enable) {
782     android::wp<WifiChip> weak_ptr_this(this);
783     const auto& on_alert_callback = [weak_ptr_this](
784         int32_t error_code, std::vector<uint8_t> debug_data) {
785       const auto shared_ptr_this = weak_ptr_this.promote();
786       if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
787         LOG(ERROR) << "Callback invoked on an invalid object";
788         return;
789       }
790       for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
791         if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
792           LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
793         }
794       }
795     };
796     legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
797         on_alert_callback);
798   } else {
799     legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler();
800   }
801   return createWifiStatusFromLegacyError(legacy_status);
802 }
803
804 WifiStatus WifiChip::handleChipConfiguration(ChipModeId mode_id) {
805   // If the chip is already configured in a different mode, stop
806   // the legacy HAL and then start it after firmware mode change.
807   // Currently the underlying implementation has a deadlock issue.
808   // We should return ERROR_NOT_SUPPORTED if chip is already configured in
809   // a different mode.
810   if (current_mode_id_ != kInvalidModeId) {
811     // TODO(b/37446050): Fix the deadlock.
812     return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
813   }
814   bool success;
815   if (mode_id == kStaChipModeId) {
816     success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
817   } else {
818     success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
819   }
820   if (!success) {
821     return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
822   }
823   legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
824   if (legacy_status != legacy_hal::WIFI_SUCCESS) {
825     LOG(ERROR) << "Failed to start legacy HAL: "
826                << legacyErrorToString(legacy_status);
827     return createWifiStatusFromLegacyError(legacy_status);
828   }
829   return createWifiStatus(WifiStatusCode::SUCCESS);
830 }
831
832 WifiStatus WifiChip::registerDebugRingBufferCallback() {
833   if (debug_ring_buffer_cb_registered_) {
834     return createWifiStatus(WifiStatusCode::SUCCESS);
835   }
836
837   android::wp<WifiChip> weak_ptr_this(this);
838   const auto& on_ring_buffer_data_callback = [weak_ptr_this](
839       const std::string& /* name */,
840       const std::vector<uint8_t>& data,
841       const legacy_hal::wifi_ring_buffer_status& status) {
842     const auto shared_ptr_this = weak_ptr_this.promote();
843     if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
844       LOG(ERROR) << "Callback invoked on an invalid object";
845       return;
846     }
847     WifiDebugRingBufferStatus hidl_status;
848     if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
849             status, &hidl_status)) {
850       LOG(ERROR) << "Error converting ring buffer status";
851       return;
852     }
853     for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
854       if (!callback->onDebugRingBufferDataAvailable(hidl_status, data).isOk()) {
855         LOG(ERROR) << "Failed to invoke onDebugRingBufferDataAvailable"
856                    << " callback on: " << toString(callback);
857
858       }
859     }
860   };
861   legacy_hal::wifi_error legacy_status =
862       legacy_hal_.lock()->registerRingBufferCallbackHandler(
863           on_ring_buffer_data_callback);
864
865   if (legacy_status == legacy_hal::WIFI_SUCCESS) {
866     debug_ring_buffer_cb_registered_ = true;
867   }
868   return createWifiStatusFromLegacyError(legacy_status);
869 }
870
871 }  // namespace implementation
872 }  // namespace V1_0
873 }  // namespace wifi
874 }  // namespace hardware
875 }  // namespace android