1 /*
2  * Copyright (C) 2017 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 "info.h"
18 
19 #include <sys/stat.h>
20 
21 #include <string>
22 
23 static const char* const kInterfaceNames[] = {"wlan0", "wlan1"};
24 
Info()25 Info::Info() {
26     mInterfaces.reserve(sizeof(kInterfaceNames) / sizeof(kInterfaceNames[0]));
27     mInterfaceHandles.reserve(sizeof(kInterfaceNames) / sizeof(kInterfaceNames[0]));
28     for (const auto& name : kInterfaceNames) {
29         std::string test_path = std::string("/sys/class/net/") + name;
30         struct stat ignored_statbuf;
31         if (stat(test_path.c_str(), &ignored_statbuf) == 0) {
32             mInterfaces.emplace_back(mNetlink, name);
33             auto handle = reinterpret_cast<wifi_interface_handle>(&mInterfaces.back());
34             mInterfaceHandles.emplace_back(handle);
35         }
36     }
37 }
38 
init()39 bool Info::init() {
40     if (!mNetlink.init()) {
41         return false;
42     }
43     for (auto& iface : mInterfaces) {
44         if (!iface.init()) {
45             return false;
46         }
47     }
48     return true;
49 }
50 
eventLoop()51 void Info::eventLoop() {
52     mNetlink.eventLoop();
53 }
54 
stop(StopHandler stopHandler)55 void Info::stop(StopHandler stopHandler) {
56     mNetlink.stop(stopHandler);
57 }
58 
getInterfaces(int * num,wifi_interface_handle ** interfaces)59 wifi_error Info::getInterfaces(int* num, wifi_interface_handle** interfaces) {
60     if (num == nullptr || interfaces == nullptr) {
61         return WIFI_ERROR_INVALID_ARGS;
62     }
63     *num = mInterfaceHandles.size();
64     *interfaces = mInterfaceHandles.data();
65 
66     return WIFI_SUCCESS;
67 }
68 
69