service.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. ResultVal<bool> SyncRequest() override {
  61. u32* cmd_buff = GetCommandBuffer();
  62. auto itr = m_functions.find(cmd_buff[0]);
  63. if (itr == m_functions.end()) {
  64. ERROR_LOG(OSHLE, "unknown/unimplemented function: port=%s, command=0x%08X",
  65. GetPortName().c_str(), cmd_buff[0]);
  66. // TODO(bunnei): Hack - ignore error
  67. u32* cmd_buff = Service::GetCommandBuffer();
  68. cmd_buff[1] = 0;
  69. return MakeResult<bool>(false);
  70. }
  71. if (itr->second.func == nullptr) {
  72. ERROR_LOG(OSHLE, "unimplemented function: port=%s, name=%s",
  73. GetPortName().c_str(), itr->second.name.c_str());
  74. // TODO(bunnei): Hack - ignore error
  75. u32* cmd_buff = Service::GetCommandBuffer();
  76. cmd_buff[1] = 0;
  77. return MakeResult<bool>(false);
  78. }
  79. itr->second.func(this);
  80. return MakeResult<bool>(false); // TODO: Implement return from actual function
  81. }
  82. ResultVal<bool> WaitSynchronization() override {
  83. // TODO(bunnei): ImplementMe
  84. ERROR_LOG(OSHLE, "unimplemented function");
  85. return UnimplementedFunction(ErrorModule::OS);
  86. }
  87. protected:
  88. /**
  89. * Registers the functions in the service
  90. */
  91. void Register(const FunctionInfo* functions, int len) {
  92. for (int i = 0; i < len; i++) {
  93. m_functions[functions[i].id] = functions[i];
  94. }
  95. }
  96. private:
  97. std::vector<Handle> m_handles;
  98. std::map<u32, FunctionInfo> m_functions;
  99. };
  100. /// Simple class to manage accessing services from ports and UID handles
  101. class Manager {
  102. public:
  103. Manager();
  104. ~Manager();
  105. /// Add a service to the manager (does not create it though)
  106. void AddService(Interface* service);
  107. /// Removes a service from the manager (does not delete it though)
  108. void DeleteService(const std::string& port_name);
  109. /// Get a Service Interface from its UID
  110. Interface* FetchFromHandle(u32 uid);
  111. /// Get a Service Interface from its port
  112. Interface* FetchFromPortName(const std::string& port_name);
  113. private:
  114. std::vector<Interface*> m_services;
  115. std::map<std::string, u32> m_port_map;
  116. };
  117. /// Initialize ServiceManager
  118. void Init();
  119. /// Shutdown ServiceManager
  120. void Shutdown();
  121. extern Manager* g_manager; ///< Service manager
  122. } // namespace