findmy_state.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "findmy_state.h"
  2. #include <string.h>
  3. #include <stddef.h>
  4. #include <furi_hal_bt.h>
  5. #include <furi_hal_power.h>
  6. #include <flipper_format/flipper_format.h>
  7. bool findmy_state_load(FindMyState* out_state) {
  8. FindMyState state;
  9. // Try to load from file
  10. bool loaded_from_file = false;
  11. Storage* storage = furi_record_open(RECORD_STORAGE);
  12. if(storage_file_exists(storage, FINDMY_STATE_PATH)) {
  13. FlipperFormat* file = flipper_format_file_alloc(storage);
  14. FuriString* str = furi_string_alloc();
  15. uint32_t tmp;
  16. do {
  17. if(!flipper_format_file_open_existing(file, FINDMY_STATE_PATH)) break;
  18. if(!flipper_format_read_header(file, str, &tmp)) break;
  19. if(furi_string_cmp_str(str, FINDMY_STATE_HEADER)) break;
  20. if(tmp != FINDMY_STATE_VER) break;
  21. if(!flipper_format_read_bool(file, "beacon_active", &state.beacon_active, 1)) break;
  22. if(!flipper_format_read_uint32(file, "broadcast_interval", &tmp, 1)) break;
  23. state.broadcast_interval = tmp;
  24. if(!flipper_format_read_uint32(file, "transmit_power", &tmp, 1)) break;
  25. state.transmit_power = tmp;
  26. if(!flipper_format_read_uint32(file, "tag_type", &tmp, 1)) {
  27. tmp = FindMyTypeApple;
  28. flipper_format_rewind(file);
  29. }
  30. state.tag_type = tmp;
  31. if(!flipper_format_read_bool(file, "show_mac", &state.show_mac, 1)) {
  32. // Support migrating from old config
  33. state.show_mac = false;
  34. flipper_format_rewind(file);
  35. }
  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. furi_string_free(str);
  43. flipper_format_free(file);
  44. }
  45. furi_record_close(RECORD_STORAGE);
  46. // Otherwise set default values
  47. if(!loaded_from_file) {
  48. state.beacon_active = false;
  49. state.broadcast_interval = 5;
  50. state.transmit_power = 6;
  51. state.show_mac = false;
  52. state.tag_type = FindMyTypeApple;
  53. // Set default mac
  54. uint8_t default_mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
  55. memcpy(state.mac, default_mac, sizeof(state.mac));
  56. // Set default empty AirTag data
  57. uint8_t* data = state.data;
  58. *data++ = 0x1E; // Length
  59. *data++ = 0xFF; // Manufacturer Specific Data
  60. *data++ = 0x4C; // Company ID (Apple, Inc.)
  61. *data++ = 0x00; // ...
  62. *data++ = 0x12; // Type (FindMy)
  63. *data++ = 0x19; // Length
  64. *data++ = 0x00; // Battery Status set to Full
  65. // Placeholder Empty Public Key without the MAC address
  66. for(size_t i = 0; i < 22; ++i) {
  67. *data++ = 0x00;
  68. }
  69. *data++ = 0x00; // First 2 bits are the version
  70. *data++ = 0x00; // Hint (0x00)
  71. }
  72. // Copy to caller state before popping stack
  73. memcpy(out_state, &state, sizeof(state));
  74. // Return if active, can be used to start after loading in an if statement
  75. return state.beacon_active;
  76. }
  77. static void findmy_state_update_payload_battery(FindMyState* state) {
  78. // Update the battery level in the payload
  79. if(state->tag_type == FindMyTypeApple) {
  80. uint32_t battery_capacity = furi_hal_power_get_battery_full_capacity();
  81. uint32_t battery_remaining = furi_hal_power_get_battery_remaining_capacity();
  82. uint8_t battery_percent = (battery_remaining * 100) / battery_capacity;
  83. uint8_t battery_level;
  84. if(battery_percent > 80) {
  85. battery_level = BATTERY_FULL;
  86. } else if(battery_percent > 50) {
  87. battery_level = BATTERY_MEDIUM;
  88. } else if(battery_percent > 20) {
  89. battery_level = BATTERY_LOW;
  90. } else {
  91. battery_level = BATTERY_CRITICAL;
  92. }
  93. state->data[6] = battery_level;
  94. }
  95. }
  96. void findmy_state_apply(FindMyState* state) {
  97. // This function applies configured state to the beacon (loaded values)
  98. // Stop beacon before configuring
  99. if(furi_hal_bt_extra_beacon_is_active()) {
  100. furi_check(furi_hal_bt_extra_beacon_stop());
  101. }
  102. // Make config struct from configured parameters and set it
  103. GapExtraBeaconConfig config = {
  104. .min_adv_interval_ms = state->broadcast_interval * 1000, // Converting s to ms
  105. .max_adv_interval_ms = (state->broadcast_interval * 1000) + 150,
  106. .adv_channel_map = GapAdvChannelMapAll,
  107. .adv_power_level = GapAdvPowerLevel_0dBm + state->transmit_power,
  108. .address_type = GapAddressTypePublic,
  109. };
  110. memcpy(config.address, state->mac, sizeof(config.address));
  111. furi_check(furi_hal_bt_extra_beacon_set_config(&config));
  112. // Update data payload with battery level and set it
  113. findmy_state_update_payload_battery(state);
  114. furi_check(
  115. furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
  116. // Start beacon if configured
  117. if(state->beacon_active) {
  118. furi_check(furi_hal_bt_extra_beacon_start());
  119. }
  120. }
  121. static void findmy_state_save(FindMyState* state) {
  122. Storage* storage = furi_record_open(RECORD_STORAGE);
  123. storage_simply_mkdir(storage, FINDMY_STATE_DIR);
  124. FlipperFormat* file = flipper_format_file_alloc(storage);
  125. do {
  126. uint32_t tmp;
  127. if(!flipper_format_file_open_always(file, FINDMY_STATE_PATH)) break;
  128. if(!flipper_format_write_header_cstr(file, FINDMY_STATE_HEADER, FINDMY_STATE_VER)) break;
  129. if(!flipper_format_write_bool(file, "beacon_active", &state->beacon_active, 1)) break;
  130. tmp = state->broadcast_interval;
  131. if(!flipper_format_write_uint32(file, "broadcast_interval", &tmp, 1)) break;
  132. tmp = state->transmit_power;
  133. if(!flipper_format_write_uint32(file, "transmit_power", &tmp, 1)) break;
  134. tmp = state->tag_type;
  135. if(!flipper_format_write_uint32(file, "tag_type", &tmp, 1)) break;
  136. if(!flipper_format_write_bool(file, "show_mac", &state->show_mac, 1)) break;
  137. if(!flipper_format_write_hex(file, "mac", state->mac, sizeof(state->mac))) break;
  138. if(!flipper_format_write_hex(
  139. file, "data", state->data, findmy_state_data_size(state->tag_type)))
  140. break;
  141. } while(0);
  142. flipper_format_free(file);
  143. furi_record_close(RECORD_STORAGE);
  144. }
  145. void findmy_state_save_and_apply(FindMyState* state) {
  146. findmy_state_apply(state);
  147. findmy_state_save(state);
  148. }
  149. uint8_t findmy_state_data_size(FindMyType type) {
  150. switch(type) {
  151. case FindMyTypeApple:
  152. case FindMyTypeSamsung:
  153. return 31;
  154. case FindMyTypeTile:
  155. return 21;
  156. default:
  157. return 0;
  158. }
  159. }