esubghz_chat.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/modules/text_box.h>
  5. #include <gui/modules/text_input.h>
  6. #include <gui/view_dispatcher.h>
  7. #include <gui/scene_manager.h>
  8. #include <toolbox/sha256.h>
  9. #include <notification/notification_messages.h>
  10. #include "crypto/gcm.h"
  11. #define APPLICATION_NAME "ESubGhzChat"
  12. #define DEFAULT_FREQ 433920000
  13. #define KEY_BITS 256
  14. #define IV_BYTES 12
  15. #define TAG_BYTES 16
  16. #define RX_TX_BUFFER_SIZE 1024
  17. #define CHAT_BOX_STORE_SIZE 4096
  18. #define TEXT_INPUT_STORE_SIZE 512
  19. typedef struct {
  20. SceneManager *scene_manager;
  21. ViewDispatcher *view_dispatcher;
  22. NotificationApp *notification;
  23. TextBox *chat_box;
  24. FuriString *chat_box_store;
  25. TextInput *text_input;
  26. char text_input_store[TEXT_INPUT_STORE_SIZE + 1];
  27. FuriString *name_prefix;
  28. FuriString *msg_input;
  29. bool encrypted;
  30. uint32_t frequency;
  31. gcm_context gcm_ctx;
  32. uint8_t rx_buffer[RX_TX_BUFFER_SIZE];
  33. uint8_t tx_buffer[RX_TX_BUFFER_SIZE];
  34. char rx_str_buffer[RX_TX_BUFFER_SIZE + 1];
  35. } ESubGhzChatState;
  36. typedef enum {
  37. ESubGhzChatScene_FreqInput,
  38. ESubGhzChatScene_PassInput,
  39. ESubGhzChatScene_ChatInput,
  40. ESubGhzChatScene_ChatBox,
  41. ESubGhzChatScene_MAX
  42. } ESubGhzChatScene;
  43. typedef enum {
  44. ESubGhzChatView_Input,
  45. ESubGhzChatView_ChatBox,
  46. } ESubGhzChatView;
  47. typedef enum {
  48. ESubGhzChatEvent_FreqEntered,
  49. ESubGhzChatEvent_PassEntered,
  50. ESubGhzChatEvent_MsgEntered
  51. } ESubGhzChatEvent;
  52. static void esubghz_chat_explicit_bzero(void *s, size_t len)
  53. {
  54. memset(s, 0, len);
  55. asm volatile("" ::: "memory");
  56. }
  57. static void post_rx(ESubGhzChatState *state, size_t rx_size)
  58. {
  59. furi_assert(state);
  60. if (rx_size == 0) {
  61. return;
  62. }
  63. furi_check(rx_size <= RX_TX_BUFFER_SIZE);
  64. if (!state->encrypted) {
  65. memcpy(state->rx_str_buffer, state->rx_buffer, rx_size);
  66. state->rx_str_buffer[rx_size] = 0;
  67. } else {
  68. // TODO
  69. return;
  70. }
  71. furi_string_cat_printf(state->chat_box_store, "\n%s",
  72. state->rx_str_buffer);
  73. notification_message(state->notification, &sequence_single_vibro);
  74. }
  75. static void freq_input_cb(void *context)
  76. {
  77. furi_assert(context);
  78. ESubGhzChatState* state = context;
  79. furi_string_cat_printf(state->chat_box_store, "Frequency: %lu",
  80. state->frequency);
  81. scene_manager_handle_custom_event(state->scene_manager,
  82. ESubGhzChatEvent_FreqEntered);
  83. }
  84. static bool freq_input_validator(const char *text, FuriString *error,
  85. void *context)
  86. {
  87. furi_assert(text);
  88. furi_assert(error);
  89. furi_assert(context);
  90. ESubGhzChatState* state = context;
  91. int ret = sscanf(text, "%lu", &(state->frequency));
  92. if (ret != 1) {
  93. furi_string_printf(error, "Please enter\nfrequency\nin Hz!");
  94. return false;
  95. }
  96. if (!furi_hal_subghz_is_frequency_valid(state->frequency)) {
  97. furi_string_printf(error, "Frequency\n%lu\n is invalid!",
  98. state->frequency);
  99. return false;
  100. }
  101. if (!furi_hal_subghz_is_tx_allowed(state->frequency)) {
  102. furi_string_printf(error, "TX forbidden\non frequency\n%lu!",
  103. state->frequency);
  104. return false;
  105. }
  106. return true;
  107. }
  108. static void pass_input_cb(void *context)
  109. {
  110. furi_assert(context);
  111. ESubGhzChatState* state = context;
  112. furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
  113. (state->encrypted ? "true" : "false"));
  114. scene_manager_handle_custom_event(state->scene_manager,
  115. ESubGhzChatEvent_PassEntered);
  116. }
  117. static bool pass_input_validator(const char *text, FuriString *error,
  118. void *context)
  119. {
  120. furi_assert(text);
  121. furi_assert(error);
  122. furi_assert(context);
  123. ESubGhzChatState* state = context;
  124. if (strlen(text) == 0) {
  125. state->encrypted = false;
  126. return true;
  127. }
  128. unsigned char key[KEY_BITS / 8];
  129. state->encrypted = true;
  130. sha256((unsigned char *) text, strlen(text), key);
  131. // TODO: remove this
  132. furi_string_cat_printf(state->chat_box_store, "\nKey:");
  133. int i;
  134. for (i = 0; i < KEY_BITS / 8; i++) {
  135. furi_string_cat_printf(state->chat_box_store, " %02x", key[i]);
  136. }
  137. int ret = gcm_setkey(&(state->gcm_ctx), key, KEY_BITS / 8);
  138. esubghz_chat_explicit_bzero(key, sizeof(key));
  139. if (ret != 0) {
  140. gcm_zero_ctx(&(state->gcm_ctx));
  141. furi_string_printf(error, "Failed to\nset key!");
  142. return false;
  143. }
  144. return true;
  145. }
  146. static void chat_input_cb(void *context)
  147. {
  148. furi_assert(context);
  149. ESubGhzChatState* state = context;
  150. if (strlen(state->text_input_store) == 0) {
  151. scene_manager_handle_custom_event(state->scene_manager,
  152. ESubGhzChatEvent_MsgEntered);
  153. return;
  154. }
  155. furi_string_set(state->msg_input, state->name_prefix);
  156. furi_string_cat_str(state->msg_input, state->text_input_store);
  157. furi_string_cat_printf(state->chat_box_store, "\n%s",
  158. furi_string_get_cstr(state->msg_input));
  159. size_t msg_len = strlen(furi_string_get_cstr(state->msg_input));
  160. size_t tx_size = msg_len;
  161. if (state->encrypted) {
  162. tx_size += IV_BYTES + TAG_BYTES;
  163. furi_check(tx_size <= sizeof(state->tx_buffer));
  164. furi_hal_random_fill_buf(state->tx_buffer, IV_BYTES);
  165. gcm_crypt_and_tag(&(state->gcm_ctx), ENCRYPT,
  166. state->tx_buffer, IV_BYTES,
  167. NULL, 0,
  168. (unsigned char *)
  169. furi_string_get_cstr(state->msg_input),
  170. state->tx_buffer + IV_BYTES,
  171. msg_len,
  172. state->tx_buffer + IV_BYTES + msg_len,
  173. TAG_BYTES);
  174. } else {
  175. furi_check(tx_size <= sizeof(state->tx_buffer));
  176. memcpy(state->tx_buffer,
  177. furi_string_get_cstr(state->msg_input),
  178. tx_size);
  179. }
  180. furi_string_set_char(state->msg_input, 0, 0);
  181. // TODO: remove this
  182. furi_string_cat_printf(state->chat_box_store, "\nTXed (HEX):");
  183. size_t i;
  184. for (i = 0; i < tx_size; i++) {
  185. furi_string_cat_printf(state->chat_box_store, " %02x",
  186. state->tx_buffer[i]);
  187. }
  188. // TODO: remove this
  189. memcpy(state->rx_buffer, state->tx_buffer, tx_size);
  190. post_rx(state, tx_size);
  191. // TODO: actually transmit
  192. scene_manager_handle_custom_event(state->scene_manager,
  193. ESubGhzChatEvent_MsgEntered);
  194. }
  195. static void scene_on_enter_freq_input(void* context)
  196. {
  197. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_freq_input");
  198. furi_assert(context);
  199. ESubGhzChatState* state = context;
  200. snprintf(state->text_input_store, TEXT_INPUT_STORE_SIZE, "%lu",
  201. (uint32_t) DEFAULT_FREQ);
  202. text_input_reset(state->text_input);
  203. text_input_set_result_callback(
  204. state->text_input,
  205. freq_input_cb,
  206. state,
  207. state->text_input_store,
  208. sizeof(state->text_input_store),
  209. true);
  210. text_input_set_validator(
  211. state->text_input,
  212. freq_input_validator,
  213. state);
  214. text_input_set_header_text(
  215. state->text_input,
  216. "Frequency");
  217. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  218. }
  219. static bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
  220. {
  221. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_freq_input");
  222. furi_assert(context);
  223. ESubGhzChatState* state = context;
  224. bool consumed = false;
  225. switch(event.type) {
  226. case SceneManagerEventTypeCustom:
  227. switch(event.event) {
  228. case ESubGhzChatEvent_FreqEntered:
  229. scene_manager_next_scene(state->scene_manager,
  230. ESubGhzChatScene_PassInput);
  231. consumed = true;
  232. break;
  233. }
  234. break;
  235. case SceneManagerEventTypeBack:
  236. view_dispatcher_stop(state->view_dispatcher);
  237. consumed = true;
  238. break;
  239. default:
  240. consumed = false;
  241. break;
  242. }
  243. return consumed;
  244. }
  245. static void scene_on_exit_freq_input(void* context)
  246. {
  247. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_freq_input");
  248. furi_assert(context);
  249. ESubGhzChatState* state = context;
  250. text_input_reset(state->text_input);
  251. }
  252. static void scene_on_enter_pass_input(void* context)
  253. {
  254. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_pass_input");
  255. furi_assert(context);
  256. ESubGhzChatState* state = context;
  257. state->text_input_store[0] = 0;
  258. text_input_reset(state->text_input);
  259. text_input_set_result_callback(
  260. state->text_input,
  261. pass_input_cb,
  262. state,
  263. state->text_input_store,
  264. sizeof(state->text_input_store),
  265. true);
  266. text_input_set_validator(
  267. state->text_input,
  268. pass_input_validator,
  269. state);
  270. text_input_set_header_text(
  271. state->text_input,
  272. "Password (empty for no encr.)");
  273. text_input_set_minimum_length(state->text_input, 0);
  274. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  275. }
  276. static bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
  277. {
  278. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_pass_input");
  279. furi_assert(context);
  280. ESubGhzChatState* state = context;
  281. bool consumed = false;
  282. switch(event.type) {
  283. case SceneManagerEventTypeCustom:
  284. switch(event.event) {
  285. case ESubGhzChatEvent_PassEntered:
  286. scene_manager_next_scene(state->scene_manager,
  287. ESubGhzChatScene_ChatInput);
  288. consumed = true;
  289. break;
  290. }
  291. break;
  292. case SceneManagerEventTypeBack:
  293. view_dispatcher_stop(state->view_dispatcher);
  294. consumed = true;
  295. break;
  296. default:
  297. consumed = false;
  298. break;
  299. }
  300. return consumed;
  301. }
  302. static void scene_on_exit_pass_input(void* context)
  303. {
  304. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_pass_input");
  305. furi_assert(context);
  306. ESubGhzChatState* state = context;
  307. text_input_reset(state->text_input);
  308. }
  309. static void scene_on_enter_chat_input(void* context)
  310. {
  311. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_input");
  312. furi_assert(context);
  313. ESubGhzChatState* state = context;
  314. state->text_input_store[0] = 0;
  315. text_input_reset(state->text_input);
  316. text_input_set_result_callback(
  317. state->text_input,
  318. chat_input_cb,
  319. state,
  320. state->text_input_store,
  321. sizeof(state->text_input_store),
  322. true);
  323. text_input_set_validator(
  324. state->text_input,
  325. NULL,
  326. NULL);
  327. text_input_set_header_text(
  328. state->text_input,
  329. "Message");
  330. text_input_set_minimum_length(state->text_input, 0);
  331. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
  332. }
  333. static bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
  334. {
  335. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_input");
  336. furi_assert(context);
  337. ESubGhzChatState* state = context;
  338. bool consumed = false;
  339. switch(event.type) {
  340. case SceneManagerEventTypeCustom:
  341. switch(event.event) {
  342. case ESubGhzChatEvent_MsgEntered:
  343. scene_manager_next_scene(state->scene_manager,
  344. ESubGhzChatScene_ChatBox);
  345. consumed = true;
  346. break;
  347. }
  348. break;
  349. case SceneManagerEventTypeBack:
  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. static void scene_on_exit_chat_input(void* context)
  360. {
  361. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_input");
  362. furi_assert(context);
  363. ESubGhzChatState* state = context;
  364. text_input_reset(state->text_input);
  365. }
  366. static void scene_on_enter_chat_box(void* context)
  367. {
  368. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_chat_box");
  369. furi_assert(context);
  370. ESubGhzChatState* state = context;
  371. text_box_reset(state->chat_box);
  372. text_box_set_text(state->chat_box,
  373. furi_string_get_cstr(state->chat_box_store));
  374. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  375. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  376. }
  377. static bool scene_on_event_chat_box(void* context, SceneManagerEvent event)
  378. {
  379. UNUSED(event);
  380. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_chat_box");
  381. furi_assert(context);
  382. // TODO
  383. return false;
  384. }
  385. static void scene_on_exit_chat_box(void* context)
  386. {
  387. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_chat_box");
  388. furi_assert(context);
  389. ESubGhzChatState* state = context;
  390. text_box_reset(state->chat_box);
  391. }
  392. static void (*const esubghz_chat_scene_on_enter_handlers[])(void*) = {
  393. scene_on_enter_freq_input,
  394. scene_on_enter_pass_input,
  395. scene_on_enter_chat_input,
  396. scene_on_enter_chat_box
  397. };
  398. static bool (*const esubghz_chat_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  399. scene_on_event_freq_input,
  400. scene_on_event_pass_input,
  401. scene_on_event_chat_input,
  402. scene_on_event_chat_box
  403. };
  404. static void (*const esubghz_chat_scene_on_exit_handlers[])(void*) = {
  405. scene_on_exit_freq_input,
  406. scene_on_exit_pass_input,
  407. scene_on_exit_chat_input,
  408. scene_on_exit_chat_box
  409. };
  410. static const SceneManagerHandlers esubghz_chat_scene_event_handlers = {
  411. .on_enter_handlers = esubghz_chat_scene_on_enter_handlers,
  412. .on_event_handlers = esubghz_chat_scene_on_event_handlers,
  413. .on_exit_handlers = esubghz_chat_scene_on_exit_handlers,
  414. .scene_num = ESubGhzChatScene_MAX};
  415. static bool esubghz_chat_custom_event_callback(void* context, uint32_t event)
  416. {
  417. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_custom_event_callback");
  418. furi_assert(context);
  419. ESubGhzChatState* state = context;
  420. return scene_manager_handle_custom_event(state->scene_manager, event);
  421. }
  422. static bool esubghz_chat_navigation_event_callback(void* context)
  423. {
  424. FURI_LOG_T(APPLICATION_NAME, "esubghz_chat_navigation_event_callback");
  425. furi_assert(context);
  426. ESubGhzChatState* state = context;
  427. return scene_manager_handle_back_event(state->scene_manager);
  428. }
  429. static bool helper_strings_alloc(ESubGhzChatState *state)
  430. {
  431. furi_assert(state);
  432. state->name_prefix = furi_string_alloc();
  433. if (state->name_prefix == NULL) {
  434. return false;
  435. }
  436. state->msg_input = furi_string_alloc();
  437. if (state->msg_input == NULL) {
  438. furi_string_free(state->name_prefix);
  439. return false;
  440. }
  441. return true;
  442. }
  443. static void helper_strings_free(ESubGhzChatState *state)
  444. {
  445. furi_assert(state);
  446. furi_string_free(state->name_prefix);
  447. furi_string_free(state->msg_input);
  448. }
  449. static bool chat_box_alloc(ESubGhzChatState *state)
  450. {
  451. furi_assert(state);
  452. state->chat_box = text_box_alloc();
  453. if (state->chat_box == NULL) {
  454. return false;
  455. }
  456. state->chat_box_store = furi_string_alloc();
  457. if (state->chat_box_store == NULL) {
  458. text_box_free(state->chat_box);
  459. return false;
  460. }
  461. furi_string_reserve(state->chat_box_store, CHAT_BOX_STORE_SIZE);
  462. furi_string_set_char(state->chat_box_store, 0, 0);
  463. text_box_set_text(state->chat_box,
  464. furi_string_get_cstr(state->chat_box_store));
  465. text_box_set_focus(state->chat_box, TextBoxFocusEnd);
  466. return true;
  467. }
  468. static void chat_box_free(ESubGhzChatState *state)
  469. {
  470. furi_assert(state);
  471. text_box_free(state->chat_box);
  472. furi_string_free(state->chat_box_store);
  473. }
  474. int32_t esubghz_chat(void)
  475. {
  476. gcm_initialize();
  477. int32_t err = -1;
  478. FURI_LOG_I(APPLICATION_NAME, "Starting...");
  479. ESubGhzChatState *state = malloc(sizeof(ESubGhzChatState));
  480. if (state == NULL) {
  481. goto err_alloc;
  482. }
  483. memset(state, 0, sizeof(*state));
  484. state->scene_manager = scene_manager_alloc(
  485. &esubghz_chat_scene_event_handlers, state);
  486. if (state->scene_manager == NULL) {
  487. goto err_alloc_sm;
  488. }
  489. state->view_dispatcher = view_dispatcher_alloc();
  490. if (state->view_dispatcher == NULL) {
  491. goto err_alloc_vd;
  492. }
  493. if (!helper_strings_alloc(state)) {
  494. goto err_alloc_hs;
  495. }
  496. state->text_input = text_input_alloc();
  497. if (state->text_input == NULL) {
  498. goto err_alloc_ti;
  499. }
  500. if (!chat_box_alloc(state)) {
  501. goto err_alloc_cb;
  502. }
  503. // set chat name prefix
  504. // TODO: handle escape chars here somehow
  505. furi_string_printf(state->name_prefix, "\033[0;33m%s\033[0m: ",
  506. furi_hal_version_get_name_ptr());
  507. view_dispatcher_enable_queue(state->view_dispatcher);
  508. view_dispatcher_set_event_callback_context(state->view_dispatcher, state);
  509. view_dispatcher_set_custom_event_callback(
  510. state->view_dispatcher,
  511. esubghz_chat_custom_event_callback);
  512. view_dispatcher_set_navigation_event_callback(
  513. state->view_dispatcher,
  514. esubghz_chat_navigation_event_callback);
  515. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
  516. text_input_get_view(state->text_input));
  517. view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
  518. text_box_get_view(state->chat_box));
  519. /* no error handling here, don't know how */
  520. Gui *gui = furi_record_open(RECORD_GUI);
  521. view_dispatcher_attach_to_gui(state->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  522. /* no error handling here, don't know how */
  523. state->notification = furi_record_open(RECORD_NOTIFICATION);
  524. scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
  525. view_dispatcher_run(state->view_dispatcher);
  526. err = 0;
  527. furi_record_close(RECORD_NOTIFICATION);
  528. furi_record_close(RECORD_GUI);
  529. view_dispatcher_remove_view(state->view_dispatcher, ESubGhzChatView_Input);
  530. view_dispatcher_remove_view(state->view_dispatcher, ESubGhzChatView_ChatBox);
  531. // clear the key and potential password
  532. esubghz_chat_explicit_bzero(state->text_input_store,
  533. sizeof(state->text_input_store));
  534. gcm_zero_ctx(&(state->gcm_ctx));
  535. chat_box_free(state);
  536. err_alloc_cb:
  537. text_input_free(state->text_input);
  538. err_alloc_ti:
  539. helper_strings_free(state);
  540. err_alloc_hs:
  541. view_dispatcher_free(state->view_dispatcher);
  542. err_alloc_vd:
  543. scene_manager_free(state->scene_manager);
  544. err_alloc_sm:
  545. free(state);
  546. err_alloc:
  547. if (err != 0) {
  548. FURI_LOG_E(APPLICATION_NAME, "Failed to launch (alloc error)!");
  549. } else {
  550. FURI_LOG_I(APPLICATION_NAME, "Clean exit.");
  551. }
  552. return err;
  553. }