esubghz_chat.c 14 KB

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