From 00aa56eb2fb48a1097abbf35b2a58234477672e5 Mon Sep 17 00:00:00 2001 From: Christopher Wiley Date: Fri, 24 Jun 2016 14:25:38 -0700 Subject: Allow some HAL interactions to be mocked Wrap them in a very thin class to simple mocking. While here, simplify the interface for consumers by exposing simple knobs to change the firmware mode. Bug: 29634806 Change-Id: I49f567f04f0d5b2bcbda3c308ceb4fdd37b3a8b2 Test: Compiles, wificond can consume this code in tests. --- libwifi_hal/Android.mk | 3 ++ libwifi_hal/driver_tool.cpp | 54 ++++++++++++++++++++++++++++++ libwifi_hal/include/wifi_hal/driver_tool.h | 50 +++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 libwifi_hal/driver_tool.cpp create mode 100644 libwifi_hal/include/wifi_hal/driver_tool.h diff --git a/libwifi_hal/Android.mk b/libwifi_hal/Android.mk index 879ad8711..dee5a3a85 100644 --- a/libwifi_hal/Android.mk +++ b/libwifi_hal/Android.mk @@ -96,6 +96,7 @@ endif # ============================================================ include $(CLEAR_VARS) LOCAL_MODULE := libwifi-hal +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := \ $(LOCAL_PATH)/include \ $(call include-path-for, libhardware_legacy) @@ -104,6 +105,8 @@ LOCAL_SHARED_LIBRARIES := \ liblog \ libnl \ libutils +LOCAL_SRC_FILES := \ + driver_tool.cpp LOCAL_WHOLE_STATIC_LIBRARIES := $(LIB_WIFI_HAL) libwifi-hal-common include $(BUILD_SHARED_LIBRARY) diff --git a/libwifi_hal/driver_tool.cpp b/libwifi_hal/driver_tool.cpp new file mode 100644 index 000000000..688017b5c --- /dev/null +++ b/libwifi_hal/driver_tool.cpp @@ -0,0 +1,54 @@ +/* + * 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 "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::LoadDriver() { + return ::wifi_load_driver() == 0; +} + +bool DriverTool::UnloadDriver() { + return ::wifi_unload_driver() == 0; +} + +bool DriverTool::IsDriverLoaded() { + return ::wifi_unload_driver() != 0; +} + +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 diff --git a/libwifi_hal/include/wifi_hal/driver_tool.h b/libwifi_hal/include/wifi_hal/driver_tool.h new file mode 100644 index 000000000..f1a43cc0c --- /dev/null +++ b/libwifi_hal/include/wifi_hal/driver_tool.h @@ -0,0 +1,50 @@ +/* + * 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. + */ + +#ifndef ANDROID_WIFI_SYSTEM_DRIVER_TOOL_H +#define ANDROID_WIFI_SYSTEM_DRIVER_TOOL_H + +namespace android { +namespace wifi_hal { + +// Utilities for interacting with the driver. +class DriverTool { + public: + static const int kFirmwareModeSta; + static const int kFirmwareModeAp; + static const int kFirmwareModeP2p; + + DriverTool() = default; + virtual ~DriverTool() = default; + + // These methods allow manipulation of the WiFi driver. + // They all return true on success, and false otherwise. + virtual bool LoadDriver(); + virtual bool UnloadDriver(); + virtual bool IsDriverLoaded(); + + // Change the firmware mode. + // |mode| is one of the kFirmwareMode* constants defined above. + // Returns true on success, and false otherwise. + virtual bool ChangeFirmwareMode(int mode); + +}; // class DriverTool + +} // namespace wifi_hal +} // namespace android + +#endif // ANDROID_WIFI_SYSTEM_DRIVER_TOOL_H + -- cgit v1.2.3