service.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  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/string_util.h"
  11. #include "core/mem_map.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/session.h"
  14. #include "core/hle/svc.h"
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Namespace Service
  17. namespace Service {
  18. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  19. class Manager;
  20. /// Interface to a CTROS service
  21. class Interface : public Kernel::Session {
  22. // TODO(yuriks): An "Interface" being a Kernel::Object is mostly non-sense. Interface should be
  23. // just something that encapsulates a session and acts as a helper to implement service
  24. // processes.
  25. friend class Manager;
  26. public:
  27. std::string GetName() const override { return GetPortName(); }
  28. typedef void (*Function)(Interface*);
  29. struct FunctionInfo {
  30. u32 id;
  31. Function func;
  32. std::string name;
  33. };
  34. /**
  35. * Gets the string name used by CTROS for a service
  36. * @return Port name of service
  37. */
  38. virtual std::string GetPortName() const {
  39. return "[UNKNOWN SERVICE PORT]";
  40. }
  41. /// Allocates a new handle for the service
  42. Handle CreateHandle(Kernel::Object *obj) {
  43. // TODO(yuriks): Fix error reporting
  44. Handle handle = Kernel::g_handle_table.Create(obj).ValueOr(INVALID_HANDLE);
  45. m_handles.push_back(handle);
  46. return handle;
  47. }
  48. /// Frees a handle from the service
  49. template <class T>
  50. void DeleteHandle(const Handle handle) {
  51. Kernel::g_handle_table.Close(handle);
  52. m_handles.erase(std::remove(m_handles.begin(), m_handles.end(), handle), m_handles.end());
  53. }
  54. ResultVal<bool> SyncRequest() override {
  55. u32* cmd_buff = Kernel::GetCommandBuffer();
  56. auto itr = m_functions.find(cmd_buff[0]);
  57. if (itr == m_functions.end() || itr->second.func == nullptr) {
  58. // Number of params == bits 0-5 + bits 6-11
  59. int num_params = (cmd_buff[0] & 0x3F) + ((cmd_buff[0] >> 6) & 0x3F);
  60. std::string error = "unknown/unimplemented function '%s': port=%s";
  61. for (int i = 1; i <= num_params; ++i) {
  62. error += Common::StringFromFormat(", cmd_buff[%i]=%u", i, cmd_buff[i]);
  63. }
  64. std::string name = (itr == m_functions.end()) ? Common::StringFromFormat("0x%08X", cmd_buff[0]) : itr->second.name;
  65. LOG_ERROR(Service, error.c_str(), name.c_str(), GetPortName().c_str());
  66. // TODO(bunnei): Hack - ignore error
  67. cmd_buff[1] = 0;
  68. return MakeResult<bool>(false);
  69. }
  70. itr->second.func(this);
  71. return MakeResult<bool>(false); // TODO: Implement return from actual function
  72. }
  73. protected:
  74. /**
  75. * Registers the functions in the service
  76. */
  77. void Register(const FunctionInfo* functions, int len) {
  78. for (int i = 0; i < len; i++) {
  79. m_functions[functions[i].id] = functions[i];
  80. }
  81. }
  82. private:
  83. std::vector<Handle> m_handles;
  84. std::map<u32, FunctionInfo> m_functions;
  85. };
  86. /// Simple class to manage accessing services from ports and UID handles
  87. class Manager {
  88. public:
  89. Manager();
  90. ~Manager();
  91. /// Add a service to the manager (does not create it though)
  92. void AddService(Interface* service);
  93. /// Removes a service from the manager (does not delete it though)
  94. void DeleteService(const std::string& port_name);
  95. /// Get a Service Interface from its UID
  96. Interface* FetchFromHandle(u32 uid);
  97. /// Get a Service Interface from its port
  98. Interface* FetchFromPortName(const std::string& port_name);
  99. private:
  100. std::vector<Interface*> m_services;
  101. std::map<std::string, u32> m_port_map;
  102. };
  103. /// Initialize ServiceManager
  104. void Init();
  105. /// Shutdown ServiceManager
  106. void Shutdown();
  107. extern Manager* g_manager; ///< Service manager
  108. } // namespace