Tidy up some code and patterns

This commit is contained in:
Willy-JL
2023-10-21 04:36:38 +01:00
parent 8fe0650bed
commit 9d3e347f35
13 changed files with 183 additions and 187 deletions

View File

@@ -5,7 +5,7 @@
// Documentation at https://developers.google.com/nearby/fast-pair/specifications/introduction
const struct {
uint32_t id;
uint32_t value;
const char* name;
} models[] = {
// Genuine devices
@@ -40,19 +40,18 @@ const struct {
const uint8_t models_count = COUNT_OF(models);
static const char* fastpair_get_name(const ProtocolCfg* _cfg) {
const FastpairCfg* cfg = &_cfg->fastpair;
UNUSED(cfg);
UNUSED(_cfg);
return "FastPair";
}
static void fastpair_make_packet(uint8_t* _size, uint8_t** _packet, const ProtocolCfg* _cfg) {
const FastpairCfg* cfg = _cfg ? &_cfg->fastpair : NULL;
uint32_t model_id;
if(cfg && cfg->model_id != 0x000000) {
model_id = cfg->model_id;
uint32_t model;
if(cfg && cfg->model != 0x000000) {
model = cfg->model;
} else {
model_id = models[rand() % models_count].id;
model = models[rand() % models_count].value;
}
uint8_t size = 14;
@@ -68,9 +67,9 @@ static void fastpair_make_packet(uint8_t* _size, uint8_t** _packet, const Protoc
packet[i++] = 0x16; // AD Type (Service Data)
packet[i++] = 0x2C; // Service UUID (Google LLC, FastPair)
packet[i++] = 0xFE; // ...
packet[i++] = (model_id >> 0x10) & 0xFF; // Model ID
packet[i++] = (model_id >> 0x08) & 0xFF; // ...
packet[i++] = (model_id >> 0x00) & 0xFF; // ...
packet[i++] = (model >> 0x10) & 0xFF;
packet[i++] = (model >> 0x08) & 0xFF;
packet[i++] = (model >> 0x00) & 0xFF;
packet[i++] = 2; // Size
packet[i++] = 0x0A; // AD Type (Tx Power Level)
@@ -82,27 +81,27 @@ static void fastpair_make_packet(uint8_t* _size, uint8_t** _packet, const Protoc
enum {
_ConfigExtraStart = ConfigExtraStart,
ConfigModelId,
ConfigModel,
};
static void config_callback(void* _ctx, uint32_t index) {
Ctx* ctx = _ctx;
scene_manager_set_scene_state(ctx->scene_manager, SceneConfig, index);
switch(index) {
case ConfigModelId:
scene_manager_next_scene(ctx->scene_manager, SceneFastpairModelId);
case ConfigModel:
scene_manager_next_scene(ctx->scene_manager, SceneFastpairModel);
default:
break;
}
}
static void model_id_changed(VariableItem* item) {
static void model_changed(VariableItem* item) {
FastpairCfg* cfg = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
if(index) {
index--;
cfg->model_id = models[index].id;
cfg->model = models[index].value;
variable_item_set_current_value_text(item, models[index].name);
} else {
cfg->model_id = 0x000000;
cfg->model = 0x000000;
variable_item_set_current_value_text(item, "Random");
}
}
@@ -112,22 +111,22 @@ static void fastpair_extra_config(Ctx* ctx) {
VariableItem* item;
size_t value_index;
item = variable_item_list_add(list, "Model ID", models_count + 1, model_id_changed, cfg);
item = variable_item_list_add(list, "Model Code", models_count + 1, model_changed, cfg);
const char* model_name = NULL;
char model_name_buf[9];
if(cfg->model_id == 0x000000) {
if(cfg->model == 0x000000) {
model_name = "Random";
value_index = 0;
} else {
for(uint8_t i = 0; i < models_count; i++) {
if(cfg->model_id == models[i].id) {
if(cfg->model == models[i].value) {
model_name = models[i].name;
value_index = i + 1;
break;
}
}
if(!model_name) {
snprintf(model_name_buf, sizeof(model_name_buf), "%06lX", cfg->model_id);
snprintf(model_name_buf, sizeof(model_name_buf), "%06lX", cfg->model);
model_name = model_name_buf;
value_index = models_count + 1;
}
@@ -147,24 +146,24 @@ const Protocol protocol_fastpair = {
.extra_config = fastpair_extra_config,
};
static void model_id_callback(void* _ctx, uint32_t index) {
static void model_callback(void* _ctx, uint32_t index) {
Ctx* ctx = _ctx;
FastpairCfg* cfg = &ctx->attack->payload.cfg.fastpair;
switch(index) {
case 0:
cfg->model_id = 0x000000;
cfg->model = 0x000000;
scene_manager_previous_scene(ctx->scene_manager);
break;
case models_count + 1:
scene_manager_next_scene(ctx->scene_manager, SceneFastpairModelIdCustom);
scene_manager_next_scene(ctx->scene_manager, SceneFastpairModelCustom);
break;
default:
cfg->model_id = models[index - 1].id;
cfg->model = models[index - 1].value;
scene_manager_previous_scene(ctx->scene_manager);
break;
}
}
void scene_fastpair_model_id_on_enter(void* _ctx) {
void scene_fastpair_model_on_enter(void* _ctx) {
Ctx* ctx = _ctx;
FastpairCfg* cfg = &ctx->attack->payload.cfg.fastpair;
Submenu* submenu = ctx->submenu;
@@ -172,19 +171,19 @@ void scene_fastpair_model_id_on_enter(void* _ctx) {
bool found = false;
submenu_reset(submenu);
submenu_add_item(submenu, "Random", 0, model_id_callback, ctx);
if(cfg->model_id == 0x000000) {
submenu_add_item(submenu, "Random", 0, model_callback, ctx);
if(cfg->model == 0x000000) {
found = true;
selected = 0;
}
for(uint8_t i = 0; i < models_count; i++) {
submenu_add_item(submenu, models[i].name, i + 1, model_id_callback, ctx);
if(!found && cfg->model_id == models[i].id) {
submenu_add_item(submenu, models[i].name, i + 1, model_callback, ctx);
if(!found && cfg->model == models[i].value) {
found = true;
selected = i + 1;
}
}
submenu_add_item(submenu, "Custom", models_count + 1, model_id_callback, ctx);
submenu_add_item(submenu, "Custom", models_count + 1, model_callback, ctx);
if(!found) {
found = true;
selected = models_count + 1;
@@ -194,44 +193,44 @@ void scene_fastpair_model_id_on_enter(void* _ctx) {
view_dispatcher_switch_to_view(ctx->view_dispatcher, ViewSubmenu);
}
bool scene_fastpair_model_id_on_event(void* _ctx, SceneManagerEvent event) {
bool scene_fastpair_model_on_event(void* _ctx, SceneManagerEvent event) {
UNUSED(_ctx);
UNUSED(event);
return false;
}
void scene_fastpair_model_id_on_exit(void* _ctx) {
void scene_fastpair_model_on_exit(void* _ctx) {
UNUSED(_ctx);
}
static void model_id_custom_callback(void* _ctx) {
static void model_custom_callback(void* _ctx) {
Ctx* ctx = _ctx;
scene_manager_previous_scene(ctx->scene_manager);
scene_manager_previous_scene(ctx->scene_manager);
}
void scene_fastpair_model_id_custom_on_enter(void* _ctx) {
void scene_fastpair_model_custom_on_enter(void* _ctx) {
Ctx* ctx = _ctx;
FastpairCfg* cfg = &ctx->attack->payload.cfg.fastpair;
ByteInput* byte_input = ctx->byte_input;
byte_input_set_header_text(byte_input, "Enter custom Model ID");
byte_input_set_header_text(byte_input, "Enter custom Model Code");
ctx->byte_store[0] = (cfg->model_id >> 0x10) & 0xFF;
ctx->byte_store[1] = (cfg->model_id >> 0x08) & 0xFF;
ctx->byte_store[2] = (cfg->model_id >> 0x00) & 0xFF;
ctx->byte_store[0] = (cfg->model >> 0x10) & 0xFF;
ctx->byte_store[1] = (cfg->model >> 0x08) & 0xFF;
ctx->byte_store[2] = (cfg->model >> 0x00) & 0xFF;
byte_input_set_result_callback(
byte_input, model_id_custom_callback, NULL, ctx, (void*)ctx->byte_store, 3);
byte_input, model_custom_callback, NULL, ctx, (void*)ctx->byte_store, 3);
view_dispatcher_switch_to_view(ctx->view_dispatcher, ViewByteInput);
}
bool scene_fastpair_model_id_custom_on_event(void* _ctx, SceneManagerEvent event) {
bool scene_fastpair_model_custom_on_event(void* _ctx, SceneManagerEvent event) {
UNUSED(_ctx);
UNUSED(event);
return false;
}
void scene_fastpair_model_id_custom_on_exit(void* _ctx) {
void scene_fastpair_model_custom_on_exit(void* _ctx) {
Ctx* ctx = _ctx;
FastpairCfg* cfg = &ctx->attack->payload.cfg.fastpair;
cfg->model_id =
cfg->model =
(ctx->byte_store[0] << 0x10) + (ctx->byte_store[1] << 0x08) + (ctx->byte_store[2] << 0x00);
}