rpc_property.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <flipper.pb.h>
  2. #include <furi_hal.h>
  3. #include <furi_hal_info.h>
  4. #include <furi_hal_power.h>
  5. #include <core/core_defines.h>
  6. #include "rpc_i.h"
  7. #define TAG "RpcProperty"
  8. #define PROPERTY_CATEGORY_DEVICE_INFO "devinfo"
  9. #define PROPERTY_CATEGORY_POWER_INFO "pwrinfo"
  10. #define PROPERTY_CATEGORY_POWER_DEBUG "pwrdebug"
  11. typedef struct {
  12. RpcSession* session;
  13. PB_Main* response;
  14. FuriString* subkey;
  15. } RpcPropertyContext;
  16. static void
  17. rpc_system_property_get_callback(const char* key, const char* value, bool last, void* context) {
  18. furi_assert(key);
  19. furi_assert(value);
  20. furi_assert(context);
  21. furi_assert(key);
  22. furi_assert(value);
  23. RpcPropertyContext* ctx = context;
  24. RpcSession* session = ctx->session;
  25. PB_Main* response = ctx->response;
  26. if(!strncmp(key, furi_string_get_cstr(ctx->subkey), furi_string_size(ctx->subkey))) {
  27. response->content.system_device_info_response.key = strdup(key);
  28. response->content.system_device_info_response.value = strdup(value);
  29. rpc_send_and_release(session, response);
  30. }
  31. if(last) {
  32. rpc_send_and_release_empty(session, response->command_id, PB_CommandStatus_OK);
  33. }
  34. }
  35. static void rpc_system_property_get_process(const PB_Main* request, void* context) {
  36. furi_assert(request);
  37. furi_assert(request->which_content == PB_Main_property_get_request_tag);
  38. FURI_LOG_D(TAG, "GetProperty");
  39. RpcSession* session = (RpcSession*)context;
  40. furi_assert(session);
  41. FuriString* topkey = furi_string_alloc();
  42. FuriString* subkey = furi_string_alloc_set_str(request->content.property_get_request.key);
  43. const size_t sep_idx = furi_string_search_char(subkey, '.');
  44. if(sep_idx == FURI_STRING_FAILURE) {
  45. furi_string_swap(topkey, subkey);
  46. } else {
  47. furi_string_set_n(topkey, subkey, 0, sep_idx);
  48. furi_string_right(subkey, sep_idx + 1);
  49. }
  50. PB_Main* response = malloc(sizeof(PB_Main));
  51. response->command_id = request->command_id;
  52. response->command_status = PB_CommandStatus_OK;
  53. response->has_next = true;
  54. response->which_content = PB_Main_property_get_response_tag;
  55. RpcPropertyContext property_context = {
  56. .session = session,
  57. .response = response,
  58. .subkey = subkey,
  59. };
  60. if(!furi_string_cmp(topkey, PROPERTY_CATEGORY_DEVICE_INFO)) {
  61. furi_hal_info_get(rpc_system_property_get_callback, '.', &property_context);
  62. } else if(!furi_string_cmp(topkey, PROPERTY_CATEGORY_POWER_INFO)) {
  63. furi_hal_power_info_get(rpc_system_property_get_callback, '.', &property_context);
  64. } else if(!furi_string_cmp(topkey, PROPERTY_CATEGORY_POWER_DEBUG)) {
  65. furi_hal_power_debug_get(rpc_system_property_get_callback, &property_context);
  66. } else {
  67. rpc_send_and_release_empty(
  68. session, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  69. }
  70. furi_string_free(subkey);
  71. furi_string_free(topkey);
  72. free(response);
  73. }
  74. void* rpc_system_property_alloc(RpcSession* session) {
  75. furi_assert(session);
  76. RpcHandler rpc_handler = {
  77. .message_handler = NULL,
  78. .decode_submessage = NULL,
  79. .context = session,
  80. };
  81. rpc_handler.message_handler = rpc_system_property_get_process;
  82. rpc_add_handler(session, PB_Main_property_get_request_tag, &rpc_handler);
  83. return NULL;
  84. }