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_telink/init.h"
  24. #include <zephyr/logging/log.h>
  25. #include <zephyr/sys/reboot.h>
  26. #include <zephyr/kernel.h>
  27. LOG_MODULE_DECLARE(app, CONFIG_MATTER_LOG_LEVEL);
  28. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  29. #include "pigweed/rpc_services/Attributes.h"
  30. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  31. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  32. #include "pigweed/rpc_services/Button.h"
  33. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  34. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  35. #include "pigweed/rpc_services/Descriptor.h"
  36. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  37. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  38. #include "pigweed/rpc_services/Device.h"
  39. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  40. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  41. #include "pigweed/rpc_services/Lighting.h"
  42. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  43. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  44. #include "pigweed/rpc_services/Locking.h"
  45. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  46. #if defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  47. #include "pigweed/rpc_services/OtCli.h"
  48. #endif // defined(PW_RPC_OTCLI_SERVICE) && PW_RPC_OTCLI_SERVICE
  49. #if defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  50. #include "pigweed/rpc_services/Thread.h"
  51. #endif // defined(PW_RPC_THREAD_SERVICE) && PW_RPC_THREAD_SERVICE
  52. #if defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  53. #define PW_TRACE_BUFFER_SIZE_BYTES 1024
  54. #include "pw_trace/trace.h"
  55. #include "pw_trace_tokenized/trace_rpc_service_nanopb.h"
  56. // Define trace time for pw_trace
  57. PW_TRACE_TIME_TYPE pw_trace_GetTraceTime()
  58. {
  59. return (PW_TRACE_TIME_TYPE) chip::System::SystemClock().GetMonotonicMicroseconds64().count();
  60. }
  61. // Microsecond time source
  62. size_t pw_trace_GetTraceTimeTicksPerSecond()
  63. {
  64. return 1000000;
  65. }
  66. #endif // defined(PW_RPC_TRACING_SERVICE) && PW_RPC_TRACING_SERVICE
  67. namespace chip {
  68. namespace rpc {
  69. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  70. namespace {
  71. void reboot_timer_handler(struct k_timer * dummy)
  72. {
  73. sys_reboot(0);
  74. }
  75. K_TIMER_DEFINE(reboot_timer, reboot_timer_handler, NULL);
  76. } // namespace
  77. class TelinkDevice final : public Device
  78. {
  79. public:
  80. pw::Status Reboot(const pw_protobuf_Empty & request, pw_protobuf_Empty & response) override
  81. {
  82. k_timer_start(&reboot_timer, K_SECONDS(1), K_FOREVER);
  83. return pw::OkStatus();
  84. }
  85. };
  86. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  87. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  88. class TelinkButton final : public Button
  89. {
  90. public:
  91. pw::Status Event(const chip_rpc_ButtonEvent & request, pw_protobuf_Empty & response) override
  92. {
  93. GetAppTask().ButtonEventHandler((AppTask::ButtonId_t) request.idx, request.pushed);
  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. TelinkButton 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. TelinkDevice 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