Add dbus interface files and Makefile for CMain. add_dbus_interface
authorheewon Park <h_w.park@samsung.com>
Thu, 28 Mar 2019 10:42:37 +0000 (19:42 +0900)
committerheewon Park <h_w.park@samsung.com>
Thu, 28 Mar 2019 10:42:37 +0000 (19:42 +0900)
Signed-off-by: heewon Park <h_w.park@samsung.com>
src/CMain/Makefile [new file with mode: 0644]
src/CMain/inc/dbus_interface.h [new file with mode: 0644]
src/CMain/inc/orchestration.h [new file with mode: 0644]
src/CMain/orchestration.h [deleted file]
src/CMain/src/dbus_interface.c [new file with mode: 0644]
src/CMain/src/main.c [new file with mode: 0644]

diff --git a/src/CMain/Makefile b/src/CMain/Makefile
new file mode 100644 (file)
index 0000000..8afb089
--- /dev/null
@@ -0,0 +1,24 @@
+#
+# Makefile
+#
+
+CC=gcc
+all: orchestration
+
+# %.o: %.c
+#      $(CC) -Wall -c `pkg-config --cflags dbus-1`
+# lt; -o $@
+
+orchestration: main.o dbus_interface.o
+       $(CC) -g -Wall -Werror ./src/main.o ./src/dbus_interface.o -o orchestration -I ./inc `pkg-config --libs --cflags gio-2.0 gio-unix-2.0 glib-2.0 dbus-glib-1 dbus-1`
+
+main.o: ./src/main.c
+       $(CC) -Wall ./src/main.c -c -o ./src/main.o -I ./inc `pkg-config --libs --cflags glib-2.0`
+
+dbus_interface.o: ./src/dbus_interface.c
+       $(CC) -Wall ./src/dbus_interface.c -c -o ./src/dbus_interface.o -I ./inc `pkg-config --libs --cflags gio-2.0 gio-unix-2.0 glib-2.0 dbus-glib-1 dbus-1`
+
+
+.PHONY: clean
+clean:
+       rm ./src/*.o orchestration
\ No newline at end of file
diff --git a/src/CMain/inc/dbus_interface.h b/src/CMain/inc/dbus_interface.h
new file mode 100644 (file)
index 0000000..b9a4282
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef __ORCHESTRATION_DBUS_H__
+#define __ORCHESTRATION_DBUS_H__
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+typedef enum  {
+       ORCH_ERROR_NONE = 0,
+       ORCH_ERROR_INVALID_PARAMETER = -1,
+       ORCH_ERROR_FAULT = -2,
+} ORCH_DBUS_ERROR;
+
+typedef void (*request_service_cb)(char* app_name, char* service_info);
+
+ORCH_DBUS_ERROR orch_dbus_initialize(request_service_cb cb);
+void orch_dbus_finish(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ORCHESTRATION_DBUS_H__ */
\ No newline at end of file
diff --git a/src/CMain/inc/orchestration.h b/src/CMain/inc/orchestration.h
new file mode 100644 (file)
index 0000000..50bfb22
--- /dev/null
@@ -0,0 +1,74 @@
+/* Code generated by cmd/cgo; DO NOT EDIT. */
+
+/* package interface */
+
+
+#line 1 "cgo-builtin-prolog"
+
+#include <stddef.h> /* for ptrdiff_t below */
+
+#ifndef GO_CGO_EXPORT_PROLOGUE_H
+#define GO_CGO_EXPORT_PROLOGUE_H
+
+typedef struct { const char *p; ptrdiff_t n; } _GoString_;
+
+#endif
+
+/* Start of preamble from import "C" comments.  */
+
+
+
+
+/* End of preamble from import "C" comments.  */
+
+
+/* Start of boilerplate cgo prologue.  */
+#line 1 "cgo-gcc-export-header-prolog"
+
+#ifndef GO_CGO_PROLOGUE_H
+#define GO_CGO_PROLOGUE_H
+
+typedef signed char GoInt8;
+typedef unsigned char GoUint8;
+typedef short GoInt16;
+typedef unsigned short GoUint16;
+typedef int GoInt32;
+typedef unsigned int GoUint32;
+typedef long long GoInt64;
+typedef unsigned long long GoUint64;
+typedef GoInt64 GoInt;
+typedef GoUint64 GoUint;
+typedef __SIZE_TYPE__ GoUintptr;
+typedef float GoFloat32;
+typedef double GoFloat64;
+typedef float _Complex GoComplex64;
+typedef double _Complex GoComplex128;
+
+/*
+  static assertion to make sure the file is being used on architecture
+  at least with matching size of GoInt.
+*/
+typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
+
+typedef _GoString_ GoString;
+typedef void *GoMap;
+typedef void *GoChan;
+typedef struct { void *t; void *v; } GoInterface;
+typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
+
+#endif
+
+/* End of boilerplate cgo prologue.  */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+extern int OrchestrationInit();
+
+extern int OrchestrationRequestService();
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/src/CMain/orchestration.h b/src/CMain/orchestration.h
deleted file mode 100644 (file)
index 50bfb22..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-/* Code generated by cmd/cgo; DO NOT EDIT. */
-
-/* package interface */
-
-
-#line 1 "cgo-builtin-prolog"
-
-#include <stddef.h> /* for ptrdiff_t below */
-
-#ifndef GO_CGO_EXPORT_PROLOGUE_H
-#define GO_CGO_EXPORT_PROLOGUE_H
-
-typedef struct { const char *p; ptrdiff_t n; } _GoString_;
-
-#endif
-
-/* Start of preamble from import "C" comments.  */
-
-
-
-
-/* End of preamble from import "C" comments.  */
-
-
-/* Start of boilerplate cgo prologue.  */
-#line 1 "cgo-gcc-export-header-prolog"
-
-#ifndef GO_CGO_PROLOGUE_H
-#define GO_CGO_PROLOGUE_H
-
-typedef signed char GoInt8;
-typedef unsigned char GoUint8;
-typedef short GoInt16;
-typedef unsigned short GoUint16;
-typedef int GoInt32;
-typedef unsigned int GoUint32;
-typedef long long GoInt64;
-typedef unsigned long long GoUint64;
-typedef GoInt64 GoInt;
-typedef GoUint64 GoUint;
-typedef __SIZE_TYPE__ GoUintptr;
-typedef float GoFloat32;
-typedef double GoFloat64;
-typedef float _Complex GoComplex64;
-typedef double _Complex GoComplex128;
-
-/*
-  static assertion to make sure the file is being used on architecture
-  at least with matching size of GoInt.
-*/
-typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
-
-typedef _GoString_ GoString;
-typedef void *GoMap;
-typedef void *GoChan;
-typedef struct { void *t; void *v; } GoInterface;
-typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
-
-#endif
-
-/* End of boilerplate cgo prologue.  */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-extern int OrchestrationInit();
-
-extern int OrchestrationRequestService();
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/src/CMain/src/dbus_interface.c b/src/CMain/src/dbus_interface.c
new file mode 100644 (file)
index 0000000..a72888e
--- /dev/null
@@ -0,0 +1,193 @@
+#include <gio/gio.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <dbus_interface.h>
+
+/* ---------------------------------------------------------------------------------------------------- */
+static guint owner_id;
+static GDBusConnection *_gdbus_conn;
+static GDBusNodeInfo *introspection_data = NULL;
+
+request_service_cb _request_service_cb = NULL;
+
+#define _ORCHESTRATION_BUS_NAME "org.tizen.orchestration"
+#define _ORCHESTRATION_OBJECT_PATH "/org/tizen/orchestration"
+#define _ORCHESTRATION_SVC_ERROR_DOMAIN "orchestration"
+
+GDBusErrorEntry _orchestration_svc_errors[] = {
+    {ORCH_ERROR_NONE, "NoError"},
+    {ORCH_ERROR_INVALID_PARAMETER, "Invalid parameter"},
+    {ORCH_ERROR_FAULT, "Orchestration Failed"},
+};
+
+/* Introspection data for the service we are exporting */
+static gchar introspection_xml[] =
+    "  <node>"
+    "  <interface name='org.tizen.orchestration.agent'>"
+    "        <method name='request_service'>"
+    "          <arg type='s' name='app_name' direction='in'/>"
+    "          <arg type='s' name='service_info' direction='in'/>"
+    "        </method>"
+    "  </interface>"
+    "  </node>";
+/* ---------------------------------------------------------------------------------------------------- */
+
+static GQuark _ORCHESTRATION_ERROR_quark(void)
+{
+    static volatile gsize quark_volatile = 0;
+    g_dbus_error_register_error_domain(_ORCHESTRATION_SVC_ERROR_DOMAIN,
+                                       &quark_volatile,
+                                       _orchestration_svc_errors,
+                                       G_N_ELEMENTS(_orchestration_svc_errors));
+    return (GQuark)quark_volatile;
+}
+
+int _request_service(GVariant *parameters, GVariant **reply_body)
+{
+    char *app_name, *service_info;
+
+    g_variant_get(parameters, "(&ss)", &app_name, &service_info);
+
+    if (app_name == NULL)
+        return ORCH_ERROR_INVALID_PARAMETER;
+    if (service_info == NULL)
+        return ORCH_ERROR_INVALID_PARAMETER;
+
+    _request_service_cb(app_name, service_info);
+
+    return ORCH_ERROR_NONE;
+}
+
+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)
+{
+    int ret = ORCH_ERROR_NONE;
+    GVariant *reply_body = NULL;
+
+    if (g_strcmp0(method_name, "request_service") == 0)
+    {
+        ret = _request_service(parameters, &reply_body);
+    }
+    if (ret == ORCH_ERROR_NONE)
+    {
+        printf("Orchestration service Success, method name : %s\n", method_name);
+        g_dbus_method_invocation_return_value(invocation, reply_body);
+    }
+    else
+    {
+        printf("Orchestration service fail, method name : %s\n", method_name);
+
+        GError *error = g_error_new(_ORCHESTRATION_ERROR_quark(), ret, "request service failed");
+        g_dbus_method_invocation_return_gerror(invocation, error);
+        g_error_free(error);
+    }
+}
+
+static const GDBusInterfaceVTable _interface_vtable =
+{
+        _handle_method_call,
+        NULL,
+        NULL
+};
+
+static int _dbus_init(void)
+{
+    GError *error = NULL;
+
+    if (_gdbus_conn == NULL)
+    {
+        _gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
+        if (_gdbus_conn == NULL)
+        {
+            if (error != NULL)
+            {
+                printf("Failed to get dbus [%s]", error->message);
+                g_error_free(error);
+            }
+            return ORCH_ERROR_FAULT;
+        }
+    }
+
+    return ORCH_ERROR_NONE;
+}
+
+void orch_dbus_finish(void)
+{
+    g_bus_unown_name(owner_id);
+    g_dbus_node_info_unref(introspection_data);
+}
+
+ORCH_DBUS_ERROR orch_dbus_initialize(request_service_cb cb)
+{
+    int result;
+    guint orch_registration_id;
+
+    GError *error = NULL;
+
+    if (cb == NULL)
+    {
+        result = ORCH_ERROR_INVALID_PARAMETER;
+        goto out;
+    }
+    _request_service_cb = cb;
+
+    result = _dbus_init();
+    if (result != ORCH_ERROR_NONE)
+    {
+        printf("Can't init dbus [%d]", result);
+        goto out;
+    }
+    owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
+                              _ORCHESTRATION_BUS_NAME,
+                              G_BUS_NAME_OWNER_FLAGS_NONE,
+                              NULL,
+                              NULL,
+                              NULL,
+                              NULL, NULL);
+    if (!owner_id)
+    {
+        printf("Failed to own name");
+        result = ORCH_ERROR_FAULT;
+        goto out;
+    }
+
+    introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
+    if (!introspection_data)
+    {
+        printf("g_dbus_node_info_new_for_xml is failed.");
+        if (error != NULL)
+        {
+            printf("g_dbus_node_info_new_for_xml err [%s]", error->message);
+            g_error_free(error);
+        }
+        result = ORCH_ERROR_FAULT;
+        goto out;
+    }
+
+    orch_registration_id = g_dbus_connection_register_object(_gdbus_conn,
+                                                             _ORCHESTRATION_OBJECT_PATH, introspection_data->interfaces[0],
+                                                             &_interface_vtable, NULL, NULL, NULL);
+
+    if (orch_registration_id == 0)
+    {
+        printf("Failed to g_dbus_connection_register_object");
+        result = ORCH_ERROR_FAULT;
+        goto out;
+    }
+
+out:
+    if (introspection_data)
+    {
+        g_dbus_node_info_unref(introspection_data);
+    }
+
+    return result;
+}
\ No newline at end of file
diff --git a/src/CMain/src/main.c b/src/CMain/src/main.c
new file mode 100644 (file)
index 0000000..0f38e54
--- /dev/null
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <gio/gio.h>
+
+#include <dbus_interface.h>
+
+void request_cb(char* app_name, char* service_info){
+       printf("app_name : %s\n", app_name);
+       printf("service_info : %s\n", service_info);
+}
+
+int main(void){
+       int result;
+       GMainLoop *loop;
+       result = orch_dbus_initialize(request_cb);
+       if(result != ORCH_ERROR_NONE){
+               printf("dbus error\n");
+       }
+
+    loop = g_main_loop_new(NULL, FALSE);
+       if (!loop)
+           return -1;
+
+    g_main_loop_run (loop);
+
+       return 0;
+}
\ No newline at end of file