Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 "wifi_nan_iface.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "aidl_return_util.h" |
| 22 | #include "aidl_struct_util.h" |
| 23 | #include "wifi_status_util.h" |
| 24 | |
| 25 | namespace aidl { |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace wifi { |
| 29 | using aidl_return_util::validateAndCall; |
| 30 | |
| 31 | WifiNanIface::WifiNanIface(const std::string& ifname, bool is_dedicated_iface, |
| 32 | const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
| 33 | const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util) |
| 34 | : ifname_(ifname), |
| 35 | is_dedicated_iface_(is_dedicated_iface), |
| 36 | legacy_hal_(legacy_hal), |
| 37 | iface_util_(iface_util), |
| 38 | is_valid_(true) {} |
| 39 | |
| 40 | std::shared_ptr<WifiNanIface> WifiNanIface::create( |
| 41 | const std::string& ifname, bool is_dedicated_iface, |
| 42 | const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
| 43 | const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util) { |
| 44 | std::shared_ptr<WifiNanIface> ptr = ndk::SharedRefBase::make<WifiNanIface>( |
| 45 | ifname, is_dedicated_iface, legacy_hal, iface_util); |
| 46 | if (is_dedicated_iface) { |
| 47 | // If using a dedicated iface, set the iface up first. |
| 48 | if (!iface_util.lock()->setUpState(ifname, true)) { |
| 49 | // Fatal failure, invalidate the iface object. |
| 50 | ptr->invalidate(); |
| 51 | return nullptr; |
| 52 | } |
| 53 | } |
| 54 | std::weak_ptr<WifiNanIface> weak_ptr_this(ptr); |
| 55 | ptr->setWeakPtr(weak_ptr_this); |
| 56 | ptr->registerCallbackHandlers(); |
| 57 | return ptr; |
| 58 | } |
| 59 | |
| 60 | void WifiNanIface::registerCallbackHandlers() { |
| 61 | // Register all the callbacks here. These should be valid for the lifetime |
| 62 | // of the object. Whenever the mode changes legacy HAL will remove |
| 63 | // all of these callbacks. |
| 64 | legacy_hal::NanCallbackHandlers callback_handlers; |
| 65 | std::weak_ptr<WifiNanIface> weak_ptr_this = weak_ptr_this_; |
| 66 | |
| 67 | // Callback for response. |
| 68 | callback_handlers.on_notify_response = [weak_ptr_this](legacy_hal::transaction_id id, |
| 69 | const legacy_hal::NanResponseMsg& msg) { |
| 70 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 71 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 72 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 73 | return; |
| 74 | } |
| 75 | NanStatus nanStatus; |
| 76 | if (!aidl_struct_util::convertLegacyNanResponseHeaderToAidl(msg, &nanStatus)) { |
| 77 | LOG(ERROR) << "Failed to convert nan response header"; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | switch (msg.response_type) { |
| 82 | case legacy_hal::NAN_RESPONSE_ENABLED: { |
| 83 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 84 | if (!callback->notifyEnableResponse(id, nanStatus).isOk()) { |
| 85 | LOG(ERROR) << "Failed to invoke the callback"; |
| 86 | } |
| 87 | } |
| 88 | break; |
| 89 | } |
| 90 | case legacy_hal::NAN_RESPONSE_DISABLED: { |
| 91 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 92 | if (!callback->notifyDisableResponse(id, nanStatus).isOk()) { |
| 93 | LOG(ERROR) << "Failed to invoke the callback"; |
| 94 | } |
| 95 | } |
| 96 | break; |
| 97 | } |
| 98 | case legacy_hal::NAN_RESPONSE_PUBLISH: { |
| 99 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 100 | if (!callback->notifyStartPublishResponse(id, nanStatus, |
| 101 | msg.body.publish_response.publish_id) |
| 102 | .isOk()) { |
| 103 | LOG(ERROR) << "Failed to invoke the callback"; |
| 104 | } |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | case legacy_hal::NAN_RESPONSE_PUBLISH_CANCEL: { |
| 109 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 110 | if (!callback->notifyStopPublishResponse(id, nanStatus).isOk()) { |
| 111 | LOG(ERROR) << "Failed to invoke the callback"; |
| 112 | } |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | case legacy_hal::NAN_RESPONSE_TRANSMIT_FOLLOWUP: { |
| 117 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 118 | if (!callback->notifyTransmitFollowupResponse(id, nanStatus).isOk()) { |
| 119 | LOG(ERROR) << "Failed to invoke the callback"; |
| 120 | } |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | case legacy_hal::NAN_RESPONSE_SUBSCRIBE: { |
| 125 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 126 | if (!callback->notifyStartSubscribeResponse( |
| 127 | id, nanStatus, msg.body.subscribe_response.subscribe_id) |
| 128 | .isOk()) { |
| 129 | LOG(ERROR) << "Failed to invoke the callback"; |
| 130 | } |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | case legacy_hal::NAN_RESPONSE_SUBSCRIBE_CANCEL: { |
| 135 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 136 | if (!callback->notifyStopSubscribeResponse(id, nanStatus).isOk()) { |
| 137 | LOG(ERROR) << "Failed to invoke the callback"; |
| 138 | } |
| 139 | } |
| 140 | break; |
| 141 | } |
| 142 | case legacy_hal::NAN_RESPONSE_CONFIG: { |
| 143 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 144 | if (!callback->notifyConfigResponse(id, nanStatus).isOk()) { |
| 145 | LOG(ERROR) << "Failed to invoke the callback"; |
| 146 | } |
| 147 | } |
| 148 | break; |
| 149 | } |
| 150 | case legacy_hal::NAN_GET_CAPABILITIES: { |
| 151 | NanCapabilities aidl_struct; |
| 152 | if (!aidl_struct_util::convertLegacyNanCapabilitiesResponseToAidl( |
| 153 | msg.body.nan_capabilities, &aidl_struct)) { |
| 154 | LOG(ERROR) << "Failed to convert nan capabilities response"; |
| 155 | return; |
| 156 | } |
| 157 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 158 | if (!callback->notifyCapabilitiesResponse(id, nanStatus, aidl_struct).isOk()) { |
| 159 | LOG(ERROR) << "Failed to invoke the callback"; |
| 160 | } |
| 161 | } |
| 162 | break; |
| 163 | } |
| 164 | case legacy_hal::NAN_DP_INTERFACE_CREATE: { |
| 165 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 166 | if (!callback->notifyCreateDataInterfaceResponse(id, nanStatus).isOk()) { |
| 167 | LOG(ERROR) << "Failed to invoke the callback"; |
| 168 | } |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | case legacy_hal::NAN_DP_INTERFACE_DELETE: { |
| 173 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 174 | if (!callback->notifyDeleteDataInterfaceResponse(id, nanStatus).isOk()) { |
| 175 | LOG(ERROR) << "Failed to invoke the callback"; |
| 176 | } |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | case legacy_hal::NAN_DP_INITIATOR_RESPONSE: { |
| 181 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 182 | if (!callback->notifyInitiateDataPathResponse( |
| 183 | id, nanStatus, |
| 184 | msg.body.data_request_response.ndp_instance_id) |
| 185 | .isOk()) { |
| 186 | LOG(ERROR) << "Failed to invoke the callback"; |
| 187 | } |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | case legacy_hal::NAN_DP_RESPONDER_RESPONSE: { |
| 192 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 193 | if (!callback->notifyRespondToDataPathIndicationResponse(id, nanStatus) |
| 194 | .isOk()) { |
| 195 | LOG(ERROR) << "Failed to invoke the callback"; |
| 196 | } |
| 197 | } |
| 198 | break; |
| 199 | } |
| 200 | case legacy_hal::NAN_DP_END: { |
| 201 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 202 | if (!callback->notifyTerminateDataPathResponse(id, nanStatus).isOk()) { |
| 203 | LOG(ERROR) << "Failed to invoke the callback"; |
| 204 | } |
| 205 | } |
| 206 | break; |
| 207 | } |
| 208 | case legacy_hal::NAN_RESPONSE_BEACON_SDF_PAYLOAD: |
| 209 | /* fall through */ |
| 210 | case legacy_hal::NAN_RESPONSE_TCA: |
| 211 | /* fall through */ |
| 212 | case legacy_hal::NAN_RESPONSE_STATS: |
| 213 | /* fall through */ |
| 214 | case legacy_hal::NAN_RESPONSE_ERROR: |
| 215 | /* fall through */ |
| 216 | default: |
| 217 | LOG(ERROR) << "Unknown or unhandled response type: " << msg.response_type; |
| 218 | return; |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | callback_handlers.on_event_disc_eng_event = [weak_ptr_this]( |
| 223 | const legacy_hal::NanDiscEngEventInd& msg) { |
| 224 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 225 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 226 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 227 | return; |
| 228 | } |
| 229 | NanClusterEventInd aidl_struct; |
| 230 | // event types defined identically - hence can be cast |
| 231 | aidl_struct.eventType = (NanClusterEventType)msg.event_type; |
| 232 | aidl_struct.addr = std::array<uint8_t, 6>(); |
| 233 | std::copy(msg.data.mac_addr.addr, msg.data.mac_addr.addr + 6, std::begin(aidl_struct.addr)); |
| 234 | |
| 235 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 236 | if (!callback->eventClusterEvent(aidl_struct).isOk()) { |
| 237 | LOG(ERROR) << "Failed to invoke the callback"; |
| 238 | } |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | callback_handlers.on_event_disabled = [weak_ptr_this](const legacy_hal::NanDisabledInd& msg) { |
| 243 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 244 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 245 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 246 | return; |
| 247 | } |
| 248 | NanStatus status; |
| 249 | aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason, sizeof(msg.nan_reason), |
| 250 | &status); |
| 251 | |
| 252 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 253 | if (!callback->eventDisabled(status).isOk()) { |
| 254 | LOG(ERROR) << "Failed to invoke the callback"; |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | callback_handlers.on_event_publish_terminated = |
| 260 | [weak_ptr_this](const legacy_hal::NanPublishTerminatedInd& msg) { |
| 261 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 262 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 263 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 264 | return; |
| 265 | } |
| 266 | NanStatus status; |
| 267 | aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason, |
| 268 | sizeof(msg.nan_reason), &status); |
| 269 | |
| 270 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 271 | if (!callback->eventPublishTerminated(msg.publish_id, status).isOk()) { |
| 272 | LOG(ERROR) << "Failed to invoke the callback"; |
| 273 | } |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | callback_handlers.on_event_subscribe_terminated = |
| 278 | [weak_ptr_this](const legacy_hal::NanSubscribeTerminatedInd& msg) { |
| 279 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 280 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 281 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 282 | return; |
| 283 | } |
| 284 | NanStatus status; |
| 285 | aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason, |
| 286 | sizeof(msg.nan_reason), &status); |
| 287 | |
| 288 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 289 | if (!callback->eventSubscribeTerminated(msg.subscribe_id, status).isOk()) { |
| 290 | LOG(ERROR) << "Failed to invoke the callback"; |
| 291 | } |
| 292 | } |
| 293 | }; |
| 294 | |
| 295 | callback_handlers.on_event_match = [weak_ptr_this](const legacy_hal::NanMatchInd& msg) { |
| 296 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 297 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 298 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 299 | return; |
| 300 | } |
| 301 | NanMatchInd aidl_struct; |
| 302 | if (!aidl_struct_util::convertLegacyNanMatchIndToAidl(msg, &aidl_struct)) { |
| 303 | LOG(ERROR) << "Failed to convert nan capabilities response"; |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 308 | if (!callback->eventMatch(aidl_struct).isOk()) { |
| 309 | LOG(ERROR) << "Failed to invoke the callback"; |
| 310 | } |
| 311 | } |
| 312 | }; |
| 313 | |
| 314 | callback_handlers.on_event_match_expired = [weak_ptr_this]( |
| 315 | const legacy_hal::NanMatchExpiredInd& msg) { |
| 316 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 317 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 318 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 319 | return; |
| 320 | } |
| 321 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 322 | if (!callback->eventMatchExpired(msg.publish_subscribe_id, msg.requestor_instance_id) |
| 323 | .isOk()) { |
| 324 | LOG(ERROR) << "Failed to invoke the callback"; |
| 325 | } |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | callback_handlers.on_event_followup = [weak_ptr_this](const legacy_hal::NanFollowupInd& msg) { |
| 330 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 331 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 332 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 333 | return; |
| 334 | } |
| 335 | NanFollowupReceivedInd aidl_struct; |
| 336 | if (!aidl_struct_util::convertLegacyNanFollowupIndToAidl(msg, &aidl_struct)) { |
| 337 | LOG(ERROR) << "Failed to convert nan capabilities response"; |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 342 | if (!callback->eventFollowupReceived(aidl_struct).isOk()) { |
| 343 | LOG(ERROR) << "Failed to invoke the callback"; |
| 344 | } |
| 345 | } |
| 346 | }; |
| 347 | |
| 348 | callback_handlers.on_event_transmit_follow_up = |
| 349 | [weak_ptr_this](const legacy_hal::NanTransmitFollowupInd& msg) { |
| 350 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 351 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 352 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 353 | return; |
| 354 | } |
| 355 | NanStatus status; |
| 356 | aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason, |
| 357 | sizeof(msg.nan_reason), &status); |
| 358 | |
| 359 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 360 | if (!callback->eventTransmitFollowup(msg.id, status).isOk()) { |
| 361 | LOG(ERROR) << "Failed to invoke the callback"; |
| 362 | } |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | callback_handlers.on_event_data_path_request = |
| 367 | [weak_ptr_this](const legacy_hal::NanDataPathRequestInd& msg) { |
| 368 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 369 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 370 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 371 | return; |
| 372 | } |
| 373 | NanDataPathRequestInd aidl_struct; |
| 374 | if (!aidl_struct_util::convertLegacyNanDataPathRequestIndToAidl(msg, |
| 375 | &aidl_struct)) { |
| 376 | LOG(ERROR) << "Failed to convert nan capabilities response"; |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 381 | if (!callback->eventDataPathRequest(aidl_struct).isOk()) { |
| 382 | LOG(ERROR) << "Failed to invoke the callback"; |
| 383 | } |
| 384 | } |
| 385 | }; |
| 386 | |
| 387 | callback_handlers.on_event_data_path_confirm = |
| 388 | [weak_ptr_this](const legacy_hal::NanDataPathConfirmInd& msg) { |
| 389 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 390 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 391 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 392 | return; |
| 393 | } |
| 394 | NanDataPathConfirmInd aidl_struct; |
| 395 | if (!aidl_struct_util::convertLegacyNanDataPathConfirmIndToAidl(msg, |
| 396 | &aidl_struct)) { |
| 397 | LOG(ERROR) << "Failed to convert nan capabilities response"; |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 402 | if (!callback->eventDataPathConfirm(aidl_struct).isOk()) { |
| 403 | LOG(ERROR) << "Failed to invoke the callback"; |
| 404 | } |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | callback_handlers.on_event_data_path_end = |
| 409 | [weak_ptr_this](const legacy_hal::NanDataPathEndInd& msg) { |
| 410 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 411 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 412 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 413 | return; |
| 414 | } |
| 415 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 416 | for (int i = 0; i < msg.num_ndp_instances; ++i) { |
| 417 | if (!callback->eventDataPathTerminated(msg.ndp_instance_id[i]).isOk()) { |
| 418 | LOG(ERROR) << "Failed to invoke the callback"; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | }; |
| 423 | |
| 424 | callback_handlers.on_event_beacon_sdf_payload = |
| 425 | [](const legacy_hal::NanBeaconSdfPayloadInd& /* msg */) { |
| 426 | LOG(ERROR) << "on_event_beacon_sdf_payload - should not be called"; |
| 427 | }; |
| 428 | |
| 429 | callback_handlers.on_event_range_request = [](const legacy_hal::NanRangeRequestInd& /* msg */) { |
| 430 | LOG(ERROR) << "on_event_range_request - should not be called"; |
| 431 | }; |
| 432 | |
| 433 | callback_handlers.on_event_range_report = [](const legacy_hal::NanRangeReportInd& /* msg */) { |
| 434 | LOG(ERROR) << "on_event_range_report - should not be called"; |
| 435 | }; |
| 436 | |
| 437 | callback_handlers.on_event_schedule_update = |
| 438 | [weak_ptr_this](const legacy_hal::NanDataPathScheduleUpdateInd& msg) { |
| 439 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 440 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 441 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 442 | return; |
| 443 | } |
| 444 | NanDataPathScheduleUpdateInd aidl_struct; |
| 445 | if (!aidl_struct_util::convertLegacyNanDataPathScheduleUpdateIndToAidl( |
| 446 | msg, &aidl_struct)) { |
| 447 | LOG(ERROR) << "Failed to convert nan capabilities response"; |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 452 | if (!callback->eventDataPathScheduleUpdate(aidl_struct).isOk()) { |
| 453 | LOG(ERROR) << "Failed to invoke the callback"; |
| 454 | } |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | legacy_hal::wifi_error legacy_status = |
| 459 | legacy_hal_.lock()->nanRegisterCallbackHandlers(ifname_, callback_handlers); |
| 460 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 461 | LOG(ERROR) << "Failed to register nan callbacks. Invalidating object"; |
| 462 | invalidate(); |
| 463 | } |
| 464 | |
| 465 | // Register for iface state toggle events. |
| 466 | iface_util::IfaceEventHandlers event_handlers = {}; |
| 467 | event_handlers.on_state_toggle_off_on = [weak_ptr_this](const std::string& /* iface_name */) { |
| 468 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 469 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 470 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 471 | return; |
| 472 | } |
| 473 | // Tell framework that NAN has been disabled. |
| 474 | NanStatus status = {NanStatusCode::UNSUPPORTED_CONCURRENCY_NAN_DISABLED, ""}; |
| 475 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 476 | if (!callback->eventDisabled(status).isOk()) { |
| 477 | LOG(ERROR) << "Failed to invoke the callback"; |
| 478 | } |
| 479 | } |
| 480 | }; |
| 481 | iface_util_.lock()->registerIfaceEventHandlers(ifname_, event_handlers); |
| 482 | } |
| 483 | |
| 484 | void WifiNanIface::setWeakPtr(std::weak_ptr<WifiNanIface> ptr) { |
| 485 | weak_ptr_this_ = ptr; |
| 486 | } |
| 487 | |
| 488 | void WifiNanIface::invalidate() { |
| 489 | if (!isValid()) { |
| 490 | return; |
| 491 | } |
| 492 | // send commands to HAL to actually disable and destroy interfaces |
| 493 | legacy_hal_.lock()->nanDisableRequest(ifname_, 0xFFFF); |
| 494 | legacy_hal_.lock()->nanDataInterfaceDelete(ifname_, 0xFFFE, "aware_data0"); |
| 495 | legacy_hal_.lock()->nanDataInterfaceDelete(ifname_, 0xFFFD, "aware_data1"); |
| 496 | iface_util_.lock()->unregisterIfaceEventHandlers(ifname_); |
| 497 | legacy_hal_.reset(); |
| 498 | event_cb_handler_.invalidate(); |
| 499 | is_valid_ = false; |
| 500 | if (is_dedicated_iface_) { |
| 501 | // If using a dedicated iface, set the iface down. |
| 502 | iface_util_.lock()->setUpState(ifname_, false); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | bool WifiNanIface::isValid() { |
| 507 | return is_valid_; |
| 508 | } |
| 509 | |
| 510 | std::string WifiNanIface::getName() { |
| 511 | return ifname_; |
| 512 | } |
| 513 | |
| 514 | std::set<std::shared_ptr<IWifiNanIfaceEventCallback>> WifiNanIface::getEventCallbacks() { |
| 515 | LOG(ERROR) << "Using original getEventCallbacks"; |
| 516 | return event_cb_handler_.getCallbacks(); |
| 517 | } |
| 518 | |
| 519 | ndk::ScopedAStatus WifiNanIface::getName(std::string* _aidl_return) { |
| 520 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 521 | &WifiNanIface::getNameInternal, _aidl_return); |
| 522 | } |
| 523 | |
| 524 | ndk::ScopedAStatus WifiNanIface::registerEventCallback( |
| 525 | const std::shared_ptr<IWifiNanIfaceEventCallback>& callback) { |
| 526 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 527 | &WifiNanIface::registerEventCallbackInternal, callback); |
| 528 | } |
| 529 | |
| 530 | ndk::ScopedAStatus WifiNanIface::getCapabilitiesRequest(char16_t in_cmdId) { |
| 531 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 532 | &WifiNanIface::getCapabilitiesRequestInternal, in_cmdId); |
| 533 | } |
| 534 | |
| 535 | ndk::ScopedAStatus WifiNanIface::enableRequest(char16_t in_cmdId, const NanEnableRequest& in_msg1, |
| 536 | const NanConfigRequestSupplemental& in_msg2) { |
| 537 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 538 | &WifiNanIface::enableRequestInternal, in_cmdId, in_msg1, in_msg2); |
| 539 | } |
| 540 | |
| 541 | ndk::ScopedAStatus WifiNanIface::configRequest(char16_t in_cmdId, const NanConfigRequest& in_msg1, |
| 542 | const NanConfigRequestSupplemental& in_msg2) { |
| 543 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 544 | &WifiNanIface::configRequestInternal, in_cmdId, in_msg1, in_msg2); |
| 545 | } |
| 546 | |
| 547 | ndk::ScopedAStatus WifiNanIface::disableRequest(char16_t in_cmdId) { |
| 548 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 549 | &WifiNanIface::disableRequestInternal, in_cmdId); |
| 550 | } |
| 551 | |
| 552 | ndk::ScopedAStatus WifiNanIface::startPublishRequest(char16_t in_cmdId, |
| 553 | const NanPublishRequest& in_msg) { |
| 554 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 555 | &WifiNanIface::startPublishRequestInternal, in_cmdId, in_msg); |
| 556 | } |
| 557 | |
| 558 | ndk::ScopedAStatus WifiNanIface::stopPublishRequest(char16_t in_cmdId, int8_t in_sessionId) { |
| 559 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 560 | &WifiNanIface::stopPublishRequestInternal, in_cmdId, in_sessionId); |
| 561 | } |
| 562 | |
| 563 | ndk::ScopedAStatus WifiNanIface::startSubscribeRequest(char16_t in_cmdId, |
| 564 | const NanSubscribeRequest& in_msg) { |
| 565 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 566 | &WifiNanIface::startSubscribeRequestInternal, in_cmdId, in_msg); |
| 567 | } |
| 568 | |
| 569 | ndk::ScopedAStatus WifiNanIface::stopSubscribeRequest(char16_t in_cmdId, int8_t in_sessionId) { |
| 570 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 571 | &WifiNanIface::stopSubscribeRequestInternal, in_cmdId, in_sessionId); |
| 572 | } |
| 573 | |
| 574 | ndk::ScopedAStatus WifiNanIface::transmitFollowupRequest(char16_t in_cmdId, |
| 575 | const NanTransmitFollowupRequest& in_msg) { |
| 576 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 577 | &WifiNanIface::transmitFollowupRequestInternal, in_cmdId, in_msg); |
| 578 | } |
| 579 | |
| 580 | ndk::ScopedAStatus WifiNanIface::createDataInterfaceRequest(char16_t in_cmdId, |
| 581 | const std::string& in_ifaceName) { |
| 582 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 583 | &WifiNanIface::createDataInterfaceRequestInternal, in_cmdId, |
| 584 | in_ifaceName); |
| 585 | } |
| 586 | |
| 587 | ndk::ScopedAStatus WifiNanIface::deleteDataInterfaceRequest(char16_t in_cmdId, |
| 588 | const std::string& in_ifaceName) { |
| 589 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 590 | &WifiNanIface::deleteDataInterfaceRequestInternal, in_cmdId, |
| 591 | in_ifaceName); |
| 592 | } |
| 593 | |
| 594 | ndk::ScopedAStatus WifiNanIface::initiateDataPathRequest(char16_t in_cmdId, |
| 595 | const NanInitiateDataPathRequest& in_msg) { |
| 596 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 597 | &WifiNanIface::initiateDataPathRequestInternal, in_cmdId, in_msg); |
| 598 | } |
| 599 | |
| 600 | ndk::ScopedAStatus WifiNanIface::respondToDataPathIndicationRequest( |
| 601 | char16_t in_cmdId, const NanRespondToDataPathIndicationRequest& in_msg) { |
| 602 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 603 | &WifiNanIface::respondToDataPathIndicationRequestInternal, in_cmdId, |
| 604 | in_msg); |
| 605 | } |
| 606 | |
| 607 | ndk::ScopedAStatus WifiNanIface::terminateDataPathRequest(char16_t in_cmdId, |
| 608 | int32_t in_ndpInstanceId) { |
| 609 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 610 | &WifiNanIface::terminateDataPathRequestInternal, in_cmdId, |
| 611 | in_ndpInstanceId); |
| 612 | } |
| 613 | |
| 614 | std::pair<std::string, ndk::ScopedAStatus> WifiNanIface::getNameInternal() { |
| 615 | return {ifname_, ndk::ScopedAStatus::ok()}; |
| 616 | } |
| 617 | |
| 618 | ndk::ScopedAStatus WifiNanIface::registerEventCallbackInternal( |
| 619 | const std::shared_ptr<IWifiNanIfaceEventCallback>& callback) { |
| 620 | if (!event_cb_handler_.addCallback(callback)) { |
| 621 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 622 | } |
| 623 | return ndk::ScopedAStatus::ok(); |
| 624 | } |
| 625 | |
| 626 | ndk::ScopedAStatus WifiNanIface::getCapabilitiesRequestInternal(char16_t cmd_id) { |
| 627 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->nanGetCapabilities(ifname_, cmd_id); |
| 628 | return createWifiStatusFromLegacyError(legacy_status); |
| 629 | } |
| 630 | |
| 631 | ndk::ScopedAStatus WifiNanIface::enableRequestInternal(char16_t cmd_id, |
| 632 | const NanEnableRequest& msg1, |
| 633 | const NanConfigRequestSupplemental& msg2) { |
| 634 | legacy_hal::NanEnableRequest legacy_msg; |
| 635 | if (!aidl_struct_util::convertAidlNanEnableRequestToLegacy(msg1, msg2, &legacy_msg)) { |
| 636 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 637 | } |
| 638 | legacy_hal::wifi_error legacy_status = |
| 639 | legacy_hal_.lock()->nanEnableRequest(ifname_, cmd_id, legacy_msg); |
| 640 | return createWifiStatusFromLegacyError(legacy_status); |
| 641 | } |
| 642 | |
| 643 | ndk::ScopedAStatus WifiNanIface::configRequestInternal(char16_t cmd_id, |
| 644 | const NanConfigRequest& msg1, |
| 645 | const NanConfigRequestSupplemental& msg2) { |
| 646 | legacy_hal::NanConfigRequest legacy_msg; |
| 647 | if (!aidl_struct_util::convertAidlNanConfigRequestToLegacy(msg1, msg2, &legacy_msg)) { |
| 648 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 649 | } |
| 650 | legacy_hal::wifi_error legacy_status = |
| 651 | legacy_hal_.lock()->nanConfigRequest(ifname_, cmd_id, legacy_msg); |
| 652 | return createWifiStatusFromLegacyError(legacy_status); |
| 653 | } |
| 654 | |
| 655 | ndk::ScopedAStatus WifiNanIface::disableRequestInternal(char16_t cmd_id) { |
| 656 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->nanDisableRequest(ifname_, cmd_id); |
| 657 | return createWifiStatusFromLegacyError(legacy_status); |
| 658 | } |
| 659 | |
| 660 | ndk::ScopedAStatus WifiNanIface::startPublishRequestInternal(char16_t cmd_id, |
| 661 | const NanPublishRequest& msg) { |
| 662 | legacy_hal::NanPublishRequest legacy_msg; |
| 663 | if (!aidl_struct_util::convertAidlNanPublishRequestToLegacy(msg, &legacy_msg)) { |
| 664 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 665 | } |
| 666 | legacy_hal::wifi_error legacy_status = |
| 667 | legacy_hal_.lock()->nanPublishRequest(ifname_, cmd_id, legacy_msg); |
| 668 | return createWifiStatusFromLegacyError(legacy_status); |
| 669 | } |
| 670 | |
| 671 | ndk::ScopedAStatus WifiNanIface::stopPublishRequestInternal(char16_t cmd_id, int8_t sessionId) { |
| 672 | legacy_hal::NanPublishCancelRequest legacy_msg; |
| 673 | legacy_msg.publish_id = sessionId; |
| 674 | legacy_hal::wifi_error legacy_status = |
| 675 | legacy_hal_.lock()->nanPublishCancelRequest(ifname_, cmd_id, legacy_msg); |
| 676 | return createWifiStatusFromLegacyError(legacy_status); |
| 677 | } |
| 678 | |
| 679 | ndk::ScopedAStatus WifiNanIface::startSubscribeRequestInternal(char16_t cmd_id, |
| 680 | const NanSubscribeRequest& msg) { |
| 681 | legacy_hal::NanSubscribeRequest legacy_msg; |
| 682 | if (!aidl_struct_util::convertAidlNanSubscribeRequestToLegacy(msg, &legacy_msg)) { |
| 683 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 684 | } |
| 685 | legacy_hal::wifi_error legacy_status = |
| 686 | legacy_hal_.lock()->nanSubscribeRequest(ifname_, cmd_id, legacy_msg); |
| 687 | return createWifiStatusFromLegacyError(legacy_status); |
| 688 | } |
| 689 | |
| 690 | ndk::ScopedAStatus WifiNanIface::stopSubscribeRequestInternal(char16_t cmd_id, int8_t sessionId) { |
| 691 | legacy_hal::NanSubscribeCancelRequest legacy_msg; |
| 692 | legacy_msg.subscribe_id = sessionId; |
| 693 | legacy_hal::wifi_error legacy_status = |
| 694 | legacy_hal_.lock()->nanSubscribeCancelRequest(ifname_, cmd_id, legacy_msg); |
| 695 | return createWifiStatusFromLegacyError(legacy_status); |
| 696 | } |
| 697 | |
| 698 | ndk::ScopedAStatus WifiNanIface::transmitFollowupRequestInternal( |
| 699 | char16_t cmd_id, const NanTransmitFollowupRequest& msg) { |
| 700 | legacy_hal::NanTransmitFollowupRequest legacy_msg; |
| 701 | if (!aidl_struct_util::convertAidlNanTransmitFollowupRequestToLegacy(msg, &legacy_msg)) { |
| 702 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 703 | } |
| 704 | legacy_hal::wifi_error legacy_status = |
| 705 | legacy_hal_.lock()->nanTransmitFollowupRequest(ifname_, cmd_id, legacy_msg); |
| 706 | return createWifiStatusFromLegacyError(legacy_status); |
| 707 | } |
| 708 | |
| 709 | ndk::ScopedAStatus WifiNanIface::createDataInterfaceRequestInternal(char16_t cmd_id, |
| 710 | const std::string& iface_name) { |
| 711 | legacy_hal::wifi_error legacy_status = |
| 712 | legacy_hal_.lock()->nanDataInterfaceCreate(ifname_, cmd_id, iface_name); |
| 713 | return createWifiStatusFromLegacyError(legacy_status); |
| 714 | } |
| 715 | ndk::ScopedAStatus WifiNanIface::deleteDataInterfaceRequestInternal(char16_t cmd_id, |
| 716 | const std::string& iface_name) { |
| 717 | legacy_hal::wifi_error legacy_status = |
| 718 | legacy_hal_.lock()->nanDataInterfaceDelete(ifname_, cmd_id, iface_name); |
| 719 | return createWifiStatusFromLegacyError(legacy_status); |
| 720 | } |
| 721 | ndk::ScopedAStatus WifiNanIface::initiateDataPathRequestInternal( |
| 722 | char16_t cmd_id, const NanInitiateDataPathRequest& msg) { |
| 723 | legacy_hal::NanDataPathInitiatorRequest legacy_msg; |
| 724 | if (!aidl_struct_util::convertAidlNanDataPathInitiatorRequestToLegacy(msg, &legacy_msg)) { |
| 725 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 726 | } |
| 727 | legacy_hal::wifi_error legacy_status = |
| 728 | legacy_hal_.lock()->nanDataRequestInitiator(ifname_, cmd_id, legacy_msg); |
| 729 | return createWifiStatusFromLegacyError(legacy_status); |
| 730 | } |
| 731 | ndk::ScopedAStatus WifiNanIface::respondToDataPathIndicationRequestInternal( |
| 732 | char16_t cmd_id, const NanRespondToDataPathIndicationRequest& msg) { |
| 733 | legacy_hal::NanDataPathIndicationResponse legacy_msg; |
| 734 | if (!aidl_struct_util::convertAidlNanDataPathIndicationResponseToLegacy(msg, &legacy_msg)) { |
| 735 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 736 | } |
| 737 | legacy_hal::wifi_error legacy_status = |
| 738 | legacy_hal_.lock()->nanDataIndicationResponse(ifname_, cmd_id, legacy_msg); |
| 739 | return createWifiStatusFromLegacyError(legacy_status); |
| 740 | } |
| 741 | ndk::ScopedAStatus WifiNanIface::terminateDataPathRequestInternal(char16_t cmd_id, |
| 742 | int32_t ndpInstanceId) { |
| 743 | legacy_hal::wifi_error legacy_status = |
| 744 | legacy_hal_.lock()->nanDataEnd(ifname_, cmd_id, ndpInstanceId); |
| 745 | return createWifiStatusFromLegacyError(legacy_status); |
| 746 | } |
| 747 | |
| 748 | } // namespace wifi |
| 749 | } // namespace hardware |
| 750 | } // namespace android |
| 751 | } // namespace aidl |