mirror of
https://github.com/DarkFlippers/unleashed-firmware
synced 2024-11-30 00:10:30 +00:00
JS CLI command (#3539)
* js command * made the js command exit when there's an error * JS CLI: moved to js_app * JS: abortable cli invocations * JS: less debug logging in console logs, fix storage descriptor leak in cli Co-authored-by: Milk-Cool <milan-pl@mail.ru> Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
parent
bb439a9761
commit
84beb9b23e
5 changed files with 98 additions and 4 deletions
|
@ -6,6 +6,7 @@ App(
|
|||
"updater_app",
|
||||
"storage_move_to_sd",
|
||||
"js_app",
|
||||
"js_app_start",
|
||||
# "archive",
|
||||
],
|
||||
)
|
||||
|
|
|
@ -8,6 +8,13 @@ App(
|
|||
order=0,
|
||||
)
|
||||
|
||||
App(
|
||||
appid="js_app_start",
|
||||
apptype=FlipperAppType.STARTUP,
|
||||
entry_point="js_app_on_system_start",
|
||||
order=160,
|
||||
)
|
||||
|
||||
App(
|
||||
appid="js_dialog",
|
||||
apptype=FlipperAppType.PLUGIN,
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "js_app_i.h"
|
||||
#include <toolbox/path.h>
|
||||
#include <assets_icons.h>
|
||||
#include <cli/cli.h>
|
||||
|
||||
#define TAG "JS app"
|
||||
|
||||
|
@ -129,3 +130,85 @@ int32_t js_app(void* arg) {
|
|||
js_app_free(app);
|
||||
return 0;
|
||||
} //-V773
|
||||
|
||||
typedef struct {
|
||||
Cli* cli;
|
||||
FuriSemaphore* exit_sem;
|
||||
} JsCliContext;
|
||||
|
||||
static void js_cli_print(JsCliContext* ctx, const char* msg) {
|
||||
cli_write(ctx->cli, (uint8_t*)msg, strlen(msg));
|
||||
}
|
||||
|
||||
static void js_cli_exit(JsCliContext* ctx) {
|
||||
furi_check(furi_semaphore_release(ctx->exit_sem) == FuriStatusOk);
|
||||
}
|
||||
|
||||
static void js_cli_callback(JsThreadEvent event, const char* msg, void* context) {
|
||||
JsCliContext* ctx = context;
|
||||
switch(event) {
|
||||
case JsThreadEventError:
|
||||
js_cli_print(ctx, "---- ERROR ----\r\n");
|
||||
js_cli_print(ctx, msg);
|
||||
js_cli_print(ctx, "\r\n");
|
||||
break;
|
||||
case JsThreadEventErrorTrace:
|
||||
js_cli_print(ctx, "Trace:\r\n");
|
||||
js_cli_print(ctx, msg);
|
||||
js_cli_print(ctx, "\r\n");
|
||||
|
||||
js_cli_exit(ctx); // Exit when an error occurs
|
||||
break;
|
||||
case JsThreadEventPrint:
|
||||
js_cli_print(ctx, msg);
|
||||
js_cli_print(ctx, "\r\n");
|
||||
break;
|
||||
case JsThreadEventDone:
|
||||
js_cli_print(ctx, "Script done!\r\n");
|
||||
|
||||
js_cli_exit(ctx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void js_cli_execute(Cli* cli, FuriString* args, void* context) {
|
||||
UNUSED(context);
|
||||
|
||||
const char* path = furi_string_get_cstr(args);
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
|
||||
do {
|
||||
if(furi_string_size(args) == 0) {
|
||||
printf("Usage:\r\njs <path>\r\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if(!storage_file_exists(storage, path)) {
|
||||
printf("Can not open file %s\r\n", path);
|
||||
break;
|
||||
}
|
||||
|
||||
JsCliContext ctx = {.cli = cli};
|
||||
ctx.exit_sem = furi_semaphore_alloc(1, 0);
|
||||
|
||||
printf("Running script %s, press CTRL+C to stop\r\n", path);
|
||||
JsThread* js_thread = js_thread_run(path, js_cli_callback, &ctx);
|
||||
|
||||
while(furi_semaphore_acquire(ctx.exit_sem, 100) != FuriStatusOk) {
|
||||
if(cli_cmd_interrupt_received(cli)) break;
|
||||
}
|
||||
|
||||
js_thread_stop(js_thread);
|
||||
furi_semaphore_free(ctx.exit_sem);
|
||||
} while(false);
|
||||
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
void js_app_on_system_start(void) {
|
||||
#ifdef SRV_CLI
|
||||
Cli* cli = furi_record_open(RECORD_CLI);
|
||||
cli_add_command(cli, "js", CliCommandFlagDefault, js_cli_execute, NULL);
|
||||
furi_record_close(RECORD_CLI);
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
|
||||
#define TAG "JS modules"
|
||||
|
||||
// Absolute path is used to make possible plugin load from CLI
|
||||
#define MODULES_PATH "/ext/apps_data/js_app/plugins"
|
||||
|
||||
typedef struct {
|
||||
JsModeConstructor create;
|
||||
JsModeDestructor destroy;
|
||||
|
@ -81,7 +84,7 @@ mjs_val_t js_module_require(JsModules* modules, const char* name, size_t name_le
|
|||
// External module load
|
||||
if(!module_found) {
|
||||
FuriString* module_path = furi_string_alloc();
|
||||
furi_string_printf(module_path, "%s/js_%s.fal", APP_DATA_PATH("plugins"), name);
|
||||
furi_string_printf(module_path, "%s/js_%s.fal", MODULES_PATH, name);
|
||||
FURI_LOG_I(TAG, "Loading external module %s", furi_string_get_cstr(module_path));
|
||||
do {
|
||||
uint32_t plugin_cnt_last = plugin_manager_get_count(modules->plugin_manager);
|
||||
|
|
|
@ -44,12 +44,12 @@ static void js_print(struct mjs* mjs) {
|
|||
FuriString* msg_str = furi_string_alloc();
|
||||
js_str_print(msg_str, mjs);
|
||||
|
||||
printf("%s\r\n", furi_string_get_cstr(msg_str));
|
||||
|
||||
JsThread* worker = mjs_get_context(mjs);
|
||||
furi_assert(worker);
|
||||
if(worker->app_callback) {
|
||||
worker->app_callback(JsThreadEventPrint, furi_string_get_cstr(msg_str), worker->context);
|
||||
} else {
|
||||
FURI_LOG_D(TAG, "%s\r\n", furi_string_get_cstr(msg_str));
|
||||
}
|
||||
|
||||
furi_string_free(msg_str);
|
||||
|
|
Loading…
Reference in a new issue