1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include <stdio.h>
17425bb815Sopenharmony_ci#include <stdint.h>
18425bb815Sopenharmony_ci#include <string.h>
19425bb815Sopenharmony_ci
20425bb815Sopenharmony_ci/* Infra */
21425bb815Sopenharmony_ci#include "infra/bsp.h"
22425bb815Sopenharmony_ci#include "infra/reboot.h"
23425bb815Sopenharmony_ci#include "infra/log.h"
24425bb815Sopenharmony_ci#include "infra/time.h"
25425bb815Sopenharmony_ci#include "infra/system_events.h"
26425bb815Sopenharmony_ci#include "infra/tcmd/handler.h"
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci#include "cfw/cfw.h"
29425bb815Sopenharmony_ci/* Watchdog helper */
30425bb815Sopenharmony_ci#include "infra/wdt_helper.h"
31425bb815Sopenharmony_ci
32425bb815Sopenharmony_ci#include "jerryscript.h"
33425bb815Sopenharmony_ci#include "jerryscript-port.h"
34425bb815Sopenharmony_ci#include "string.h"
35425bb815Sopenharmony_ci
36425bb815Sopenharmony_ci#include "zephyr.h"
37425bb815Sopenharmony_ci#include "microkernel/task.h"
38425bb815Sopenharmony_ci#include "os/os.h"
39425bb815Sopenharmony_ci#include "misc/printk.h"
40425bb815Sopenharmony_ci
41425bb815Sopenharmony_cistatic T_QUEUE queue;
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_cijerry_value_t print_function;
44425bb815Sopenharmony_ci
45425bb815Sopenharmony_civoid jerry_resolve_error (jerry_value_t ret_value)
46425bb815Sopenharmony_ci{
47425bb815Sopenharmony_ci  if (jerry_value_is_error (ret_value))
48425bb815Sopenharmony_ci  {
49425bb815Sopenharmony_ci    ret_value = jerry_get_value_from_error (ret_value, true);
50425bb815Sopenharmony_ci    jerry_value_t err_str_val = jerry_value_to_string (ret_value);
51425bb815Sopenharmony_ci    jerry_size_t err_str_size = jerry_get_utf8_string_size (err_str_val);
52425bb815Sopenharmony_ci    jerry_char_t *err_str_buf = (jerry_char_t *) balloc (err_str_size, NULL);
53425bb815Sopenharmony_ci    jerry_size_t sz = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size);
54425bb815Sopenharmony_ci    err_str_buf[sz] = 0;
55425bb815Sopenharmony_ci    printk ("Script Error: unhandled exception: %s\n", err_str_buf);
56425bb815Sopenharmony_ci    bfree(err_str_buf);
57425bb815Sopenharmony_ci    jerry_release_value (err_str_val);
58425bb815Sopenharmony_ci  }
59425bb815Sopenharmony_ci}
60425bb815Sopenharmony_ci
61425bb815Sopenharmony_civoid help ()
62425bb815Sopenharmony_ci{
63425bb815Sopenharmony_ci  printk ("Usage:\n");
64425bb815Sopenharmony_ci  printk ("js e 'JavaScript Command'\n");
65425bb815Sopenharmony_ci  printk ("eg. js e print ('Hello World');\n");
66425bb815Sopenharmony_ci}
67425bb815Sopenharmony_ci
68425bb815Sopenharmony_civoid eval_jerry_script (int argc, char *argv[], struct tcmd_handler_ctx *ctx)
69425bb815Sopenharmony_ci{
70425bb815Sopenharmony_ci  if (argc < 3)
71425bb815Sopenharmony_ci  {
72425bb815Sopenharmony_ci    TCMD_RSP_ERROR (ctx, NULL);
73425bb815Sopenharmony_ci    help ();
74425bb815Sopenharmony_ci    return;
75425bb815Sopenharmony_ci  }
76425bb815Sopenharmony_ci  else
77425bb815Sopenharmony_ci  {
78425bb815Sopenharmony_ci    OS_ERR_TYPE err;
79425bb815Sopenharmony_ci    size_t str_total_length = 0;
80425bb815Sopenharmony_ci    size_t *str_lens = (size_t *) balloc ((argc - 2) * sizeof(size_t), &err);
81425bb815Sopenharmony_ci    if (str_lens == NULL || err != E_OS_OK)
82425bb815Sopenharmony_ci    {
83425bb815Sopenharmony_ci      printk ("%s: allocate memory failed!", __func__);
84425bb815Sopenharmony_ci      TCMD_RSP_ERROR (ctx, NULL);
85425bb815Sopenharmony_ci      return;
86425bb815Sopenharmony_ci    }
87425bb815Sopenharmony_ci    for (int i = 2; i < argc; ++i)
88425bb815Sopenharmony_ci    {
89425bb815Sopenharmony_ci      str_lens[i - 2] = strlen (argv[i]);
90425bb815Sopenharmony_ci      str_total_length += str_lens[i - 2] + 1;
91425bb815Sopenharmony_ci    }
92425bb815Sopenharmony_ci    err = E_OS_OK;
93425bb815Sopenharmony_ci    char *buffer = (char *) balloc (str_total_length, &err);
94425bb815Sopenharmony_ci    if (buffer == NULL || err != E_OS_OK)
95425bb815Sopenharmony_ci    {
96425bb815Sopenharmony_ci      printk ("%s: allocate memory failed!", __func__);
97425bb815Sopenharmony_ci      TCMD_RSP_ERROR (ctx, NULL);
98425bb815Sopenharmony_ci      return;
99425bb815Sopenharmony_ci    }
100425bb815Sopenharmony_ci
101425bb815Sopenharmony_ci    char *p = buffer;
102425bb815Sopenharmony_ci    for (int i = 2; i < argc; ++i)
103425bb815Sopenharmony_ci    {
104425bb815Sopenharmony_ci      for (int j =0; j < str_lens[i - 2]; ++j)
105425bb815Sopenharmony_ci      {
106425bb815Sopenharmony_ci        *p = argv[i][j];
107425bb815Sopenharmony_ci        ++p;
108425bb815Sopenharmony_ci      }
109425bb815Sopenharmony_ci      *p = ' ';
110425bb815Sopenharmony_ci      ++p;
111425bb815Sopenharmony_ci    }
112425bb815Sopenharmony_ci    *p = '\0';
113425bb815Sopenharmony_ci
114425bb815Sopenharmony_ci    jerry_value_t eval_ret = jerry_eval (buffer, str_total_length - 1, JERRY_PARSE_NO_OPTS);
115425bb815Sopenharmony_ci
116425bb815Sopenharmony_ci    if (jerry_value_is_error (eval_ret))
117425bb815Sopenharmony_ci    {
118425bb815Sopenharmony_ci      jerry_resolve_error (eval_ret);
119425bb815Sopenharmony_ci      TCMD_RSP_ERROR (ctx, NULL);
120425bb815Sopenharmony_ci    }
121425bb815Sopenharmony_ci    else
122425bb815Sopenharmony_ci    {
123425bb815Sopenharmony_ci      jerry_value_t args[] = {eval_ret};
124425bb815Sopenharmony_ci      jerry_value_t ret_val_print = jerry_call_function (print_function,
125425bb815Sopenharmony_ci                                                         jerry_create_undefined (),
126425bb815Sopenharmony_ci                                                         args,
127425bb815Sopenharmony_ci                                                         1);
128425bb815Sopenharmony_ci      jerry_release_value (ret_val_print);
129425bb815Sopenharmony_ci      TCMD_RSP_FINAL (ctx, NULL);
130425bb815Sopenharmony_ci    }
131425bb815Sopenharmony_ci    jerry_release_value (eval_ret);
132425bb815Sopenharmony_ci    bfree (buffer);
133425bb815Sopenharmony_ci    bfree (str_lens);
134425bb815Sopenharmony_ci  }
135425bb815Sopenharmony_ci}
136425bb815Sopenharmony_ci
137425bb815Sopenharmony_civoid jerry_start ()
138425bb815Sopenharmony_ci{
139425bb815Sopenharmony_ci  union { double d; unsigned u; } now = { .d = jerry_port_get_current_time () };
140425bb815Sopenharmony_ci  srand (now.u);
141425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
142425bb815Sopenharmony_ci  jerry_value_t global_obj_val = jerry_get_global_object ();
143425bb815Sopenharmony_ci  jerry_value_t print_func_name_val = jerry_create_string ((jerry_char_t *) "print");
144425bb815Sopenharmony_ci  print_function = jerry_get_property (global_obj_val, print_func_name_val);
145425bb815Sopenharmony_ci  jerry_release_value (print_func_name_val);
146425bb815Sopenharmony_ci  jerry_release_value (global_obj_val);
147425bb815Sopenharmony_ci}
148425bb815Sopenharmony_ci
149425bb815Sopenharmony_ci/* Application main entry point */
150425bb815Sopenharmony_civoid main_task (void *param)
151425bb815Sopenharmony_ci{
152425bb815Sopenharmony_ci  /* Init BSP (also init BSP on ARC core) */
153425bb815Sopenharmony_ci  queue = bsp_init ();
154425bb815Sopenharmony_ci  /* start Quark watchdog */
155425bb815Sopenharmony_ci  wdt_start (WDT_MAX_TIMEOUT_MS);
156425bb815Sopenharmony_ci  /* Init the CFW */
157425bb815Sopenharmony_ci  cfw_init (queue);
158425bb815Sopenharmony_ci  jerry_start ();
159425bb815Sopenharmony_ci  /* Loop to process message queue */
160425bb815Sopenharmony_ci  while (1)
161425bb815Sopenharmony_ci  {
162425bb815Sopenharmony_ci    OS_ERR_TYPE err = E_OS_OK;
163425bb815Sopenharmony_ci    /* Process message with a given timeout */
164425bb815Sopenharmony_ci    queue_process_message_wait (queue, 5000, &err);
165425bb815Sopenharmony_ci    /* Acknowledge the system watchdog to prevent panic and reset */
166425bb815Sopenharmony_ci    wdt_keepalive ();
167425bb815Sopenharmony_ci  }
168425bb815Sopenharmony_ci}
169425bb815Sopenharmony_ci
170425bb815Sopenharmony_ciDECLARE_TEST_COMMAND (js, e, eval_jerry_script);
171