py_app.c 3.6 KB

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