BLE Spam refactor random/set/custom mode handling

This commit is contained in:
Willy-JL
2023-10-25 00:02:36 +01:00
parent 60ec667178
commit 0f3fbd1cd7
7 changed files with 277 additions and 185 deletions

View File

@@ -69,7 +69,7 @@ static const char* type_names[EasysetupTypeCOUNT] = {
[EasysetupTypeWatch] = "EasySetup Watch",
};
static const char* get_name(const ProtocolCfg* _cfg) {
const EasysetupCfg* cfg = &_cfg->easysetup;
const EasysetupCfg* cfg = &_cfg->specific.easysetup;
return type_names[cfg->type];
}
@@ -77,8 +77,8 @@ static uint8_t packet_sizes[EasysetupTypeCOUNT] = {
[EasysetupTypeBuds] = 31,
[EasysetupTypeWatch] = 15,
};
void make_packet(uint8_t* out_size, uint8_t** out_packet, const ProtocolCfg* _cfg) {
const EasysetupCfg* cfg = _cfg ? &_cfg->easysetup : NULL;
void make_packet(uint8_t* out_size, uint8_t** out_packet, ProtocolCfg* _cfg) {
EasysetupCfg* cfg = _cfg ? &_cfg->specific.easysetup : NULL;
EasysetupType type;
if(cfg && cfg->type != 0x00) {
@@ -98,10 +98,14 @@ void make_packet(uint8_t* out_size, uint8_t** out_packet, const ProtocolCfg* _cf
switch(type) {
case EasysetupTypeBuds: {
uint32_t model;
if(cfg && cfg->data.buds.model != 0x000000) {
model = cfg->data.buds.model;
} else {
switch(cfg ? _cfg->mode : ProtocolModeRandom) {
case ProtocolModeRandom:
default:
model = buds_models[rand() % buds_models_count].value;
break;
case ProtocolModeValue:
model = cfg->data.buds.model;
break;
}
packet[i++] = 27; // Size
@@ -141,10 +145,14 @@ void make_packet(uint8_t* out_size, uint8_t** out_packet, const ProtocolCfg* _cf
}
case EasysetupTypeWatch: {
uint8_t model;
if(cfg && cfg->data.watch.model != 0x00) {
model = cfg->data.watch.model;
} else {
switch(cfg ? _cfg->mode : ProtocolModeRandom) {
case ProtocolModeRandom:
default:
model = watch_models[rand() % watch_models_count].value;
break;
case ProtocolModeValue:
model = cfg->data.watch.model;
break;
}
packet[i++] = 14; // Size
@@ -185,7 +193,8 @@ enum {
};
static void config_callback(void* _ctx, uint32_t index) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
scene_manager_set_scene_state(ctx->scene_manager, SceneConfig, index);
switch(cfg->type) {
case EasysetupTypeBuds: {
@@ -218,31 +227,36 @@ static void config_callback(void* _ctx, uint32_t index) {
}
}
static void buds_model_changed(VariableItem* item) {
EasysetupCfg* cfg = variable_item_get_context(item);
ProtocolCfg* _cfg = variable_item_get_context(item);
EasysetupCfg* cfg = &_cfg->specific.easysetup;
uint8_t index = variable_item_get_current_value_index(item);
if(index) {
index--;
_cfg->mode = ProtocolModeValue;
cfg->data.buds.model = buds_models[index].value;
variable_item_set_current_value_text(item, buds_models[index].name);
} else {
cfg->data.buds.model = 0x000000;
_cfg->mode = ProtocolModeRandom;
variable_item_set_current_value_text(item, "Random");
}
}
static void watch_model_changed(VariableItem* item) {
EasysetupCfg* cfg = variable_item_get_context(item);
ProtocolCfg* _cfg = variable_item_get_context(item);
EasysetupCfg* cfg = &_cfg->specific.easysetup;
uint8_t index = variable_item_get_current_value_index(item);
if(index) {
index--;
_cfg->mode = ProtocolModeValue;
cfg->data.watch.model = watch_models[index].value;
variable_item_set_current_value_text(item, watch_models[index].name);
} else {
cfg->data.watch.model = 0x00;
_cfg->mode = ProtocolModeRandom;
variable_item_set_current_value_text(item, "Random");
}
}
static void extra_config(Ctx* ctx) {
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
VariableItemList* list = ctx->variable_item_list;
VariableItem* item;
size_t value_index;
@@ -250,13 +264,16 @@ static void extra_config(Ctx* ctx) {
switch(cfg->type) {
case EasysetupTypeBuds: {
item = variable_item_list_add(
list, "Model Code", buds_models_count + 1, buds_model_changed, cfg);
list, "Model Code", buds_models_count + 1, buds_model_changed, _cfg);
const char* model_name = NULL;
char model_name_buf[9];
if(cfg->data.buds.model == 0x000000) {
switch(_cfg->mode) {
case ProtocolModeRandom:
default:
model_name = "Random";
value_index = 0;
} else {
break;
case ProtocolModeValue:
for(uint8_t i = 0; i < buds_models_count; i++) {
if(cfg->data.buds.model == buds_models[i].value) {
model_name = buds_models[i].name;
@@ -269,6 +286,7 @@ static void extra_config(Ctx* ctx) {
model_name = model_name_buf;
value_index = buds_models_count + 1;
}
break;
}
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, model_name);
@@ -278,13 +296,16 @@ static void extra_config(Ctx* ctx) {
}
case EasysetupTypeWatch: {
item = variable_item_list_add(
list, "Model Code", watch_models_count + 1, watch_model_changed, cfg);
list, "Model Code", watch_models_count + 1, watch_model_changed, _cfg);
const char* model_name = NULL;
char model_name_buf[3];
if(cfg->data.watch.model == 0x00) {
switch(_cfg->mode) {
case ProtocolModeRandom:
default:
model_name = "Random";
value_index = 0;
} else {
break;
case ProtocolModeValue:
for(uint8_t i = 0; i < watch_models_count; i++) {
if(cfg->data.watch.model == watch_models[i].value) {
model_name = watch_models[i].name;
@@ -297,6 +318,7 @@ static void extra_config(Ctx* ctx) {
model_name = model_name_buf;
value_index = watch_models_count + 1;
}
break;
}
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, model_name);
@@ -314,7 +336,7 @@ static uint8_t config_counts[EasysetupTypeCOUNT] = {
[EasysetupTypeWatch] = ConfigWatchCOUNT - ConfigExtraStart - 1,
};
static uint8_t config_count(const ProtocolCfg* _cfg) {
const EasysetupCfg* cfg = &_cfg->easysetup;
const EasysetupCfg* cfg = &_cfg->specific.easysetup;
return config_counts[cfg->type];
}
@@ -328,16 +350,18 @@ const Protocol protocol_easysetup = {
static void buds_model_callback(void* _ctx, uint32_t index) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
switch(index) {
case 0:
cfg->data.buds.model = 0x000000;
_cfg->mode = ProtocolModeRandom;
scene_manager_previous_scene(ctx->scene_manager);
break;
case buds_models_count + 1:
scene_manager_next_scene(ctx->scene_manager, SceneEasysetupBudsModelCustom);
break;
default:
_cfg->mode = ProtocolModeValue;
cfg->data.buds.model = buds_models[index - 1].value;
scene_manager_previous_scene(ctx->scene_manager);
break;
@@ -345,27 +369,28 @@ static void buds_model_callback(void* _ctx, uint32_t index) {
}
void scene_easysetup_buds_model_on_enter(void* _ctx) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
Submenu* submenu = ctx->submenu;
uint32_t selected = 0;
bool found = false;
submenu_reset(submenu);
submenu_add_item(submenu, "Random", 0, buds_model_callback, ctx);
if(cfg->data.buds.model == 0x000000) {
found = true;
if(_cfg->mode == ProtocolModeRandom) {
selected = 0;
}
bool found = false;
for(uint8_t i = 0; i < buds_models_count; i++) {
submenu_add_item(submenu, buds_models[i].name, i + 1, buds_model_callback, ctx);
if(!found && cfg->data.buds.model == buds_models[i].value) {
if(!found && _cfg->mode == ProtocolModeValue &&
cfg->data.buds.model == buds_models[i].value) {
found = true;
selected = i + 1;
}
}
submenu_add_item(submenu, "Custom", buds_models_count + 1, buds_model_callback, ctx);
if(!found) {
found = true;
if(!found && _cfg->mode == ProtocolModeValue) {
selected = buds_models_count + 1;
}
@@ -384,7 +409,9 @@ void scene_easysetup_buds_model_on_exit(void* _ctx) {
static void buds_model_custom_callback(void* _ctx) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
_cfg->mode = ProtocolModeValue;
cfg->data.buds.model =
(ctx->byte_store[0] << 0x10) + (ctx->byte_store[1] << 0x08) + (ctx->byte_store[2] << 0x00);
scene_manager_previous_scene(ctx->scene_manager);
@@ -392,7 +419,8 @@ static void buds_model_custom_callback(void* _ctx) {
}
void scene_easysetup_buds_model_custom_on_enter(void* _ctx) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
ByteInput* byte_input = ctx->byte_input;
byte_input_set_header_text(byte_input, "Enter custom Model Code");
@@ -417,16 +445,18 @@ void scene_easysetup_buds_model_custom_on_exit(void* _ctx) {
static void watch_model_callback(void* _ctx, uint32_t index) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
switch(index) {
case 0:
cfg->data.watch.model = 0x00;
_cfg->mode = ProtocolModeRandom;
scene_manager_previous_scene(ctx->scene_manager);
break;
case watch_models_count + 1:
scene_manager_next_scene(ctx->scene_manager, SceneEasysetupWatchModelCustom);
break;
default:
_cfg->mode = ProtocolModeValue;
cfg->data.watch.model = watch_models[index - 1].value;
scene_manager_previous_scene(ctx->scene_manager);
break;
@@ -434,27 +464,28 @@ static void watch_model_callback(void* _ctx, uint32_t index) {
}
void scene_easysetup_watch_model_on_enter(void* _ctx) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
Submenu* submenu = ctx->submenu;
uint32_t selected = 0;
bool found = false;
submenu_reset(submenu);
submenu_add_item(submenu, "Random", 0, watch_model_callback, ctx);
if(cfg->data.watch.model == 0x00) {
found = true;
if(_cfg->mode == ProtocolModeRandom) {
selected = 0;
}
bool found = false;
for(uint8_t i = 0; i < watch_models_count; i++) {
submenu_add_item(submenu, watch_models[i].name, i + 1, watch_model_callback, ctx);
if(!found && cfg->data.watch.model == watch_models[i].value) {
if(!found && _cfg->mode == ProtocolModeValue &&
cfg->data.watch.model == watch_models[i].value) {
found = true;
selected = i + 1;
}
}
submenu_add_item(submenu, "Custom", watch_models_count + 1, watch_model_callback, ctx);
if(!found) {
found = true;
if(!found && _cfg->mode == ProtocolModeValue) {
selected = watch_models_count + 1;
}
@@ -473,14 +504,17 @@ void scene_easysetup_watch_model_on_exit(void* _ctx) {
static void watch_model_custom_callback(void* _ctx) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
_cfg->mode = ProtocolModeValue;
cfg->data.watch.model = (ctx->byte_store[0] << 0x00);
scene_manager_previous_scene(ctx->scene_manager);
scene_manager_previous_scene(ctx->scene_manager);
}
void scene_easysetup_watch_model_custom_on_enter(void* _ctx) {
Ctx* ctx = _ctx;
EasysetupCfg* cfg = &ctx->attack->payload.cfg.easysetup;
ProtocolCfg* _cfg = &ctx->attack->payload.cfg;
EasysetupCfg* cfg = &_cfg->specific.easysetup;
ByteInput* byte_input = ctx->byte_input;
byte_input_set_header_text(byte_input, "Enter custom Model Code");