service.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/common_types.h"
  11. #include "core/mem_map.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/syscall.h"
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. // Namespace Service
  16. namespace Service {
  17. static const int kMaxPortSize = 0x08; ///< Maximum size of a port name (8 characters)
  18. static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
  19. /**
  20. * Returns a pointer to the command buffer in kernel memory
  21. * @param offset Optional offset into command buffer
  22. * @return Pointer to command buffer
  23. */
  24. inline static u32* GetCommandBuffer(const int offset=0) {
  25. return (u32*)Memory::GetPointer(Memory::KERNEL_MEMORY_VADDR + kCommandHeaderOffset + offset);
  26. }
  27. class Manager;
  28. /// Interface to a CTROS service
  29. class Interface : public Kernel::Object {
  30. friend class Manager;
  31. public:
  32. const char *GetName() { return GetPortName(); }
  33. const char *GetTypeName() { return GetPortName(); }
  34. static Kernel::HandleType GetStaticHandleType() { return Kernel::HandleType::Service; }
  35. Kernel::HandleType GetHandleType() const { return Kernel::HandleType::Service; }
  36. typedef void (*Function)(Interface*);
  37. struct FunctionInfo {
  38. u32 id;
  39. Function func;
  40. std::string name;
  41. };
  42. /**
  43. * Gets the string name used by CTROS for a service
  44. * @return Port name of service
  45. */
  46. virtual const char *GetPortName() const {
  47. return "[UNKNOWN SERVICE PORT]";
  48. }
  49. /// Allocates a new handle for the service
  50. Handle CreateHandle(Kernel::Object *obj) {
  51. Handle handle = Kernel::g_object_pool.Create(obj);
  52. m_handles.push_back(handle);
  53. return handle;
  54. }
  55. /// Frees a handle from the service
  56. template <class T>
  57. void DeleteHandle(const Handle handle) {
  58. g_object_pool.Destroy<T>(handle);
  59. m_handles.erase(std::remove(m_handles.begin(), m_handles.end(), handle), m_handles.end());
  60. }
  61. /**
  62. * Called when svcSendSyncRequest is called, loads command buffer and executes comand
  63. * @return Return result of svcSendSyncRequest passed back to user app
  64. */
  65. Result Sync() {
  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(), cmd_buff[0]);
  71. return -1;
  72. }
  73. if (itr->second.func == NULL) {
  74. ERROR_LOG(OSHLE, "Unimplemented function: port = %s, name = %s!",
  75. GetPortName(), itr->second.name.c_str());
  76. return -1;
  77. }
  78. itr->second.func(this);
  79. return 0; // TODO: Implement return from actual function
  80. }
  81. protected:
  82. /**
  83. * Registers the functions in the service
  84. */
  85. void Register(const FunctionInfo* functions, int len) {
  86. for (int i = 0; i < len; i++) {
  87. m_functions[functions[i].id] = functions[i];
  88. }
  89. }
  90. private:
  91. std::vector<Handle> m_handles;
  92. std::map<u32, FunctionInfo> m_functions;
  93. };
  94. /// Simple class to manage accessing services from ports and UID handles
  95. class Manager {
  96. public:
  97. Manager();
  98. ~Manager();
  99. /// Add a service to the manager (does not create it though)
  100. void AddService(Interface* service);
  101. /// Removes a service from the manager (does not delete it though)
  102. void DeleteService(std::string port_name);
  103. /// Get a Service Interface from its UID
  104. Interface* FetchFromHandle(u32 uid);
  105. /// Get a Service Interface from its port
  106. Interface* FetchFromPortName(std::string port_name);
  107. private:
  108. std::vector<Interface*> m_services;
  109. std::map<std::string, u32> m_port_map;
  110. };
  111. /// Initialize ServiceManager
  112. void Init();
  113. /// Shutdown ServiceManager
  114. void Shutdown();
  115. extern Manager* g_manager; ///< Service manager
  116. } // namespace