Skip to content

Commit 314e74f

Browse files
committed
targets/zephyr/Makefile.zephyr: Use zephyr_getline module for line input.
The original implementation used shell facility, but it was designed for a unix shell like input, and automatically tokenized it into space-separated "words", with limit of 10 (i.e. 9 spaces per line). For JavaScript input, it is quite easy to have more than 9 spaces per line, and get error: Too many parameters (max 10) After consultation with upstream (https://jira.zephyrproject.org/browse/ZEP-532) it was decided that the best approach is to skip using shell facility and use Zephyr console facility. That however requires some Zephyr-specific boilerplate code. This code was implemented as reusable modules in https://github.com/pfalcon/zephyr_console_helpers repository, to be usable for other console-based projects too. zephyr_getline.h/c in this commits are direct imports from this repository. JerryScript-DCO-1.0-Signed-off-by: Paul Sokolovsky [email protected]
1 parent 2725311 commit 314e74f

File tree

5 files changed

+106
-136
lines changed

5 files changed

+106
-136
lines changed

targets/zephyr/README.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,21 @@ Test command line in a serial terminal.
132132

133133
You should see something similar to this:
134134
```
135-
Jerry Compilation May 26 2016 13:37:50
135+
JerryScript build: Aug 12 2016 17:12:55
136+
JerryScript API 1.0
137+
Zephyr version 1.4.0
136138
js>
137139
```
138140

139141

140142
Run the example javascript command test function
141143
```
142-
js> test
143-
Script [var test=0; for (t=100; t<1000; t++) test+=t; print ('Hi JS World! '+test);]
144+
js> var test=0; for (t=100; t<1000; t++) test+=t; print ('Hi JS World! '+test);
144145
Hi JS World! 494550
145146
```
146147

147148

148-
Try more complex functions:
149+
Try a more complex function:
149150
```
150-
js>function hello(t){t=t*10;return t}; print("result"+hello(10.5));
151+
js> function hello(t) {t=t*10;return t}; print("result"+hello(10.5));
151152
```
152-
153-
154-
Help will provide a list of commands
155-
```
156-
> help
157-
```
158-
159-
This program, is built in top of the Zephyr command line, so there is a limit of 10 spaces.

targets/zephyr/src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ endif
1919
# Adding path for jerry script APIs
2020
ZEPHYRINCLUDE += -I$(JERRY_INCLUDE)
2121

22-
obj-y += main-zephyr.o
22+
obj-y += main-zephyr.o getline-zephyr.o

targets/zephyr/src/getline-zephyr.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2016 Linaro
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <zephyr.h>
18+
#include <uart.h>
19+
#include <drivers/console/uart_console.h>
20+
#include "getline-zephyr.h"
21+
22+
/* While app processes one input line, Zephyr will have another line
23+
buffer to accumulate more console input. */
24+
static struct uart_console_input line_bufs[2];
25+
26+
static struct nano_fifo free_queue;
27+
static struct nano_fifo used_queue;
28+
29+
char *zephyr_getline(void)
30+
{
31+
static struct uart_console_input *cmd;
32+
33+
/* Recycle cmd buffer returned previous time */
34+
if (cmd != NULL)
35+
{
36+
nano_fifo_put(&free_queue, cmd);
37+
}
38+
39+
cmd = nano_fifo_get(&used_queue, TICKS_UNLIMITED);
40+
return cmd->line;
41+
}
42+
43+
void zephyr_getline_init(void)
44+
{
45+
int i;
46+
47+
nano_fifo_init(&used_queue);
48+
nano_fifo_init(&free_queue);
49+
for (i = 0; i < sizeof(line_bufs) / sizeof(*line_bufs); i++)
50+
{
51+
nano_fifo_put(&free_queue, &line_bufs[i]);
52+
}
53+
54+
/* Zephyr UART handler takes an empty buffer from free_queue,
55+
stores UART input in it until EOL, and then puts it into
56+
used_queue. */
57+
uart_register_input(&free_queue, &used_queue, NULL);
58+
}

targets/zephyr/src/getline-zephyr.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2016 Linaro
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
void zephyr_getline_init(void);
18+
char *zephyr_getline(void);

targets/zephyr/src/main-zephyr.c

Lines changed: 23 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -20,120 +20,20 @@
2020
#include <zephyr.h>
2121
#include <misc/printk.h>
2222
#include <misc/shell.h>
23+
#include "getline-zephyr.h"
2324

2425
#include "jerry-api.h"
2526

26-
#if defined (CONFIG_STDOUT_CONSOLE)
27-
#include <stdio.h>
28-
#define PRINT printf
29-
#else
30-
#include <misc/printk.h>
31-
#define PRINT printk
32-
#endif
33-
34-
static char *source_buffer = NULL;
35-
static unsigned char flags = 0;
3627
static jerry_value_t print_function;
3728

38-
#define VERBOSE 0x01
39-
40-
/**
41-
* Jerryscript simple test loop
42-
*/
43-
int jerryscript_test ()
29+
static int shell_cmd_handler (char *source_buffer)
4430
{
4531
jerry_value_t ret_val;
4632

47-
const char script[] =
48-
"var test=0; " \
49-
"for (var t=100; t<1000; t++) test+=t; " \
50-
"print ('Hi JS World! '+test);";
51-
52-
printf ("Script [%s]\n", script);
53-
ret_val = jerry_eval ((jerry_char_t *) script,
54-
strlen (script),
55-
false);
56-
57-
return jerry_value_has_error_flag (ret_val) ? -1 : 0;
58-
} /* jerryscript_test */
59-
60-
61-
static int shell_cmd_verbose (int argc, char *argv[])
62-
{
63-
printf ("Enable verbose \n");
64-
flags |= VERBOSE;
65-
return 0;
66-
} /* shell_cmd_verbose */
67-
68-
69-
static int shell_cmd_syntax_help (int argc, char *argv[])
70-
{
71-
printf ("version jerryscript & zephyr versions\n");
72-
return 0;
73-
} /* shell_cmd_syntax_help */
74-
75-
76-
static int shell_cmd_version (int argc, char *argv[])
77-
{
78-
uint32_t version = sys_kernel_version_get ();
79-
80-
printf ("Jerryscript API %d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION);
81-
82-
printk ("Zephyr version %d.%d.%d\n", SYS_KERNEL_VER_MAJOR (version),
83-
SYS_KERNEL_VER_MINOR (version),
84-
SYS_KERNEL_VER_PATCHLEVEL (version));
85-
return 0;
86-
} /* shell_cmd_version */
87-
88-
89-
static int shell_cmd_test (int argc, char *argv[])
90-
{
91-
return jerryscript_test ();
92-
} /* shell_cmd_test */
93-
94-
95-
static int shell_cmd_handler (int argc, char *argv[])
96-
{
97-
if (argc <= 0)
98-
{
99-
return -1;
100-
}
101-
102-
unsigned int size = 0;
103-
for (int t = 0; t < argc; t++)
104-
{
105-
size += strlen (argv[t]) + 1;
106-
}
107-
108-
source_buffer = (char *) malloc (size);
109-
110-
char *d = source_buffer;
111-
unsigned int len;
112-
113-
for (int t = 0; t < argc; t++)
114-
{
115-
len = strlen (argv[t]);
116-
memcpy (d, argv[t], len);
117-
d += len;
118-
*d = ' ';
119-
d++;
120-
}
121-
122-
* (d - 1) = '\0';
123-
124-
if (flags & VERBOSE)
125-
{
126-
printf ("[%s] %lu\n", source_buffer, strlen (source_buffer));
127-
}
128-
129-
jerry_value_t ret_val;
130-
13133
ret_val = jerry_eval ((jerry_char_t *) source_buffer,
13234
strlen (source_buffer),
13335
false);
13436

135-
free (source_buffer);
136-
13737
if (jerry_value_has_error_flag (ret_val))
13838
{
13939
/* User-friendly error messages require at least "cp" JerryScript
@@ -159,21 +59,16 @@ static int shell_cmd_handler (int argc, char *argv[])
15959
return 0;
16060
} /* shell_cmd_handler */
16161

162-
#define SHELL_COMMAND(name,cmd) { name, cmd }
163-
164-
const struct shell_cmd commands[] =
165-
{
166-
SHELL_COMMAND ("syntax", shell_cmd_syntax_help),
167-
SHELL_COMMAND ("version", shell_cmd_version),
168-
SHELL_COMMAND ("test", shell_cmd_test),
169-
SHELL_COMMAND ("verbose", shell_cmd_verbose),
170-
SHELL_COMMAND (NULL, NULL)
171-
};
172-
173-
17462
void main (void)
17563
{
176-
printf ("Jerry Compilation " __DATE__ " " __TIME__ "\n");
64+
uint32_t zephyr_ver = sys_kernel_version_get ();
65+
printf ("JerryScript build: " __DATE__ " " __TIME__ "\n");
66+
printf ("JerryScript API %d.%d\n", JERRY_API_MAJOR_VERSION, JERRY_API_MINOR_VERSION);
67+
printf ("Zephyr version %d.%d.%d\n", (int)SYS_KERNEL_VER_MAJOR (zephyr_ver),
68+
(int)SYS_KERNEL_VER_MINOR (zephyr_ver),
69+
(int)SYS_KERNEL_VER_PATCHLEVEL (zephyr_ver));
70+
71+
zephyr_getline_init ();
17772
jerry_init (JERRY_INIT_EMPTY);
17873
jerry_value_t global_obj_val = jerry_get_global_object ();
17974

@@ -186,11 +81,17 @@ void main (void)
18681
printf ("Error: could not look up print function, expression results won't be printed\n");
18782
}
18883

189-
shell_register_app_cmd_handler (shell_cmd_handler);
190-
shell_init ("js> ", commands);
191-
/* Don't call jerry_cleanup() here, as shell_init() returns after setting
192-
up background task to process shell input, and that task calls
193-
shell_cmd_handler(), etc. as callbacks. This processing happens in
194-
the infinite loop, so JerryScript doesn't need to be de-initialized. */
195-
} /* main */
84+
while (1)
85+
{
86+
char *s;
87+
printf("js> ");
88+
fflush(stdout);
89+
s = zephyr_getline ();
90+
if (*s)
91+
{
92+
shell_cmd_handler (s);
93+
}
94+
}
19695

96+
/* As we never retturn from REPL above, don't call jerry_cleanup() here. */
97+
} /* main */

0 commit comments

Comments
 (0)