esubghz_chat.c 14 KB

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