From 7cbcf65c48bf4c9008e4bb41a27fa370699577b8 Mon Sep 17 00:00:00 2001 From: Yelin Jeong Date: Thu, 4 Jul 2024 13:25:08 +0900 Subject: [PATCH] [Tizen] Use message port to communicate This patch introduces message port in hybrid application. Using message port, Tizen native and web application can communicate. Signed-off-by: Yelin Jeong --- .../ImageClassificationOffloading/js/main.js | 2 ++ .../ImageClassificationOffloading/js/utils.js | 19 ++++++++++++++++++ .../inc/main.h | 2 ++ .../src/main.c | 20 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/Tizen.web/ImageClassificationOffloading/js/main.js b/Tizen.web/ImageClassificationOffloading/js/main.js index 536929fe..a8c2b855 100644 --- a/Tizen.web/ImageClassificationOffloading/js/main.js +++ b/Tizen.web/ImageClassificationOffloading/js/main.js @@ -14,6 +14,7 @@ import { GetImgPath, loadLabelInfo, startHybridService, + startMessagePort, } from "./utils.js"; let fHandle = null; @@ -161,6 +162,7 @@ window.onload = async function () { const networkType = await getNetworkType(); ip = await getIpAddress(networkType); labels = loadLabelInfo(); + startMessagePort(); startHybridService(); document.getElementById("start_local").addEventListener("click", function () { runLocal(); diff --git a/Tizen.web/ImageClassificationOffloading/js/utils.js b/Tizen.web/ImageClassificationOffloading/js/utils.js index d7696263..5c410e8e 100644 --- a/Tizen.web/ImageClassificationOffloading/js/utils.js +++ b/Tizen.web/ImageClassificationOffloading/js/utils.js @@ -132,3 +132,22 @@ export function startHybridService() { console.log("Get AppContext failed, " + e); } } + +export function startMessagePort() { + try { + const gLocalMessagePort = + tizen.messageport.requestLocalMessagePort("MESSAGE_PORT"); + gLocalMessagePort.addMessagePortListener(function (data) { + for (var i = 0; i < data.length; i++) { + var key = data[i].key; + switch (key) { + case "command": + console.log("key:" + key + " / value:" + data[i].value); + break; + } + } + }); + } catch (e) { + console.log("Failed to create local message port " + e.name); + } +} diff --git a/Tizen.web/ImageClassificationOffloadingService/inc/main.h b/Tizen.web/ImageClassificationOffloadingService/inc/main.h index aaf601e7..dd40a410 100644 --- a/Tizen.web/ImageClassificationOffloadingService/inc/main.h +++ b/Tizen.web/ImageClassificationOffloadingService/inc/main.h @@ -16,5 +16,7 @@ #endif #define LOG_TAG "image_classification_offloading_service" +#define REMOTE_APP_ID "EQmf4iSfpX.ImageClassificationOffloading" +#define PORT_NAME "MESSAGE_PORT" #endif /* __IMAGECLASSIFICATIONOFFLOADINGSERVICE_H__ */ diff --git a/Tizen.web/ImageClassificationOffloadingService/src/main.c b/Tizen.web/ImageClassificationOffloadingService/src/main.c index 1e2ebb24..05bf4278 100644 --- a/Tizen.web/ImageClassificationOffloadingService/src/main.c +++ b/Tizen.web/ImageClassificationOffloadingService/src/main.c @@ -7,6 +7,8 @@ */ #include "main.h" +#include +#include #include #include @@ -44,5 +46,23 @@ int main(int argc, char *argv[]) { dlog_print(DLOG_ERROR, LOG_TAG, "Image classification offloading service start"); + + bool found; + int ret = message_port_check_remote_port(REMOTE_APP_ID, PORT_NAME, &found); + + if (ret != MESSAGE_PORT_ERROR_NONE || !found) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to check remote port"); + } + + /* Todo: Send network information to ui application */ + bundle *b = bundle_create(); + bundle_add_str(b, "command", "connected"); + ret = message_port_send_message(REMOTE_APP_ID, PORT_NAME, b); + if (ret != MESSAGE_PORT_ERROR_NONE) { + dlog_print(DLOG_ERROR, LOG_TAG, "Failed to send message to %s:%s", + REMOTE_APP_ID, PORT_NAME); + } + bundle_free(b); + return service_app_main(argc, argv, &event_callback, NULL); }