Merge "add app interface module"
[apps/native/tizen-things-daemon.git] / lib / src / ttsd-worker-lib.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Contact: Junkyu Han <junkyu.han@samsung.com>
5  *
6  * Licensed under the Flora License, Version 1.1 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://floralicense.org/license/
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an AS IS BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <glib.h>
20 #include <gio/gio.h>
21
22 #include "log.h"
23 #include "ttsd-worker-lib.h"
24 #include "tizen-things-lib-dbus.h"
25
26 #define TTSD_WORKER_DBUS_NAME "ttsd.worker.dbus"
27 #define TTSD_WORKER_DBUS_OBJECT_PATH "/ttsd_worker_dbus_object"
28 #define TTSD_WORKER_DBUS_INTERFACE "ttsd.worker.dbus"
29
30 typedef struct {
31         GCancellable *cancellable;
32         TtsdWorkerDbus *dbus_object;
33         ttsd_worker_submit_report_completed_cb completed_cb;
34         void *completed_cb_data;
35 } dbus_h;
36
37 static dbus_h *__make_dbus_connection(void);
38 static void __destroy_dbus_connection(dbus_h *h);
39 static void __submit_completed_cb(GObject *source_object, GAsyncResult *res, gpointer user_data);
40
41 static void __destroy_dbus_connection(dbus_h *h)
42 {
43         _D("Destory the DBus Connection");
44         if (h->cancellable) {
45                 g_cancellable_cancel(h->cancellable);
46                 g_object_unref(h->cancellable);
47         }
48         if (h->dbus_object)
49                 g_object_unref(h->dbus_object);
50 }
51
52 static dbus_h *__make_dbus_connection(void)
53 {
54         dbus_h *h = NULL;
55         GError *error = NULL;
56
57         _D("Make the DBus Connection");
58
59         h = g_try_malloc0(sizeof(dbus_h));
60         if (!h)
61                 return NULL;
62
63         h->cancellable = g_cancellable_new();
64
65         h->dbus_object = ttsd_worker_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
66                         G_DBUS_PROXY_FLAGS_NONE,
67                         TTSD_WORKER_DBUS_NAME,
68                         TTSD_WORKER_DBUS_OBJECT_PATH,
69                         h->cancellable,
70                         &error);
71         if (!h->dbus_object) {
72                 _E("Fail to create the proxy object because of %s", error->message);
73                 g_error_free(error);
74                 __destroy_dbus_connection(h);
75                 return NULL;
76         }
77
78         return h;
79 }
80
81 static void __submit_completed_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
82 {
83         GError *g_error = NULL;
84         dbus_h *h = user_data;
85         gint ret_val = -1;
86         gboolean ret = FALSE;
87         const char *result = NULL;
88
89         _D("Send completed");
90
91         if (h == NULL) {
92                 _E("There's no user_data for dbus_h");
93                 return;
94         }
95
96         ret = ttsd_worker_dbus_call_ttsd_worker_submit_report_finish(h->dbus_object, &ret_val, res, &g_error);
97         if (!ret) {
98                 _E("DBus proxy call is failed[%s]", g_error->message);
99                 result = "Failed to submit report";
100                 g_error_free(g_error);
101         } else {
102                 if (ret_val < 0) {
103                         _E("Submit report is failed");
104                         result = "Failed to submit report";
105                 } else
106                         result = "Success to submit report";
107         }
108
109         if (h->completed_cb)
110                 h->completed_cb(result, h->completed_cb_data);
111
112         __destroy_dbus_connection(h);
113 }
114
115 int ttsd_worker_submit_report(const char *report, ttsd_worker_submit_report_completed_cb completed_cb, void *user_data)
116 {
117         dbus_h *h = NULL;
118
119         h = __make_dbus_connection();
120         if (!h) {
121                 _E("Failed to make dbus connection");
122                 return -1;
123         }
124
125         h->completed_cb = completed_cb;
126         h->completed_cb_data = user_data;
127
128         ttsd_worker_dbus_call_ttsd_worker_submit_report(h->dbus_object,
129                         report,
130                         h->cancellable,
131                         (GAsyncReadyCallback) __submit_completed_cb,
132                         h);
133
134         return 0;
135 }
136
137 int ttsd_worker_submit_report_sync(const char *report)
138 {
139         dbus_h *h = NULL;
140         GError *error = NULL;
141         gint ret_val = -1;
142         gboolean ret = FALSE;
143
144         h = __make_dbus_connection();
145         if (!h) {
146                 _E("Failed to make dbus connection");
147                 return -1;
148         }
149
150         ret = ttsd_worker_dbus_call_ttsd_worker_submit_report_sync(h->dbus_object,
151                         report,
152                         &ret_val,
153                         h->cancellable,
154                         &error);
155         if (!ret) {
156                 _E("DBus proxy call is failed[%s]", error->message);
157                 g_error_free(error);
158                 return -1;
159         } else {
160                 if (ret_val < 0) {
161                         _E("Submit report is failed");
162                         return -1;
163                 }
164         }
165
166         __destroy_dbus_connection(h);
167
168         return 0;
169 }