2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Junkyu Han <junkyu.han@samsung.com>
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
10 * http://floralicense.org/license/
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.
22 #include "log-private.h"
23 #include "ttd-worker-lib.h"
24 #include "common-worker-inf-def.h"
25 #include "common-worker-inf-dbus.h"
27 struct _ttd_worker_report_s {
28 ttd_worker_submit_report_completed_cb completed_cb;
29 void *completed_cb_data;
32 ttd_worker_working_state_e state;
36 GCancellable *cancellable;
37 TtdWorkerDbus *dbus_object;
38 unsigned int ref_count;
41 static worker_dbus_handle *g_dbus_handle = NULL;
43 static void __free_dbus_handle(worker_dbus_handle *handle)
48 if (handle->cancellable) {
49 g_cancellable_cancel(handle->cancellable);
50 g_object_unref(handle->cancellable);
53 if (handle->dbus_object)
54 g_object_unref(handle->dbus_object);
59 static void __unref_dbus_connection(void)
61 _D("Destory the DBus Connection");
66 g_dbus_handle->ref_count--;
68 if (!g_dbus_handle->ref_count) {
69 __free_dbus_handle(g_dbus_handle);
74 static int __ref_dbus_connection(void)
76 worker_dbus_handle *handle = NULL;
80 g_dbus_handle->ref_count++;
81 _D("DBus ref count : %u", g_dbus_handle->ref_count);
85 _D("Make a new DBus Connection");
87 handle = g_try_malloc0(sizeof(worker_dbus_handle));
91 handle->cancellable = g_cancellable_new();
94 ttd_worker_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
95 G_DBUS_PROXY_FLAGS_NONE,
97 TTD_WORKER_DBUS_OBJECT_PATH,
100 if (!handle->dbus_object) {
101 _E("Fail to create the proxy object because of %s", error->message);
104 __free_dbus_handle(handle);
108 g_dbus_handle = handle;
109 g_dbus_handle->ref_count = 1;
110 _D("DBus ref count : %u", g_dbus_handle->ref_count);
115 static void __submit_completed_cb(
116 GObject *source_object, GAsyncResult *res, gpointer user_data)
118 GError *error = NULL;
119 ttd_worker_report *report = user_data;
121 gboolean ret = FALSE;
122 ttd_worker_submit_result_e result;
124 _D("Send completed");
125 ret = ttd_worker_dbus_call_ttd_worker_submit_report_finish(
126 g_dbus_handle->dbus_object, &ret_val, res, &error);
128 _E("DBus proxy call is failed[%s]", error->message);
129 result = TTD_WORKER_SUBMIT_FAIL;
133 _E("Submit report is failed");
134 result = TTD_WORKER_SUBMIT_FAIL;
136 result = TTD_WORKER_SUBMIT_SUCCESS;
139 if (report && report->completed_cb)
140 report->completed_cb(result, report, report->completed_cb_data);
143 void __free_report(ttd_worker_report *report)
148 g_free(report->cmd_id);
149 g_free(report->report_json);
153 ttd_worker_report *__alloc_new_report(void)
155 ttd_worker_report *re = NULL;
157 re = g_try_malloc0(sizeof(ttd_worker_report));
158 retvm_if(!re, NULL, "failed to alloc report");
160 re->state = TTD_WORKER_STATE_UNKNOWN;
162 if (__ref_dbus_connection()) {
170 ttd_worker_report *ttd_worker_report_new(void)
172 return __alloc_new_report();
175 ttd_worker_report *ttd_worker_report_new_by_cmd_id(const char *cmd_id)
177 ttd_worker_report *report = NULL;
179 report = __alloc_new_report();
181 report->cmd_id = g_strdup(cmd_id);
186 void ttd_worker_report_free(ttd_worker_report *report)
188 __free_report(report);
189 __unref_dbus_connection();
192 int ttd_worker_report_set_cmd_id(ttd_worker_report *report, const char *cmd_id)
194 retvm_if(!report, -1, "report is NULL");
196 g_free(report->cmd_id);
199 report->cmd_id = g_strdup(cmd_id);
206 int ttd_worker_report_set_working_state(
207 ttd_worker_report *report, ttd_worker_working_state_e state)
209 retvm_if(!report, -1, "report is NULL");
211 report->state = state;
216 /* report_data should be JSON format */
217 int ttd_worker_report_set_report_data(
218 ttd_worker_report *report, const char *report_data)
220 retvm_if(!report, -1, "report is NULL");
222 g_free(report->report_json);
225 report->report_json = g_strdup(report_data);
227 _D("unset report_json");
232 int ttd_worker_report_get_cmd_id(
233 ttd_worker_report *report, const char **cmd_id)
235 retvm_if(!report, -1, "report is NULL");
236 retvm_if(!cmd_id, -1, "cmd_id is NULL");
238 *cmd_id = (const char *)report->cmd_id;
243 int ttd_worker_report_get_working_state(
244 ttd_worker_report *report, ttd_worker_working_state_e *state)
246 retvm_if(!report, -1, "report is NULL");
247 retvm_if(!state, -1, "state is NULL");
249 *state = report->state;
254 int ttd_worker_report_get_report_data(
255 ttd_worker_report *report, const char **report_data)
257 retvm_if(!report, -1, "report is NULL");
258 retvm_if(!report_data, -1, "report_data is NULL");
260 *report_data = (const char *)report->report_json;
265 int ttd_worker_submit_report(ttd_worker_report *report,
266 ttd_worker_submit_report_completed_cb completed_cb,
267 void *completed_cb_data)
269 ttd_worker_report *rep = NULL;
270 retvm_if(!g_dbus_handle->dbus_object, -1, "dbus_object is NULL");
271 retvm_if(!report, -1, "report is NULL");
272 retvm_if(!report->cmd_id, -1, "cmd_id is NULL");
273 retvm_if(!report->report_json, -1, "report data is NULL");
274 retvm_if(report->state == TTD_WORKER_STATE_UNKNOWN, -1, "state is UNKNOWN");
279 report->completed_cb = completed_cb;
280 report->completed_cb_data = completed_cb_data;
282 ttd_worker_dbus_call_ttd_worker_submit_report(
283 g_dbus_handle->dbus_object,
287 g_dbus_handle->cancellable,
288 (GAsyncReadyCallback) __submit_completed_cb,
294 int ttd_worker_submit_report_sync(
295 ttd_worker_report *report, ttd_worker_submit_result_e *result)
297 GError *error = NULL;
299 gboolean ret = FALSE;
300 ttd_worker_submit_result_e res = TTD_WORKER_SUBMIT_SUCCESS;
303 *result = TTD_WORKER_SUBMIT_FAIL;
305 retvm_if(!g_dbus_handle->dbus_object, -1, "dbus_object is NULL");
306 retvm_if(!report, -1, "report is NULL");
307 retvm_if(!report->cmd_id, -1, "cmd_id is NULL");
308 retvm_if(!report->report_json, -1, "report data is NULL");
309 retvm_if(report->state == TTD_WORKER_STATE_UNKNOWN, -1, "state is UNKNOWN");
311 ret = ttd_worker_dbus_call_ttd_worker_submit_report_sync(
312 g_dbus_handle->dbus_object,
317 g_dbus_handle->cancellable,
320 _E("DBus proxy call is failed[%s]", error->message);
322 res = TTD_WORKER_SUBMIT_FAIL;
325 _E("Submit report is failed");
326 res = TTD_WORKER_SUBMIT_FAIL;