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 <stdlib.h> 17425bb815Sopenharmony_ci#include <stdio.h> 18425bb815Sopenharmony_ci 19425bb815Sopenharmony_ci#include "c_types.h" 20425bb815Sopenharmony_ci#include "gpio.h" 21425bb815Sopenharmony_ci 22425bb815Sopenharmony_ci#include "jerryscript.h" 23425bb815Sopenharmony_ci#include "jerry_extapi.h" 24425bb815Sopenharmony_ci 25425bb815Sopenharmony_ci#define __UNUSED__ __attribute__((unused)) 26425bb815Sopenharmony_ci 27425bb815Sopenharmony_ci#define DELCARE_HANDLER(NAME) \ 28425bb815Sopenharmony_cistatic jerry_value_t \ 29425bb815Sopenharmony_ciNAME ## _handler (const jerry_value_t function_obj_val __UNUSED__, \ 30425bb815Sopenharmony_ci const jerry_value_t this_val __UNUSED__, \ 31425bb815Sopenharmony_ci const jerry_value_t args_p[], \ 32425bb815Sopenharmony_ci const jerry_length_t args_cnt) 33425bb815Sopenharmony_ci 34425bb815Sopenharmony_ci#define REGISTER_HANDLER(NAME) \ 35425bb815Sopenharmony_ci register_native_function ( # NAME, NAME ## _handler) 36425bb815Sopenharmony_ci 37425bb815Sopenharmony_ciDELCARE_HANDLER(assert) { 38425bb815Sopenharmony_ci if (args_cnt == 1 39425bb815Sopenharmony_ci && jerry_value_is_boolean (args_p[0]) 40425bb815Sopenharmony_ci && jerry_get_boolean_value (args_p[0])) 41425bb815Sopenharmony_ci { 42425bb815Sopenharmony_ci printf (">> Jerry assert true\r\n"); 43425bb815Sopenharmony_ci return jerry_create_boolean (true); 44425bb815Sopenharmony_ci } 45425bb815Sopenharmony_ci printf ("Script assertion failed\n"); 46425bb815Sopenharmony_ci exit (JERRY_STANDALONE_EXIT_CODE_FAIL); 47425bb815Sopenharmony_ci return jerry_create_boolean (false); 48425bb815Sopenharmony_ci} /* assert */ 49425bb815Sopenharmony_ci 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ciDELCARE_HANDLER(print) { 52425bb815Sopenharmony_ci if (args_cnt) 53425bb815Sopenharmony_ci { 54425bb815Sopenharmony_ci for (jerry_length_t cc = 0; cc < args_cnt; cc++) 55425bb815Sopenharmony_ci { 56425bb815Sopenharmony_ci if (jerry_value_is_string (args_p[cc])) 57425bb815Sopenharmony_ci { 58425bb815Sopenharmony_ci jerry_size_t size = jerry_get_utf8_string_size (args_p[0]); 59425bb815Sopenharmony_ci char *buffer; 60425bb815Sopenharmony_ci buffer = (char *) malloc(size + 1); 61425bb815Sopenharmony_ci 62425bb815Sopenharmony_ci if(!buffer) 63425bb815Sopenharmony_ci { 64425bb815Sopenharmony_ci // not enough memory for this string. 65425bb815Sopenharmony_ci printf("[<too-long-string>]"); 66425bb815Sopenharmony_ci continue; 67425bb815Sopenharmony_ci } 68425bb815Sopenharmony_ci 69425bb815Sopenharmony_ci jerry_string_to_utf8_char_buffer (args_p[cc], 70425bb815Sopenharmony_ci (jerry_char_t *) buffer, 71425bb815Sopenharmony_ci size); 72425bb815Sopenharmony_ci *(buffer + size) = 0; 73425bb815Sopenharmony_ci printf("%s ", buffer); 74425bb815Sopenharmony_ci free (buffer); 75425bb815Sopenharmony_ci } 76425bb815Sopenharmony_ci else if (jerry_value_is_number (args_p[cc])) 77425bb815Sopenharmony_ci { 78425bb815Sopenharmony_ci double number = jerry_get_number_value (args_p[cc]); 79425bb815Sopenharmony_ci if ((int) number == number) 80425bb815Sopenharmony_ci { 81425bb815Sopenharmony_ci printf ("%d", (int) number); 82425bb815Sopenharmony_ci } 83425bb815Sopenharmony_ci else 84425bb815Sopenharmony_ci { 85425bb815Sopenharmony_ci char buff[50]; 86425bb815Sopenharmony_ci sprintf(buff, "%.10f", number); 87425bb815Sopenharmony_ci printf("%s", buff); 88425bb815Sopenharmony_ci } 89425bb815Sopenharmony_ci 90425bb815Sopenharmony_ci } 91425bb815Sopenharmony_ci } 92425bb815Sopenharmony_ci printf ("\r\n"); 93425bb815Sopenharmony_ci } 94425bb815Sopenharmony_ci return jerry_create_boolean (true); 95425bb815Sopenharmony_ci} /* print */ 96425bb815Sopenharmony_ci 97425bb815Sopenharmony_ciDELCARE_HANDLER(gpio_dir) { 98425bb815Sopenharmony_ci if (args_cnt < 2) 99425bb815Sopenharmony_ci { 100425bb815Sopenharmony_ci return jerry_create_boolean (false); 101425bb815Sopenharmony_ci } 102425bb815Sopenharmony_ci 103425bb815Sopenharmony_ci int port = (int) jerry_get_number_value (args_p[0]); 104425bb815Sopenharmony_ci int value = (int) jerry_get_number_value (args_p[1]); 105425bb815Sopenharmony_ci 106425bb815Sopenharmony_ci if (value) 107425bb815Sopenharmony_ci { 108425bb815Sopenharmony_ci GPIO_AS_OUTPUT(1 << port); 109425bb815Sopenharmony_ci } 110425bb815Sopenharmony_ci else 111425bb815Sopenharmony_ci { 112425bb815Sopenharmony_ci GPIO_AS_INPUT(1 << port); 113425bb815Sopenharmony_ci } 114425bb815Sopenharmony_ci 115425bb815Sopenharmony_ci return jerry_create_boolean (true); 116425bb815Sopenharmony_ci} /* gpio_dir */ 117425bb815Sopenharmony_ci 118425bb815Sopenharmony_ciDELCARE_HANDLER(gpio_set) { 119425bb815Sopenharmony_ci if (args_cnt < 2) 120425bb815Sopenharmony_ci { 121425bb815Sopenharmony_ci return jerry_create_boolean (false); 122425bb815Sopenharmony_ci } 123425bb815Sopenharmony_ci 124425bb815Sopenharmony_ci int port = (int) jerry_get_number_value (args_p[0]); 125425bb815Sopenharmony_ci int value = (int) jerry_get_number_value (args_p[1]); 126425bb815Sopenharmony_ci 127425bb815Sopenharmony_ci GPIO_OUTPUT_SET(port, value); 128425bb815Sopenharmony_ci 129425bb815Sopenharmony_ci return jerry_create_boolean (true); 130425bb815Sopenharmony_ci} /* gpio_set */ 131425bb815Sopenharmony_ci 132425bb815Sopenharmony_ci 133425bb815Sopenharmony_ciDELCARE_HANDLER(gpio_get) { 134425bb815Sopenharmony_ci if (args_cnt < 1) 135425bb815Sopenharmony_ci { 136425bb815Sopenharmony_ci return jerry_create_boolean (false); 137425bb815Sopenharmony_ci } 138425bb815Sopenharmony_ci 139425bb815Sopenharmony_ci int port = (int) jerry_get_number_value (args_p[0]); 140425bb815Sopenharmony_ci int value = GPIO_INPUT_GET(port) ? 1 : 0; 141425bb815Sopenharmony_ci 142425bb815Sopenharmony_ci return jerry_create_number ((double) value); 143425bb815Sopenharmony_ci} /* gpio_get */ 144425bb815Sopenharmony_ci 145425bb815Sopenharmony_cistatic bool 146425bb815Sopenharmony_ciregister_native_function (const char* name, 147425bb815Sopenharmony_ci jerry_external_handler_t handler) 148425bb815Sopenharmony_ci{ 149425bb815Sopenharmony_ci jerry_value_t global_obj_val = jerry_get_global_object (); 150425bb815Sopenharmony_ci jerry_value_t reg_func_val = jerry_create_external_function (handler); 151425bb815Sopenharmony_ci bool bok = true; 152425bb815Sopenharmony_ci 153425bb815Sopenharmony_ci if (!(jerry_value_is_function (reg_func_val) 154425bb815Sopenharmony_ci && jerry_value_is_constructor (reg_func_val))) 155425bb815Sopenharmony_ci { 156425bb815Sopenharmony_ci printf ("!!! create_external_function failed !!!\r\n"); 157425bb815Sopenharmony_ci jerry_release_value (reg_func_val); 158425bb815Sopenharmony_ci jerry_release_value (global_obj_val); 159425bb815Sopenharmony_ci return false; 160425bb815Sopenharmony_ci } 161425bb815Sopenharmony_ci 162425bb815Sopenharmony_ci jerry_value_t prop_name_val = jerry_create_string ((const jerry_char_t *) name); 163425bb815Sopenharmony_ci jerry_value_t res = jerry_set_property (global_obj_val, prop_name_val, reg_func_val); 164425bb815Sopenharmony_ci 165425bb815Sopenharmony_ci jerry_release_value (reg_func_val); 166425bb815Sopenharmony_ci jerry_release_value (global_obj_val); 167425bb815Sopenharmony_ci jerry_release_value (prop_name_val); 168425bb815Sopenharmony_ci 169425bb815Sopenharmony_ci if (jerry_value_is_error (res)) 170425bb815Sopenharmony_ci { 171425bb815Sopenharmony_ci printf ("!!! register_native_function failed: [%s]\r\n", name); 172425bb815Sopenharmony_ci jerry_release_value (res); 173425bb815Sopenharmony_ci return false; 174425bb815Sopenharmony_ci } 175425bb815Sopenharmony_ci 176425bb815Sopenharmony_ci jerry_release_value (res); 177425bb815Sopenharmony_ci 178425bb815Sopenharmony_ci return true; 179425bb815Sopenharmony_ci} /* register_native_function */ 180425bb815Sopenharmony_ci 181425bb815Sopenharmony_civoid js_register_functions (void) 182425bb815Sopenharmony_ci{ 183425bb815Sopenharmony_ci REGISTER_HANDLER(assert); 184425bb815Sopenharmony_ci REGISTER_HANDLER(print); 185425bb815Sopenharmony_ci REGISTER_HANDLER(gpio_dir); 186425bb815Sopenharmony_ci REGISTER_HANDLER(gpio_set); 187425bb815Sopenharmony_ci REGISTER_HANDLER(gpio_get); 188425bb815Sopenharmony_ci} /* js_register_functions */ 189