nfc_debug_log.c 1.9 KB

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