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 "wificond/client_interface_binder.h"
18 
19 #include <algorithm>
20 #include <vector>
21 
22 #include <linux/if_ether.h>
23 
24 #include <android-base/logging.h>
25 
26 #include <binder/Status.h>
27 
28 #include "wificond/client_interface_impl.h"
29 
30 using android::binder::Status;
31 using android::net::wifi::ISendMgmtFrameEvent;
32 using android::net::wifi::IWifiScannerImpl;
33 using std::vector;
34 
35 namespace android {
36 namespace wificond {
37 
ClientInterfaceBinder(ClientInterfaceImpl * impl)38 ClientInterfaceBinder::ClientInterfaceBinder(ClientInterfaceImpl* impl)
39     : impl_(impl) {
40 }
41 
~ClientInterfaceBinder()42 ClientInterfaceBinder::~ClientInterfaceBinder() {
43 }
44 
getPacketCounters(vector<int32_t> * out_packet_counters)45 Status ClientInterfaceBinder::getPacketCounters(
46     vector<int32_t>* out_packet_counters) {
47   if (impl_ == nullptr) {
48     return Status::ok();
49   }
50   impl_->GetPacketCounters(out_packet_counters);
51   return Status::ok();
52 }
53 
signalPoll(vector<int32_t> * out_signal_poll_results)54 Status ClientInterfaceBinder::signalPoll(
55     vector<int32_t>* out_signal_poll_results) {
56   if (impl_ == nullptr) {
57     return Status::ok();
58   }
59   impl_->SignalPoll(out_signal_poll_results);
60   return Status::ok();
61 }
62 
getMacAddress(vector<uint8_t> * out_mac_address)63 Status ClientInterfaceBinder::getMacAddress(vector<uint8_t>* out_mac_address) {
64   if (impl_ == nullptr) {
65     return Status::ok();
66   }
67   const std::array<uint8_t, ETH_ALEN>& mac = impl_->GetMacAddress();
68   *out_mac_address = vector<uint8_t>(mac.begin(), mac.end());
69   return Status::ok();
70 }
71 
getInterfaceName(std::string * out_name)72 Status ClientInterfaceBinder::getInterfaceName(std::string* out_name) {
73   if (impl_ == nullptr) {
74     return Status::ok();
75   }
76   *out_name = impl_->GetInterfaceName();
77   return Status::ok();
78 }
79 
getWifiScannerImpl(sp<IWifiScannerImpl> * out_wifi_scanner_impl)80 Status ClientInterfaceBinder::getWifiScannerImpl(
81     sp<IWifiScannerImpl>* out_wifi_scanner_impl) {
82   if (impl_ == nullptr) {
83     *out_wifi_scanner_impl = nullptr;
84     return Status::ok();
85   }
86   *out_wifi_scanner_impl = impl_->GetScanner();
87   return Status::ok();
88 }
89 
90 
setMacAddress(const vector<uint8_t> & mac,bool * success)91 Status ClientInterfaceBinder::setMacAddress(const vector<uint8_t>& mac, bool* success) {
92   if (impl_ == nullptr) {
93     *success = false;
94     return Status::ok();
95   }
96   if (mac.size() != ETH_ALEN) {
97     LOG(ERROR) << "Invalid MAC length " << mac.size();
98     *success = false;
99     return Status::ok();
100   }
101   std::array<uint8_t, ETH_ALEN> mac_array;
102   std::copy_n(mac.begin(), ETH_ALEN, mac_array.begin());
103   *success = impl_->SetMacAddress(mac_array);
104   return Status::ok();
105 }
106 
SendMgmtFrame(const vector<uint8_t> & frame,const sp<ISendMgmtFrameEvent> & callback,int32_t mcs)107 Status ClientInterfaceBinder::SendMgmtFrame(const vector<uint8_t>& frame,
108     const sp<ISendMgmtFrameEvent>& callback, int32_t mcs) {
109   if (impl_ == nullptr) {
110     callback->OnFailure(ISendMgmtFrameEvent::SEND_MGMT_FRAME_ERROR_UNKNOWN);
111     return Status::ok();
112   }
113   // TODO (b/112029045) validate mcs
114   impl_->SendMgmtFrame(frame, callback, mcs);
115   return Status::ok();
116 }
117 
118 }  // namespace wificond
119 }  // namespace android
120