Update DBus Server for updating PS-Key. 87/233687/1
authorJungYong KIM <jyong2.kim@samsung.com>
Wed, 29 Jan 2020 05:16:03 +0000 (14:16 +0900)
committerMukunth A <a.mukunth@samsung.com>
Mon, 18 May 2020 07:59:43 +0000 (07:59 +0000)
Change-Id: I088d5d0538f1566246fa3b46cd7d0cecc2e4d00a
Signed-off-by: JungYong KIM <jyong2.kim@samsung.com>
(cherry picked from commit 1ceabb258c4df250cdbdba96d8bb075293ecfdcf)

18 files changed:
.gitmodules
CMain/Makefile
CMain/inc/orchestration.h
CMain/inc/orchestration_cipher.h [new file with mode: 0644]
CMain/inc/orchestration_dbus_server.h [new file with mode: 0644]
CMain/inc/orchestration_key.h [new file with mode: 0644]
CMain/inc/orchestration_service.h [new file with mode: 0644]
CMain/inc/orchestration_type.h [moved from CMain/inc/orchestration_server.h with 71% similarity]
CMain/lib/liborchestration.a
CMain/src/main.c
CMain/src/orchestration_cipher.c [new file with mode: 0644]
CMain/src/orchestration_dbus_server.c [moved from CMain/src/orchestration_server.c with 71% similarity]
CMain/src/orchestration_key.c [new file with mode: 0644]
CMain/src/orchestration_service.c [new file with mode: 0644]
CMain/unittest/Makefile
CMain/unittest/test_orchestration_server.cpp
libedge-orchestration/src/orchestration_client.c
packaging/edge-orchestration.spec

index de90f78..48785e6 100644 (file)
@@ -1,4 +1,4 @@
 [submodule "edge-home-orchestration-go"]
        path = edge-home-orchestration-go
        url = https://github.com/lf-edge/edge-home-orchestration-go.git
-       branch = Baobab
+       branch = master
index b8c497d..f97b19c 100644 (file)
@@ -11,11 +11,14 @@ LIBRARY_FILE        := orchestration
 BINARY_FILE            := edge-orchestration
 SRC_FILES              := \
                        $(SRC_DIR)/main.c \
-                       $(SRC_DIR)/orchestration_server.c
+                       $(SRC_DIR)/orchestration_dbus_server.c \
+                       $(SRC_DIR)/orchestration_service.c \
+                       $(SRC_DIR)/orchestration_cipher.c \
+                       $(SRC_DIR)/orchestration_key.c
 OBJ_FILES              := *.o
 
 # Build parameter
-CFLAGS := -g -Wall -Werror -fPIE -pie
+CFLAGS := -g -Wall -Werror -fPIE -pie -Wno-unused-function
 
 all: clean build
 
index 38acfd6..8e73edb 100644 (file)
@@ -36,6 +36,17 @@ typedef struct { const char *p; ptrdiff_t n; } _GoString_;
  * limitations under the License.
  *
  *******************************************************************************/
+
+#include <stdlib.h>
+
+#ifndef __ORCHESTRATION_H__
+#define __ORCHESTRATION_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
 #define MAX_SVC_INFO_NUM 3
 typedef struct {
        char* ExecutionType;
@@ -53,6 +64,32 @@ typedef struct {
        TargetInfo RemoteTargetInfo;
 } ResponseService;
 
+typedef char* (*identityGetterFunc)();
+typedef char* (*keyGetterFunc)(char* id);
+
+identityGetterFunc iGetter;
+keyGetterFunc kGetter;
+
+static void setPSKHandler(identityGetterFunc ihandle, keyGetterFunc khandle){
+       iGetter = ihandle;
+       kGetter = khandle;
+}
+
+static char* bridge_iGetter(){
+       return iGetter();
+}
+
+static char* bridge_kGetter(char* id){
+       return kGetter(id);
+}
+#ifdef __cplusplus
+}
+
+#endif
+
+#endif // __ORCHESTRATION_H__
+
+
 #line 1 "cgo-generated-wrapper"
 
 
@@ -110,6 +147,8 @@ extern ResponseService OrchestrationRequestService(char* p0, int p1, char* p2, R
 
 extern int PrintLog(char* p0);
 
+extern void SetPSKHandler(identityGetterFunc p0, keyGetterFunc p1);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/CMain/inc/orchestration_cipher.h b/CMain/inc/orchestration_cipher.h
new file mode 100644 (file)
index 0000000..704dd7f
--- /dev/null
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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.
+ *
+ *******************************************************************************/
+
+#ifndef __ORCHESTRATION_CIPHER_H__
+#define __ORCHESTRATION_CIPHER_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int encrypter(char* in, int inLen, char* out);
+int decrypter(char* in, int inLen, char* out);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ORCHESTRATION_CIPHER_H__ */
diff --git a/CMain/inc/orchestration_dbus_server.h b/CMain/inc/orchestration_dbus_server.h
new file mode 100644 (file)
index 0000000..d31e13a
--- /dev/null
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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.
+ *
+ *******************************************************************************/
+
+#ifndef __ORCHESTRATION_SERVER_H__
+#define __ORCHESTRATION_SERVER_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <orchestration_type.h>
+
+void set_default_dbus_interface(void);
+int orchestration_server_initialize(dbus_funcs cb);
+void orchestration_server_finish(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ORCHESTRATION_SERVER_H__ */
diff --git a/CMain/inc/orchestration_key.h b/CMain/inc/orchestration_key.h
new file mode 100644 (file)
index 0000000..42b52f0
--- /dev/null
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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.
+ *
+ *******************************************************************************/
+
+#ifndef __ORCHESTRATION_KEY_H__
+#define __ORCHESTRATION_KEY_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+int updateKeyPair(char* id,char* key);
+char* identityGetter();
+char* keyGetter(char* id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ORCHESTRATION_KEY_H__ */
diff --git a/CMain/inc/orchestration_service.h b/CMain/inc/orchestration_service.h
new file mode 100644 (file)
index 0000000..98f2acd
--- /dev/null
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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.
+ *
+ *******************************************************************************/
+
+#ifndef __ORCHESTRATION_SERVICE_H__
+#define __ORCHESTRATION_SERVICE_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include <orchestration.h>
+#include <orchestration_type.h>
+
+_orchestration_state_e orchestration_service_start();
+dbus_funcs getDbusFuncs();
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ORCHESTRATION_SERVER_H__ */
similarity index 71%
rename from CMain/inc/orchestration_server.h
rename to CMain/inc/orchestration_type.h
index f3f32bd..5980dee 100644 (file)
  *
  *******************************************************************************/
 
-#ifndef __ORCHESTRATION_SERVER_H__
-#define __ORCHESTRATION_SERVER_H__
+#ifndef __ORCHESTRATION_TYPE_H__
+#define __ORCHESTRATION_TYPE_H__
 
 #ifdef __cplusplus
 extern "C"
 {
 #endif
 
+#include <stdio.h>
+#include <stdlib.h>
 #include <stdbool.h>
 #include <orchestration.h>
 
-#define DEBUG(fmt, ...) printf("[%s:%d] ", __FILE__, __LINE__); printf((fmt), ##__VA_ARGS__);
-
 typedef enum  {
        /* Errors of Orchestration*/
        ORCH_ERROR_NOT_READY = -1,
@@ -42,14 +42,20 @@ typedef enum  {
        ORCH_ERROR_NONE = 0,
 } _orchestration_state_e;
 
-typedef int (*request_service_cb)(char* app_name, bool self_select, RequestServiceInfo service_info[], int count, int client_pid);
+typedef RequestServiceInfo requestServiceInfo;
+
+typedef int (*request_service_func)(char* app_name, bool self_select, requestServiceInfo service_info[], int count, char* client_pname);
+typedef int (*update_key_pair_func)(char* id, char* key, char* client_pname);
 
-void set_default_dbus_interface(void);
-int orchestration_server_initialize(request_service_cb cb);
-void orchestration_server_finish(void);
+typedef struct _dbus_funcs{
+       request_service_func request_service_f;
+       update_key_pair_func update_key_pair_f;
+} dbus_funcs;
+
+#define DEBUG(fmt, ...) printf("[%s:%d] ", __FILE__, __LINE__); printf((fmt), ##__VA_ARGS__);
 
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* __ORCHESTRATION_SERVER_H__ */
+#endif /* __ORCHESTRATION_TYPE_H__ */
index 484e310..12235f2 100644 (file)
Binary files a/CMain/lib/liborchestration.a and b/CMain/lib/liborchestration.a differ
index 2498416..e73a81c 100644 (file)
  *
  *******************************************************************************/
 
-#include <stdio.h>
+
 #include <gio/gio.h>
 #include <stdarg.h>
-#include <stdlib.h>
-#include <pthread.h>
-#include <string.h>
-
-#include <orchestration_server.h>
 
-int get_process_name_by_pid(char* process_name, int pid) {
-       char fname[1024] = {0, };
-       char line[1024] = {0, };
-       ssize_t fname_len = snprintf(NULL, 0, "/proc/%d/status", pid);
-
-       snprintf(fname, fname_len + 1, "/proc/%d/status", pid);
-       FILE *fp = fopen(fname, "r");
-       if (fp == NULL) {
-               DEBUG("%d Process not found!!!\n", pid);
-               return -1;
-       }
-       while (!feof(fp)) {
-               fgets(line, 1024, fp);
-               if (strstr(line, "Name:") != NULL) {
-                       int index = 0;
-                       char *ptr = strchr(line, ':');
-                       while (1) {
-                               ptr++;
-                               if (*ptr == '\n' || *ptr == '\0')
-                                       break;
-                               if (*ptr != ' ' && *ptr != '\t')
-                                       process_name[index++] = *ptr;
-                       }
-                       process_name[index] = '\0';
-                       printf("process_name = %s\n", process_name);
-                       fclose(fp);
-                       return 0;
-               }
-       }
-       printf("%d Process status have no Process name!!\n", pid);
-       fclose(fp);
-       return -1;
-}
-
-int request_cb(char* app_name, bool self_select, RequestServiceInfo service_info[], int count, int client_pid){
-       int ret = ORCH_ERROR_NONE;
-       char process_name[128] = {0, };
-       const char unknown[] = "Unknown";
-       ResponseService result;
-
-       if (get_process_name_by_pid(process_name, client_pid) != 0) {
-               DEBUG("could not get Process name");
-               strncpy(process_name, unknown, strlen(unknown));
-       }
-
-       DEBUG("[orchestration_server]\n")
-       DEBUG("\t app_name : %s\n", app_name);
-       DEBUG("\t self_select : %s\n", self_select ? "true" : "false");
-       DEBUG("\t count : %d\n", count);
-       DEBUG("\t client_pid : %d\n", client_pid);
-       DEBUG("\t process_name : %s\n", process_name);
-       for (int ix = 0; ix < count; ix++) {
-               DEBUG("\t service_info[%d].ExecutionType : %s\n", ix, service_info[ix].ExecutionType);
-               DEBUG("\t service_info[%d].ExeCmd : %s\n", ix, service_info[ix].ExeCmd);
-       }
-
-       result = OrchestrationRequestService(app_name, self_select, process_name, service_info, count);
-       DEBUG("result = %s\n", result.Message);
-
-       /* TO DO : parsing API response */
-       return ret;
-}
-
-void* thread_orchestration() {
-       DEBUG("Start OrchestrationInit Thread !!\n");
-       OrchestrationInit();
-       return NULL;
-}
+#include <orchestration_service.h>
+#include <orchestration_dbus_server.h>
 
 int main(void){
        int result;
-       pthread_t p_thread;
 
        GMainLoop *loop;
 
        /* DEBUG for log can be used after OrchestrationInit */
-       if (pthread_create(&p_thread, NULL, thread_orchestration, NULL) != 0) {
-               DEBUG("Fail to start OrchestrationInit Thread !!");
+       result = orchestration_service_start();
+       if (result != ORCH_ERROR_NONE){
+               DEBUG("orchestration_service_initialize failed\n");
                return -1;
        }
 
-       set_default_dbus_interface();
        DEBUG("orchestration_server_initialize call\n");
-       result = orchestration_server_initialize(request_cb);
+       set_default_dbus_interface();
+       result = orchestration_server_initialize(getDbusFuncs());
        if(result != ORCH_ERROR_NONE){
                DEBUG("orchestration_server_initialize failed\n");
                orchestration_server_finish();
        }
 
        loop = g_main_loop_new(NULL, FALSE);
-       if (!loop)
+       if (!loop){
                return -1;
+       }
 
        g_main_loop_run (loop);
        orchestration_server_finish();
 
-
        return 0;
 }
 
diff --git a/CMain/src/orchestration_cipher.c b/CMain/src/orchestration_cipher.c
new file mode 100644 (file)
index 0000000..d40a641
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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 <orchestration_cipher.h>
+
+int encrypter(char* in, int inLen, char* out) {
+       out = in;
+       return inLen;
+}
+int decrypter(char* in, int inLen, char* out) {
+       out = in;
+       return inLen;
+}
similarity index 71%
rename from CMain/src/orchestration_server.c
rename to CMain/src/orchestration_dbus_server.c
index 1a00eee..add4635 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright 2019 Samsung Electronics All Rights Reserved.
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  *******************************************************************************/
 
+#include "orchestration_dbus_server.h"
+
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "gdbus_interface.h"
-#include "orchestration_server.h"
 
 static _gdbus_interface _gdbus;
 
@@ -27,11 +29,39 @@ static guint owner_id;
 static GDBusConnection *_gdbus_conn;
 static GDBusNodeInfo *introspection_data = NULL;
 
-request_service_cb _request_service_cb = NULL;
+request_service_func _request_service_cb = NULL;
+update_key_pair_func _update_key_pair_cb = NULL;
 
 #define _ORCHESTRATION_BUS_NAME "org.tizen.orchestration"
 #define _ORCHESTRATION_OBJECT_PATH "/org/tizen/orchestration"
 
+
+static int _request_service(GVariant *parameters, int origin_client_pid);
+static int _update_key_pair(GVariant *parameters, int origin_client_pid);
+static int get_process_name_by_pid(char* process_name, int pid);
+static int get_pid_with_connection(const gchar* sender);
+static int _get_sync(void);
+static int _set_node_info_new_for_xml(void);
+static int _connection_register_object(void);
+static int _own_name_on_connection(void);
+static int orch_dbus_initialize(void);
+static void _handle_method_call(
+    GDBusConnection *connection,
+    const gchar *sender,
+    const gchar *object_path,
+    const gchar *interface_name,
+    const gchar *method_name,
+    GVariant *parameters,
+    GDBusMethodInvocation *invocation,
+    gpointer user_data);
+
+static const GDBusInterfaceVTable _interface_vtable =
+{
+    _handle_method_call,
+    NULL,
+    NULL
+};
+
 /* Introspection data for the service we are exporting */
 static gchar introspection_xml[] =
     "  <node>"
@@ -44,6 +74,12 @@ static gchar introspection_xml[] =
     "          <arg type='i' name='client_pid' direction='in'/>"
     "          <arg type='i' name='return_value' direction='out'/>"
     "        </method>"
+       "        <method name='update_key_pair'>"
+       "          <arg type='s' name='id' direction='in'/>"
+       "          <arg type='s' name='key' direction='in'/>"
+       "          <arg type='i' name='client_pid' direction='in'/>"
+       "          <arg type='i' name='return_value' direction='out'/>"
+       "        </method>"
     "  </interface>"
     "  </node>";
 /* ---------------------------------------------------------------------------------------------------- */
@@ -53,43 +89,6 @@ static gchar introspection_xml[] =
 #define _FREEDESKTOP_INTERFACE "org.freedesktop.DBus"
 #define _FREEDESKTOP_GETPROCESSID_METHOD "GetConnectionUnixProcessID"
 
-int get_pid_with_connection(const gchar* sender) {
-    int origin_client_pid;
-    GError *error = NULL;
-    GVariant * connection;
-
-    connection = g_variant_new ("(s)", sender);
-
-    GVariant *result =
-        _gdbus.connection_call_sync (_gdbus_conn,
-                                _FREEDESKTOP_BUS_NAME,
-                                _FREEDESKTOP_OBJECT_PATH,
-                                _FREEDESKTOP_INTERFACE,
-                                _FREEDESKTOP_GETPROCESSID_METHOD,
-                                connection,
-                                NULL,
-                                G_DBUS_CALL_FLAGS_NONE,
-                                -1,
-                                NULL,
-                                &error);
-
-    g_variant_get(result, "(u)", &origin_client_pid);
-    DEBUG("origin_client_pid: %d\n", origin_client_pid);
-
-    if (result)
-        g_variant_unref (result);
-
-    if (error) {
-        DEBUG("Failed to call remote method - %i : %s\n", error->code, error->message);
-        g_error_free (error);
-        error = NULL;
-        g_object_unref (_gdbus_conn);
-        return -1;
-    }
-
-    return origin_client_pid;
-}
-
 void set_default_dbus_interface(void)
 {
     _gdbus.get_sync = g_bus_get_sync;
@@ -103,47 +102,77 @@ void set_default_dbus_interface(void)
     _gdbus.connection_call_sync = g_dbus_connection_call_sync;
 }
 
-static int _request_service(GVariant *parameters, int origin_client_pid)
+int orchestration_server_initialize(dbus_funcs cb)
 {
     int result = ORCH_ERROR_NONE;
 
-    char *app_name;
-    int count;
-    bool self_select;
-    int client_pid;
-    GVariantIter *iter;
-    RequestServiceInfo service_info[MAX_SVC_INFO_NUM] = {NULL, };
+    if (cb.request_service_f == NULL || cb.update_key_pair_f == NULL) {
+        result = ORCH_ERROR_INVALID_PARAMETER;
+        printf("request_service_cb is null\n");
+        goto out;
+    }
+    _request_service_cb = cb.request_service_f;
+    _update_key_pair_cb = cb.update_key_pair_f;
 
-    if (!parameters)
-    {
-        return ORCH_ERROR_INVALID_PARAMETER;
+    if (!owner_id) {
+        result = orch_dbus_initialize();
+        if (result != ORCH_ERROR_NONE)
+        {
+            printf("orch_dbus_initialize is failed\n");
+            goto out;
+        }
     }
 
-    g_variant_get(parameters, "(sia(ss)ii)", &app_name, &self_select, &iter, &count, &client_pid);
+out:
+    return result;
+}
 
-    DEBUG("[orchestration dbus_interface] _request_service\n");
-    DEBUG("\t app_name = %s\n", app_name);
-    DEBUG("\t self_select = %s\n", self_select ? "true" : "false");
-    DEBUG("\t count = %d\n", count);
-    DEBUG("\t client_pid = %d\n", client_pid);
+void orchestration_server_finish(void)
+{
+    if (owner_id)
+    {
+        _gdbus.unown_name(owner_id);
+    }
+    if (introspection_data)
+    {
+        _gdbus.node_info_unref(introspection_data);
+    }
+    printf("orchestration_server_finish\n");
+}
 
-    for (int i = 0; i < count; i++) {
-        g_variant_iter_loop (iter, "(ss)", &service_info[i].ExecutionType, &service_info[i].ExeCmd);
+static int orch_dbus_initialize(void)
+{
+    int result;
 
-        DEBUG("\t service_info[%d].ExecutionType = %s\n", i, service_info[i].ExecutionType);
-        DEBUG("\t service_info[%d].ExeCmd = %s\n", i, service_info[i].ExeCmd);
+    result = _get_sync();
+    if (result != ORCH_ERROR_NONE)
+    {
+        printf("Failed to _dbus_init\n");
+        goto out;
     }
-    g_variant_iter_free (iter);
 
-    if (g_strcmp0(app_name, "") == 0)
-        return ORCH_ERROR_INVALID_PARAMETER;
+    result = _set_node_info_new_for_xml();
+    if (result != ORCH_ERROR_NONE)
+    {
+        printf("Failed to _set_node_info_new_for_xml\n");
+        goto out;
+    }
 
-    if (origin_client_pid != client_pid) {
-        DEBUG("not matched clinet_pid from client and dbus, origin_client_pid(%d), client_pid(%d)\n", origin_client_pid, client_pid);
+    result = _connection_register_object();
+    if (result != ORCH_ERROR_NONE)
+    {
+        printf("Failed to _connection_register_object\n");
+        goto out;
     }
 
-    result = _request_service_cb(app_name, self_select, service_info, count, origin_client_pid);
+    result = _own_name_on_connection();
+    if (result != ORCH_ERROR_NONE)
+    {
+        printf("Failed to _dbus_own_name_onconnection\n");
+        goto out;
+    }
 
+out:
     return result;
 }
 
@@ -165,10 +194,17 @@ static void _handle_method_call(
         DEBUG("get_pid_with_connection error!!\n");
     }
 
+
     if (g_strcmp0(method_name, "request_service") == 0)
     {
+        DEBUG("receive method request_service\n");
         ret = _request_service(parameters, origin_client_pid);
     }
+    else if (g_strcmp0(method_name, "update_key_pair") == 0)
+    {
+        DEBUG("receive method update key pair\n");
+        ret = _update_key_pair(parameters, origin_client_pid);
+    }
 
     if (ret == ORCH_ERROR_NONE)
     {
@@ -181,13 +217,6 @@ static void _handle_method_call(
     _gdbus.invocation_return_value(invocation, g_variant_new("(i)", ret));
 }
 
-static const GDBusInterfaceVTable _interface_vtable =
-{
-    _handle_method_call,
-    NULL,
-    NULL
-};
-
 static int _get_sync(void)
 {
     GError *error = NULL;
@@ -266,77 +295,164 @@ static int _connection_register_object(void)
     return result;
 }
 
-static int orch_dbus_initialize(void)
+static int _request_service(GVariant *parameters, int origin_client_pid)
 {
-    int result;
+    int result = ORCH_ERROR_NONE;
 
-    result = _get_sync();
-    if (result != ORCH_ERROR_NONE)
-    {
-        printf("Failed to _dbus_init\n");
-        goto out;
-    }
+    char *app_name;
+    int count;
+    bool self_select;
+    int client_pid;
+    GVariantIter *iter;
+    RequestServiceInfo service_info[MAX_SVC_INFO_NUM] = {NULL, };
 
-    result = _set_node_info_new_for_xml();
-    if (result != ORCH_ERROR_NONE)
+    if (!parameters)
     {
-        printf("Failed to _set_node_info_new_for_xml\n");
-        goto out;
+        return ORCH_ERROR_INVALID_PARAMETER;
     }
 
-    result = _connection_register_object();
-    if (result != ORCH_ERROR_NONE)
-    {
-        printf("Failed to _connection_register_object\n");
-        goto out;
+    g_variant_get(parameters, "(sia(ss)ii)", &app_name, &self_select, &iter, &count, &client_pid);
+
+    DEBUG("[orchestration dbus_interface] _request_service\n");
+    DEBUG("\t app_name = %s\n", app_name);
+    DEBUG("\t self_select = %s\n", self_select ? "true" : "false");
+    DEBUG("\t count = %d\n", count);
+    DEBUG("\t client_pid = %d\n", client_pid);
+
+    for (int i = 0; i < count; i++) {
+        g_variant_iter_loop (iter, "(ss)", &service_info[i].ExecutionType, &service_info[i].ExeCmd);
+
+        DEBUG("\t service_info[%d].ExecutionType = %s\n", i, service_info[i].ExecutionType);
+        DEBUG("\t service_info[%d].ExeCmd = %s\n", i, service_info[i].ExeCmd);
     }
+    g_variant_iter_free (iter);
 
-    result = _own_name_on_connection();
-    if (result != ORCH_ERROR_NONE)
-    {
-        printf("Failed to _dbus_own_name_onconnection\n");
-        goto out;
+    if (g_strcmp0(app_name, "") == 0)
+        return ORCH_ERROR_INVALID_PARAMETER;
+
+    if (origin_client_pid != client_pid) {
+        DEBUG("not matched clinet_pid from client and dbus, origin_client_pid(%d), client_pid(%d)\n", origin_client_pid, client_pid);
     }
 
-out:
+    char process_name[128] = {0, };
+       const char unknown[] = "Unknown";
+       if (get_process_name_by_pid(process_name, origin_client_pid) != 0) {
+               DEBUG("could not get Process name");
+               strncpy(process_name, unknown, strlen(unknown));
+       }
+
+    result = _request_service_cb(app_name, self_select, service_info, count, process_name);
+
     return result;
 }
 
-int orchestration_server_initialize(request_service_cb cb)
+static int _update_key_pair(GVariant *parameters, int origin_client_pid)
 {
     int result = ORCH_ERROR_NONE;
 
-    if (cb == NULL)
-    {
-        result = ORCH_ERROR_INVALID_PARAMETER;
-        printf("request_service_cb is null\n");
-        goto out;
+    char *key;
+    char *id;
+    int client_pid;
+
+    if (!parameters) {
+        return ORCH_ERROR_INVALID_PARAMETER;
     }
-    _request_service_cb = cb;
 
-    if (!owner_id)
-    {
-        result = orch_dbus_initialize();
-        if (result != ORCH_ERROR_NONE)
-        {
-            printf("orch_dbus_initialize is failed\n");
-            goto out;
-        }
+    g_variant_get(parameters, "(ssi)", &id, &key, &client_pid);
+
+    DEBUG("[orchestration dbus_interface] _update_key_pair\n");
+    DEBUG("\t id = %s\n", id);
+    DEBUG("\t key = %s\n", key);
+    DEBUG("\t client_pid = %d\n", client_pid);
+
+    if (g_strcmp0(id, "") == 0 || g_strcmp0(key, "") == 0) {
+        return ORCH_ERROR_INVALID_PARAMETER;
     }
 
-out:
+    if (origin_client_pid != client_pid) {
+        DEBUG("not matched clinet_pid from client and dbus, origin_client_pid(%d), client_pid(%d)\n", origin_client_pid, client_pid);
+    }
+
+    char process_name[128] = {0, };
+       const char unknown[] = "Unknown";
+       if (get_process_name_by_pid(process_name, origin_client_pid) != 0) {
+               DEBUG("could not get Process name");
+               strncpy(process_name, unknown, strlen(unknown));
+       }
+
+       result = _update_key_pair_cb(id, key, process_name);
+
     return result;
 }
 
-void orchestration_server_finish(void)
-{
-    if (owner_id)
-    {
-        _gdbus.unown_name(owner_id);
-    }
-    if (introspection_data)
-    {
-        _gdbus.node_info_unref(introspection_data);
+
+static int get_process_name_by_pid(char* process_name, int pid) {
+       char fname[1024] = {0, };
+       char line[1024] = {0, };
+       ssize_t fname_len = snprintf(NULL, 0, "/proc/%d/status", pid);
+
+       snprintf(fname, fname_len + 1, "/proc/%d/status", pid);
+       FILE *fp = fopen(fname, "r");
+       if (fp == NULL) {
+               DEBUG("%d Process not found!!!\n", pid);
+               return -1;
+       }
+       while (!feof(fp)) {
+               fgets(line, 1024, fp);
+               if (strstr(line, "Name:") != NULL) {
+                       int index = 0;
+                       char *ptr = strchr(line, ':');
+                       while (1) {
+                               ptr++;
+                               if (*ptr == '\n' || *ptr == '\0')
+                                       break;
+                               if (*ptr != ' ' && *ptr != '\t')
+                                       process_name[index++] = *ptr;
+                       }
+                       process_name[index] = '\0';
+                       printf("process_name = %s\n", process_name);
+                       fclose(fp);
+                       return 0;
+               }
+       }
+       printf("%d Process status have no Process name!!\n", pid);
+       fclose(fp);
+       return -1;
+}
+
+static int get_pid_with_connection(const gchar* sender) {
+    int origin_client_pid;
+    GError *error = NULL;
+    GVariant * connection;
+
+    connection = g_variant_new ("(s)", sender);
+
+    GVariant *result =
+        _gdbus.connection_call_sync (_gdbus_conn,
+                                _FREEDESKTOP_BUS_NAME,
+                                _FREEDESKTOP_OBJECT_PATH,
+                                _FREEDESKTOP_INTERFACE,
+                                _FREEDESKTOP_GETPROCESSID_METHOD,
+                                connection,
+                                NULL,
+                                G_DBUS_CALL_FLAGS_NONE,
+                                -1,
+                                NULL,
+                                &error);
+
+    g_variant_get(result, "(u)", &origin_client_pid);
+    DEBUG("origin_client_pid: %d\n", origin_client_pid);
+
+    if (result)
+        g_variant_unref (result);
+
+    if (error) {
+        DEBUG("Failed to call remote method - %i : %s\n", error->code, error->message);
+        g_error_free (error);
+        error = NULL;
+        g_object_unref (_gdbus_conn);
+        return -1;
     }
-    printf("orchestration_server_finish\n");
+
+    return origin_client_pid;
 }
diff --git a/CMain/src/orchestration_key.c b/CMain/src/orchestration_key.c
new file mode 100644 (file)
index 0000000..2d00093
--- /dev/null
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "orchestration_type.h"
+
+#define MAX_KEY_SIZE 256
+
+static char id[MAX_KEY_SIZE];
+static char key[MAX_KEY_SIZE];
+
+int updateKeyPair(char* _id,char* _key) {
+       if (strlen(_id) > MAX_KEY_SIZE) {
+               DEBUG("invalid id size: %u/n", strlen(_id));
+               return -1;
+       }
+       else if (strlen(_key) > MAX_KEY_SIZE) {
+               DEBUG("invalid key size: %u/n", strlen(_key));
+               return -1;
+       }
+       strncpy(id, _id, strlen(_id));
+       strncpy(key, _key, strlen(_key));
+
+       return 0;
+}
+
+char* identityGetter() {
+       return id;
+}
+char* keyGetter(char* id) {
+       return key;
+}
diff --git a/CMain/src/orchestration_service.c b/CMain/src/orchestration_service.c
new file mode 100644 (file)
index 0000000..065dd17
--- /dev/null
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Copyright 2020 Samsung Electronics All Rights Reserved.
+ *
+ * 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 <orchestration_service.h>
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <pthread.h>
+#include <gio/gio.h>
+
+#include "orchestration_key.h"
+
+static int request_service(
+               char* app_name,
+               bool self_select,
+               requestServiceInfo service_info[],
+               int count,
+               char* client_pname);
+static int update_key_pair(
+               char* id,
+               char* key,
+               char* client_pname);
+
+void *orchestration_service_thread() {
+       DEBUG("Start OrchestrationInit Thread !!\n");
+       OrchestrationInit();
+       SetPSKHandler(identityGetter, keyGetter);
+
+       return NULL;
+}
+
+_orchestration_state_e orchestration_service_start() {
+       pthread_t p_thread;
+
+       if (pthread_create(&p_thread, NULL, orchestration_service_thread, NULL) != 0) {
+               DEBUG("Fail to start OrchestrationInit Thread !!");
+               return ORCH_ERROR_NOT_READY;
+       }
+
+       return ORCH_ERROR_NONE;
+}
+
+dbus_funcs getDbusFuncs() {
+       dbus_funcs funcs;
+       funcs.request_service_f = request_service;
+       funcs.update_key_pair_f = update_key_pair;
+       return funcs;
+}
+
+static int request_service(char* app_name, bool self_select, requestServiceInfo service_info[], int count, char* client_pname){
+       int ret = ORCH_ERROR_NONE;
+       ResponseService result;
+
+       DEBUG("[orchestration_server]\n")
+       DEBUG("\t app_name : %s\n", app_name);
+       DEBUG("\t self_select : %s\n", self_select ? "true" : "false");
+       DEBUG("\t count : %d\n", count);
+       DEBUG("\t process_name : %s\n", client_pname);
+       for (int ix = 0; ix < count; ix++) {
+               DEBUG("\t service_info[%d].ExecutionType : %s\n", ix, service_info[ix].ExecutionType);
+               DEBUG("\t service_info[%d].ExeCmd : %s\n", ix, service_info[ix].ExeCmd);
+       }
+
+       result = OrchestrationRequestService(app_name, self_select, client_pname, service_info, count);
+       DEBUG("result = %s\n", result.Message);
+
+       /* TO DO : parsing API response */
+       return ret;
+}
+
+#define ALLOWED_KEY_UPDATER "homeedge"
+
+static int update_key_pair(char* id, char* key, char* client_pname) {
+       if (g_strcmp0(client_pname, ALLOWED_KEY_UPDATER) != 0) {
+               DEBUG("process_name : %s -> is not allowed.\n", client_pname);
+               return -1;
+       }
+
+       return updateKeyPair(id, key);
+}
index e186a99..d7ba9b1 100644 (file)
@@ -11,7 +11,7 @@ OBJ_FILES     := *.o
 
 # Build parameter
 CC             := g++
-CFLAGS := -g -Wall -Werror
+CFLAGS := -g -Wall -Werror -Wno-unused-function
 LIBS    := -lgtest -lgtest_main -lpthread -fprofile-arcs -ftest-coverage
 # BUILDFLAGS := -lgtest -lgtest_main -lpthread -fprofile-arcs -ftest-coverage
 
index f3760aa..ab6a029 100644 (file)
@@ -17,7 +17,8 @@
 
 extern "C"
 {
-#include "orchestration_server.c"
+#include "orchestration_dbus_server.c"
+#include <orchestration_type.h>
 #include <gio/gio.h>
 }
 
@@ -32,7 +33,7 @@ GDBusNodeInfo *valid_GDBusNodeInfo;
 GDBusInterfaceInfo *valid_GDBusinterface;
 gboolean requestCbResult;
 
-int fake_request_cb(char* app_name, bool self_select, RequestServiceInfo service_info[], int count, int client_pid)
+int fake_request_cb(char* app_name, bool self_select, RequestServiceInfo service_info[], int count, char * client_pname)
 {
     if (!strcmp(app_name, APP_NAME) && 
         count == 1 &&
@@ -49,6 +50,13 @@ int fake_request_cb(char* app_name, bool self_select, RequestServiceInfo service
     }
 }
 
+int fake_update_key_pair(char* id, char* key, char* client_pname) {
+       if (id != NULL && key != NULL && client_pname != NULL) {
+               return 1;
+       }
+       return -1;
+}
+
 GDBusConnection *fake_get_sync_return_NULL(GBusType bus_type, GCancellable *cancellable, GError **error)
 {
     *error = NULL;
@@ -166,7 +174,10 @@ protected:
 
 TEST_F(OrchestrationServerTests, WhenCalledServerInit_WithInvalidcb_ExpectedErrorReturn)
 {
-    ASSERT_EQ(orchestration_server_initialize(NULL), ORCH_ERROR_INVALID_PARAMETER);
+       dbus_funcs fake_funcs;
+       fake_funcs.request_service_f = NULL;
+       fake_funcs.update_key_pair_f = NULL;
+    ASSERT_EQ(orchestration_server_initialize(fake_funcs), ORCH_ERROR_INVALID_PARAMETER);
 }
 
 TEST_F(OrchestrationServerTests, WhenCalledServerInit_OccuredDbusError_ExpectedErrorReturn)
@@ -174,7 +185,11 @@ TEST_F(OrchestrationServerTests, WhenCalledServerInit_OccuredDbusError_ExpectedE
     set_default_dbus_interface();
     _gdbus.get_sync = fake_get_sync_return_NULL;
 
-    ASSERT_EQ(orchestration_server_initialize(fake_request_cb), ORCH_ERROR_DBUS_FAILURE);
+    dbus_funcs fake_funcs;
+       fake_funcs.request_service_f = fake_request_cb;
+       fake_funcs.update_key_pair_f = fake_update_key_pair;
+
+    ASSERT_EQ(orchestration_server_initialize(fake_funcs), ORCH_ERROR_DBUS_FAILURE);
 }
 
 TEST_F(OrchestrationServerTests, WhenCalledOrchDbusInit_GetSyncFailure_ExpectedErrorReturn)
@@ -251,7 +266,11 @@ TEST_F(OrchestrationServerTests, WhenCalledServerInit_ExpectedSuccess)
     _gdbus.node_info_new_for_xml = fake_node_info_new_for_xml_success;
     _gdbus.connection_register_object = fake_connection_register_object_success;
 
-    ASSERT_EQ(orchestration_server_initialize(fake_request_cb), ORCH_ERROR_NONE);
+    dbus_funcs fake_funcs;
+       fake_funcs.request_service_f = fake_request_cb;
+       fake_funcs.update_key_pair_f = fake_update_key_pair;
+
+    ASSERT_EQ(orchestration_server_initialize(fake_funcs), ORCH_ERROR_NONE);
 
     if (valid_GDBusConnection != NULL)
     {
index 8db5e5b..ec0e5e7 100755 (executable)
@@ -19,6 +19,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <dbus_consumer.h>
+#include <gio/gio.h>
 
 /* ---------------------------------------------------------------------------------------------------- */
 orchestration_changed_service_status_cb _changed_service_status_cb;
index bded75c..93734df 100644 (file)
@@ -126,4 +126,4 @@ systemctl restart %{name}
 %attr(755,system_fw,system_fw)%{_includedir}/%{name}/orchestration_client.h
 # orchestration sample
 %attr(755,system_fw,system_fw)%{_bindir}/edge_*
-%attr(755,system_fw,system_fw)/var/%{name}/apps/native_sample/*
\ No newline at end of file
+%attr(755,system_fw,system_fw)/var/%{name}/apps/native_sample/*