upython_file.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <toolbox/cli/cli_command.h>
  2. #include <cli/cli_main_commands.h>
  3. #include <furi.h>
  4. #include <genhdr/mpversion.h>
  5. #include <mp_flipper_compiler.h>
  6. #include <mp_flipper_repl.h>
  7. #include "upython.h"
  8. void upython_file_execute(FuriString* file) {
  9. size_t stack;
  10. const char* path = furi_string_get_cstr(file);
  11. FuriString* file_path = furi_string_alloc_printf("%s", path);
  12. do {
  13. FURI_LOG_I(TAG, "executing script %s", path);
  14. const size_t heap_size = memmgr_get_free_heap() * 0.1;
  15. const size_t stack_size = 2 * 1024;
  16. uint8_t* heap = malloc(heap_size * sizeof(uint8_t));
  17. FURI_LOG_D(TAG, "initial heap size is %zu bytes", heap_size);
  18. FURI_LOG_D(TAG, "stack size is %zu bytes", stack_size);
  19. size_t index = furi_string_search_rchar(file_path, '/');
  20. if(index == FURI_STRING_FAILURE) {
  21. FURI_LOG_E(TAG, "invalid file path");
  22. break;
  23. }
  24. bool is_py_file = furi_string_end_with_str(file_path, ".py");
  25. furi_string_left(file_path, index);
  26. mp_flipper_set_root_module_path(furi_string_get_cstr(file_path));
  27. mp_flipper_init(heap, heap_size, stack_size, &stack);
  28. if(is_py_file) {
  29. mp_flipper_exec_py_file(path);
  30. }
  31. mp_flipper_deinit();
  32. free(heap);
  33. } while(false);
  34. furi_string_free(file_path);
  35. }