mphalport.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <storage/storage.h>
  4. #include <py/mphal.h>
  5. #include <py/builtin.h>
  6. #include <py/reader.h>
  7. #include "state.h"
  8. void mp_hal_stdout_tx_str(const char* str) {
  9. printf("%s", str);
  10. }
  11. mp_import_stat_t mp_import_stat(const char* path) {
  12. Storage* storage = furi_record_open(RECORD_STORAGE);
  13. FuriString* file_path = furi_string_alloc();
  14. mp_import_stat_t stat = MP_IMPORT_STAT_NO_EXIST;
  15. furi_string_printf(file_path, "%s/%s", root_module_path, path);
  16. FileInfo* info = NULL;
  17. if(storage_common_stat(storage, furi_string_get_cstr(file_path), info) == FSE_OK) {
  18. stat = (info && info->flags & FSF_DIRECTORY) == FSF_DIRECTORY ? MP_IMPORT_STAT_DIR : MP_IMPORT_STAT_FILE;
  19. }
  20. furi_string_free(file_path);
  21. furi_record_close(RECORD_STORAGE);
  22. return stat;
  23. }
  24. typedef struct {
  25. size_t pointer;
  26. char* content;
  27. size_t size;
  28. } FileReaderContext;
  29. FileReaderContext* file_reader_context_alloc(File* file) {
  30. FileReaderContext* context = malloc(sizeof(FileReaderContext));
  31. context->pointer = 0;
  32. context->size = storage_file_size(file);
  33. context->content = malloc(context->size * sizeof(char));
  34. storage_file_read(file, context->content, context->size);
  35. return context;
  36. }
  37. void file_reader_context_free(FileReaderContext* context) {
  38. free(context->content);
  39. free(context);
  40. }
  41. mp_uint_t file_reader_read(void* data) {
  42. FileReaderContext* context = data;
  43. if(context->size == 0 || context->pointer >= context->size) {
  44. return MP_READER_EOF;
  45. }
  46. return context->content[context->pointer++];
  47. }
  48. void file_reader_close(void* data) {
  49. file_reader_context_free(data);
  50. }
  51. void mp_reader_new_file(mp_reader_t* reader, qstr filename) {
  52. FuriString* file_path = furi_string_alloc();
  53. Storage* storage = furi_record_open(RECORD_STORAGE);
  54. File* file = storage_file_alloc(storage);
  55. furi_string_printf(file_path, "%s/%s", root_module_path, qstr_str(filename));
  56. if(!storage_file_open(file, furi_string_get_cstr(file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  57. furi_crash("unable to open file");
  58. }
  59. reader->data = file_reader_context_alloc(file);
  60. reader->readbyte = file_reader_read;
  61. reader->close = file_reader_close;
  62. storage_file_free(file);
  63. furi_record_close(RECORD_STORAGE);
  64. furi_string_free(file_path);
  65. }