service.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_types.h"
  9. #include "core/hle/syscall.h"
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // Namespace Service
  12. namespace Service {
  13. typedef s32 NativeUID; ///< Native handle for a service
  14. class Manager;
  15. /// Interface to a CTROS service
  16. class Interface {
  17. friend class Manager;
  18. public:
  19. virtual ~Interface() {
  20. }
  21. /**
  22. * Gets the UID for the serice
  23. * @return UID of service in native format
  24. */
  25. NativeUID GetUID() const {
  26. return (NativeUID)m_uid;
  27. }
  28. /**
  29. * Gets the string name used by CTROS for a service
  30. * @return String name of service
  31. */
  32. virtual std::string GetName() const {
  33. return "[UNKNOWN SERVICE NAME]";
  34. }
  35. /**
  36. * Gets the string name used by CTROS for a service
  37. * @return Port name of service
  38. */
  39. virtual std::string GetPortName() const {
  40. return "[UNKNOWN SERVICE PORT]";
  41. }
  42. /**
  43. * Called when svcSendSyncRequest is called, loads command buffer and executes comand
  44. * @return Return result of svcSendSyncRequest passed back to user app
  45. */
  46. virtual Syscall::Result Sync() = 0;
  47. private:
  48. u32 m_uid;
  49. };
  50. /// Simple class to manage accessing services from ports and UID handles
  51. class Manager {
  52. public:
  53. Manager();
  54. ~Manager();
  55. /// Add a service to the manager (does not create it though)
  56. void AddService(Interface* service);
  57. /// Removes a service from the manager (does not delete it though)
  58. void DeleteService(std::string port_name);
  59. /// Get a Service Interface from its UID
  60. Interface* FetchFromUID(u32 uid);
  61. /// Get a Service Interface from its port
  62. Interface* FetchFromPortName(std::string port_name);
  63. private:
  64. /// Convert an index into m_services vector into a UID
  65. static u32 GetUIDFromIndex(const int index) {
  66. return index | 0x10000000;
  67. }
  68. /// Convert a UID into an index into m_services
  69. static int GetIndexFromUID(const u32 uid) {
  70. return uid & 0x0FFFFFFF;
  71. }
  72. std::vector<Interface*> m_services;
  73. std::map<std::string, u32> m_port_map;
  74. DISALLOW_COPY_AND_ASSIGN(Manager);
  75. };
  76. /// Initialize ServiceManager
  77. void Init();
  78. /// Shutdown ServiceManager
  79. void Shutdown();
  80. extern Manager* g_manager; ///< Service manager
  81. } // namespace