py_app.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <mp_flipper_runtime.h>
  4. #include <mp_flipper_compiler.h>
  5. #include "py_app.h"
  6. #define PYTHON_SETTINGS_VERSION 0x01
  7. #define PYTHON_SETTINGS_PATH INT_PATH(".python.settings")
  8. int32_t py_app(void *arg)
  9. {
  10. if (arg == NULL || strlen(arg) == 0)
  11. {
  12. return 0;
  13. }
  14. FuriString *file_path = furi_string_alloc_printf("%s", (const char *)arg);
  15. py_app_file_execute(file_path);
  16. furi_string_free(file_path);
  17. return 0;
  18. }
  19. void py_app_file_execute(FuriString *file)
  20. {
  21. size_t stack;
  22. PyApp *app = py_app_alloc();
  23. const char *path = furi_string_get_cstr(file);
  24. FuriString *file_path = furi_string_alloc_printf("%s", path);
  25. do
  26. {
  27. FURI_LOG_I(TAG, "executing script %s", path);
  28. const size_t heap_size = memmgr_get_free_heap() * app->heap_size;
  29. const size_t stack_size = app->stack_size;
  30. uint8_t *heap = malloc(heap_size * sizeof(uint8_t));
  31. FURI_LOG_D(TAG, "allocated memory is %zu bytes", heap_size);
  32. FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
  33. size_t index = furi_string_search_rchar(file_path, '/');
  34. furi_check(index != FURI_STRING_FAILURE);
  35. bool is_py_file = furi_string_end_with_str(file_path, ".py");
  36. furi_string_left(file_path, index);
  37. mp_flipper_set_root_module_path(furi_string_get_cstr(file_path));
  38. mp_flipper_init(heap, heap_size, stack_size, &stack);
  39. if (is_py_file)
  40. {
  41. mp_flipper_exec_py_file(path);
  42. }
  43. else
  44. {
  45. mp_flipper_exec_mpy_file(path);
  46. }
  47. mp_flipper_deinit();
  48. free(heap);
  49. } while (false);
  50. furi_string_free(file_path);
  51. py_app_free(app);
  52. }
  53. PyApp *py_app_load_settings()
  54. {
  55. File *file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  56. PyApp *settings = malloc(sizeof(PyApp));
  57. const size_t settings_size = sizeof(PyApp);
  58. FURI_LOG_I(TAG, "loading settings from \"%s\"", PYTHON_SETTINGS_PATH);
  59. bool fs_result = storage_file_open(file, PYTHON_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  60. if (fs_result)
  61. {
  62. size_t bytes_count = storage_file_read(file, settings, settings_size);
  63. fs_result = bytes_count == settings_size;
  64. }
  65. if (fs_result)
  66. {
  67. FURI_LOG_I(TAG, "load success");
  68. }
  69. else
  70. {
  71. FURI_LOG_E(TAG, "load failed, %s", storage_file_get_error_desc(file));
  72. settings->heap_size = 0.3;
  73. settings->stack_size = 1024;
  74. settings->version = PYTHON_SETTINGS_VERSION;
  75. }
  76. if (fs_result && settings->version != PYTHON_SETTINGS_VERSION)
  77. {
  78. FURI_LOG_E(TAG, "version(%d != %d) mismatch", settings->version, PYTHON_SETTINGS_VERSION);
  79. }
  80. storage_file_close(file);
  81. storage_file_free(file);
  82. furi_record_close(RECORD_STORAGE);
  83. return settings;
  84. };
  85. void py_app_save_settings(PyApp *settings)
  86. {
  87. File *file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  88. const size_t settings_size = sizeof(PyApp);
  89. FURI_LOG_I(TAG, "saving settings to \"%s\"", PYTHON_SETTINGS_PATH);
  90. bool fs_result = storage_file_open(file, PYTHON_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  91. if (fs_result)
  92. {
  93. size_t bytes_count = storage_file_write(file, settings, settings_size);
  94. fs_result = bytes_count == settings_size;
  95. }
  96. if (fs_result)
  97. {
  98. FURI_LOG_I(TAG, "save success");
  99. }
  100. else
  101. {
  102. FURI_LOG_E(TAG, "save failed, %s", storage_file_get_error_desc(file));
  103. }
  104. storage_file_close(file);
  105. storage_file_free(file);
  106. furi_record_close(RECORD_STORAGE);
  107. };
  108. PyApp *py_app_alloc()
  109. {
  110. return py_app_load_settings();
  111. }
  112. void py_app_free(PyApp *app)
  113. {
  114. free(app);
  115. }