nfc_debug_log.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "nfc_debug_log.h"
  2. #include <storage/storage.h>
  3. #include <stream/buffered_file_stream.h>
  4. #define TAG "NfcDebugLog"
  5. #define NFC_DEBUG_PCAP_FILENAME EXT_PATH("nfc/debug.txt")
  6. struct NfcDebugLog {
  7. Stream* file_stream;
  8. FuriString* data_str;
  9. };
  10. NfcDebugLog* nfc_debug_log_alloc() {
  11. NfcDebugLog* instance = malloc(sizeof(NfcDebugLog));
  12. Storage* storage = furi_record_open(RECORD_STORAGE);
  13. instance->file_stream = buffered_file_stream_alloc(storage);
  14. if(!buffered_file_stream_open(
  15. instance->file_stream, NFC_DEBUG_PCAP_FILENAME, FSAM_WRITE, FSOM_OPEN_APPEND)) {
  16. buffered_file_stream_close(instance->file_stream);
  17. stream_free(instance->file_stream);
  18. instance->file_stream = NULL;
  19. }
  20. if(!instance->file_stream) {
  21. free(instance);
  22. instance = NULL;
  23. } else {
  24. instance->data_str = furi_string_alloc();
  25. }
  26. furi_record_close(RECORD_STORAGE);
  27. return instance;
  28. }
  29. void nfc_debug_log_free(NfcDebugLog* instance) {
  30. furi_assert(instance);
  31. furi_assert(instance->file_stream);
  32. furi_assert(instance->data_str);
  33. buffered_file_stream_close(instance->file_stream);
  34. stream_free(instance->file_stream);
  35. furi_string_free(instance->data_str);
  36. free(instance);
  37. }
  38. void nfc_debug_log_process_data(
  39. NfcDebugLog* instance,
  40. uint8_t* data,
  41. uint16_t len,
  42. bool reader_to_tag,
  43. bool crc_dropped) {
  44. furi_assert(instance);
  45. furi_assert(instance->file_stream);
  46. furi_assert(instance->data_str);
  47. furi_assert(data);
  48. UNUSED(crc_dropped);
  49. furi_string_printf(instance->data_str, "%lu %c:", furi_get_tick(), reader_to_tag ? 'R' : 'T');
  50. uint16_t data_len = len;
  51. for(size_t i = 0; i < data_len; i++) {
  52. furi_string_cat_printf(instance->data_str, " %02x", data[i]);
  53. }
  54. furi_string_push_back(instance->data_str, '\n');
  55. stream_write_string(instance->file_stream, instance->data_str);
  56. }