findmy_state.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "findmy_state.h"
  2. #include <string.h>
  3. #include <stddef.h>
  4. #include <furi_hal_bt.h>
  5. #include <flipper_format/flipper_format.h>
  6. bool findmy_state_load(FindMyState* out_state) {
  7. FindMyState state;
  8. // Try to load from file
  9. bool loaded_from_file = false;
  10. Storage* storage = furi_record_open(RECORD_STORAGE);
  11. if(storage_file_exists(storage, FINDMY_STATE_PATH)) {
  12. FlipperFormat* file = flipper_format_file_alloc(storage);
  13. do {
  14. uint32_t tmp;
  15. FuriString* str = furi_string_alloc();
  16. if(!flipper_format_file_open_existing(file, FINDMY_STATE_PATH)) break;
  17. if(!flipper_format_read_header(file, str, &tmp)) break;
  18. if(furi_string_cmp_str(str, FINDMY_STATE_HEADER)) break;
  19. if(tmp != FINDMY_STATE_VER) break;
  20. if(!flipper_format_read_bool(file, "beacon_active", &state.beacon_active, 1)) break;
  21. if(!flipper_format_read_uint32(file, "broadcast_interval", &tmp, 1)) break;
  22. state.broadcast_interval = tmp;
  23. if(!flipper_format_read_uint32(file, "transmit_power", &tmp, 1)) break;
  24. state.transmit_power = tmp;
  25. if(!flipper_format_read_bool(file, "show_mac", &state.show_mac, 1)) {
  26. // Support migrating from old config
  27. state.show_mac = false;
  28. flipper_format_rewind(file);
  29. }
  30. if(!flipper_format_read_uint32(file, "tag_type", &tmp, 1)) {
  31. // Support migrating from old config
  32. tmp = FindMyTypeApple;
  33. flipper_format_rewind(file);
  34. }
  35. state.tag_type = tmp;
  36. if(!flipper_format_read_hex(file, "mac", state.mac, sizeof(state.mac))) break;
  37. if(!flipper_format_read_hex(
  38. file, "data", state.data, findmy_state_data_size(state.tag_type)))
  39. break;
  40. loaded_from_file = true;
  41. } while(0);
  42. flipper_format_free(file);
  43. }
  44. furi_record_close(RECORD_STORAGE);
  45. // Otherwise set default values
  46. if(!loaded_from_file) {
  47. state.beacon_active = false;
  48. state.broadcast_interval = 5;
  49. state.transmit_power = 6;
  50. state.show_mac = false;
  51. state.tag_type = FindMyTypeApple;
  52. // Set default mac
  53. uint8_t default_mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
  54. memcpy(state.mac, default_mac, sizeof(state.mac));
  55. // Set default empty AirTag data
  56. uint8_t* data = state.data;
  57. *data++ = 0x1E; // Length
  58. *data++ = 0xFF; // Manufacturer Specific Data
  59. *data++ = 0x4C; // Company ID (Apple, Inc.)
  60. *data++ = 0x00; // ...
  61. *data++ = 0x12; // Type (FindMy)
  62. *data++ = 0x19; // Length
  63. *data++ = 0x00; // Status
  64. // Placeholder Empty Public Key without the MAC address
  65. for(size_t i = 0; i < 22; ++i) {
  66. *data++ = 0x00;
  67. }
  68. *data++ = 0x00; // First 2 bits are the version, the rest is the battery level
  69. *data++ = 0x00; // Hint (0x00)
  70. }
  71. // Sync values to config
  72. findmy_state_sync_config(&state);
  73. // Set constants
  74. state.config.adv_channel_map = GapAdvChannelMapAll;
  75. state.config.address_type = GapAddressTypePublic;
  76. // Copy to caller state before popping stack
  77. memcpy(out_state, &state, sizeof(state));
  78. // Return if active, can be used to start after loading in an if statement
  79. return state.beacon_active;
  80. }
  81. void findmy_state_apply(FindMyState* state) {
  82. // Stop any running beacon
  83. if(furi_hal_bt_extra_beacon_is_active()) {
  84. furi_check(furi_hal_bt_extra_beacon_stop());
  85. }
  86. furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));
  87. furi_check(
  88. furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
  89. if(state->beacon_active) {
  90. furi_check(furi_hal_bt_extra_beacon_start());
  91. }
  92. }
  93. void findmy_state_sync_config(FindMyState* state) {
  94. state->config.min_adv_interval_ms = state->broadcast_interval * 1000; // Converting s to ms
  95. state->config.max_adv_interval_ms = (state->broadcast_interval * 1000) + 150;
  96. state->config.adv_power_level = GapAdvPowerLevel_0dBm + state->transmit_power;
  97. memcpy(state->config.address, state->mac, sizeof(state->config.address));
  98. }
  99. void findmy_state_save(FindMyState* state) {
  100. Storage* storage = furi_record_open(RECORD_STORAGE);
  101. storage_simply_mkdir(storage, FINDMY_STATE_DIR);
  102. FlipperFormat* file = flipper_format_file_alloc(storage);
  103. do {
  104. uint32_t tmp;
  105. if(!flipper_format_file_open_always(file, FINDMY_STATE_PATH)) break;
  106. if(!flipper_format_write_header_cstr(file, FINDMY_STATE_HEADER, FINDMY_STATE_VER)) break;
  107. if(!flipper_format_write_bool(file, "beacon_active", &state->beacon_active, 1)) break;
  108. tmp = state->broadcast_interval;
  109. if(!flipper_format_write_uint32(file, "broadcast_interval", &tmp, 1)) break;
  110. tmp = state->transmit_power;
  111. if(!flipper_format_write_uint32(file, "transmit_power", &tmp, 1)) break;
  112. tmp = state->tag_type;
  113. if(!flipper_format_write_uint32(file, "tag_type", &tmp, 1)) break;
  114. if(!flipper_format_write_bool(file, "show_mac", &state->show_mac, 1)) break;
  115. if(!flipper_format_write_hex(file, "mac", state->mac, sizeof(state->mac))) break;
  116. if(!flipper_format_write_hex(
  117. file, "data", state->data, findmy_state_data_size(state->tag_type)))
  118. break;
  119. } while(0);
  120. flipper_format_free(file);
  121. furi_record_close(RECORD_STORAGE);
  122. }
  123. uint8_t findmy_state_data_size(FindMyType type) {
  124. switch(type) {
  125. case FindMyTypeApple:
  126. case FindMyTypeSamsung:
  127. return 31;
  128. case FindMyTypeTile:
  129. return 21;
  130. default:
  131. return 0;
  132. }
  133. }