From 929c0d1d2abb1df2c9997c60da7941d6c44cdb2d Mon Sep 17 00:00:00 2001 From: Logan Chien Date: Fri, 21 Apr 2017 11:28:41 +0800 Subject: Split InterfaceTool from libwifi-system.so This commit extracts InterfaceTool from libwifi-system.so into a new library named libwifi-system-iface.so. libwifi-system-iface.so will be vendor_available because it will be used by both wificond and android.hardware.wifi@1.0-service. Bug: 37429084 Test: Sailfish builds, boots, and wifi works. Change-Id: Iad93c6d258507df3bbb97228814ff5ce5cc2a2f9 --- Android.bp | 1 + libwifi_system/Android.bp | 1 - .../include/wifi_system/interface_tool.h | 46 --------- libwifi_system/interface_tool.cpp | 107 --------------------- .../include/wifi_system_test/mock_interface_tool.h | 38 -------- libwifi_system_iface/Android.bp | 58 +++++++++++ .../include/wifi_system/interface_tool.h | 46 +++++++++ libwifi_system_iface/interface_tool.cpp | 107 +++++++++++++++++++++ .../include/wifi_system_test/mock_interface_tool.h | 38 ++++++++ 9 files changed, 250 insertions(+), 192 deletions(-) delete mode 100644 libwifi_system/include/wifi_system/interface_tool.h delete mode 100644 libwifi_system/interface_tool.cpp delete mode 100644 libwifi_system/testlib/include/wifi_system_test/mock_interface_tool.h create mode 100644 libwifi_system_iface/Android.bp create mode 100644 libwifi_system_iface/include/wifi_system/interface_tool.h create mode 100644 libwifi_system_iface/interface_tool.cpp create mode 100644 libwifi_system_iface/testlib/include/wifi_system_test/mock_interface_tool.h diff --git a/Android.bp b/Android.bp index 60aff26de..ca791257a 100644 --- a/Android.bp +++ b/Android.bp @@ -14,4 +14,5 @@ subdirs = [ "libwifi_system", + "libwifi_system_iface", ] diff --git a/libwifi_system/Android.bp b/libwifi_system/Android.bp index edc84dfc1..877d05f1d 100644 --- a/libwifi_system/Android.bp +++ b/libwifi_system/Android.bp @@ -41,7 +41,6 @@ cc_library_shared { ], srcs: [ "hostapd_manager.cpp", - "interface_tool.cpp", "supplicant_manager.cpp", ], } diff --git a/libwifi_system/include/wifi_system/interface_tool.h b/libwifi_system/include/wifi_system/interface_tool.h deleted file mode 100644 index aabdd9a52..000000000 --- a/libwifi_system/include/wifi_system/interface_tool.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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_INTERFACE_TOOL_H -#define ANDROID_WIFI_SYSTEM_INTERFACE_TOOL_H - -namespace android { -namespace wifi_system { - -class InterfaceTool { - public: - InterfaceTool() = default; - virtual ~InterfaceTool() = default; - - // Get the interface state of |if_name|. - // Returns true iff the interface is up. - virtual bool GetUpState(const char* if_name); - - // Set the interface named by |if_name| up or down. - // Returns true on success, false otherwise. - virtual bool SetUpState(const char* if_name, bool request_up); - - // A helpful alias for SetUpState() that assumes there is a single system - // WiFi interface. Prefer this form if you're hardcoding "wlan0" to help us - // identify all the places we are hardcoding the name of the wifi interface. - virtual bool SetWifiUpState(bool request_up); - -}; // class InterfaceTool - -} // namespace wifi_system -} // namespace android - -#endif // ANDROID_WIFI_SYSTEM_INTERFACE_TOOL_H diff --git a/libwifi_system/interface_tool.cpp b/libwifi_system/interface_tool.cpp deleted file mode 100644 index f0d40efd4..000000000 --- a/libwifi_system/interface_tool.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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_system/interface_tool.h" - -#include -#include -/* We need linux/if.h for flags like IFF_UP. Sadly, it forward declares - struct sockaddr and must be included after sys/socket.h. */ -#include - -#include -#include - -namespace android { -namespace wifi_system { -namespace { - -const char kWlan0InterfaceName[] = "wlan0"; - -bool GetIfState(const char* if_name, int sock, struct ifreq* ifr) { - memset(ifr, 0, sizeof(*ifr)); - if (strlcpy(ifr->ifr_name, if_name, sizeof(ifr->ifr_name)) >= - sizeof(ifr->ifr_name)) { - LOG(ERROR) << "Interface name is too long: " << if_name; - return false; - } - - if (TEMP_FAILURE_RETRY(ioctl(sock, SIOCGIFFLAGS, ifr)) != 0) { - LOG(ERROR) << "Could not read interface state for " << if_name - << " (" << strerror(errno) << ")"; - return false; - } - - return true; -} - -} // namespace - -bool InterfaceTool::GetUpState(const char* if_name) { - base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); - if (sock.get() < 0) { - LOG(ERROR) << "Failed to open socket to set up/down state (" - << strerror(errno) << ")"; - return false; - } - - struct ifreq ifr; - if (!GetIfState(if_name, sock.get(), &ifr)) { - return false; // logging done internally - } - - return ifr.ifr_flags & IFF_UP; -} - -bool InterfaceTool::SetUpState(const char* if_name, bool request_up) { - base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); - if (sock.get() < 0) { - LOG(ERROR) << "Failed to open socket to set up/down state (" - << strerror(errno) << ")"; - return false; - } - - struct ifreq ifr; - if (!GetIfState(if_name, sock.get(), &ifr)) { - return false; // logging done internally - } - - const bool currently_up = ifr.ifr_flags & IFF_UP; - if (currently_up == request_up) { - return true; - } - - if (request_up) { - ifr.ifr_flags |= IFF_UP; - } else { - ifr.ifr_flags &= ~IFF_UP; - } - - if (TEMP_FAILURE_RETRY(ioctl(sock.get(), SIOCSIFFLAGS, &ifr)) != 0) { - LOG(ERROR) << "Could not set interface flags for " << if_name - << " (" << strerror(errno) << ")"; - return false; - } - - return true; -} - -bool InterfaceTool::SetWifiUpState(bool request_up) { - return SetUpState(kWlan0InterfaceName, request_up); -} - -} // namespace wifi_system -} // namespace android diff --git a/libwifi_system/testlib/include/wifi_system_test/mock_interface_tool.h b/libwifi_system/testlib/include/wifi_system_test/mock_interface_tool.h deleted file mode 100644 index b9926c938..000000000 --- a/libwifi_system/testlib/include/wifi_system_test/mock_interface_tool.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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_TEST_MOCK_INTERFACE_TOOL_H -#define ANDROID_WIFI_SYSTEM_TEST_MOCK_INTERFACE_TOOL_H - -#include - -namespace android { -namespace wifi_system { - -class MockInterfaceTool : public InterfaceTool { - public: - ~MockInterfaceTool() override = default; - - MOCK_METHOD1(GetUpState, bool(const char* if_name)); - MOCK_METHOD2(SetUpState, bool(const char* if_name, bool request_up)); - MOCK_METHOD1(SetWifiUpState, bool(bool request_up)); - -}; // class MockInterfaceTool - -} // namespace wifi_system -} // namespace android - -#endif // ANDROID_WIFI_SYSTEM_TEST_MOCK_INTERFACE_TOOL_H diff --git a/libwifi_system_iface/Android.bp b/libwifi_system_iface/Android.bp new file mode 100644 index 000000000..4be0aa01c --- /dev/null +++ b/libwifi_system_iface/Android.bp @@ -0,0 +1,58 @@ +// Copyright (C) 2017 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. + +wifi_system_iface_cflags = [ + "-Wall", + "-Werror", + "-Wextra", + "-Winit-self", + "-Wno-unused-function", + "-Wno-unused-parameter", + "-Wshadow", + "-Wunused-variable", + "-Wwrite-strings", +] + +// Device independent wifi system logic. +// ============================================================ +cc_library_shared { + name: "libwifi-system-iface", + vendor_available: true, + cflags: wifi_system_iface_cflags, + local_include_dirs: ["include"], + export_include_dirs: ["include"], + shared_libs: [ + "libbase", + ], + + srcs: [ + "interface_tool.cpp", + ], +} + +// Test utilities (e.g. mock classes) for libwifi-system-iface +// ============================================================ +cc_library_static { + name: "libwifi-system-iface-test", + cflags: wifi_system_iface_cflags, + local_include_dirs: [ + "include", + "testlib/include", + ], + static_libs: ["libgmock"], + export_include_dirs: [ + "include", + "testlib/include", + ], +} diff --git a/libwifi_system_iface/include/wifi_system/interface_tool.h b/libwifi_system_iface/include/wifi_system/interface_tool.h new file mode 100644 index 000000000..aabdd9a52 --- /dev/null +++ b/libwifi_system_iface/include/wifi_system/interface_tool.h @@ -0,0 +1,46 @@ +/* + * 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_INTERFACE_TOOL_H +#define ANDROID_WIFI_SYSTEM_INTERFACE_TOOL_H + +namespace android { +namespace wifi_system { + +class InterfaceTool { + public: + InterfaceTool() = default; + virtual ~InterfaceTool() = default; + + // Get the interface state of |if_name|. + // Returns true iff the interface is up. + virtual bool GetUpState(const char* if_name); + + // Set the interface named by |if_name| up or down. + // Returns true on success, false otherwise. + virtual bool SetUpState(const char* if_name, bool request_up); + + // A helpful alias for SetUpState() that assumes there is a single system + // WiFi interface. Prefer this form if you're hardcoding "wlan0" to help us + // identify all the places we are hardcoding the name of the wifi interface. + virtual bool SetWifiUpState(bool request_up); + +}; // class InterfaceTool + +} // namespace wifi_system +} // namespace android + +#endif // ANDROID_WIFI_SYSTEM_INTERFACE_TOOL_H diff --git a/libwifi_system_iface/interface_tool.cpp b/libwifi_system_iface/interface_tool.cpp new file mode 100644 index 000000000..f0d40efd4 --- /dev/null +++ b/libwifi_system_iface/interface_tool.cpp @@ -0,0 +1,107 @@ +/* + * 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_system/interface_tool.h" + +#include +#include +/* We need linux/if.h for flags like IFF_UP. Sadly, it forward declares + struct sockaddr and must be included after sys/socket.h. */ +#include + +#include +#include + +namespace android { +namespace wifi_system { +namespace { + +const char kWlan0InterfaceName[] = "wlan0"; + +bool GetIfState(const char* if_name, int sock, struct ifreq* ifr) { + memset(ifr, 0, sizeof(*ifr)); + if (strlcpy(ifr->ifr_name, if_name, sizeof(ifr->ifr_name)) >= + sizeof(ifr->ifr_name)) { + LOG(ERROR) << "Interface name is too long: " << if_name; + return false; + } + + if (TEMP_FAILURE_RETRY(ioctl(sock, SIOCGIFFLAGS, ifr)) != 0) { + LOG(ERROR) << "Could not read interface state for " << if_name + << " (" << strerror(errno) << ")"; + return false; + } + + return true; +} + +} // namespace + +bool InterfaceTool::GetUpState(const char* if_name) { + base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); + if (sock.get() < 0) { + LOG(ERROR) << "Failed to open socket to set up/down state (" + << strerror(errno) << ")"; + return false; + } + + struct ifreq ifr; + if (!GetIfState(if_name, sock.get(), &ifr)) { + return false; // logging done internally + } + + return ifr.ifr_flags & IFF_UP; +} + +bool InterfaceTool::SetUpState(const char* if_name, bool request_up) { + base::unique_fd sock(socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)); + if (sock.get() < 0) { + LOG(ERROR) << "Failed to open socket to set up/down state (" + << strerror(errno) << ")"; + return false; + } + + struct ifreq ifr; + if (!GetIfState(if_name, sock.get(), &ifr)) { + return false; // logging done internally + } + + const bool currently_up = ifr.ifr_flags & IFF_UP; + if (currently_up == request_up) { + return true; + } + + if (request_up) { + ifr.ifr_flags |= IFF_UP; + } else { + ifr.ifr_flags &= ~IFF_UP; + } + + if (TEMP_FAILURE_RETRY(ioctl(sock.get(), SIOCSIFFLAGS, &ifr)) != 0) { + LOG(ERROR) << "Could not set interface flags for " << if_name + << " (" << strerror(errno) << ")"; + return false; + } + + return true; +} + +bool InterfaceTool::SetWifiUpState(bool request_up) { + return SetUpState(kWlan0InterfaceName, request_up); +} + +} // namespace wifi_system +} // namespace android diff --git a/libwifi_system_iface/testlib/include/wifi_system_test/mock_interface_tool.h b/libwifi_system_iface/testlib/include/wifi_system_test/mock_interface_tool.h new file mode 100644 index 000000000..b9926c938 --- /dev/null +++ b/libwifi_system_iface/testlib/include/wifi_system_test/mock_interface_tool.h @@ -0,0 +1,38 @@ +/* + * 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_TEST_MOCK_INTERFACE_TOOL_H +#define ANDROID_WIFI_SYSTEM_TEST_MOCK_INTERFACE_TOOL_H + +#include + +namespace android { +namespace wifi_system { + +class MockInterfaceTool : public InterfaceTool { + public: + ~MockInterfaceTool() override = default; + + MOCK_METHOD1(GetUpState, bool(const char* if_name)); + MOCK_METHOD2(SetUpState, bool(const char* if_name, bool request_up)); + MOCK_METHOD1(SetWifiUpState, bool(bool request_up)); + +}; // class MockInterfaceTool + +} // namespace wifi_system +} // namespace android + +#endif // ANDROID_WIFI_SYSTEM_TEST_MOCK_INTERFACE_TOOL_H -- cgit v1.2.3