upython_file.c 1.3 KB

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