service.h 4.2 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 <vector>
  6. #include <map>
  7. #include <string>
  8. #include "common/common.h"
  9. #include "common/common_types.h"
  10. #include "core/hle/syscall.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace Service
  13. namespace Service {
  14. typedef s32 NativeUID; ///< Native handle for a service
  15. static const int kMaxPortSize = 0x08; ///< Maximum size of a port name (8 characters)
  16. static const int kCommandHeaderOffset = 0x80; ///< Offset into command buffer of header
  17. class Manager;
  18. /// Interface to a CTROS service
  19. class Interface : NonCopyable {
  20. friend class Manager;
  21. public:
  22. Interface() {
  23. }
  24. virtual ~Interface() {
  25. }
  26. typedef void (*Function)(Interface*);
  27. struct FunctionInfo {
  28. u32 id;
  29. Function func;
  30. std::string name;
  31. };
  32. /**
  33. * Gets the UID for the serice
  34. * @return UID of service in native format
  35. */
  36. NativeUID GetUID() const {
  37. return (NativeUID)m_uid;
  38. }
  39. /**
  40. * Gets the string name used by CTROS for a service
  41. * @return Port name of service
  42. */
  43. virtual std::string GetPortName() const {
  44. return "[UNKNOWN SERVICE PORT]";
  45. }
  46. /// Allocates a new handle for the service
  47. Syscall::Handle NewHandle() {
  48. Syscall::Handle handle = (m_handles.size() << 16) | m_uid;
  49. m_handles.push_back(handle);
  50. return handle;
  51. }
  52. /// Frees a handle from the service
  53. void DeleteHandle(Syscall::Handle handle) {
  54. for(auto iter = m_handles.begin(); iter != m_handles.end(); ++iter) {
  55. if(*iter == handle) {
  56. m_handles.erase(iter);
  57. break;
  58. }
  59. }
  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. Syscall::Result Sync() {
  66. u32* cmd_buff = (u32*)HLE::GetPointer(HLE::CMD_BUFFER_ADDR + kCommandHeaderOffset);
  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. return -1;
  72. }
  73. if (itr->second.func == NULL) {
  74. ERROR_LOG(OSHLE, "Unimplemented function: port = %s, name = %s!",
  75. GetPortName().c_str(), 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. u32 m_uid;
  92. std::vector<Syscall::Handle> m_handles;
  93. std::map<u32, FunctionInfo> m_functions;
  94. };
  95. /// Simple class to manage accessing services from ports and UID handles
  96. class Manager {
  97. public:
  98. Manager();
  99. ~Manager();
  100. /// Add a service to the manager (does not create it though)
  101. void AddService(Interface* service);
  102. /// Removes a service from the manager (does not delete it though)
  103. void DeleteService(std::string port_name);
  104. /// Get a Service Interface from its UID
  105. Interface* FetchFromUID(u32 uid);
  106. /// Get a Service Interface from its port
  107. Interface* FetchFromPortName(std::string port_name);
  108. private:
  109. /// Convert an index into m_services vector into a UID
  110. static u32 GetUIDFromIndex(const int index) {
  111. return index | 0x10000000;
  112. }
  113. /// Convert a UID into an index into m_services
  114. static int GetIndexFromUID(const u32 uid) {
  115. return uid & 0x0FFFFFFF;
  116. }
  117. std::vector<Interface*> m_services;
  118. std::map<std::string, u32> m_port_map;
  119. };
  120. /// Initialize ServiceManager
  121. void Init();
  122. /// Shutdown ServiceManager
  123. void Shutdown();
  124. extern Manager* g_manager; ///< Service manager
  125. } // namespace