Rpc.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  19. #include "AppTask.h"
  20. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  21. #include "PigweedLoggerMutex.h"
  22. #include "pigweed/RpcService.h"
  23. #include "pw_sys_io_nrfconnect/init.h"
  24. #include <zephyr/logging/log.h>
  25. #include <zephyr/kernel.h>
  26. LOG_MODULE_DECLARE(app, CONFIG_CHIP_APP_LOG_LEVEL);
  27. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  28. #include "pigweed/rpc_services/Attributes.h"
  29. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  30. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  31. #include "pigweed/rpc_services/Button.h"
  32. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  33. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  34. #include "pigweed/rpc_services/Descriptor.h"
  35. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  36. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  37. #include "pigweed/rpc_services/Device.h"
  38. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  39. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  40. #include "pigweed/rpc_services/Lighting.h"
  41. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  42. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  43. #include "pigweed/rpc_services/Locking.h"
  44. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  45. #if defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  46. #include "pigweed/rpc_services/OtCli.h"
  47. #endif // defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  48. #if defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  49. #include "pigweed/rpc_services/Thread.h"
  50. #endif // defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  51. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  52. #define PW_TRACE_BUFFER_SIZE_BYTES 1024
  53. #include "pw_trace/trace.h"
  54. #include "pw_trace_tokenized/trace_rpc_service_nanopb.h"
  55. // Define trace time for pw_trace
  56. PW_TRACE_TIME_TYPE pw_trace_GetTraceTime()
  57. {
  58. return (PW_TRACE_TIME_TYPE) chip::System::SystemClock().GetMonotonicMicroseconds64().count();
  59. }
  60. // Microsecond time source
  61. size_t pw_trace_GetTraceTimeTicksPerSecond()
  62. {
  63. return 1000000;
  64. }
  65. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  66. namespace chip {
  67. namespace rpc {
  68. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  69. namespace {
  70. void reboot_timer_handler(struct k_timer * dummy)
  71. {
  72. NVIC_SystemReset();
  73. }
  74. K_TIMER_DEFINE(reboot_timer, reboot_timer_handler, NULL);
  75. } // namespace
  76. class NrfDevice final : public Device
  77. {
  78. public:
  79. pw::Status Reboot(const pw_protobuf_Empty & request, pw_protobuf_Empty & response) override
  80. {
  81. k_timer_start(&reboot_timer, K_SECONDS(1), K_FOREVER);
  82. return pw::OkStatus();
  83. }
  84. };
  85. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  86. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  87. class NrfButton final : public Button
  88. {
  89. public:
  90. pw::Status Event(const chip_rpc_ButtonEvent & request, pw_protobuf_Empty & response) override
  91. {
  92. AppTask::Instance().ButtonEventHandler(request.pushed << request.idx /* button_state */,
  93. 1 << request.idx /* has_changed */);
  94. return pw::OkStatus();
  95. }
  96. };
  97. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  98. namespace {
  99. constexpr size_t kRpcTaskSize = 5120;
  100. constexpr int kRpcPriority = 5;
  101. K_THREAD_STACK_DEFINE(rpc_stack_area, kRpcTaskSize);
  102. struct k_thread rpc_thread_data;
  103. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  104. Attributes attributes_service;
  105. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  106. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  107. NrfButton button_service;
  108. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  109. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  110. Descriptor descriptor_service;
  111. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  112. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  113. NrfDevice device_service;
  114. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  115. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  116. Lighting lighting_service;
  117. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  118. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  119. Locking locking;
  120. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  121. #if defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  122. OtCli ot_cli_service;
  123. #endif // defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  124. #if defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  125. Thread thread;
  126. #endif // defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  127. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  128. pw::trace::TraceService trace_service(pw::trace::GetTokenizedTracer());
  129. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  130. void RegisterServices(pw::rpc::Server & server)
  131. {
  132. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  133. server.RegisterService(attributes_service);
  134. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  135. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  136. server.RegisterService(button_service);
  137. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  138. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  139. server.RegisterService(descriptor_service);
  140. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  141. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  142. server.RegisterService(device_service);
  143. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  144. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  145. server.RegisterService(lighting_service);
  146. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  147. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  148. server.RegisterService(locking);
  149. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  150. #if defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  151. server.RegisterService(ot_cli_service);
  152. #endif // defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  153. #if defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  154. server.RegisterService(thread);
  155. #endif // defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  156. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  157. server.RegisterService(trace_service);
  158. PW_TRACE_SET_ENABLED(true);
  159. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  160. }
  161. } // namespace
  162. void RunRpcService(void *, void *, void *)
  163. {
  164. Start(RegisterServices, &logger_mutex);
  165. }
  166. k_tid_t Init()
  167. {
  168. pw_sys_io_Init();
  169. k_tid_t tid = k_thread_create(&rpc_thread_data, rpc_stack_area, K_THREAD_STACK_SIZEOF(rpc_stack_area), RunRpcService, NULL,
  170. NULL, NULL, kRpcPriority, 0, K_NO_WAIT);
  171. return tid;
  172. }
  173. } // namespace rpc
  174. } // namespace chip