rpc.c 21 KB

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