esubghz_chat.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 <lib/subghz/devices/cc1101_int/cc1101_int_interconnect.h>
  13. #include <lib/subghz/subghz_tx_rx_worker.h>
  14. #include "esubghz_chat_icons.h"
  15. #include "crypto/gcm.h"
  16. #define APPLICATION_NAME "ESubGhzChat"
  17. #define DEFAULT_FREQ 433920000
  18. #define KEY_BITS 256
  19. #define IV_BYTES 12
  20. #define TAG_BYTES 16
  21. #define RX_TX_BUFFER_SIZE 1024
  22. #define CHAT_BOX_STORE_SIZE 4096
  23. #define TEXT_INPUT_STORE_SIZE 512
  24. #define TICK_INTERVAL 50
  25. #define MESSAGE_COMPLETION_TIMEOUT 500
  26. #define TIMEOUT_BETWEEN_MESSAGES 500
  27. #define KBD_UNLOCK_CNT 3
  28. #define KBD_UNLOCK_TIMEOUT 1000
  29. typedef struct {
  30. SceneManager *scene_manager;
  31. ViewDispatcher *view_dispatcher;
  32. NotificationApp *notification;
  33. // UI elements
  34. TextBox *chat_box;
  35. FuriString *chat_box_store;
  36. TextInput *text_input;
  37. char text_input_store[TEXT_INPUT_STORE_SIZE + 1];
  38. // for Sub-GHz
  39. uint32_t frequency;
  40. SubGhzTxRxWorker *subghz_worker;
  41. #ifdef FW_ORIGIN_Official
  42. const SubGhzDevice *subghz_device;
  43. #endif /* FW_ORIGIN_Official */
  44. // message assembly before TX
  45. FuriString *name_prefix;
  46. FuriString *msg_input;
  47. // encryption
  48. bool encrypted;
  49. gcm_context gcm_ctx;
  50. // RX and TX buffers
  51. uint8_t rx_buffer[RX_TX_BUFFER_SIZE];
  52. uint8_t tx_buffer[RX_TX_BUFFER_SIZE];
  53. char rx_str_buffer[RX_TX_BUFFER_SIZE + 1];
  54. volatile uint32_t last_time_rx_data;
  55. // for locking
  56. ViewPortDrawCallback orig_draw_cb;
  57. ViewPortInputCallback orig_input_cb;
  58. bool kbd_locked;
  59. uint32_t kbd_lock_msg_ticks;
  60. uint8_t kbd_lock_count;
  61. bool kbd_ok_input_ongoing;
  62. } ESubGhzChatState;
  63. typedef enum {
  64. ESubGhzChatScene_FreqInput,
  65. ESubGhzChatScene_PassInput,
  66. ESubGhzChatScene_ChatInput,
  67. ESubGhzChatScene_ChatBox,
  68. ESubGhzChatScene_MAX
  69. } ESubGhzChatScene;
  70. typedef enum {
  71. ESubGhzChatView_Input,
  72. ESubGhzChatView_ChatBox,
  73. } ESubGhzChatView;
  74. typedef enum {
  75. ESubGhzChatEvent_FreqEntered,
  76. ESubGhzChatEvent_PassEntered,
  77. ESubGhzChatEvent_MsgEntered
  78. } ESubGhzChatEvent;
  79. /* Function to clear sensitive memory. */
  80. static void esubghz_chat_explicit_bzero(void *s, size_t len)
  81. {
  82. memset(s, 0, len);
  83. asm volatile("" ::: "memory");
  84. }
  85. /* Callback for RX events from the Sub-GHz worker. Records the current ticks as
  86. * the time of the last reception. */
  87. static void have_read_cb(void* context)
  88. {
  89. furi_assert(context);
  90. ESubGhzChatState* state = context;
  91. state->last_time_rx_data = furi_get_tick();
  92. }
  93. /* Decrypts a message for post_rx(). */
  94. static bool post_rx_decrypt(ESubGhzChatState *state, size_t rx_size)
  95. {
  96. if (rx_size < IV_BYTES + TAG_BYTES + 1) {
  97. return false;
  98. }
  99. int ret = gcm_auth_decrypt(&(state->gcm_ctx),
  100. state->rx_buffer, IV_BYTES,
  101. NULL, 0,
  102. state->rx_buffer + IV_BYTES,
  103. (uint8_t *) state->rx_str_buffer,
  104. rx_size - (IV_BYTES + TAG_BYTES),
  105. state->rx_buffer + rx_size - TAG_BYTES,
  106. TAG_BYTES);
  107. state->rx_str_buffer[rx_size - (IV_BYTES + TAG_BYTES)] = 0;
  108. return (ret == 0);
  109. }
  110. /* Post RX handler, decrypts received messages, displays them in the text box
  111. * and sends a notification. */
  112. static void post_rx(ESubGhzChatState *state, size_t rx_size)
  113. {
  114. furi_assert(state);
  115. if (rx_size == 0) {
  116. return;
  117. }
  118. furi_check(rx_size <= RX_TX_BUFFER_SIZE);
  119. /* decrypt if necessary */
  120. if (!state->encrypted) {
  121. memcpy(state->rx_str_buffer, state->rx_buffer, rx_size);
  122. state->rx_str_buffer[rx_size] = 0;
  123. } else {
  124. /* if decryption fails output an error message */
  125. if (!post_rx_decrypt(state, rx_size)) {
  126. strcpy(state->rx_str_buffer, "ERR: Decryption failed!");
  127. }
  128. }
  129. /* append message to text box */
  130. furi_string_cat_printf(state->chat_box_store, "\n%s [%u]",
  131. state->rx_str_buffer, rx_size);
  132. /* send notification (make the flipper vibrate) */
  133. notification_message(state->notification, &sequence_single_vibro);
  134. /* reset text box contents and focus */
  135. text_box_set_text(state->chat_box,
  136. furi_string_get_cstr(state->chat_box_store));
  137. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  138. }
  139. /* Sends FreqEntered event to scene manager and displays the frequency in the
  140. * text box. */
  141. static void freq_input_cb(void *context)
  142. {
  143. furi_assert(context);
  144. ESubGhzChatState* state = context;
  145. furi_string_cat_printf(state->chat_box_store, "Frequency: %lu",
  146. state->frequency);
  147. scene_manager_handle_custom_event(state->scene_manager,
  148. ESubGhzChatEvent_FreqEntered);
  149. }
  150. /* Validates the entered frequency. */
  151. static bool freq_input_validator(const char *text, FuriString *error,
  152. void *context)
  153. {
  154. furi_assert(text);
  155. furi_assert(error);
  156. furi_assert(context);
  157. ESubGhzChatState* state = context;
  158. int ret = sscanf(text, "%lu", &(state->frequency));
  159. if (ret != 1) {
  160. furi_string_printf(error, "Please enter\nfrequency\nin Hz!");
  161. return false;
  162. }
  163. if (!subghz_devices_is_frequency_valid(state->subghz_device,
  164. state->frequency)) {
  165. furi_string_printf(error, "Frequency\n%lu\n is invalid!",
  166. state->frequency);
  167. return false;
  168. }
  169. #ifdef FW_ORIGIN_Official
  170. if (!furi_hal_region_is_frequency_allowed(state->frequency)) {
  171. #else /* FW_ORIGIN_Official */
  172. if (!furi_hal_subghz_is_tx_allowed(state->frequency)) {
  173. #endif /* FW_ORIGIN_Official */
  174. furi_string_printf(error, "TX forbidden\non frequency\n%lu!",
  175. state->frequency);
  176. return false;
  177. }
  178. return true;
  179. }
  180. /* Sends PassEntered event to scene manager and displays whether or not
  181. * encryption has been enabled in the text box. Also clears the text input
  182. * buffer to remove the password and starts the Sub-GHz worker. */
  183. static void pass_input_cb(void *context)
  184. {
  185. furi_assert(context);
  186. ESubGhzChatState* state = context;
  187. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  188. (state->encrypted ? "yes" : "no"));
  189. /* clear the text input buffer to remove the password */
  190. esubghz_chat_explicit_bzero(state->text_input_store,
  191. sizeof(state->text_input_store));
  192. subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
  193. state->frequency);
  194. scene_manager_handle_custom_event(state->scene_manager,
  195. ESubGhzChatEvent_PassEntered);
  196. }
  197. /* If a password was entered this derives a key from the password using a
  198. * single pass of SHA256 and initiates the AES-GCM context for encryption. If
  199. * the initiation fails, the password is rejected. */
  200. static bool pass_input_validator(const char *text, FuriString *error,
  201. void *context)
  202. {
  203. furi_assert(text);
  204. furi_assert(error);
  205. furi_assert(context);
  206. ESubGhzChatState* state = context;
  207. #ifdef FW_ORIGIN_Official
  208. if (strlen(text) == 0) {
  209. furi_string_printf(error, "Enter a\npassword!");
  210. return false;
  211. }
  212. if (strcmp(text, " ") == 0) {
  213. #else /* FW_ORIGIN_Official */
  214. if (strlen(text) == 0) {
  215. #endif /* FW_ORIGIN_Official */
  216. state->encrypted = false;
  217. return true;
  218. }
  219. unsigned char key[KEY_BITS / 8];
  220. /* derive a key from the password */
  221. sha256((unsigned char *) text, strlen(text), key);
  222. // TODO: remove this
  223. furi_string_cat_printf(state->chat_box_store, "\nKey:");
  224. int i;
  225. for (i = 0; i < KEY_BITS / 8; i++) {
  226. furi_string_cat_printf(state->chat_box_store, " %02x", key[i]);
  227. }
  228. /* initiate the AES-GCM context */
  229. int ret = gcm_setkey(&(state->gcm_ctx), key, KEY_BITS / 8);
  230. /* cleanup */
  231. esubghz_chat_explicit_bzero(key, sizeof(key));
  232. if (ret != 0) {
  233. esubghz_chat_explicit_bzero(&(state->gcm_ctx),
  234. sizeof(state->gcm_ctx));
  235. furi_string_printf(error, "Failed to\nset key!");
  236. return false;
  237. }
  238. state->encrypted = true;
  239. return true;
  240. }
  241. /* If no message was entred this simply emits a MsgEntered event to the scene
  242. * manager to switch to the text box. If a message was entered it is appended
  243. * to the name string. The result is encrypted, if encryption is enabled, and
  244. * then copied into the TX buffer. The contents of the TX buffer are then
  245. * transmitted. The sent message is appended to the text box and a MsgEntered
  246. * event is sent to the scene manager to switch to the text box view. */
  247. static void chat_input_cb(void *context)
  248. {
  249. furi_assert(context);
  250. ESubGhzChatState* state = context;
  251. /* no message, just switch to the text box view */
  252. #ifdef FW_ORIGIN_Official
  253. if (strcmp(state->text_input_store, " ") == 0) {
  254. #else /* FW_ORIGIN_Official */
  255. if (strlen(state->text_input_store) == 0) {
  256. #endif /* FW_ORIGIN_Official */
  257. scene_manager_handle_custom_event(state->scene_manager,
  258. ESubGhzChatEvent_MsgEntered);
  259. return;
  260. }
  261. /* concatenate the name prefix and the actual message */
  262. furi_string_set(state->msg_input, state->name_prefix);
  263. furi_string_cat_str(state->msg_input, state->text_input_store);
  264. /* append the message to the chat box */
  265. furi_string_cat_printf(state->chat_box_store, "\n%s",
  266. furi_string_get_cstr(state->msg_input));
  267. /* encrypt message if necessary */
  268. size_t msg_len = strlen(furi_string_get_cstr(state->msg_input));
  269. size_t tx_size = msg_len;
  270. if (state->encrypted) {
  271. tx_size += IV_BYTES + TAG_BYTES;
  272. furi_check(tx_size <= sizeof(state->tx_buffer));
  273. furi_hal_random_fill_buf(state->tx_buffer, IV_BYTES);
  274. gcm_crypt_and_tag(&(state->gcm_ctx), ENCRYPT,
  275. state->tx_buffer, IV_BYTES,
  276. NULL, 0,
  277. (unsigned char *)
  278. furi_string_get_cstr(state->msg_input),
  279. state->tx_buffer + IV_BYTES,
  280. msg_len,
  281. state->tx_buffer + IV_BYTES + msg_len,
  282. TAG_BYTES);
  283. } else {
  284. furi_check(tx_size <= sizeof(state->tx_buffer));
  285. memcpy(state->tx_buffer,
  286. furi_string_get_cstr(state->msg_input),
  287. tx_size);
  288. }
  289. /* clear message input buffer */
  290. furi_string_set_char(state->msg_input, 0, 0);
  291. // TODO: remove this
  292. furi_string_cat_printf(state->chat_box_store, "\nTXed (HEX):");
  293. size_t i;
  294. for (i = 0; i < tx_size; i++) {
  295. furi_string_cat_printf(state->chat_box_store, " %02x",
  296. state->tx_buffer[i]);
  297. }
  298. /* transmit */
  299. subghz_tx_rx_worker_write(state->subghz_worker, state->tx_buffer,
  300. tx_size);
  301. /* switch to text box view */
  302. scene_manager_handle_custom_event(state->scene_manager,
  303. ESubGhzChatEvent_MsgEntered);
  304. }
  305. /* Prepares the frequency input scene. */
  306. static void scene_on_enter_freq_input(void* context)
  307. {
  308. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_freq_input");
  309. furi_assert(context);
  310. ESubGhzChatState* state = context;
  311. snprintf(state->text_input_store, TEXT_INPUT_STORE_SIZE, "%lu",
  312. (uint32_t) DEFAULT_FREQ);
  313. text_input_reset(state->text_input);
  314. text_input_set_result_callback(
  315. state->text_input,
  316. freq_input_cb,
  317. state,
  318. state->text_input_store,
  319. sizeof(state->text_input_store),
  320. true);
  321. text_input_set_validator(
  322. state->text_input,
  323. freq_input_validator,
  324. state);
  325. text_input_set_header_text(
  326. state->text_input,
  327. "Frequency");
  328. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  329. }
  330. /* Handles scene manager events for the frequency input scene. */
  331. static bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
  332. {
  333. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_freq_input");
  334. furi_assert(context);
  335. ESubGhzChatState* state = context;
  336. bool consumed = false;
  337. switch(event.type) {
  338. case SceneManagerEventTypeCustom:
  339. switch(event.event) {
  340. /* switch to password input scene */
  341. case ESubGhzChatEvent_FreqEntered:
  342. scene_manager_next_scene(state->scene_manager,
  343. ESubGhzChatScene_PassInput);
  344. consumed = true;
  345. break;
  346. }
  347. break;
  348. case SceneManagerEventTypeBack:
  349. /* stop the application if the user presses back here */
  350. view_dispatcher_stop(state->view_dispatcher);
  351. consumed = true;
  352. break;
  353. default:
  354. consumed = false;
  355. break;
  356. }
  357. return consumed;
  358. }
  359. /* Cleans up the frequency input scene. */
  360. static void scene_on_exit_freq_input(void* context)
  361. {
  362. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_freq_input");
  363. furi_assert(context);
  364. ESubGhzChatState* state = context;
  365. text_input_reset(state->text_input);
  366. }
  367. /* Prepares the password input scene. */
  368. static void scene_on_enter_pass_input(void* context)
  369. {
  370. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  371. furi_assert(context);
  372. ESubGhzChatState* state = context;
  373. state->text_input_store[0] = 0;
  374. text_input_reset(state->text_input);
  375. text_input_set_result_callback(
  376. state->text_input,
  377. pass_input_cb,
  378. state,
  379. state->text_input_store,
  380. sizeof(state->text_input_store),
  381. true);
  382. text_input_set_validator(
  383. state->text_input,
  384. pass_input_validator,
  385. state);
  386. text_input_set_header_text(
  387. state->text_input,
  388. #ifdef FW_ORIGIN_Official
  389. "Password (space for no encr.)");
  390. #else /* FW_ORIGIN_Official */
  391. "Password (empty for no encr.)");
  392. text_input_set_minimum_length(state->text_input, 0);
  393. #endif /* FW_ORIGIN_Official */
  394. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  395. }
  396. /* Handles scene manager events for the password input scene. */
  397. static bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  398. {
  399. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  400. furi_assert(context);
  401. ESubGhzChatState* state = context;
  402. bool consumed = false;
  403. switch(event.type) {
  404. case SceneManagerEventTypeCustom:
  405. switch(event.event) {
  406. /* switch to message input scene */
  407. case ESubGhzChatEvent_PassEntered:
  408. scene_manager_next_scene(state->scene_manager,
  409. ESubGhzChatScene_ChatInput);
  410. consumed = true;
  411. break;
  412. }
  413. break;
  414. case SceneManagerEventTypeBack:
  415. /* stop the application if the user presses back here */
  416. view_dispatcher_stop(state->view_dispatcher);
  417. consumed = true;
  418. break;
  419. default:
  420. consumed = false;
  421. break;
  422. }
  423. return consumed;
  424. }
  425. /* Cleans up the password input scene. */
  426. static void scene_on_exit_pass_input(void* context)
  427. {
  428. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  429. furi_assert(context);
  430. ESubGhzChatState* state = context;
  431. text_input_reset(state->text_input);
  432. }
  433. /* Prepares the message input scene. */
  434. static void scene_on_enter_chat_input(void* context)
  435. {
  436. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  437. furi_assert(context);
  438. ESubGhzChatState* state = context;
  439. state->text_input_store[0] = 0;
  440. text_input_reset(state->text_input);
  441. text_input_set_result_callback(
  442. state->text_input,
  443. chat_input_cb,
  444. state,
  445. state->text_input_store,
  446. sizeof(state->text_input_store),
  447. true);
  448. text_input_set_validator(
  449. state->text_input,
  450. NULL,
  451. NULL);
  452. text_input_set_header_text(
  453. state->text_input,
  454. #ifdef FW_ORIGIN_Official
  455. "Message (space for none)");
  456. #else /* FW_ORIGIN_Official */
  457. "Message");
  458. text_input_set_minimum_length(state->text_input, 0);
  459. #endif /* FW_ORIGIN_Official */
  460. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  461. }
  462. /* Handles scene manager events for the message input scene. */
  463. static bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  464. {
  465. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  466. furi_assert(context);
  467. ESubGhzChatState* state = context;
  468. bool consumed = false;
  469. switch(event.type) {
  470. case SceneManagerEventTypeCustom:
  471. switch(event.event) {
  472. /* switch to text box scene */
  473. case ESubGhzChatEvent_MsgEntered:
  474. scene_manager_next_scene(state->scene_manager,
  475. ESubGhzChatScene_ChatBox);
  476. consumed = true;
  477. break;
  478. }
  479. break;
  480. case SceneManagerEventTypeBack:
  481. /* stop the application if the user presses back here */
  482. view_dispatcher_stop(state->view_dispatcher);
  483. consumed = true;
  484. break;
  485. default:
  486. consumed = false;
  487. break;
  488. }
  489. return consumed;
  490. }
  491. /* Cleans up the password input scene. */
  492. static void scene_on_exit_chat_input(void* context)
  493. {
  494. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  495. furi_assert(context);
  496. ESubGhzChatState* state = context;
  497. text_input_reset(state->text_input);
  498. }
  499. /* Prepares the text box scene. */
  500. static void scene_on_enter_chat_box(void* context)
  501. {
  502. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_box");
  503. furi_assert(context);
  504. ESubGhzChatState* state = context;
  505. text_box_reset(state->chat_box);
  506. text_box_set_text(state->chat_box,
  507. furi_string_get_cstr(state->chat_box_store));
  508. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  509. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  510. }
  511. /* Handles scene manager events for the text box scene. No events are handled
  512. * here. */
  513. static bool scene_on_event_chat_box(void* context, SceneManagerEvent event)
  514. {
  515. UNUSED(event);
  516. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_box");
  517. furi_assert(context);
  518. return false;
  519. }
  520. /* Cleans up the text box scene. */
  521. static void scene_on_exit_chat_box(void* context)
  522. {
  523. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_box");
  524. furi_assert(context);
  525. ESubGhzChatState* state = context;
  526. text_box_reset(state->chat_box);
  527. }
  528. /* Scene entry handlers. */
  529. static void (*const esubghz_chat_scene_on_enter_handlers[])(void*) = {
  530. scene_on_enter_freq_input,
  531. scene_on_enter_pass_input,
  532. scene_on_enter_chat_input,
  533. scene_on_enter_chat_box
  534. };
  535. /* Scene event handlers. */
  536. static bool (*const esubghz_chat_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  537. scene_on_event_freq_input,
  538. scene_on_event_pass_input,
  539. scene_on_event_chat_input,
  540. scene_on_event_chat_box
  541. };
  542. /* Scene exit handlers. */
  543. static void (*const esubghz_chat_scene_on_exit_handlers[])(void*) = {
  544. scene_on_exit_freq_input,
  545. scene_on_exit_pass_input,
  546. scene_on_exit_chat_input,
  547. scene_on_exit_chat_box
  548. };
  549. /* Handlers for the scene manager. */
  550. static const SceneManagerHandlers esubghz_chat_scene_event_handlers = {
  551. .on_enter_handlers = esubghz_chat_scene_on_enter_handlers,
  552. .on_event_handlers = esubghz_chat_scene_on_event_handlers,
  553. .on_exit_handlers = esubghz_chat_scene_on_exit_handlers,
  554. .scene_num = ESubGhzChatScene_MAX};
  555. /* Whether or not to display the locked message. */
  556. static bool kbd_lock_msg_display(ESubGhzChatState *state)
  557. {
  558. return (state->kbd_lock_msg_ticks != 0);
  559. }
  560. /* Whether or not to hide the locked message again. */
  561. static bool kbd_lock_msg_reset_timeout(ESubGhzChatState *state)
  562. {
  563. if (state->kbd_lock_msg_ticks == 0) {
  564. return false;
  565. }
  566. if (furi_get_tick() - state->kbd_lock_msg_ticks > KBD_UNLOCK_TIMEOUT) {
  567. return true;
  568. }
  569. return false;
  570. }
  571. /* Resets the timeout for the locked message and turns off the backlight if
  572. * specified. */
  573. static void kbd_lock_msg_reset(ESubGhzChatState *state, bool backlight_off)
  574. {
  575. state->kbd_lock_msg_ticks = 0;
  576. state->kbd_lock_count = 0;
  577. if (backlight_off) {
  578. notification_message(state->notification,
  579. &sequence_display_backlight_off);
  580. }
  581. }
  582. /* Locks the keyboard. */
  583. static void kbd_lock(ESubGhzChatState *state)
  584. {
  585. state->kbd_locked = true;
  586. kbd_lock_msg_reset(state, true);
  587. }
  588. /* Unlocks the keyboard. */
  589. static void kbd_unlock(ESubGhzChatState *state)
  590. {
  591. state->kbd_locked = false;
  592. kbd_lock_msg_reset(state, false);
  593. }
  594. /* Custom event callback for view dispatcher. Just calls scene manager. */
  595. static bool esubghz_chat_custom_event_callback(void* context, uint32_t event)
  596. {
  597. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_custom_event_callback");
  598. furi_assert(context);
  599. ESubGhzChatState* state = context;
  600. return scene_manager_handle_custom_event(state->scene_manager, event);
  601. }
  602. /* Navigation event callback for view dispatcher. Just calls scene manager. */
  603. static bool esubghz_chat_navigation_event_callback(void* context)
  604. {
  605. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_navigation_event_callback");
  606. furi_assert(context);
  607. ESubGhzChatState* state = context;
  608. return scene_manager_handle_back_event(state->scene_manager);
  609. }
  610. /* Tick event callback for view dispatcher. Called every TICK_INTERVAL. Resets
  611. * the locked message if necessary. Retrieves a received message from the
  612. * Sub-GHz worker and calls post_rx(). Then calls the scene manager. */
  613. static void esubghz_chat_tick_event_callback(void* context)
  614. {
  615. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_tick_event_callback");
  616. furi_assert(context);
  617. ESubGhzChatState* state = context;
  618. /* reset locked message if necessary */
  619. if (kbd_lock_msg_reset_timeout(state)) {
  620. kbd_lock_msg_reset(state, true);
  621. }
  622. /* if the maximum message size was reached or the
  623. * MESSAGE_COMPLETION_TIMEOUT has expired, retrieve a message and call
  624. * post_rx() */
  625. size_t avail = 0;
  626. while ((avail = subghz_tx_rx_worker_available(state->subghz_worker)) >
  627. 0) {
  628. volatile uint32_t since_last_rx = furi_get_tick() -
  629. state->last_time_rx_data;
  630. if (avail < RX_TX_BUFFER_SIZE && since_last_rx <
  631. MESSAGE_COMPLETION_TIMEOUT) {
  632. break;
  633. }
  634. size_t rx_size = subghz_tx_rx_worker_read(state->subghz_worker,
  635. state->rx_buffer, RX_TX_BUFFER_SIZE);
  636. post_rx(state, rx_size);
  637. }
  638. /* call scene manager */
  639. scene_manager_handle_tick_event(state->scene_manager);
  640. }
  641. /* Hooks into the view port's draw callback to overlay the keyboard locked
  642. * message. */
  643. static void esubghz_hooked_draw_callback(Canvas* canvas, void* context)
  644. {
  645. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_draw_callback");
  646. furi_assert(canvas);
  647. furi_assert(context);
  648. ESubGhzChatState* state = context;
  649. /* call original callback */
  650. state->orig_draw_cb(canvas, state->view_dispatcher);
  651. /* display if the keyboard is locked */
  652. if (state->kbd_locked) {
  653. canvas_set_font(canvas, FontPrimary);
  654. elements_multiline_text_framed(canvas, 42, 30, "Locked");
  655. }
  656. /* display the unlock message if necessary */
  657. if (kbd_lock_msg_display(state)) {
  658. canvas_set_font(canvas, FontSecondary);
  659. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  660. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  661. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  662. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  663. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  664. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  665. }
  666. }
  667. /* Hooks into the view port's input callback to handle the user locking the
  668. * keyboard. */
  669. static void esubghz_hooked_input_callback(InputEvent* event, void* context)
  670. {
  671. FURI_LOG_T(APPLICATION_NAME, "esubghz_hooked_input_callback");
  672. furi_assert(event);
  673. furi_assert(context);
  674. ESubGhzChatState* state = context;
  675. /* if the keyboard is locked no key presses are forwarded */
  676. if (state->kbd_locked) {
  677. /* key has been pressed, display the unlock message and
  678. * initiate the timer */
  679. if (state->kbd_lock_count == 0) {
  680. state->kbd_lock_msg_ticks = furi_get_tick();
  681. }
  682. /* back button has been pressed, increase the lock counter */
  683. if (event->key == InputKeyBack && event->type ==
  684. InputTypeShort) {
  685. state->kbd_lock_count++;
  686. }
  687. /* unlock the keyboard */
  688. if (state->kbd_lock_count >= KBD_UNLOCK_CNT) {
  689. kbd_unlock(state);
  690. }
  691. /* do not handle the event */
  692. return;
  693. }
  694. if (event->key == InputKeyOk) {
  695. /* if we are in the chat view and no input is ongoing, allow
  696. * locking */
  697. if (state->view_dispatcher->current_view ==
  698. text_box_get_view(state->chat_box) &&
  699. !(state->kbd_ok_input_ongoing)) {
  700. /* lock keyboard upon long press of Ok button */
  701. if (event->type == InputTypeLong) {
  702. kbd_lock(state);
  703. }
  704. /* do not handle any Ok key events to prevent blocking
  705. * of other keys */
  706. return;
  707. }
  708. /* handle ongoing inputs when chaning to chat view */
  709. if (event->type == InputTypePress) {
  710. state->kbd_ok_input_ongoing = true;
  711. } else if (event->type == InputTypeRelease) {
  712. state->kbd_ok_input_ongoing = false;
  713. }
  714. }
  715. /* call original callback */
  716. state->orig_input_cb(event, state->view_dispatcher);
  717. }
  718. static bool helper_strings_alloc(ESubGhzChatState *state)
  719. {
  720. furi_assert(state);
  721. state->name_prefix = furi_string_alloc();
  722. if (state->name_prefix == NULL) {
  723. return false;
  724. }
  725. state->msg_input = furi_string_alloc();
  726. if (state->msg_input == NULL) {
  727. furi_string_free(state->name_prefix);
  728. return false;
  729. }
  730. return true;
  731. }
  732. static void helper_strings_free(ESubGhzChatState *state)
  733. {
  734. furi_assert(state);
  735. furi_string_free(state->name_prefix);
  736. furi_string_free(state->msg_input);
  737. }
  738. static bool chat_box_alloc(ESubGhzChatState *state)
  739. {
  740. furi_assert(state);
  741. state->chat_box = text_box_alloc();
  742. if (state->chat_box == NULL) {
  743. return false;
  744. }
  745. state->chat_box_store = furi_string_alloc();
  746. if (state->chat_box_store == NULL) {
  747. text_box_free(state->chat_box);
  748. return false;
  749. }
  750. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  751. furi_string_set_char(state->chat_box_store, 0, 0);
  752. text_box_set_text(state->chat_box,
  753. furi_string_get_cstr(state->chat_box_store));
  754. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  755. return true;
  756. }
  757. static void chat_box_free(ESubGhzChatState *state)
  758. {
  759. furi_assert(state);
  760. text_box_free(state->chat_box);
  761. furi_string_free(state->chat_box_store);
  762. }
  763. int32_t esubghz_chat(void)
  764. {
  765. /* init the GCM and AES tables */
  766. gcm_initialize();
  767. int32_t err = -1;
  768. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  769. /* allocate necessary structs and buffers */
  770. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  771. if (state == NULL) {
  772. goto err_alloc;
  773. }
  774. memset(state, 0, sizeof(*state));
  775. state->scene_manager = scene_manager_alloc(
  776. &esubghz_chat_scene_event_handlers, state);
  777. if (state->scene_manager == NULL) {
  778. goto err_alloc_sm;
  779. }
  780. state->view_dispatcher = view_dispatcher_alloc();
  781. if (state->view_dispatcher == NULL) {
  782. goto err_alloc_vd;
  783. }
  784. if (!helper_strings_alloc(state)) {
  785. goto err_alloc_hs;
  786. }
  787. state->text_input = text_input_alloc();
  788. if (state->text_input == NULL) {
  789. goto err_alloc_ti;
  790. }
  791. if (!chat_box_alloc(state)) {
  792. goto err_alloc_cb;
  793. }
  794. state->subghz_worker = subghz_tx_rx_worker_alloc();
  795. if (state->subghz_worker == NULL) {
  796. goto err_alloc_worker;
  797. }
  798. /* set the have_read callback of the Sub-GHz worker */
  799. subghz_tx_rx_worker_set_callback_have_read(state->subghz_worker,
  800. have_read_cb, state);
  801. /* enter suppress charge mode */
  802. furi_hal_power_suppress_charge_enter();
  803. /* init internal device */
  804. subghz_devices_init();
  805. state->subghz_device = subghz_devices_get_by_name(SUBGHZ_DEVICE_CC1101_INT_NAME);
  806. /* set chat name prefix */
  807. // TODO: handle escape chars here somehow
  808. furi_string_printf(state->name_prefix, "\033[0;33m%s\033[0m: ",
  809. furi_hal_version_get_name_ptr());
  810. /* get notification record, we use this to make the flipper vibrate */
  811. /* no error handling here, don't know how */
  812. state->notification = furi_record_open(RECORD_NOTIFICATION);
  813. /* hook into the view port's draw and input callbacks */
  814. state->orig_draw_cb = state->view_dispatcher->view_port->draw_callback;
  815. state->orig_input_cb = state->view_dispatcher->view_port->input_callback;
  816. view_port_draw_callback_set(state->view_dispatcher->view_port,
  817. esubghz_hooked_draw_callback, state);
  818. view_port_input_callback_set(state->view_dispatcher->view_port,
  819. esubghz_hooked_input_callback, state);
  820. view_dispatcher_enable_queue(state->view_dispatcher);
  821. /* set callbacks for view dispatcher */
  822. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  823. view_dispatcher_set_custom_event_callback(
  824. state->view_dispatcher,
  825. esubghz_chat_custom_event_callback);
  826. view_dispatcher_set_navigation_event_callback(
  827. state->view_dispatcher,
  828. esubghz_chat_navigation_event_callback);
  829. view_dispatcher_set_tick_event_callback(
  830. state->view_dispatcher,
  831. esubghz_chat_tick_event_callback,
  832. TICK_INTERVAL);
  833. /* add our two views to the view dispatcher */
  834. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  835. text_input_get_view(state->text_input));
  836. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  837. text_box_get_view(state->chat_box));
  838. /* get the GUI record and attach the view dispatcher to the GUI */
  839. /* no error handling here, don't know how */
  840. Gui *gui = furi_record_open(RECORD_GUI);
  841. view_dispatcher_attach_to_gui(state->view_dispatcher, gui,
  842. ViewDispatcherTypeFullscreen);
  843. /* switch to the frequency input scene */
  844. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  845. /* run the view dispatcher, this call only returns when we close the
  846. * application */
  847. view_dispatcher_run(state->view_dispatcher);
  848. /* if it is running, stop the Sub-GHz worker */
  849. if (subghz_tx_rx_worker_is_running(state->subghz_worker)) {
  850. subghz_tx_rx_worker_stop(state->subghz_worker);
  851. }
  852. err = 0;
  853. /* close GUI record */
  854. furi_record_close(RECORD_GUI);
  855. /* remove our two views from the view dispatcher */
  856. view_dispatcher_remove_view(state->view_dispatcher,
  857. ESubGhzChatView_Input);
  858. view_dispatcher_remove_view(state->view_dispatcher,
  859. ESubGhzChatView_ChatBox);
  860. /* close notification record */
  861. furi_record_close(RECORD_NOTIFICATION);
  862. /* clear the key and potential password */
  863. esubghz_chat_explicit_bzero(state->text_input_store,
  864. sizeof(state->text_input_store));
  865. esubghz_chat_explicit_bzero(&(state->gcm_ctx), sizeof(state->gcm_ctx));
  866. /* deinit devices */
  867. subghz_devices_deinit();
  868. /* exit suppress charge mode */
  869. furi_hal_power_suppress_charge_exit();
  870. /* free everything we allocated */
  871. subghz_tx_rx_worker_free(state->subghz_worker);
  872. err_alloc_worker:
  873. chat_box_free(state);
  874. err_alloc_cb:
  875. text_input_free(state->text_input);
  876. err_alloc_ti:
  877. helper_strings_free(state);
  878. err_alloc_hs:
  879. view_dispatcher_free(state->view_dispatcher);
  880. err_alloc_vd:
  881. scene_manager_free(state->scene_manager);
  882. err_alloc_sm:
  883. free(state);
  884. err_alloc:
  885. if (err != 0) {
  886. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  887. } else {
  888. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  889. }
  890. return err;
  891. }