rpc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #include "rpc_i.h"
  2. #include <pb.h>
  3. #include <pb_decode.h>
  4. #include <pb_encode.h>
  5. #include <storage.pb.h>
  6. #include <flipper.pb.h>
  7. #include <portmacro.h>
  8. #include <furi.h>
  9. #include <cli/cli.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stream_buffer.h>
  13. #include <m-string.h>
  14. #include <m-dict.h>
  15. #define TAG "RpcSrv"
  16. #define RPC_EVENT_NEW_DATA (1 << 0)
  17. #define RPC_EVENT_DISCONNECT (1 << 1)
  18. #define RPC_EVENTS_ALL (RPC_EVENT_DISCONNECT | RPC_EVENT_NEW_DATA)
  19. DICT_DEF2(RpcHandlerDict, pb_size_t, M_DEFAULT_OPLIST, RpcHandler, M_POD_OPLIST)
  20. typedef struct {
  21. RpcSystemAlloc alloc;
  22. RpcSystemFree free;
  23. void* context;
  24. } RpcSystemCallbacks;
  25. static RpcSystemCallbacks rpc_systems[] = {
  26. {
  27. .alloc = rpc_system_system_alloc,
  28. .free = NULL,
  29. },
  30. {
  31. .alloc = rpc_system_storage_alloc,
  32. .free = rpc_system_storage_free,
  33. },
  34. {
  35. .alloc = rpc_system_app_alloc,
  36. .free = NULL,
  37. },
  38. {
  39. .alloc = rpc_system_gui_alloc,
  40. .free = rpc_system_gui_free,
  41. },
  42. };
  43. struct RpcSession {
  44. RpcSendBytesCallback send_bytes_callback;
  45. RpcBufferIsEmptyCallback buffer_is_empty_callback;
  46. RpcSessionClosedCallback closed_callback;
  47. void* context;
  48. osMutexId_t callbacks_mutex;
  49. Rpc* rpc;
  50. bool terminate;
  51. void** system_contexts;
  52. };
  53. struct Rpc {
  54. bool busy;
  55. osMutexId_t busy_mutex;
  56. RpcSession session;
  57. osEventFlagsId_t events;
  58. StreamBufferHandle_t stream;
  59. RpcHandlerDict_t handlers;
  60. PB_Main* decoded_message;
  61. };
  62. static bool content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg);
  63. static void rpc_close_session_process(const PB_Main* msg_request, void* context) {
  64. furi_assert(msg_request);
  65. furi_assert(context);
  66. Rpc* rpc = context;
  67. rpc_send_and_release_empty(rpc, msg_request->command_id, PB_CommandStatus_OK);
  68. osMutexAcquire(rpc->session.callbacks_mutex, osWaitForever);
  69. if(rpc->session.closed_callback) {
  70. rpc->session.closed_callback(rpc->session.context);
  71. }
  72. osMutexRelease(rpc->session.callbacks_mutex);
  73. }
  74. static size_t rpc_sprintf_msg_file(
  75. string_t str,
  76. const char* prefix,
  77. const PB_Storage_File* msg_file,
  78. size_t msg_files_size) {
  79. size_t cnt = 0;
  80. for(int i = 0; i < msg_files_size; ++i, ++msg_file) {
  81. string_cat_printf(
  82. str,
  83. "%s[%c] size: %5ld",
  84. prefix,
  85. msg_file->type == PB_Storage_File_FileType_DIR ? 'd' : 'f',
  86. msg_file->size);
  87. if(msg_file->name) {
  88. string_cat_printf(str, " \'%s\'", msg_file->name);
  89. }
  90. if(msg_file->data && msg_file->data->size) {
  91. string_cat_printf(
  92. str,
  93. " (%d):\'%.*s%s\'",
  94. msg_file->data->size,
  95. MIN(msg_file->data->size, 30),
  96. msg_file->data->bytes,
  97. msg_file->data->size > 30 ? "..." : "");
  98. }
  99. string_cat_printf(str, "\r\n");
  100. }
  101. return cnt;
  102. }
  103. void rpc_print_data(const char* prefix, uint8_t* buffer, size_t size) {
  104. string_t str;
  105. string_init(str);
  106. string_reserve(str, 100 + size * 5);
  107. string_cat_printf(str, "\r\n%s DEC(%d): {", prefix, size);
  108. for(int i = 0; i < size; ++i) {
  109. string_cat_printf(str, "%d, ", buffer[i]);
  110. }
  111. string_cat_printf(str, "}\r\n");
  112. printf("%s", string_get_cstr(str));
  113. string_reset(str);
  114. string_reserve(str, 100 + size * 3);
  115. string_cat_printf(str, "%s HEX(%d): {", prefix, size);
  116. for(int i = 0; i < size; ++i) {
  117. string_cat_printf(str, "%02X", buffer[i]);
  118. }
  119. string_cat_printf(str, "}\r\n\r\n");
  120. printf("%s", string_get_cstr(str));
  121. string_clear(str);
  122. }
  123. void rpc_print_message(const PB_Main* message) {
  124. string_t str;
  125. string_init(str);
  126. string_cat_printf(
  127. str,
  128. "PB_Main: {\r\n\tresult: %d cmd_id: %ld (%s)\r\n",
  129. message->command_status,
  130. message->command_id,
  131. message->has_next ? "has_next" : "last");
  132. switch(message->which_content) {
  133. default:
  134. /* not implemented yet */
  135. string_cat_printf(str, "\tNOT_IMPLEMENTED (%d) {\r\n", message->which_content);
  136. break;
  137. case PB_Main_stop_session_tag:
  138. string_cat_printf(str, "\tstop_session {\r\n");
  139. break;
  140. case PB_Main_app_start_request_tag: {
  141. string_cat_printf(str, "\tapp_start {\r\n");
  142. const char* name = message->content.app_start_request.name;
  143. const char* args = message->content.app_start_request.args;
  144. if(name) {
  145. string_cat_printf(str, "\t\tname: %s\r\n", name);
  146. }
  147. if(args) {
  148. string_cat_printf(str, "\t\targs: %s\r\n", args);
  149. }
  150. break;
  151. }
  152. case PB_Main_app_lock_status_request_tag: {
  153. string_cat_printf(str, "\tapp_lock_status_request {\r\n");
  154. break;
  155. }
  156. case PB_Main_app_lock_status_response_tag: {
  157. string_cat_printf(str, "\tapp_lock_status_response {\r\n");
  158. bool lock_status = message->content.app_lock_status_response.locked;
  159. string_cat_printf(str, "\t\tlocked: %s\r\n", lock_status ? "true" : "false");
  160. break;
  161. }
  162. case PB_Main_storage_md5sum_request_tag: {
  163. string_cat_printf(str, "\tmd5sum_request {\r\n");
  164. const char* path = message->content.storage_md5sum_request.path;
  165. if(path) {
  166. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  167. }
  168. break;
  169. }
  170. case PB_Main_storage_md5sum_response_tag: {
  171. string_cat_printf(str, "\tmd5sum_response {\r\n");
  172. const char* path = message->content.storage_md5sum_response.md5sum;
  173. if(path) {
  174. string_cat_printf(str, "\t\tmd5sum: %s\r\n", path);
  175. }
  176. break;
  177. }
  178. case PB_Main_system_ping_request_tag:
  179. string_cat_printf(str, "\tping_request {\r\n");
  180. break;
  181. case PB_Main_system_ping_response_tag:
  182. string_cat_printf(str, "\tping_response {\r\n");
  183. break;
  184. case PB_Main_system_device_info_request_tag:
  185. string_cat_printf(str, "\tdevice_info_request {\r\n");
  186. break;
  187. case PB_Main_system_device_info_response_tag:
  188. string_cat_printf(str, "\tdevice_info_response {\r\n");
  189. string_cat_printf(
  190. str,
  191. "\t\t%s: %s\r\n",
  192. message->content.system_device_info_response.key,
  193. message->content.system_device_info_response.value);
  194. break;
  195. case PB_Main_storage_mkdir_request_tag:
  196. string_cat_printf(str, "\tmkdir {\r\n");
  197. break;
  198. case PB_Main_storage_delete_request_tag: {
  199. string_cat_printf(str, "\tdelete {\r\n");
  200. const char* path = message->content.storage_delete_request.path;
  201. if(path) {
  202. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  203. }
  204. break;
  205. }
  206. case PB_Main_empty_tag:
  207. string_cat_printf(str, "\tempty {\r\n");
  208. break;
  209. case PB_Main_storage_info_request_tag: {
  210. string_cat_printf(str, "\tinfo_request {\r\n");
  211. const char* path = message->content.storage_info_request.path;
  212. if(path) {
  213. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  214. }
  215. break;
  216. }
  217. case PB_Main_storage_info_response_tag: {
  218. string_cat_printf(str, "\tinfo_response {\r\n");
  219. string_cat_printf(
  220. str, "\t\ttotal_space: %lu\r\n", message->content.storage_info_response.total_space);
  221. string_cat_printf(
  222. str, "\t\tfree_space: %lu\r\n", message->content.storage_info_response.free_space);
  223. break;
  224. }
  225. case PB_Main_storage_stat_request_tag: {
  226. string_cat_printf(str, "\tstat_request {\r\n");
  227. const char* path = message->content.storage_stat_request.path;
  228. if(path) {
  229. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  230. }
  231. break;
  232. }
  233. case PB_Main_storage_stat_response_tag: {
  234. string_cat_printf(str, "\tstat_response {\r\n");
  235. if(message->content.storage_stat_response.has_file) {
  236. const PB_Storage_File* msg_file = &message->content.storage_stat_response.file;
  237. rpc_sprintf_msg_file(str, "\t\t\t", msg_file, 1);
  238. }
  239. break;
  240. }
  241. case PB_Main_storage_list_request_tag: {
  242. string_cat_printf(str, "\tlist_request {\r\n");
  243. const char* path = message->content.storage_list_request.path;
  244. if(path) {
  245. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  246. }
  247. break;
  248. }
  249. case PB_Main_storage_read_request_tag: {
  250. string_cat_printf(str, "\tread_request {\r\n");
  251. const char* path = message->content.storage_read_request.path;
  252. if(path) {
  253. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  254. }
  255. break;
  256. }
  257. case PB_Main_storage_write_request_tag: {
  258. string_cat_printf(str, "\twrite_request {\r\n");
  259. const char* path = message->content.storage_write_request.path;
  260. if(path) {
  261. string_cat_printf(str, "\t\tpath: %s\r\n", path);
  262. }
  263. if(message->content.storage_write_request.has_file) {
  264. const PB_Storage_File* msg_file = &message->content.storage_write_request.file;
  265. rpc_sprintf_msg_file(str, "\t\t\t", msg_file, 1);
  266. }
  267. break;
  268. }
  269. case PB_Main_storage_read_response_tag:
  270. string_cat_printf(str, "\tread_response {\r\n");
  271. if(message->content.storage_read_response.has_file) {
  272. const PB_Storage_File* msg_file = &message->content.storage_read_response.file;
  273. rpc_sprintf_msg_file(str, "\t\t\t", msg_file, 1);
  274. }
  275. break;
  276. case PB_Main_storage_list_response_tag: {
  277. const PB_Storage_File* msg_file = message->content.storage_list_response.file;
  278. size_t msg_file_count = message->content.storage_list_response.file_count;
  279. string_cat_printf(str, "\tlist_response {\r\n");
  280. rpc_sprintf_msg_file(str, "\t\t", msg_file, msg_file_count);
  281. break;
  282. }
  283. case PB_Main_storage_rename_request_tag: {
  284. string_cat_printf(str, "\trename_request {\r\n");
  285. string_cat_printf(
  286. str, "\t\told_path: %s\r\n", message->content.storage_rename_request.old_path);
  287. string_cat_printf(
  288. str, "\t\tnew_path: %s\r\n", message->content.storage_rename_request.new_path);
  289. break;
  290. }
  291. case PB_Main_gui_start_screen_stream_request_tag:
  292. string_cat_printf(str, "\tstart_screen_stream {\r\n");
  293. break;
  294. case PB_Main_gui_stop_screen_stream_request_tag:
  295. string_cat_printf(str, "\tstop_screen_stream {\r\n");
  296. break;
  297. case PB_Main_gui_screen_frame_tag:
  298. string_cat_printf(str, "\tscreen_frame {\r\n");
  299. break;
  300. case PB_Main_gui_send_input_event_request_tag:
  301. string_cat_printf(str, "\tsend_input_event {\r\n");
  302. string_cat_printf(
  303. str, "\t\tkey: %d\r\n", message->content.gui_send_input_event_request.key);
  304. string_cat_printf(
  305. str, "\t\type: %d\r\n", message->content.gui_send_input_event_request.type);
  306. break;
  307. case PB_Main_gui_start_virtual_display_request_tag:
  308. string_cat_printf(str, "\tstart_virtual_display {\r\n");
  309. break;
  310. case PB_Main_gui_stop_virtual_display_request_tag:
  311. string_cat_printf(str, "\tstop_virtual_display {\r\n");
  312. break;
  313. }
  314. string_cat_printf(str, "\t}\r\n}\r\n");
  315. printf("%s", string_get_cstr(str));
  316. string_clear(str);
  317. }
  318. static Rpc* rpc_alloc(void) {
  319. Rpc* rpc = furi_alloc(sizeof(Rpc));
  320. rpc->busy_mutex = osMutexNew(NULL);
  321. rpc->busy = false;
  322. rpc->events = osEventFlagsNew(NULL);
  323. rpc->stream = xStreamBufferCreate(RPC_BUFFER_SIZE, 1);
  324. rpc->decoded_message = furi_alloc(sizeof(PB_Main));
  325. rpc->decoded_message->cb_content.funcs.decode = content_callback;
  326. rpc->decoded_message->cb_content.arg = rpc;
  327. RpcHandlerDict_init(rpc->handlers);
  328. return rpc;
  329. }
  330. RpcSession* rpc_session_open(Rpc* rpc) {
  331. furi_assert(rpc);
  332. bool result = false;
  333. furi_check(osMutexAcquire(rpc->busy_mutex, osWaitForever) == osOK);
  334. if(rpc->busy) {
  335. result = false;
  336. } else {
  337. rpc->busy = true;
  338. result = true;
  339. }
  340. furi_check(osMutexRelease(rpc->busy_mutex) == osOK);
  341. if(result) {
  342. RpcSession* session = &rpc->session;
  343. session->callbacks_mutex = osMutexNew(NULL);
  344. session->rpc = rpc;
  345. session->terminate = false;
  346. xStreamBufferReset(rpc->stream);
  347. session->system_contexts = furi_alloc(COUNT_OF(rpc_systems) * sizeof(void*));
  348. for(int i = 0; i < COUNT_OF(rpc_systems); ++i) {
  349. session->system_contexts[i] = rpc_systems[i].alloc(rpc);
  350. }
  351. RpcHandler rpc_handler = {
  352. .message_handler = rpc_close_session_process,
  353. .decode_submessage = NULL,
  354. .context = rpc,
  355. };
  356. rpc_add_handler(rpc, PB_Main_stop_session_tag, &rpc_handler);
  357. FURI_LOG_D(TAG, "Session started");
  358. }
  359. return result ? &rpc->session : NULL; /* support 1 open session for now */
  360. }
  361. void rpc_session_close(RpcSession* session) {
  362. furi_assert(session);
  363. furi_assert(session->rpc);
  364. furi_assert(session->rpc->busy);
  365. rpc_session_set_send_bytes_callback(session, NULL);
  366. rpc_session_set_close_callback(session, NULL);
  367. rpc_session_set_buffer_is_empty_callback(session, NULL);
  368. osEventFlagsSet(session->rpc->events, RPC_EVENT_DISCONNECT);
  369. }
  370. static void rpc_free_session(RpcSession* session) {
  371. furi_assert(session);
  372. for(int i = 0; i < COUNT_OF(rpc_systems); ++i) {
  373. if(rpc_systems[i].free) {
  374. rpc_systems[i].free(session->system_contexts[i]);
  375. }
  376. }
  377. free(session->system_contexts);
  378. osMutexDelete(session->callbacks_mutex);
  379. RpcHandlerDict_reset(session->rpc->handlers);
  380. session->context = NULL;
  381. session->closed_callback = NULL;
  382. session->send_bytes_callback = NULL;
  383. session->buffer_is_empty_callback = NULL;
  384. }
  385. void rpc_session_set_context(RpcSession* session, void* context) {
  386. furi_assert(session);
  387. furi_assert(session->rpc);
  388. furi_assert(session->rpc->busy);
  389. osMutexAcquire(session->callbacks_mutex, osWaitForever);
  390. session->context = context;
  391. osMutexRelease(session->callbacks_mutex);
  392. }
  393. void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback) {
  394. furi_assert(session);
  395. furi_assert(session->rpc);
  396. furi_assert(session->rpc->busy);
  397. osMutexAcquire(session->callbacks_mutex, osWaitForever);
  398. session->closed_callback = callback;
  399. osMutexRelease(session->callbacks_mutex);
  400. }
  401. void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback) {
  402. furi_assert(session);
  403. furi_assert(session->rpc);
  404. furi_assert(session->rpc->busy);
  405. osMutexAcquire(session->callbacks_mutex, osWaitForever);
  406. session->send_bytes_callback = callback;
  407. osMutexRelease(session->callbacks_mutex);
  408. }
  409. void rpc_session_set_buffer_is_empty_callback(
  410. RpcSession* session,
  411. RpcBufferIsEmptyCallback callback) {
  412. furi_assert(session);
  413. furi_assert(session->rpc->busy);
  414. osMutexAcquire(session->callbacks_mutex, osWaitForever);
  415. session->buffer_is_empty_callback = callback;
  416. osMutexRelease(session->callbacks_mutex);
  417. }
  418. /* Doesn't forbid using rpc_feed_bytes() after session close - it's safe.
  419. * Because any bytes received in buffer will be flushed before next session.
  420. * If bytes get into stream buffer before it's get epmtified and this
  421. * command is gets processed - it's safe either. But case of it is quite
  422. * odd: client sends close request and sends command after.
  423. */
  424. size_t
  425. rpc_session_feed(RpcSession* session, uint8_t* encoded_bytes, size_t size, TickType_t timeout) {
  426. furi_assert(session);
  427. Rpc* rpc = session->rpc;
  428. furi_assert(rpc->busy);
  429. size_t bytes_sent = xStreamBufferSend(rpc->stream, encoded_bytes, size, timeout);
  430. osEventFlagsSet(rpc->events, RPC_EVENT_NEW_DATA);
  431. return bytes_sent;
  432. }
  433. size_t rpc_session_get_available_size(RpcSession* session) {
  434. furi_assert(session);
  435. Rpc* rpc = session->rpc;
  436. return xStreamBufferSpacesAvailable(rpc->stream);
  437. }
  438. bool rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
  439. Rpc* rpc = istream->state;
  440. uint32_t flags = 0;
  441. size_t bytes_received = 0;
  442. furi_assert(istream->bytes_left);
  443. while(1) {
  444. bytes_received +=
  445. xStreamBufferReceive(rpc->stream, buf + bytes_received, count - bytes_received, 0);
  446. if(xStreamBufferIsEmpty(rpc->stream)) {
  447. if(rpc->session.buffer_is_empty_callback) {
  448. rpc->session.buffer_is_empty_callback(rpc->session.context);
  449. }
  450. }
  451. if(count == bytes_received) {
  452. break;
  453. } else {
  454. flags = osEventFlagsWait(rpc->events, RPC_EVENTS_ALL, 0, osWaitForever);
  455. if(flags & RPC_EVENT_DISCONNECT) {
  456. if(xStreamBufferIsEmpty(rpc->stream)) {
  457. rpc->session.terminate = true;
  458. istream->bytes_left = 0;
  459. bytes_received = 0;
  460. break;
  461. } else {
  462. /* Save disconnect flag and continue reading buffer */
  463. osEventFlagsSet(rpc->events, RPC_EVENT_DISCONNECT);
  464. }
  465. }
  466. }
  467. }
  468. #if SRV_RPC_DEBUG
  469. rpc_print_data("INPUT", buf, bytes_received);
  470. #endif
  471. return (count == bytes_received);
  472. }
  473. void rpc_send_and_release(Rpc* rpc, PB_Main* message) {
  474. furi_assert(rpc);
  475. furi_assert(message);
  476. RpcSession* session = &rpc->session;
  477. pb_ostream_t ostream = PB_OSTREAM_SIZING;
  478. #if SRV_RPC_DEBUG
  479. FURI_LOG_I(TAG, "OUTPUT:");
  480. rpc_print_message(message);
  481. #endif
  482. bool result = pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
  483. furi_check(result && ostream.bytes_written);
  484. uint8_t* buffer = furi_alloc(ostream.bytes_written);
  485. ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
  486. pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
  487. #if SRV_RPC_DEBUG
  488. rpc_print_data("OUTPUT", buffer, ostream.bytes_written);
  489. #endif
  490. osMutexAcquire(session->callbacks_mutex, osWaitForever);
  491. if(session->send_bytes_callback) {
  492. session->send_bytes_callback(session->context, buffer, ostream.bytes_written);
  493. }
  494. osMutexRelease(session->callbacks_mutex);
  495. free(buffer);
  496. pb_release(&PB_Main_msg, message);
  497. }
  498. static bool content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg) {
  499. furi_assert(stream);
  500. Rpc* rpc = *arg;
  501. RpcHandler* handler = RpcHandlerDict_get(rpc->handlers, field->tag);
  502. if(handler && handler->decode_submessage) {
  503. handler->decode_submessage(stream, field, arg);
  504. }
  505. return true;
  506. }
  507. int32_t rpc_srv(void* p) {
  508. Rpc* rpc = rpc_alloc();
  509. furi_record_create("rpc", rpc);
  510. Cli* cli = furi_record_open("cli");
  511. cli_add_command(
  512. cli, "start_rpc_session", CliCommandFlagParallelSafe, rpc_cli_command_start_session, rpc);
  513. while(1) {
  514. pb_istream_t istream = {
  515. .callback = rpc_pb_stream_read,
  516. .state = rpc,
  517. .errmsg = NULL,
  518. .bytes_left = RPC_MAX_MESSAGE_SIZE, /* max incoming message size */
  519. };
  520. if(pb_decode_ex(&istream, &PB_Main_msg, rpc->decoded_message, PB_DECODE_DELIMITED)) {
  521. #if SRV_RPC_DEBUG
  522. FURI_LOG_I(TAG, "INPUT:");
  523. rpc_print_message(rpc->decoded_message);
  524. #endif
  525. RpcHandler* handler =
  526. RpcHandlerDict_get(rpc->handlers, rpc->decoded_message->which_content);
  527. if(handler && handler->message_handler) {
  528. handler->message_handler(rpc->decoded_message, handler->context);
  529. } else if(!handler && !rpc->session.terminate) {
  530. FURI_LOG_E(TAG, "Unhandled message, tag: %d", rpc->decoded_message->which_content);
  531. }
  532. } else {
  533. xStreamBufferReset(rpc->stream);
  534. if(!rpc->session.terminate) {
  535. FURI_LOG_E(TAG, "Decode failed, error: \'%.128s\'", PB_GET_ERROR(&istream));
  536. }
  537. }
  538. pb_release(&PB_Main_msg, rpc->decoded_message);
  539. if(rpc->session.terminate) {
  540. FURI_LOG_D(TAG, "Session terminated");
  541. osEventFlagsClear(rpc->events, RPC_EVENTS_ALL);
  542. rpc_free_session(&rpc->session);
  543. rpc->busy = false;
  544. }
  545. }
  546. return 0;
  547. }
  548. void rpc_add_handler(Rpc* rpc, pb_size_t message_tag, RpcHandler* handler) {
  549. furi_assert(RpcHandlerDict_get(rpc->handlers, message_tag) == NULL);
  550. RpcHandlerDict_set_at(rpc->handlers, message_tag, *handler);
  551. }
  552. void rpc_send_and_release_empty(Rpc* rpc, uint32_t command_id, PB_CommandStatus status) {
  553. PB_Main message = {
  554. .command_id = command_id,
  555. .command_status = status,
  556. .has_next = false,
  557. .which_content = PB_Main_empty_tag,
  558. };
  559. rpc_send_and_release(rpc, &message);
  560. pb_release(&PB_Main_msg, &message);
  561. }