esubghz_chat.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/modules/text_box.h>
  5. #include <gui/modules/text_input.h>
  6. #include <gui/view_dispatcher.h>
  7. #include <gui/scene_manager.h>
  8. #include <toolbox/sha256.h>
  9. #include <notification/notification_messages.h>
  10. #include "crypto/gcm.h"
  11. #define APPLICATION_NAME "ESubGhzChat"
  12. #define DEFAULT_FREQ 433920000
  13. #define KEY_BITS 256
  14. #define IV_BYTES 12
  15. #define TAG_BYTES 16
  16. #define RX_TX_BUFFER_SIZE 1024
  17. #define CHAT_BOX_STORE_SIZE 4096
  18. #define TEXT_INPUT_STORE_SIZE 512
  19. typedef struct {
  20. SceneManager *scene_manager;
  21. ViewDispatcher *view_dispatcher;
  22. NotificationApp *notification;
  23. TextBox *chat_box;
  24. FuriString *chat_box_store;
  25. TextInput *text_input;
  26. char text_input_store[TEXT_INPUT_STORE_SIZE + 1];
  27. FuriString *name_prefix;
  28. FuriString *msg_input;
  29. bool encrypted;
  30. uint32_t frequency;
  31. gcm_context gcm_ctx;
  32. uint8_t rx_buffer[RX_TX_BUFFER_SIZE];
  33. uint8_t tx_buffer[RX_TX_BUFFER_SIZE];
  34. char rx_str_buffer[RX_TX_BUFFER_SIZE + 1];
  35. } ESubGhzChatState;
  36. typedef enum {
  37. ESubGhzChatScene_FreqInput,
  38. ESubGhzChatScene_PassInput,
  39. ESubGhzChatScene_ChatInput,
  40. ESubGhzChatScene_ChatBox,
  41. ESubGhzChatScene_MAX
  42. } ESubGhzChatScene;
  43. typedef enum {
  44. ESubGhzChatView_Input,
  45. ESubGhzChatView_ChatBox,
  46. } ESubGhzChatView;
  47. typedef enum {
  48. ESubGhzChatEvent_FreqEntered,
  49. ESubGhzChatEvent_PassEntered,
  50. ESubGhzChatEvent_MsgEntered
  51. } ESubGhzChatEvent;
  52. static void esubghz_chat_explicit_bzero(void *s, size_t len)
  53. {
  54. memset(s, 0, len);
  55. asm volatile("" ::: "memory");
  56. }
  57. static void post_rx(ESubGhzChatState *state, size_t rx_size)
  58. {
  59. furi_assert(state);
  60. if (rx_size == 0) {
  61. return;
  62. }
  63. furi_check(rx_size <= RX_TX_BUFFER_SIZE);
  64. if (!state->encrypted) {
  65. memcpy(state->rx_str_buffer, state->rx_buffer, rx_size);
  66. state->rx_str_buffer[rx_size] = 0;
  67. } else {
  68. if (rx_size < IV_BYTES + TAG_BYTES + 1) {
  69. return;
  70. }
  71. int ret = gcm_auth_decrypt(&(state->gcm_ctx),
  72. state->rx_buffer, IV_BYTES,
  73. NULL, 0,
  74. state->rx_buffer + IV_BYTES,
  75. (uint8_t *) state->rx_str_buffer,
  76. rx_size - (IV_BYTES + TAG_BYTES),
  77. state->rx_buffer + rx_size - TAG_BYTES,
  78. TAG_BYTES);
  79. state->rx_str_buffer[rx_size - (IV_BYTES + TAG_BYTES)] = 0;
  80. if (ret != 0) {
  81. strcpy(state->rx_str_buffer, "ERR: Decryption failed!");
  82. }
  83. }
  84. furi_string_cat_printf(state->chat_box_store, "\n%s",
  85. state->rx_str_buffer);
  86. notification_message(state->notification, &sequence_single_vibro);
  87. }
  88. static void freq_input_cb(void *context)
  89. {
  90. furi_assert(context);
  91. ESubGhzChatState* state = context;
  92. furi_string_cat_printf(state->chat_box_store, "Frequency: %lu",
  93. state->frequency);
  94. scene_manager_handle_custom_event(state->scene_manager,
  95. ESubGhzChatEvent_FreqEntered);
  96. }
  97. static bool freq_input_validator(const char *text, FuriString *error,
  98. void *context)
  99. {
  100. furi_assert(text);
  101. furi_assert(error);
  102. furi_assert(context);
  103. ESubGhzChatState* state = context;
  104. int ret = sscanf(text, "%lu", &(state->frequency));
  105. if (ret != 1) {
  106. furi_string_printf(error, "Please enter\nfrequency\nin Hz!");
  107. return false;
  108. }
  109. if (!furi_hal_subghz_is_frequency_valid(state->frequency)) {
  110. furi_string_printf(error, "Frequency\n%lu\n is invalid!",
  111. state->frequency);
  112. return false;
  113. }
  114. if (!furi_hal_subghz_is_tx_allowed(state->frequency)) {
  115. furi_string_printf(error, "TX forbidden\non frequency\n%lu!",
  116. state->frequency);
  117. return false;
  118. }
  119. return true;
  120. }
  121. static void pass_input_cb(void *context)
  122. {
  123. furi_assert(context);
  124. ESubGhzChatState* state = context;
  125. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  126. (state->encrypted ? "true" : "false"));
  127. scene_manager_handle_custom_event(state->scene_manager,
  128. ESubGhzChatEvent_PassEntered);
  129. }
  130. static bool pass_input_validator(const char *text, FuriString *error,
  131. void *context)
  132. {
  133. furi_assert(text);
  134. furi_assert(error);
  135. furi_assert(context);
  136. ESubGhzChatState* state = context;
  137. if (strlen(text) == 0) {
  138. state->encrypted = false;
  139. return true;
  140. }
  141. unsigned char key[KEY_BITS / 8];
  142. state->encrypted = true;
  143. sha256((unsigned char *) text, strlen(text), key);
  144. // TODO: remove this
  145. furi_string_cat_printf(state->chat_box_store, "\nKey:");
  146. int i;
  147. for (i = 0; i < KEY_BITS / 8; i++) {
  148. furi_string_cat_printf(state->chat_box_store, " %02x", key[i]);
  149. }
  150. int ret = gcm_setkey(&(state->gcm_ctx), key, KEY_BITS / 8);
  151. esubghz_chat_explicit_bzero(key, sizeof(key));
  152. if (ret != 0) {
  153. gcm_zero_ctx(&(state->gcm_ctx));
  154. furi_string_printf(error, "Failed to\nset key!");
  155. return false;
  156. }
  157. return true;
  158. }
  159. static void chat_input_cb(void *context)
  160. {
  161. furi_assert(context);
  162. ESubGhzChatState* state = context;
  163. if (strlen(state->text_input_store) == 0) {
  164. scene_manager_handle_custom_event(state->scene_manager,
  165. ESubGhzChatEvent_MsgEntered);
  166. return;
  167. }
  168. furi_string_set(state->msg_input, state->name_prefix);
  169. furi_string_cat_str(state->msg_input, state->text_input_store);
  170. furi_string_cat_printf(state->chat_box_store, "\n%s",
  171. furi_string_get_cstr(state->msg_input));
  172. size_t msg_len = strlen(furi_string_get_cstr(state->msg_input));
  173. size_t tx_size = msg_len;
  174. if (state->encrypted) {
  175. tx_size += IV_BYTES + TAG_BYTES;
  176. furi_check(tx_size <= sizeof(state->tx_buffer));
  177. furi_hal_random_fill_buf(state->tx_buffer, IV_BYTES);
  178. gcm_crypt_and_tag(&(state->gcm_ctx), ENCRYPT,
  179. state->tx_buffer, IV_BYTES,
  180. NULL, 0,
  181. (unsigned char *)
  182. furi_string_get_cstr(state->msg_input),
  183. state->tx_buffer + IV_BYTES,
  184. msg_len,
  185. state->tx_buffer + IV_BYTES + msg_len,
  186. TAG_BYTES);
  187. } else {
  188. furi_check(tx_size <= sizeof(state->tx_buffer));
  189. memcpy(state->tx_buffer,
  190. furi_string_get_cstr(state->msg_input),
  191. tx_size);
  192. }
  193. furi_string_set_char(state->msg_input, 0, 0);
  194. // TODO: remove this
  195. furi_string_cat_printf(state->chat_box_store, "\nTXed (HEX):");
  196. size_t i;
  197. for (i = 0; i < tx_size; i++) {
  198. furi_string_cat_printf(state->chat_box_store, " %02x",
  199. state->tx_buffer[i]);
  200. }
  201. // TODO: remove this
  202. memcpy(state->rx_buffer, state->tx_buffer, tx_size);
  203. post_rx(state, tx_size);
  204. // TODO: actually transmit
  205. scene_manager_handle_custom_event(state->scene_manager,
  206. ESubGhzChatEvent_MsgEntered);
  207. }
  208. static void scene_on_enter_freq_input(void* context)
  209. {
  210. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_freq_input");
  211. furi_assert(context);
  212. ESubGhzChatState* state = context;
  213. snprintf(state->text_input_store, TEXT_INPUT_STORE_SIZE, "%lu",
  214. (uint32_t) DEFAULT_FREQ);
  215. text_input_reset(state->text_input);
  216. text_input_set_result_callback(
  217. state->text_input,
  218. freq_input_cb,
  219. state,
  220. state->text_input_store,
  221. sizeof(state->text_input_store),
  222. true);
  223. text_input_set_validator(
  224. state->text_input,
  225. freq_input_validator,
  226. state);
  227. text_input_set_header_text(
  228. state->text_input,
  229. "Frequency");
  230. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  231. }
  232. static bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
  233. {
  234. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_freq_input");
  235. furi_assert(context);
  236. ESubGhzChatState* state = context;
  237. bool consumed = false;
  238. switch(event.type) {
  239. case SceneManagerEventTypeCustom:
  240. switch(event.event) {
  241. case ESubGhzChatEvent_FreqEntered:
  242. scene_manager_next_scene(state->scene_manager,
  243. ESubGhzChatScene_PassInput);
  244. consumed = true;
  245. break;
  246. }
  247. break;
  248. case SceneManagerEventTypeBack:
  249. view_dispatcher_stop(state->view_dispatcher);
  250. consumed = true;
  251. break;
  252. default:
  253. consumed = false;
  254. break;
  255. }
  256. return consumed;
  257. }
  258. static void scene_on_exit_freq_input(void* context)
  259. {
  260. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_freq_input");
  261. furi_assert(context);
  262. ESubGhzChatState* state = context;
  263. text_input_reset(state->text_input);
  264. }
  265. static void scene_on_enter_pass_input(void* context)
  266. {
  267. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  268. furi_assert(context);
  269. ESubGhzChatState* state = context;
  270. state->text_input_store[0] = 0;
  271. text_input_reset(state->text_input);
  272. text_input_set_result_callback(
  273. state->text_input,
  274. pass_input_cb,
  275. state,
  276. state->text_input_store,
  277. sizeof(state->text_input_store),
  278. true);
  279. text_input_set_validator(
  280. state->text_input,
  281. pass_input_validator,
  282. state);
  283. text_input_set_header_text(
  284. state->text_input,
  285. "Password (empty for no encr.)");
  286. text_input_set_minimum_length(state->text_input, 0);
  287. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  288. }
  289. static bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  290. {
  291. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  292. furi_assert(context);
  293. ESubGhzChatState* state = context;
  294. bool consumed = false;
  295. switch(event.type) {
  296. case SceneManagerEventTypeCustom:
  297. switch(event.event) {
  298. case ESubGhzChatEvent_PassEntered:
  299. scene_manager_next_scene(state->scene_manager,
  300. ESubGhzChatScene_ChatInput);
  301. consumed = true;
  302. break;
  303. }
  304. break;
  305. case SceneManagerEventTypeBack:
  306. view_dispatcher_stop(state->view_dispatcher);
  307. consumed = true;
  308. break;
  309. default:
  310. consumed = false;
  311. break;
  312. }
  313. return consumed;
  314. }
  315. static void scene_on_exit_pass_input(void* context)
  316. {
  317. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  318. furi_assert(context);
  319. ESubGhzChatState* state = context;
  320. text_input_reset(state->text_input);
  321. }
  322. static void scene_on_enter_chat_input(void* context)
  323. {
  324. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  325. furi_assert(context);
  326. ESubGhzChatState* state = context;
  327. state->text_input_store[0] = 0;
  328. text_input_reset(state->text_input);
  329. text_input_set_result_callback(
  330. state->text_input,
  331. chat_input_cb,
  332. state,
  333. state->text_input_store,
  334. sizeof(state->text_input_store),
  335. true);
  336. text_input_set_validator(
  337. state->text_input,
  338. NULL,
  339. NULL);
  340. text_input_set_header_text(
  341. state->text_input,
  342. "Message");
  343. text_input_set_minimum_length(state->text_input, 0);
  344. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  345. }
  346. static bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  347. {
  348. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  349. furi_assert(context);
  350. ESubGhzChatState* state = context;
  351. bool consumed = false;
  352. switch(event.type) {
  353. case SceneManagerEventTypeCustom:
  354. switch(event.event) {
  355. case ESubGhzChatEvent_MsgEntered:
  356. scene_manager_next_scene(state->scene_manager,
  357. ESubGhzChatScene_ChatBox);
  358. consumed = true;
  359. break;
  360. }
  361. break;
  362. case SceneManagerEventTypeBack:
  363. view_dispatcher_stop(state->view_dispatcher);
  364. consumed = true;
  365. break;
  366. default:
  367. consumed = false;
  368. break;
  369. }
  370. return consumed;
  371. }
  372. static void scene_on_exit_chat_input(void* context)
  373. {
  374. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  375. furi_assert(context);
  376. ESubGhzChatState* state = context;
  377. text_input_reset(state->text_input);
  378. }
  379. static void scene_on_enter_chat_box(void* context)
  380. {
  381. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_box");
  382. furi_assert(context);
  383. ESubGhzChatState* state = context;
  384. text_box_reset(state->chat_box);
  385. text_box_set_text(state->chat_box,
  386. furi_string_get_cstr(state->chat_box_store));
  387. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  388. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  389. }
  390. static bool scene_on_event_chat_box(void* context, SceneManagerEvent event)
  391. {
  392. UNUSED(event);
  393. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_box");
  394. furi_assert(context);
  395. // TODO
  396. return false;
  397. }
  398. static void scene_on_exit_chat_box(void* context)
  399. {
  400. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_box");
  401. furi_assert(context);
  402. ESubGhzChatState* state = context;
  403. text_box_reset(state->chat_box);
  404. }
  405. static void (*const esubghz_chat_scene_on_enter_handlers[])(void*) = {
  406. scene_on_enter_freq_input,
  407. scene_on_enter_pass_input,
  408. scene_on_enter_chat_input,
  409. scene_on_enter_chat_box
  410. };
  411. static bool (*const esubghz_chat_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  412. scene_on_event_freq_input,
  413. scene_on_event_pass_input,
  414. scene_on_event_chat_input,
  415. scene_on_event_chat_box
  416. };
  417. static void (*const esubghz_chat_scene_on_exit_handlers[])(void*) = {
  418. scene_on_exit_freq_input,
  419. scene_on_exit_pass_input,
  420. scene_on_exit_chat_input,
  421. scene_on_exit_chat_box
  422. };
  423. static const SceneManagerHandlers esubghz_chat_scene_event_handlers = {
  424. .on_enter_handlers = esubghz_chat_scene_on_enter_handlers,
  425. .on_event_handlers = esubghz_chat_scene_on_event_handlers,
  426. .on_exit_handlers = esubghz_chat_scene_on_exit_handlers,
  427. .scene_num = ESubGhzChatScene_MAX};
  428. static bool esubghz_chat_custom_event_callback(void* context, uint32_t event)
  429. {
  430. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_custom_event_callback");
  431. furi_assert(context);
  432. ESubGhzChatState* state = context;
  433. return scene_manager_handle_custom_event(state->scene_manager, event);
  434. }
  435. static bool esubghz_chat_navigation_event_callback(void* context)
  436. {
  437. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_navigation_event_callback");
  438. furi_assert(context);
  439. ESubGhzChatState* state = context;
  440. return scene_manager_handle_back_event(state->scene_manager);
  441. }
  442. static bool helper_strings_alloc(ESubGhzChatState *state)
  443. {
  444. furi_assert(state);
  445. state->name_prefix = furi_string_alloc();
  446. if (state->name_prefix == NULL) {
  447. return false;
  448. }
  449. state->msg_input = furi_string_alloc();
  450. if (state->msg_input == NULL) {
  451. furi_string_free(state->name_prefix);
  452. return false;
  453. }
  454. return true;
  455. }
  456. static void helper_strings_free(ESubGhzChatState *state)
  457. {
  458. furi_assert(state);
  459. furi_string_free(state->name_prefix);
  460. furi_string_free(state->msg_input);
  461. }
  462. static bool chat_box_alloc(ESubGhzChatState *state)
  463. {
  464. furi_assert(state);
  465. state->chat_box = text_box_alloc();
  466. if (state->chat_box == NULL) {
  467. return false;
  468. }
  469. state->chat_box_store = furi_string_alloc();
  470. if (state->chat_box_store == NULL) {
  471. text_box_free(state->chat_box);
  472. return false;
  473. }
  474. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  475. furi_string_set_char(state->chat_box_store, 0, 0);
  476. text_box_set_text(state->chat_box,
  477. furi_string_get_cstr(state->chat_box_store));
  478. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  479. return true;
  480. }
  481. static void chat_box_free(ESubGhzChatState *state)
  482. {
  483. furi_assert(state);
  484. text_box_free(state->chat_box);
  485. furi_string_free(state->chat_box_store);
  486. }
  487. int32_t esubghz_chat(void)
  488. {
  489. gcm_initialize();
  490. int32_t err = -1;
  491. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  492. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  493. if (state == NULL) {
  494. goto err_alloc;
  495. }
  496. memset(state, 0, sizeof(*state));
  497. state->scene_manager = scene_manager_alloc(
  498. &esubghz_chat_scene_event_handlers, state);
  499. if (state->scene_manager == NULL) {
  500. goto err_alloc_sm;
  501. }
  502. state->view_dispatcher = view_dispatcher_alloc();
  503. if (state->view_dispatcher == NULL) {
  504. goto err_alloc_vd;
  505. }
  506. if (!helper_strings_alloc(state)) {
  507. goto err_alloc_hs;
  508. }
  509. state->text_input = text_input_alloc();
  510. if (state->text_input == NULL) {
  511. goto err_alloc_ti;
  512. }
  513. if (!chat_box_alloc(state)) {
  514. goto err_alloc_cb;
  515. }
  516. // set chat name prefix
  517. // TODO: handle escape chars here somehow
  518. furi_string_printf(state->name_prefix, "\033[0;33m%s\033[0m: ",
  519. furi_hal_version_get_name_ptr());
  520. view_dispatcher_enable_queue(state->view_dispatcher);
  521. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  522. view_dispatcher_set_custom_event_callback(
  523. state->view_dispatcher,
  524. esubghz_chat_custom_event_callback);
  525. view_dispatcher_set_navigation_event_callback(
  526. state->view_dispatcher,
  527. esubghz_chat_navigation_event_callback);
  528. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  529. text_input_get_view(state->text_input));
  530. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  531. text_box_get_view(state->chat_box));
  532. /* no error handling here, don't know how */
  533. Gui *gui = furi_record_open(RECORD_GUI);
  534. view_dispatcher_attach_to_gui(state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  535. /* no error handling here, don't know how */
  536. state->notification = furi_record_open(RECORD_NOTIFICATION);
  537. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  538. view_dispatcher_run(state->view_dispatcher);
  539. err = 0;
  540. furi_record_close(RECORD_NOTIFICATION);
  541. furi_record_close(RECORD_GUI);
  542. view_dispatcher_remove_view(state->view_dispatcher, ESubGhzChatView_Input);
  543. view_dispatcher_remove_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  544. // clear the key and potential password
  545. esubghz_chat_explicit_bzero(state->text_input_store,
  546. sizeof(state->text_input_store));
  547. gcm_zero_ctx(&(state->gcm_ctx));
  548. chat_box_free(state);
  549. err_alloc_cb:
  550. text_input_free(state->text_input);
  551. err_alloc_ti:
  552. helper_strings_free(state);
  553. err_alloc_hs:
  554. view_dispatcher_free(state->view_dispatcher);
  555. err_alloc_vd:
  556. scene_manager_free(state->scene_manager);
  557. err_alloc_sm:
  558. free(state);
  559. err_alloc:
  560. if (err != 0) {
  561. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  562. } else {
  563. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  564. }
  565. return err;
  566. }