loclass_writer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if(!buffered_file_stream_open(
  16. instance->file_stream, LOCLASS_LOGS_PATH, FSAM_WRITE, FSOM_OPEN_APPEND)) {
  17. buffered_file_stream_close(instance->file_stream);
  18. stream_free(instance->file_stream);
  19. free(instance);
  20. instance = NULL;
  21. }
  22. furi_record_close(RECORD_STORAGE);
  23. return instance;
  24. }
  25. void loclass_writer_free(LoclassWriter* instance) {
  26. furi_assert(instance != NULL);
  27. buffered_file_stream_close(instance->file_stream);
  28. stream_free(instance->file_stream);
  29. free(instance);
  30. }
  31. bool loclass_writer_write_start_stop(LoclassWriter* instance, bool start) {
  32. furi_assert(instance != NULL);
  33. FuriHalRtcDateTime curr_dt;
  34. furi_hal_rtc_get_datetime(&curr_dt);
  35. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  36. FuriString* str = furi_string_alloc_printf(
  37. "loclass-v1-info ts %lu %s\n", curr_ts, start ? "started" : "finished");
  38. bool write_success = stream_write_string(instance->file_stream, str);
  39. furi_string_free(str);
  40. return write_success;
  41. }
  42. bool loclass_writer_write_params(
  43. LoclassWriter* instance,
  44. uint8_t log_no,
  45. const uint8_t csn[8],
  46. const uint8_t epurse[8],
  47. const uint8_t nr[4],
  48. const uint8_t mac[4]) {
  49. furi_assert(instance != NULL);
  50. FuriHalRtcDateTime curr_dt;
  51. furi_hal_rtc_get_datetime(&curr_dt);
  52. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  53. FuriString* str = furi_string_alloc_printf(
  54. "loclass-v1-mac ts %lu no %u "
  55. "csn %02x%02x%02x%02x%02x%02x%02x%02x "
  56. "cc %02x%02x%02x%02x%02x%02x%02x%02x "
  57. "nr %02x%02x%02x%02x "
  58. "mac %02x%02x%02x%02x\n",
  59. curr_ts,
  60. log_no,
  61. csn[0],
  62. csn[1],
  63. csn[2],
  64. csn[3],
  65. csn[4],
  66. csn[5],
  67. csn[6],
  68. csn[7],
  69. epurse[0],
  70. epurse[1],
  71. epurse[2],
  72. epurse[3],
  73. epurse[4],
  74. epurse[5],
  75. epurse[6],
  76. epurse[7],
  77. nr[0],
  78. nr[1],
  79. nr[2],
  80. nr[3],
  81. mac[0],
  82. mac[1],
  83. mac[2],
  84. mac[3]);
  85. bool write_success = stream_write_string(instance->file_stream, str);
  86. furi_string_free(str);
  87. return write_success;
  88. }