From 9771615f1638c2a548067717c12426539ad60541 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Mon, 13 Mar 2017 16:53:20 -0500
Subject: [PATCH 1/7] Services/UDS: Initial support for hosting local-wlan
 networks.

Currently it will let games create a network as hosts, but will not broadcast it anywhere and will not allow clients to connect.
---
 src/core/hle/service/nwm/nwm_uds.cpp | 315 +++++++++++++++++++++++----
 src/core/hle/service/nwm/nwm_uds.h   |  61 ++++++
 2 files changed, 334 insertions(+), 42 deletions(-)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index 08fade320..789476418 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -1,16 +1,48 @@
-// Copyright 2014 Citra Emulator Project
+// Copyright 2017 Citra Emulator Project
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
+#include <cstring>
+#include <unordered_map>
+#include <vector>
 #include "common/common_types.h"
 #include "common/logging/log.h"
 #include "core/hle/kernel/event.h"
+#include "core/hle/kernel/shared_memory.h"
+#include "core/hle/result.h"
 #include "core/hle/service/nwm/nwm_uds.h"
+#include "core/memory.h"
 
 namespace Service {
 namespace NWM {
 
-static Kernel::SharedPtr<Kernel::Event> uds_handle_event;
+// Event that is signaled every time the connection status changes.
+static Kernel::SharedPtr<Kernel::Event> connection_status_event;
+
+// Shared memory provided by the application to store the receive buffer.
+// This is not currently used.
+static Kernel::SharedPtr<Kernel::SharedMemory> recv_buffer_memory;
+
+// Connection status of this 3DS.
+static ConnectionStatus connection_status{};
+
+// Node information about the current 3DS.
+// TODO(Subv): Keep an array of all nodes connected to the network,
+// that data has to be retransmitted in every beacon frame.
+static NodeInfo node_info;
+
+// Mapping of bind node ids to their respective events.
+static std::unordered_map<u32, Kernel::SharedPtr<Kernel::Event>> bind_node_events;
+
+// The wifi network channel that the network is currently on.
+// Since we're not actually interacting with physical radio waves, this is just a dummy value.
+static u8 network_channel = DefaultNetworkChannel;
+
+// The identifier of the network kind, this is used to filter away networks that we're not interested in.
+static u32 wlan_comm_id = 0;
+
+// Application data that is sent when broadcasting the beacon frames.
+static std::vector<u8> application_data;
 
 /**
  * NWM_UDS::Shutdown service function
@@ -32,14 +64,14 @@ static void Shutdown(Interface* self) {
 
 /**
  * NWM_UDS::RecvBeaconBroadcastData service function
+ * Returns the raw beacon data for nearby networks that match the supplied WlanCommId.
  *  Inputs:
  *      1 : Output buffer max size
- *      2 : Unknown
- *      3 : Unknown
- *      4 : MAC address?
- *   6-14 : Unknown, usually zero / uninitialized?
- *     15 : WLan Comm ID
- *     16 : This is the ID also located at offset 0xE in the CTR-generation structure.
+ *    2-3 : Unknown
+ *    4-5 : Host MAC address.
+ *   6-14 : Unused
+ *     15 : WLan Comm Id
+ *     16 : Id
  *     17 : Value 0
  *     18 : Input handle
  *     19 : (Size<<4) | 12
@@ -77,42 +109,234 @@ static void RecvBeaconBroadcastData(Interface* self) {
 /**
  * NWM_UDS::Initialize service function
  *  Inputs:
- *      1 : Unknown
- *   2-11 : Input Structure
- *     12 : Unknown u16
+ *      1 : Shared memory size
+ *   2-11 : Input NodeInfo Structure
+ *     12 : 2-byte Version
  *     13 : Value 0
- *     14 : Handle
+ *     14 : Shared memory handle
  *  Outputs:
  *      0 : Return header
  *      1 : Result of function, 0 on success, otherwise error code
  *      2 : Value 0
- *      3 : Output handle
+ *      3 : Output event handle
  */
 static void InitializeWithVersion(Interface* self) {
-    u32* cmd_buff = Kernel::GetCommandBuffer();
-    u32 unk1 = cmd_buff[1];
-    u32 unk2 = cmd_buff[12];
-    u32 value = cmd_buff[13];
-    u32 handle = cmd_buff[14];
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1B, 12, 2);
 
-    // Because NWM service is not implemented at all, we stub the Initialize function with an error
-    // code instead of success to prevent games from using the service and from causing more issues.
-    // The error code is from a real 3DS with wifi off, thus believed to be "network disabled".
-    /*
-    cmd_buff[1] = RESULT_SUCCESS.raw;
-    cmd_buff[2] = 0;
-    cmd_buff[3] = Kernel::g_handle_table.Create(uds_handle_event)
-                      .MoveFrom(); // TODO(purpasmart): Verify if this is a event handle
-    */
-    cmd_buff[0] = IPC::MakeHeader(0x1B, 1, 2);
-    cmd_buff[1] = ResultCode(static_cast<ErrorDescription>(2), ErrorModule::UDS,
-                             ErrorSummary::StatusChanged, ErrorLevel::Status)
-                      .raw;
-    cmd_buff[2] = 0;
-    cmd_buff[3] = 0;
+    u32 sharedmem_size = rp.Pop<u32>();
 
-    LOG_WARNING(Service_NWM, "(STUBBED) called unk1=0x%08X, unk2=0x%08X, value=%u, handle=0x%08X",
-                unk1, unk2, value, handle);
+    // Update the node information with the data the game gave us.
+    rp.PopRaw(node_info);
+
+    u16 version;
+    rp.PopRaw(version);
+    Kernel::Handle sharedmem_handle = rp.PopHandle();
+
+    recv_buffer_memory = Kernel::g_handle_table.Get<Kernel::SharedMemory>(sharedmem_handle);
+
+    ASSERT_MSG(recv_buffer_memory->size == sharedmem_size, "Invalid shared memory size.");
+
+    // Reset the connection status, it contains all zeros after initialization,
+    // except for the actual status value.
+    connection_status = {};
+    connection_status.status = static_cast<u32>(NetworkStatus::NotConnected);
+
+    IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
+    rb.Push(RESULT_SUCCESS);
+    rb.PushCopyHandles(Kernel::g_handle_table.Create(connection_status_event).MoveFrom());
+
+    LOG_DEBUG(Service_NWM, "called sharedmem_size=0x%08X, version=0x%08X, sharedmem_handle=0x%08X",
+              sharedmem_size, version, sharedmem_handle);
+}
+
+/**
+ * NWM_UDS::GetConnectionStatus service function.
+ * Returns the connection status structure for the currently open network connection.
+ * This structure contains information about the connection,
+ * like the number of connected nodes, etc.
+ *  Inputs:
+ *      0 : Command header.
+ *  Outputs:
+ *      0 : Return header
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2-13 : Channel of the current WiFi network connection.
+ */
+static void GetConnectionStatus(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0xB, 0, 0);
+    IPC::RequestBuilder rb = rp.MakeBuilder(13, 0);
+
+    rb.Push(RESULT_SUCCESS);
+    rb.PushRaw(connection_status);
+
+    LOG_DEBUG(Service_NWM, "called");
+}
+
+/**
+ * NWM_UDS::Bind service function.
+ * Binds a BindNodeId to a data channel and retrieves a data event.
+ *  Inputs:
+ *      1 : BindNodeId
+ *      2 : Receive buffer size.
+ *      3 : u8 Data channel to bind to.
+ *      4 : Network node id.
+ *  Outputs:
+ *      0 : Return header
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : Copy handle descriptor.
+ *      3 : Data available event handle.
+ */
+static void Bind(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x12, 4, 0);
+
+    u32 bind_node_id = rp.Pop<u32>();
+    u32 recv_buffer_size = rp.Pop<u32>();
+    u8 data_channel;
+    rp.PopRaw(data_channel);
+    u16 network_node_id;
+    rp.PopRaw(network_node_id);
+
+    // TODO(Subv): Store the data channel and verify it when receiving data frames.
+
+    LOG_DEBUG(Service_NWM, "called");
+
+    if (data_channel == 0) {
+        IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
+        rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::UDS,
+                           ErrorSummary::WrongArgument, ErrorLevel::Usage));
+        return;
+    }
+
+    // Create a new event for this bind node.
+    // TODO(Subv): Signal this event when new data is received on this data channel.
+    auto event = Kernel::Event::Create(Kernel::ResetType::OneShot,
+                                       "NWM::BindNodeEvent" + std::to_string(bind_node_id));
+    bind_node_events[bind_node_id] = event;
+
+    IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
+
+    rb.Push(RESULT_SUCCESS);
+    rb.PushCopyHandles(Kernel::g_handle_table.Create(event).MoveFrom());
+}
+
+/**
+ * NWM_UDS::BeginHostingNetwork service function.
+ * Creates a network and starts broadcasting its presence.
+ *  Inputs:
+ *      1 : Passphrase buffer size.
+ *      3 : VAddr of the NetworkInfo structure.
+ *      5 : VAddr of the passphrase.
+ *  Outputs:
+ *      0 : Return header
+ *      1 : Result of function, 0 on success, otherwise error code
+ */
+static void BeginHostingNetwork(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1D, 1, 4);
+
+    const u32 passphrase_size = rp.Pop<u32>();
+
+    size_t desc_size;
+    const VAddr network_info_address = rp.PopStaticBuffer(&desc_size, false);
+    ASSERT(desc_size == sizeof(NetworkInfo));
+    const VAddr passphrase_address = rp.PopStaticBuffer(&desc_size, false);
+    ASSERT(desc_size == passphrase_size);
+
+    // TODO(Subv): Store the passphrase and verify it when attempting a connection.
+
+    LOG_DEBUG(Service_NWM, "called");
+
+    NetworkInfo network_info;
+    Memory::ReadBlock(network_info_address, &network_info, sizeof(NetworkInfo));
+
+    connection_status.status = static_cast<u32>(NetworkStatus::ConnectedAsHost);
+    connection_status.max_nodes = network_info.max_nodes;
+    wlan_comm_id = network_info.wlan_comm_id;
+
+    // There's currently only one node in the network (the host).
+    connection_status.total_nodes = 1;
+    // The host is always the first node
+    connection_status.network_node_id = 1;
+    node_info.network_node_id = 1;
+    // Set the bit 0 in the nodes bitmask to indicate that node 1 is already taken.
+    connection_status.node_bitmask |= 1;
+
+    // If the game has a preferred channel, use that instead.
+    if (network_info.channel != 0)
+        network_channel = network_info.channel;
+
+    // Clear the pre-existing application data.
+    application_data.clear();
+
+    connection_status_event->Signal();
+
+    // TODO(Subv): Start broadcasting the network, send a beacon frame every 102.4ms.
+
+    LOG_WARNING(Service_NWM,
+                "An UDS network has been created, but broadcasting it is unimplemented.");
+
+    IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
+    rb.Push(RESULT_SUCCESS);
+}
+
+/**
+ * NWM_UDS::GetChannel service function.
+ * Returns the WiFi channel in which the network we're connected to is transmitting.
+ *  Inputs:
+ *      0 : Command header.
+ *  Outputs:
+ *      0 : Return header
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : Channel of the current WiFi network connection.
+ */
+static void GetChannel(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 0, 0);
+    IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
+
+    u8 channel = network_channel;
+
+    if (connection_status.status == static_cast<u32>(NetworkStatus::NotConnected))
+        channel = 0;
+
+    rb.Push(RESULT_SUCCESS);
+    rb.PushRaw(channel);
+
+    LOG_DEBUG(Service_NWM, "called");
+}
+
+/**
+ * NWM_UDS::SetApplicationData service function.
+ * Updates the application data that is being broadcast in the beacon frames
+ * for the network that we're hosting.
+ *  Inputs:
+ *      1 : Data size.
+ *      3 : VAddr of the data.
+ *  Outputs:
+ *      0 : Return header
+ *      1 : Result of function, 0 on success, otherwise error code
+ *      2 : Channel of the current WiFi network connection.
+ */
+static void SetApplicationData(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 1, 2);
+
+    u32 size = rp.Pop<u32>();
+
+    size_t desc_size;
+    const VAddr address = rp.PopStaticBuffer(&desc_size, false);
+    ASSERT(desc_size == size);
+
+    LOG_DEBUG(Service_NWM, "called");
+
+    IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
+
+    if (size > ApplicationDataSize) {
+        rb.Push(ResultCode(ErrorDescription::TooLarge, ErrorModule::UDS,
+                           ErrorSummary::WrongArgument, ErrorLevel::Usage));
+        return;
+    }
+
+    application_data.resize(size);
+    Memory::ReadBlock(address, application_data.data(), size);
+
+    rb.Push(RESULT_SUCCESS);
 }
 
 const Interface::FunctionInfo FunctionTable[] = {
@@ -126,20 +350,20 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x00080000, nullptr, "DestroyNetwork"},
     {0x00090442, nullptr, "ConnectNetwork (deprecated)"},
     {0x000A0000, nullptr, "DisconnectNetwork"},
-    {0x000B0000, nullptr, "GetConnectionStatus"},
+    {0x000B0000, GetConnectionStatus, "GetConnectionStatus"},
     {0x000D0040, nullptr, "GetNodeInformation"},
     {0x000E0006, nullptr, "DecryptBeaconData (deprecated)"},
     {0x000F0404, RecvBeaconBroadcastData, "RecvBeaconBroadcastData"},
-    {0x00100042, nullptr, "SetApplicationData"},
+    {0x00100042, SetApplicationData, "SetApplicationData"},
     {0x00110040, nullptr, "GetApplicationData"},
-    {0x00120100, nullptr, "Bind"},
+    {0x00120100, Bind, "Bind"},
     {0x00130040, nullptr, "Unbind"},
     {0x001400C0, nullptr, "PullPacket"},
     {0x00150080, nullptr, "SetMaxSendDelay"},
     {0x00170182, nullptr, "SendTo"},
-    {0x001A0000, nullptr, "GetChannel"},
+    {0x001A0000, GetChannel, "GetChannel"},
     {0x001B0302, InitializeWithVersion, "InitializeWithVersion"},
-    {0x001D0044, nullptr, "BeginHostingNetwork"},
+    {0x001D0044, BeginHostingNetwork, "BeginHostingNetwork"},
     {0x001E0084, nullptr, "ConnectToNetwork"},
     {0x001F0006, nullptr, "DecryptBeaconData"},
     {0x00200040, nullptr, "Flush"},
@@ -148,13 +372,20 @@ const Interface::FunctionInfo FunctionTable[] = {
 };
 
 NWM_UDS::NWM_UDS() {
-    uds_handle_event = Kernel::Event::Create(Kernel::ResetType::OneShot, "NWM::uds_handle_event");
+    connection_status_event =
+        Kernel::Event::Create(Kernel::ResetType::OneShot, "NWM::connection_status_event");
 
     Register(FunctionTable);
 }
 
 NWM_UDS::~NWM_UDS() {
-    uds_handle_event = nullptr;
+    application_data.clear();
+    bind_node_events.clear();
+    connection_status_event = nullptr;
+    recv_buffer_memory = nullptr;
+
+    connection_status = {};
+    connection_status.status = static_cast<u32>(NetworkStatus::NotConnected);
 }
 
 } // namespace NWM
diff --git a/src/core/hle/service/nwm/nwm_uds.h b/src/core/hle/service/nwm/nwm_uds.h
index 55db748f6..3dfc32de8 100644
--- a/src/core/hle/service/nwm/nwm_uds.h
+++ b/src/core/hle/service/nwm/nwm_uds.h
@@ -4,6 +4,10 @@
 
 #pragma once
 
+#include <array>
+#include <cstddef>
+#include "common/common_types.h"
+#include "common/swap.h"
 #include "core/hle/service/service.h"
 
 // Local-WLAN service
@@ -11,6 +15,63 @@
 namespace Service {
 namespace NWM {
 
+const size_t ApplicationDataSize = 0xC8;
+const u8 DefaultNetworkChannel = 11;
+
+struct NodeInfo {
+    u64_le friend_code_seed;
+    std::array<u16_le, 10> username;
+    INSERT_PADDING_BYTES(4);
+    u16_le network_node_id;
+    INSERT_PADDING_BYTES(6);
+};
+
+static_assert(sizeof(NodeInfo) == 40, "NodeInfo has incorrect size.");
+
+enum class NetworkStatus {
+    NotConnected = 3,
+    ConnectedAsHost = 6,
+    ConnectedAsClient = 9,
+    ConnectedAsSpectator = 10,
+};
+
+struct ConnectionStatus {
+    u32_le status;
+    INSERT_PADDING_WORDS(1);
+    u16_le network_node_id;
+    INSERT_PADDING_BYTES(2);
+    INSERT_PADDING_BYTES(32);
+    u8 total_nodes;
+    u8 max_nodes;
+    u16_le node_bitmask;
+};
+
+static_assert(sizeof(ConnectionStatus) == 0x30, "ConnectionStatus has incorrect size.");
+
+struct NetworkInfo {
+    std::array<u8, 6> host_mac_address;
+    u8 channel;
+    INSERT_PADDING_BYTES(1);
+    u8 initialized;
+    INSERT_PADDING_BYTES(3);
+    std::array<u8, 3> oui_value;
+    u8 oui_type;
+    // This field is received as BigEndian from the game.
+    u32_be wlan_comm_id;
+    u8 id;
+    INSERT_PADDING_BYTES(1);
+    u16_be attributes;
+    u32_be network_id;
+    u8 total_nodes;
+    u8 max_nodes;
+    INSERT_PADDING_BYTES(2);
+    INSERT_PADDING_BYTES(0x1F);
+    u8 application_data_size;
+    std::array<u8, ApplicationDataSize> application_data;
+};
+
+static_assert(sizeof(NetworkInfo) == 0x108, "NetworkInfo has incorrect size.");
+
 class NWM_UDS final : public Interface {
 public:
     NWM_UDS();

From 4243c1198ff960bcf5cfaaf38cc5cc1689a2bafd Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Mon, 13 Mar 2017 22:21:01 -0500
Subject: [PATCH 2/7] Services/UDS: Store the entire NetworkInfo structure that
 was used to create the network.

It will be needed when generating the beacon frames.
---
 src/core/hle/service/nwm/nwm_uds.cpp | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index 789476418..b69c90ef1 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -38,11 +38,8 @@ static std::unordered_map<u32, Kernel::SharedPtr<Kernel::Event>> bind_node_event
 // Since we're not actually interacting with physical radio waves, this is just a dummy value.
 static u8 network_channel = DefaultNetworkChannel;
 
-// The identifier of the network kind, this is used to filter away networks that we're not interested in.
-static u32 wlan_comm_id = 0;
-
-// Application data that is sent when broadcasting the beacon frames.
-static std::vector<u8> application_data;
+// Information about the network that we're currently connected to.
+static NetworkInfo network_info;
 
 /**
  * NWM_UDS::Shutdown service function
@@ -244,12 +241,10 @@ static void BeginHostingNetwork(Interface* self) {
 
     LOG_DEBUG(Service_NWM, "called");
 
-    NetworkInfo network_info;
     Memory::ReadBlock(network_info_address, &network_info, sizeof(NetworkInfo));
 
     connection_status.status = static_cast<u32>(NetworkStatus::ConnectedAsHost);
     connection_status.max_nodes = network_info.max_nodes;
-    wlan_comm_id = network_info.wlan_comm_id;
 
     // There's currently only one node in the network (the host).
     connection_status.total_nodes = 1;
@@ -263,9 +258,6 @@ static void BeginHostingNetwork(Interface* self) {
     if (network_info.channel != 0)
         network_channel = network_info.channel;
 
-    // Clear the pre-existing application data.
-    application_data.clear();
-
     connection_status_event->Signal();
 
     // TODO(Subv): Start broadcasting the network, send a beacon frame every 102.4ms.
@@ -333,8 +325,8 @@ static void SetApplicationData(Interface* self) {
         return;
     }
 
-    application_data.resize(size);
-    Memory::ReadBlock(address, application_data.data(), size);
+    network_info.application_data_size = size;
+    Memory::ReadBlock(address, network_info.application_data.data(), size);
 
     rb.Push(RESULT_SUCCESS);
 }
@@ -379,7 +371,7 @@ NWM_UDS::NWM_UDS() {
 }
 
 NWM_UDS::~NWM_UDS() {
-    application_data.clear();
+    network_info = {};
     bind_node_events.clear();
     connection_status_event = nullptr;
     recv_buffer_memory = nullptr;

From 97f1e62b668fae7d34f23e8f10c86187a09756d0 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sat, 18 Mar 2017 13:15:07 -0500
Subject: [PATCH 3/7] Service/UDS: Schedule an event to broadcast the beacon
 frames every 102.4ms.

---
 src/core/hle/service/nwm/nwm_uds.cpp | 55 +++++++++++++++++++++++++++-
 src/core/hle/service/nwm/nwm_uds.h   |  5 +++
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index b69c90ef1..da7c2bb0d 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -7,6 +7,7 @@
 #include <vector>
 #include "common/common_types.h"
 #include "common/logging/log.h"
+#include "core/core_timing.h"
 #include "core/hle/kernel/event.h"
 #include "core/hle/kernel/shared_memory.h"
 #include "core/hle/result.h"
@@ -41,6 +42,9 @@ static u8 network_channel = DefaultNetworkChannel;
 // Information about the network that we're currently connected to.
 static NetworkInfo network_info;
 
+// Event that will generate and send the 802.11 beacon frames.
+static int beacon_broadcast_event;
+
 /**
  * NWM_UDS::Shutdown service function
  *  Inputs:
@@ -260,7 +264,9 @@ static void BeginHostingNetwork(Interface* self) {
 
     connection_status_event->Signal();
 
-    // TODO(Subv): Start broadcasting the network, send a beacon frame every 102.4ms.
+    // Start broadcasting the network, send a beacon frame every 102.4ms.
+    CoreTiming::ScheduleEvent(msToCycles(DefaultBeaconInterval * MillisecondsPerTU),
+                              beacon_broadcast_event, 0);
 
     LOG_WARNING(Service_NWM,
                 "An UDS network has been created, but broadcasting it is unimplemented.");
@@ -269,6 +275,33 @@ static void BeginHostingNetwork(Interface* self) {
     rb.Push(RESULT_SUCCESS);
 }
 
+/**
+ * NWM_UDS::DestroyNetwork service function.
+ * Closes the network that we're currently hosting.
+ *  Inputs:
+ *      0 : Command header.
+ *  Outputs:
+ *      0 : Return header
+ *      1 : Result of function, 0 on success, otherwise error code
+ */
+static void DestroyNetwork(Interface* self) {
+    IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x08, 0, 0);
+
+    // TODO(Subv): Find out what happens if this is called while
+    // no network is being hosted.
+
+    // Unschedule the beacon broadcast event.
+    CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0);
+
+    connection_status.status = static_cast<u8>(NetworkStatus::NotConnected);
+
+    IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
+
+    rb.Push(RESULT_SUCCESS);
+
+    LOG_WARNING(Service_NWM, "called");
+}
+
 /**
  * NWM_UDS::GetChannel service function.
  * Returns the WiFi channel in which the network we're connected to is transmitting.
@@ -331,6 +364,19 @@ static void SetApplicationData(Interface* self) {
     rb.Push(RESULT_SUCCESS);
 }
 
+// Sends a 802.11 beacon frame with information about the current network.
+static void BeaconBroadcastCallback(u64 userdata, int cycles_late) {
+    // Don't do anything if we're not actually hosting a network
+    if (connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsHost))
+        return;
+
+    // TODO(Subv): Actually generate the beacon and send it.
+
+    // Start broadcasting the network, send a beacon frame every 102.4ms.
+    CoreTiming::ScheduleEvent(msToCycles(DefaultBeaconInterval * MillisecondsPerTU) - cycles_late,
+                              beacon_broadcast_event, 0);
+}
+
 const Interface::FunctionInfo FunctionTable[] = {
     {0x00010442, nullptr, "Initialize (deprecated)"},
     {0x00020000, nullptr, "Scrap"},
@@ -339,7 +385,7 @@ const Interface::FunctionInfo FunctionTable[] = {
     {0x00050040, nullptr, "EjectClient"},
     {0x00060000, nullptr, "EjectSpectator"},
     {0x00070080, nullptr, "UpdateNetworkAttribute"},
-    {0x00080000, nullptr, "DestroyNetwork"},
+    {0x00080000, DestroyNetwork, "DestroyNetwork"},
     {0x00090442, nullptr, "ConnectNetwork (deprecated)"},
     {0x000A0000, nullptr, "DisconnectNetwork"},
     {0x000B0000, GetConnectionStatus, "GetConnectionStatus"},
@@ -368,6 +414,9 @@ NWM_UDS::NWM_UDS() {
         Kernel::Event::Create(Kernel::ResetType::OneShot, "NWM::connection_status_event");
 
     Register(FunctionTable);
+
+    beacon_broadcast_event =
+        CoreTiming::RegisterEvent("UDS::BeaconBroadcastCallback", BeaconBroadcastCallback);
 }
 
 NWM_UDS::~NWM_UDS() {
@@ -378,6 +427,8 @@ NWM_UDS::~NWM_UDS() {
 
     connection_status = {};
     connection_status.status = static_cast<u32>(NetworkStatus::NotConnected);
+
+    CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0);
 }
 
 } // namespace NWM
diff --git a/src/core/hle/service/nwm/nwm_uds.h b/src/core/hle/service/nwm/nwm_uds.h
index 3dfc32de8..65349f9fd 100644
--- a/src/core/hle/service/nwm/nwm_uds.h
+++ b/src/core/hle/service/nwm/nwm_uds.h
@@ -18,6 +18,11 @@ namespace NWM {
 const size_t ApplicationDataSize = 0xC8;
 const u8 DefaultNetworkChannel = 11;
 
+// Number of milliseconds in a TU.
+const double MillisecondsPerTU = 1.024;
+// Interval measured in TU, the default value is 100TU = 102.4ms
+const u16 DefaultBeaconInterval = 100;
+
 struct NodeInfo {
     u64_le friend_code_seed;
     std::array<u16_le, 10> username;

From d7d5bf411a65c69e7bf9aa488567802fb2bd5ef2 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sat, 25 Mar 2017 14:21:01 -0500
Subject: [PATCH 4/7] Services/UDS: Do not allow trying to start up a network
 that only the host can connect to.

---
 src/core/hle/service/nwm/nwm_uds.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index da7c2bb0d..1d5b298a2 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -247,6 +247,9 @@ static void BeginHostingNetwork(Interface* self) {
 
     Memory::ReadBlock(network_info_address, &network_info, sizeof(NetworkInfo));
 
+    // The real UDS module throws a fatal error if this assert fails.
+    ASSERT_MSG(network_info.max_nodes > 1, "Trying to host a network of only one member.");
+
     connection_status.status = static_cast<u32>(NetworkStatus::ConnectedAsHost);
     connection_status.max_nodes = network_info.max_nodes;
 

From 0ae6d8be5b04739fb7ef16320adf7b96ba122ebd Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sat, 25 Mar 2017 14:21:31 -0500
Subject: [PATCH 5/7] Services/UDS: Signal the connection event when closing
 down the network.

---
 src/core/hle/service/nwm/nwm_uds.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index 1d5b298a2..e1bf49393 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -297,6 +297,7 @@ static void DestroyNetwork(Interface* self) {
     CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0);
 
     connection_status.status = static_cast<u8>(NetworkStatus::NotConnected);
+    connection_status_event->Signal();
 
     IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
 

From f8f3b27eb397e348a80137aee075c19ac37e1f48 Mon Sep 17 00:00:00 2001
From: Subv <subv2112@gmail.com>
Date: Sun, 26 Mar 2017 07:02:45 -0500
Subject: [PATCH 6/7] Services/UDS: Use consistent spelling for WiFi and
 simplify the GetChannel function.

---
 src/core/hle/service/nwm/nwm_uds.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index e1bf49393..f56925fee 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -35,7 +35,7 @@ static NodeInfo node_info;
 // Mapping of bind node ids to their respective events.
 static std::unordered_map<u32, Kernel::SharedPtr<Kernel::Event>> bind_node_events;
 
-// The wifi network channel that the network is currently on.
+// The WiFi network channel that the network is currently on.
 // Since we're not actually interacting with physical radio waves, this is just a dummy value.
 static u8 network_channel = DefaultNetworkChannel;
 
@@ -320,10 +320,10 @@ static void GetChannel(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 0, 0);
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
 
-    u8 channel = network_channel;
+    bool is_connected = connection_status.status !=
+        static_cast<u32>(NetworkStatus::NotConnected);
 
-    if (connection_status.status == static_cast<u32>(NetworkStatus::NotConnected))
-        channel = 0;
+    u8 channel = is_connected ? network_channel : 0;
 
     rb.Push(RESULT_SUCCESS);
     rb.PushRaw(channel);

From 5c4bd3ef33a3e660119686a9b7160c08454666a0 Mon Sep 17 00:00:00 2001
From: Sebastian Valle <subv2112@gmail.com>
Date: Mon, 27 Mar 2017 13:08:26 -0500
Subject: [PATCH 7/7] Services/UDS: Fixed a style mistake in GetChannel.

---
 src/core/hle/service/nwm/nwm_uds.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp
index f56925fee..ef6c5ebe3 100644
--- a/src/core/hle/service/nwm/nwm_uds.cpp
+++ b/src/core/hle/service/nwm/nwm_uds.cpp
@@ -320,8 +320,7 @@ static void GetChannel(Interface* self) {
     IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 0, 0);
     IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
 
-    bool is_connected = connection_status.status !=
-        static_cast<u32>(NetworkStatus::NotConnected);
+    bool is_connected = connection_status.status != static_cast<u32>(NetworkStatus::NotConnected);
 
     u8 channel = is_connected ? network_channel : 0;