Rpc.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "pw_rpc/server.h"
  19. #include "pw_rpc_system_server/rpc_server.h"
  20. #include <thread>
  21. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  22. #include "pigweed/rpc_services/Attributes.h"
  23. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  24. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  25. #include "pigweed/rpc_services/Descriptor.h"
  26. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  27. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  28. #include "pigweed/rpc_services/Device.h"
  29. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  30. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  31. #include "pigweed/rpc_services/Lighting.h"
  32. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  33. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  34. #include "pigweed/rpc_services/Locking.h"
  35. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  36. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  37. #define PW_TRACE_BUFFER_SIZE_BYTES 1024
  38. #include "pw_trace/trace.h"
  39. #include "pw_trace_tokenized/trace_rpc_service_nanopb.h"
  40. // Define trace time for pw_trace
  41. PW_TRACE_TIME_TYPE pw_trace_GetTraceTime()
  42. {
  43. return (PW_TRACE_TIME_TYPE) chip::System::SystemClock().GetMonotonicMicroseconds64().count();
  44. }
  45. // Microsecond time source
  46. size_t pw_trace_GetTraceTimeTicksPerSecond()
  47. {
  48. return 1000000;
  49. }
  50. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  51. namespace chip {
  52. namespace rpc {
  53. namespace {
  54. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  55. Attributes attributes_service;
  56. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  57. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  58. Descriptor descriptor_service;
  59. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  60. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  61. Device device_service;
  62. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  63. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  64. Lighting lighting_service;
  65. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  66. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  67. pw::trace::TraceService trace_service(pw::trace::GetTokenizedTracer());
  68. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  69. void RegisterServices(pw::rpc::Server & server)
  70. {
  71. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  72. server.RegisterService(attributes_service);
  73. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  74. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  75. server.RegisterService(descriptor_service);
  76. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  77. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  78. server.RegisterService(device_service);
  79. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  80. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  81. server.RegisterService(lighting_service);
  82. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  83. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  84. server.RegisterService(trace_service);
  85. PW_TRACE_SET_ENABLED(true);
  86. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  87. }
  88. } // namespace
  89. void RunRpcService()
  90. {
  91. pw::rpc::system_server::Init();
  92. RegisterServices(pw::rpc::system_server::Server());
  93. pw::rpc::system_server::Start();
  94. }
  95. int Init()
  96. {
  97. int err = 0;
  98. std::thread rpc_service(RunRpcService);
  99. rpc_service.detach();
  100. return err;
  101. }
  102. } // namespace rpc
  103. } // namespace chip