1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #include "c_types.h"
20 #include "gpio.h"
21
22 #include "jerryscript.h"
23 #include "jerry_extapi.h"
24
25 #define __UNUSED__ __attribute__((unused))
26
27 #define DELCARE_HANDLER(NAME) \
28 static jerry_value_t \
29 NAME ## _handler (const jerry_value_t function_obj_val __UNUSED__, \
30 const jerry_value_t this_val __UNUSED__, \
31 const jerry_value_t args_p[], \
32 const jerry_length_t args_cnt)
33
34 #define REGISTER_HANDLER(NAME) \
35 register_native_function ( # NAME, NAME ## _handler)
36
DELCARE_HANDLER(assert)37 DELCARE_HANDLER(assert) {
38 if (args_cnt == 1
39 && jerry_value_is_boolean (args_p[0])
40 && jerry_get_boolean_value (args_p[0]))
41 {
42 printf (">> Jerry assert true\r\n");
43 return jerry_create_boolean (true);
44 }
45 printf ("Script assertion failed\n");
46 exit (JERRY_STANDALONE_EXIT_CODE_FAIL);
47 return jerry_create_boolean (false);
48 } /* assert */
49
50
DELCARE_HANDLER(print)51 DELCARE_HANDLER(print) {
52 if (args_cnt)
53 {
54 for (jerry_length_t cc = 0; cc < args_cnt; cc++)
55 {
56 if (jerry_value_is_string (args_p[cc]))
57 {
58 jerry_size_t size = jerry_get_utf8_string_size (args_p[0]);
59 char *buffer;
60 buffer = (char *) malloc(size + 1);
61
62 if(!buffer)
63 {
64 // not enough memory for this string.
65 printf("[<too-long-string>]");
66 continue;
67 }
68
69 jerry_string_to_utf8_char_buffer (args_p[cc],
70 (jerry_char_t *) buffer,
71 size);
72 *(buffer + size) = 0;
73 printf("%s ", buffer);
74 free (buffer);
75 }
76 else if (jerry_value_is_number (args_p[cc]))
77 {
78 double number = jerry_get_number_value (args_p[cc]);
79 if ((int) number == number)
80 {
81 printf ("%d", (int) number);
82 }
83 else
84 {
85 char buff[50];
86 sprintf(buff, "%.10f", number);
87 printf("%s", buff);
88 }
89
90 }
91 }
92 printf ("\r\n");
93 }
94 return jerry_create_boolean (true);
95 } /* print */
96
DELCARE_HANDLER(gpio_dir)97 DELCARE_HANDLER(gpio_dir) {
98 if (args_cnt < 2)
99 {
100 return jerry_create_boolean (false);
101 }
102
103 int port = (int) jerry_get_number_value (args_p[0]);
104 int value = (int) jerry_get_number_value (args_p[1]);
105
106 if (value)
107 {
108 GPIO_AS_OUTPUT(1 << port);
109 }
110 else
111 {
112 GPIO_AS_INPUT(1 << port);
113 }
114
115 return jerry_create_boolean (true);
116 } /* gpio_dir */
117
DELCARE_HANDLER(gpio_set)118 DELCARE_HANDLER(gpio_set) {
119 if (args_cnt < 2)
120 {
121 return jerry_create_boolean (false);
122 }
123
124 int port = (int) jerry_get_number_value (args_p[0]);
125 int value = (int) jerry_get_number_value (args_p[1]);
126
127 GPIO_OUTPUT_SET(port, value);
128
129 return jerry_create_boolean (true);
130 } /* gpio_set */
131
132
DELCARE_HANDLER(gpio_get)133 DELCARE_HANDLER(gpio_get) {
134 if (args_cnt < 1)
135 {
136 return jerry_create_boolean (false);
137 }
138
139 int port = (int) jerry_get_number_value (args_p[0]);
140 int value = GPIO_INPUT_GET(port) ? 1 : 0;
141
142 return jerry_create_number ((double) value);
143 } /* gpio_get */
144
145 static bool
register_native_function(const char * name,jerry_external_handler_t handler)146 register_native_function (const char* name,
147 jerry_external_handler_t handler)
148 {
149 jerry_value_t global_obj_val = jerry_get_global_object ();
150 jerry_value_t reg_func_val = jerry_create_external_function (handler);
151 bool bok = true;
152
153 if (!(jerry_value_is_function (reg_func_val)
154 && jerry_value_is_constructor (reg_func_val)))
155 {
156 printf ("!!! create_external_function failed !!!\r\n");
157 jerry_release_value (reg_func_val);
158 jerry_release_value (global_obj_val);
159 return false;
160 }
161
162 jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) name);
163 jerry_value_t res = jerry_set_property (global_obj_val, prop_name_val, reg_func_val);
164
165 jerry_release_value (reg_func_val);
166 jerry_release_value (global_obj_val);
167 jerry_release_value (prop_name_val);
168
169 if (jerry_value_is_error (res))
170 {
171 printf ("!!! register_native_function failed: [%s]\r\n", name);
172 jerry_release_value (res);
173 return false;
174 }
175
176 jerry_release_value (res);
177
178 return true;
179 } /* register_native_function */
180
js_register_functions(void)181 void js_register_functions (void)
182 {
183 REGISTER_HANDLER(assert);
184 REGISTER_HANDLER(print);
185 REGISTER_HANDLER(gpio_dir);
186 REGISTER_HANDLER(gpio_set);
187 REGISTER_HANDLER(gpio_get);
188 } /* js_register_functions */
189