mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-13 15:48:35 -07:00
BLE Spam refactor namespace and use viewdispatcher
This commit is contained in:
126
applications/external/ble_spam/ble_spam.c
vendored
126
applications/external/ble_spam/ble_spam.c
vendored
@@ -1,6 +1,7 @@
|
||||
#include <gui/gui.h>
|
||||
#include <furi_hal_bt.h>
|
||||
#include <gui/elements.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
|
||||
#include "protocols/_registry.h"
|
||||
|
||||
@@ -14,8 +15,8 @@
|
||||
typedef struct {
|
||||
const char* title;
|
||||
const char* text;
|
||||
const BleSpamProtocol* protocol;
|
||||
BleSpamPayload payload;
|
||||
const Protocol* protocol;
|
||||
Payload payload;
|
||||
} Attack;
|
||||
|
||||
static Attack attacks[] = {
|
||||
@@ -32,7 +33,7 @@ static Attack attacks[] = {
|
||||
{
|
||||
.title = "iOS 17 Lockup Crash",
|
||||
.text = "Newer iPhones, long range",
|
||||
.protocol = &ble_spam_protocol_continuity,
|
||||
.protocol = &protocol_continuity,
|
||||
.payload =
|
||||
{
|
||||
.random_mac = false,
|
||||
@@ -49,7 +50,7 @@ static Attack attacks[] = {
|
||||
{
|
||||
.title = "Apple Action Modal",
|
||||
.text = "Lock cooldown, long range",
|
||||
.protocol = &ble_spam_protocol_continuity,
|
||||
.protocol = &protocol_continuity,
|
||||
.payload =
|
||||
{
|
||||
.random_mac = false,
|
||||
@@ -66,7 +67,7 @@ static Attack attacks[] = {
|
||||
{
|
||||
.title = "Apple Device Popup",
|
||||
.text = "No cooldown, close range",
|
||||
.protocol = &ble_spam_protocol_continuity,
|
||||
.protocol = &protocol_continuity,
|
||||
.payload =
|
||||
{
|
||||
.random_mac = false,
|
||||
@@ -83,7 +84,7 @@ static Attack attacks[] = {
|
||||
{
|
||||
.title = "Android Device Pair",
|
||||
.text = "Reboot cooldown, long range",
|
||||
.protocol = &ble_spam_protocol_fastpair,
|
||||
.protocol = &protocol_fastpair,
|
||||
.payload =
|
||||
{
|
||||
.random_mac = true,
|
||||
@@ -96,7 +97,7 @@ static Attack attacks[] = {
|
||||
{
|
||||
.title = "Windows Device Found",
|
||||
.text = "Requires enabling SwiftPair",
|
||||
.protocol = &ble_spam_protocol_swiftpair,
|
||||
.protocol = &protocol_swiftpair,
|
||||
.payload =
|
||||
{
|
||||
.random_mac = true,
|
||||
@@ -108,7 +109,7 @@ static Attack attacks[] = {
|
||||
},
|
||||
};
|
||||
|
||||
#define ATTACK_COUNT ((signed)COUNT_OF(attacks))
|
||||
#define ATTACKS_COUNT ((signed)COUNT_OF(attacks))
|
||||
|
||||
uint16_t delays[] = {20, 50, 100, 200};
|
||||
|
||||
@@ -126,16 +127,15 @@ static int32_t adv_thread(void* ctx) {
|
||||
uint16_t delay;
|
||||
uint8_t* packet;
|
||||
uint8_t mac[GAP_MAC_ADDR_SIZE];
|
||||
BleSpamPayload* payload = &attacks[state->index].payload;
|
||||
const BleSpamProtocol* protocol = attacks[state->index].protocol;
|
||||
Payload* payload = &attacks[state->index].payload;
|
||||
const Protocol* protocol = attacks[state->index].protocol;
|
||||
if(!payload->random_mac) furi_hal_random_fill_buf(mac, sizeof(mac));
|
||||
|
||||
while(state->advertising) {
|
||||
if(protocol) {
|
||||
protocol->make_packet(&size, &packet, &payload->cfg);
|
||||
} else {
|
||||
ble_spam_protocols[rand() % ble_spam_protocols_count]->make_packet(
|
||||
&size, &packet, NULL);
|
||||
protocols[rand() % protocols_count]->make_packet(&size, &packet, NULL);
|
||||
}
|
||||
furi_hal_bt_custom_adv_set(packet, size);
|
||||
free(packet);
|
||||
@@ -164,19 +164,23 @@ static void toggle_adv(State* state) {
|
||||
}
|
||||
}
|
||||
|
||||
enum {
|
||||
ViewMain,
|
||||
};
|
||||
|
||||
#define PAGE_MIN (-3)
|
||||
#define PAGE_MAX ATTACK_COUNT
|
||||
#define PAGE_MAX ATTACKS_COUNT
|
||||
enum {
|
||||
PageHelpApps = PAGE_MIN,
|
||||
PageHelpDelay,
|
||||
PageHelpDistance,
|
||||
PageStart = 0,
|
||||
PageEnd = ATTACK_COUNT - 1,
|
||||
PageEnd = ATTACKS_COUNT - 1,
|
||||
PageAboutCredits = PAGE_MAX,
|
||||
};
|
||||
|
||||
static void draw_callback(Canvas* canvas, void* ctx) {
|
||||
State* state = ctx;
|
||||
State* state = *(State**)ctx;
|
||||
const char* back = "Back";
|
||||
const char* next = "Next";
|
||||
switch(state->index) {
|
||||
@@ -195,9 +199,9 @@ static void draw_callback(Canvas* canvas, void* ctx) {
|
||||
}
|
||||
|
||||
const Attack* attack =
|
||||
(state->index >= 0 && state->index <= ATTACK_COUNT - 1) ? &attacks[state->index] : NULL;
|
||||
const BleSpamPayload* payload = attack ? &attack->payload : NULL;
|
||||
const BleSpamProtocol* protocol = attack ? attack->protocol : NULL;
|
||||
(state->index >= 0 && state->index <= ATTACKS_COUNT - 1) ? &attacks[state->index] : NULL;
|
||||
const Payload* payload = attack ? &attack->payload : NULL;
|
||||
const Protocol* protocol = attack ? attack->protocol : NULL;
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_icon(canvas, 4, 3, protocol ? protocol->icon : &I_ble);
|
||||
@@ -285,7 +289,7 @@ static void draw_callback(Canvas* canvas, void* ctx) {
|
||||
sizeof(str),
|
||||
"%02i/%02i: %s",
|
||||
state->index + 1,
|
||||
ATTACK_COUNT,
|
||||
ATTACKS_COUNT,
|
||||
protocol ? protocol->get_name(&payload->cfg) : "Everything");
|
||||
canvas_draw_str(canvas, 4 - (state->index < 19 ? 1 : 0), 21, str);
|
||||
|
||||
@@ -308,37 +312,19 @@ static void draw_callback(Canvas* canvas, void* ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static void input_callback(InputEvent* input, void* ctx) {
|
||||
FuriMessageQueue* input_queue = ctx;
|
||||
static bool input_callback(InputEvent* input, void* ctx) {
|
||||
View* view = ctx;
|
||||
State* state = *(State**)view_get_model(view);
|
||||
bool consumed = false;
|
||||
|
||||
if(input->type == InputTypeShort || input->type == InputTypeLong ||
|
||||
input->type == InputTypeRepeat) {
|
||||
furi_message_queue_put(input_queue, input, 0);
|
||||
}
|
||||
}
|
||||
consumed = true;
|
||||
|
||||
int32_t ble_spam(void* p) {
|
||||
UNUSED(p);
|
||||
State* state = malloc(sizeof(State));
|
||||
state->thread = furi_thread_alloc();
|
||||
furi_thread_set_callback(state->thread, adv_thread);
|
||||
furi_thread_set_context(state->thread, state);
|
||||
furi_thread_set_stack_size(state->thread, 4096);
|
||||
|
||||
FuriMessageQueue* input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
|
||||
ViewPort* view_port = view_port_alloc();
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
view_port_input_callback_set(view_port, input_callback, input_queue);
|
||||
view_port_draw_callback_set(view_port, draw_callback, state);
|
||||
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
|
||||
|
||||
bool running = true;
|
||||
while(running) {
|
||||
InputEvent input;
|
||||
furi_check(furi_message_queue_get(input_queue, &input, FuriWaitForever) == FuriStatusOk);
|
||||
|
||||
bool is_attack = state->index >= 0 && state->index <= ATTACK_COUNT - 1;
|
||||
bool is_attack = state->index >= 0 && state->index <= ATTACKS_COUNT - 1;
|
||||
bool advertising = state->advertising;
|
||||
switch(input.key) {
|
||||
|
||||
switch(input->key) {
|
||||
case InputKeyOk:
|
||||
if(is_attack) toggle_adv(state);
|
||||
break;
|
||||
@@ -366,19 +352,53 @@ int32_t ble_spam(void* p) {
|
||||
break;
|
||||
case InputKeyBack:
|
||||
if(advertising) toggle_adv(state);
|
||||
running = false;
|
||||
consumed = false;
|
||||
break;
|
||||
default:
|
||||
continue;
|
||||
break;
|
||||
}
|
||||
|
||||
view_port_update(view_port);
|
||||
}
|
||||
|
||||
gui_remove_view_port(gui, view_port);
|
||||
view_commit_model(view, consumed);
|
||||
return consumed;
|
||||
}
|
||||
|
||||
static uint32_t exit_callback(void* ctx) {
|
||||
UNUSED(ctx);
|
||||
return VIEW_NONE;
|
||||
}
|
||||
|
||||
int32_t ble_spam(void* p) {
|
||||
UNUSED(p);
|
||||
State* state = malloc(sizeof(State));
|
||||
state->thread = furi_thread_alloc();
|
||||
furi_thread_set_callback(state->thread, adv_thread);
|
||||
furi_thread_set_context(state->thread, state);
|
||||
furi_thread_set_stack_size(state->thread, 4096);
|
||||
|
||||
Gui* gui = furi_record_open(RECORD_GUI);
|
||||
ViewDispatcher* view_dispatcher = view_dispatcher_alloc();
|
||||
view_dispatcher_enable_queue(view_dispatcher);
|
||||
view_dispatcher_set_event_callback_context(view_dispatcher, state);
|
||||
|
||||
View* view = view_alloc();
|
||||
view_allocate_model(view, ViewModelTypeLockFree, sizeof(State*));
|
||||
with_view_model(
|
||||
view, State * *model, { *model = state; }, false);
|
||||
view_set_context(view, view);
|
||||
view_set_draw_callback(view, draw_callback);
|
||||
view_set_input_callback(view, input_callback);
|
||||
view_set_previous_callback(view, exit_callback);
|
||||
view_dispatcher_add_view(view_dispatcher, ViewMain, view);
|
||||
|
||||
view_dispatcher_attach_to_gui(view_dispatcher, gui, ViewDispatcherTypeFullscreen);
|
||||
view_dispatcher_switch_to_view(view_dispatcher, ViewMain);
|
||||
view_dispatcher_run(view_dispatcher);
|
||||
|
||||
view_dispatcher_remove_view(view_dispatcher, ViewMain);
|
||||
view_free(view);
|
||||
view_dispatcher_free(view_dispatcher);
|
||||
furi_record_close(RECORD_GUI);
|
||||
view_port_free(view_port);
|
||||
furi_message_queue_free(input_queue);
|
||||
|
||||
furi_thread_free(state->thread);
|
||||
free(state);
|
||||
|
||||
Reference in New Issue
Block a user