suica_loading.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "../../metroflip_i.h"
  2. #include <lib/nfc/protocols/felica/felica.h>
  3. #include "suica_structs_i.h"
  4. void suica_add_entry(SuicaHistoryViewModel* model, const uint8_t* entry) {
  5. if(model->size <= 0) {
  6. model->travel_history =
  7. (uint8_t*)malloc(3 * FELICA_DATA_BLOCK_SIZE); // Each entry is 16 bytes
  8. model->size = 0;
  9. model->capacity = 3;
  10. }
  11. // Check if resizing is needed
  12. if(model->size == model->capacity) {
  13. size_t new_capacity = model->capacity * 2; // Double the capacity
  14. uint8_t* new_data =
  15. (uint8_t*)realloc(model->travel_history, new_capacity * FELICA_DATA_BLOCK_SIZE);
  16. model->travel_history = new_data;
  17. model->capacity = new_capacity;
  18. }
  19. // Copy the 16-byte entry to the next slot
  20. for(size_t i = 0; i < FELICA_DATA_BLOCK_SIZE; i++) {
  21. model->travel_history[(model->size * FELICA_DATA_BLOCK_SIZE) + i] = entry[i];
  22. }
  23. model->size++;
  24. }
  25. void load_suica_data(void* context, FlipperFormat* format) {
  26. Metroflip* app = (Metroflip*)context;
  27. app->suica_context = malloc(sizeof(SuicaContext));
  28. app->suica_context->view_history = view_alloc();
  29. view_set_context(app->suica_context->view_history, app);
  30. view_allocate_model(
  31. app->suica_context->view_history, ViewModelTypeLockFree, sizeof(SuicaHistoryViewModel));
  32. SuicaHistoryViewModel* model = view_get_model(app->suica_context->view_history);
  33. uint8_t* byte_array_buffer = (uint8_t*)malloc(FELICA_DATA_BLOCK_SIZE);
  34. FuriString* entry_preamble = furi_string_alloc();
  35. // Read the travel history entries
  36. for(uint8_t i = 0; i < SUICA_MAX_HISTORY_ENTRIES; i++) {
  37. furi_string_printf(entry_preamble, "Travel %02X", i);
  38. // For every line in the flipper format file
  39. // We read the entire line's hex and store it in the byte_array_buffer
  40. if(!flipper_format_read_hex(
  41. format,
  42. furi_string_get_cstr(entry_preamble),
  43. byte_array_buffer,
  44. FELICA_DATA_BLOCK_SIZE))
  45. break;
  46. uint8_t block_data[16] = {0};
  47. for(size_t j = 0; j < FELICA_DATA_BLOCK_SIZE; j++) {
  48. block_data[j] = byte_array_buffer[j];
  49. }
  50. suica_add_entry(model, block_data);
  51. }
  52. furi_string_free(entry_preamble);
  53. free(byte_array_buffer);
  54. }