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/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