nwm_uds.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <unordered_map>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "common/logging/log.h"
  9. #include "core/core_timing.h"
  10. #include "core/hle/kernel/event.h"
  11. #include "core/hle/kernel/shared_memory.h"
  12. #include "core/hle/result.h"
  13. #include "core/hle/service/nwm/nwm_uds.h"
  14. #include "core/memory.h"
  15. namespace Service {
  16. namespace NWM {
  17. // Event that is signaled every time the connection status changes.
  18. static Kernel::SharedPtr<Kernel::Event> connection_status_event;
  19. // Shared memory provided by the application to store the receive buffer.
  20. // This is not currently used.
  21. static Kernel::SharedPtr<Kernel::SharedMemory> recv_buffer_memory;
  22. // Connection status of this 3DS.
  23. static ConnectionStatus connection_status{};
  24. // Node information about the current 3DS.
  25. // TODO(Subv): Keep an array of all nodes connected to the network,
  26. // that data has to be retransmitted in every beacon frame.
  27. static NodeInfo node_info;
  28. // Mapping of bind node ids to their respective events.
  29. static std::unordered_map<u32, Kernel::SharedPtr<Kernel::Event>> bind_node_events;
  30. // The wifi network channel that the network is currently on.
  31. // Since we're not actually interacting with physical radio waves, this is just a dummy value.
  32. static u8 network_channel = DefaultNetworkChannel;
  33. // Information about the network that we're currently connected to.
  34. static NetworkInfo network_info;
  35. // Event that will generate and send the 802.11 beacon frames.
  36. static int beacon_broadcast_event;
  37. /**
  38. * NWM_UDS::Shutdown service function
  39. * Inputs:
  40. * 1 : None
  41. * Outputs:
  42. * 0 : Return header
  43. * 1 : Result of function, 0 on success, otherwise error code
  44. */
  45. static void Shutdown(Interface* self) {
  46. u32* cmd_buff = Kernel::GetCommandBuffer();
  47. // TODO(purpasmart): Verify return header on HW
  48. cmd_buff[1] = RESULT_SUCCESS.raw;
  49. LOG_WARNING(Service_NWM, "(STUBBED) called");
  50. }
  51. /**
  52. * NWM_UDS::RecvBeaconBroadcastData service function
  53. * Returns the raw beacon data for nearby networks that match the supplied WlanCommId.
  54. * Inputs:
  55. * 1 : Output buffer max size
  56. * 2-3 : Unknown
  57. * 4-5 : Host MAC address.
  58. * 6-14 : Unused
  59. * 15 : WLan Comm Id
  60. * 16 : Id
  61. * 17 : Value 0
  62. * 18 : Input handle
  63. * 19 : (Size<<4) | 12
  64. * 20 : Output buffer ptr
  65. * Outputs:
  66. * 0 : Return header
  67. * 1 : Result of function, 0 on success, otherwise error code
  68. */
  69. static void RecvBeaconBroadcastData(Interface* self) {
  70. u32* cmd_buff = Kernel::GetCommandBuffer();
  71. u32 out_buffer_size = cmd_buff[1];
  72. u32 unk1 = cmd_buff[2];
  73. u32 unk2 = cmd_buff[3];
  74. u32 mac_address = cmd_buff[4];
  75. u32 unk3 = cmd_buff[6];
  76. u32 wlan_comm_id = cmd_buff[15];
  77. u32 ctr_gen_id = cmd_buff[16];
  78. u32 value = cmd_buff[17];
  79. u32 input_handle = cmd_buff[18];
  80. u32 new_buffer_size = cmd_buff[19];
  81. u32 out_buffer_ptr = cmd_buff[20];
  82. cmd_buff[1] = RESULT_SUCCESS.raw;
  83. LOG_WARNING(Service_NWM,
  84. "(STUBBED) called out_buffer_size=0x%08X, unk1=0x%08X, unk2=0x%08X,"
  85. "mac_address=0x%08X, unk3=0x%08X, wlan_comm_id=0x%08X, ctr_gen_id=0x%08X,"
  86. "value=%u, input_handle=0x%08X, new_buffer_size=0x%08X, out_buffer_ptr=0x%08X",
  87. out_buffer_size, unk1, unk2, mac_address, unk3, wlan_comm_id, ctr_gen_id, value,
  88. input_handle, new_buffer_size, out_buffer_ptr);
  89. }
  90. /**
  91. * NWM_UDS::Initialize service function
  92. * Inputs:
  93. * 1 : Shared memory size
  94. * 2-11 : Input NodeInfo Structure
  95. * 12 : 2-byte Version
  96. * 13 : Value 0
  97. * 14 : Shared memory handle
  98. * Outputs:
  99. * 0 : Return header
  100. * 1 : Result of function, 0 on success, otherwise error code
  101. * 2 : Value 0
  102. * 3 : Output event handle
  103. */
  104. static void InitializeWithVersion(Interface* self) {
  105. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1B, 12, 2);
  106. u32 sharedmem_size = rp.Pop<u32>();
  107. // Update the node information with the data the game gave us.
  108. rp.PopRaw(node_info);
  109. u16 version;
  110. rp.PopRaw(version);
  111. Kernel::Handle sharedmem_handle = rp.PopHandle();
  112. recv_buffer_memory = Kernel::g_handle_table.Get<Kernel::SharedMemory>(sharedmem_handle);
  113. ASSERT_MSG(recv_buffer_memory->size == sharedmem_size, "Invalid shared memory size.");
  114. // Reset the connection status, it contains all zeros after initialization,
  115. // except for the actual status value.
  116. connection_status = {};
  117. connection_status.status = static_cast<u32>(NetworkStatus::NotConnected);
  118. IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
  119. rb.Push(RESULT_SUCCESS);
  120. rb.PushCopyHandles(Kernel::g_handle_table.Create(connection_status_event).MoveFrom());
  121. LOG_DEBUG(Service_NWM, "called sharedmem_size=0x%08X, version=0x%08X, sharedmem_handle=0x%08X",
  122. sharedmem_size, version, sharedmem_handle);
  123. }
  124. /**
  125. * NWM_UDS::GetConnectionStatus service function.
  126. * Returns the connection status structure for the currently open network connection.
  127. * This structure contains information about the connection,
  128. * like the number of connected nodes, etc.
  129. * Inputs:
  130. * 0 : Command header.
  131. * Outputs:
  132. * 0 : Return header
  133. * 1 : Result of function, 0 on success, otherwise error code
  134. * 2-13 : Channel of the current WiFi network connection.
  135. */
  136. static void GetConnectionStatus(Interface* self) {
  137. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0xB, 0, 0);
  138. IPC::RequestBuilder rb = rp.MakeBuilder(13, 0);
  139. rb.Push(RESULT_SUCCESS);
  140. rb.PushRaw(connection_status);
  141. LOG_DEBUG(Service_NWM, "called");
  142. }
  143. /**
  144. * NWM_UDS::Bind service function.
  145. * Binds a BindNodeId to a data channel and retrieves a data event.
  146. * Inputs:
  147. * 1 : BindNodeId
  148. * 2 : Receive buffer size.
  149. * 3 : u8 Data channel to bind to.
  150. * 4 : Network node id.
  151. * Outputs:
  152. * 0 : Return header
  153. * 1 : Result of function, 0 on success, otherwise error code
  154. * 2 : Copy handle descriptor.
  155. * 3 : Data available event handle.
  156. */
  157. static void Bind(Interface* self) {
  158. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x12, 4, 0);
  159. u32 bind_node_id = rp.Pop<u32>();
  160. u32 recv_buffer_size = rp.Pop<u32>();
  161. u8 data_channel;
  162. rp.PopRaw(data_channel);
  163. u16 network_node_id;
  164. rp.PopRaw(network_node_id);
  165. // TODO(Subv): Store the data channel and verify it when receiving data frames.
  166. LOG_DEBUG(Service_NWM, "called");
  167. if (data_channel == 0) {
  168. IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
  169. rb.Push(ResultCode(ErrorDescription::NotAuthorized, ErrorModule::UDS,
  170. ErrorSummary::WrongArgument, ErrorLevel::Usage));
  171. return;
  172. }
  173. // Create a new event for this bind node.
  174. // TODO(Subv): Signal this event when new data is received on this data channel.
  175. auto event = Kernel::Event::Create(Kernel::ResetType::OneShot,
  176. "NWM::BindNodeEvent" + std::to_string(bind_node_id));
  177. bind_node_events[bind_node_id] = event;
  178. IPC::RequestBuilder rb = rp.MakeBuilder(1, 2);
  179. rb.Push(RESULT_SUCCESS);
  180. rb.PushCopyHandles(Kernel::g_handle_table.Create(event).MoveFrom());
  181. }
  182. /**
  183. * NWM_UDS::BeginHostingNetwork service function.
  184. * Creates a network and starts broadcasting its presence.
  185. * Inputs:
  186. * 1 : Passphrase buffer size.
  187. * 3 : VAddr of the NetworkInfo structure.
  188. * 5 : VAddr of the passphrase.
  189. * Outputs:
  190. * 0 : Return header
  191. * 1 : Result of function, 0 on success, otherwise error code
  192. */
  193. static void BeginHostingNetwork(Interface* self) {
  194. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1D, 1, 4);
  195. const u32 passphrase_size = rp.Pop<u32>();
  196. size_t desc_size;
  197. const VAddr network_info_address = rp.PopStaticBuffer(&desc_size, false);
  198. ASSERT(desc_size == sizeof(NetworkInfo));
  199. const VAddr passphrase_address = rp.PopStaticBuffer(&desc_size, false);
  200. ASSERT(desc_size == passphrase_size);
  201. // TODO(Subv): Store the passphrase and verify it when attempting a connection.
  202. LOG_DEBUG(Service_NWM, "called");
  203. Memory::ReadBlock(network_info_address, &network_info, sizeof(NetworkInfo));
  204. connection_status.status = static_cast<u32>(NetworkStatus::ConnectedAsHost);
  205. connection_status.max_nodes = network_info.max_nodes;
  206. // There's currently only one node in the network (the host).
  207. connection_status.total_nodes = 1;
  208. // The host is always the first node
  209. connection_status.network_node_id = 1;
  210. node_info.network_node_id = 1;
  211. // Set the bit 0 in the nodes bitmask to indicate that node 1 is already taken.
  212. connection_status.node_bitmask |= 1;
  213. // If the game has a preferred channel, use that instead.
  214. if (network_info.channel != 0)
  215. network_channel = network_info.channel;
  216. connection_status_event->Signal();
  217. // Start broadcasting the network, send a beacon frame every 102.4ms.
  218. CoreTiming::ScheduleEvent(msToCycles(DefaultBeaconInterval * MillisecondsPerTU),
  219. beacon_broadcast_event, 0);
  220. LOG_WARNING(Service_NWM,
  221. "An UDS network has been created, but broadcasting it is unimplemented.");
  222. IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
  223. rb.Push(RESULT_SUCCESS);
  224. }
  225. /**
  226. * NWM_UDS::DestroyNetwork service function.
  227. * Closes the network that we're currently hosting.
  228. * Inputs:
  229. * 0 : Command header.
  230. * Outputs:
  231. * 0 : Return header
  232. * 1 : Result of function, 0 on success, otherwise error code
  233. */
  234. static void DestroyNetwork(Interface* self) {
  235. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x08, 0, 0);
  236. // TODO(Subv): Find out what happens if this is called while
  237. // no network is being hosted.
  238. // Unschedule the beacon broadcast event.
  239. CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0);
  240. connection_status.status = static_cast<u8>(NetworkStatus::NotConnected);
  241. IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
  242. rb.Push(RESULT_SUCCESS);
  243. LOG_WARNING(Service_NWM, "called");
  244. }
  245. /**
  246. * NWM_UDS::GetChannel service function.
  247. * Returns the WiFi channel in which the network we're connected to is transmitting.
  248. * Inputs:
  249. * 0 : Command header.
  250. * Outputs:
  251. * 0 : Return header
  252. * 1 : Result of function, 0 on success, otherwise error code
  253. * 2 : Channel of the current WiFi network connection.
  254. */
  255. static void GetChannel(Interface* self) {
  256. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 0, 0);
  257. IPC::RequestBuilder rb = rp.MakeBuilder(2, 0);
  258. u8 channel = network_channel;
  259. if (connection_status.status == static_cast<u32>(NetworkStatus::NotConnected))
  260. channel = 0;
  261. rb.Push(RESULT_SUCCESS);
  262. rb.PushRaw(channel);
  263. LOG_DEBUG(Service_NWM, "called");
  264. }
  265. /**
  266. * NWM_UDS::SetApplicationData service function.
  267. * Updates the application data that is being broadcast in the beacon frames
  268. * for the network that we're hosting.
  269. * Inputs:
  270. * 1 : Data size.
  271. * 3 : VAddr of the data.
  272. * Outputs:
  273. * 0 : Return header
  274. * 1 : Result of function, 0 on success, otherwise error code
  275. * 2 : Channel of the current WiFi network connection.
  276. */
  277. static void SetApplicationData(Interface* self) {
  278. IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x1A, 1, 2);
  279. u32 size = rp.Pop<u32>();
  280. size_t desc_size;
  281. const VAddr address = rp.PopStaticBuffer(&desc_size, false);
  282. ASSERT(desc_size == size);
  283. LOG_DEBUG(Service_NWM, "called");
  284. IPC::RequestBuilder rb = rp.MakeBuilder(1, 0);
  285. if (size > ApplicationDataSize) {
  286. rb.Push(ResultCode(ErrorDescription::TooLarge, ErrorModule::UDS,
  287. ErrorSummary::WrongArgument, ErrorLevel::Usage));
  288. return;
  289. }
  290. network_info.application_data_size = size;
  291. Memory::ReadBlock(address, network_info.application_data.data(), size);
  292. rb.Push(RESULT_SUCCESS);
  293. }
  294. // Sends a 802.11 beacon frame with information about the current network.
  295. static void BeaconBroadcastCallback(u64 userdata, int cycles_late) {
  296. // Don't do anything if we're not actually hosting a network
  297. if (connection_status.status != static_cast<u32>(NetworkStatus::ConnectedAsHost))
  298. return;
  299. // TODO(Subv): Actually generate the beacon and send it.
  300. // Start broadcasting the network, send a beacon frame every 102.4ms.
  301. CoreTiming::ScheduleEvent(msToCycles(DefaultBeaconInterval * MillisecondsPerTU) - cycles_late,
  302. beacon_broadcast_event, 0);
  303. }
  304. const Interface::FunctionInfo FunctionTable[] = {
  305. {0x00010442, nullptr, "Initialize (deprecated)"},
  306. {0x00020000, nullptr, "Scrap"},
  307. {0x00030000, Shutdown, "Shutdown"},
  308. {0x00040402, nullptr, "CreateNetwork (deprecated)"},
  309. {0x00050040, nullptr, "EjectClient"},
  310. {0x00060000, nullptr, "EjectSpectator"},
  311. {0x00070080, nullptr, "UpdateNetworkAttribute"},
  312. {0x00080000, DestroyNetwork, "DestroyNetwork"},
  313. {0x00090442, nullptr, "ConnectNetwork (deprecated)"},
  314. {0x000A0000, nullptr, "DisconnectNetwork"},
  315. {0x000B0000, GetConnectionStatus, "GetConnectionStatus"},
  316. {0x000D0040, nullptr, "GetNodeInformation"},
  317. {0x000E0006, nullptr, "DecryptBeaconData (deprecated)"},
  318. {0x000F0404, RecvBeaconBroadcastData, "RecvBeaconBroadcastData"},
  319. {0x00100042, SetApplicationData, "SetApplicationData"},
  320. {0x00110040, nullptr, "GetApplicationData"},
  321. {0x00120100, Bind, "Bind"},
  322. {0x00130040, nullptr, "Unbind"},
  323. {0x001400C0, nullptr, "PullPacket"},
  324. {0x00150080, nullptr, "SetMaxSendDelay"},
  325. {0x00170182, nullptr, "SendTo"},
  326. {0x001A0000, GetChannel, "GetChannel"},
  327. {0x001B0302, InitializeWithVersion, "InitializeWithVersion"},
  328. {0x001D0044, BeginHostingNetwork, "BeginHostingNetwork"},
  329. {0x001E0084, nullptr, "ConnectToNetwork"},
  330. {0x001F0006, nullptr, "DecryptBeaconData"},
  331. {0x00200040, nullptr, "Flush"},
  332. {0x00210080, nullptr, "SetProbeResponseParam"},
  333. {0x00220402, nullptr, "ScanOnConnection"},
  334. };
  335. NWM_UDS::NWM_UDS() {
  336. connection_status_event =
  337. Kernel::Event::Create(Kernel::ResetType::OneShot, "NWM::connection_status_event");
  338. Register(FunctionTable);
  339. beacon_broadcast_event =
  340. CoreTiming::RegisterEvent("UDS::BeaconBroadcastCallback", BeaconBroadcastCallback);
  341. }
  342. NWM_UDS::~NWM_UDS() {
  343. network_info = {};
  344. bind_node_events.clear();
  345. connection_status_event = nullptr;
  346. recv_buffer_memory = nullptr;
  347. connection_status = {};
  348. connection_status.status = static_cast<u32>(NetworkStatus::NotConnected);
  349. CoreTiming::UnscheduleEvent(beacon_broadcast_event, 0);
  350. }
  351. } // namespace NWM
  352. } // namespace Service