1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "wifi.h"
18 
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <cutils/properties.h>
22 #include <sys/stat.h>
23 #include <sys/sysmacros.h>
24 
25 #include "aidl_return_util.h"
26 #include "aidl_sync_util.h"
27 #include "wifi_status_util.h"
28 
29 namespace {
30 using android::base::unique_fd;
31 
32 // Starting Chip ID, will be assigned to primary chip
33 static constexpr int32_t kPrimaryChipId = 0;
34 constexpr char kCpioMagic[] = "070701";
35 constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
36 
37 // Helper function for |cpioArchiveFilesInDir|
cpioWriteHeader(int out_fd,struct stat & st,const char * file_name,size_t file_name_len)38 bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name, size_t file_name_len) {
39     const int buf_size = 32 * 1024;
40     std::array<char, buf_size> read_buf;
41     ssize_t llen = snprintf(
42             read_buf.data(), buf_size, "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
43             kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid, st.st_gid,
44             static_cast<int>(st.st_nlink), static_cast<int>(st.st_mtime),
45             static_cast<int>(st.st_size), major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
46             minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
47     if (write(out_fd, read_buf.data(), llen < buf_size ? llen : buf_size - 1) == -1) {
48         PLOG(ERROR) << "Error writing cpio header to file " << file_name;
49         return false;
50     }
51     if (write(out_fd, file_name, file_name_len) == -1) {
52         PLOG(ERROR) << "Error writing filename to file " << file_name;
53         return false;
54     }
55 
56     // NUL Pad header up to 4 multiple bytes.
57     llen = (llen + file_name_len) % 4;
58     if (llen != 0) {
59         const uint32_t zero = 0;
60         if (write(out_fd, &zero, 4 - llen) == -1) {
61             PLOG(ERROR) << "Error padding 0s to file " << file_name;
62             return false;
63         }
64     }
65     return true;
66 }
67 
68 // Helper function for |cpioArchiveFilesInDir|
cpioWriteFileContent(int fd_read,int out_fd,struct stat & st)69 size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
70     // writing content of file
71     std::array<char, 32 * 1024> read_buf;
72     ssize_t llen = st.st_size;
73     size_t n_error = 0;
74     while (llen > 0) {
75         ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
76         if (bytes_read == -1) {
77             PLOG(ERROR) << "Error reading file";
78             return ++n_error;
79         }
80         llen -= bytes_read;
81         if (write(out_fd, read_buf.data(), bytes_read) == -1) {
82             PLOG(ERROR) << "Error writing data to file";
83             return ++n_error;
84         }
85         if (bytes_read == 0) {  // this should never happen, but just in case
86                                 // to unstuck from while loop
87             PLOG(ERROR) << "Unexpected read result";
88             n_error++;
89             break;
90         }
91     }
92     llen = st.st_size % 4;
93     if (llen != 0) {
94         const uint32_t zero = 0;
95         if (write(out_fd, &zero, 4 - llen) == -1) {
96             PLOG(ERROR) << "Error padding 0s to file";
97             return ++n_error;
98         }
99     }
100     return n_error;
101 }
102 
103 // Helper function for |cpioArchiveFilesInDir|
cpioWriteFileTrailer(int out_fd)104 bool cpioWriteFileTrailer(int out_fd) {
105     const int buf_size = 4096;
106     std::array<char, buf_size> read_buf;
107     read_buf.fill(0);
108     ssize_t llen = snprintf(read_buf.data(), 4096, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0);
109     if (write(out_fd, read_buf.data(), (llen < buf_size ? llen : buf_size - 1) + 4) == -1) {
110         PLOG(ERROR) << "Error writing trailing bytes";
111         return false;
112     }
113     return true;
114 }
115 
116 // Archives all files in |input_dir| and writes result into |out_fd|
117 // Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
118 // portion
cpioArchiveFilesInDir(int out_fd,const char * input_dir)119 size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
120     struct dirent* dp;
121     size_t n_error = 0;
122     std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir), closedir);
123     if (!dir_dump) {
124         PLOG(ERROR) << "Failed to open directory";
125         return ++n_error;
126     }
127     while ((dp = readdir(dir_dump.get()))) {
128         if (dp->d_type != DT_REG) {
129             continue;
130         }
131         std::string cur_file_name(dp->d_name);
132         struct stat st;
133         const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
134         if (stat(cur_file_path.c_str(), &st) == -1) {
135             PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
136             n_error++;
137             continue;
138         }
139         const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
140         if (fd_read == -1) {
141             PLOG(ERROR) << "Failed to open file " << cur_file_path;
142             n_error++;
143             continue;
144         }
145         std::string file_name_with_last_modified_time =
146                 cur_file_name + "-" + std::to_string(st.st_mtime);
147         // string.size() does not include the null terminator. The cpio FreeBSD
148         // file header expects the null character to be included in the length.
149         const size_t file_name_len = file_name_with_last_modified_time.size() + 1;
150         unique_fd file_auto_closer(fd_read);
151         if (!cpioWriteHeader(out_fd, st, file_name_with_last_modified_time.c_str(),
152                              file_name_len)) {
153             return ++n_error;
154         }
155         size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
156         if (write_error) {
157             return n_error + write_error;
158         }
159     }
160     if (!cpioWriteFileTrailer(out_fd)) {
161         return ++n_error;
162     }
163     return n_error;
164 }
165 
166 }  // namespace
167 
168 namespace aidl {
169 namespace android {
170 namespace hardware {
171 namespace wifi {
172 using aidl_return_util::validateAndCall;
173 using aidl_return_util::validateAndCallWithLock;
174 using aidl_sync_util::acquireGlobalLock;
175 
Wifi(const std::shared_ptr<::android::wifi_system::InterfaceTool> iface_tool,const std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory,const std::shared_ptr<mode_controller::WifiModeController> mode_controller,const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags)176 Wifi::Wifi(const std::shared_ptr<::android::wifi_system::InterfaceTool> iface_tool,
177            const std::shared_ptr<legacy_hal::WifiLegacyHalFactory> legacy_hal_factory,
178            const std::shared_ptr<mode_controller::WifiModeController> mode_controller,
179            const std::shared_ptr<feature_flags::WifiFeatureFlags> feature_flags)
180     : iface_tool_(iface_tool),
181       legacy_hal_factory_(legacy_hal_factory),
182       mode_controller_(mode_controller),
183       feature_flags_(feature_flags),
184       run_state_(RunState::STOPPED) {}
185 
isValid()186 bool Wifi::isValid() {
187     // This object is always valid.
188     return true;
189 }
190 
registerEventCallback(const std::shared_ptr<IWifiEventCallback> & in_callback)191 ndk::ScopedAStatus Wifi::registerEventCallback(
192         const std::shared_ptr<IWifiEventCallback>& in_callback) {
193     return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN,
194                            &Wifi::registerEventCallbackInternal, in_callback);
195 }
196 
isStarted(bool * _aidl_return)197 ndk::ScopedAStatus Wifi::isStarted(bool* _aidl_return) {
198     *_aidl_return = (run_state_ != RunState::STOPPED);
199     return ndk::ScopedAStatus::ok();
200 }
201 
start()202 ndk::ScopedAStatus Wifi::start() {
203     return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::startInternal);
204 }
205 
stop()206 ndk::ScopedAStatus Wifi::stop() {
207     return validateAndCallWithLock(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::stopInternal);
208 }
209 
getChipIds(std::vector<int32_t> * _aidl_return)210 ndk::ScopedAStatus Wifi::getChipIds(std::vector<int32_t>* _aidl_return) {
211     return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::getChipIdsInternal,
212                            _aidl_return);
213 }
214 
getChip(int32_t in_chipId,std::shared_ptr<IWifiChip> * _aidl_return)215 ndk::ScopedAStatus Wifi::getChip(int32_t in_chipId, std::shared_ptr<IWifiChip>* _aidl_return) {
216     return validateAndCall(this, WifiStatusCode::ERROR_UNKNOWN, &Wifi::getChipInternal,
217                            _aidl_return, in_chipId);
218 }
219 
dump(int fd,const char ** args,uint32_t numArgs)220 binder_status_t Wifi::dump(int fd, const char** args, uint32_t numArgs) {
221     const auto lock = acquireGlobalLock();
222     LOG(INFO) << "-----------Debug was called----------------";
223     if (chips_.size() != 0) {
224         for (std::shared_ptr<WifiChip> chip : chips_) {
225             if (!chip.get()) continue;
226             chip->dump(fd, args, numArgs);
227         }
228     }
229     uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
230     if (n_error != 0) {
231         LOG(ERROR) << n_error << " errors occurred in cpio function";
232     }
233     ::android::base::WriteStringToFd("\n", fd);
234     fsync(fd);
235     return STATUS_OK;
236 }
237 
registerEventCallbackInternal(const std::shared_ptr<IWifiEventCallback> & event_callback)238 ndk::ScopedAStatus Wifi::registerEventCallbackInternal(
239         const std::shared_ptr<IWifiEventCallback>& event_callback) {
240     if (!event_cb_handler_.addCallback(event_callback)) {
241         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
242     }
243     return ndk::ScopedAStatus::ok();
244 }
245 
startInternal()246 ndk::ScopedAStatus Wifi::startInternal() {
247     if (run_state_ == RunState::STARTED) {
248         return ndk::ScopedAStatus::ok();
249     } else if (run_state_ == RunState::STOPPING) {
250         return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, "HAL is stopping");
251     }
252     ndk::ScopedAStatus wifi_status = initializeModeControllerAndLegacyHal();
253     if (wifi_status.isOk()) {
254         // Register the callback for subsystem restart
255         const auto& on_subsystem_restart_callback = [this](const std::string& error) {
256             ndk::ScopedAStatus wifi_status = createWifiStatus(WifiStatusCode::ERROR_UNKNOWN, error);
257             for (const auto& callback : event_cb_handler_.getCallbacks()) {
258                 LOG(INFO) << "Attempting to invoke onSubsystemRestart "
259                              "callback";
260                 WifiStatusCode errorCode =
261                         static_cast<WifiStatusCode>(wifi_status.getServiceSpecificError());
262                 if (!callback->onSubsystemRestart(errorCode).isOk()) {
263                     LOG(ERROR) << "Failed to invoke onSubsystemRestart callback";
264                 } else {
265                     LOG(INFO) << "Succeeded to invoke onSubsystemRestart "
266                                  "callback";
267                 }
268             }
269         };
270 
271         // Create the chip instance once the HAL is started.
272         int32_t chipId = kPrimaryChipId;
273         for (auto& hal : legacy_hals_) {
274             chips_.push_back(
275                     WifiChip::create(chipId, chipId == kPrimaryChipId, hal, mode_controller_,
276                                      std::make_shared<iface_util::WifiIfaceUtil>(iface_tool_, hal),
277                                      feature_flags_, on_subsystem_restart_callback, false));
278             chipId++;
279         }
280         run_state_ = RunState::STARTED;
281         for (const auto& callback : event_cb_handler_.getCallbacks()) {
282             if (!callback->onStart().isOk()) {
283                 LOG(ERROR) << "Failed to invoke onStart callback";
284             };
285         }
286         LOG(INFO) << "Wifi HAL started";
287     } else {
288         for (const auto& callback : event_cb_handler_.getCallbacks()) {
289             WifiStatusCode errorCode =
290                     static_cast<WifiStatusCode>(wifi_status.getServiceSpecificError());
291             if (!callback->onFailure(errorCode).isOk()) {
292                 LOG(ERROR) << "Failed to invoke onFailure callback";
293             }
294         }
295         LOG(ERROR) << "Wifi HAL start failed";
296         // Clear the event callback objects since the HAL start failed.
297         event_cb_handler_.invalidate();
298     }
299     return wifi_status;
300 }
301 
stopInternal(std::unique_lock<std::recursive_mutex> * lock)302 ndk::ScopedAStatus Wifi::stopInternal(
303         /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
304     if (run_state_ == RunState::STOPPED) {
305         return ndk::ScopedAStatus::ok();
306     } else if (run_state_ == RunState::STOPPING) {
307         return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE, "HAL is stopping");
308     }
309     // Clear the chip object and its child objects since the HAL is now
310     // stopped.
311     for (auto& chip : chips_) {
312         if (chip.get()) {
313             chip->invalidate();
314             chip.reset();
315         }
316     }
317     chips_.clear();
318     ndk::ScopedAStatus wifi_status = stopLegacyHalAndDeinitializeModeController(lock);
319     if (wifi_status.isOk()) {
320         for (const auto& callback : event_cb_handler_.getCallbacks()) {
321             if (!callback->onStop().isOk()) {
322                 LOG(ERROR) << "Failed to invoke onStop callback";
323             };
324         }
325         LOG(INFO) << "Wifi HAL stopped";
326     } else {
327         for (const auto& callback : event_cb_handler_.getCallbacks()) {
328             WifiStatusCode errorCode =
329                     static_cast<WifiStatusCode>(wifi_status.getServiceSpecificError());
330             if (!callback->onFailure(errorCode).isOk()) {
331                 LOG(ERROR) << "Failed to invoke onFailure callback";
332             }
333         }
334         LOG(ERROR) << "Wifi HAL stop failed";
335     }
336     // Clear the event callback objects since the HAL is now stopped.
337     event_cb_handler_.invalidate();
338     return wifi_status;
339 }
340 
getChipIdsInternal()341 std::pair<std::vector<int32_t>, ndk::ScopedAStatus> Wifi::getChipIdsInternal() {
342     std::vector<int32_t> chip_ids;
343 
344     for (auto& chip : chips_) {
345         int32_t chip_id = getChipIdFromWifiChip(chip);
346         if (chip_id != INT32_MAX) chip_ids.emplace_back(chip_id);
347     }
348     return {std::move(chip_ids), ndk::ScopedAStatus::ok()};
349 }
350 
getChipInternal(int32_t chip_id)351 std::pair<std::shared_ptr<IWifiChip>, ndk::ScopedAStatus> Wifi::getChipInternal(int32_t chip_id) {
352     for (auto& chip : chips_) {
353         int32_t cand_id = getChipIdFromWifiChip(chip);
354         if ((cand_id != INT32_MAX) && (cand_id == chip_id)) return {chip, ndk::ScopedAStatus::ok()};
355     }
356 
357     return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
358 }
359 
initializeModeControllerAndLegacyHal()360 ndk::ScopedAStatus Wifi::initializeModeControllerAndLegacyHal() {
361     if (!mode_controller_->initialize()) {
362         LOG(ERROR) << "Failed to initialize firmware mode controller";
363         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
364     }
365 
366     legacy_hals_ = legacy_hal_factory_->getHals();
367     if (legacy_hals_.empty()) return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
368     int index = 0;  // for failure log
369     for (auto& hal : legacy_hals_) {
370         legacy_hal::wifi_error legacy_status = hal->initialize();
371         if (legacy_status != legacy_hal::WIFI_SUCCESS) {
372             // Currently WifiLegacyHal::initialize does not allocate extra mem,
373             // only initializes the function table. If this changes, need to
374             // implement WifiLegacyHal::deinitialize and deinitalize the
375             // HALs already initialized
376             LOG(ERROR) << "Failed to initialize legacy HAL index: " << index
377                        << " error: " << legacyErrorToString(legacy_status);
378             return createWifiStatusFromLegacyError(legacy_status);
379         }
380         index++;
381     }
382     return ndk::ScopedAStatus::ok();
383 }
384 
stopLegacyHalAndDeinitializeModeController(std::unique_lock<std::recursive_mutex> * lock)385 ndk::ScopedAStatus Wifi::stopLegacyHalAndDeinitializeModeController(
386         /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock) {
387     legacy_hal::wifi_error legacy_status = legacy_hal::WIFI_SUCCESS;
388     int index = 0;
389 
390     run_state_ = RunState::STOPPING;
391     for (auto& hal : legacy_hals_) {
392         legacy_hal::wifi_error tmp = hal->stop(lock, [&]() {});
393         if (tmp != legacy_hal::WIFI_SUCCESS) {
394             LOG(ERROR) << "Failed to stop legacy HAL index: " << index
395                        << " error: " << legacyErrorToString(legacy_status);
396             legacy_status = tmp;
397         }
398         index++;
399     }
400     run_state_ = RunState::STOPPED;
401 
402     if (legacy_status != legacy_hal::WIFI_SUCCESS) {
403         LOG(ERROR) << "One or more legacy HALs failed to stop";
404         return createWifiStatusFromLegacyError(legacy_status);
405     }
406     if (!mode_controller_->deinitialize()) {
407         LOG(ERROR) << "Failed to deinitialize firmware mode controller";
408         return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
409     }
410     return ndk::ScopedAStatus::ok();
411 }
412 
getChipIdFromWifiChip(std::shared_ptr<WifiChip> & chip)413 int32_t Wifi::getChipIdFromWifiChip(std::shared_ptr<WifiChip>& chip) {
414     int32_t chip_id = INT32_MAX;
415     if (chip.get()) {
416         ndk::ScopedAStatus status = chip->getId(&chip_id);
417         if (!status.isOk()) {
418             // Reset value if operation failed.
419             chip_id = INT32_MAX;
420         }
421     }
422     return chip_id;
423 }
424 
425 }  // namespace wifi
426 }  // namespace hardware
427 }  // namespace android
428 }  // namespace aidl
429