service.h 2.2 KB

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