4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>,
7 * Jaeho Lee <jaeho81.lee@samsung.com>, Shobhit Srivastava <shobhit.s@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
27 #include "comm_config.h"
28 #include "comm_client.h"
29 #include "comm_pkg_mgr_client_dbus_bindings.h"
30 #include "comm_status_broadcast_client_dbus_bindings.h"
31 #include "comm_status_broadcast_signal_marshaller.h"
36 /* Resources to be freed */
37 DBusGConnection *conn;
39 DBusGProxy *request_proxy;
40 DBusGProxy *signal_proxy;
47 comm_client *comm_client_new(void)
49 comm_client *cc = NULL;
51 cc = calloc(1, sizeof(comm_client));
55 cc->conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &(cc->err));
56 if (NULL == cc->conn) {
57 g_printerr("Failed to open connection to dbus: %s\n",
59 g_error_free(cc->err);
65 cc->request_proxy = dbus_g_proxy_new_for_name(cc->conn,
66 COMM_PKG_MGR_DBUS_SERVICE,
67 /* name : written in service file */
68 COMM_PKG_MGR_DBUS_PATH,
69 /* path : written as a node in xml */
70 COMM_PKG_MGR_DBUS_INTERFACE
71 /* interface : written as an interface in xml */
77 int comm_client_free(comm_client *cc)
83 g_error_free(cc->err);
85 dbus_g_connection_unref(cc->conn);
86 if (cc->request_proxy)
87 g_object_unref(cc->request_proxy);
89 g_object_unref(cc->signal_proxy);
99 status_signal_handler(DBusGProxy *proxy,
101 const char *pkg_type,
103 const char *key, const char *val, gpointer data)
105 comm_client *cc = (comm_client *) data;
107 SECURE_LOGD("Got signal: %s/%s/%s/%s/%s", req_id, pkg_type,
110 if (cc->pkgid && pkgid &&
111 0 == strncmp(cc->pkgid, pkgid,
112 strlen(cc->pkgid))) {
113 dbg("Run signal handler");
114 cc->signal_cb(cc->signal_cb_data, req_id, pkg_type,
117 dbg("pkgid is different. (My pkgid:%s)"
118 " Though pass signal to user callback.", cc->pkgid);
119 cc->signal_cb(cc->signal_cb_data, req_id, pkg_type,
123 dbg("No signal handler is set. Do nothing.");
128 comm_client_request(comm_client *cc, const char *req_id, const int req_type,
129 const char *pkg_type, const char *pkgid,
130 const char *args, const char *cookie)
133 gint ret = COMM_RET_ERROR;
135 SECURE_LOGD("got request:%s/%d/%s/%s/%s/%s\n", req_id, req_type, pkg_type,
136 pkgid, args, cookie);
139 pkgid = ""; /* NULL check */
141 r = com_samsung_slp_pkgmgr_request(cc->request_proxy, req_id, req_type,
142 pkg_type, pkgid, args, cookie,
147 g_printerr("Failed to send request via dbus: %s\n",
150 g_error_free(cc->err);
158 dbg("freeing pkgid");
162 cc->pkgid = strdup(pkgid);
170 comm_client_set_status_callback(comm_client *cc, status_cb cb, void *cb_data)
173 if (!cc->signal_proxy) {
174 dbg("signal_proxy is NULL. Try to create a proxy for signal.");
175 cc->signal_proxy = dbus_g_proxy_new_for_name(cc->conn,
176 COMM_STATUS_BROADCAST_DBUS_SERVICE_PREFIX,
177 COMM_STATUS_BROADCAST_DBUS_PATH,
178 COMM_STATUS_BROADCAST_DBUS_INTERFACE);
179 if (NULL == cc->signal_proxy) {
180 g_printerr("Failed to create proxy for signal\n", NULL);
181 return COMM_RET_ERROR;
185 /* Proxy is existing. Do nothing. */
189 cc->signal_cb_data = cb_data;
191 dbg("Register signal-type marshaller.");
192 dbus_g_object_register_marshaller(
193 g_cclosure_user_marshal_VOID__STRING_STRING_STRING_STRING_STRING,
195 G_TYPE_NONE, /* return type */
196 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
197 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); /* termination flag */
199 dbg("Add signal to proxy.");
200 dbus_g_proxy_add_signal(cc->signal_proxy,
201 COMM_STATUS_BROADCAST_SIGNAL_STATUS,
205 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
207 dbg("Connect signal to proxy.");
209 dbus_g_proxy_connect_signal(cc->signal_proxy,
210 COMM_STATUS_BROADCAST_SIGNAL_STATUS,
211 G_CALLBACK(status_signal_handler),