service.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include <boost/container/flat_map.hpp>
  10. #include "common/common.h"
  11. #include "common/string_util.h"
  12. #include "core/mem_map.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/kernel/session.h"
  15. #include "core/hle/svc.h"
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Namespace Service
  18. namespace Service {
  19. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  20. class Manager;
  21. /// Interface to a CTROS service
  22. class Interface : public Kernel::Session {
  23. // TODO(yuriks): An "Interface" being a Kernel::Object is mostly non-sense. Interface should be
  24. // just something that encapsulates a session and acts as a helper to implement service
  25. // processes.
  26. friend class Manager;
  27. /**
  28. * Creates a function string for logging, complete with the name (or header code, depending
  29. * on what's passed in) the port name, and all the cmd_buff arguments.
  30. */
  31. std::string MakeFunctionString(const char* name, const char* port_name, const u32* cmd_buff) {
  32. // Number of params == bits 0-5 + bits 6-11
  33. int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F);
  34. std::string function_string = Common::StringFromFormat("function '%s': port=%s", name, port_name);
  35. for (int i = 1; i <= num_params; ++i) {
  36. function_string += Common::StringFromFormat(", cmd_buff[%i]=%u", i, cmd_buff[i]);
  37. }
  38. return function_string;
  39. }
  40. public:
  41. std::string GetName() const override { return GetPortName(); }
  42. typedef void (*Function)(Interface*);
  43. struct FunctionInfo {
  44. u32 id;
  45. Function func;
  46. const char* name;
  47. };
  48. /**
  49. * Gets the string name used by CTROS for a service
  50. * @return Port name of service
  51. */
  52. virtual std::string GetPortName() const {
  53. return "[UNKNOWN SERVICE PORT]";
  54. }
  55. ResultVal<bool> SyncRequest() override {
  56. u32* cmd_buff = Kernel::GetCommandBuffer();
  57. auto itr = m_functions.find(cmd_buff[0]);
  58. if (itr == m_functions.end() || itr->second.func == nullptr) {
  59. std::string function_name = (itr == m_functions.end()) ? Common::StringFromFormat("0x%08X", cmd_buff[0]) : itr->second.name;
  60. LOG_ERROR(Service, "unknown / unimplemented %s", MakeFunctionString(function_name.c_str(), GetPortName().c_str(), cmd_buff).c_str());
  61. // TODO(bunnei): Hack - ignore error
  62. cmd_buff[1] = 0;
  63. return MakeResult<bool>(false);
  64. } else {
  65. LOG_TRACE(Service, "%s", MakeFunctionString(itr->second.name, GetPortName().c_str(), cmd_buff).c_str());
  66. }
  67. itr->second.func(this);
  68. return MakeResult<bool>(false); // TODO: Implement return from actual function
  69. }
  70. protected:
  71. /**
  72. * Registers the functions in the service
  73. */
  74. template <size_t N>
  75. void Register(const FunctionInfo (&functions)[N]) {
  76. m_functions.reserve(N);
  77. for (auto& fn : functions) {
  78. // Usually this array is sorted by id already, so hint to instead at the end
  79. m_functions.emplace_hint(m_functions.cend(), fn.id, fn);
  80. }
  81. }
  82. private:
  83. boost::container::flat_map<u32, FunctionInfo> m_functions;
  84. };
  85. /// Initialize ServiceManager
  86. void Init();
  87. /// Shutdown ServiceManager
  88. void Shutdown();
  89. /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
  90. extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_kernel_named_ports;
  91. /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
  92. extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_srv_services;
  93. /// Adds a service to the services table
  94. void AddService(Interface* interface_);
  95. } // namespace