blob: ce6902da8a96b361a318bc1cc7089d1f4b6fb51f [file] [log] [blame]
Gabriel Birenf3262f92022-07-15 23:25:39 +00001/*
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
25namespace aidl {
26namespace android {
27namespace hardware {
28namespace wifi {
29using aidl_return_util::validateAndCall;
30
31WifiNanIface::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
40std::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
60void 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 }
Nate Jiang38e8db52022-12-02 17:30:27 -0800208 case legacy_hal::NAN_PAIRING_INITIATOR_RESPONSE: {
209 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
210 if (!callback->notifyInitiatePairingResponse(
211 id, nanStatus,
212 msg.body.pairing_request_response.paring_instance_id)
213 .isOk()) {
214 LOG(ERROR) << "Failed to invoke the callback";
215 }
216 }
217 break;
218 }
219 case legacy_hal::NAN_PAIRING_RESPONDER_RESPONSE: {
220 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
221 if (!callback->notifyRespondToPairingIndicationResponse(id, nanStatus).isOk()) {
222 LOG(ERROR) << "Failed to invoke the callback";
223 }
224 }
225 break;
226 }
227 case legacy_hal::NAN_BOOTSTRAPPING_INITIATOR_RESPONSE: {
228 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
229 if (!callback->notifyInitiateBootstrappingResponse(
230 id, nanStatus,
231 msg.body.bootstrapping_request_response
232 .bootstrapping_instance_id)
233 .isOk()) {
234 LOG(ERROR) << "Failed to invoke the callback";
235 }
236 }
237 break;
238 }
239 case legacy_hal::NAN_BOOTSTRAPPING_RESPONDER_RESPONSE: {
240 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
241 if (!callback->notifyRespondToBootstrappingIndicationResponse(id, nanStatus)
242 .isOk()) {
243 LOG(ERROR) << "Failed to invoke the callback";
244 }
245 }
246 break;
247 }
Gabriel Birenf3262f92022-07-15 23:25:39 +0000248 case legacy_hal::NAN_RESPONSE_BEACON_SDF_PAYLOAD:
249 /* fall through */
250 case legacy_hal::NAN_RESPONSE_TCA:
251 /* fall through */
252 case legacy_hal::NAN_RESPONSE_STATS:
253 /* fall through */
254 case legacy_hal::NAN_RESPONSE_ERROR:
255 /* fall through */
256 default:
257 LOG(ERROR) << "Unknown or unhandled response type: " << msg.response_type;
258 return;
259 }
260 };
261
262 callback_handlers.on_event_disc_eng_event = [weak_ptr_this](
263 const legacy_hal::NanDiscEngEventInd& msg) {
264 const auto shared_ptr_this = weak_ptr_this.lock();
265 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
266 LOG(ERROR) << "Callback invoked on an invalid object";
267 return;
268 }
269 NanClusterEventInd aidl_struct;
270 // event types defined identically - hence can be cast
271 aidl_struct.eventType = (NanClusterEventType)msg.event_type;
272 aidl_struct.addr = std::array<uint8_t, 6>();
273 std::copy(msg.data.mac_addr.addr, msg.data.mac_addr.addr + 6, std::begin(aidl_struct.addr));
274
275 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
276 if (!callback->eventClusterEvent(aidl_struct).isOk()) {
277 LOG(ERROR) << "Failed to invoke the callback";
278 }
279 }
280 };
281
282 callback_handlers.on_event_disabled = [weak_ptr_this](const legacy_hal::NanDisabledInd& msg) {
283 const auto shared_ptr_this = weak_ptr_this.lock();
284 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
285 LOG(ERROR) << "Callback invoked on an invalid object";
286 return;
287 }
288 NanStatus status;
289 aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason, sizeof(msg.nan_reason),
290 &status);
291
292 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
293 if (!callback->eventDisabled(status).isOk()) {
294 LOG(ERROR) << "Failed to invoke the callback";
295 }
296 }
297 };
298
299 callback_handlers.on_event_publish_terminated =
300 [weak_ptr_this](const legacy_hal::NanPublishTerminatedInd& msg) {
301 const auto shared_ptr_this = weak_ptr_this.lock();
302 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
303 LOG(ERROR) << "Callback invoked on an invalid object";
304 return;
305 }
306 NanStatus status;
307 aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason,
308 sizeof(msg.nan_reason), &status);
309
310 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
311 if (!callback->eventPublishTerminated(msg.publish_id, status).isOk()) {
312 LOG(ERROR) << "Failed to invoke the callback";
313 }
314 }
315 };
316
317 callback_handlers.on_event_subscribe_terminated =
318 [weak_ptr_this](const legacy_hal::NanSubscribeTerminatedInd& msg) {
319 const auto shared_ptr_this = weak_ptr_this.lock();
320 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
321 LOG(ERROR) << "Callback invoked on an invalid object";
322 return;
323 }
324 NanStatus status;
325 aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason,
326 sizeof(msg.nan_reason), &status);
327
328 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
329 if (!callback->eventSubscribeTerminated(msg.subscribe_id, status).isOk()) {
330 LOG(ERROR) << "Failed to invoke the callback";
331 }
332 }
333 };
334
335 callback_handlers.on_event_match = [weak_ptr_this](const legacy_hal::NanMatchInd& msg) {
336 const auto shared_ptr_this = weak_ptr_this.lock();
337 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
338 LOG(ERROR) << "Callback invoked on an invalid object";
339 return;
340 }
341 NanMatchInd aidl_struct;
342 if (!aidl_struct_util::convertLegacyNanMatchIndToAidl(msg, &aidl_struct)) {
343 LOG(ERROR) << "Failed to convert nan capabilities response";
344 return;
345 }
346
347 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
348 if (!callback->eventMatch(aidl_struct).isOk()) {
349 LOG(ERROR) << "Failed to invoke the callback";
350 }
351 }
352 };
353
354 callback_handlers.on_event_match_expired = [weak_ptr_this](
355 const legacy_hal::NanMatchExpiredInd& msg) {
356 const auto shared_ptr_this = weak_ptr_this.lock();
357 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
358 LOG(ERROR) << "Callback invoked on an invalid object";
359 return;
360 }
361 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
362 if (!callback->eventMatchExpired(msg.publish_subscribe_id, msg.requestor_instance_id)
363 .isOk()) {
364 LOG(ERROR) << "Failed to invoke the callback";
365 }
366 }
367 };
368
369 callback_handlers.on_event_followup = [weak_ptr_this](const legacy_hal::NanFollowupInd& msg) {
370 const auto shared_ptr_this = weak_ptr_this.lock();
371 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
372 LOG(ERROR) << "Callback invoked on an invalid object";
373 return;
374 }
375 NanFollowupReceivedInd aidl_struct;
376 if (!aidl_struct_util::convertLegacyNanFollowupIndToAidl(msg, &aidl_struct)) {
377 LOG(ERROR) << "Failed to convert nan capabilities response";
378 return;
379 }
380
381 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
382 if (!callback->eventFollowupReceived(aidl_struct).isOk()) {
383 LOG(ERROR) << "Failed to invoke the callback";
384 }
385 }
386 };
387
388 callback_handlers.on_event_transmit_follow_up =
389 [weak_ptr_this](const legacy_hal::NanTransmitFollowupInd& msg) {
390 const auto shared_ptr_this = weak_ptr_this.lock();
391 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
392 LOG(ERROR) << "Callback invoked on an invalid object";
393 return;
394 }
395 NanStatus status;
396 aidl_struct_util::convertToNanStatus(msg.reason, msg.nan_reason,
397 sizeof(msg.nan_reason), &status);
398
399 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
400 if (!callback->eventTransmitFollowup(msg.id, status).isOk()) {
401 LOG(ERROR) << "Failed to invoke the callback";
402 }
403 }
404 };
405
406 callback_handlers.on_event_data_path_request =
407 [weak_ptr_this](const legacy_hal::NanDataPathRequestInd& msg) {
408 const auto shared_ptr_this = weak_ptr_this.lock();
409 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
410 LOG(ERROR) << "Callback invoked on an invalid object";
411 return;
412 }
413 NanDataPathRequestInd aidl_struct;
414 if (!aidl_struct_util::convertLegacyNanDataPathRequestIndToAidl(msg,
415 &aidl_struct)) {
416 LOG(ERROR) << "Failed to convert nan capabilities response";
417 return;
418 }
419
420 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
421 if (!callback->eventDataPathRequest(aidl_struct).isOk()) {
422 LOG(ERROR) << "Failed to invoke the callback";
423 }
424 }
425 };
426
427 callback_handlers.on_event_data_path_confirm =
428 [weak_ptr_this](const legacy_hal::NanDataPathConfirmInd& msg) {
429 const auto shared_ptr_this = weak_ptr_this.lock();
430 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
431 LOG(ERROR) << "Callback invoked on an invalid object";
432 return;
433 }
434 NanDataPathConfirmInd aidl_struct;
435 if (!aidl_struct_util::convertLegacyNanDataPathConfirmIndToAidl(msg,
436 &aidl_struct)) {
437 LOG(ERROR) << "Failed to convert nan capabilities response";
438 return;
439 }
440
441 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
442 if (!callback->eventDataPathConfirm(aidl_struct).isOk()) {
443 LOG(ERROR) << "Failed to invoke the callback";
444 }
445 }
446 };
447
448 callback_handlers.on_event_data_path_end =
449 [weak_ptr_this](const legacy_hal::NanDataPathEndInd& msg) {
450 const auto shared_ptr_this = weak_ptr_this.lock();
451 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
452 LOG(ERROR) << "Callback invoked on an invalid object";
453 return;
454 }
455 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
456 for (int i = 0; i < msg.num_ndp_instances; ++i) {
457 if (!callback->eventDataPathTerminated(msg.ndp_instance_id[i]).isOk()) {
458 LOG(ERROR) << "Failed to invoke the callback";
459 }
460 }
461 }
462 };
463
Nate Jiang38e8db52022-12-02 17:30:27 -0800464 callback_handlers.on_event_pairing_request =
465 [weak_ptr_this](const legacy_hal::NanPairingRequestInd& msg) {
466 const auto shared_ptr_this = weak_ptr_this.lock();
467 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
468 LOG(ERROR) << "Callback invoked on an invalid object";
469 return;
470 }
471 NanPairingRequestInd aidl_struct;
472 if (!aidl_struct_util::convertLegacyNanPairingRequestIndToAidl(msg, &aidl_struct)) {
473 LOG(ERROR) << "Failed to convert nan capabilities response";
474 return;
475 }
476
477 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
478 if (!callback->eventPairingRequest(aidl_struct).isOk()) {
479 LOG(ERROR) << "Failed to invoke the callback";
480 }
481 }
482 };
483 callback_handlers.on_event_pairing_confirm =
484 [weak_ptr_this](const legacy_hal::NanPairingConfirmInd& msg) {
485 const auto shared_ptr_this = weak_ptr_this.lock();
486 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
487 LOG(ERROR) << "Callback invoked on an invalid object";
488 return;
489 }
490 NanPairingConfirmInd aidl_struct;
491 if (!aidl_struct_util::convertLegacyNanPairingConfirmIndToAidl(msg, &aidl_struct)) {
492 LOG(ERROR) << "Failed to convert nan capabilities response";
493 return;
494 }
495
496 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
497 if (!callback->eventPairingConfirm(aidl_struct).isOk()) {
498 LOG(ERROR) << "Failed to invoke the callback";
499 }
500 }
501 };
502 callback_handlers.on_event_bootstrapping_request =
503 [weak_ptr_this](const legacy_hal::NanBootstrappingRequestInd& msg) {
504 const auto shared_ptr_this = weak_ptr_this.lock();
505 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
506 LOG(ERROR) << "Callback invoked on an invalid object";
507 return;
508 }
509 NanBootstrappingRequestInd aidl_struct;
510 if (!aidl_struct_util::convertLegacyNanBootstrappingRequestIndToAidl(
511 msg, &aidl_struct)) {
512 LOG(ERROR) << "Failed to convert nan capabilities response";
513 return;
514 }
515
516 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
517 if (!callback->eventBootstrappingRequest(aidl_struct).isOk()) {
518 LOG(ERROR) << "Failed to invoke the callback";
519 }
520 }
521 };
522 callback_handlers.on_event_bootstrapping_confirm =
523 [weak_ptr_this](const legacy_hal::NanBootstrappingConfirmInd& msg) {
524 const auto shared_ptr_this = weak_ptr_this.lock();
525 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
526 LOG(ERROR) << "Callback invoked on an invalid object";
527 return;
528 }
529 NanBootstrappingConfirmInd aidl_struct;
530 if (!aidl_struct_util::convertLegacyNanBootstrappingConfirmIndToAidl(
531 msg, &aidl_struct)) {
532 LOG(ERROR) << "Failed to convert nan capabilities response";
533 return;
534 }
535
536 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
537 if (!callback->eventBootstrappingConfirm(aidl_struct).isOk()) {
538 LOG(ERROR) << "Failed to invoke the callback";
539 }
540 }
541 };
542
Gabriel Birenf3262f92022-07-15 23:25:39 +0000543 callback_handlers.on_event_beacon_sdf_payload =
544 [](const legacy_hal::NanBeaconSdfPayloadInd& /* msg */) {
545 LOG(ERROR) << "on_event_beacon_sdf_payload - should not be called";
546 };
547
548 callback_handlers.on_event_range_request = [](const legacy_hal::NanRangeRequestInd& /* msg */) {
549 LOG(ERROR) << "on_event_range_request - should not be called";
550 };
551
552 callback_handlers.on_event_range_report = [](const legacy_hal::NanRangeReportInd& /* msg */) {
553 LOG(ERROR) << "on_event_range_report - should not be called";
554 };
555
556 callback_handlers.on_event_schedule_update =
557 [weak_ptr_this](const legacy_hal::NanDataPathScheduleUpdateInd& msg) {
558 const auto shared_ptr_this = weak_ptr_this.lock();
559 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
560 LOG(ERROR) << "Callback invoked on an invalid object";
561 return;
562 }
563 NanDataPathScheduleUpdateInd aidl_struct;
564 if (!aidl_struct_util::convertLegacyNanDataPathScheduleUpdateIndToAidl(
565 msg, &aidl_struct)) {
566 LOG(ERROR) << "Failed to convert nan capabilities response";
567 return;
568 }
569
570 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
571 if (!callback->eventDataPathScheduleUpdate(aidl_struct).isOk()) {
572 LOG(ERROR) << "Failed to invoke the callback";
573 }
574 }
575 };
576
577 legacy_hal::wifi_error legacy_status =
578 legacy_hal_.lock()->nanRegisterCallbackHandlers(ifname_, callback_handlers);
579 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
580 LOG(ERROR) << "Failed to register nan callbacks. Invalidating object";
581 invalidate();
582 }
583
584 // Register for iface state toggle events.
585 iface_util::IfaceEventHandlers event_handlers = {};
586 event_handlers.on_state_toggle_off_on = [weak_ptr_this](const std::string& /* iface_name */) {
587 const auto shared_ptr_this = weak_ptr_this.lock();
588 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
589 LOG(ERROR) << "Callback invoked on an invalid object";
590 return;
591 }
592 // Tell framework that NAN has been disabled.
593 NanStatus status = {NanStatusCode::UNSUPPORTED_CONCURRENCY_NAN_DISABLED, ""};
594 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
595 if (!callback->eventDisabled(status).isOk()) {
596 LOG(ERROR) << "Failed to invoke the callback";
597 }
598 }
599 };
600 iface_util_.lock()->registerIfaceEventHandlers(ifname_, event_handlers);
601}
602
603void WifiNanIface::setWeakPtr(std::weak_ptr<WifiNanIface> ptr) {
604 weak_ptr_this_ = ptr;
605}
606
607void WifiNanIface::invalidate() {
608 if (!isValid()) {
609 return;
610 }
611 // send commands to HAL to actually disable and destroy interfaces
612 legacy_hal_.lock()->nanDisableRequest(ifname_, 0xFFFF);
613 legacy_hal_.lock()->nanDataInterfaceDelete(ifname_, 0xFFFE, "aware_data0");
614 legacy_hal_.lock()->nanDataInterfaceDelete(ifname_, 0xFFFD, "aware_data1");
615 iface_util_.lock()->unregisterIfaceEventHandlers(ifname_);
616 legacy_hal_.reset();
617 event_cb_handler_.invalidate();
618 is_valid_ = false;
619 if (is_dedicated_iface_) {
620 // If using a dedicated iface, set the iface down.
621 iface_util_.lock()->setUpState(ifname_, false);
622 }
623}
624
625bool WifiNanIface::isValid() {
626 return is_valid_;
627}
628
629std::string WifiNanIface::getName() {
630 return ifname_;
631}
632
633std::set<std::shared_ptr<IWifiNanIfaceEventCallback>> WifiNanIface::getEventCallbacks() {
634 LOG(ERROR) << "Using original getEventCallbacks";
635 return event_cb_handler_.getCallbacks();
636}
637
638ndk::ScopedAStatus WifiNanIface::getName(std::string* _aidl_return) {
639 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
640 &WifiNanIface::getNameInternal, _aidl_return);
641}
642
643ndk::ScopedAStatus WifiNanIface::registerEventCallback(
644 const std::shared_ptr<IWifiNanIfaceEventCallback>& callback) {
645 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
646 &WifiNanIface::registerEventCallbackInternal, callback);
647}
648
649ndk::ScopedAStatus WifiNanIface::getCapabilitiesRequest(char16_t in_cmdId) {
650 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
651 &WifiNanIface::getCapabilitiesRequestInternal, in_cmdId);
652}
653
654ndk::ScopedAStatus WifiNanIface::enableRequest(char16_t in_cmdId, const NanEnableRequest& in_msg1,
655 const NanConfigRequestSupplemental& in_msg2) {
656 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
657 &WifiNanIface::enableRequestInternal, in_cmdId, in_msg1, in_msg2);
658}
659
660ndk::ScopedAStatus WifiNanIface::configRequest(char16_t in_cmdId, const NanConfigRequest& in_msg1,
661 const NanConfigRequestSupplemental& in_msg2) {
662 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
663 &WifiNanIface::configRequestInternal, in_cmdId, in_msg1, in_msg2);
664}
665
666ndk::ScopedAStatus WifiNanIface::disableRequest(char16_t in_cmdId) {
667 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
668 &WifiNanIface::disableRequestInternal, in_cmdId);
669}
670
671ndk::ScopedAStatus WifiNanIface::startPublishRequest(char16_t in_cmdId,
672 const NanPublishRequest& in_msg) {
673 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
674 &WifiNanIface::startPublishRequestInternal, in_cmdId, in_msg);
675}
676
677ndk::ScopedAStatus WifiNanIface::stopPublishRequest(char16_t in_cmdId, int8_t in_sessionId) {
678 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
679 &WifiNanIface::stopPublishRequestInternal, in_cmdId, in_sessionId);
680}
681
682ndk::ScopedAStatus WifiNanIface::startSubscribeRequest(char16_t in_cmdId,
683 const NanSubscribeRequest& in_msg) {
684 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
685 &WifiNanIface::startSubscribeRequestInternal, in_cmdId, in_msg);
686}
687
688ndk::ScopedAStatus WifiNanIface::stopSubscribeRequest(char16_t in_cmdId, int8_t in_sessionId) {
689 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
690 &WifiNanIface::stopSubscribeRequestInternal, in_cmdId, in_sessionId);
691}
692
693ndk::ScopedAStatus WifiNanIface::transmitFollowupRequest(char16_t in_cmdId,
694 const NanTransmitFollowupRequest& in_msg) {
695 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
696 &WifiNanIface::transmitFollowupRequestInternal, in_cmdId, in_msg);
697}
698
699ndk::ScopedAStatus WifiNanIface::createDataInterfaceRequest(char16_t in_cmdId,
700 const std::string& in_ifaceName) {
701 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
702 &WifiNanIface::createDataInterfaceRequestInternal, in_cmdId,
703 in_ifaceName);
704}
705
706ndk::ScopedAStatus WifiNanIface::deleteDataInterfaceRequest(char16_t in_cmdId,
707 const std::string& in_ifaceName) {
708 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
709 &WifiNanIface::deleteDataInterfaceRequestInternal, in_cmdId,
710 in_ifaceName);
711}
712
713ndk::ScopedAStatus WifiNanIface::initiateDataPathRequest(char16_t in_cmdId,
714 const NanInitiateDataPathRequest& in_msg) {
715 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
716 &WifiNanIface::initiateDataPathRequestInternal, in_cmdId, in_msg);
717}
718
719ndk::ScopedAStatus WifiNanIface::respondToDataPathIndicationRequest(
720 char16_t in_cmdId, const NanRespondToDataPathIndicationRequest& in_msg) {
721 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
722 &WifiNanIface::respondToDataPathIndicationRequestInternal, in_cmdId,
723 in_msg);
724}
725
726ndk::ScopedAStatus WifiNanIface::terminateDataPathRequest(char16_t in_cmdId,
727 int32_t in_ndpInstanceId) {
728 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
729 &WifiNanIface::terminateDataPathRequestInternal, in_cmdId,
730 in_ndpInstanceId);
731}
732
Nate Jiang38e8db52022-12-02 17:30:27 -0800733ndk::ScopedAStatus WifiNanIface::initiatePairingRequest(char16_t in_cmdId,
734 const NanPairingRequest& in_msg) {
735 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
736 &WifiNanIface::initiatePairingRequestInternal, in_cmdId, in_msg);
737}
738
739ndk::ScopedAStatus WifiNanIface::respondToPairingIndicationRequest(
740 char16_t in_cmdId, const NanRespondToPairingIndicationRequest& in_msg) {
741 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
742 &WifiNanIface::respondToPairingIndicationRequestInternal, in_cmdId,
743 in_msg);
744}
745
746ndk::ScopedAStatus WifiNanIface::initiateBootstrappingRequest(
747 char16_t in_cmdId, const NanBootstrappingRequest& in_msg) {
748 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
749 &WifiNanIface::initiateBootstrappingRequestInternal, in_cmdId, in_msg);
750}
751
752ndk::ScopedAStatus WifiNanIface::respondToBootstrappingIndicationRequest(
753 char16_t in_cmdId, const NanBootstrappingResponse& in_msg) {
754 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
755 &WifiNanIface::respondToBootstrappingIndicationRequestInternal, in_cmdId,
756 in_msg);
757}
758
Gabriel Birenf3262f92022-07-15 23:25:39 +0000759std::pair<std::string, ndk::ScopedAStatus> WifiNanIface::getNameInternal() {
760 return {ifname_, ndk::ScopedAStatus::ok()};
761}
762
763ndk::ScopedAStatus WifiNanIface::registerEventCallbackInternal(
764 const std::shared_ptr<IWifiNanIfaceEventCallback>& callback) {
765 if (!event_cb_handler_.addCallback(callback)) {
766 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
767 }
768 return ndk::ScopedAStatus::ok();
769}
770
771ndk::ScopedAStatus WifiNanIface::getCapabilitiesRequestInternal(char16_t cmd_id) {
772 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->nanGetCapabilities(ifname_, cmd_id);
773 return createWifiStatusFromLegacyError(legacy_status);
774}
775
776ndk::ScopedAStatus WifiNanIface::enableRequestInternal(char16_t cmd_id,
777 const NanEnableRequest& msg1,
778 const NanConfigRequestSupplemental& msg2) {
779 legacy_hal::NanEnableRequest legacy_msg;
780 if (!aidl_struct_util::convertAidlNanEnableRequestToLegacy(msg1, msg2, &legacy_msg)) {
781 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
782 }
783 legacy_hal::wifi_error legacy_status =
784 legacy_hal_.lock()->nanEnableRequest(ifname_, cmd_id, legacy_msg);
785 return createWifiStatusFromLegacyError(legacy_status);
786}
787
788ndk::ScopedAStatus WifiNanIface::configRequestInternal(char16_t cmd_id,
789 const NanConfigRequest& msg1,
790 const NanConfigRequestSupplemental& msg2) {
791 legacy_hal::NanConfigRequest legacy_msg;
792 if (!aidl_struct_util::convertAidlNanConfigRequestToLegacy(msg1, msg2, &legacy_msg)) {
793 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
794 }
795 legacy_hal::wifi_error legacy_status =
796 legacy_hal_.lock()->nanConfigRequest(ifname_, cmd_id, legacy_msg);
797 return createWifiStatusFromLegacyError(legacy_status);
798}
799
800ndk::ScopedAStatus WifiNanIface::disableRequestInternal(char16_t cmd_id) {
801 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->nanDisableRequest(ifname_, cmd_id);
802 return createWifiStatusFromLegacyError(legacy_status);
803}
804
805ndk::ScopedAStatus WifiNanIface::startPublishRequestInternal(char16_t cmd_id,
806 const NanPublishRequest& msg) {
807 legacy_hal::NanPublishRequest legacy_msg;
808 if (!aidl_struct_util::convertAidlNanPublishRequestToLegacy(msg, &legacy_msg)) {
809 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
810 }
811 legacy_hal::wifi_error legacy_status =
812 legacy_hal_.lock()->nanPublishRequest(ifname_, cmd_id, legacy_msg);
813 return createWifiStatusFromLegacyError(legacy_status);
814}
815
816ndk::ScopedAStatus WifiNanIface::stopPublishRequestInternal(char16_t cmd_id, int8_t sessionId) {
817 legacy_hal::NanPublishCancelRequest legacy_msg;
818 legacy_msg.publish_id = sessionId;
819 legacy_hal::wifi_error legacy_status =
820 legacy_hal_.lock()->nanPublishCancelRequest(ifname_, cmd_id, legacy_msg);
821 return createWifiStatusFromLegacyError(legacy_status);
822}
823
824ndk::ScopedAStatus WifiNanIface::startSubscribeRequestInternal(char16_t cmd_id,
825 const NanSubscribeRequest& msg) {
826 legacy_hal::NanSubscribeRequest legacy_msg;
827 if (!aidl_struct_util::convertAidlNanSubscribeRequestToLegacy(msg, &legacy_msg)) {
828 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
829 }
830 legacy_hal::wifi_error legacy_status =
831 legacy_hal_.lock()->nanSubscribeRequest(ifname_, cmd_id, legacy_msg);
832 return createWifiStatusFromLegacyError(legacy_status);
833}
834
835ndk::ScopedAStatus WifiNanIface::stopSubscribeRequestInternal(char16_t cmd_id, int8_t sessionId) {
836 legacy_hal::NanSubscribeCancelRequest legacy_msg;
837 legacy_msg.subscribe_id = sessionId;
838 legacy_hal::wifi_error legacy_status =
839 legacy_hal_.lock()->nanSubscribeCancelRequest(ifname_, cmd_id, legacy_msg);
840 return createWifiStatusFromLegacyError(legacy_status);
841}
842
843ndk::ScopedAStatus WifiNanIface::transmitFollowupRequestInternal(
844 char16_t cmd_id, const NanTransmitFollowupRequest& msg) {
845 legacy_hal::NanTransmitFollowupRequest legacy_msg;
846 if (!aidl_struct_util::convertAidlNanTransmitFollowupRequestToLegacy(msg, &legacy_msg)) {
847 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
848 }
849 legacy_hal::wifi_error legacy_status =
850 legacy_hal_.lock()->nanTransmitFollowupRequest(ifname_, cmd_id, legacy_msg);
851 return createWifiStatusFromLegacyError(legacy_status);
852}
853
854ndk::ScopedAStatus WifiNanIface::createDataInterfaceRequestInternal(char16_t cmd_id,
855 const std::string& iface_name) {
856 legacy_hal::wifi_error legacy_status =
857 legacy_hal_.lock()->nanDataInterfaceCreate(ifname_, cmd_id, iface_name);
858 return createWifiStatusFromLegacyError(legacy_status);
859}
860ndk::ScopedAStatus WifiNanIface::deleteDataInterfaceRequestInternal(char16_t cmd_id,
861 const std::string& iface_name) {
862 legacy_hal::wifi_error legacy_status =
863 legacy_hal_.lock()->nanDataInterfaceDelete(ifname_, cmd_id, iface_name);
864 return createWifiStatusFromLegacyError(legacy_status);
865}
866ndk::ScopedAStatus WifiNanIface::initiateDataPathRequestInternal(
867 char16_t cmd_id, const NanInitiateDataPathRequest& msg) {
868 legacy_hal::NanDataPathInitiatorRequest legacy_msg;
869 if (!aidl_struct_util::convertAidlNanDataPathInitiatorRequestToLegacy(msg, &legacy_msg)) {
870 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
871 }
872 legacy_hal::wifi_error legacy_status =
873 legacy_hal_.lock()->nanDataRequestInitiator(ifname_, cmd_id, legacy_msg);
874 return createWifiStatusFromLegacyError(legacy_status);
875}
876ndk::ScopedAStatus WifiNanIface::respondToDataPathIndicationRequestInternal(
877 char16_t cmd_id, const NanRespondToDataPathIndicationRequest& msg) {
878 legacy_hal::NanDataPathIndicationResponse legacy_msg;
879 if (!aidl_struct_util::convertAidlNanDataPathIndicationResponseToLegacy(msg, &legacy_msg)) {
880 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
881 }
882 legacy_hal::wifi_error legacy_status =
883 legacy_hal_.lock()->nanDataIndicationResponse(ifname_, cmd_id, legacy_msg);
884 return createWifiStatusFromLegacyError(legacy_status);
885}
886ndk::ScopedAStatus WifiNanIface::terminateDataPathRequestInternal(char16_t cmd_id,
887 int32_t ndpInstanceId) {
888 legacy_hal::wifi_error legacy_status =
889 legacy_hal_.lock()->nanDataEnd(ifname_, cmd_id, ndpInstanceId);
890 return createWifiStatusFromLegacyError(legacy_status);
891}
Nate Jiang38e8db52022-12-02 17:30:27 -0800892ndk::ScopedAStatus WifiNanIface::initiatePairingRequestInternal(char16_t cmd_id,
893 const NanPairingRequest& msg) {
894 legacy_hal::NanPairingRequest legacy_msg;
895 if (!aidl_struct_util::convertAidlNanPairingInitiatorRequestToLegacy(msg, &legacy_msg)) {
896 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
897 }
898 legacy_hal::wifi_error legacy_status =
899 legacy_hal_.lock()->nanPairingRequest(ifname_, cmd_id, legacy_msg);
900 return createWifiStatusFromLegacyError(legacy_status);
901}
902ndk::ScopedAStatus WifiNanIface::respondToPairingIndicationRequestInternal(
903 char16_t cmd_id, const NanRespondToPairingIndicationRequest& msg) {
904 legacy_hal::NanPairingIndicationResponse legacy_msg;
905 if (!aidl_struct_util::convertAidlNanPairingIndicationResponseToLegacy(msg, &legacy_msg)) {
906 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
907 }
908 legacy_hal::wifi_error legacy_status =
909 legacy_hal_.lock()->nanPairingIndicationResponse(ifname_, cmd_id, legacy_msg);
910 return createWifiStatusFromLegacyError(legacy_status);
911}
912ndk::ScopedAStatus WifiNanIface::initiateBootstrappingRequestInternal(
913 char16_t cmd_id, const NanBootstrappingRequest& msg) {
914 legacy_hal::NanBootstrappingRequest legacy_msg;
915 if (!aidl_struct_util::convertAidlNanBootstrappingInitiatorRequestToLegacy(msg, &legacy_msg)) {
916 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
917 }
918 legacy_hal::wifi_error legacy_status =
919 legacy_hal_.lock()->nanBootstrappingRequest(ifname_, cmd_id, legacy_msg);
920 return createWifiStatusFromLegacyError(legacy_status);
921}
922ndk::ScopedAStatus WifiNanIface::respondToBootstrappingIndicationRequestInternal(
923 char16_t cmd_id, const NanBootstrappingResponse& msg) {
924 legacy_hal::NanBootstrappingIndicationResponse legacy_msg;
925 if (!aidl_struct_util::convertAidlNanBootstrappingIndicationResponseToLegacy(msg,
926 &legacy_msg)) {
927 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
928 }
929 legacy_hal::wifi_error legacy_status =
930 legacy_hal_.lock()->nanBootstrappingIndicationResponse(ifname_, cmd_id, legacy_msg);
931 return createWifiStatusFromLegacyError(legacy_status);
932}
Gabriel Birenf3262f92022-07-15 23:25:39 +0000933
934} // namespace wifi
935} // namespace hardware
936} // namespace android
937} // namespace aidl