hostapd(hidl): Add HIDL interface skeletal code

Adding support for HIDL IPC in hostapd. This CL only adds the skeletal
framework for supporting HIDL interface implementation.

There is also a change in the hostapd.h to fix a C++ compilation error.

Bug: 36646171
Test: Compiles with CONFIG_CTRL_IFACE_HIDL=y in android.config
Change-Id: I4a7047a56f8e9be1ed51f2315df707ef5daddc11
diff --git a/hostapd/Android.mk b/hostapd/Android.mk
index bbc9c0b..77533d7 100644
--- a/hostapd/Android.mk
+++ b/hostapd/Android.mk
@@ -1069,6 +1069,12 @@
 OBJS_c += src/utils/edit_simple.c
 endif
 
+ifdef CONFIG_CTRL_IFACE_HIDL
+HOSTAPD_USE_HIDL=y
+L_CFLAGS += -DCONFIG_CTRL_IFACE_HIDL
+L_CPPFLAGS = -Wall -Werror
+endif
+
 ########################
 
 include $(CLEAR_VARS)
@@ -1086,6 +1092,7 @@
 LOCAL_MODULE := hostapd
 LOCAL_MODULE_TAGS := optional
 LOCAL_PROPRIETARY_MODULE := true
+LOCAL_MODULE_RELATIVE_PATH := hw
 ifdef CONFIG_DRIVER_CUSTOM
 LOCAL_STATIC_LIBRARIES := libCustomWifi
 endif
@@ -1100,10 +1107,39 @@
 LOCAL_STATIC_LIBRARIES += libnl_2
 endif
 endif
+ifeq ($(HOSTAPD_USE_HIDL), y)
+LOCAL_SHARED_LIBRARIES += android.hardware.wifi.hostapd@1.0
+LOCAL_SHARED_LIBRARIES += libhidlbase libhidltransport libhwbinder libutils
+LOCAL_STATIC_LIBRARIES += libhostapd_hidl
+endif
 LOCAL_CFLAGS := $(L_CFLAGS)
 LOCAL_SRC_FILES := $(OBJS)
 LOCAL_C_INCLUDES := $(INCLUDES)
 LOCAL_INIT_RC := hostapd.android.rc
 include $(BUILD_EXECUTABLE)
 
+ifeq ($(HOSTAPD_USE_HIDL), y)
+### Hidl service library ###
+########################
+include $(CLEAR_VARS)
+LOCAL_MODULE := libhostapd_hidl
+LOCAL_VENDOR_MODULE := true
+LOCAL_CPPFLAGS := $(L_CPPFLAGS)
+LOCAL_CFLAGS := $(L_CFLAGS)
+LOCAL_C_INCLUDES := $(INCLUDES)
+HIDL_INTERFACE_VERSION = 1.0
+LOCAL_SRC_FILES := \
+    hidl/$(HIDL_INTERFACE_VERSION)/hidl.cpp \
+    hidl/$(HIDL_INTERFACE_VERSION)/hostapd.cpp
+LOCAL_SHARED_LIBRARIES := \
+    android.hardware.wifi.hostapd@1.0 \
+    libhidlbase \
+    libhidltransport \
+    libhwbinder \
+    libutils \
+    liblog
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+    $(LOCAL_PATH)/hidl/$(HIDL_INTERFACE_VERSION)
+include $(BUILD_STATIC_LIBRARY)
+endif # HOSTAPD_USE_HIDL == y
 endif # ifeq ($(WPA_BUILD_HOSTAPD),true)
diff --git a/hostapd/CleanSpec.mk b/hostapd/CleanSpec.mk
index 653aebb..5d50cd4 100644
--- a/hostapd/CleanSpec.mk
+++ b/hostapd/CleanSpec.mk
@@ -49,4 +49,5 @@
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
 # ************************************************
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/bin/hostapd)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/bin/hostapd)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/bin/hostapd_cli)
diff --git a/hostapd/android.config b/hostapd/android.config
index 08d21f0..3e40aa1 100644
--- a/hostapd/android.config
+++ b/hostapd/android.config
@@ -210,3 +210,7 @@
 # /dev/urandom earlier in boot' seeds /dev/urandom with that entropy before
 # either wpa_supplicant or hostapd are run.
 CONFIG_NO_RANDOM_POOL=y
+
+# Add support for Hidl control interface
+# Only applicable for Android platforms.
+CONFIG_CTRL_IFACE_HIDL=n
diff --git a/hostapd/hidl/.clang-format b/hostapd/hidl/.clang-format
new file mode 100644
index 0000000..42fadb5
--- /dev/null
+++ b/hostapd/hidl/.clang-format
@@ -0,0 +1,9 @@
+BasedOnStyle: Google
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Mozilla
+AllowShortIfStatementsOnASingleLine: false
+IndentCaseLabels: false
+AccessModifierOffset: -8
+AlignAfterOpenBracket: AlwaysBreak
+SortIncludes: false
diff --git a/hostapd/hidl/1.0/hidl.cpp b/hostapd/hidl/1.0/hidl.cpp
new file mode 100644
index 0000000..1f80c7d
--- /dev/null
+++ b/hostapd/hidl/1.0/hidl.cpp
@@ -0,0 +1,71 @@
+/*
+ * hidl interface for wpa_supplicant daemon
+ * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2018, Roshan Pius <rpius@google.com>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#include <hwbinder/IPCThreadState.h>
+#include <hidl/HidlTransportSupport.h>
+
+#include "hostapd.h"
+
+extern "C"
+{
+#include "hidl.h"
+#include "utils/common.h"
+#include "utils/eloop.h"
+#include "utils/includes.h"
+}
+
+using android::hardware::configureRpcThreadpool;
+using android::hardware::IPCThreadState;
+using android::hardware::wifi::hostapd::V1_0::IHostapd;
+using android::hardware::wifi::hostapd::V1_0::implementation::Hostapd;
+
+// This file is a bridge between the hostapd code written in 'C' and the HIDL
+// interface in C++. So, using "C" style static globals here!
+static int hidl_fd = -1;
+static android::sp<IHostapd> service;
+
+void hostapd_hidl_sock_handler(
+    int /* sock */, void * /* eloop_ctx */, void * /* sock_ctx */)
+{
+	IPCThreadState::self()->handlePolledCommands();
+}
+
+int hostapd_hidl_init(struct hapd_interfaces *interfaces)
+{
+	wpa_printf(MSG_DEBUG, "Initing hidl control");
+
+	IPCThreadState::self()->disableBackgroundScheduling(true);
+	IPCThreadState::self()->setupPolling(&hidl_fd);
+	if (hidl_fd < 0)
+		goto err;
+
+	wpa_printf(MSG_INFO, "Processing hidl events on FD %d", hidl_fd);
+	// Look for read events from the hidl socket in the eloop.
+	if (eloop_register_read_sock(
+		hidl_fd, hostapd_hidl_sock_handler, interfaces, NULL) < 0)
+		goto err;
+	service = new Hostapd(interfaces);
+	if (!service)
+		goto err;
+	if (service->registerAsService() != android::NO_ERROR)
+		goto err;
+	return 0;
+err:
+	hostapd_hidl_deinit(interfaces);
+	return -1;
+}
+
+void hostapd_hidl_deinit(struct hapd_interfaces *interfaces)
+{
+	wpa_printf(MSG_DEBUG, "Deiniting hidl control");
+	eloop_unregister_read_sock(hidl_fd);
+	IPCThreadState::shutdown();
+	hidl_fd = -1;
+	service.clear();
+}
diff --git a/hostapd/hidl/1.0/hidl.h b/hostapd/hidl/1.0/hidl.h
new file mode 100644
index 0000000..5decf64
--- /dev/null
+++ b/hostapd/hidl/1.0/hidl.h
@@ -0,0 +1,30 @@
+/*
+ * hidl interface for wpa_supplicant daemon
+ * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2018, Roshan Pius <rpius@google.com>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#ifndef HOSTAPD_HIDL_HIDL_H
+#define HOSTAPD_HIDL_HIDL_H
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif  // _cplusplus
+#include "ap/hostapd.h"
+
+/**
+ * This is the hidl RPC interface entry point to the hostapd core.
+ * This initializes the hidl driver & IHostapd instance.
+ */
+int hostapd_hidl_init(struct hapd_interfaces *interfaces);
+void hostapd_hidl_deinit(struct hapd_interfaces *interfaces);
+
+#ifdef __cplusplus
+}
+#endif  // _cplusplus
+
+#endif  // HOSTAPD_HIDL_HIDL_H
diff --git a/hostapd/hidl/1.0/hidl_return_util.h b/hostapd/hidl/1.0/hidl_return_util.h
new file mode 100644
index 0000000..1625dc2
--- /dev/null
+++ b/hostapd/hidl/1.0/hidl_return_util.h
@@ -0,0 +1,44 @@
+/*
+ * hidl interface for wpa_hostapd daemon
+ * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2018, Roshan Pius <rpius@google.com>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#ifndef HIDL_RETURN_UTIL_H_
+#define HIDL_RETURN_UTIL_H_
+
+#include <functional>
+
+namespace android {
+namespace hardware {
+namespace wifi {
+namespace hostapd {
+namespace V1_0 {
+namespace implementation {
+namespace hidl_return_util {
+
+/**
+ * These utility functions are used to invoke a method on the provided
+ * HIDL interface object.
+ */
+// Use for HIDL methods which return only an instance of HostapdStatus.
+template <typename ObjT, typename WorkFuncT, typename... Args>
+Return<void> call(
+    ObjT* obj, WorkFuncT&& work,
+    const std::function<void(const HostapdStatus&)>& hidl_cb, Args&&... args)
+{
+	hidl_cb((obj->*work)(std::forward<Args>(args)...));
+	return Void();
+}
+
+}  // namespace hidl_return_util
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace hostapd
+}  // namespace wifi
+}  // namespace hardware
+}  // namespace android
+#endif  // HIDL_RETURN_UTIL_H_
diff --git a/hostapd/hidl/1.0/hostapd.cpp b/hostapd/hidl/1.0/hostapd.cpp
new file mode 100644
index 0000000..8834a82
--- /dev/null
+++ b/hostapd/hidl/1.0/hostapd.cpp
@@ -0,0 +1,55 @@
+/*
+ * hidl interface for wpa_hostapd daemon
+ * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2018, Roshan Pius <rpius@google.com>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#include "hostapd.h"
+#include "hidl_return_util.h"
+
+namespace android {
+namespace hardware {
+namespace wifi {
+namespace hostapd {
+namespace V1_0 {
+namespace implementation {
+using hidl_return_util::call;
+
+Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces)
+{}
+
+Return<void> Hostapd::addAccessPoint(
+    const IfaceParams& iface_params, const NetworkParams& nw_params,
+    addAccessPoint_cb _hidl_cb)
+{
+	return call(
+	    this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params,
+	    nw_params);
+}
+
+Return<void> Hostapd::removeAccessPoint(
+    const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb)
+{
+	return call(
+	    this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name);
+}
+
+HostapdStatus Hostapd::addAccessPointInternal(
+    const IfaceParams& iface_params, const NetworkParams& nw_params)
+{
+	return {HostapdStatusCode::SUCCESS, ""};
+}
+
+HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name)
+{
+	return {HostapdStatusCode::SUCCESS, ""};
+}
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace hostapd
+}  // namespace wifi
+}  // namespace hardware
+}  // namespace android
diff --git a/hostapd/hidl/1.0/hostapd.h b/hostapd/hidl/1.0/hostapd.h
new file mode 100644
index 0000000..d4f632e
--- /dev/null
+++ b/hostapd/hidl/1.0/hostapd.h
@@ -0,0 +1,71 @@
+/*
+ * hidl interface for wpa_hostapd daemon
+ * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2018, Roshan Pius <rpius@google.com>
+ *
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
+ */
+
+#ifndef HOSTAPD_HIDL_SUPPLICANT_H
+#define HOSTAPD_HIDL_SUPPLICANT_H
+
+#include <string>
+
+#include <android-base/macros.h>
+
+#include <android/hardware/wifi/hostapd/1.0/IHostapd.h>
+
+extern "C"
+{
+#include "utils/common.h"
+#include "utils/includes.h"
+#include "utils/wpa_debug.h"
+#include "ap/hostapd.h"
+}
+
+namespace android {
+namespace hardware {
+namespace wifi {
+namespace hostapd {
+namespace V1_0 {
+namespace implementation {
+
+/**
+ * Implementation of the hostapd hidl object. This hidl
+ * object is used core for global control operations on
+ * hostapd.
+ */
+class Hostapd : public V1_0::IHostapd
+{
+public:
+	Hostapd(hapd_interfaces* interfaces);
+	~Hostapd() override = default;
+
+	// Hidl methods exposed.
+	Return<void> addAccessPoint(
+	    const IfaceParams& iface_params, const NetworkParams& nw_params,
+	    addAccessPoint_cb _hidl_cb) override;
+	Return<void> removeAccessPoint(
+	    const hidl_string& iface_name,
+	    removeAccessPoint_cb _hidl_cb) override;
+
+private:
+	// Corresponding worker functions for the HIDL methods.
+	HostapdStatus addAccessPointInternal(
+	    const IfaceParams& iface_params, const NetworkParams& nw_params);
+	HostapdStatus removeAccessPointInternal(const std::string& iface_name);
+
+	// Raw pointer to the global structure maintained by the core.
+	struct hapd_interfaces* interfaces_;
+
+	DISALLOW_COPY_AND_ASSIGN(Hostapd);
+};
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace hostapd
+}  // namespace wifi
+}  // namespace hardware
+}  // namespace android
+
+#endif  // HOSTAPD_HIDL_SUPPLICANT_H
diff --git a/hostapd/hostapd.android.rc b/hostapd/hostapd.android.rc
index 36b00af..85c63d0 100644
--- a/hostapd/hostapd.android.rc
+++ b/hostapd/hostapd.android.rc
@@ -7,13 +7,12 @@
 #
 
 on post-fs-data
-    mkdir /data/misc/wifi/hostapd 0770 wifi wifi
+    mkdir /data/vendor/wifi 0770 wifi wifi
+    mkdir /data/vendor/wifi/hostapd 0770 wifi wifi
 
-service hostapd /vendor/bin/hostapd \
-        /data/misc/wifi/hostapd.conf
+service hostapd /vendor/bin/hw/hostapd
     class main
     user wifi
     group wifi net_raw net_admin
-    writepid /data/misc/wifi/hostapd.pid
     disabled
     oneshot
diff --git a/hostapd/main.c b/hostapd/main.c
index ce94d05..8d90b38 100644
--- a/hostapd/main.c
+++ b/hostapd/main.c
@@ -28,7 +28,9 @@
 #include "config_file.h"
 #include "eap_register.h"
 #include "ctrl_iface.h"
-
+#ifdef CONFIG_CTRL_IFACE_HIDL
+#include "hidl.h"
+#endif /* CONFIG_CTRL_IFACE_HIDL */
 
 struct hapd_global {
 	void **drv_priv;
@@ -659,8 +661,14 @@
 	interfaces.reload_config = hostapd_reload_config;
 	interfaces.config_read_cb = hostapd_config_read;
 	interfaces.for_each_interface = hostapd_for_each_interface;
+#ifdef CONFIG_CTRL_IFACE_HIDL
+	// No per-netdev setup for HIDL control interface.
+	interfaces.ctrl_iface_init = NULL;
+	interfaces.ctrl_iface_deinit = NULL;
+#else /* CONFIG_CTRL_IFACE_HIDL */
 	interfaces.ctrl_iface_init = hostapd_ctrl_iface_init;
 	interfaces.ctrl_iface_deinit = hostapd_ctrl_iface_deinit;
+#endif /* CONFIG_CTRL_IFACE_HIDL */
 	interfaces.driver_init = hostapd_driver_init;
 	interfaces.global_iface_path = NULL;
 	interfaces.global_iface_name = NULL;
@@ -751,9 +759,11 @@
 		}
 	}
 
+#ifndef CONFIG_CTRL_IFACE_HIDL
 	if (optind == argc && interfaces.global_iface_path == NULL &&
 	    num_bss_configs == 0)
 		usage();
+#endif
 
 	wpa_msg_register_ifname_cb(hostapd_msg_ifname_cb);
 
@@ -893,7 +903,14 @@
 			goto out;
 	}
 
+#ifdef CONFIG_CTRL_IFACE_HIDL
+	if (hostapd_hidl_init(&interfaces)) {
+		wpa_printf(MSG_ERROR, "Failed to initialize HIDL interface");
+		goto out;
+	}
+#else /* CONFIG_CTRL_IFACE_HIDL */
 	hostapd_global_ctrl_iface_init(&interfaces);
+#endif /* CONFIG_CTRL_IFACE_HIDL */
 
 	if (hostapd_global_run(&interfaces, daemonize, pid_file)) {
 		wpa_printf(MSG_ERROR, "Failed to start eloop");
@@ -903,7 +920,11 @@
 	ret = 0;
 
  out:
+#ifdef CONFIG_CTRL_IFACE_HIDL
+	hostapd_hidl_deinit(&interfaces);
+#else /* CONFIG_CTRL_IFACE_HIDL */
 	hostapd_global_ctrl_iface_deinit(&interfaces);
+#endif /* CONFIG_CTRL_IFACE_HIDL */
 	/* Deinitialize all interfaces */
 	for (i = 0; i < interfaces.count; i++) {
 		if (!interfaces.iface[i])
diff --git a/src/ap/hostapd.h b/src/ap/hostapd.h
index f77e6ec..d7c6720 100644
--- a/src/ap/hostapd.h
+++ b/src/ap/hostapd.h
@@ -372,6 +372,16 @@
 #endif /* CONFIG_TAXONOMY */
 };
 
+enum hostapd_iface_state {
+	HAPD_IFACE_UNINITIALIZED,
+	HAPD_IFACE_DISABLED,
+	HAPD_IFACE_COUNTRY_UPDATE,
+	HAPD_IFACE_ACS,
+	HAPD_IFACE_HT_SCAN,
+	HAPD_IFACE_DFS,
+	HAPD_IFACE_ENABLED
+};
+
 /**
  * struct hostapd_iface - hostapd per-interface data structure
  */
@@ -382,16 +392,7 @@
 	struct hostapd_config *conf;
 	char phy[16]; /* Name of the PHY (radio) */
 
-	enum hostapd_iface_state {
-		HAPD_IFACE_UNINITIALIZED,
-		HAPD_IFACE_DISABLED,
-		HAPD_IFACE_COUNTRY_UPDATE,
-		HAPD_IFACE_ACS,
-		HAPD_IFACE_HT_SCAN,
-		HAPD_IFACE_DFS,
-		HAPD_IFACE_ENABLED
-	} state;
-
+        enum hostapd_iface_state state;
 #ifdef CONFIG_MESH
 	struct mesh_conf *mconf;
 #endif /* CONFIG_MESH */