From 1e0fa58f54cfbe407b3cc90e5e526ef47305c647 Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 12:13:17 -0700
Subject: [PATCH 1/7] Ensure wpa client lib dependency is installed.

---
 development/docker/images/netremote/dev/hostapd/Dockerfile | 5 +++--
 linux/README.md                                            | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/development/docker/images/netremote/dev/hostapd/Dockerfile b/development/docker/images/netremote/dev/hostapd/Dockerfile
index 223685af..8ae59e74 100644
--- a/development/docker/images/netremote/dev/hostapd/Dockerfile
+++ b/development/docker/images/netremote/dev/hostapd/Dockerfile
@@ -6,10 +6,11 @@ FROM abeltrano/netremote-dev
 RUN sudo apt-get update && \
     sudo apt-get install -y -qq --no-install-recommends \
         # hostapd build dependencies.
-        # libnl-3-dev libssl-dev libnl-genl-3-dev
+        # libnl-3-dev libssl-dev libwpa-client-dev libnl-genl-3-dev
         libnl-3-dev \
-        libssl-dev \
         libnl-genl-3-dev \
+        libssl-dev \
+        libwpa-client-dev \
         # wpa_supplicant build dependencies.
         # libnl-3-dev libssl-dev libnl-genl-3-dev libdbus-c++-dev libnl-route-3-dev
         libdbus-c++-dev \
diff --git a/linux/README.md b/linux/README.md
index f1bc6061..7931e97e 100644
--- a/linux/README.md
+++ b/linux/README.md
@@ -64,7 +64,7 @@ sudo apt-get install -y build-essential git ninja-build clang clang-format clang
 The above will install all the tools required to compile, lint, and debug the project. If hostapd will also be compiled ([git://w1.fi/hostap.git](git://w1.fi/hostap.git)), install the following additional development dependencies:
 
 ```bash
-sudo apt-get install -y libnl-3-dev libssl-dev libnl-genl-3-dev
+sudo apt-get install -y libnl-3-dev libssl-dev libwpa-client-dev libnl-genl-3-dev 
 ```
 
 ### 3. Install Docker on Windows

From 3c76273dff7ee0e8b7850ddf773ae321d428dec3 Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 12:18:22 -0700
Subject: [PATCH 2/7] Add initial bits to compile hostap project.

---
 CMakeLists.txt                          | 58 +++++++++++++++++++++++++
 cmake/hostap.cmake                      | 13 ++++++
 external/hostap/.config                 |  2 +
 external/hostap/CMakeLists.txt          | 23 ++++++++++
 linux/CMakeLists.txt                    |  1 +
 linux/hostap-controller/CMakeLists.txt  | 16 +++++++
 linux/hostap-controller/Placeholder.cxx |  4 ++
 windows/CMakeLists.txt                  |  0
 8 files changed, 117 insertions(+)
 create mode 100644 cmake/hostap.cmake
 create mode 100644 external/hostap/.config
 create mode 100644 external/hostap/CMakeLists.txt
 create mode 100644 linux/CMakeLists.txt
 create mode 100644 linux/hostap-controller/CMakeLists.txt
 create mode 100644 linux/hostap-controller/Placeholder.cxx
 create mode 100644 windows/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0f1527d4..8b3db929 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,6 +13,25 @@ project(netremote
   VERSION 0.0.1
 )
 
+# Conditional inclusion of OS-dependent source trees.
+if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
+  set(BUILD_FOR_LINUX TRUE)
+elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
+  set(BUILD_FOR_WINDOWS TRUE)
+else ()
+  MESSAGE(FATAL_ERROR "No supported target OS detected, SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
+endif()
+
+MESSAGE(STATUS "Target OS ${CMAKE_SYSTEM_NAME} detected")
+
+# Option determining whether the hostapd client library should be built from
+# source, or a pre-built, packaged version discovered on the system.
+option(
+  BUILD_HOSTAP_EXTERNAL
+  "Build hostap project from built-in external source"
+  ON
+)
+
 # Set language configutation options. The C++ standard used must be the lowest
 # common denominator for all the OS-dependent projects. In practice, since this
 # project must build under the Windows build system (build.exe), its toolchain
@@ -27,9 +46,11 @@ set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
 
 include(CheckPIESupported)
 include(CTest)
+include(ExternalProject)
 include(GNUInstallDirs)
 include(grpc)
 include(protoc)
+include(hostap)
 
 # Enable verbose output until project is bootstrapped.
 set(CMAKE_VERBOSE_MAKEFILE ON)
@@ -108,6 +129,43 @@ if (CMAKE_BUILD_TYPE MATCHES "(Release|RelWithDebInfo|MinSizeRel)")
   add_compile_definitions(_FORTIFY_SOURCE=2)
 endif()
 
+# Common source directories
+set(NETREMOTE_DIR_SRC ${CMAKE_CURRENT_LIST_DIR}/src)
+set(NETREMOTE_DIR_LINUX ${CMAKE_CURRENT_LIST_DIR}/linux)
+set(NETREMOTE_DIR_WINDOWS ${CMAKE_CURRENT_LIST_DIR}/windows)
+
+# Add common, global compile definitions.
+add_compile_definitions(
+    MAGIC_ENUM_RANGE_MIN=0
+    MAGIC_ENUM_RANGE_MAX=255
+)
+
+# Conditional inclusion of OS-dependent source trees.
+if (BUILD_FOR_LINUX)
+  add_subdirectory(linux)
+elseif (BUILD_FOR_WINDOWS)
+  # Make public version of wil available.
+  set(WIL_BUILD_TESTS OFF CACHE INTERNAL "Turn off wil tests")
+  set(WIL_BUILD_PACKAGING OFF CACHE INTERNAL "Turn off wil packaging")
+
+  find_package(wil CONFIG REQUIRED)
+
+  # Add Windows-specific global compile definitions.
+  add_compile_definitions(
+      _UNICODE
+      UNICODE
+      NOMINMAX
+      WIN32_LEAN_AND_MEAN
+  )
+
+  # Allow more than default number of sections in object files.
+  add_compile_options(
+    /bigobj
+  )
+
+  add_subdirectory(windows)
+endif()
+
 add_subdirectory(protocol)
 add_subdirectory(src)
 add_subdirectory(tests)
diff --git a/cmake/hostap.cmake b/cmake/hostap.cmake
new file mode 100644
index 00000000..45e00f96
--- /dev/null
+++ b/cmake/hostap.cmake
@@ -0,0 +1,13 @@
+
+if (BUILD_HOSTAP_EXTERNAL)
+    add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../external/hostap)
+else()
+    find_library(LIBWPA_CLIENT
+        NAMES libwpa_client.so
+        REQUIRED)
+endif()
+
+if(LIBWPA_CLIENT)
+    set(LIBWPA_CLIENT_TARGET ${LIBWPA_CLIENT})
+    MESSAGE(STATUS "Found wpa client: ${LIBWPA_CLIENT}")
+endif()
diff --git a/external/hostap/.config b/external/hostap/.config
new file mode 100644
index 00000000..e57b0b2b
--- /dev/null
+++ b/external/hostap/.config
@@ -0,0 +1,2 @@
+CONFIG_CTRL_IFACE=y
+CONFIG_BUILD_WPA_CLIENT_SO=y
\ No newline at end of file
diff --git a/external/hostap/CMakeLists.txt b/external/hostap/CMakeLists.txt
new file mode 100644
index 00000000..e7bc4f2d
--- /dev/null
+++ b/external/hostap/CMakeLists.txt
@@ -0,0 +1,23 @@
+
+find_program(MAKE_EXE 
+    NAMES gmake nmake make
+    REQUIRED
+)
+
+set(HOSTAP_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/hostap-prefix/src/hostap/wpa_supplicant)
+
+ExternalProject_Add(hostap
+    GIT_REPOSITORY "http://w1.fi/hostap.git"
+    GIT_TAG "hostap_2_10"
+    CONFIGURE_COMMAND cp -n ${CMAKE_CURRENT_SOURCE_DIR}/.config ${HOSTAP_PREFIX}/.config
+    BINARY_DIR ${HOSTAP_PREFIX}
+    BUILD_COMMAND QUIET=1 ${MAKE_EXE} libwpa_client.so
+    INSTALL_DIR ${HOSTAP_PREFIX}
+    INSTALL_COMMAND QUIET=1 DESTDIR=${HOSTAP_PREFIX} ${MAKE_EXE} install
+)
+
+set(LIBWPA_CLIENT
+    ${HOSTAP_PREFIX}/libwpa_client.so
+    CACHE FILEPATH
+    "wpa client shared object"
+)
diff --git a/linux/CMakeLists.txt b/linux/CMakeLists.txt
new file mode 100644
index 00000000..e31076a5
--- /dev/null
+++ b/linux/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(hostap-controller)
\ No newline at end of file
diff --git a/linux/hostap-controller/CMakeLists.txt b/linux/hostap-controller/CMakeLists.txt
new file mode 100644
index 00000000..866ae640
--- /dev/null
+++ b/linux/hostap-controller/CMakeLists.txt
@@ -0,0 +1,16 @@
+
+add_library(hostap-controller SHARED "")
+
+if (BUILD_HOSTAP_EXTERNAL)
+    add_dependencies(hostap-controller hostap)
+endif()
+
+target_sources(hostap-controller
+    PRIVATE
+        Placeholder.cxx    
+)
+
+target_link_libraries(hostap-controller
+    INTERFACE
+        ${LIBWPA_CLIENT_TARGET}
+)
diff --git a/linux/hostap-controller/Placeholder.cxx b/linux/hostap-controller/Placeholder.cxx
new file mode 100644
index 00000000..4631bc9e
--- /dev/null
+++ b/linux/hostap-controller/Placeholder.cxx
@@ -0,0 +1,4 @@
+
+#include <cstddef>
+
+#include <wpa_ctrl.h>
diff --git a/windows/CMakeLists.txt b/windows/CMakeLists.txt
new file mode 100644
index 00000000..e69de29b

From a04234619d7cd72c156b536f147b20c6b69361ea Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 12:18:36 -0700
Subject: [PATCH 3/7] Add wil for windows source.

---
 vcpkg.json | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/vcpkg.json b/vcpkg.json
index 450355b5..ea38c0ce 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -8,6 +8,10 @@
     },
     "protobuf",
     "catch2",
-    "magic-enum"
+    "magic-enum",
+    {
+      "name": "wil",
+      "platform": "windows"
+    }
   ]
 }
\ No newline at end of file

From 1dde0a1573291942e132c59da3bad3052f0ad202 Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 12:32:04 -0700
Subject: [PATCH 4/7] Export persistent internal vars for external-project
 hostap paths.

---
 external/hostap/CMakeLists.txt | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/external/hostap/CMakeLists.txt b/external/hostap/CMakeLists.txt
index e7bc4f2d..7291f3d9 100644
--- a/external/hostap/CMakeLists.txt
+++ b/external/hostap/CMakeLists.txt
@@ -4,7 +4,10 @@ find_program(MAKE_EXE
     REQUIRED
 )
 
-set(HOSTAP_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/hostap-prefix/src/hostap/wpa_supplicant)
+set(HOSTAP_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/hostap-prefix/src/hostap/wpa_supplicant CACHE INTERNAL "Path of external project code")
+set(HOSTAP_LOCAL_INCLUDE_DIR ${HOSTAP_PREFIX}/usr/local/include CACHE INTERNAL "Path of external project includes")
+set(HOSTAP_LOCAL_LIB_DIR ${HOSTAP_PREFIX}/usr/local/lib CACHE INTERNAL "Path of external project built libraries")
+set(HOSTAP_LOCAL_SBIN_DIR ${HOSTAP_PREFIX}/usr/local/sbin CACHE INTERNAL "Path of external project built system binaries")
 
 ExternalProject_Add(hostap
     GIT_REPOSITORY "http://w1.fi/hostap.git"

From 1f1f6db7a3f6170bbdbe6fbc70cca569c1e9758e Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 12:32:40 -0700
Subject: [PATCH 5/7] Add internally built hostap include dir to include path.

---
 linux/hostap-controller/CMakeLists.txt | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/linux/hostap-controller/CMakeLists.txt b/linux/hostap-controller/CMakeLists.txt
index 866ae640..9d42406b 100644
--- a/linux/hostap-controller/CMakeLists.txt
+++ b/linux/hostap-controller/CMakeLists.txt
@@ -3,6 +3,11 @@ add_library(hostap-controller SHARED "")
 
 if (BUILD_HOSTAP_EXTERNAL)
     add_dependencies(hostap-controller hostap)
+
+    target_include_directories(hostap-controller
+        PRIVATE
+            ${HOSTAP_LOCAL_INCLUDE_DIR}
+    )
 endif()
 
 target_sources(hostap-controller
@@ -10,6 +15,7 @@ target_sources(hostap-controller
         Placeholder.cxx    
 )
 
+
 target_link_libraries(hostap-controller
     INTERFACE
         ${LIBWPA_CLIENT_TARGET}

From 7e47e212b6686569e482de5c9aee9c3b5183cb32 Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 12:47:11 -0700
Subject: [PATCH 6/7] Tighten install commands.

---
 external/hostap/CMakeLists.txt | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/external/hostap/CMakeLists.txt b/external/hostap/CMakeLists.txt
index 7291f3d9..548b7d10 100644
--- a/external/hostap/CMakeLists.txt
+++ b/external/hostap/CMakeLists.txt
@@ -4,10 +4,12 @@ find_program(MAKE_EXE
     REQUIRED
 )
 
+set(HOSTAP_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR})
+
 set(HOSTAP_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/hostap-prefix/src/hostap/wpa_supplicant CACHE INTERNAL "Path of external project code")
-set(HOSTAP_LOCAL_INCLUDE_DIR ${HOSTAP_PREFIX}/usr/local/include CACHE INTERNAL "Path of external project includes")
-set(HOSTAP_LOCAL_LIB_DIR ${HOSTAP_PREFIX}/usr/local/lib CACHE INTERNAL "Path of external project built libraries")
-set(HOSTAP_LOCAL_SBIN_DIR ${HOSTAP_PREFIX}/usr/local/sbin CACHE INTERNAL "Path of external project built system binaries")
+set(HOSTAP_LOCAL_INCLUDE_DIR ${HOSTAP_INSTALL_DIR}/usr/local/include CACHE INTERNAL "Path of external project includes")
+set(HOSTAP_LOCAL_LIB_DIR ${HOSTAP_INSTALL_DIR}/usr/local/lib CACHE INTERNAL "Path of external project built libraries")
+set(HOSTAP_LOCAL_SBIN_DIR ${HOSTAP_INSTALL_DIR}/usr/local/sbin CACHE INTERNAL "Path of external project built system binaries")
 
 ExternalProject_Add(hostap
     GIT_REPOSITORY "http://w1.fi/hostap.git"
@@ -15,12 +17,12 @@ ExternalProject_Add(hostap
     CONFIGURE_COMMAND cp -n ${CMAKE_CURRENT_SOURCE_DIR}/.config ${HOSTAP_PREFIX}/.config
     BINARY_DIR ${HOSTAP_PREFIX}
     BUILD_COMMAND QUIET=1 ${MAKE_EXE} libwpa_client.so
-    INSTALL_DIR ${HOSTAP_PREFIX}
-    INSTALL_COMMAND QUIET=1 DESTDIR=${HOSTAP_PREFIX} ${MAKE_EXE} install
+    INSTALL_DIR ${HOSTAP_INSTALL_DIR}
+    INSTALL_COMMAND QUIET=1 DESTDIR=${HOSTAP_INSTALL_DIR} ${MAKE_EXE} install
 )
 
 set(LIBWPA_CLIENT
-    ${HOSTAP_PREFIX}/libwpa_client.so
+    ${HOSTAP_LOCAL_LIB_DIR}/libwpa_client.so
     CACHE FILEPATH
     "wpa client shared object"
 )

From f8a000619fca19af337681bd52420bce1245c807 Mon Sep 17 00:00:00 2001
From: Andrew Beltrano <abeltrano@gmail.com>
Date: Fri, 10 Nov 2023 14:25:03 -0700
Subject: [PATCH 7/7] Include host cmake script only on Linux.

---
 CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8b3db929..645a4180 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,7 +50,6 @@ include(ExternalProject)
 include(GNUInstallDirs)
 include(grpc)
 include(protoc)
-include(hostap)
 
 # Enable verbose output until project is bootstrapped.
 set(CMAKE_VERBOSE_MAKEFILE ON)
@@ -142,6 +141,7 @@ add_compile_definitions(
 
 # Conditional inclusion of OS-dependent source trees.
 if (BUILD_FOR_LINUX)
+  include(hostap)
   add_subdirectory(linux)
 elseif (BUILD_FOR_WINDOWS)
   # Make public version of wil available.