From: Sumin Lim Date: Wed, 19 Jul 2017 17:54:56 +0000 (+0900) Subject: Apply Tizen Appfw on IoT.js X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b2d10f40bd0d47745ab0ecc7d14ce7a12c757723;p=platform%2Fupstream%2Fiotjs.git Apply Tizen Appfw on IoT.js 1. Add tizen dependency on CMake 2. Apply Dlog 3. Apply Appcore-agent on iotjs.c (need to change cmake) 4. Enable prepare, check handler on Libtuv 5. Add modules gcontext, tizenappfw Change-Id: Ib3fa0407417ce7e7c8b51667970ef7864a461c95 --- diff --git a/build.config b/build.config index 7a7271d..e0b7f78 100644 --- a/build.config +++ b/build.config @@ -98,19 +98,19 @@ "linux": ["m", "rt"], "darwin": [], "nuttx": [], - "tizen": ["m", "rt", "curl"], + "tizen": ["m", "rt", "curl", "glib-2.0"], "tizenrt": [] } }, "module": { - "always": ["buffer", "console", "events", "fs", "module", "timers"], + "always": ["buffer", "console", "events", "fs", "module", "timers", "gcontext", "i2c", "https"], "include": ["assert", "dns", "http", "net", "stream", "testdriver"], "exclude": { "all": [], "linux": ["adc", "ble", "dgram", "gpio", "i2c", "pwm", "spi", "uart"], "nuttx": ["adc", "dgram", "gpio", "i2c", "pwm", "stm32f4dis", "uart"], "darwin": [], - "tizen": ["adc", "ble", "dgram", "gpio", "i2c", "pwm", "spi", "uart", "https"], + "tizen": ["adc", "ble", "dgram", "pwm", "spi", "uart" ], "tizenrt": [] } } diff --git a/cmake/iotjs.cmake b/cmake/iotjs.cmake index 9e59905..c4181a4 100644 --- a/cmake/iotjs.cmake +++ b/cmake/iotjs.cmake @@ -228,15 +228,25 @@ separate_arguments(EXTERNAL_INCLUDE_DIR) separate_arguments(EXTERNAL_STATIC_LIB) separate_arguments(EXTERNAL_SHARED_LIB) -set(IOTJS_INCLUDE_DIRS - ${EXTERNAL_INCLUDE_DIR} - ${ROOT_DIR}/include - ${IOTJS_SOURCE_DIR} - ${JERRY_PORT_DIR}/include - ${JERRY_INCLUDE_DIR} - ${HTTPPARSER_INCLUDE_DIR} - ${TUV_INCLUDE_DIR} -) + +#for TIZEN + INCLUDE(FindPkgConfig) + SET(dependents "dlog glib-2.0 appcore-agent capi-appfw-service-application capi-appfw-app-common") + pkg_check_modules(fw_name REQUIRED ${dependents}) + FOREACH(flag ${fw_name_CFLAGS}) + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}") + ENDFOREACH(flag) + + set(IOTJS_INCLUDE_DIRS + ${EXTERNAL_INCLUDE_DIR} + ${ROOT_DIR}/include + ${IOTJS_SOURCE_DIR} + ${JERRY_PORT_DIR}/include + ${JERRY_INCLUDE_DIR} + ${HTTPPARSER_INCLUDE_DIR} + ${TUV_INCLUDE_DIR} + ${fw_name_CFLAGS} + ) set(IOTJS_CFLAGS ${IOTJS_CFLAGS} ${CFLAGS_COMMON}) @@ -255,6 +265,7 @@ target_link_libraries(${TARGET_LIB_IOTJS} libhttp-parser ${EXTERNAL_STATIC_LIB} ${EXTERNAL_SHARED_LIB} + ${fw_name_LDFLAGS} ) if(NOT BUILD_LIB_ONLY) diff --git a/config/tizen/packaging/iotjs.spec b/config/tizen/packaging/iotjs.spec index d01d45d..fef7f48 100644 --- a/config/tizen/packaging/iotjs.spec +++ b/config/tizen/packaging/iotjs.spec @@ -13,6 +13,7 @@ BuildRequires: python BuildRequires: cmake BuildRequires: glibc-static BuildRequires: aul +BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(appcore-agent) BuildRequires: pkgconfig(capi-appfw-service-application) BuildRequires: pkgconfig(capi-appfw-app-common) diff --git a/deps/libtuv/src/unix/core.c b/deps/libtuv/src/unix/core.c index 8b29ef9..b29af58 100644 --- a/deps/libtuv/src/unix/core.c +++ b/deps/libtuv/src/unix/core.c @@ -126,7 +126,15 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) { case UV_UDP: uv__udp_close((uv_udp_t*)handle); break; +//ADDED_LINES FOR TIZEN + case UV_PREPARE: + uv__prepare_close((uv_prepare_t*)handle); + break; + case UV_CHECK: + uv__check_close((uv_check_t*)handle); + break; +// case UV_IDLE: uv__idle_close((uv_idle_t*)handle); break; @@ -190,8 +198,8 @@ static void uv__finish_close(uv_handle_t* handle) { handle->flags |= UV_CLOSED; switch (handle->type) { - // case UV_PREPARE: - // case UV_CHECK: + case UV_PREPARE: + case UV_CHECK: case UV_IDLE: case UV_ASYNC: case UV_TIMER: @@ -297,6 +305,7 @@ int uv_run(uv_loop_t* loop, uv_run_mode mode) { uv__run_timers(loop); ran_pending = uv__run_pending(loop); uv__run_idle(loop); + uv__run_prepare(loop); timeout = 0; if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT) diff --git a/deps/libtuv/src/unix/loop-watcher.c b/deps/libtuv/src/unix/loop-watcher.c index 7b5bedb..e3719a0 100644 --- a/deps/libtuv/src/unix/loop-watcher.c +++ b/deps/libtuv/src/unix/loop-watcher.c @@ -78,4 +78,6 @@ uv_##name##_stop(handle); \ } +UV_LOOP_WATCHER_DEFINE(prepare, PREPARE) +UV_LOOP_WATCHER_DEFINE(check, CHECK) UV_LOOP_WATCHER_DEFINE(idle, IDLE) diff --git a/gbsbuild.sh b/gbsbuild.sh new file mode 100755 index 0000000..2e8ba3b --- /dev/null +++ b/gbsbuild.sh @@ -0,0 +1,98 @@ +#!/bin/bash + +# Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cd .. +rm -rf ./iotjs_tizen_gbs +echo "******************************************************************" +echo "* Tizen GBS build *" +echo "* *" +echo "* Please input user, passwd of http://Tizen.org on '~/.gbs.conf' *" +echo "* For more information, please read Guide Docs. folder *" +echo "* ~/.gbs.conf sample is at 'config/tizen/sample.gbs.conf'. *" +echo "* *" +echo "******************************************************************" +read -p "[Warning] This working folder will be copied to ../iotjs_tizen_gbs \ +Are you sure to continue? (y/n) " -n 1 -r + +echo "" +if [[ $REPLY =~ ^[Yy]$ ]] +then + echo copy from $OLDPWD to ../iotjs_tizen_gbs + cp -ra $OLDPWD iotjs_tizen_gbs + cd iotjs_tizen_gbs + echo -e "\n(1) Now, cloning submodules. " + git submodule init + echo -e "\n(2) Update submodules... " + git submodule update + echo -e "\n(3) Remove compiler specified lines on CMake" + rm ./cmake/config/arm-tizen.cmake + echo "include(CMakeForceCompiler) + + set(CMAKE_SYSTEM_NAME Linux) + set(CMAKE_SYSTEM_PROCESSOR armv7l)"\ + >> ./cmake/config/arm-tizen.cmake + + rm ./deps/libtuv/cmake/config/config_arm-tizen.cmake + echo "include(CMakeForceCompiler) + + set(CMAKE_SYSTEM_NAME Linux) + set(CMAKE_SYSTEM_PROCESSOR arm)"\ + >> ./deps/libtuv/cmake/config/config_arm-tizen.cmake + + rm ./deps/jerry/cmake/toolchain_linux_armv7l-el.cmake + echo "set(CMAKE_SYSTEM_NAME Linux) + set(CMAKE_SYSTEM_PROCESSOR armv7l-el) + set(FLAGS_COMMON_ARCH -mlittle-endian -mthumb)" \ + >> ./deps/jerry/cmake/toolchain_linux_armv7l-el.cmake + + echo -e "\n(4) 3 files have been modified." + + find ./ -name '.git' | xargs rm -rf + # Initialize Git repositoryㅣ + if [ ! -d .git ] + then + git init ./ + git checkout -b tizen_gbs + git add ./ + git commit -m "Initial commit" + fi + + echo -e "\n(5) Calling core gbs build command" + gbscommand="gbs build -A armv7l --include-all" + echo $gbscommand + if eval $gbscommand + then + echo "========================================================" + echo "1. GBS Build is successful." + echo "2. Please move to new working folder ../iotjs_tizen_gbs " + echo " cd ../iotjs_tizen_gbs" + echo "3. From now, you can build with this command on new directory" + echo " gbs build -A armv7l --include" + echo "4. Your new branch 'tizen_gbs' is added." + echo "5. 'iotjs origin' repository is added." + git remote add origin https://github.com/samsung/iotjs + echo "(You cant fetch origin repository with this command)" + echo " git fetch --all" + echo "========================================================" + # git remote add origin + # https://review.tizen.org/git/platform/upstream/iotjs + git branch -a + git status + else + echo "GBS Build failed!" + exit 1 + fi +fi diff --git a/index.js b/index.js new file mode 100644 index 0000000..629eab3 --- /dev/null +++ b/index.js @@ -0,0 +1,10 @@ +var fs = require('fs'); +console.log('start reading a file...'); +fs.readFile('file.md', function (err, content) { + if (err) { + console.log('error happened during reading the file'); + return console.log(err); + } + console.log(content); +}) +console.log('end of the file'); diff --git a/src/iotjs.c b/src/iotjs.c index 69a8dc8..075eef1 100644 --- a/src/iotjs.c +++ b/src/iotjs.c @@ -29,8 +29,49 @@ #include #include - - + +#include +#include +#include + +#include +#include +#include + +#include + + +bool initialized_; +typedef struct { GPollFD *pfd; } gcontext_pollfd; + +typedef struct { + int fd; + uv_poll_t *pt; + gcontext_pollfd *pollfd; + int ref; +} poll_handler; + +typedef struct { + int max_priority; + int nfds; + long unsigned int allocated_nfds; + GPollFD *fds; + GMainContext *gc; + + GSList *poll_handlers; +} gcontext; + +//static uv_check_t check_handle; +//static uv_timer_t timeout_handle; +bool query ; +GMainContext *gc; +gcontext *ctx; + +//GMainContext *context; +GSource *source; +GMainLoop *loop; + + /** * Initialize JerryScript. */ @@ -40,14 +81,14 @@ static bool iotjs_jerry_initialize(const iotjs_environment_t* env) { if (iotjs_environment_config(env)->memstat) { jerry_flag |= JERRY_INIT_MEM_STATS; -#if !defined(__NUTTX__) && !defined(__TIZENRT__) +#ifndef __NUTTX__ jerry_port_default_set_log_level(JERRY_LOG_LEVEL_DEBUG); #endif } if (iotjs_environment_config(env)->show_opcode) { jerry_flag |= JERRY_INIT_SHOW_OPCODES; -#if !defined(__NUTTX__) && !defined(__TIZENRT__) +#ifndef __NUTTX__ jerry_port_default_set_log_level(JERRY_LOG_LEVEL_DEBUG); #endif } @@ -94,6 +135,8 @@ static void iotjs_jerry_release() { static bool iotjs_run() { + DLOG("==%s", __func__); + // Evaluating 'iotjs.js' returns a function. bool throws = false; #ifndef ENABLE_SNAPSHOT @@ -175,37 +218,114 @@ static void iotjs_uv_walk_to_close_callback(uv_handle_t* handle, void* arg) { } -int iotjs_entry(int argc, char** argv) { - // Initialize debug print. - init_debug_settings(); +bool _on_app_create_cb(void *data) { + DLOG("== %s ", __func__); + DLOG("===== on CB, An Iot.js tizen app is started."); + if ( data == NULL ) { + DLOG("User_dAta is NULL"); + return false; + } + return true; +} + +void _on_app_terminate_cb(void *data){ + DLOG("== %s ", __func__); + DLOG("==== An Iot.js tizen App is terminated."); - // Create environment. iotjs_environment_t* env = (iotjs_environment_t*)iotjs_environment_get(); - // Parse command line arguments. - if (!iotjs_environment_parse_command_line_arguments(env, (uint32_t)argc, - argv)) { - DLOG("iotjs_environment_parse_command_line_arguments failed"); - return 1; + iotjs_environment_go_state_exiting(env); + + // Emit 'exit' event. + iotjs_process_emit_exit(0); + + // Release builtin modules. + iotjs_module_list_cleanup(); + + // Release commonly used jerry values. + iotjs_binding_finalize(); + + + return; +} + +void _on_app_control_cb(app_control_h app_control, void *data) +{ + DLOG("== %s ", __func__); + DLOG("An Iot.js tizen App Control is applied."); + return; +} + +static gboolean GMain_prepare (GSource *source, gint *timeout_) { + //always on prepare + return TRUE; +} + +static gboolean GMain_check(GSource *source) { + return TRUE; +} + +static gboolean GMain_dispatch (GSource *source, GSourceFunc callback, gpointer user_data) { + //uv_run(uloop, UV_RUN_ONCE); + iotjs_environment_t* env = (iotjs_environment_t*)iotjs_environment_get(); + uv_run(iotjs_environment_loop(env), UV_RUN_ONCE); + return TRUE; +} + +static GSourceFuncs Iterator_funcs = { + GMain_prepare, + GMain_check, + GMain_dispatch, + NULL, + NULL, + NULL +}; + +static void __loop_init(int argc, char **argv, void *data) +{ + gc = g_main_context_default(); + source = g_source_new(&Iterator_funcs, sizeof(GSource)); + loop = g_main_loop_new (gc, FALSE); + g_source_attach((GSource *)source, gc); + + if (initialized_) { + return; } + initialized_ = true; + + DLOG("==-==-== %s ", __func__); + iotjs_environment_t* env = (iotjs_environment_t*)iotjs_environment_get(); + // Set event loop. iotjs_environment_set_loop(env, uv_default_loop()); - // Initialize JerryScript engine. if (!iotjs_jerry_initialize(env)) { DLOG("iotjs_jerry_initialize failed"); - return 1; + //return 1; } +} - // Start IoT.js - if (!iotjs_start(env)) { - DLOG("iotjs_start failed"); - return 1; - } +static void __loop_run(void *data) +{ + iotjs_environment_t* env = (iotjs_environment_t*)iotjs_environment_get(); + + DLOG("----------------- %s ", __func__); + // Start IoT.js + if (!iotjs_start(env)) { + DLOG("iotjs_start failed"); + //return 1; + } + DLOG("IOTJS _LOOP_ RUN finish loop"); + DLOG("== %s ", __func__); +/* + g_main_loop_unref(loop); + g_main_loop_quit(loop); +*/ // close uv loop. // uv_stop(iotjs_environment_loop(env)); + uv_walk(iotjs_environment_loop(env), iotjs_uv_walk_to_close_callback, NULL); uv_run(iotjs_environment_loop(env), UV_RUN_DEFAULT); @@ -220,6 +340,90 @@ int iotjs_entry(int argc, char** argv) { // Release debug print setting. release_debug_settings(); +} + + +static void __loop_exit(void *data) +{ + DLOG("== %s ", __func__); +} + +static void __loop_fini(void) +{ + DLOG("== %s ", __func__); +} + +static void +service_app_lang_changed(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_LANGUAGE_CHANGED*/ + DLOG("_cb_APP_EVENT_LANGUAGE_CHANGED"); +} + +static void +service_app_region_changed(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_REGION_FORMAT_CHANGED*/ + DLOG("APP_EVENT_REGION_FORMAT_CHANGED"); +} + +static void +service_app_low_battery(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_LOW_BATTERY*/ + DLOG("APP_EVENT_LOW_BATTERY"); +} + +static void +service_app_low_memory(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_LOW_MEMORY*/ + DLOG("APP_EVENT_LOW_MEMORY"); +} + + +int iotjs_entry(int argc, char** argv) { + // Initialize debug print. + init_debug_settings(); + + // Create environment. + iotjs_environment_t* env = (iotjs_environment_t*)iotjs_environment_get(); + + // Parse command line arguments. + if (!iotjs_environment_parse_command_line_arguments(env, (uint32_t)argc, + argv)) { + DLOG("iotjs_environment_parse_command_line_arguments failed"); + return 1; + } + // Tizen Service App + service_app_loop_method_s method = { + .init = __loop_init, + .run = __loop_run, + .exit = __loop_exit, + .fini = __loop_fini + }; + + service_app_lifecycle_callback_s event_callbacks; + event_callbacks.create = _on_app_create_cb; + event_callbacks.terminate = _on_app_terminate_cb; + event_callbacks.app_control = _on_app_control_cb; + + app_event_handler_h handlers[5] = {NULL, }; + + service_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], + APP_EVENT_LOW_BATTERY, service_app_low_battery, &env); + service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], + APP_EVENT_LOW_MEMORY, service_app_low_memory, &env); + service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], + APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &env); + service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], + APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &env); + + int ret = service_app_main_ext(argc, argv, &event_callbacks, &method, env); + if(!ret) { + DLOG("Initialize app is failed!"); + return 1; + } return 0; } diff --git a/src/iotjs_debuglog.h b/src/iotjs_debuglog.h index 7468027..0a90a14 100644 --- a/src/iotjs_debuglog.h +++ b/src/iotjs_debuglog.h @@ -29,6 +29,9 @@ extern const char* iotjs_debug_prefix[4]; #define DBGLEV_WARN 2 #define DBGLEV_INFO 3 +#define LOG_TAG "IOTJS_RUNTIME" +#include + #define IOTJS_DLOG(lvl, ...) \ do { \ if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \ @@ -36,6 +39,7 @@ extern const char* iotjs_debug_prefix[4]; fprintf(iotjs_log_stream, __VA_ARGS__); \ fprintf(iotjs_log_stream, "\n"); \ fflush(iotjs_log_stream); \ + dlog_print(DLOG_INFO, LOG_TAG, __VA_ARGS__); \ } \ } while (0) #define DLOG(...) IOTJS_DLOG(DBGLEV_ERR, __VA_ARGS__) diff --git a/src/iotjs_magic_strings.h b/src/iotjs_magic_strings.h index b5abb49..86cd151 100644 --- a/src/iotjs_magic_strings.h +++ b/src/iotjs_magic_strings.h @@ -83,6 +83,8 @@ #define IOTJS_MAGIC_STRING_FINISHREQUEST "finishRequest" #define IOTJS_MAGIC_STRING_FLOAT "FLOAT" #define IOTJS_MAGIC_STRING_FSTAT "fstat" +#define IOTJS_MAGIC_STRING_GCONTEXTINIT "init" +#define IOTJS_MAGIC_STRING_GCONTEXTUNINIT "uninit" #define IOTJS_MAGIC_STRING_GETADDRINFO "getaddrinfo" #define IOTJS_MAGIC_STRING_GETSOCKNAME "getsockname" #define IOTJS_MAGIC_STRING_GPIO "Gpio" @@ -192,6 +194,8 @@ #define IOTJS_MAGIC_STRING_STDERR "stderr" #define IOTJS_MAGIC_STRING_STDOUT "stdout" #define IOTJS_MAGIC_STRING_STOP "stop" +#define IOTJS_MAGIC_STRING_TIZENAPPFWSTART "appstart" +#define IOTJS_MAGIC_STRING_TIZENAPPFWON "appon" #define IOTJS_MAGIC_STRING_TOHEXSTRING "toHexString" #define IOTJS_MAGIC_STRING_TOSTRING "toString" #define IOTJS_MAGIC_STRING_TRANSFERARRAY "transferArray" diff --git a/src/js/gcontext.js b/src/js/gcontext.js new file mode 100644 index 0000000..292b5ef --- /dev/null +++ b/src/js/gcontext.js @@ -0,0 +1,34 @@ +/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var util = require('util'); +var gcontextBuiltin = process.binding(process.binding.gcontext); + + +function Gcontext() { + +} + +Gcontext.prototype.init = function() { + gcontextBuiltin.init(); +}; + +Gcontext.prototype.uninit = function() { + gcontextBuiltin.uninit(); +}; + +module.exports = new Gcontext(); +module.exports.Gcontext = Gcontext; \ No newline at end of file diff --git a/src/js/tizenappfw.js b/src/js/tizenappfw.js new file mode 100644 index 0000000..1ac075c --- /dev/null +++ b/src/js/tizenappfw.js @@ -0,0 +1,44 @@ +/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var util = require('util'); +var tizenappfwBuiltin = process.binding(process.binding.tizenappfw); +//var gcontext = require('gcontext'); + +function Tizenappfw() { +// gcontext.init(); +} + +Tizenappfw.prototype.appStart = function() { + console.log("app start"); + tizenappfwBuiltin.appstart(); +}; + + +Tizenappfw.prototype.appStop = function() { + console.log("app Stop"); + //gcontext.uninit(); + //tizenappfwBuiltin.appstart(); +}; + + +Tizenappfw.prototype.appon = function() { + tizenappfwBuiltin.appon(); + console.log("app on"); +}; + +module.exports = new Tizenappfw(); +module.exports.Tizenappfw = Tizenappfw; \ No newline at end of file diff --git a/src/modules/iotjs_module_console.c b/src/modules/iotjs_module_console.c index c827636..bda7a55 100644 --- a/src/modules/iotjs_module_console.c +++ b/src/modules/iotjs_module_console.c @@ -15,16 +15,27 @@ #include "iotjs_def.h" - static void Print(iotjs_jhandler_t* jhandler, FILE* out_fd) { JHANDLER_CHECK_ARGS(1, string); iotjs_string_t msg = JHANDLER_GET_ARG(0, string); - fprintf(out_fd, "%s", iotjs_string_data(&msg)); + const char* str = iotjs_string_data(&msg); + unsigned str_len = iotjs_string_size(&msg); + unsigned idx = 0; + +//TODO_for_TIZEN + dlog_print(DLOG_INFO, LOG_TAG, str); + + for (idx = 0; idx < str_len; idx++) { + if (str[idx] != 0) { + fprintf(out_fd, "%c", str[idx]); + } else { + fprintf(out_fd, "\\u0000"); + } + } iotjs_string_destroy(&msg); } - JHANDLER_FUNCTION(Stdout) { Print(jhandler, stdout); } diff --git a/src/modules/iotjs_module_gcontext.c b/src/modules/iotjs_module_gcontext.c new file mode 100644 index 0000000..5a97451 --- /dev/null +++ b/src/modules/iotjs_module_gcontext.c @@ -0,0 +1,310 @@ +/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iotjs_def.h" +#include "iotjs_binding.h" +#include "iotjs_handlewrap.h" + +#include +#include +#include +#include + +bool initialized_; +typedef struct { GPollFD *pfd; } gcontext_pollfd; + +typedef struct { + int fd; + uv_poll_t *pt; + gcontext_pollfd *pollfd; + int ref; +} poll_handler; + +typedef struct { + int max_priority; + int nfds; + int allocated_nfds; + GPollFD *fds; + GMainContext *gc; + + GSList *poll_handlers; +} gcontext; + +typedef struct { + iotjs_handlewrap_t handlewrap; +} IOTJS_VALIDATED_STRUCT(iotjs_gcontextwrap_t); + +iotjs_gcontextwrap_t *iotjs_gcontextwrap_create(const iotjs_jval_t *jgcontext); +int iotjs_gcontextwrap_stop(iotjs_gcontextwrap_t *gcontextwrap); + +static void iotjs_gcontextwrap_destroy(iotjs_gcontextwrap_t *gcontextwrap); +IOTJS_DEFINE_NATIVE_HANDLE_INFO_THIS_MODULE(gcontextwrap); + + +static uv_check_t check_handle; +static uv_timer_t timeout_handle; +static bool query = false; +static GMainContext *gc = NULL; +static gcontext *ctx = NULL; + +static void poll_cb(uv_poll_t *uv_handle, int status, int events) { + gcontext_pollfd *_pfd = (gcontext_pollfd *)uv_handle->data; + GPollFD *pfd = _pfd->pfd; + pfd->revents |= pfd->events & ((events & UV_READABLE ? G_IO_IN : 0) | + (events & UV_WRITABLE ? G_IO_OUT : 0)); + uv_poll_stop(uv_handle); +} + +static void timeout_cb(uv_timer_t *handle) { + query = true; + uv_timer_stop(&timeout_handle); +} + +static void prepare_cb(uv_prepare_t *handle) { + gint i; + gint timeout; + char *flagsTable = NULL; + + if (!query) + return; + g_main_context_prepare(ctx->gc, &ctx->max_priority); + + // Getting all sources from GLib main context + while (ctx->allocated_nfds < + (ctx->nfds = g_main_context_query(ctx->gc, ctx->max_priority, + &timeout, ctx->fds, + ctx->allocated_nfds))) { + g_free(ctx->fds); + ctx->allocated_nfds = ctx->nfds; + ctx->fds = g_new(GPollFD, (unsigned)ctx->allocated_nfds); + } + + // Poll + if (ctx->nfds || timeout != 0) { + flagsTable = (char *)g_new0(char, (unsigned)ctx->allocated_nfds); + // Reduce reference count of handler + GSList *phandler_iter = NULL; + for (phandler_iter = g_slist_nth(ctx->poll_handlers, 0); + phandler_iter != NULL; phandler_iter = g_slist_next(phandler_iter)) { + poll_handler *handle = (poll_handler *)phandler_iter->data; + handle->ref = 0; + + for (i = 0; i < ctx->nfds; ++i) { + GPollFD *pfd = ctx->fds + i; + + if (handle->fd == pfd->fd) { + *(flagsTable + i) = 1; + handle->pollfd->pfd = pfd; + handle->ref = 1; + pfd->revents = 0; + uv_poll_start(handle->pt, UV_READABLE | UV_WRITABLE, poll_cb); + break; + } + } + } + + // Process current file descriptors from GContext + for (i = 0; i < ctx->nfds; ++i) { + GPollFD *pfd = ctx->fds + i; + gint exists = (gint) * (flagsTable + i); + + if (exists) + continue; + + pfd->revents = 0; + + // Preparing poll handler + poll_handler *phandler = g_new(poll_handler, 1); + gcontext_pollfd *pollfd = g_new(gcontext_pollfd, 1); + pollfd->pfd = pfd; + phandler->fd = pfd->fd; + phandler->pollfd = pollfd; // + phandler->ref = 1; + + // Create uv poll handler, then append own poll handler on it + uv_poll_t *pt = (uv_poll_t *)malloc(sizeof(uv_poll_t)); + memset(pt, 0, sizeof(uv_poll_t)); + pt->data = pollfd; + phandler->pt = pt; + + uv_poll_init(iotjs_environment_loop(iotjs_environment_get()), pt, + pfd->fd); + uv_poll_start(pt, UV_READABLE | UV_WRITABLE, poll_cb); + + ctx->poll_handlers = g_slist_append(ctx->poll_handlers, phandler); + } + g_free(flagsTable); + + // Remove handlers which aren't required + phandler_iter = g_slist_nth(ctx->poll_handlers, 0); + while (phandler_iter != NULL) { + // poll_handler* handle = (poll_handler*) phandler_iter->data; + poll_handler *handle = (poll_handler *)phandler_iter->data; + + if (handle->ref == 0) { + uv_unref((uv_handle_t *)handle->pt); + uv_poll_stop(handle->pt); + uv_close((uv_handle_t *)handle->pt, (uv_close_cb)free); + + g_free(handle->pollfd); + ctx->poll_handlers = g_slist_remove(ctx->poll_handlers, phandler_iter); + + continue; + } + phandler_iter = g_slist_next(phandler_iter); + } // end of while + } +} + +static void check_cb(uv_check_t *handle) { + if (!ctx->nfds) + return; + + int ready = + g_main_context_check(ctx->gc, ctx->max_priority, ctx->fds, ctx->nfds); + if (ready) + g_main_context_dispatch(ctx->gc); + /* The libuv event loop is lightweight and quicker than GLib. + it requires to hold on for a while. */ + query = false; + uv_timer_start(&timeout_handle, timeout_cb, 5, 0); +} + +iotjs_gcontextwrap_t *iotjs_gcontextwrap_create(const iotjs_jval_t *jgcontext) { + iotjs_gcontextwrap_t *gcontextwrap = IOTJS_ALLOC(iotjs_gcontextwrap_t); + uv_prepare_t *uv_prepare = IOTJS_ALLOC(uv_prepare_t); + IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_gcontextwrap_t, gcontextwrap); + + iotjs_handlewrap_initialize(&_this->handlewrap, jgcontext, + (uv_handle_t *)(uv_prepare), + &this_module_native_info); + // Initialize handler. + const iotjs_environment_t *env = iotjs_environment_get(); + /* Prepare Handler start*/ + uv_prepare_init(iotjs_environment_loop(env), uv_prepare); + uv_prepare_start(uv_prepare, prepare_cb); + + return gcontextwrap; +} + +iotjs_jval_t *iotjs_gcontextwrap_jobject(iotjs_gcontextwrap_t *gcontextwrap) { + IOTJS_VALIDATED_STRUCT_METHOD(iotjs_gcontextwrap_t, gcontextwrap); + iotjs_jval_t *jobject = iotjs_handlewrap_jobject(&_this->handlewrap); + IOTJS_ASSERT(iotjs_jval_is_object(jobject)); + return jobject; +} + +JHANDLER_FUNCTION(Init) { + const iotjs_jval_t *jgcontext = JHANDLER_GET_THIS(object); + iotjs_gcontextwrap_t *gcontext_wrap = iotjs_gcontextwrap_create(jgcontext); + IOTJS_ASSERT(iotjs_jval_is_object(iotjs_gcontextwrap_jobject(gcontext_wrap))); + IOTJS_ASSERT(iotjs_jval_get_object_native_handle(jgcontext) != 0); + + if (initialized_) { + return; + } + + initialized_ = true; + gc = g_main_context_default(); + + ctx = g_new(gcontext, 1); + ctx->max_priority = 0; + ctx->nfds = 0; + + g_main_context_acquire(gc); + ctx->gc = g_main_context_ref(gc); + ctx->fds = NULL; + ctx->allocated_nfds = 0; + ctx->poll_handlers = NULL; + query = true; + + /* Check */ + uv_check_init(iotjs_environment_loop(iotjs_environment_get()), &check_handle); + uv_check_start(&check_handle, check_cb); + + /* Timer */ + uv_timer_init(iotjs_environment_loop(iotjs_environment_get()), + &timeout_handle); +} + +static void PrepareHandlerDestroy(uv_handle_t *handle) { + IOTJS_RELEASE(handle); +} + +JHANDLER_FUNCTION(Uninit) { + if (!initialized_) + return; + initialized_ = false; + + // Remove all handlers + GSList *iter = NULL; + poll_handler *phandler = NULL; + iter = g_slist_nth(ctx->poll_handlers, 0); + + while (iter != NULL) { + phandler = (poll_handler *)iter->data; + + // Stop polling handler + uv_unref((uv_handle_t *)phandler->pt); + uv_poll_stop(phandler->pt); + uv_close((uv_handle_t *)phandler->pt, (uv_close_cb)free); + + g_free(phandler->pollfd); + ctx->poll_handlers = g_slist_remove(ctx->poll_handlers, iter); + iter = g_slist_next(iter); + } + + // Release GMainContext loop + g_main_context_unref(ctx->gc); + + uv_loop_close(iotjs_environment_loop(iotjs_environment_get())); + uv_stop(iotjs_environment_loop(iotjs_environment_get())); + g_free(ctx->fds); + + uv_timer_stop(&timeout_handle); + uv_close((uv_handle_t *)&timeout_handle, NULL); + + uv_unref((uv_handle_t *)&check_handle); + uv_check_stop(&check_handle); + uv_close((uv_handle_t *)&check_handle, NULL); + + const iotjs_jval_t *jgcontext = JHANDLER_GET_THIS(object); + + // Stop prepare_handle + iotjs_handlewrap_t *handlewrap = iotjs_handlewrap_from_jobject(jgcontext); + uv_prepare_t *prepare_handle = + (uv_prepare_t *)iotjs_handlewrap_get_uv_handle(handlewrap); + uv_prepare_stop(prepare_handle); + + // close handle_wrap + if (!uv_is_closing(iotjs_handlewrap_get_uv_handle(handlewrap))) { + iotjs_handlewrap_close(handlewrap, PrepareHandlerDestroy); + } +} + +static void iotjs_gcontextwrap_destroy(iotjs_gcontextwrap_t *gcontextwrap) { + IOTJS_VALIDATED_STRUCT_DESTRUCTOR(iotjs_gcontextwrap_t, gcontextwrap); + iotjs_handlewrap_destroy(&(_this->handlewrap)); + IOTJS_RELEASE(gcontextwrap); +} + +iotjs_jval_t InitGcontext() { + iotjs_jval_t gcontext = iotjs_jval_create_object(); + initialized_ = false; + + iotjs_jval_set_method(&gcontext, IOTJS_MAGIC_STRING_GCONTEXTINIT, Init); + iotjs_jval_set_method(&gcontext, IOTJS_MAGIC_STRING_GCONTEXTUNINIT, Uninit); + return gcontext; +} diff --git a/src/modules/iotjs_module_tizenappfw.c b/src/modules/iotjs_module_tizenappfw.c new file mode 100644 index 0000000..3b38bdf --- /dev/null +++ b/src/modules/iotjs_module_tizenappfw.c @@ -0,0 +1,126 @@ +/* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "iotjs_def.h" + +#include +#include +#include +//#include "iotjs_module_gcontext.h" + +static void __loop_init(int argc, char **argv, void *data) { + DLOG("1. %s \n", __func__); +} + +static void __loop_run(void *data) { + DLOG("2. %s \n", __func__); +// bool isLoaded = gcontext_init(); +// DLOG("is gcontext loaded:%d \n", isLoaded); +} + +static void __loop_exit(void *data) { + DLOG("3. %s \n", __func__); +} + +static void __loop_fini(void) +{ + DLOG("4. %s \n", __func__); +} + +static void +service_app_lang_changed(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_LANGUAGE_CHANGED*/ + DLOG("APP_EVENT_LANGUAGE_CHANGED"); +} + +static void +service_app_region_changed(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_REGION_FORMAT_CHANGED*/ + DLOG("APP_EVENT_REGION_FORMAT_CHANGED"); +} + +static void +service_app_low_memory(app_event_info_h event_info, void *user_data) +{ + /*APP_EVENT_LOW_MEMORY*/ + DLOG("APP_EVENT_LOW_MEMORY"); +} + +bool _on_app_create_cb(void *data) { + DLOG("%s \n", __func__); + return 1; +} + +void _on_app_terminate_cb(void *data) { + DLOG("%s \n", __func__); +} + +void _on_app_control_cb(app_control_h app_control, void *data) +{ + DLOG("%s \n", __func__); + DLOG("An Iot.js tizen App Control is applied."); + return; +} + +JHANDLER_FUNCTION(Appstart) { + DLOG("==================== APP Start ============== "); + + iotjs_environment_t* env = (iotjs_environment_t*)iotjs_environment_get(); + int argc = (int) iotjs_environment_argc(env); + //temporal + char * argv1 ="a"; + char **argv = &argv1; + // Tizen Service App + service_app_loop_method_s method = { + .init = __loop_init, + .run = __loop_run, + .exit = __loop_exit, + .fini = __loop_fini + }; + + service_app_lifecycle_callback_s event_callbacks; + event_callbacks.create = _on_app_create_cb; + event_callbacks.terminate = _on_app_terminate_cb; + event_callbacks.app_control = _on_app_control_cb; + + app_event_handler_h handlers[5] = {NULL, }; + + service_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], + APP_EVENT_LOW_MEMORY, service_app_low_memory, &env); + service_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], + APP_EVENT_LANGUAGE_CHANGED, service_app_lang_changed, &env); + service_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], + APP_EVENT_REGION_FORMAT_CHANGED, service_app_region_changed, &env); + + int ret = service_app_main_ext(argc, argv, &event_callbacks, &method, env); + if(!ret){ + DLOG("Initialize app is failed!"); + } +} + +JHANDLER_FUNCTION(Appon) { + DLOG("APP On"); +} + +iotjs_jval_t InitTizenappfw() { + iotjs_jval_t tizenappfw = iotjs_jval_create_object(); + + iotjs_jval_set_method(&tizenappfw, IOTJS_MAGIC_STRING_TIZENAPPFWSTART, Appstart); + iotjs_jval_set_method(&tizenappfw, IOTJS_MAGIC_STRING_TIZENAPPFWON, Appon); + + return tizenappfw; +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..e8f5e2d --- /dev/null +++ b/test.js @@ -0,0 +1,35 @@ +/* + +var AppControl = require('tizen-app-control'); +var app = require('tizen-application'); + +app.on('appcontrol', function(requested){ + if (requested.operation == AppControl.OPERATION_MAIN) { + // main .. + } else { + } +}); + +app.on('pause', function() { + // pause ... +}); + +app.on('terminate', function() { + // release resources +}); + + +app.start().then(function() { + + console.log(app.name); + console.log(app.id); + +}).catch(function(e) { + console.log(e.message); + +}); + +*/ + +var tizenappfw = require("tizenappfw"); +tizenappfw.appstart(); diff --git a/testEmitter.js b/testEmitter.js new file mode 100644 index 0000000..d521331 --- /dev/null +++ b/testEmitter.js @@ -0,0 +1,22 @@ + +var events = require('events'); +var util = require('util'); + +// emit 이벤트를 발생시키는 오브젝트 +var MyServer = function() { + events.EventEmitter.call(this); +}; + +// MyServer 객체를 EventEmitter로부터 상속 +util.inherits(MyServer, events.EventEmitter); + +// 객체 생성 +var server = new MyServer(); + +// 객체에 리스너 등록 +server.on('connection', function() { + console.log('connection caused'); +}); + +// 이벤트 발생 +server.emit('connection');