service.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <string>
  6. #include <unordered_map>
  7. #include <boost/container/flat_map.hpp>
  8. #include "common/common_types.h"
  9. #include "core/hle/kernel/kernel.h"
  10. #include "core/hle/kernel/session.h"
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. // Namespace Service
  13. namespace Service {
  14. static const int kMaxPortSize = 8; ///< Maximum size of a port name (8 characters)
  15. /// Interface to a CTROS service
  16. class Interface : public Kernel::Session {
  17. // TODO(yuriks): An "Interface" being a Kernel::Object is mostly non-sense. Interface should be
  18. // just something that encapsulates a session and acts as a helper to implement service
  19. // processes.
  20. public:
  21. std::string GetName() const override { return GetPortName(); }
  22. typedef void (*Function)(Interface*);
  23. struct FunctionInfo {
  24. u32 id;
  25. Function func;
  26. const char* name;
  27. };
  28. /**
  29. * Gets the string name used by CTROS for a service
  30. * @return Port name of service
  31. */
  32. virtual std::string GetPortName() const {
  33. return "[UNKNOWN SERVICE PORT]";
  34. }
  35. ResultVal<bool> SyncRequest() override;
  36. protected:
  37. /**
  38. * Registers the functions in the service
  39. */
  40. template <size_t N>
  41. inline void Register(const FunctionInfo (&functions)[N]) {
  42. Register(functions, N);
  43. }
  44. void Register(const FunctionInfo* functions, size_t n);
  45. private:
  46. boost::container::flat_map<u32, FunctionInfo> m_functions;
  47. };
  48. /// Initialize ServiceManager
  49. void Init();
  50. /// Shutdown ServiceManager
  51. void Shutdown();
  52. /// Map of named ports managed by the kernel, which can be retrieved using the ConnectToPort SVC.
  53. extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_kernel_named_ports;
  54. /// Map of services registered with the "srv:" service, retrieved using GetServiceHandle.
  55. extern std::unordered_map<std::string, Kernel::SharedPtr<Interface>> g_srv_services;
  56. /// Adds a service to the services table
  57. void AddService(Interface* interface_);
  58. } // namespace