Skip to content

Commit

Permalink
FindMy: Refactors and improvements
Browse files Browse the repository at this point in the history
- state handled reparately, decoupled from app
- better apple/samsung/unknown parsing and handling
- improve error handling
- add url to manifest + up stack size just in case
  • Loading branch information
Willy-JL committed Mar 8, 2024
1 parent 09af215 commit 7c48c61
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 106 deletions.
4 changes: 2 additions & 2 deletions applications/system/findmy/application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ App(
apptype=FlipperAppType.EXTERNAL,
entry_point="findmy_main",
requires=["gui"],
stack_size=1 * 1024,
order=35,
stack_size=2 * 1024,
fap_icon="location_icon.png",
fap_category="Bluetooth",
fap_author="@MatthewKuKanich",
fap_weburl="https://github.com/MatthewKuKanich/FindMyFlipper",
fap_version="1.0",
fap_description="BLE FindMy Location Beacon",
)
100 changes: 38 additions & 62 deletions applications/system/findmy/findmy.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ static FindMy* findmy_app_alloc() {

view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);

app->beacon_active = false;
findmy_main_update_active(app->findmy_main, app->beacon_active);
app->broadcast_interval = 5;
findmy_main_update_interval(app->findmy_main, app->broadcast_interval);
app->transmit_power = 6;
app->apple = true;
findmy_main_update_apple(app->findmy_main, app->apple);
findmy_state_load(&app->state);
findmy_state_apply(&app->state);

findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
findmy_main_update_type(app->findmy_main, findmy_data_get_type(app->state.data));

return app;
}
Expand All @@ -74,47 +73,10 @@ static void findmy_app_free(FindMy* app) {
free(app);
}

static void findmy_start(FindMy* app) {
furi_hal_bt_extra_beacon_stop(); // Stop any running beacon

app->config.min_adv_interval_ms = app->broadcast_interval * 1000; // Converting s to ms
app->config.max_adv_interval_ms = (app->broadcast_interval * 1000) + 150;
app->config.adv_channel_map = GapAdvChannelMapAll;
app->config.adv_power_level = GapAdvPowerLevel_0dBm + app->transmit_power;
app->config.address_type = GapAddressTypePublic;

uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
furi_hal_bt_reverse_mac_addr(mac);
memcpy(&app->config.address, mac, sizeof(app->config.address));
furi_check(furi_hal_bt_extra_beacon_set_config(&app->config));

uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE];
uint8_t* it = data;

// For Apple AirTags
*it++ = 0x1E; // Length
*it++ = 0xFF; // Manufacturer Specific Data
*it++ = 0x4C; // Company ID (Apple, Inc.)
*it++ = 0x00; // ...
*it++ = 0x12; // Type (FindMy)
*it++ = 0x19; // Length
*it++ = 0x00; // Status
// Placeholder Empty Public Key without the MAC address
for(size_t i = 0; i < 22; ++i) {
*it++ = 0x00;
}
*it++ = 0x00; // First 2 bits are the version, the rest is the battery level
*it++ = 0x00; // Hint (0x00)

furi_check(furi_hal_bt_extra_beacon_set_data(data, it - data));
}

int32_t findmy_main(void* p) {
UNUSED(p);
FindMy* app = findmy_app_alloc();

findmy_start(app);

scene_manager_next_scene(app->scene_manager, FindMySceneMain);

view_dispatcher_run(app->view_dispatcher);
Expand All @@ -127,16 +89,16 @@ void findmy_change_broadcast_interval(FindMy* app, uint8_t value) {
if(value > 10 || value < 1) {
return;
}
app->broadcast_interval = value;
findmy_main_update_interval(app->findmy_main, app->broadcast_interval);
if(app->beacon_active) {
app->state.broadcast_interval = value;
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
if(furi_hal_bt_extra_beacon_is_active()) {
// Always check if beacon is active before changing config
furi_check(furi_hal_bt_extra_beacon_stop());
}
app->config.min_adv_interval_ms = app->broadcast_interval * 1000;
app->config.max_adv_interval_ms = app->config.min_adv_interval_ms + 150;
furi_check(furi_hal_bt_extra_beacon_set_config(&app->config));
if(app->beacon_active) {
app->state.config.min_adv_interval_ms = app->state.broadcast_interval * 1000;
app->state.config.max_adv_interval_ms = app->state.config.min_adv_interval_ms + 150;
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
if(app->state.beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
}
}
Expand All @@ -145,24 +107,38 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) {
if(value > 6) {
return;
}
app->transmit_power = value;
if(app->beacon_active) {
app->state.transmit_power = value;
if(furi_hal_bt_extra_beacon_is_active()) {
furi_check(furi_hal_bt_extra_beacon_stop());
}
app->config.adv_power_level = GapAdvPowerLevel_0dBm + app->transmit_power;
furi_check(furi_hal_bt_extra_beacon_set_config(&app->config));
if(app->beacon_active) {
app->state.config.adv_power_level = GapAdvPowerLevel_0dBm + app->state.transmit_power;
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
if(app->state.beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
}
}

void findmy_toggle_beacon(FindMy* app) {
app->beacon_active = !app->beacon_active;
findmy_main_update_active(app->findmy_main, app->beacon_active);
findmy_main_update_apple(app->findmy_main, app->apple);
if(app->beacon_active) {
furi_hal_bt_extra_beacon_start();
app->state.beacon_active = !app->state.beacon_active;
if(furi_hal_bt_extra_beacon_is_active()) {
furi_check(furi_hal_bt_extra_beacon_stop());
}
if(app->state.beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
}
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
}

FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]) {
if(data[0] == 0x1E && // Length
data[1] == 0xFF && // Manufacturer Specific Data
data[2] == 0x4C && // Company ID (Apple, Inc.)
data[3] == 0x00 && // ...
data[4] == 0x12 && // Type (FindMy)
data[5] == 0x19 // Length
) {
return FindMyTypeApple;
} else {
furi_hal_bt_extra_beacon_stop();
return FindMyTypeSamsung;
}
}
2 changes: 2 additions & 0 deletions applications/system/findmy/findmy.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

typedef struct FindMy FindMy;

typedef enum FindMyType FindMyType;
13 changes: 8 additions & 5 deletions applications/system/findmy/findmy_i.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "findmy.h"
#include "findmy_state.h"
#include <furi_hal_bt.h>
#include <extra_beacon.h>
#include <assets_icons.h>
Expand All @@ -24,11 +25,7 @@ struct FindMy {
uint8_t mac_buf[EXTRA_BEACON_MAC_ADDR_SIZE];
uint8_t packet_buf[EXTRA_BEACON_MAX_DATA_SIZE];

GapExtraBeaconConfig config;
bool apple;
bool beacon_active;
uint8_t broadcast_interval;
uint8_t transmit_power;
FindMyState state;
};

typedef enum {
Expand All @@ -37,6 +34,12 @@ typedef enum {
FindMyViewVarItemList,
} FindMyView;

enum FindMyType {
FindMyTypeApple,
FindMyTypeSamsung,
};

void findmy_change_broadcast_interval(FindMy* app, uint8_t value);
void findmy_change_transmit_power(FindMy* app, uint8_t value);
void findmy_toggle_beacon(FindMy* app);
FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]);
61 changes: 61 additions & 0 deletions applications/system/findmy/findmy_state.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "findmy_state.h"

#include <string.h>
#include <stddef.h>
#include <furi_hal_bt.h>

bool findmy_state_load(FindMyState* out_state) {
FindMyState state;

// Set default values
state.beacon_active = false;
state.broadcast_interval = 5;
state.transmit_power = 6;
state.config.min_adv_interval_ms = state.broadcast_interval * 1000; // Converting s to ms
state.config.max_adv_interval_ms = (state.broadcast_interval * 1000) + 150;
state.config.adv_channel_map = GapAdvChannelMapAll;
state.config.adv_power_level = GapAdvPowerLevel_0dBm + state.transmit_power;
state.config.address_type = GapAddressTypePublic;

// Set default mac
uint8_t default_mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
memcpy(state.mac, default_mac, sizeof(state.mac));
memcpy(state.config.address, default_mac, sizeof(state.config.address));

// Set default empty AirTag data
uint8_t* data = state.data;
*data++ = 0x1E; // Length
*data++ = 0xFF; // Manufacturer Specific Data
*data++ = 0x4C; // Company ID (Apple, Inc.)
*data++ = 0x00; // ...
*data++ = 0x12; // Type (FindMy)
*data++ = 0x19; // Length
*data++ = 0x00; // Status
// Placeholder Empty Public Key without the MAC address
for(size_t i = 0; i < 22; ++i) {
*data++ = 0x00;
}
*data++ = 0x00; // First 2 bits are the version, the rest is the battery level
*data++ = 0x00; // Hint (0x00)

// Copy to caller state before popping stack
memcpy(out_state, &state, sizeof(state));

// Return if active, can be used to start after loading in an if statement
return state.beacon_active;
}

void findmy_state_apply(FindMyState* state) {
// Stop any running beacon
if(furi_hal_bt_extra_beacon_is_active()) {
furi_check(furi_hal_bt_extra_beacon_stop());
}

furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));

furi_check(furi_hal_bt_extra_beacon_set_data(state->data, sizeof(state->data)));

if(state->beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
}
}
17 changes: 17 additions & 0 deletions applications/system/findmy/findmy_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <extra_beacon.h>

typedef struct {
uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE];
uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE];
GapExtraBeaconConfig config;

bool beacon_active;
uint8_t broadcast_interval;
uint8_t transmit_power;
} FindMyState;

bool findmy_state_load(FindMyState* out_state);

void findmy_state_apply(FindMyState* state);
24 changes: 12 additions & 12 deletions applications/system/findmy/scenes/findmy_scene_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ void findmy_scene_config_broadcast_interval_changed(VariableItem* item) {
uint8_t index = variable_item_get_current_value_index(item);
findmy_change_broadcast_interval(app, index + 1);
char str[5];
snprintf(str, sizeof(str), "%ds", app->broadcast_interval);
snprintf(str, sizeof(str), "%ds", app->state.broadcast_interval);
variable_item_set_current_value_text(item, str);
variable_item_set_current_value_index(item, app->broadcast_interval - 1);
variable_item_set_current_value_index(item, app->state.broadcast_interval - 1);
}

void findmy_scene_config_transmit_power_changed(VariableItem* item) {
FindMy* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
findmy_change_transmit_power(app, index);
char str[7];
snprintf(str, sizeof(str), "%ddBm", app->transmit_power);
snprintf(str, sizeof(str), "%ddBm", app->state.transmit_power);
variable_item_set_current_value_text(item, str);
variable_item_set_current_value_index(item, app->transmit_power);
variable_item_set_current_value_index(item, app->state.transmit_power);
}

void findmy_scene_config_callback(void* context, uint32_t index) {
Expand All @@ -45,17 +45,17 @@ void findmy_scene_config_on_enter(void* context) {
findmy_scene_config_broadcast_interval_changed,
app);
// Broadcast Interval is 1-10, so use 0-9 and offset indexes by 1
variable_item_set_current_value_index(item, app->broadcast_interval - 1);
char broadcast_interval_s[5];
snprintf(broadcast_interval_s, sizeof(broadcast_interval_s), "%ds", app->broadcast_interval);
variable_item_set_current_value_text(item, broadcast_interval_s);
variable_item_set_current_value_index(item, app->state.broadcast_interval - 1);
char interval_str[5];
snprintf(interval_str, sizeof(interval_str), "%ds", app->state.broadcast_interval);
variable_item_set_current_value_text(item, interval_str);

item = variable_item_list_add(
var_item_list, "Transmit Power", 7, findmy_scene_config_transmit_power_changed, app);
variable_item_set_current_value_index(item, app->transmit_power);
char transmit_power_s[7];
snprintf(transmit_power_s, sizeof(transmit_power_s), "%ddBm", app->transmit_power);
variable_item_set_current_value_text(item, transmit_power_s);
variable_item_set_current_value_index(item, app->state.transmit_power);
char power_str[7];
snprintf(power_str, sizeof(power_str), "%ddBm", app->state.transmit_power);
variable_item_set_current_value_text(item, power_str);

item = variable_item_list_add(var_item_list, "Register Tag", 0, NULL, NULL);
item = variable_item_list_add(
Expand Down
13 changes: 10 additions & 3 deletions applications/system/findmy/scenes/findmy_scene_config_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void findmy_scene_config_mac_on_enter(void* context) {

byte_input_set_header_text(byte_input, "Enter Bluetooth MAC:");

memcpy(app->mac_buf, &app->config.address, sizeof(app->mac_buf));
memcpy(app->mac_buf, app->state.mac, sizeof(app->mac_buf));
furi_hal_bt_reverse_mac_addr(app->mac_buf);

byte_input_set_result_callback(
Expand All @@ -39,8 +39,15 @@ bool findmy_scene_config_mac_on_event(void* context, SceneManagerEvent event) {
switch(event.event) {
case ByteInputResultOk:
furi_hal_bt_reverse_mac_addr(app->mac_buf);
memcpy(&app->config.address, app->mac_buf, sizeof(app->config.address));
furi_hal_bt_extra_beacon_set_config(&app->config);
memcpy(&app->state.mac, app->mac_buf, sizeof(app->state.mac));
memcpy(&app->state.config.address, app->mac_buf, sizeof(app->state.config.address));
if(furi_hal_bt_extra_beacon_is_active()) {
furi_check(furi_hal_bt_extra_beacon_stop());
}
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
if(app->state.beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
}
scene_manager_next_scene(app->scene_manager, FindMySceneConfigPacket);
break;
default:
Expand Down
13 changes: 4 additions & 9 deletions applications/system/findmy/scenes/findmy_scene_config_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ void findmy_scene_config_packet_on_enter(void* context) {

byte_input_set_header_text(byte_input, "Enter Bluetooth Payload:");

memset(app->packet_buf, 0, sizeof(app->packet_buf));
furi_hal_bt_extra_beacon_get_data(app->packet_buf);
memcpy(app->packet_buf, app->state.data, sizeof(app->packet_buf));

byte_input_set_result_callback(
byte_input,
Expand All @@ -40,14 +39,10 @@ bool findmy_scene_config_packet_on_event(void* context, SceneManagerEvent event)
case ByteInputResultOk:
scene_manager_search_and_switch_to_previous_scene(
app->scene_manager, FindMySceneConfig);
memcpy(app->state.data, app->packet_buf, sizeof(app->state.data));
furi_check(
furi_hal_bt_extra_beacon_set_data(app->packet_buf, sizeof(app->packet_buf)));
if(app->packet_buf[0] == 0x1E && app->packet_buf[3] == 0x00) {
app->apple = true; // Checks payload data for Apple identifier
} else {
app->apple = false;
}
findmy_main_update_apple(app->findmy_main, app->apple);
furi_hal_bt_extra_beacon_set_data(app->state.data, sizeof(app->state.data)));
findmy_main_update_type(app->findmy_main, findmy_data_get_type(app->state.data));
break;
default:
break;
Expand Down
Loading

0 comments on commit 7c48c61

Please sign in to comment.