esubghz_chat.c 14 KB

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