esubghz_chat.c 29 KB

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