loclass_writer.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "loclass_writer.h"
  2. #include <furi/furi.h>
  3. #include <furi_hal.h>
  4. #include <storage/storage.h>
  5. #include <stream/stream.h>
  6. #include <stream/buffered_file_stream.h>
  7. struct LoclassWriter {
  8. Stream* file_stream;
  9. };
  10. #define LOCLASS_LOGS_PATH EXT_PATH("apps_data/picopass/.loclass.log")
  11. LoclassWriter* loclass_writer_alloc() {
  12. LoclassWriter* instance = malloc(sizeof(LoclassWriter));
  13. Storage* storage = furi_record_open(RECORD_STORAGE);
  14. instance->file_stream = buffered_file_stream_alloc(storage);
  15. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  16. if(!buffered_file_stream_open(
  17. instance->file_stream, LOCLASS_LOGS_PATH, FSAM_WRITE, FSOM_OPEN_APPEND)) {
  18. buffered_file_stream_close(instance->file_stream);
  19. stream_free(instance->file_stream);
  20. free(instance);
  21. instance = NULL;
  22. }
  23. furi_record_close(RECORD_STORAGE);
  24. return instance;
  25. }
  26. void loclass_writer_free(LoclassWriter* instance) {
  27. furi_assert(instance != NULL);
  28. buffered_file_stream_close(instance->file_stream);
  29. stream_free(instance->file_stream);
  30. free(instance);
  31. }
  32. bool loclass_writer_write_start_stop(LoclassWriter* instance, bool start) {
  33. furi_assert(instance != NULL);
  34. uint32_t curr_ts = furi_hal_rtc_get_timestamp();
  35. FuriString* str = furi_string_alloc_printf(
  36. "loclass-v1-info ts %lu %s\n", curr_ts, start ? "started" : "finished");
  37. bool write_success = stream_write_string(instance->file_stream, str);
  38. furi_string_free(str);
  39. return write_success;
  40. }
  41. bool loclass_writer_write_params(
  42. LoclassWriter* instance,
  43. uint8_t log_no,
  44. const uint8_t csn[8],
  45. const uint8_t epurse[8],
  46. const uint8_t nr[4],
  47. const uint8_t mac[4]) {
  48. furi_assert(instance != NULL);
  49. uint32_t curr_ts = furi_hal_rtc_get_timestamp();
  50. FuriString* str = furi_string_alloc_printf(
  51. "loclass-v1-mac ts %lu no %u "
  52. "csn %02x%02x%02x%02x%02x%02x%02x%02x "
  53. "cc %02x%02x%02x%02x%02x%02x%02x%02x "
  54. "nr %02x%02x%02x%02x "
  55. "mac %02x%02x%02x%02x\n",
  56. curr_ts,
  57. log_no,
  58. csn[0],
  59. csn[1],
  60. csn[2],
  61. csn[3],
  62. csn[4],
  63. csn[5],
  64. csn[6],
  65. csn[7],
  66. epurse[0],
  67. epurse[1],
  68. epurse[2],
  69. epurse[3],
  70. epurse[4],
  71. epurse[5],
  72. epurse[6],
  73. epurse[7],
  74. nr[0],
  75. nr[1],
  76. nr[2],
  77. nr[3],
  78. mac[0],
  79. mac[1],
  80. mac[2],
  81. mac[3]);
  82. bool write_success = stream_write_string(instance->file_stream, str);
  83. furi_string_free(str);
  84. return write_success;
  85. }