rpc_system.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "flipper.pb.h"
  2. #include "rpc_i.h"
  3. #include "status.pb.h"
  4. #include <furi-hal-info.h>
  5. #include <furi-hal-bootloader.h>
  6. #include <power/power_service/power.h>
  7. void rpc_system_system_ping_process(const PB_Main* msg_request, void* context) {
  8. furi_assert(msg_request);
  9. furi_assert(msg_request->which_content == PB_Main_system_ping_request_tag);
  10. furi_assert(context);
  11. Rpc* rpc = context;
  12. if(msg_request->has_next) {
  13. rpc_send_and_release_empty(
  14. rpc, msg_request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  15. return;
  16. }
  17. PB_Main msg_response = PB_Main_init_default;
  18. msg_response.has_next = false;
  19. msg_response.command_status = PB_CommandStatus_OK;
  20. msg_response.command_id = msg_request->command_id;
  21. msg_response.which_content = PB_Main_system_ping_response_tag;
  22. const PB_System_PingRequest* request = &msg_request->content.system_ping_request;
  23. PB_System_PingResponse* response = &msg_response.content.system_ping_response;
  24. if(request->data && (request->data->size > 0)) {
  25. response->data = furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(request->data->size));
  26. memcpy(response->data->bytes, request->data->bytes, request->data->size);
  27. response->data->size = request->data->size;
  28. }
  29. rpc_send_and_release(rpc, &msg_response);
  30. }
  31. void rpc_system_system_reboot_process(const PB_Main* request, void* context) {
  32. furi_assert(request);
  33. furi_assert(request->which_content == PB_Main_system_reboot_request_tag);
  34. furi_assert(context);
  35. Rpc* rpc = context;
  36. const int mode = request->content.system_reboot_request.mode;
  37. if(mode == PB_System_RebootRequest_RebootMode_OS) {
  38. power_reboot(PowerBootModeNormal);
  39. } else if(mode == PB_System_RebootRequest_RebootMode_DFU) {
  40. power_reboot(PowerBootModeDfu);
  41. } else {
  42. rpc_send_and_release_empty(
  43. rpc, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  44. }
  45. }
  46. typedef struct {
  47. Rpc* rpc;
  48. PB_Main* response;
  49. } RpcSystemSystemDeviceInfoContext;
  50. void rpc_system_system_device_info_callback(
  51. const char* key,
  52. const char* value,
  53. bool last,
  54. void* context) {
  55. furi_assert(key);
  56. furi_assert(value);
  57. furi_assert(context);
  58. RpcSystemSystemDeviceInfoContext* ctx = context;
  59. char* str_key = strdup(key);
  60. char* str_value = strdup(value);
  61. ctx->response->has_next = !last;
  62. ctx->response->content.system_device_info_response.key = str_key;
  63. ctx->response->content.system_device_info_response.value = str_value;
  64. rpc_send_and_release(ctx->rpc, ctx->response);
  65. }
  66. void rpc_system_system_device_info_process(const PB_Main* request, void* context) {
  67. furi_assert(request);
  68. furi_assert(request->which_content == PB_Main_system_device_info_request_tag);
  69. furi_assert(context);
  70. Rpc* rpc = context;
  71. PB_Main* response = furi_alloc(sizeof(PB_Main));
  72. response->command_id = request->command_id;
  73. response->which_content = PB_Main_system_device_info_response_tag;
  74. response->command_status = PB_CommandStatus_OK;
  75. RpcSystemSystemDeviceInfoContext device_info_context = {
  76. .rpc = rpc,
  77. .response = response,
  78. };
  79. furi_hal_info_get(rpc_system_system_device_info_callback, &device_info_context);
  80. free(response);
  81. }
  82. void rpc_system_system_factory_reset_process(const PB_Main* request, void* context) {
  83. furi_assert(request);
  84. furi_assert(request->which_content == PB_Main_system_factory_reset_request_tag);
  85. furi_assert(context);
  86. furi_hal_bootloader_set_flags(FuriHalBootloaderFlagFactoryReset);
  87. power_reboot(PowerBootModeNormal);
  88. }
  89. void* rpc_system_system_alloc(Rpc* rpc) {
  90. RpcHandler rpc_handler = {
  91. .message_handler = NULL,
  92. .decode_submessage = NULL,
  93. .context = rpc,
  94. };
  95. rpc_handler.message_handler = rpc_system_system_ping_process;
  96. rpc_add_handler(rpc, PB_Main_system_ping_request_tag, &rpc_handler);
  97. rpc_handler.message_handler = rpc_system_system_reboot_process;
  98. rpc_add_handler(rpc, PB_Main_system_reboot_request_tag, &rpc_handler);
  99. rpc_handler.message_handler = rpc_system_system_device_info_process;
  100. rpc_add_handler(rpc, PB_Main_system_device_info_request_tag, &rpc_handler);
  101. rpc_handler.message_handler = rpc_system_system_factory_reset_process;
  102. rpc_add_handler(rpc, PB_Main_system_factory_reset_request_tag, &rpc_handler);
  103. return NULL;
  104. }