loclass_writer.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. FuriHalRtcDateTime curr_dt;
  35. furi_hal_rtc_get_datetime(&curr_dt);
  36. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  37. FuriString* str = furi_string_alloc_printf(
  38. "loclass-v1-info ts %lu %s\n", curr_ts, start ? "started" : "finished");
  39. bool write_success = stream_write_string(instance->file_stream, str);
  40. furi_string_free(str);
  41. return write_success;
  42. }
  43. bool loclass_writer_write_params(
  44. LoclassWriter* instance,
  45. uint8_t log_no,
  46. const uint8_t csn[8],
  47. const uint8_t epurse[8],
  48. const uint8_t nr[4],
  49. const uint8_t mac[4]) {
  50. furi_assert(instance != NULL);
  51. FuriHalRtcDateTime curr_dt;
  52. furi_hal_rtc_get_datetime(&curr_dt);
  53. uint32_t curr_ts = furi_hal_rtc_datetime_to_timestamp(&curr_dt);
  54. FuriString* str = furi_string_alloc_printf(
  55. "loclass-v1-mac ts %lu no %u "
  56. "csn %02x%02x%02x%02x%02x%02x%02x%02x "
  57. "cc %02x%02x%02x%02x%02x%02x%02x%02x "
  58. "nr %02x%02x%02x%02x "
  59. "mac %02x%02x%02x%02x\n",
  60. curr_ts,
  61. log_no,
  62. csn[0],
  63. csn[1],
  64. csn[2],
  65. csn[3],
  66. csn[4],
  67. csn[5],
  68. csn[6],
  69. csn[7],
  70. epurse[0],
  71. epurse[1],
  72. epurse[2],
  73. epurse[3],
  74. epurse[4],
  75. epurse[5],
  76. epurse[6],
  77. epurse[7],
  78. nr[0],
  79. nr[1],
  80. nr[2],
  81. nr[3],
  82. mac[0],
  83. mac[1],
  84. mac[2],
  85. mac[3]);
  86. bool write_success = stream_write_string(instance->file_stream, str);
  87. furi_string_free(str);
  88. return write_success;
  89. }