blob: 05ea638409b632d91ff954ffe07fc7c194f1a53c [file] [log] [blame]
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001/*
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
xshu5899e8e2018-01-09 15:36:03 -080017#include <fcntl.h>
18
Roshan Pius3c4e8a32016-10-03 14:53:58 -070019#include <android-base/logging.h>
xshu5899e8e2018-01-09 15:36:03 -080020#include <android-base/unique_fd.h>
Roshan Pius9377a0d2017-10-06 13:18:54 -070021#include <cutils/properties.h>
xshu5899e8e2018-01-09 15:36:03 -080022#include <sys/stat.h>
23#include <sys/sysmacros.h>
Roshan Pius3c4e8a32016-10-03 14:53:58 -070024
Roshan Pius3c868522016-10-27 12:43:49 -070025#include "hidl_return_util.h"
Roshan Piuse2d0ab52016-12-05 15:24:20 -080026#include "hidl_struct_util.h"
Roshan Pius3c868522016-10-27 12:43:49 -070027#include "wifi_chip.h"
Roshan Pius5c055462016-10-11 08:27:27 -070028#include "wifi_status_util.h"
Roshan Pius3c4e8a32016-10-03 14:53:58 -070029
Roshan Pius35d958c2016-10-06 16:47:38 -070030namespace {
xshu5899e8e2018-01-09 15:36:03 -080031using android::base::unique_fd;
Roshan Pius35d958c2016-10-06 16:47:38 -070032using android::hardware::hidl_string;
Roshan Piusabcf78f2017-10-06 16:30:38 -070033using android::hardware::hidl_vec;
Roshan Pius2c06a3f2016-12-15 17:51:40 -080034using android::hardware::wifi::V1_0::ChipModeId;
Roshan Pius52947fb2016-11-18 11:38:07 -080035using android::hardware::wifi::V1_0::IfaceType;
Roshan Pius675609b2017-10-31 14:24:58 -070036using android::hardware::wifi::V1_0::IWifiChip;
Roshan Piusabcf78f2017-10-06 16:30:38 -070037using android::sp;
Roshan Pius52947fb2016-11-18 11:38:07 -080038
Roshan Pius2c06a3f2016-12-15 17:51:40 -080039constexpr ChipModeId kInvalidModeId = UINT32_MAX;
Roshan Piuscc338202017-11-02 13:54:09 -070040// These mode ID's should be unique (even across combo versions). Refer to
41// handleChipConfiguration() for it's usage.
42// Mode ID's for V1
43constexpr ChipModeId kV1StaChipModeId = 0;
44constexpr ChipModeId kV1ApChipModeId = 1;
45// Mode ID for V2
46constexpr ChipModeId kV2ChipModeId = 2;
Roshan Pius35d958c2016-10-06 16:47:38 -070047
xshu5899e8e2018-01-09 15:36:03 -080048constexpr char kCpioMagic[] = "070701";
49constexpr size_t kMaxBufferSizeBytes = 1024 * 1024;
50constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60;
51constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
52
Roshan Pius35d958c2016-10-06 16:47:38 -070053template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070054void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
55 iface->invalidate();
56 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface),
57 ifaces.end());
58}
59
60template <typename Iface>
61void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
62 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070063 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070064 }
Roshan Pius675609b2017-10-31 14:24:58 -070065 ifaces.clear();
66}
67
68template <typename Iface>
69std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
70 std::vector<hidl_string> names;
71 for (const auto& iface : ifaces) {
72 names.emplace_back(iface->getName());
73 }
74 return names;
75}
76
77template <typename Iface>
78sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces,
79 const std::string& name) {
80 std::vector<hidl_string> names;
81 for (const auto& iface : ifaces) {
82 if (name == iface->getName()) {
83 return iface;
84 }
85 }
86 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070087}
Roshan Pius9377a0d2017-10-06 13:18:54 -070088
89std::string getWlan0IfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -070090 std::array<char, PROPERTY_VALUE_MAX> buffer;
91 property_get("wifi.interface", buffer.data(), "wlan0");
92 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -070093}
94
Roshan Pius9377a0d2017-10-06 13:18:54 -070095std::string getWlan1IfaceName() {
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070096 std::array<char, PROPERTY_VALUE_MAX> buffer;
97 property_get("wifi.concurrent.interface", buffer.data(), "wlan1");
98 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -070099}
Roshan Pius9377a0d2017-10-06 13:18:54 -0700100
101std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700102 std::array<char, PROPERTY_VALUE_MAX> buffer;
103 property_get("wifi.direct.interface", buffer.data(), "p2p0");
104 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700105}
106
xshu5899e8e2018-01-09 15:36:03 -0800107// delete files older than a predefined time in the wifi tombstone dir
108bool removeOldFilesInternal() {
109 time_t now = time(0);
110 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
111 DIR* dir_dump = opendir(kTombstoneFolderPath);
112 if (!dir_dump) {
113 LOG(ERROR) << "Failed to open directory: " << strerror(errno);
114 return false;
115 }
116 unique_fd dir_auto_closer(dirfd(dir_dump));
117 struct dirent* dp;
118 bool success = true;
119 while ((dp = readdir(dir_dump))) {
120 if (dp->d_type != DT_REG) {
121 continue;
122 }
123 std::string cur_file_name(dp->d_name);
124 struct stat cur_file_stat;
125 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
126 if (stat(cur_file_path.c_str(), &cur_file_stat) != -1) {
127 if (cur_file_stat.st_mtime < delete_files_before) {
128 if (unlink(cur_file_path.c_str()) != 0) {
129 LOG(ERROR) << "Error deleting file " << strerror(errno);
130 success = false;
131 }
132 }
133 } else {
134 LOG(ERROR) << "Failed to get file stat for " << cur_file_path
135 << ": " << strerror(errno);
136 success = false;
137 }
138 }
139 return success;
140}
141
142// Archives all files in |input_dir| and writes result into |out_fd|
143// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
144// portion
145size_t cpioFilesInDir(int out_fd, const char* input_dir) {
146 struct dirent* dp;
147 size_t n_error = 0;
148 char read_buf[32 * 1024];
149 DIR* dir_dump = opendir(input_dir);
150 if (!dir_dump) {
151 LOG(ERROR) << "Failed to open directory: " << strerror(errno);
152 n_error++;
153 return n_error;
154 }
155 unique_fd dir_auto_closer(dirfd(dir_dump));
156 while ((dp = readdir(dir_dump))) {
157 if (dp->d_type != DT_REG) {
158 continue;
159 }
160 std::string cur_file_name(dp->d_name);
161 const size_t file_name_len =
162 cur_file_name.size() + 1; // string.size() does not include the
163 // null terminator. The cpio FreeBSD file
164 // header expects the null character to
165 // be included in the length.
166 struct stat st;
167 ssize_t llen;
168 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
169 if (stat(cur_file_path.c_str(), &st) != -1) {
170 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
171 unique_fd file_auto_closer(fd_read);
172 if (fd_read == -1) {
173 LOG(ERROR) << "Failed to read file " << cur_file_path << " "
174 << strerror(errno);
175 n_error++;
176 continue;
177 }
178 llen = sprintf(
179 read_buf,
180 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
181 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
182 st.st_gid, static_cast<int>(st.st_nlink),
183 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
184 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
185 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
186 if (write(out_fd, read_buf, llen) == -1) {
187 LOG(ERROR) << "Error writing cpio header to file "
188 << cur_file_path << " " << strerror(errno);
189 n_error++;
190 return n_error;
191 }
192 if (write(out_fd, cur_file_name.c_str(), file_name_len) == -1) {
193 LOG(ERROR) << "Error writing filename to file " << cur_file_path
194 << " " << strerror(errno);
195 n_error++;
196 return n_error;
197 }
198
199 // NUL Pad header up to 4 multiple bytes.
200 llen = (llen + file_name_len) % 4;
201 if (llen != 0) {
202 const uint32_t zero = 0;
203 if (write(out_fd, &zero, 4 - llen) == -1) {
204 LOG(ERROR) << "Error padding 0s to file " << cur_file_path
205 << " " << strerror(errno);
206 n_error++;
207 return n_error;
208 }
209 }
210
211 // writing content of file
212 llen = st.st_size;
213 while (llen > 0) {
214 ssize_t bytes_read = read(fd_read, read_buf, sizeof(read_buf));
215 if (bytes_read == -1) {
216 LOG(ERROR) << "Error reading file " << cur_file_path << " "
217 << strerror(errno);
218 n_error++;
219 return n_error;
220 }
221 llen -= bytes_read;
222 if (write(out_fd, read_buf, bytes_read) == -1) {
223 LOG(ERROR) << "Error writing data to file " << cur_file_path
224 << " " << strerror(errno);
225 n_error++;
226 return n_error;
227 }
228 if (bytes_read ==
229 0) { // this should never happen, but just in case
230 // to unstuck from while loop
231 LOG(ERROR) << "Unexpected file size for " << cur_file_path
232 << " " << strerror(errno);
233 n_error++;
234 break;
235 }
236 }
237 llen = st.st_size % 4;
238 if (llen != 0) {
239 const uint32_t zero = 0;
240 write(out_fd, &zero, 4 - llen);
241 }
242 } else {
243 LOG(ERROR) << "Failed to get file stat for " << cur_file_path
244 << ": " << strerror(errno);
245 n_error++;
246 }
247 }
248 memset(read_buf, 0, sizeof(read_buf));
249 if (write(out_fd, read_buf,
250 sprintf(read_buf, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0) +
251 4) == -1) {
252 LOG(ERROR) << "Error writing trailing bytes " << strerror(errno);
253 n_error++;
254 }
255 return n_error;
256}
257
258// Helper function to create a non-const char*.
259std::vector<char> makeCharVec(const std::string& str) {
260 std::vector<char> vec(str.size() + 1);
261 vec.assign(str.begin(), str.end());
262 vec.push_back('\0');
263 return vec;
264}
265
Roshan Piusabcf78f2017-10-06 16:30:38 -0700266} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700267
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700268namespace android {
269namespace hardware {
270namespace wifi {
Etan Cohen6ce50902017-09-14 07:30:57 -0700271namespace V1_2 {
Roshan Pius79a99752016-10-04 13:03:58 -0700272namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700273using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800274using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700275
Roshan Pius52947fb2016-11-18 11:38:07 -0800276WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700277 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700278 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
279 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800280 : chip_id_(chip_id),
281 legacy_hal_(legacy_hal),
282 mode_controller_(mode_controller),
Roshan Pius200a17d2017-11-01 13:03:35 -0700283 feature_flags_(feature_flags),
Roshan Pius52947fb2016-11-18 11:38:07 -0800284 is_valid_(true),
Roshan Pius48185b22016-12-15 19:10:30 -0800285 current_mode_id_(kInvalidModeId),
Roshan Piuscc338202017-11-02 13:54:09 -0700286 debug_ring_buffer_cb_registered_(false) {
287 populateModes();
288}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700289
Roshan Piusaabe5752016-09-29 09:03:59 -0700290void WifiChip::invalidate() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700291 invalidateAndRemoveAllIfaces();
292 legacy_hal_.reset();
293 event_cb_handler_.invalidate();
294 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700295}
296
Roshan Piusabcf78f2017-10-06 16:30:38 -0700297bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700298
Roshan Piusd37341f2017-01-31 13:13:28 -0800299std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700300 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800301}
302
Roshan Pius5c055462016-10-11 08:27:27 -0700303Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700304 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
305 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700306}
307
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700308Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800309 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700310 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700311 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
312 &WifiChip::registerEventCallbackInternal,
313 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700314}
315
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700316Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700317 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
318 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700319}
320
Roshan Pius5c055462016-10-11 08:27:27 -0700321Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700322 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
323 &WifiChip::getAvailableModesInternal,
324 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700325}
326
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800327Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700328 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800329 return validateAndCallWithLock(
330 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
331 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700332}
333
Roshan Pius5c055462016-10-11 08:27:27 -0700334Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700335 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
336 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700337}
338
Roshan Pius5c055462016-10-11 08:27:27 -0700339Return<void> WifiChip::requestChipDebugInfo(
340 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700341 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
342 &WifiChip::requestChipDebugInfoInternal,
343 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700344}
345
346Return<void> WifiChip::requestDriverDebugDump(
347 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700348 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
349 &WifiChip::requestDriverDebugDumpInternal,
350 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700351}
352
Roshan Pius5c055462016-10-11 08:27:27 -0700353Return<void> WifiChip::requestFirmwareDebugDump(
354 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700355 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
356 &WifiChip::requestFirmwareDebugDumpInternal,
357 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700358}
359
Roshan Pius5c055462016-10-11 08:27:27 -0700360Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700361 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
362 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700363}
364
Roshan Pius5c055462016-10-11 08:27:27 -0700365Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700366 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
367 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700368}
369
Roshan Pius5c055462016-10-11 08:27:27 -0700370Return<void> WifiChip::getApIface(const hidl_string& ifname,
371 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700372 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
373 &WifiChip::getApIfaceInternal, hidl_status_cb,
374 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700375}
376
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800377Return<void> WifiChip::removeApIface(const hidl_string& ifname,
378 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700379 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
380 &WifiChip::removeApIfaceInternal, hidl_status_cb,
381 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800382}
383
Roshan Pius5c055462016-10-11 08:27:27 -0700384Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700385 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
386 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700387}
388
Roshan Pius5c055462016-10-11 08:27:27 -0700389Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700390 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
391 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700392}
393
394Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700395 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700396 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
397 &WifiChip::getNanIfaceInternal, hidl_status_cb,
398 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700399}
400
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800401Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
402 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700403 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
404 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
405 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800406}
407
Roshan Pius5c055462016-10-11 08:27:27 -0700408Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700409 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
410 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700411}
412
Roshan Pius5c055462016-10-11 08:27:27 -0700413Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700414 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
415 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700416}
417
418Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700419 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700420 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
421 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
422 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700423}
424
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800425Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
426 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700427 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
428 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
429 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800430}
431
Roshan Pius5c055462016-10-11 08:27:27 -0700432Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700433 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
434 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700435}
436
Roshan Pius5c055462016-10-11 08:27:27 -0700437Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700438 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
439 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700440}
441
442Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700443 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700444 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
445 &WifiChip::getStaIfaceInternal, hidl_status_cb,
446 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700447}
448
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800449Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
450 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700451 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
452 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
453 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800454}
455
Roshan Pius5c055462016-10-11 08:27:27 -0700456Return<void> WifiChip::createRttController(
457 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700458 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
459 &WifiChip::createRttControllerInternal,
460 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700461}
462
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700463Return<void> WifiChip::getDebugRingBuffersStatus(
464 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700465 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
466 &WifiChip::getDebugRingBuffersStatusInternal,
467 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700468}
469
470Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700471 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
472 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700473 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700474 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
475 &WifiChip::startLoggingToDebugRingBufferInternal,
476 hidl_status_cb, ring_name, verbose_level,
477 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700478}
479
480Return<void> WifiChip::forceDumpToDebugRingBuffer(
481 const hidl_string& ring_name,
482 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700483 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
484 &WifiChip::forceDumpToDebugRingBufferInternal,
485 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700486}
487
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800488Return<void> WifiChip::stopLoggingToDebugRingBuffer(
489 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700490 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
491 &WifiChip::stopLoggingToDebugRingBufferInternal,
492 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800493}
494
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700495Return<void> WifiChip::getDebugHostWakeReasonStats(
496 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700497 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
498 &WifiChip::getDebugHostWakeReasonStatsInternal,
499 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700500}
501
Roshan Pius203cb032016-12-14 17:41:20 -0800502Return<void> WifiChip::enableDebugErrorAlerts(
503 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700504 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
505 &WifiChip::enableDebugErrorAlertsInternal,
506 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800507}
508
Roshan Pius735ff432017-07-25 08:48:08 -0700509Return<void> WifiChip::selectTxPowerScenario(
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800510 V1_1::IWifiChip::TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700511 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
512 &WifiChip::selectTxPowerScenarioInternal,
513 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700514}
515
Roshan Pius735ff432017-07-25 08:48:08 -0700516Return<void> WifiChip::resetTxPowerScenario(
517 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700518 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
519 &WifiChip::resetTxPowerScenarioInternal,
520 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700521}
522
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800523Return<void> WifiChip::registerEventCallback_1_2(
524 const sp<IWifiChipEventCallback>& event_callback,
525 registerEventCallback_cb hidl_status_cb) {
526 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
527 &WifiChip::registerEventCallbackInternal_1_2,
528 hidl_status_cb, event_callback);
529}
530
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800531Return<void> WifiChip::selectTxPowerScenario_1_2(
532 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
533 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
534 &WifiChip::selectTxPowerScenarioInternal_1_2, hidl_status_cb, scenario);
535}
536
xshu5899e8e2018-01-09 15:36:03 -0800537Return<void> WifiChip::debug(const hidl_handle& handle,
538 const hidl_vec<hidl_string>&) {
539 if (handle != nullptr && handle->numFds >= 1) {
540 int fd = handle->data[0];
541 if (!writeRingbufferFilesInternal()) {
542 LOG(ERROR) << "Error writing files to flash";
543 }
544 uint32_t n_error = cpioFilesInDir(fd, kTombstoneFolderPath);
545 if (n_error != 0) {
546 LOG(ERROR) << n_error << " errors occured in cpio function";
547 }
548 fsync(fd);
549 } else {
550 LOG(ERROR) << "File handle error";
551 }
552 return Void();
553}
554
Roshan Pius35d958c2016-10-06 16:47:38 -0700555void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700556 invalidateAndClearAll(ap_ifaces_);
557 invalidateAndClearAll(nan_ifaces_);
558 invalidateAndClearAll(p2p_ifaces_);
559 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700560 // Since all the ifaces are invalid now, all RTT controller objects
561 // using those ifaces also need to be invalidated.
562 for (const auto& rtt : rtt_controllers_) {
563 rtt->invalidate();
564 }
565 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700566}
567
Roshan Pius3c868522016-10-27 12:43:49 -0700568std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700569 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700570}
571
572WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800573 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
574 // Deprecated support for this callback.
575 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700576}
577
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700578std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700579 legacy_hal::wifi_error legacy_status;
580 uint32_t legacy_feature_set;
581 uint32_t legacy_logger_feature_set;
582 std::tie(legacy_status, legacy_feature_set) =
583 legacy_hal_.lock()->getSupportedFeatureSet(getWlan0IfaceName());
584 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
585 return {createWifiStatusFromLegacyError(legacy_status), 0};
586 }
587 std::tie(legacy_status, legacy_logger_feature_set) =
588 legacy_hal_.lock()->getLoggerSupportedFeatureSet(getWlan0IfaceName());
589 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800590 // some devices don't support querying logger feature set
591 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700592 }
593 uint32_t hidl_caps;
594 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
595 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
596 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
597 }
598 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700599}
600
Roshan Pius3c868522016-10-27 12:43:49 -0700601std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
602WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700603 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700604}
605
Roshan Piusba38d9c2017-12-08 07:32:08 -0800606WifiStatus WifiChip::configureChipInternal(
607 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
608 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700609 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700610 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
611 }
612 if (mode_id == current_mode_id_) {
613 LOG(DEBUG) << "Already in the specified mode " << mode_id;
614 return createWifiStatus(WifiStatusCode::SUCCESS);
615 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800616 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700617 if (status.code != WifiStatusCode::SUCCESS) {
618 for (const auto& callback : event_cb_handler_.getCallbacks()) {
619 if (!callback->onChipReconfigureFailure(status).isOk()) {
620 LOG(ERROR)
621 << "Failed to invoke onChipReconfigureFailure callback";
622 }
623 }
624 return status;
625 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800626 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700627 if (!callback->onChipReconfigured(mode_id).isOk()) {
628 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
629 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800630 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700631 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800632 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800633 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700634}
635
636std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700637 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700638 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
639 current_mode_id_};
640 }
641 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700642}
643
644std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
645WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700646 IWifiChip::ChipDebugInfo result;
647 legacy_hal::wifi_error legacy_status;
648 std::string driver_desc;
649 std::tie(legacy_status, driver_desc) =
650 legacy_hal_.lock()->getDriverVersion(getWlan0IfaceName());
651 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
652 LOG(ERROR) << "Failed to get driver version: "
653 << legacyErrorToString(legacy_status);
654 WifiStatus status = createWifiStatusFromLegacyError(
655 legacy_status, "failed to get driver version");
656 return {status, result};
657 }
658 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700659
Roshan Piusabcf78f2017-10-06 16:30:38 -0700660 std::string firmware_desc;
661 std::tie(legacy_status, firmware_desc) =
662 legacy_hal_.lock()->getFirmwareVersion(getWlan0IfaceName());
663 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
664 LOG(ERROR) << "Failed to get firmware version: "
665 << legacyErrorToString(legacy_status);
666 WifiStatus status = createWifiStatusFromLegacyError(
667 legacy_status, "failed to get firmware version");
668 return {status, result};
669 }
670 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700671
Roshan Piusabcf78f2017-10-06 16:30:38 -0700672 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700673}
674
675std::pair<WifiStatus, std::vector<uint8_t>>
676WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700677 legacy_hal::wifi_error legacy_status;
678 std::vector<uint8_t> driver_dump;
679 std::tie(legacy_status, driver_dump) =
680 legacy_hal_.lock()->requestDriverMemoryDump(getWlan0IfaceName());
681 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
682 LOG(ERROR) << "Failed to get driver debug dump: "
683 << legacyErrorToString(legacy_status);
684 return {createWifiStatusFromLegacyError(legacy_status),
685 std::vector<uint8_t>()};
686 }
687 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700688}
689
690std::pair<WifiStatus, std::vector<uint8_t>>
691WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700692 legacy_hal::wifi_error legacy_status;
693 std::vector<uint8_t> firmware_dump;
694 std::tie(legacy_status, firmware_dump) =
695 legacy_hal_.lock()->requestFirmwareMemoryDump(getWlan0IfaceName());
696 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
697 LOG(ERROR) << "Failed to get firmware debug dump: "
698 << legacyErrorToString(legacy_status);
699 return {createWifiStatusFromLegacyError(legacy_status), {}};
700 }
701 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700702}
703
704std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700705 if (!canCurrentModeSupportIfaceOfType(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700706 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800707 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700708 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700709 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_);
710 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700711 for (const auto& callback : event_cb_handler_.getCallbacks()) {
712 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
713 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
714 }
715 }
Roshan Pius675609b2017-10-31 14:24:58 -0700716 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700717}
718
719std::pair<WifiStatus, std::vector<hidl_string>>
720WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700721 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700722 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
723 }
Roshan Pius675609b2017-10-31 14:24:58 -0700724 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700725}
726
727std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800728 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700729 const auto iface = findUsingName(ap_ifaces_, ifname);
730 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700731 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
732 }
Roshan Pius675609b2017-10-31 14:24:58 -0700733 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700734}
735
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800736WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700737 const auto iface = findUsingName(ap_ifaces_, ifname);
738 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700739 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800740 }
Roshan Pius675609b2017-10-31 14:24:58 -0700741 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700742 for (const auto& callback : event_cb_handler_.getCallbacks()) {
743 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
744 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
745 }
746 }
747 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800748}
749
Roshan Pius3c868522016-10-27 12:43:49 -0700750std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700751 if (!canCurrentModeSupportIfaceOfType(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700752 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800753 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700754 // These are still assumed to be based on wlan0.
Roshan Piuscc338202017-11-02 13:54:09 -0700755 std::string ifname = getWlan0IfaceName();
756 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
757 nan_ifaces_.push_back(iface);
758 for (const auto& callback : event_cb_handler_.getCallbacks()) {
759 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
760 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
761 }
762 }
763 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700764}
765
766std::pair<WifiStatus, std::vector<hidl_string>>
767WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700768 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700769 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
770 }
Roshan Pius675609b2017-10-31 14:24:58 -0700771 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700772}
773
774std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800775 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700776 const auto iface = findUsingName(nan_ifaces_, ifname);
777 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700778 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
779 }
Roshan Pius675609b2017-10-31 14:24:58 -0700780 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700781}
782
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800783WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700784 const auto iface = findUsingName(nan_ifaces_, ifname);
785 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700786 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800787 }
Roshan Pius675609b2017-10-31 14:24:58 -0700788 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700789 for (const auto& callback : event_cb_handler_.getCallbacks()) {
790 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
791 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
792 }
793 }
794 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800795}
796
Roshan Pius3c868522016-10-27 12:43:49 -0700797std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700798 if (!canCurrentModeSupportIfaceOfType(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700799 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800800 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700801 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700802 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
803 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700804 for (const auto& callback : event_cb_handler_.getCallbacks()) {
805 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
806 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
807 }
808 }
Roshan Pius675609b2017-10-31 14:24:58 -0700809 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700810}
811
812std::pair<WifiStatus, std::vector<hidl_string>>
813WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700814 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700815 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
816 }
Roshan Pius675609b2017-10-31 14:24:58 -0700817 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700818}
819
820std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800821 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700822 const auto iface = findUsingName(p2p_ifaces_, ifname);
823 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700824 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
825 }
Roshan Pius675609b2017-10-31 14:24:58 -0700826 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700827}
828
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800829WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700830 const auto iface = findUsingName(p2p_ifaces_, ifname);
831 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700832 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800833 }
Roshan Pius675609b2017-10-31 14:24:58 -0700834 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700835 for (const auto& callback : event_cb_handler_.getCallbacks()) {
836 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
837 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
838 }
839 }
840 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800841}
842
Roshan Pius3c868522016-10-27 12:43:49 -0700843std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700844 if (!canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700845 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800846 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700847 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700848 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_);
849 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700850 for (const auto& callback : event_cb_handler_.getCallbacks()) {
851 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
852 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
853 }
854 }
Roshan Pius675609b2017-10-31 14:24:58 -0700855 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700856}
857
858std::pair<WifiStatus, std::vector<hidl_string>>
859WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700860 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700861 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
862 }
Roshan Pius675609b2017-10-31 14:24:58 -0700863 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700864}
865
866std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800867 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700868 const auto iface = findUsingName(sta_ifaces_, ifname);
869 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700870 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
871 }
Roshan Pius675609b2017-10-31 14:24:58 -0700872 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700873}
874
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800875WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700876 const auto iface = findUsingName(sta_ifaces_, ifname);
877 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700878 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800879 }
Roshan Pius675609b2017-10-31 14:24:58 -0700880 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700881 for (const auto& callback : event_cb_handler_.getCallbacks()) {
882 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
883 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
884 }
885 }
886 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800887}
888
Roshan Pius3c868522016-10-27 12:43:49 -0700889std::pair<WifiStatus, sp<IWifiRttController>>
890WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700891 sp<WifiRttController> rtt =
892 new WifiRttController(getWlan0IfaceName(), bound_iface, legacy_hal_);
893 rtt_controllers_.emplace_back(rtt);
894 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -0700895}
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700896
897std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
898WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700899 legacy_hal::wifi_error legacy_status;
900 std::vector<legacy_hal::wifi_ring_buffer_status>
901 legacy_ring_buffer_status_vec;
902 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
903 legacy_hal_.lock()->getRingBuffersStatus(getWlan0IfaceName());
904 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
905 return {createWifiStatusFromLegacyError(legacy_status), {}};
906 }
907 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
908 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
909 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
910 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
911 }
912 return {createWifiStatus(WifiStatusCode::SUCCESS),
913 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700914}
915
916WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700917 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
918 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
919 WifiStatus status = registerDebugRingBufferCallback();
920 if (status.code != WifiStatusCode::SUCCESS) {
921 return status;
922 }
923 legacy_hal::wifi_error legacy_status =
924 legacy_hal_.lock()->startRingBufferLogging(
925 getWlan0IfaceName(), ring_name,
926 static_cast<
927 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
928 verbose_level),
929 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -0800930 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
931 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -0700932 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700933}
934
935WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -0800936 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700937 WifiStatus status = registerDebugRingBufferCallback();
938 if (status.code != WifiStatusCode::SUCCESS) {
939 return status;
940 }
941 legacy_hal::wifi_error legacy_status =
942 legacy_hal_.lock()->getRingBufferData(getWlan0IfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -0800943
Roshan Piusabcf78f2017-10-06 16:30:38 -0700944 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700945}
946
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800947WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700948 legacy_hal::wifi_error legacy_status =
949 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
950 getWlan0IfaceName());
951 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800952}
953
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700954std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
955WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700956 legacy_hal::wifi_error legacy_status;
957 legacy_hal::WakeReasonStats legacy_stats;
958 std::tie(legacy_status, legacy_stats) =
959 legacy_hal_.lock()->getWakeReasonStats(getWlan0IfaceName());
960 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
961 return {createWifiStatusFromLegacyError(legacy_status), {}};
962 }
963 WifiDebugHostWakeReasonStats hidl_stats;
964 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
965 &hidl_stats)) {
966 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
967 }
968 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700969}
970
Roshan Pius203cb032016-12-14 17:41:20 -0800971WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700972 legacy_hal::wifi_error legacy_status;
973 if (enable) {
974 android::wp<WifiChip> weak_ptr_this(this);
975 const auto& on_alert_callback = [weak_ptr_this](
976 int32_t error_code,
977 std::vector<uint8_t> debug_data) {
978 const auto shared_ptr_this = weak_ptr_this.promote();
979 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
980 LOG(ERROR) << "Callback invoked on an invalid object";
981 return;
982 }
983 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
984 if (!callback->onDebugErrorAlert(error_code, debug_data)
985 .isOk()) {
986 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
987 }
988 }
989 };
990 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -0700991 getWlan0IfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700992 } else {
993 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -0700994 getWlan0IfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700995 }
996 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -0800997}
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800998
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800999WifiStatus WifiChip::selectTxPowerScenarioInternal(
1000 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001001 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1002 getWlan0IfaceName(),
1003 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1004 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001005}
1006
Roshan Pius735ff432017-07-25 08:48:08 -07001007WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001008 auto legacy_status =
1009 legacy_hal_.lock()->resetTxPowerScenario(getWlan0IfaceName());
1010 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001011}
1012
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001013WifiStatus WifiChip::registerEventCallbackInternal_1_2(
1014 const sp<IWifiChipEventCallback>& event_callback) {
1015 if (!event_cb_handler_.addCallback(event_callback)) {
1016 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1017 }
1018 return createWifiStatus(WifiStatusCode::SUCCESS);
1019}
1020
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001021WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario) {
1022 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1023 getWlan0IfaceName(),
1024 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1025 return createWifiStatusFromLegacyError(legacy_status);
1026}
1027
Roshan Piusba38d9c2017-12-08 07:32:08 -08001028WifiStatus WifiChip::handleChipConfiguration(
1029 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1030 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001031 // If the chip is already configured in a different mode, stop
1032 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001033 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001034 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1035 << " to mode " << mode_id;
1036 invalidateAndRemoveAllIfaces();
1037 legacy_hal::wifi_error legacy_status =
1038 legacy_hal_.lock()->stop(lock, []() {});
1039 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1040 LOG(ERROR) << "Failed to stop legacy HAL: "
1041 << legacyErrorToString(legacy_status);
1042 return createWifiStatusFromLegacyError(legacy_status);
1043 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001044 }
Roshan Piuscc338202017-11-02 13:54:09 -07001045 // Firmware mode change not needed for V2 devices.
1046 bool success = true;
1047 if (mode_id == kV1StaChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001048 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Roshan Piuscc338202017-11-02 13:54:09 -07001049 } else if (mode_id == kV1ApChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001050 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1051 }
1052 if (!success) {
1053 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1054 }
1055 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1056 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1057 LOG(ERROR) << "Failed to start legacy HAL: "
1058 << legacyErrorToString(legacy_status);
1059 return createWifiStatusFromLegacyError(legacy_status);
1060 }
Roshan Pius85c64412018-01-22 17:58:40 -08001061 // Every time the HAL is restarted, we need to register the
1062 // radio mode change callback.
1063 WifiStatus status = registerRadioModeChangeCallback();
1064 if (status.code != WifiStatusCode::SUCCESS) {
1065 // This probably is not a critical failure?
1066 LOG(ERROR) << "Failed to register radio mode change callback";
1067 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001068 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001069}
Roshan Pius48185b22016-12-15 19:10:30 -08001070
1071WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001072 if (debug_ring_buffer_cb_registered_) {
1073 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001074 }
Roshan Pius3797e182017-03-30 18:01:54 -07001075
Roshan Piusabcf78f2017-10-06 16:30:38 -07001076 android::wp<WifiChip> weak_ptr_this(this);
1077 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001078 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001079 const std::vector<uint8_t>& data,
1080 const legacy_hal::wifi_ring_buffer_status& status) {
1081 const auto shared_ptr_this = weak_ptr_this.promote();
1082 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1083 LOG(ERROR) << "Callback invoked on an invalid object";
1084 return;
1085 }
1086 WifiDebugRingBufferStatus hidl_status;
1087 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1088 status, &hidl_status)) {
1089 LOG(ERROR) << "Error converting ring buffer status";
1090 return;
1091 }
xshu5899e8e2018-01-09 15:36:03 -08001092 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1093 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1094 Ringbuffer& cur_buffer = target->second;
1095 cur_buffer.append(data);
1096 } else {
1097 LOG(ERROR) << "Ringname " << name << " not found";
1098 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001099 }
1100 };
1101 legacy_hal::wifi_error legacy_status =
1102 legacy_hal_.lock()->registerRingBufferCallbackHandler(
1103 getWlan0IfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001104
Roshan Piusabcf78f2017-10-06 16:30:38 -07001105 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1106 debug_ring_buffer_cb_registered_ = true;
1107 }
1108 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001109}
1110
Roshan Pius85c64412018-01-22 17:58:40 -08001111WifiStatus WifiChip::registerRadioModeChangeCallback() {
1112 android::wp<WifiChip> weak_ptr_this(this);
1113 const auto& on_radio_mode_change_callback =
1114 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1115 const auto shared_ptr_this = weak_ptr_this.promote();
1116 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1117 LOG(ERROR) << "Callback invoked on an invalid object";
1118 return;
1119 }
1120 std::vector<IWifiChipEventCallback::RadioModeInfo>
1121 hidl_radio_mode_infos;
1122 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1123 mac_infos, &hidl_radio_mode_infos)) {
1124 LOG(ERROR) << "Error converting wifi mac info";
1125 return;
1126 }
1127 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1128 if (!callback->onRadioModeChange(hidl_radio_mode_infos)
1129 .isOk()) {
1130 LOG(ERROR) << "Failed to invoke onRadioModeChange"
1131 << " callback on: " << toString(callback);
1132 }
1133 }
1134 };
1135 legacy_hal::wifi_error legacy_status =
1136 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1137 getWlan0IfaceName(), on_radio_mode_change_callback);
1138 return createWifiStatusFromLegacyError(legacy_status);
1139}
1140
Roshan Piuscc338202017-11-02 13:54:09 -07001141void WifiChip::populateModes() {
1142 // The chip combination supported for current devices is fixed.
1143 // They can be one of the following based on device features:
1144 // a) 2 separate modes of operation with 1 interface combination each:
1145 // Mode 1 (STA mode): Will support 1 STA and 1 P2P or NAN(optional)
1146 // concurrent iface operations.
1147 // Mode 2 (AP mode): Will support 1 AP iface operation.
1148 //
1149 // b) 1 mode of operation with 2 interface combinations
1150 // (conditional on isDualInterfaceSupported()):
1151 // Interface Combination 1: Will support 1 STA and 1 P2P or NAN(optional)
1152 // concurrent iface operations.
1153 // Interface Combination 2: Will support 1 STA and 1 STA or AP concurrent
1154 // iface operations.
1155 // If Aware is enabled (conditional on isAwareSupported()), the iface
1156 // combination will be modified to support either P2P or NAN in place of
1157 // just P2P.
1158 if (feature_flags_.lock()->isDualInterfaceSupported()) {
1159 // V2 Iface combinations for Mode Id = 2.
1160 const IWifiChip::ChipIfaceCombinationLimit
1161 chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1162 const IWifiChip::ChipIfaceCombinationLimit
1163 chip_iface_combination_limit_2 = {{IfaceType::STA, IfaceType::AP},
1164 1};
1165 IWifiChip::ChipIfaceCombinationLimit chip_iface_combination_limit_3;
1166 if (feature_flags_.lock()->isAwareSupported()) {
1167 chip_iface_combination_limit_3 = {{IfaceType::P2P, IfaceType::NAN},
1168 1};
1169 } else {
1170 chip_iface_combination_limit_3 = {{IfaceType::P2P}, 1};
1171 }
1172 const IWifiChip::ChipIfaceCombination chip_iface_combination_1 = {
1173 {chip_iface_combination_limit_1, chip_iface_combination_limit_2}};
1174 const IWifiChip::ChipIfaceCombination chip_iface_combination_2 = {
1175 {chip_iface_combination_limit_1, chip_iface_combination_limit_3}};
1176 const IWifiChip::ChipMode chip_mode = {
1177 kV2ChipModeId,
1178 {chip_iface_combination_1, chip_iface_combination_2}};
1179 modes_ = {chip_mode};
1180 } else {
1181 // V1 Iface combinations for Mode Id = 0. (STA Mode)
1182 const IWifiChip::ChipIfaceCombinationLimit
1183 sta_chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1184 IWifiChip::ChipIfaceCombinationLimit sta_chip_iface_combination_limit_2;
1185 if (feature_flags_.lock()->isAwareSupported()) {
1186 sta_chip_iface_combination_limit_2 = {
1187 {IfaceType::P2P, IfaceType::NAN}, 1};
1188 } else {
1189 sta_chip_iface_combination_limit_2 = {{IfaceType::P2P}, 1};
1190 }
1191 const IWifiChip::ChipIfaceCombination sta_chip_iface_combination = {
1192 {sta_chip_iface_combination_limit_1,
1193 sta_chip_iface_combination_limit_2}};
1194 const IWifiChip::ChipMode sta_chip_mode = {
1195 kV1StaChipModeId, {sta_chip_iface_combination}};
1196 // Iface combinations for Mode Id = 1. (AP Mode)
1197 const IWifiChip::ChipIfaceCombinationLimit
1198 ap_chip_iface_combination_limit = {{IfaceType::AP}, 1};
1199 const IWifiChip::ChipIfaceCombination ap_chip_iface_combination = {
1200 {ap_chip_iface_combination_limit}};
1201 const IWifiChip::ChipMode ap_chip_mode = {kV1ApChipModeId,
1202 {ap_chip_iface_combination}};
1203 modes_ = {sta_chip_mode, ap_chip_mode};
1204 }
1205}
1206
1207std::vector<IWifiChip::ChipIfaceCombination>
1208WifiChip::getCurrentModeIfaceCombinations() {
1209 if (!isValidModeId(current_mode_id_)) {
1210 LOG(ERROR) << "Chip not configured in a mode yet";
1211 return {};
1212 }
1213 for (const auto& mode : modes_) {
1214 if (mode.id == current_mode_id_) {
1215 return mode.availableCombinations;
1216 }
1217 }
1218 CHECK(0) << "Expected to find iface combinations for current mode!";
1219 return {};
1220}
1221
1222// Returns a map indexed by IfaceType with the number of ifaces currently
1223// created of the corresponding type.
1224std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1225 std::map<IfaceType, size_t> iface_counts;
1226 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1227 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1228 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1229 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1230 return iface_counts;
1231}
1232
1233// This expands the provided iface combinations to a more parseable
1234// form. Returns a vector of available combinations possible with the number
1235// of ifaces of each type in the combination.
1236// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1237std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1238 const IWifiChip::ChipIfaceCombination& combination) {
1239 uint32_t num_expanded_combos = 1;
1240 for (const auto& limit : combination.limits) {
1241 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1242 num_expanded_combos *= limit.types.size();
1243 }
1244 }
1245
1246 // Allocate the vector of expanded combos and reset all iface counts to 0
1247 // in each combo.
1248 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1249 expanded_combos.resize(num_expanded_combos);
1250 for (auto& expanded_combo : expanded_combos) {
1251 for (const auto type :
1252 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1253 expanded_combo[type] = 0;
1254 }
1255 }
1256 uint32_t span = num_expanded_combos;
1257 for (const auto& limit : combination.limits) {
1258 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1259 span /= limit.types.size();
1260 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1261 const auto iface_type =
1262 limit.types[(k / span) % limit.types.size()];
1263 expanded_combos[k][iface_type]++;
1264 }
1265 }
1266 }
1267 return expanded_combos;
1268}
1269
1270bool WifiChip::canExpandedIfaceCombinationSupportIfaceOfType(
1271 const std::map<IfaceType, size_t>& combo, IfaceType requested_type) {
1272 const auto current_combo = getCurrentIfaceCombination();
1273
1274 // Check if we have space for 1 more iface of |type| in this combo
1275 for (const auto type :
1276 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1277 size_t num_ifaces_needed = current_combo.at(type);
1278 if (type == requested_type) {
1279 num_ifaces_needed++;
1280 }
1281 size_t num_ifaces_allowed = combo.at(type);
1282 if (num_ifaces_needed > num_ifaces_allowed) {
1283 return false;
1284 }
1285 }
1286 return true;
1287}
1288
1289// This method does the following:
1290// a) Enumerate all possible iface combos by expanding the current
1291// ChipIfaceCombination.
1292// b) Check if the requested iface type can be added to the current mode.
1293bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType type) {
1294 if (!isValidModeId(current_mode_id_)) {
1295 LOG(ERROR) << "Chip not configured in a mode yet";
1296 return false;
1297 }
1298 const auto combinations = getCurrentModeIfaceCombinations();
1299 for (const auto& combination : combinations) {
1300 const auto expanded_combos = expandIfaceCombinations(combination);
1301 for (const auto& expanded_combo : expanded_combos) {
1302 if (canExpandedIfaceCombinationSupportIfaceOfType(expanded_combo,
1303 type)) {
1304 return true;
1305 }
1306 }
1307 }
1308 return false;
1309}
1310
1311bool WifiChip::isValidModeId(ChipModeId mode_id) {
1312 for (const auto& mode : modes_) {
1313 if (mode.id == mode_id) {
1314 return true;
1315 }
1316 }
1317 return false;
1318}
1319
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001320// Return "wlan0", if "wlan0" is not already in use, else return "wlan1".
1321// This is based on the assumption that we'll have a max of 2 concurrent
1322// AP/STA ifaces.
1323std::string WifiChip::allocateApOrStaIfaceName() {
1324 auto ap_iface = findUsingName(ap_ifaces_, getWlan0IfaceName());
1325 auto sta_iface = findUsingName(sta_ifaces_, getWlan0IfaceName());
1326 if (!ap_iface.get() && !sta_iface.get()) {
1327 return getWlan0IfaceName();
1328 }
1329 ap_iface = findUsingName(ap_ifaces_, getWlan1IfaceName());
1330 sta_iface = findUsingName(sta_ifaces_, getWlan1IfaceName());
1331 if (!ap_iface.get() && !sta_iface.get()) {
1332 return getWlan1IfaceName();
1333 }
1334 // This should never happen. We screwed up somewhere if it did.
1335 CHECK(0) << "wlan0 and wlan1 in use already!";
1336 return {};
1337}
1338
xshu5899e8e2018-01-09 15:36:03 -08001339bool WifiChip::writeRingbufferFilesInternal() {
1340 if (!removeOldFilesInternal()) {
1341 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1342 return false;
1343 }
1344 // write ringbuffers to file
1345 for (const auto& item : ringbuffer_map_) {
1346 const Ringbuffer& cur_buffer = item.second;
1347 if (cur_buffer.getData().empty()) {
1348 continue;
1349 }
1350 const std::string file_path_raw =
1351 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1352 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1353 if (dump_fd == -1) {
1354 LOG(ERROR) << "create file failed: " << strerror(errno);
1355 return false;
1356 }
1357 unique_fd file_auto_closer(dump_fd);
1358 for (const auto& cur_block : cur_buffer.getData()) {
1359 if (write(dump_fd, cur_block.data(),
1360 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1361 LOG(ERROR) << "Error writing to file " << strerror(errno);
1362 }
1363 }
1364 }
1365 return true;
1366}
1367
Roshan Pius79a99752016-10-04 13:03:58 -07001368} // namespace implementation
Etan Cohen6ce50902017-09-14 07:30:57 -07001369} // namespace V1_2
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001370} // namespace wifi
1371} // namespace hardware
1372} // namespace android