esubghz_chat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/elements.h>
  4. #include <gui/gui.h>
  5. #include <gui/modules/text_box.h>
  6. #include <gui/modules/text_input.h>
  7. #include <gui/view_dispatcher_i.h>
  8. #include <gui/view_port_i.h>
  9. #include <gui/scene_manager.h>
  10. #include <toolbox/sha256.h>
  11. #include <notification/notification_messages.h>
  12. #include "esubghz_chat_icons.h"
  13. #include "crypto/gcm.h"
  14. #define APPLICATION_NAME "ESubGhzChat"
  15. #define DEFAULT_FREQ 433920000
  16. #define KEY_BITS 256
  17. #define IV_BYTES 12
  18. #define TAG_BYTES 16
  19. #define RX_TX_BUFFER_SIZE 1024
  20. #define CHAT_BOX_STORE_SIZE 4096
  21. #define TEXT_INPUT_STORE_SIZE 512
  22. #define TICK_INTERVAL 50
  23. #define MESSAGE_COMPLETION_TIMEOUT 200
  24. #define TIMEOUT_BETWEEN_MESSAGES 500
  25. #define KBD_UNLOCK_CNT 3
  26. #define KBD_UNLOCK_TIMEOUT 1000
  27. typedef struct {
  28. SceneManager *scene_manager;
  29. ViewDispatcher *view_dispatcher;
  30. NotificationApp *notification;
  31. TextBox *chat_box;
  32. FuriString *chat_box_store;
  33. TextInput *text_input;
  34. char text_input_store[TEXT_INPUT_STORE_SIZE + 1];
  35. FuriString *name_prefix;
  36. FuriString *msg_input;
  37. bool encrypted;
  38. uint32_t frequency;
  39. gcm_context gcm_ctx;
  40. uint8_t rx_buffer[RX_TX_BUFFER_SIZE];
  41. uint8_t tx_buffer[RX_TX_BUFFER_SIZE];
  42. char rx_str_buffer[RX_TX_BUFFER_SIZE + 1];
  43. FuriStreamBuffer *rx_collection_buffer;
  44. uint32_t last_time_rx_data;
  45. // for locking
  46. ViewPortDrawCallback orig_draw_cb;
  47. ViewPortInputCallback orig_input_cb;
  48. bool kbd_locked;
  49. uint32_t kbd_lock_msg_ticks;
  50. uint8_t kbd_lock_count;
  51. } ESubGhzChatState;
  52. typedef enum {
  53. ESubGhzChatScene_FreqInput,
  54. ESubGhzChatScene_PassInput,
  55. ESubGhzChatScene_ChatInput,
  56. ESubGhzChatScene_ChatBox,
  57. ESubGhzChatScene_MAX
  58. } ESubGhzChatScene;
  59. typedef enum {
  60. ESubGhzChatView_Input,
  61. ESubGhzChatView_ChatBox,
  62. } ESubGhzChatView;
  63. typedef enum {
  64. ESubGhzChatEvent_FreqEntered,
  65. ESubGhzChatEvent_PassEntered,
  66. ESubGhzChatEvent_MsgEntered
  67. } ESubGhzChatEvent;
  68. static void esubghz_chat_explicit_bzero(void *s, size_t len)
  69. {
  70. memset(s, 0, len);
  71. asm volatile("" ::: "memory");
  72. }
  73. static void post_rx(ESubGhzChatState *state, size_t rx_size)
  74. {
  75. furi_assert(state);
  76. if (rx_size == 0) {
  77. return;
  78. }
  79. furi_check(rx_size <= RX_TX_BUFFER_SIZE);
  80. if (!state->encrypted) {
  81. memcpy(state->rx_str_buffer, state->rx_buffer, rx_size);
  82. state->rx_str_buffer[rx_size] = 0;
  83. } else {
  84. if (rx_size < IV_BYTES + TAG_BYTES + 1) {
  85. return;
  86. }
  87. int ret = gcm_auth_decrypt(&(state->gcm_ctx),
  88. state->rx_buffer, IV_BYTES,
  89. NULL, 0,
  90. state->rx_buffer + IV_BYTES,
  91. (uint8_t *) state->rx_str_buffer,
  92. rx_size - (IV_BYTES + TAG_BYTES),
  93. state->rx_buffer + rx_size - TAG_BYTES,
  94. TAG_BYTES);
  95. state->rx_str_buffer[rx_size - (IV_BYTES + TAG_BYTES)] = 0;
  96. if (ret != 0) {
  97. strcpy(state->rx_str_buffer, "ERR: Decryption failed!");
  98. }
  99. }
  100. furi_string_cat_printf(state->chat_box_store, "\n%s",
  101. state->rx_str_buffer);
  102. notification_message(state->notification, &sequence_single_vibro);
  103. // Reset Text Box contents and focus
  104. text_box_set_text(state->chat_box,
  105. furi_string_get_cstr(state->chat_box_store));
  106. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  107. }
  108. static void freq_input_cb(void *context)
  109. {
  110. furi_assert(context);
  111. ESubGhzChatState* state = context;
  112. furi_string_cat_printf(state->chat_box_store, "Frequency: %lu",
  113. state->frequency);
  114. scene_manager_handle_custom_event(state->scene_manager,
  115. ESubGhzChatEvent_FreqEntered);
  116. }
  117. static bool freq_input_validator(const char *text, FuriString *error,
  118. void *context)
  119. {
  120. furi_assert(text);
  121. furi_assert(error);
  122. furi_assert(context);
  123. ESubGhzChatState* state = context;
  124. int ret = sscanf(text, "%lu", &(state->frequency));
  125. if (ret != 1) {
  126. furi_string_printf(error, "Please enter\nfrequency\nin Hz!");
  127. return false;
  128. }
  129. if (!furi_hal_subghz_is_frequency_valid(state->frequency)) {
  130. furi_string_printf(error, "Frequency\n%lu\n is invalid!",
  131. state->frequency);
  132. return false;
  133. }
  134. if (!furi_hal_subghz_is_tx_allowed(state->frequency)) {
  135. furi_string_printf(error, "TX forbidden\non frequency\n%lu!",
  136. state->frequency);
  137. return false;
  138. }
  139. return true;
  140. }
  141. static void pass_input_cb(void *context)
  142. {
  143. furi_assert(context);
  144. ESubGhzChatState* state = context;
  145. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  146. (state->encrypted ? "true" : "false"));
  147. scene_manager_handle_custom_event(state->scene_manager,
  148. ESubGhzChatEvent_PassEntered);
  149. }
  150. static bool pass_input_validator(const char *text, FuriString *error,
  151. void *context)
  152. {
  153. furi_assert(text);
  154. furi_assert(error);
  155. furi_assert(context);
  156. ESubGhzChatState* state = context;
  157. if (strlen(text) == 0) {
  158. state->encrypted = false;
  159. return true;
  160. }
  161. unsigned char key[KEY_BITS / 8];
  162. state->encrypted = true;
  163. sha256((unsigned char *) text, strlen(text), key);
  164. // TODO: remove this
  165. furi_string_cat_printf(state->chat_box_store, "\nKey:");
  166. int i;
  167. for (i = 0; i < KEY_BITS / 8; i++) {
  168. furi_string_cat_printf(state->chat_box_store, " %02x", key[i]);
  169. }
  170. int ret = gcm_setkey(&(state->gcm_ctx), key, KEY_BITS / 8);
  171. esubghz_chat_explicit_bzero(key, sizeof(key));
  172. if (ret != 0) {
  173. gcm_zero_ctx(&(state->gcm_ctx));
  174. furi_string_printf(error, "Failed to\nset key!");
  175. return false;
  176. }
  177. return true;
  178. }
  179. static void chat_input_cb(void *context)
  180. {
  181. furi_assert(context);
  182. ESubGhzChatState* state = context;
  183. if (strlen(state->text_input_store) == 0) {
  184. scene_manager_handle_custom_event(state->scene_manager,
  185. ESubGhzChatEvent_MsgEntered);
  186. return;
  187. }
  188. furi_string_set(state->msg_input, state->name_prefix);
  189. furi_string_cat_str(state->msg_input, state->text_input_store);
  190. furi_string_cat_printf(state->chat_box_store, "\n%s",
  191. furi_string_get_cstr(state->msg_input));
  192. size_t msg_len = strlen(furi_string_get_cstr(state->msg_input));
  193. size_t tx_size = msg_len;
  194. if (state->encrypted) {
  195. tx_size += IV_BYTES + TAG_BYTES;
  196. furi_check(tx_size <= sizeof(state->tx_buffer));
  197. furi_hal_random_fill_buf(state->tx_buffer, IV_BYTES);
  198. gcm_crypt_and_tag(&(state->gcm_ctx), ENCRYPT,
  199. state->tx_buffer, IV_BYTES,
  200. NULL, 0,
  201. (unsigned char *)
  202. furi_string_get_cstr(state->msg_input),
  203. state->tx_buffer + IV_BYTES,
  204. msg_len,
  205. state->tx_buffer + IV_BYTES + msg_len,
  206. TAG_BYTES);
  207. } else {
  208. furi_check(tx_size <= sizeof(state->tx_buffer));
  209. memcpy(state->tx_buffer,
  210. furi_string_get_cstr(state->msg_input),
  211. tx_size);
  212. }
  213. furi_string_set_char(state->msg_input, 0, 0);
  214. // TODO: remove this
  215. furi_string_cat_printf(state->chat_box_store, "\nTXed (HEX):");
  216. size_t i;
  217. for (i = 0; i < tx_size; i++) {
  218. furi_string_cat_printf(state->chat_box_store, " %02x",
  219. state->tx_buffer[i]);
  220. }
  221. // TODO: remove this
  222. state->last_time_rx_data = furi_get_tick();
  223. furi_stream_buffer_send(state->rx_collection_buffer,
  224. state->tx_buffer, tx_size, 0);
  225. // TODO: actually transmit
  226. scene_manager_handle_custom_event(state->scene_manager,
  227. ESubGhzChatEvent_MsgEntered);
  228. }
  229. static void scene_on_enter_freq_input(void* context)
  230. {
  231. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_freq_input");
  232. furi_assert(context);
  233. ESubGhzChatState* state = context;
  234. snprintf(state->text_input_store, TEXT_INPUT_STORE_SIZE, "%lu",
  235. (uint32_t) DEFAULT_FREQ);
  236. text_input_reset(state->text_input);
  237. text_input_set_result_callback(
  238. state->text_input,
  239. freq_input_cb,
  240. state,
  241. state->text_input_store,
  242. sizeof(state->text_input_store),
  243. true);
  244. text_input_set_validator(
  245. state->text_input,
  246. freq_input_validator,
  247. state);
  248. text_input_set_header_text(
  249. state->text_input,
  250. "Frequency");
  251. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  252. }
  253. static bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
  254. {
  255. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_freq_input");
  256. furi_assert(context);
  257. ESubGhzChatState* state = context;
  258. bool consumed = false;
  259. switch(event.type) {
  260. case SceneManagerEventTypeCustom:
  261. switch(event.event) {
  262. case ESubGhzChatEvent_FreqEntered:
  263. scene_manager_next_scene(state->scene_manager,
  264. ESubGhzChatScene_PassInput);
  265. consumed = true;
  266. break;
  267. }
  268. break;
  269. case SceneManagerEventTypeBack:
  270. view_dispatcher_stop(state->view_dispatcher);
  271. consumed = true;
  272. break;
  273. default:
  274. consumed = false;
  275. break;
  276. }
  277. return consumed;
  278. }
  279. static void scene_on_exit_freq_input(void* context)
  280. {
  281. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_freq_input");
  282. furi_assert(context);
  283. ESubGhzChatState* state = context;
  284. text_input_reset(state->text_input);
  285. }
  286. static void scene_on_enter_pass_input(void* context)
  287. {
  288. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  289. furi_assert(context);
  290. ESubGhzChatState* state = context;
  291. state->text_input_store[0] = 0;
  292. text_input_reset(state->text_input);
  293. text_input_set_result_callback(
  294. state->text_input,
  295. pass_input_cb,
  296. state,
  297. state->text_input_store,
  298. sizeof(state->text_input_store),
  299. true);
  300. text_input_set_validator(
  301. state->text_input,
  302. pass_input_validator,
  303. state);
  304. text_input_set_header_text(
  305. state->text_input,
  306. "Password (empty for no encr.)");
  307. text_input_set_minimum_length(state->text_input, 0);
  308. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  309. }
  310. static bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  311. {
  312. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  313. furi_assert(context);
  314. ESubGhzChatState* state = context;
  315. bool consumed = false;
  316. switch(event.type) {
  317. case SceneManagerEventTypeCustom:
  318. switch(event.event) {
  319. case ESubGhzChatEvent_PassEntered:
  320. scene_manager_next_scene(state->scene_manager,
  321. ESubGhzChatScene_ChatInput);
  322. consumed = true;
  323. break;
  324. }
  325. break;
  326. case SceneManagerEventTypeBack:
  327. view_dispatcher_stop(state->view_dispatcher);
  328. consumed = true;
  329. break;
  330. default:
  331. consumed = false;
  332. break;
  333. }
  334. return consumed;
  335. }
  336. static void scene_on_exit_pass_input(void* context)
  337. {
  338. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  339. furi_assert(context);
  340. ESubGhzChatState* state = context;
  341. text_input_reset(state->text_input);
  342. }
  343. static void scene_on_enter_chat_input(void* context)
  344. {
  345. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  346. furi_assert(context);
  347. ESubGhzChatState* state = context;
  348. state->text_input_store[0] = 0;
  349. text_input_reset(state->text_input);
  350. text_input_set_result_callback(
  351. state->text_input,
  352. chat_input_cb,
  353. state,
  354. state->text_input_store,
  355. sizeof(state->text_input_store),
  356. true);
  357. text_input_set_validator(
  358. state->text_input,
  359. NULL,
  360. NULL);
  361. text_input_set_header_text(
  362. state->text_input,
  363. "Message");
  364. text_input_set_minimum_length(state->text_input, 0);
  365. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  366. }
  367. static bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  368. {
  369. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  370. furi_assert(context);
  371. ESubGhzChatState* state = context;
  372. bool consumed = false;
  373. switch(event.type) {
  374. case SceneManagerEventTypeCustom:
  375. switch(event.event) {
  376. case ESubGhzChatEvent_MsgEntered:
  377. scene_manager_next_scene(state->scene_manager,
  378. ESubGhzChatScene_ChatBox);
  379. consumed = true;
  380. break;
  381. }
  382. break;
  383. case SceneManagerEventTypeBack:
  384. view_dispatcher_stop(state->view_dispatcher);
  385. consumed = true;
  386. break;
  387. default:
  388. consumed = false;
  389. break;
  390. }
  391. return consumed;
  392. }
  393. static void scene_on_exit_chat_input(void* context)
  394. {
  395. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  396. furi_assert(context);
  397. ESubGhzChatState* state = context;
  398. text_input_reset(state->text_input);
  399. }
  400. static void scene_on_enter_chat_box(void* context)
  401. {
  402. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_box");
  403. furi_assert(context);
  404. ESubGhzChatState* state = context;
  405. text_box_reset(state->chat_box);
  406. text_box_set_text(state->chat_box,
  407. furi_string_get_cstr(state->chat_box_store));
  408. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  409. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  410. }
  411. static bool scene_on_event_chat_box(void* context, SceneManagerEvent event)
  412. {
  413. UNUSED(event);
  414. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_box");
  415. furi_assert(context);
  416. return false;
  417. }
  418. static void scene_on_exit_chat_box(void* context)
  419. {
  420. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_box");
  421. furi_assert(context);
  422. ESubGhzChatState* state = context;
  423. text_box_reset(state->chat_box);
  424. }
  425. static void (*const esubghz_chat_scene_on_enter_handlers[])(void*) = {
  426. scene_on_enter_freq_input,
  427. scene_on_enter_pass_input,
  428. scene_on_enter_chat_input,
  429. scene_on_enter_chat_box
  430. };
  431. static bool (*const esubghz_chat_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  432. scene_on_event_freq_input,
  433. scene_on_event_pass_input,
  434. scene_on_event_chat_input,
  435. scene_on_event_chat_box
  436. };
  437. static void (*const esubghz_chat_scene_on_exit_handlers[])(void*) = {
  438. scene_on_exit_freq_input,
  439. scene_on_exit_pass_input,
  440. scene_on_exit_chat_input,
  441. scene_on_exit_chat_box
  442. };
  443. static const SceneManagerHandlers esubghz_chat_scene_event_handlers = {
  444. .on_enter_handlers = esubghz_chat_scene_on_enter_handlers,
  445. .on_event_handlers = esubghz_chat_scene_on_event_handlers,
  446. .on_exit_handlers = esubghz_chat_scene_on_exit_handlers,
  447. .scene_num = ESubGhzChatScene_MAX};
  448. static bool kbd_lock_msg_display(ESubGhzChatState *state)
  449. {
  450. return (state->kbd_lock_msg_ticks != 0);
  451. }
  452. static bool kbd_lock_msg_reset_timeout(ESubGhzChatState *state)
  453. {
  454. if (state->kbd_lock_msg_ticks == 0) {
  455. return false;
  456. }
  457. if (furi_get_tick() - state->kbd_lock_msg_ticks > KBD_UNLOCK_TIMEOUT) {
  458. return true;
  459. }
  460. return false;
  461. }
  462. static void kbd_lock_msg_reset(ESubGhzChatState *state, bool backlight_off)
  463. {
  464. state->kbd_lock_msg_ticks = 0;
  465. state->kbd_lock_count = 0;
  466. if (backlight_off) {
  467. notification_message(state->notification,
  468. &sequence_display_backlight_off);
  469. }
  470. }
  471. static void kbd_lock(ESubGhzChatState *state)
  472. {
  473. state->kbd_locked = true;
  474. kbd_lock_msg_reset(state, true);
  475. }
  476. static void kbd_unlock(ESubGhzChatState *state)
  477. {
  478. state->kbd_locked = false;
  479. kbd_lock_msg_reset(state, false);
  480. }
  481. static bool esubghz_chat_custom_event_callback(void* context, uint32_t event)
  482. {
  483. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_custom_event_callback");
  484. furi_assert(context);
  485. ESubGhzChatState* state = context;
  486. return scene_manager_handle_custom_event(state->scene_manager, event);
  487. }
  488. static bool esubghz_chat_navigation_event_callback(void* context)
  489. {
  490. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_navigation_event_callback");
  491. furi_assert(context);
  492. ESubGhzChatState* state = context;
  493. return scene_manager_handle_back_event(state->scene_manager);
  494. }
  495. static void esubghz_chat_tick_event_callback(void* context)
  496. {
  497. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_tick_event_callback");
  498. furi_assert(context);
  499. ESubGhzChatState* state = context;
  500. if (kbd_lock_msg_reset_timeout(state)) {
  501. kbd_lock_msg_reset(state, true);
  502. }
  503. size_t avail = furi_stream_buffer_bytes_available(
  504. state->rx_collection_buffer);
  505. if (avail > 0) {
  506. uint32_t since_last_rx = furi_get_tick() -
  507. state->last_time_rx_data;
  508. if (avail == RX_TX_BUFFER_SIZE || since_last_rx >
  509. MESSAGE_COMPLETION_TIMEOUT) {
  510. size_t rx_size = furi_stream_buffer_receive(
  511. state->rx_collection_buffer,
  512. state->rx_buffer,
  513. avail, 0);
  514. post_rx(state, rx_size);
  515. furi_stream_buffer_reset(state->rx_collection_buffer);
  516. }
  517. }
  518. scene_manager_handle_tick_event(state->scene_manager);
  519. }
  520. static void esubghz_hooked_draw_callback(Canvas* canvas, void* context)
  521. {
  522. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_draw_callback");
  523. furi_assert(context);
  524. ESubGhzChatState* state = context;
  525. state->orig_draw_cb(canvas, state->view_dispatcher);
  526. if (kbd_lock_msg_display(state)) {
  527. canvas_set_font(canvas, FontSecondary);
  528. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  529. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  530. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  531. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  532. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  533. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  534. }
  535. }
  536. static void esubghz_hooked_input_callback(InputEvent* event, void* context)
  537. {
  538. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_input_callback");
  539. furi_assert(context);
  540. ESubGhzChatState* state = context;
  541. if (state->kbd_locked) {
  542. if (state->kbd_lock_count == 0) {
  543. state->kbd_lock_msg_ticks = furi_get_tick();
  544. }
  545. if (event->key == InputKeyBack && event->type ==
  546. InputTypeShort) {
  547. state->kbd_lock_count++;
  548. }
  549. if (state->kbd_lock_count >= KBD_UNLOCK_CNT) {
  550. kbd_unlock(state);
  551. }
  552. // do not handle the event
  553. return;
  554. }
  555. // if we are in the chat view, allow locking
  556. if (state->view_dispatcher->current_view ==
  557. text_box_get_view(state->chat_box)) {
  558. if (event->key == InputKeyOk && event->type == InputTypeLong) {
  559. kbd_lock(state);
  560. return;
  561. }
  562. }
  563. state->orig_input_cb(event, state->view_dispatcher);
  564. }
  565. static bool helper_strings_alloc(ESubGhzChatState *state)
  566. {
  567. furi_assert(state);
  568. state->name_prefix = furi_string_alloc();
  569. if (state->name_prefix == NULL) {
  570. return false;
  571. }
  572. state->msg_input = furi_string_alloc();
  573. if (state->msg_input == NULL) {
  574. furi_string_free(state->name_prefix);
  575. return false;
  576. }
  577. return true;
  578. }
  579. static void helper_strings_free(ESubGhzChatState *state)
  580. {
  581. furi_assert(state);
  582. furi_string_free(state->name_prefix);
  583. furi_string_free(state->msg_input);
  584. }
  585. static bool chat_box_alloc(ESubGhzChatState *state)
  586. {
  587. furi_assert(state);
  588. state->chat_box = text_box_alloc();
  589. if (state->chat_box == NULL) {
  590. return false;
  591. }
  592. state->chat_box_store = furi_string_alloc();
  593. if (state->chat_box_store == NULL) {
  594. text_box_free(state->chat_box);
  595. return false;
  596. }
  597. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  598. furi_string_set_char(state->chat_box_store, 0, 0);
  599. text_box_set_text(state->chat_box,
  600. furi_string_get_cstr(state->chat_box_store));
  601. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  602. return true;
  603. }
  604. static void chat_box_free(ESubGhzChatState *state)
  605. {
  606. furi_assert(state);
  607. text_box_free(state->chat_box);
  608. furi_string_free(state->chat_box_store);
  609. }
  610. int32_t esubghz_chat(void)
  611. {
  612. gcm_initialize();
  613. int32_t err = -1;
  614. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  615. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  616. if (state == NULL) {
  617. goto err_alloc;
  618. }
  619. memset(state, 0, sizeof(*state));
  620. state->scene_manager = scene_manager_alloc(
  621. &esubghz_chat_scene_event_handlers, state);
  622. if (state->scene_manager == NULL) {
  623. goto err_alloc_sm;
  624. }
  625. state->view_dispatcher = view_dispatcher_alloc();
  626. if (state->view_dispatcher == NULL) {
  627. goto err_alloc_vd;
  628. }
  629. if (!helper_strings_alloc(state)) {
  630. goto err_alloc_hs;
  631. }
  632. state->text_input = text_input_alloc();
  633. if (state->text_input == NULL) {
  634. goto err_alloc_ti;
  635. }
  636. if (!chat_box_alloc(state)) {
  637. goto err_alloc_cb;
  638. }
  639. state->rx_collection_buffer = furi_stream_buffer_alloc(
  640. RX_TX_BUFFER_SIZE,
  641. RX_TX_BUFFER_SIZE);
  642. if (state->rx_collection_buffer == NULL) {
  643. goto err_alloc_rcb;
  644. }
  645. // set chat name prefix
  646. // TODO: handle escape chars here somehow
  647. furi_string_printf(state->name_prefix, "\033[0;33m%s\033[0m: ",
  648. furi_hal_version_get_name_ptr());
  649. /* no error handling here, don't know how */
  650. state->notification = furi_record_open(RECORD_NOTIFICATION);
  651. state->orig_draw_cb = state->view_dispatcher->view_port->draw_callback;
  652. state->orig_input_cb = state->view_dispatcher->view_port->input_callback;
  653. view_port_draw_callback_set(state->view_dispatcher->view_port,
  654. esubghz_hooked_draw_callback, state);
  655. view_port_input_callback_set(state->view_dispatcher->view_port,
  656. esubghz_hooked_input_callback, state);
  657. view_dispatcher_enable_queue(state->view_dispatcher);
  658. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  659. view_dispatcher_set_custom_event_callback(
  660. state->view_dispatcher,
  661. esubghz_chat_custom_event_callback);
  662. view_dispatcher_set_navigation_event_callback(
  663. state->view_dispatcher,
  664. esubghz_chat_navigation_event_callback);
  665. view_dispatcher_set_tick_event_callback(
  666. state->view_dispatcher,
  667. esubghz_chat_tick_event_callback,
  668. TICK_INTERVAL);
  669. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  670. text_input_get_view(state->text_input));
  671. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  672. text_box_get_view(state->chat_box));
  673. /* no error handling here, don't know how */
  674. Gui *gui = furi_record_open(RECORD_GUI);
  675. view_dispatcher_attach_to_gui(state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  676. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  677. view_dispatcher_run(state->view_dispatcher);
  678. err = 0;
  679. furi_record_close(RECORD_GUI);
  680. view_dispatcher_remove_view(state->view_dispatcher, ESubGhzChatView_Input);
  681. view_dispatcher_remove_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  682. furi_record_close(RECORD_NOTIFICATION);
  683. // clear the key and potential password
  684. esubghz_chat_explicit_bzero(state->text_input_store,
  685. sizeof(state->text_input_store));
  686. gcm_zero_ctx(&(state->gcm_ctx));
  687. furi_stream_buffer_free(state->rx_collection_buffer);
  688. err_alloc_rcb:
  689. chat_box_free(state);
  690. err_alloc_cb:
  691. text_input_free(state->text_input);
  692. err_alloc_ti:
  693. helper_strings_free(state);
  694. err_alloc_hs:
  695. view_dispatcher_free(state->view_dispatcher);
  696. err_alloc_vd:
  697. scene_manager_free(state->scene_manager);
  698. err_alloc_sm:
  699. free(state);
  700. err_alloc:
  701. if (err != 0) {
  702. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  703. } else {
  704. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  705. }
  706. return err;
  707. }