service.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "core/mem_map.h"
  11. #include "core/hle/kernel/kernel.h"
  12. #include "core/hle/svc.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Namespace Service
  15. namespace Service {
  16. static const int kMaxPortSize = 0x08; ///< Maximum size of a port name (8 characters)
  17. static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
  18. /**
  19. * Returns a pointer to the command buffer in kernel memory
  20. * @param offset Optional offset into command buffer
  21. * @return Pointer to command buffer
  22. */
  23. inline static u32* GetCommandBuffer(const int offset=0) {
  24. return (u32*)Memory::GetPointer(Memory::KERNEL_MEMORY_VADDR + kCommandHeaderOffset + offset);
  25. }
  26. class Manager;
  27. /// Interface to a CTROS service
  28. class Interface : public Kernel::Object {
  29. friend class Manager;
  30. public:
  31. std::string GetName() const override { return GetPortName(); }
  32. std::string GetTypeName() const override { return GetPortName(); }
  33. static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Service; }
  34. Kernel::HandleType GetHandleType() const override { return Kernel::HandleType::Service; }
  35. typedef void (*Function)(Interface*);
  36. struct FunctionInfo {
  37. u32 id;
  38. Function func;
  39. std::string name;
  40. };
  41. /**
  42. * Gets the string name used by CTROS for a service
  43. * @return Port name of service
  44. */
  45. virtual std::string GetPortName() const {
  46. return "[UNKNOWN SERVICE PORT]";
  47. }
  48. /// Allocates a new handle for the service
  49. Handle CreateHandle(Kernel::Object *obj) {
  50. Handle handle = Kernel::g_object_pool.Create(obj);
  51. m_handles.push_back(handle);
  52. return handle;
  53. }
  54. /// Frees a handle from the service
  55. template <class T>
  56. void DeleteHandle(const Handle handle) {
  57. Kernel::g_object_pool.Destroy<T>(handle);
  58. m_handles.erase(std::remove(m_handles.begin(), m_handles.end(), handle), m_handles.end());
  59. }
  60. /**
  61. * Synchronize kernel object
  62. * @param wait Boolean wait set if current thread should wait as a result of sync operation
  63. * @return Result of operation, 0 on success, otherwise error code
  64. */
  65. Result SyncRequest(bool* wait) override {
  66. u32* cmd_buff = GetCommandBuffer();
  67. auto itr = m_functions.find(cmd_buff[0]);
  68. if (itr == m_functions.end()) {
  69. ERROR_LOG(OSHLE, "unknown/unimplemented function: port=%s, command=0x%08X",
  70. GetPortName().c_str(), cmd_buff[0]);
  71. // TODO(bunnei): Hack - ignore error
  72. u32* cmd_buff = Service::GetCommandBuffer();
  73. cmd_buff[1] = 0;
  74. return 0;
  75. }
  76. if (itr->second.func == nullptr) {
  77. ERROR_LOG(OSHLE, "unimplemented function: port=%s, name=%s",
  78. GetPortName().c_str(), itr->second.name.c_str());
  79. // TODO(bunnei): Hack - ignore error
  80. u32* cmd_buff = Service::GetCommandBuffer();
  81. cmd_buff[1] = 0;
  82. return 0;
  83. }
  84. itr->second.func(this);
  85. return 0; // TODO: Implement return from actual function
  86. }
  87. /**
  88. * Wait for kernel object to synchronize
  89. * @param wait Boolean wait set if current thread should wait as a result of sync operation
  90. * @return Result of operation, 0 on success, otherwise error code
  91. */
  92. Result WaitSynchronization(bool* wait) override {
  93. // TODO(bunnei): ImplementMe
  94. ERROR_LOG(OSHLE, "unimplemented function");
  95. return 0;
  96. }
  97. protected:
  98. /**
  99. * Registers the functions in the service
  100. */
  101. void Register(const FunctionInfo* functions, int len) {
  102. for (int i = 0; i < len; i++) {
  103. m_functions[functions[i].id] = functions[i];
  104. }
  105. }
  106. private:
  107. std::vector<Handle> m_handles;
  108. std::map<u32, FunctionInfo> m_functions;
  109. };
  110. /// Simple class to manage accessing services from ports and UID handles
  111. class Manager {
  112. public:
  113. Manager();
  114. ~Manager();
  115. /// Add a service to the manager (does not create it though)
  116. void AddService(Interface* service);
  117. /// Removes a service from the manager (does not delete it though)
  118. void DeleteService(const std::string& port_name);
  119. /// Get a Service Interface from its UID
  120. Interface* FetchFromHandle(u32 uid);
  121. /// Get a Service Interface from its port
  122. Interface* FetchFromPortName(const std::string& port_name);
  123. private:
  124. std::vector<Interface*> m_services;
  125. std::map<std::string, u32> m_port_map;
  126. };
  127. /// Initialize ServiceManager
  128. void Init();
  129. /// Shutdown ServiceManager
  130. void Shutdown();
  131. extern Manager* g_manager; ///< Service manager
  132. } // namespace