service.h 4.4 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 <vector>
  6. #include <map>
  7. #include <string>
  8. #include "common/common.h"
  9. #include "common/common_types.h"
  10. #include "core/mem_map.h"
  11. #include "core/hle/syscall.h"
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////
  13. // Namespace Service
  14. namespace Service {
  15. typedef s32 NativeUID; ///< Native handle for a 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 : NonCopyable {
  29. friend class Manager;
  30. public:
  31. Interface() {
  32. }
  33. virtual ~Interface() {
  34. }
  35. typedef void (*Function)(Interface*);
  36. struct FunctionInfo {
  37. u32 id;
  38. Function func;
  39. std::string name;
  40. };
  41. /**
  42. * Gets the UID for the serice
  43. * @return UID of service in native format
  44. */
  45. NativeUID GetUID() const {
  46. return (NativeUID)m_uid;
  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. /// Allocates a new handle for the service
  56. Syscall::Handle NewHandle() {
  57. Syscall::Handle handle = (m_handles.size() << 16) | m_uid;
  58. m_handles.push_back(handle);
  59. return handle;
  60. }
  61. /// Frees a handle from the service
  62. void DeleteHandle(Syscall::Handle handle) {
  63. for(auto iter = m_handles.begin(); iter != m_handles.end(); ++iter) {
  64. if(*iter == handle) {
  65. m_handles.erase(iter);
  66. break;
  67. }
  68. }
  69. }
  70. /**
  71. * Called when svcSendSyncRequest is called, loads command buffer and executes comand
  72. * @return Return result of svcSendSyncRequest passed back to user app
  73. */
  74. Syscall::Result Sync() {
  75. u32* cmd_buff = GetCommandBuffer();
  76. auto itr = m_functions.find(cmd_buff[0]);
  77. if (itr == m_functions.end()) {
  78. ERROR_LOG(OSHLE, "Unknown/unimplemented function: port = %s, command = 0x%08X!",
  79. GetPortName().c_str(), cmd_buff[0]);
  80. return -1;
  81. }
  82. if (itr->second.func == NULL) {
  83. ERROR_LOG(OSHLE, "Unimplemented function: port = %s, name = %s!",
  84. GetPortName().c_str(), itr->second.name.c_str());
  85. return -1;
  86. }
  87. itr->second.func(this);
  88. return 0; // TODO: Implement return from actual function
  89. }
  90. protected:
  91. /**
  92. * Registers the functions in the service
  93. */
  94. void Register(const FunctionInfo* functions, int len) {
  95. for (int i = 0; i < len; i++) {
  96. m_functions[functions[i].id] = functions[i];
  97. }
  98. }
  99. private:
  100. u32 m_uid;
  101. std::vector<Syscall::Handle> m_handles;
  102. std::map<u32, FunctionInfo> m_functions;
  103. };
  104. /// Simple class to manage accessing services from ports and UID handles
  105. class Manager {
  106. public:
  107. Manager();
  108. ~Manager();
  109. /// Add a service to the manager (does not create it though)
  110. void AddService(Interface* service);
  111. /// Removes a service from the manager (does not delete it though)
  112. void DeleteService(std::string port_name);
  113. /// Get a Service Interface from its UID
  114. Interface* FetchFromUID(u32 uid);
  115. /// Get a Service Interface from its port
  116. Interface* FetchFromPortName(std::string port_name);
  117. private:
  118. /// Convert an index into m_services vector into a UID
  119. static u32 GetUIDFromIndex(const int index) {
  120. return index | 0x10000000;
  121. }
  122. /// Convert a UID into an index into m_services
  123. static int GetIndexFromUID(const u32 uid) {
  124. return uid & 0x0FFFFFFF;
  125. }
  126. std::vector<Interface*> m_services;
  127. std::map<std::string, u32> m_port_map;
  128. };
  129. /// Initialize ServiceManager
  130. void Init();
  131. /// Shutdown ServiceManager
  132. void Shutdown();
  133. extern Manager* g_manager; ///< Service manager
  134. } // namespace