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