esubghz_chat.c 29 KB

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