upython_cli.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include "upython.h"
  4. static FuriStreamBuffer* stdout_buffer = NULL;
  5. static void write_to_stdout_buffer(const char* data, size_t size) {
  6. furi_stream_buffer_send(stdout_buffer, data, size, 0);
  7. }
  8. void upython_cli(Cli* cli, FuriString* args, void* ctx) {
  9. UNUSED(ctx);
  10. if(action != ActionNone) {
  11. printf("%s is busy!\n", TAG);
  12. return;
  13. }
  14. if(furi_string_empty(args)) {
  15. action = ActionRepl;
  16. upython_repl_execute(cli);
  17. action = ActionNone;
  18. } else {
  19. furi_string_set(file_path, args);
  20. stdout_buffer = furi_stream_buffer_alloc(128, 1);
  21. stdout_callback = write_to_stdout_buffer;
  22. action = ActionExec;
  23. char data = '\0';
  24. while(action == ActionExec || !furi_stream_buffer_is_empty(stdout_buffer)) {
  25. if(furi_stream_buffer_receive(stdout_buffer, &data, 1, 0) > 0) {
  26. printf("%c", data);
  27. }
  28. }
  29. furi_stream_buffer_free(stdout_buffer);
  30. }
  31. }
  32. void upython_cli_register(void* args) {
  33. if(args != NULL) {
  34. file_path = furi_string_alloc_set_str(args);
  35. action = ActionExec;
  36. return;
  37. } else {
  38. file_path = furi_string_alloc();
  39. upython_reset_file_path();
  40. action = ActionNone;
  41. }
  42. Cli* cli = furi_record_open(RECORD_CLI);
  43. cli_add_command(cli, CLI, CliCommandFlagParallelSafe, upython_cli, NULL);
  44. furi_record_close(RECORD_CLI);
  45. }
  46. void upython_cli_unregister(void* args) {
  47. furi_string_free(file_path);
  48. if(args != NULL) {
  49. return;
  50. }
  51. Cli* cli = furi_record_open(RECORD_CLI);
  52. cli_delete_command(cli, CLI);
  53. furi_record_close(RECORD_CLI);
  54. }