esubghz_chat.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include <furi_hal.h>
  2. #include <gui/elements.h>
  3. #include <gui/gui.h>
  4. #include <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  5. #include "esubghz_chat_icons.h"
  6. #include "esubghz_chat_i.h"
  7. #define CHAT_LEAVE_DELAY 10
  8. #define TICK_INTERVAL 50
  9. #define MESSAGE_COMPLETION_TIMEOUT 500
  10. #define TIMEOUT_BETWEEN_MESSAGES 500
  11. #define KBD_UNLOCK_CNT 3
  12. #define KBD_UNLOCK_TIMEOUT 1000
  13. /* Callback for RX events from the Sub-GHz worker. Records the current ticks as
  14. * the time of the last reception. */
  15. static void have_read_cb(void* context)
  16. {
  17. furi_assert(context);
  18. ESubGhzChatState* state = context;
  19. state->last_time_rx_data = furi_get_tick();
  20. }
  21. /* Decrypts a message for post_rx(). */
  22. static bool post_rx_decrypt(ESubGhzChatState *state, size_t rx_size)
  23. {
  24. bool ret = crypto_ctx_decrypt(state->crypto_ctx,
  25. state->rx_buffer, rx_size,
  26. (uint8_t*) state->rx_str_buffer);
  27. if (ret) {
  28. state->rx_str_buffer[rx_size - (MSG_OVERHEAD)] = 0;
  29. } else {
  30. state->rx_str_buffer[0] = 0;
  31. }
  32. return ret;
  33. }
  34. /* Post RX handler, decrypts received messages, displays them in the text box
  35. * and sends a notification. */
  36. static void post_rx(ESubGhzChatState *state, size_t rx_size)
  37. {
  38. furi_assert(state);
  39. if (rx_size == 0) {
  40. return;
  41. }
  42. furi_check(rx_size <= RX_TX_BUFFER_SIZE);
  43. /* decrypt if necessary */
  44. if (!state->encrypted) {
  45. memcpy(state->rx_str_buffer, state->rx_buffer, rx_size);
  46. state->rx_str_buffer[rx_size] = 0;
  47. /* remove trailing newline if it is there, for compat with CLI
  48. * Sub-GHz chat */
  49. if (state->rx_str_buffer[rx_size - 1] == '\n') {
  50. state->rx_str_buffer[rx_size - 1] = 0;
  51. }
  52. } else {
  53. /* if decryption fails output an error message */
  54. if (!post_rx_decrypt(state, rx_size)) {
  55. strcpy(state->rx_str_buffer, "ERR: Decryption failed!");
  56. }
  57. }
  58. /* append message to text box */
  59. furi_string_cat_printf(state->chat_box_store, "\n%s",
  60. state->rx_str_buffer);
  61. /* send notification (make the flipper vibrate) */
  62. notification_message(state->notification, &sequence_single_vibro);
  63. /* reset text box contents and focus */
  64. text_box_set_text(state->chat_box,
  65. furi_string_get_cstr(state->chat_box_store));
  66. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  67. }
  68. /* Reads the message from msg_input, encrypts it if necessary and then
  69. * transmits it. */
  70. void tx_msg_input(ESubGhzChatState *state)
  71. {
  72. /* encrypt message if necessary */
  73. size_t msg_len = strlen(furi_string_get_cstr(state->msg_input));
  74. size_t tx_size = msg_len;
  75. if (state->encrypted) {
  76. tx_size += MSG_OVERHEAD;
  77. furi_check(tx_size <= sizeof(state->tx_buffer));
  78. crypto_ctx_encrypt(state->crypto_ctx,
  79. (uint8_t *)
  80. furi_string_get_cstr(state->msg_input),
  81. msg_len,
  82. state->tx_buffer);
  83. } else {
  84. tx_size += 2;
  85. furi_check(tx_size <= sizeof(state->tx_buffer));
  86. memcpy(state->tx_buffer,
  87. furi_string_get_cstr(state->msg_input),
  88. msg_len);
  89. /* append \r\n for compat with Sub-GHz CLI chat */
  90. state->tx_buffer[msg_len] = '\r';
  91. state->tx_buffer[msg_len + 1] = '\n';
  92. }
  93. /* transmit */
  94. subghz_tx_rx_worker_write(state->subghz_worker, state->tx_buffer,
  95. tx_size);
  96. }
  97. /* Displays whether or not encryption has been enabled in the text box. Also
  98. * clears the text input buffer to remove the password and starts the Sub-GHz
  99. * worker. After starting the worker a join message is transmitted. */
  100. void enter_chat(ESubGhzChatState *state)
  101. {
  102. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  103. (state->encrypted ? "yes" : "no"));
  104. /* clear the text input buffer to remove a password or key */
  105. crypto_explicit_bzero(state->text_input_store,
  106. sizeof(state->text_input_store));
  107. subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
  108. state->frequency);
  109. /* concatenate the name prefix and join message */
  110. furi_string_set(state->msg_input, state->name_prefix);
  111. furi_string_cat_str(state->msg_input, " joined chat.");
  112. /* encrypt and transmit message */
  113. tx_msg_input(state);
  114. /* clear message input buffer */
  115. furi_string_set_char(state->msg_input, 0, 0);
  116. }
  117. /* Sends a leave message */
  118. void exit_chat(ESubGhzChatState *state)
  119. {
  120. /* concatenate the name prefix and leave message */
  121. furi_string_set(state->msg_input, state->name_prefix);
  122. furi_string_cat_str(state->msg_input, " left chat.");
  123. /* encrypt and transmit message */
  124. tx_msg_input(state);
  125. /* clear message input buffer */
  126. furi_string_set_char(state->msg_input, 0, 0);
  127. /* wait for leave message to be delivered */
  128. furi_delay_ms(CHAT_LEAVE_DELAY);
  129. }
  130. /* Whether or not to display the locked message. */
  131. static bool kbd_lock_msg_display(ESubGhzChatState *state)
  132. {
  133. return (state->kbd_lock_msg_ticks != 0);
  134. }
  135. /* Whether or not to hide the locked message again. */
  136. static bool kbd_lock_msg_reset_timeout(ESubGhzChatState *state)
  137. {
  138. if (state->kbd_lock_msg_ticks == 0) {
  139. return false;
  140. }
  141. if (furi_get_tick() - state->kbd_lock_msg_ticks > KBD_UNLOCK_TIMEOUT) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. /* Resets the timeout for the locked message and turns off the backlight if
  147. * specified. */
  148. static void kbd_lock_msg_reset(ESubGhzChatState *state, bool backlight_off)
  149. {
  150. state->kbd_lock_msg_ticks = 0;
  151. state->kbd_lock_count = 0;
  152. if (backlight_off) {
  153. notification_message(state->notification,
  154. &sequence_display_backlight_off);
  155. }
  156. }
  157. /* Locks the keyboard. */
  158. static void kbd_lock(ESubGhzChatState *state)
  159. {
  160. state->kbd_locked = true;
  161. kbd_lock_msg_reset(state, true);
  162. }
  163. /* Unlocks the keyboard. */
  164. static void kbd_unlock(ESubGhzChatState *state)
  165. {
  166. state->kbd_locked = false;
  167. kbd_lock_msg_reset(state, false);
  168. }
  169. /* Custom event callback for view dispatcher. Just calls scene manager. */
  170. static bool esubghz_chat_custom_event_callback(void* context, uint32_t event)
  171. {
  172. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_custom_event_callback");
  173. furi_assert(context);
  174. ESubGhzChatState* state = context;
  175. return scene_manager_handle_custom_event(state->scene_manager, event);
  176. }
  177. /* Navigation event callback for view dispatcher. Just calls scene manager. */
  178. static bool esubghz_chat_navigation_event_callback(void* context)
  179. {
  180. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_navigation_event_callback");
  181. furi_assert(context);
  182. ESubGhzChatState* state = context;
  183. return scene_manager_handle_back_event(state->scene_manager);
  184. }
  185. /* Tick event callback for view dispatcher. Called every TICK_INTERVAL. Resets
  186. * the locked message if necessary. Retrieves a received message from the
  187. * Sub-GHz worker and calls post_rx(). Then calls the scene manager. */
  188. static void esubghz_chat_tick_event_callback(void* context)
  189. {
  190. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_tick_event_callback");
  191. furi_assert(context);
  192. ESubGhzChatState* state = context;
  193. /* reset locked message if necessary */
  194. if (kbd_lock_msg_reset_timeout(state)) {
  195. kbd_lock_msg_reset(state, true);
  196. }
  197. /* if the maximum message size was reached or the
  198. * MESSAGE_COMPLETION_TIMEOUT has expired, retrieve a message and call
  199. * post_rx() */
  200. size_t avail = 0;
  201. while ((avail = subghz_tx_rx_worker_available(state->subghz_worker)) >
  202. 0) {
  203. volatile uint32_t since_last_rx = furi_get_tick() -
  204. state->last_time_rx_data;
  205. if (avail < RX_TX_BUFFER_SIZE && since_last_rx <
  206. MESSAGE_COMPLETION_TIMEOUT) {
  207. break;
  208. }
  209. size_t rx_size = subghz_tx_rx_worker_read(state->subghz_worker,
  210. state->rx_buffer, RX_TX_BUFFER_SIZE);
  211. post_rx(state, rx_size);
  212. }
  213. /* call scene manager */
  214. scene_manager_handle_tick_event(state->scene_manager);
  215. }
  216. /* Hooks into the view port's draw callback to overlay the keyboard locked
  217. * message. */
  218. static void esubghz_hooked_draw_callback(Canvas* canvas, void* context)
  219. {
  220. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_draw_callback");
  221. furi_assert(canvas);
  222. furi_assert(context);
  223. ESubGhzChatState* state = context;
  224. /* call original callback */
  225. state->orig_draw_cb(canvas, state->view_dispatcher);
  226. /* display if the keyboard is locked */
  227. if (state->kbd_locked) {
  228. canvas_set_font(canvas, FontPrimary);
  229. elements_multiline_text_framed(canvas, 42, 30, "Locked");
  230. }
  231. /* display the unlock message if necessary */
  232. if (kbd_lock_msg_display(state)) {
  233. canvas_set_font(canvas, FontSecondary);
  234. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  235. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  236. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  237. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  238. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  239. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  240. }
  241. }
  242. /* Hooks into the view port's input callback to handle the user locking the
  243. * keyboard. */
  244. static void esubghz_hooked_input_callback(InputEvent* event, void* context)
  245. {
  246. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_input_callback");
  247. furi_assert(event);
  248. furi_assert(context);
  249. ESubGhzChatState* state = context;
  250. /* if the keyboard is locked no key presses are forwarded */
  251. if (state->kbd_locked) {
  252. /* key has been pressed, display the unlock message and
  253. * initiate the timer */
  254. if (state->kbd_lock_count == 0) {
  255. state->kbd_lock_msg_ticks = furi_get_tick();
  256. }
  257. /* back button has been pressed, increase the lock counter */
  258. if (event->key == InputKeyBack && event->type ==
  259. InputTypeShort) {
  260. state->kbd_lock_count++;
  261. }
  262. /* unlock the keyboard */
  263. if (state->kbd_lock_count >= KBD_UNLOCK_CNT) {
  264. kbd_unlock(state);
  265. }
  266. /* do not handle the event */
  267. return;
  268. }
  269. if (event->key == InputKeyOk) {
  270. /* if we are in the chat view and no input is ongoing, allow
  271. * locking */
  272. if (state->view_dispatcher->current_view ==
  273. text_box_get_view(state->chat_box) &&
  274. !(state->kbd_ok_input_ongoing)) {
  275. /* lock keyboard upon long press of Ok button */
  276. if (event->type == InputTypeLong) {
  277. kbd_lock(state);
  278. }
  279. /* do not handle any Ok key events to prevent blocking
  280. * of other keys */
  281. return;
  282. }
  283. /* handle ongoing inputs when chaning to chat view */
  284. if (event->type == InputTypePress) {
  285. state->kbd_ok_input_ongoing = true;
  286. } else if (event->type == InputTypeRelease) {
  287. state->kbd_ok_input_ongoing = false;
  288. }
  289. }
  290. /* call original callback */
  291. state->orig_input_cb(event, state->view_dispatcher);
  292. }
  293. static bool helper_strings_alloc(ESubGhzChatState *state)
  294. {
  295. furi_assert(state);
  296. state->name_prefix = furi_string_alloc();
  297. if (state->name_prefix == NULL) {
  298. return false;
  299. }
  300. state->msg_input = furi_string_alloc();
  301. if (state->msg_input == NULL) {
  302. furi_string_free(state->name_prefix);
  303. return false;
  304. }
  305. return true;
  306. }
  307. static void helper_strings_free(ESubGhzChatState *state)
  308. {
  309. furi_assert(state);
  310. furi_string_free(state->name_prefix);
  311. furi_string_free(state->msg_input);
  312. }
  313. static bool chat_box_alloc(ESubGhzChatState *state)
  314. {
  315. furi_assert(state);
  316. state->chat_box = text_box_alloc();
  317. if (state->chat_box == NULL) {
  318. return false;
  319. }
  320. state->chat_box_store = furi_string_alloc();
  321. if (state->chat_box_store == NULL) {
  322. text_box_free(state->chat_box);
  323. return false;
  324. }
  325. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  326. furi_string_set_char(state->chat_box_store, 0, 0);
  327. text_box_set_text(state->chat_box,
  328. furi_string_get_cstr(state->chat_box_store));
  329. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  330. return true;
  331. }
  332. static void chat_box_free(ESubGhzChatState *state)
  333. {
  334. furi_assert(state);
  335. text_box_free(state->chat_box);
  336. furi_string_free(state->chat_box_store);
  337. }
  338. int32_t esubghz_chat(void)
  339. {
  340. /* init the crypto system */
  341. crypto_init();
  342. int32_t err = -1;
  343. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  344. /* allocate necessary structs and buffers */
  345. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  346. if (state == NULL) {
  347. goto err_alloc;
  348. }
  349. memset(state, 0, sizeof(*state));
  350. state->scene_manager = scene_manager_alloc(
  351. &esubghz_chat_scene_event_handlers, state);
  352. if (state->scene_manager == NULL) {
  353. goto err_alloc_sm;
  354. }
  355. state->view_dispatcher = view_dispatcher_alloc();
  356. if (state->view_dispatcher == NULL) {
  357. goto err_alloc_vd;
  358. }
  359. if (!helper_strings_alloc(state)) {
  360. goto err_alloc_hs;
  361. }
  362. state->menu = menu_alloc();
  363. if (state->menu == NULL) {
  364. goto err_alloc_menu;
  365. }
  366. state->text_input = text_input_alloc();
  367. if (state->text_input == NULL) {
  368. goto err_alloc_ti;
  369. }
  370. if (!chat_box_alloc(state)) {
  371. goto err_alloc_cb;
  372. }
  373. state->subghz_worker = subghz_tx_rx_worker_alloc();
  374. if (state->subghz_worker == NULL) {
  375. goto err_alloc_worker;
  376. }
  377. state->crypto_ctx = crypto_ctx_alloc();
  378. if (state->crypto_ctx == NULL) {
  379. goto err_alloc_crypto;
  380. }
  381. /* set the have_read callback of the Sub-GHz worker */
  382. subghz_tx_rx_worker_set_callback_have_read(state->subghz_worker,
  383. have_read_cb, state);
  384. /* enter suppress charge mode */
  385. furi_hal_power_suppress_charge_enter();
  386. /* init internal device */
  387. subghz_devices_init();
  388. state->subghz_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  389. /* set chat name prefix */
  390. furi_string_printf(state->name_prefix, "%s",
  391. furi_hal_version_get_name_ptr());
  392. /* get notification record, we use this to make the flipper vibrate */
  393. /* no error handling here, don't know how */
  394. state->notification = furi_record_open(RECORD_NOTIFICATION);
  395. /* hook into the view port's draw and input callbacks */
  396. state->orig_draw_cb = state->view_dispatcher->view_port->draw_callback;
  397. state->orig_input_cb = state->view_dispatcher->view_port->input_callback;
  398. view_port_draw_callback_set(state->view_dispatcher->view_port,
  399. esubghz_hooked_draw_callback, state);
  400. view_port_input_callback_set(state->view_dispatcher->view_port,
  401. esubghz_hooked_input_callback, state);
  402. view_dispatcher_enable_queue(state->view_dispatcher);
  403. /* set callbacks for view dispatcher */
  404. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  405. view_dispatcher_set_custom_event_callback(
  406. state->view_dispatcher,
  407. esubghz_chat_custom_event_callback);
  408. view_dispatcher_set_navigation_event_callback(
  409. state->view_dispatcher,
  410. esubghz_chat_navigation_event_callback);
  411. view_dispatcher_set_tick_event_callback(
  412. state->view_dispatcher,
  413. esubghz_chat_tick_event_callback,
  414. TICK_INTERVAL);
  415. /* add our two views to the view dispatcher */
  416. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Menu,
  417. menu_get_view(state->menu));
  418. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  419. text_input_get_view(state->text_input));
  420. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  421. text_box_get_view(state->chat_box));
  422. /* get the GUI record and attach the view dispatcher to the GUI */
  423. /* no error handling here, don't know how */
  424. Gui *gui = furi_record_open(RECORD_GUI);
  425. view_dispatcher_attach_to_gui(state->view_dispatcher, gui,
  426. ViewDispatcherTypeFullscreen);
  427. /* switch to the frequency input scene */
  428. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  429. /* run the view dispatcher, this call only returns when we close the
  430. * application */
  431. view_dispatcher_run(state->view_dispatcher);
  432. /* if it is running, stop the Sub-GHz worker */
  433. if (subghz_tx_rx_worker_is_running(state->subghz_worker)) {
  434. exit_chat(state);
  435. subghz_tx_rx_worker_stop(state->subghz_worker);
  436. }
  437. err = 0;
  438. /* close GUI record */
  439. furi_record_close(RECORD_GUI);
  440. /* remove our two views from the view dispatcher */
  441. view_dispatcher_remove_view(state->view_dispatcher,
  442. ESubGhzChatView_Menu);
  443. view_dispatcher_remove_view(state->view_dispatcher,
  444. ESubGhzChatView_Input);
  445. view_dispatcher_remove_view(state->view_dispatcher,
  446. ESubGhzChatView_ChatBox);
  447. /* close notification record */
  448. furi_record_close(RECORD_NOTIFICATION);
  449. /* clear the key and potential password */
  450. crypto_explicit_bzero(state->text_input_store,
  451. sizeof(state->text_input_store));
  452. crypto_ctx_clear(state->crypto_ctx);
  453. /* deinit devices */
  454. subghz_devices_deinit();
  455. /* exit suppress charge mode */
  456. furi_hal_power_suppress_charge_exit();
  457. /* free everything we allocated */
  458. crypto_ctx_free(state->crypto_ctx);
  459. err_alloc_crypto:
  460. subghz_tx_rx_worker_free(state->subghz_worker);
  461. err_alloc_worker:
  462. chat_box_free(state);
  463. err_alloc_cb:
  464. text_input_free(state->text_input);
  465. err_alloc_ti:
  466. menu_free(state->menu);
  467. err_alloc_menu:
  468. helper_strings_free(state);
  469. err_alloc_hs:
  470. view_dispatcher_free(state->view_dispatcher);
  471. err_alloc_vd:
  472. scene_manager_free(state->scene_manager);
  473. err_alloc_sm:
  474. free(state);
  475. err_alloc:
  476. if (err != 0) {
  477. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  478. } else {
  479. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  480. }
  481. return err;
  482. }