Rpc.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 CONFIG_ENABLE_PW_RPC
  19. #include "FreeRTOS.h"
  20. #include "PigweedLoggerMutex.h"
  21. #include "RpcService.h"
  22. #include "pw_sys_io_ameba/init.h"
  23. #include "sys_api.h"
  24. #include "task.h"
  25. //#include "esp_log.h"
  26. //#include "freertos/FreeRTOS.h"
  27. //#include "freertos/event_groups.h"
  28. //#include "freertos/semphr.h"
  29. //#include "freertos/task.h"
  30. #include "pw_log/log.h"
  31. #include "pw_rpc/server.h"
  32. #include "pw_sys_io/sys_io.h"
  33. #include "support/CodeUtils.h"
  34. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  35. #include "pigweed/rpc_services/Attributes.h"
  36. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  37. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  38. //#include "ScreenManager.h"
  39. #include "pigweed/rpc_services/Button.h"
  40. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  41. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  42. #include "pigweed/rpc_services/Descriptor.h"
  43. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  44. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  45. #include "pigweed/rpc_services/Device.h"
  46. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  47. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  48. #include "pigweed/rpc_services/Lighting.h"
  49. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  50. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  51. #include "pigweed/rpc_services/Locking.h"
  52. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  53. namespace chip {
  54. namespace rpc {
  55. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  56. class AmebaButton final : public Button
  57. {
  58. public:
  59. pw::Status Event(const chip_rpc_ButtonEvent & request, pw_protobuf_Empty & response) override
  60. {
  61. return pw::Status::Unimplemented();
  62. }
  63. };
  64. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  65. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  66. class AmebaDevice final : public Device
  67. {
  68. public:
  69. pw::Status Reboot(const pw_protobuf_Empty & request, pw_protobuf_Empty & response) override
  70. {
  71. mRebootTimer = xTimerCreateStatic("Reboot", kRebootTimerPeriodTicks, false, nullptr, RebootHandler, &mRebootTimerBuffer);
  72. xTimerStart(mRebootTimer, 0);
  73. return pw::OkStatus();
  74. }
  75. private:
  76. static constexpr TickType_t kRebootTimerPeriodTicks = 1000;
  77. TimerHandle_t mRebootTimer;
  78. StaticTimer_t mRebootTimerBuffer;
  79. static void RebootHandler(TimerHandle_t) { sys_reset(); }
  80. };
  81. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  82. namespace {
  83. #define RPC_TASK_STACK_SIZE (8 * 1024)
  84. #define RPC_TASK_PRIORITY 1
  85. static TaskHandle_t sRpcTaskHandle;
  86. StaticTask_t sRpcTaskBuffer;
  87. StackType_t sRpcTaskStack[RPC_TASK_STACK_SIZE];
  88. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  89. Attributes attributes_service;
  90. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  91. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  92. AmebaButton button_service;
  93. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  94. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  95. Descriptor descriptor_service;
  96. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  97. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  98. AmebaDevice device_service;
  99. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  100. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  101. Lighting lighting_service;
  102. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  103. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  104. Locking locking;
  105. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  106. void RegisterServices(pw::rpc::Server & server)
  107. {
  108. #if defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  109. server.RegisterService(attributes_service);
  110. #endif // defined(PW_RPC_ATTRIBUTE_SERVICE) && PW_RPC_ATTRIBUTE_SERVICE
  111. #if defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  112. server.RegisterService(button_service);
  113. #endif // defined(PW_RPC_BUTTON_SERVICE) && PW_RPC_BUTTON_SERVICE
  114. #if defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  115. server.RegisterService(descriptor_service);
  116. #endif // defined(PW_RPC_DESCRIPTOR_SERVICE) && PW_RPC_DESCRIPTOR_SERVICE
  117. #if defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  118. server.RegisterService(device_service);
  119. #endif // defined(PW_RPC_DEVICE_SERVICE) && PW_RPC_DEVICE_SERVICE
  120. #if defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  121. server.RegisterService(lighting_service);
  122. #endif // defined(PW_RPC_LIGHTING_SERVICE) && PW_RPC_LIGHTING_SERVICE
  123. #if defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  124. server.RegisterService(locking);
  125. #endif // defined(PW_RPC_LOCKING_SERVICE) && PW_RPC_LOCKING_SERVICE
  126. }
  127. } // namespace
  128. void RunRpcService(void *)
  129. {
  130. Start(RegisterServices, &logger_mutex);
  131. }
  132. void Init()
  133. {
  134. PigweedLogger::init();
  135. // Start App task.
  136. sRpcTaskHandle = xTaskCreateStatic(RunRpcService, "RPC_TASK", ArraySize(sRpcTaskStack), nullptr, RPC_TASK_PRIORITY,
  137. sRpcTaskStack, &sRpcTaskBuffer);
  138. }
  139. } // namespace rpc
  140. } // namespace chip
  141. #endif // CONFIG_ENABLE_PW_RPCC