loclass_writer.c 2.8 KB

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