service.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <algorithm>
  6. #include <vector>
  7. #include <map>
  8. #include <string>
  9. #include "common/common.h"
  10. #include "common/string_util.h"
  11. #include "core/mem_map.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/session.h"
  14. #include "core/hle/svc.h"
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Namespace Service
  17. namespace Service {
  18. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  19. class Manager;
  20. /// Interface to a CTROS service
  21. class Interface : public Kernel::Session {
  22. // TODO(yuriks): An "Interface" being a Kernel::Object is mostly non-sense. Interface should be
  23. // just something that encapsulates a session and acts as a helper to implement service
  24. // processes.
  25. friend class Manager;
  26. public:
  27. std::string GetName() const override { return GetPortName(); }
  28. typedef void (*Function)(Interface*);
  29. struct FunctionInfo {
  30. u32 id;
  31. Function func;
  32. std::string name;
  33. };
  34. /**
  35. * Gets the string name used by CTROS for a service
  36. * @return Port name of service
  37. */
  38. virtual std::string GetPortName() const {
  39. return "[UNKNOWN SERVICE PORT]";
  40. }
  41. /// Allocates a new handle for the service
  42. Handle CreateHandle(Kernel::Object *obj) {
  43. Handle handle = Kernel::g_object_pool.Create(obj);
  44. m_handles.push_back(handle);
  45. return handle;
  46. }
  47. /// Frees a handle from the service
  48. template <class T>
  49. void DeleteHandle(const Handle handle) {
  50. Kernel::g_object_pool.Destroy<T>(handle);
  51. m_handles.erase(std::remove(m_handles.begin(), m_handles.end(), handle), m_handles.end());
  52. }
  53. ResultVal<bool> SyncRequest() override {
  54. u32* cmd_buff = Kernel::GetCommandBuffer();
  55. auto itr = m_functions.find(cmd_buff[0]);
  56. if (itr == m_functions.end() || itr->second.func == nullptr) {
  57. // Number of params == bits 0-5 + bits 6-11
  58. int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F);
  59. std::string error = "unknown/unimplemented function '%s': port=%s";
  60. for (int i = 1; i <= num_params; ++i) {
  61. error += Common::StringFromFormat(", cmd_buff[%i]=%u", i, cmd_buff[i]);
  62. }
  63. std::string name = (itr == m_functions.end()) ? Common::StringFromFormat("0x%08X", cmd_buff[0]) : itr->second.name;
  64. LOG_ERROR(Service, error.c_str(), name.c_str(), GetPortName().c_str());
  65. // TODO(bunnei): Hack - ignore error
  66. cmd_buff[1] = 0;
  67. return MakeResult<bool>(false);
  68. }
  69. itr->second.func(this);
  70. return MakeResult<bool>(false); // TODO: Implement return from actual function
  71. }
  72. protected:
  73. /**
  74. * Registers the functions in the service
  75. */
  76. void Register(const FunctionInfo* functions, int len) {
  77. for (int i = 0; i < len; i++) {
  78. m_functions[functions[i].id] = functions[i];
  79. }
  80. }
  81. private:
  82. std::vector<Handle> m_handles;
  83. std::map<u32, FunctionInfo> m_functions;
  84. };
  85. /// Simple class to manage accessing services from ports and UID handles
  86. class Manager {
  87. public:
  88. Manager();
  89. ~Manager();
  90. /// Add a service to the manager (does not create it though)
  91. void AddService(Interface* service);
  92. /// Removes a service from the manager (does not delete it though)
  93. void DeleteService(const std::string& port_name);
  94. /// Get a Service Interface from its UID
  95. Interface* FetchFromHandle(u32 uid);
  96. /// Get a Service Interface from its port
  97. Interface* FetchFromPortName(const std::string& port_name);
  98. private:
  99. std::vector<Interface*> m_services;
  100. std::map<std::string, u32> m_port_map;
  101. };
  102. /// Initialize ServiceManager
  103. void Init();
  104. /// Shutdown ServiceManager
  105. void Shutdown();
  106. extern Manager* g_manager; ///< Service manager
  107. } // namespace