1 //
2 // Copyright (C) 2015 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 "shill/wifi/wifi_driver_hal.h"
18 
19 #include <base/logging.h>
20 #include <hardware_brillo/wifi_driver_hal.h>
21 #include <hardware/hardware.h>
22 
23 using std::string;
24 
25 namespace shill {
26 
27 namespace {
28 
29 base::LazyInstance<WiFiDriverHal> g_wifi_driver_hal = LAZY_INSTANCE_INITIALIZER;
30 
WiFiDriverInit(wifi_driver_device_t ** out_driver)31 bool WiFiDriverInit(wifi_driver_device_t** out_driver) {
32   const hw_module_t* module;
33   wifi_driver_device_t* driver;
34   int ret;
35 
36   ret = hw_get_module(WIFI_DRIVER_DEVICE_ID_MAIN, &module);
37   if (ret != 0) {
38     LOG(ERROR) << "Failed to find HAL module";
39     return false;
40   }
41 
42   if (wifi_driver_open(module, &driver) != 0) {
43     LOG(ERROR) << "Failed to open WiFi HAL module";
44     return false;
45   }
46 
47   wifi_driver_error error;
48   error = (*driver->wifi_driver_initialize)();
49   if (error != WIFI_SUCCESS) {
50     LOG(ERROR) << "Failed to initialize WiFi driver";
51     wifi_driver_close(driver);
52     return false;
53   }
54 
55   *out_driver = driver;
56   return true;
57 }
58 
WiFiDriverShutdown(wifi_driver_device_t * driver)59 void WiFiDriverShutdown(wifi_driver_device_t* driver) {
60   wifi_driver_close(driver);
61 }
62 
WiFiDriverSetupInterface(wifi_driver_mode mode)63 string WiFiDriverSetupInterface(wifi_driver_mode mode) {
64   wifi_driver_device_t* driver;
65   if (!WiFiDriverInit(&driver)) {
66     return "";
67   }
68 
69   string name_str;
70   char device_name[DEFAULT_WIFI_DEVICE_NAME_SIZE];
71   wifi_driver_error error =
72       (*driver->wifi_driver_set_mode)(mode, device_name, sizeof(device_name));
73   if (error != WIFI_SUCCESS) {
74     LOG(ERROR) << "WiFi driver setup for mode " << mode << " failed: " << error;
75   } else {
76     name_str = string(device_name);
77   }
78 
79   WiFiDriverShutdown(driver);
80   return name_str;
81 }
82 
83 }  // namespace
84 
WiFiDriverHal()85 WiFiDriverHal::WiFiDriverHal() {}
86 
~WiFiDriverHal()87 WiFiDriverHal::~WiFiDriverHal() {}
88 
GetInstance()89 WiFiDriverHal* WiFiDriverHal::GetInstance() {
90   return g_wifi_driver_hal.Pointer();
91 }
92 
SetupStationModeInterface()93 string WiFiDriverHal::SetupStationModeInterface() {
94   return WiFiDriverSetupInterface(WIFI_MODE_STATION);
95 }
96 
SetupApModeInterface()97 string WiFiDriverHal::SetupApModeInterface() {
98   return WiFiDriverSetupInterface(WIFI_MODE_AP);
99 }
100 
101 }  // namespace shill
102