static enum v7_err jsBotLoadFile(struct v7* v7, v7_val_t* res) { v7_val_t file = v7_arg(v7, 0); if (!v7_is_string(file)) { return V7_OK; } TString fullPath = _activeBot->getWorkDir(); TString fileName = v7_to_cstring(v7, &file); fullPath += fileName.GetAsWChar(); if (!fexists(fullPath)) { return V7_OK; } v7_val_t result; enum v7_err ress = V7_OK; ress = v7_exec_file(v7, fullPath.GetAsChar(), &result); //v7_val_t* v7Obj = _activeBot->getV7Obj(); //*v7Obj = v7_mk_object(v7); if (ress != V7_OK) { if (result == V7_SYNTAX_ERROR) MessageBox(NULL, "script fail syntax error", "Nooo", 0); else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0); else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0); else if (result == V7_AST_TOO_LARGE) MessageBox(NULL, "script fail, ast too large", "Nooo", 0); } return V7_OK; }
void init_smartjs() { v7_val_t res; if (v7_exec_file(v7, "smart.js", &res) != V7_OK) { printf("smart.js execution: "); v7_println(v7, res); } }
int main(int argc, char *argv[]) { struct v7 *v7 = v7_create(); int i, error_code; // Execute inline code for (i = 1; i < argc && argv[i][0] == '-'; i++) { if (strcmp(argv[i], "-e") == 0 && i + 1 < argc) { if ((error_code = v7_exec(v7, argv[i + 1])) != V7_OK) { fprintf(stderr, "Error executing [%s]: %s\n", argv[i + 1], v7_strerror(error_code)); } i++; } } // Execute files for (; i < argc; i++) { if ((error_code = v7_exec_file(v7, argv[i])) != V7_OK) { fprintf(stderr, "%s line %d: %s\n", argv[i], v7->pstate.line_no, v7_strerror(error_code)); } } v7_destroy(&v7); return EXIT_SUCCESS; }
void init_smartjs() { v7_val_t res; if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) { printf("Init error: "); v7_println(v7, res); } }
void sj_common_api_setup(struct v7 *v7) { /* Setup JS API */ #if !defined(V7_THAW) sj_timers_api_setup(v7); sj_v7_ext_api_setup(v7); sj_gpio_api_setup(v7); sj_adc_api_setup(v7); sj_i2c_api_setup(v7); if (v7_exec_file(v7, "I2C.js", NULL) != V7_OK) { fprintf(stderr, "Error evaluating I2C.js\n"); } sj_spi_api_setup(v7); sj_ws_client_api_setup(v7); sj_mqtt_api_setup(v7); sj_debug_api_setup(v7); sj_http_api_setup(v7); sj_pwm_api_setup(v7); sj_wifi_api_setup(v7); sj_udp_tcp_api_setup(v7); // Peripherals sj_ultrasonic_distance_sensor_setup(v7); #ifndef DISABLE_C_CLUBBY sj_clubby_api_setup(v7); #endif #else (void) v7; #endif }
void sj_gpio_api_setup(struct v7 *v7) { v7_val_t gpio = v7_mk_object(v7); v7_set(v7, v7_get_global(v7), "GPIO", ~0, gpio); v7_set_method(v7, gpio, "setMode", GPIO_setMode); v7_set_method(v7, gpio, "read", GPIO_read); v7_set_method(v7, gpio, "write", GPIO_write); v7_set_method(v7, gpio, "setISR", GPIO_setISR); v7_set(v7, gpio, "INOUT", ~0, v7_mk_number(v7, GPIO_MODE_INOUT)); v7_set(v7, gpio, "IN", ~0, v7_mk_number(v7, GPIO_MODE_INPUT)); v7_set(v7, gpio, "OUT", ~0, v7_mk_number(v7, GPIO_MODE_OUTPUT)); v7_set(v7, gpio, "FLOAT", ~0, v7_mk_number(v7, GPIO_PULL_FLOAT)); v7_set(v7, gpio, "PULLUP", ~0, v7_mk_number(v7, GPIO_PULL_PULLUP)); v7_set(v7, gpio, "PULLDOWN", ~0, v7_mk_number(v7, GPIO_PULL_PULLDOWN)); v7_set(v7, gpio, "OFF", ~0, v7_mk_number(v7, GPIO_INTR_OFF)); v7_set(v7, gpio, "POSEDGE", ~0, v7_mk_number(v7, GPIO_INTR_POSEDGE)); v7_set(v7, gpio, "NEGEDGE", ~0, v7_mk_number(v7, GPIO_INTR_NEGEDGE)); v7_set(v7, gpio, "ANYEDGE", ~0, v7_mk_number(v7, GPIO_INTR_ANYEDGE)); v7_set(v7, gpio, "LOLEVEL", ~0, v7_mk_number(v7, GPIO_INTR_LOLEVEL)); v7_set(v7, gpio, "HILEVEL", ~0, v7_mk_number(v7, GPIO_INTR_HILEVEL)); /* * TODO(mkm): figure out what to do with this "esp specific" mode. * It's not really ESP specific, but the soft debouncer is currently * implemented in esp8266 platform code. */ v7_set(v7, gpio, "CLICK", ~0, v7_mk_number(v7, 6 /* GPIO_INTR_TYPE_ONCLICK */)); if (v7_exec_file(v7, "gpio.js", NULL) != V7_OK) { /* TODO(mkm): make setup functions return an error code */ abort(); } }
static void run_init_script(struct v7 *v7) { static const char *init_files[] = {"sys_init.js"}; size_t i; v7_val_t res; /* * Run startup scripts from the directory JS_DIR_NAME. * That directory should be located where the binary (s_argv0) lives. */ for (i = 0; i < sizeof(init_files) / sizeof(init_files[0]); i++) { if (v7_exec_file(v7, init_files[i], &res) != V7_OK) { fprintf(stderr, "Failed to run %s: ", init_files[i]); v7_fprintln(stderr, v7, res); } } }
int main(int argc, char *argv[]) { struct v7 *v7; int i, show_ast = 0; v7 = v7_create(); /* Execute inline code */ for (i = 1; i < argc && argv[i][0] == '-'; i++) { if (strcmp(argv[i], "-e") == 0 && i + 1 < argc) { if (show_ast) { dump_ast(argv[i + 1]); } else if (!v7_exec(v7, argv[i + 1])) { fprintf(stderr, "Error executing [%s]: %s\n", argv[i + 1], v7_get_error_string(v7)); } i++; } else if (strcmp(argv[i], static_show_ast_flag) == 0) { show_ast = 1; } else if (strcmp(argv[i], "-h") == 0) { show_usage(argv); } } /* Execute files */ for (; i < argc; i++) { if (show_ast) { size_t size; char *source_code; if ((source_code = read_file(argv[i], &size)) == NULL) { fprintf(stderr, "Cannot read [%s]\n", argv[i]); } else { dump_ast(source_code); free(source_code); } } else if (!v7_exec_file(v7, argv[i])) { fprintf(stderr, "%s\n", v7_get_error_string(v7)); } } v7_destroy(&v7); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { const char *script = "nsv7.js", *port = "4000"; struct ns_server server; struct v7 *v7; int i; // Parse command line options for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-f") == 0 && i + 1 < argc) { script = argv[++i]; } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { port = argv[++i]; } else { elog(1, "Usage: %s [-f FILE] [-p PORT]", argv[0]); } } signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); // Initialize scripting engine v7 = v7_create(); v7_init_stdlib(v7); if (v7_exec_file(v7, script) != V7_OK) { elog(1, "Error executing %s", script); } // Initialize server ns_server_init(&server, v7, ev_handler); ns_bind(&server, port); while (s_received_signal == 0) { ns_server_poll(&server, 1000); } printf("Existing on signal %d\n", s_received_signal); v7_destroy(&v7); ns_server_free(&server); return EXIT_SUCCESS; }
int main(void) { size_t n; const char *domain_str; struct v7 *v7 = v7_create(); v7_val_t domain, port0, config; /* Load JSON configuration */ if (v7_exec_file(v7, &config, "config.json") != V7_OK) { printf("%s\n", "Cannot load JSON config"); return 1; } /* Lookup values in JSON configuration object */ domain = v7_get(v7, config, "domain", 6); port0 = v7_array_get(v7, v7_get(v7, config, "ports", 5), 0); domain_str = v7_to_string(v7, &domain, &n); printf("Domain: [%.*s], port 0: [%d]\n", (int) n, domain_str, (int) v7_to_number(port0)); v7_destroy(v7); return 0; }
static void pre_init(struct v7 *v7) { static const char *init_files[] = {"smart.js", "user.js"}; const char *dir = s_argv0 + strlen(s_argv0) - 1; char path[512]; size_t i; v7_val_t res; /* * Point `dir` to the right-most directory separator of the smartjs binary. * Thus string between `s_argv0` and `dir` pointers would contain a directory * name where our executable lives. */ while (dir > s_argv0 && *dir != '/' && *dir != '\\') { dir--; } snprintf(path, sizeof(path), "%.*s/%s", (int) (dir - s_argv0), s_argv0, JS_FS_ROOT); /* All the files, conf, JS, etc are addressed relative to the current dir */ if (chdir(path) != 0) { fprintf(stderr, "cannot chdir to %s\n", path); } sj_init_v7_ext(v7); init_smartjs(v7); /* * Run startup scripts from the directory JS_DIR_NAME. * That directory should be located where the binary (s_argv0) lives. */ for (i = 0; i < sizeof(init_files) / sizeof(init_files[0]); i++) { if (v7_exec_file(v7, &res, init_files[i]) != V7_OK) { fprintf(stderr, "Failed to run %s: ", init_files[i]); v7_fprintln(stderr, v7, res); } } }
int main(int argc, char *argv[]) { int i, error_code; signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); s_v7 = v7_create(); v7_init_stdlib(s_v7); v7_set_func(s_v7, v7_get_root_namespace(s_v7), "WebsocketServer", js_ws); v7_set_func(s_v7, v7_get_root_namespace(s_v7), "RunTcpServer", js_tcp); for (i = 1; i < argc; i++) { if ((error_code = v7_exec_file(s_v7, argv[i])) != V7_OK) { fprintf(stderr, "Error executing %s line %d: %s\n", argv[i], s_v7->line_no, v7_err_to_str(error_code)); exit(EXIT_FAILURE); } } fprintf(stderr, "Existing on signal %d\n", s_received_signal); v7_destroy(&s_v7); return EXIT_SUCCESS; }
Bot::Bot(bot_manager* mgr, const char* name, const char* workDir) { _mgr = mgr; _name = name; /*_workDir = std::string(ExePath()).c_str(); _workDir += L"\\plugins\\BMP\\"; TString botNamee = name; botNamee.ToUpper(); _workDir += botNamee.GetAsWChar(); MessageBox(NULL, _workDir.GetAsChar(), "Work Dir", 0);*/ setActive(); _workDir = workDir; _workDir += L"bmp\\"; bool isScriptOk = true; if (!EnsureDirExists(_workDir.GetAsWChar())) { isScriptOk = false; } _charset.color = RGB(0xff, 0x99, 0x00); _activeBot = this; _scriptFile = _workDir.GetAsWChar(); _scriptFile += L"main.js"; if (isScriptOk && !FileExists(std::string(_scriptFile.GetAsChar()))) { TString strSay = L"BMP Plugin will not work, please put file 'main.js' into work dir. ("; strSay += _scriptFile.GetAsWChar(); strSay += L")"; //say(strSay); MessageBox(NULL, strSay.GetAsChar(), "BMP Bot Alert", 0); isScriptOk = false; } v7 = v7_create(); enum v7_err rcode = V7_OK; v7_val_t result; //events function define v7_set_method(v7, v7_get_global(v7), "on", (v7_cfunction_t*)&jsBotEvent); //utilities functions v7_set_method(v7, v7_get_global(v7), "say", (v7_cfunction_t*)&jsBotSay); v7_set_method(v7, v7_get_global(v7), "setTimeout", (v7_cfunction_t*)&jsSetTimeout); v7_set_method(v7, v7_get_global(v7), "setInterval", (v7_cfunction_t*)&jsSetInterval); v7_set_method(v7, v7_get_global(v7), "clearTimeout", (v7_cfunction_t*)&jsClearTimeout); v7_set_method(v7, v7_get_global(v7), "clearInterval", (v7_cfunction_t*)&jsClearInterval); botObj = new v7_val_t; v7_own(v7, botObj); *botObj = v7_mk_object(v7); v7_val_t botName = v7_mk_string(v7, name, ~0, 1); v7_set(v7, *botObj, "name", ~0, botName); v7_set(v7, v7_get_global(v7), "BOT", ~0, *botObj); v7_set_method(v7, *botObj, "setFontStyle", (v7_cfunction_t*)&jsBotFontStyle); v7_set_method(v7, *botObj, "load", (v7_cfunction_t*)&jsBotLoadFile); //rcode = v7_exec(v7, "var a = \"aaa\"; say(a); print(\"Yeah\");", &result); if (isScriptOk) { rcode = v7_exec_file(v7, _scriptFile.GetAsChar(), &result); if (rcode != V7_OK) { if (result == V7_SYNTAX_ERROR) MessageBox(NULL, "script fail syntax error", "Nooo", 0); else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0); else if (result == V7_EXEC_EXCEPTION) MessageBox(NULL, "script fail, exception", "Nooo", 0); else if (result == V7_AST_TOO_LARGE) MessageBox(NULL, "script fail, ast too large", "Nooo", 0); } else { v7_val_t vObj; v7_own(v7, &vObj); vObj = v7_mk_object(v7); v7Obj = &vObj; } } }
static int sj_init() { struct v7 *v7 = s_v7; LOG(LL_INFO, ("Mongoose IoT Firmware %s", build_id)); LOG(LL_INFO, ("RAM: %d total, %d free", sj_get_heap_size(), sj_get_free_heap_size())); int r = start_nwp(); if (r < 0) { LOG(LL_ERROR, ("Failed to start NWP: %d", r)); return 0; } int boot_cfg_idx = get_active_boot_cfg_idx(); if (boot_cfg_idx < 0) return 0; struct boot_cfg boot_cfg; if (read_boot_cfg(boot_cfg_idx, &boot_cfg) < 0) return 0; LOG(LL_INFO, ("Boot cfg %d: 0x%llx, 0x%u, %s @ 0x%08x, %s", boot_cfg_idx, boot_cfg.seq, boot_cfg.flags, boot_cfg.app_image_file, boot_cfg.app_load_addr, boot_cfg.fs_container_prefix)); uint64_t saved_seq = 0; if (boot_cfg.flags & BOOT_F_FIRST_BOOT) { /* Tombstone the current config. If anything goes wrong between now and * commit, next boot will use the old one. */ saved_seq = boot_cfg.seq; boot_cfg.seq = BOOT_CFG_TOMBSTONE_SEQ; write_boot_cfg(&boot_cfg, boot_cfg_idx); } r = init_fs(boot_cfg.fs_container_prefix); if (r < 0) { LOG(LL_ERROR, ("FS init error: %d", r)); if (boot_cfg.flags & BOOT_F_FIRST_BOOT) { revert_update(boot_cfg_idx, &boot_cfg); } return 0; } if (boot_cfg.flags & BOOT_F_FIRST_BOOT) { LOG(LL_INFO, ("Applying update")); if (apply_update(boot_cfg_idx, &boot_cfg) < 0) { revert_update(boot_cfg_idx, &boot_cfg); } } mongoose_init(); #ifndef CS_DISABLE_JS v7 = s_v7 = init_v7(&v7); /* Disable GC during JS API initialization. */ v7_set_gc_enabled(v7, 0); sj_gpio_api_setup(v7); sj_i2c_api_setup(v7); sj_wifi_api_setup(v7); sj_timers_api_setup(v7); #endif sj_v7_ext_api_setup(v7); sj_init_sys(v7); sj_wifi_init(v7); #ifndef DISABLE_C_CLUBBY sj_clubby_init(); #endif sj_http_api_setup(v7); #if !defined(DISABLE_C_CLUBBY) && !defined(CS_DISABLE_JS) sj_clubby_api_setup(v7); #endif /* Common config infrastructure. Mongoose & v7 must be initialized. */ init_device(v7); sj_updater_post_init(v7); #ifndef DISABLE_C_CLUBBY init_updater_clubby(v7); #endif #ifndef CS_DISABLE_JS /* SJS initialized, enable GC back, and trigger it */ v7_set_gc_enabled(v7, 1); v7_gc(v7, 1); v7_val_t res; if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) { fprintf(stderr, "Error: "); v7_fprint(stderr, v7, res); } #endif LOG(LL_INFO, ("%s init done, RAM: %d free", "Sys", sj_get_free_heap_size())); if (!sj_app_init(v7)) { LOG(LL_ERROR, ("App init failed")); abort(); } LOG(LL_INFO, ("%s init done, RAM: %d free", "App", sj_get_free_heap_size())); if (boot_cfg.flags & BOOT_F_FIRST_BOOT) { boot_cfg.seq = saved_seq; commit_update(boot_cfg_idx, &boot_cfg); clubby_updater_finish(0); } else { /* * If there is no update reply state, this will just be ignored. * But if there is, then update was rolled back and reply will be sent. */ clubby_updater_finish(-1); } #ifndef CS_DISABLE_JS sj_prompt_init(v7); #endif return 1; }
static void uart_int() { int c = UARTCharGet(CONSOLE_UART); struct prompt_event pe = {.type = PROMPT_CHAR_EVENT, .data = (void *) c}; osi_MsgQWrite(&s_v7_q, &pe, OSI_NO_WAIT); MAP_UARTIntClear(CONSOLE_UART, UART_INT_RX); } void sj_prompt_init_hal(struct v7 *v7) { (void) v7; } static void v7_task(void *arg) { struct v7 *v7 = s_v7; printf("\n\nSmart.JS for CC3200\n"); osi_MsgQCreate(&s_v7_q, "V7", sizeof(struct prompt_event), 32 /* len */); osi_InterruptRegister(CONSOLE_UART_INT, uart_int, INT_PRIORITY_LVL_1); MAP_UARTIntEnable(CONSOLE_UART, UART_INT_RX); sl_Start(NULL, NULL, NULL); v7 = s_v7 = init_v7(&v7); sj_init_timers(v7); sj_init_v7_ext(v7); init_wifi(v7); if (init_fs(v7) != 0) { fprintf(stderr, "FS initialization failed.\n"); } mongoose_init(); sj_init_http(v7); init_i2cjs(v7); /* Common config infrastructure. Mongoose & v7 must be initialized. */ init_device(v7); v7_val_t res; if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) { fprintf(stderr, "Error: "); v7_fprint(stderr, v7, res); } sj_prompt_init(v7); while (1) { struct prompt_event pe; mongoose_poll(MONGOOSE_POLL_LENGTH_MS); if (osi_MsgQRead(&s_v7_q, &pe, V7_POLL_LENGTH_MS) != OSI_OK) continue; switch (pe.type) { case PROMPT_CHAR_EVENT: { sj_prompt_process_char((char) ((int) pe.data)); break; } case V7_INVOKE_EVENT: { struct v7_invoke_event_data *ied = (struct v7_invoke_event_data *) pe.data; _sj_invoke_cb(v7, ied->func, ied->this_obj, ied->args); v7_disown(v7, &ied->args); v7_disown(v7, &ied->this_obj); v7_disown(v7, &ied->func); free(ied); break; } } } } /* Int vector table, defined in startup_gcc.c */ extern void (*const g_pfnVectors[])(void); void device_reboot(void) { sj_system_restart(); } int main() { MAP_IntVTableBaseSet((unsigned long) &g_pfnVectors[0]); MAP_IntEnable(FAULT_SYSTICK); MAP_IntMasterEnable(); PRCMCC3200MCUInit(); cc3200_leds_init(); /* Console UART init. */ MAP_PRCMPeripheralClkEnable(CONSOLE_UART_PERIPH, PRCM_RUN_MODE_CLK); MAP_PinTypeUART(PIN_55, PIN_MODE_3); /* PIN_55 -> UART0_TX */ MAP_PinTypeUART(PIN_57, PIN_MODE_3); /* PIN_57 -> UART0_RX */ MAP_UARTConfigSetExpClk( CONSOLE_UART, MAP_PRCMPeripheralClockGet(CONSOLE_UART_PERIPH), CONSOLE_BAUD_RATE, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); MAP_UARTFIFODisable(CONSOLE_UART); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); VStartSimpleLinkSpawnTask(8); osi_TaskCreate(v7_task, (const signed char *) "v7", V7_STACK_SIZE + 256, NULL, 3, NULL); osi_TaskCreate(blinkenlights_task, (const signed char *) "blink", 256, NULL, 9, NULL); osi_start(); return 0; }
static void uart_int() { int c = UARTCharGet(CONSOLE_UART); struct prompt_event pe = {.type = PROMPT_CHAR_EVENT, .data = (void *) c}; osi_MsgQWrite(&s_v7_q, &pe, OSI_NO_WAIT); MAP_UARTIntClear(CONSOLE_UART, UART_INT_RX); } void sj_prompt_init_hal(struct v7 *v7) { (void) v7; } static void v7_task(void *arg) { struct v7 *v7 = s_v7; printf("\n\nSmart.JS for CC3200\n"); osi_MsgQCreate(&s_v7_q, "V7", sizeof(struct prompt_event), 32 /* len */); osi_InterruptRegister(CONSOLE_UART_INT, uart_int, INT_PRIORITY_LVL_1); MAP_UARTIntEnable(CONSOLE_UART, UART_INT_RX); sl_Start(NULL, NULL, NULL); v7 = s_v7 = init_v7(&v7); sj_timers_api_setup(v7); sj_v7_ext_api_setup(v7); sj_init_sys(v7); init_wifi(v7); if (init_fs(v7) != 0) { fprintf(stderr, "FS initialization failed.\n"); } mongoose_init(); sj_http_api_setup(v7); sj_i2c_api_setup(v7); /* Common config infrastructure. Mongoose & v7 must be initialized. */ init_device(v7); v7_val_t res; if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) { fprintf(stderr, "Error: "); v7_fprint(stderr, v7, res); } sj_prompt_init(v7); while (1) { struct prompt_event pe; mongoose_poll(MONGOOSE_POLL_LENGTH_MS); if (osi_MsgQRead(&s_v7_q, &pe, V7_POLL_LENGTH_MS) != OSI_OK) continue; switch (pe.type) { case PROMPT_CHAR_EVENT: { sj_prompt_process_char((char) ((int) pe.data)); break; } case V7_INVOKE_EVENT: { struct v7_invoke_event_data *ied = (struct v7_invoke_event_data *) pe.data; _sj_invoke_cb(v7, ied->func, ied->this_obj, ied->args); v7_disown(v7, &ied->args); v7_disown(v7, &ied->this_obj); v7_disown(v7, &ied->func); free(ied); break; } } } } /* Int vector table, defined in startup_gcc.c */ extern void (*const g_pfnVectors[])(void); void device_reboot(void) { sj_system_restart(0); }