srv.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <tuple>
  5. #include "common/common_types.h"
  6. #include "common/logging/log.h"
  7. #include "core/hle/kernel/client_session.h"
  8. #include "core/hle/kernel/server_session.h"
  9. #include "core/hle/kernel/event.h"
  10. #include "core/hle/service/srv.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace SRV
  13. namespace SRV {
  14. static Kernel::SharedPtr<Kernel::Event> event_handle;
  15. /**
  16. * SRV::RegisterClient service function
  17. * Inputs:
  18. * 0: 0x00010002
  19. * 1: ProcessId Header (must be 0x20)
  20. * Outputs:
  21. * 0: 0x00010040
  22. * 1: ResultCode
  23. */
  24. static void RegisterClient(Service::Interface* self) {
  25. u32* cmd_buff = Kernel::GetCommandBuffer();
  26. if (cmd_buff[1] != IPC::CallingPidDesc()) {
  27. cmd_buff[0] = IPC::MakeHeader(0x0, 0x1, 0); // 0x40
  28. cmd_buff[1] = ResultCode(ErrorDescription::OS_InvalidBufferDescriptor, ErrorModule::OS,
  29. ErrorSummary::WrongArgument, ErrorLevel::Permanent)
  30. .raw;
  31. return;
  32. }
  33. cmd_buff[0] = IPC::MakeHeader(0x1, 0x1, 0); // 0x10040
  34. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  35. LOG_WARNING(Service_SRV, "(STUBBED) called");
  36. }
  37. /**
  38. * SRV::EnableNotification service function
  39. * Inputs:
  40. * 0: 0x00020000
  41. * Outputs:
  42. * 0: 0x00020042
  43. * 1: ResultCode
  44. * 2: Translation descriptor: 0x20
  45. * 3: Handle to semaphore signaled on process notification
  46. */
  47. static void EnableNotification(Service::Interface* self) {
  48. u32* cmd_buff = Kernel::GetCommandBuffer();
  49. // TODO(bunnei): Change to a semaphore once these have been implemented
  50. event_handle = Kernel::Event::Create(Kernel::ResetType::OneShot, "SRV:Event");
  51. event_handle->Clear();
  52. cmd_buff[0] = IPC::MakeHeader(0x2, 0x1, 0x2); // 0x20042
  53. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  54. cmd_buff[2] = IPC::CopyHandleDesc(1);
  55. cmd_buff[3] = Kernel::g_handle_table.Create(event_handle).MoveFrom();
  56. LOG_WARNING(Service_SRV, "(STUBBED) called");
  57. }
  58. /**
  59. * SRV::GetServiceHandle service function
  60. * Inputs:
  61. * 0: 0x00050100
  62. * 1-2: 8-byte UTF-8 service name
  63. * 3: Name length
  64. * 4: Flags (bit0: if not set, return port-handle if session-handle unavailable)
  65. * Outputs:
  66. * 1: ResultCode
  67. * 3: Service handle
  68. */
  69. static void GetServiceHandle(Service::Interface* self) {
  70. ResultCode res = RESULT_SUCCESS;
  71. u32* cmd_buff = Kernel::GetCommandBuffer();
  72. std::string port_name = std::string((const char*)&cmd_buff[1], 0, Service::kMaxPortSize);
  73. auto it = Service::g_srv_services.find(port_name);
  74. if (it != Service::g_srv_services.end()) {
  75. auto client_port = it->second;
  76. auto client_session = client_port->Connect();
  77. res = client_session.Code();
  78. if (client_session.Succeeded()) {
  79. // Return the client session
  80. cmd_buff[3] = Kernel::g_handle_table.Create(*client_session).MoveFrom();
  81. }
  82. LOG_TRACE(Service_SRV, "called port=%s, handle=0x%08X", port_name.c_str(), cmd_buff[3]);
  83. } else {
  84. LOG_ERROR(Service_SRV, "(UNIMPLEMENTED) called port=%s", port_name.c_str());
  85. res = UnimplementedFunction(ErrorModule::SRV);
  86. }
  87. cmd_buff[1] = res.raw;
  88. }
  89. /**
  90. * SRV::Subscribe service function
  91. * Inputs:
  92. * 0: 0x00090040
  93. * 1: Notification ID
  94. * Outputs:
  95. * 0: 0x00090040
  96. * 1: ResultCode
  97. */
  98. static void Subscribe(Service::Interface* self) {
  99. u32* cmd_buff = Kernel::GetCommandBuffer();
  100. u32 notification_id = cmd_buff[1];
  101. cmd_buff[0] = IPC::MakeHeader(0x9, 0x1, 0); // 0x90040
  102. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  103. LOG_WARNING(Service_SRV, "(STUBBED) called, notification_id=0x%X", notification_id);
  104. }
  105. /**
  106. * SRV::Unsubscribe service function
  107. * Inputs:
  108. * 0: 0x000A0040
  109. * 1: Notification ID
  110. * Outputs:
  111. * 0: 0x000A0040
  112. * 1: ResultCode
  113. */
  114. static void Unsubscribe(Service::Interface* self) {
  115. u32* cmd_buff = Kernel::GetCommandBuffer();
  116. u32 notification_id = cmd_buff[1];
  117. cmd_buff[0] = IPC::MakeHeader(0xA, 0x1, 0); // 0xA0040
  118. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  119. LOG_WARNING(Service_SRV, "(STUBBED) called, notification_id=0x%X", notification_id);
  120. }
  121. /**
  122. * SRV::PublishToSubscriber service function
  123. * Inputs:
  124. * 0: 0x000C0080
  125. * 1: Notification ID
  126. * 2: Flags (bit0: only fire if not fired, bit1: report errors)
  127. * Outputs:
  128. * 0: 0x000C0040
  129. * 1: ResultCode
  130. */
  131. static void PublishToSubscriber(Service::Interface* self) {
  132. u32* cmd_buff = Kernel::GetCommandBuffer();
  133. u32 notification_id = cmd_buff[1];
  134. u8 flags = cmd_buff[2] & 0xFF;
  135. cmd_buff[0] = IPC::MakeHeader(0xC, 0x1, 0); // 0xC0040
  136. cmd_buff[1] = RESULT_SUCCESS.raw; // No error
  137. LOG_WARNING(Service_SRV, "(STUBBED) called, notification_id=0x%X, flags=%u", notification_id,
  138. flags);
  139. }
  140. const Interface::FunctionInfo FunctionTable[] = {
  141. {0x00010002, RegisterClient, "RegisterClient"},
  142. {0x00020000, EnableNotification, "EnableNotification"},
  143. {0x00030100, nullptr, "RegisterService"},
  144. {0x000400C0, nullptr, "UnregisterService"},
  145. {0x00050100, GetServiceHandle, "GetServiceHandle"},
  146. {0x000600C2, nullptr, "RegisterPort"},
  147. {0x000700C0, nullptr, "UnregisterPort"},
  148. {0x00080100, nullptr, "GetPort"},
  149. {0x00090040, Subscribe, "Subscribe"},
  150. {0x000A0040, Unsubscribe, "Unsubscribe"},
  151. {0x000B0000, nullptr, "ReceiveNotification"},
  152. {0x000C0080, PublishToSubscriber, "PublishToSubscriber"},
  153. {0x000D0040, nullptr, "PublishAndGetSubscriber"},
  154. {0x000E00C0, nullptr, "IsServiceRegistered"},
  155. };
  156. ////////////////////////////////////////////////////////////////////////////////////////////////////
  157. // Interface class
  158. Interface::Interface() {
  159. Register(FunctionTable);
  160. event_handle = nullptr;
  161. }
  162. Interface::~Interface() {
  163. event_handle = nullptr;
  164. }
  165. } // namespace SRV