summaryrefslogtreecommitdiffstats
path: root/libwifi_hal/driver_tool.cpp
blob: 3089ee0a4ce5c1592ffc982a1f7676825f20c5df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "wifi_hal/driver_tool.h"

#include <android-base/logging.h>
#include <private/android_filesystem_config.h>

#include "hardware_legacy/wifi.h"

namespace android {
namespace wifi_hal {

const int DriverTool::kFirmwareModeSta = WIFI_GET_FW_PATH_STA;
const int DriverTool::kFirmwareModeAp = WIFI_GET_FW_PATH_AP;
const int DriverTool::kFirmwareModeP2p = WIFI_GET_FW_PATH_P2P;

bool DriverTool::TakeOwnershipOfFirmwareReload() {
  if (!wifi_get_fw_path(kFirmwareModeSta) &&
      !wifi_get_fw_path(kFirmwareModeAp) &&
      !wifi_get_fw_path(kFirmwareModeP2p)) {
    return true;  // HAL doesn't think we need to load firmware for any mode.
  }

  if (chown(WIFI_DRIVER_FW_PATH_PARAM, AID_WIFI, AID_WIFI) != 0) {
    PLOG(ERROR) << "Error changing ownership of '" << WIFI_DRIVER_FW_PATH_PARAM
                << "' to wifi:wifi";
    return false;
  }

  return true;
}

bool DriverTool::LoadDriver() {
  return ::wifi_load_driver() == 0;
}

bool DriverTool::UnloadDriver() {
  return ::wifi_unload_driver() == 0;
}

bool DriverTool::IsDriverLoaded() {
  return ::wifi_unload_driver() != 0;
}

bool DriverTool::IsFirmwareModeChangeNeeded(int mode) {
  return (wifi_get_fw_path(mode) != nullptr);
}

bool DriverTool::ChangeFirmwareMode(int mode) {
  const char* fwpath = wifi_get_fw_path(mode);
  if (!fwpath) {
    return true;  // HAL doesn't think we need to load firmware for this mode.
  }
  if (wifi_change_fw_path(fwpath) != 0) {
    // Not all devices actually require firmware reloads, but
    // failure to change the firmware path when it is defined is an error.
    return false;
  }
  return true;
}

}  // namespace wifi_hal
}  // namespace android