upload tizen1.0 source
[framework/telephony/libslp-tapi.git] / src / common_tapi_lib_internal.c
1 /*
2  * libslp-tapi
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <errno.h>
23 #include <string.h>
24
25 #include <glib-2.0/glib.h>
26
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <security-server.h>
30
31 #include <TelDefines.h>
32 #include <TapiCommon.h>
33
34 #include "tapi-lib-proxy.h"
35 #include "TapiUtility.h"
36 #include "tapi_proxy_internal.h"
37
38 /* This global stores the proxy connection to dbus */
39 DBusGProxy* tapi_cs_proxy_internal;
40
41 static char g_priv_cookie_name[255] = { 0, };
42 static int g_priv_cookie_size = 0;
43
44 /**
45  *
46  * This function is used to initialise a connnection with telephony server through DBUS.
47  *
48  * @return                      True / False
49  * @param[in]           NONE
50  * @param[out]          None
51  * @remark
52  * @Refer
53  */
54 static int tapi_init_cs_internal()
55 {
56         DBusGConnection* tapi_cs_connection = NULL;
57         GError* err = NULL;
58         g_type_init();
59
60         tapi_cs_connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &err);
61         TAPI_LIB_DEBUG(LEVEL_ERR, "tapi_cs_connection = 0x%x");
62
63         /*      No need to assert but, need to return FALSE     */
64         if (tapi_cs_connection == NULL) {
65                 if (err != NULL) {
66                         fprintf(stderr, "[FAILED]connection to dbus failed: %s\n", err->message);
67                         TAPI_LIB_DEBUG(LEVEL_ERR, "failed to get CS connection with error cause");
68                         g_error_free(err);
69                 }
70                 else {
71                         TAPI_LIB_DEBUG(LEVEL_ERR, "failed to get CS connection with NULL error cause");
72                 }
73                 return FALSE;
74         }
75
76         tapi_cs_proxy_internal = dbus_g_proxy_new_for_name(tapi_cs_connection, "org.projectx.telephony",
77                         "/org/projectx/app", "org.projectx.telephony");
78
79         /*      No need to assert but, need to return FALSE     */
80         TAPI_RETURN_VAL_IF_FAIL(tapi_cs_proxy_internal, FALSE);
81
82         return TRUE;
83 }
84
85 /**
86  *
87  * This function is used to check the connnection status with telephony server through DBUS.
88  *
89  * @return                      True / False
90  * @param[in]           NONE
91  * @param[out]          None
92  * @remark
93  * @Refer
94  */
95 int tapi_check_dbus_status_internal(void)
96 {
97         TS_BOOL result = TRUE;
98
99         TAPI_LIB_DEBUG(LEVEL_DEBUG, "DBUS-CS tapi_check_dbus_status");
100
101         if (tapi_cs_proxy_internal != NULL && dbus_g_proxy_get_interface(tapi_cs_proxy_internal)) {
102                 TAPI_LIB_DEBUG(LEVEL_DEBUG, "DBUS-CS Ready to use");
103                 return TRUE;
104         }
105         else {
106                 TAPI_LIB_DEBUG(LEVEL_DEBUG, "DBUS-CS Initializing...");
107                 if (tapi_init_cs_internal()) {
108                         TAPI_LIB_DEBUG(LEVEL_DEBUG, "DBUS-CS INIT completed");
109                         result = TRUE;
110                 }
111                 else {
112                         TAPI_LIB_DEBUG(LEVEL_ERR, "FAIL TO DBUS-CS INIT");
113                         result = FALSE;
114                 }
115         }
116         return result;
117 }
118
119 static void _dump_cookie(char *data, int size)
120 {
121         char buf[255] = { 0, };
122         char tmp[3] = { 0, };
123         int i = 0;
124
125         for (i = 0; i < size; i++) {
126                 snprintf(tmp, 3, "%02X", data[i]);
127                 strncat(buf, tmp, 3);
128                 if ((i + 1) % 8 == 0)
129                         strncat(buf, "  ", 2);
130         }
131
132         TAPI_LIB_DEBUG(LEVEL_DEBUG, "cookie=[%s]", buf);
133 }
134
135 static int _cookie_init(void)
136 {
137         int ret;
138
139         ret = security_server_get_cookie_size();
140         if (ret < 0) {
141                 TAPI_LIB_DEBUG(LEVEL_DEBUG, "get_cookie_size() failed. ret=%d", ret);
142                 return FALSE;
143         }
144         g_priv_cookie_size = ret;
145
146         ret = security_server_request_cookie(g_priv_cookie_name, g_priv_cookie_size);
147         if (ret < 0) {
148                 memset(g_priv_cookie_name, 0, 255);
149                 g_priv_cookie_size = 0;
150                 TAPI_LIB_DEBUG(LEVEL_DEBUG, "request_cookie() failed. ret=%d", ret);
151                 return FALSE;
152         }
153
154         _dump_cookie(g_priv_cookie_name, g_priv_cookie_size);
155
156         return TRUE;
157 }
158 /**
159  *
160  * This function is used to send the service request to telephony server through DBUS.
161  *
162  * @return                      True / False
163  * @param[in]           tapi_service,tapi_service_function,in_param1,in_param2,in_param3,in_param4
164  * @param[out]          out_param1,out_param2,out_param3,out_param4
165  * @remark
166  * @Refer
167  */
168
169 TS_BOOL tapi_send_request_internal(int tapi_service, int tapi_service_function, GArray* in_param1, GArray* in_param2,
170                 GArray* in_param3, GArray* in_param4, GArray** out_param1, GArray** out_param2, GArray** out_param3,
171                 GArray** out_param4)
172 {
173         TS_BOOL ret = FALSE;
174         int error_occurred;
175         GError* error = NULL;
176         TS_BOOL api_ret = TRUE;
177
178         GArray* in_param5 = ((void *) 0);
179
180         /**
181          * Add d-bus connection name to ALL REQUEST
182          *  - If already appended conn_name to in_param4[0] before tapi_send_request(),
183          *    then append conn_name to in_param4[1] and ignore by telephony-server.
184          */
185         tapi_dbus_connection_name app_name;
186         tel_get_app_name(&app_name);
187         if (app_name.length_of_name == 0) {
188                 TAPI_LIB_DEBUG(LEVEL_DEBUG, "No dbus connection name (pid=%d)", getpid());
189         }
190         else {
191                 TAPI_LIB_DEBUG(LEVEL_DEBUG, "conn_name=[%s]", app_name.name);
192         }
193         g_array_append_vals(in_param4, &app_name, sizeof(tapi_dbus_connection_name));
194
195         /**
196          * Add security cookie to ALL REQUEST
197          */
198         if (g_priv_cookie_size == 0) {
199                 if (_cookie_init() != TRUE) {
200                         TAPI_LIB_DEBUG(LEVEL_DEBUG, "no cookie name. pid=%d", getpid());
201                         return FALSE;
202                 }
203         }
204
205         in_param5 = g_array_new(FALSE, FALSE, sizeof(gchar));
206         g_array_append_vals(in_param5, g_priv_cookie_name, g_priv_cookie_size + 1);
207
208         /**
209          * D-Bus send
210          */
211         api_ret = org_projectx_telephony_service_request_sec(tapi_cs_proxy_internal, tapi_service, tapi_service_function,
212                         in_param1, in_param2, in_param3, in_param4, in_param5, out_param1, out_param2, out_param3, out_param4,
213                         &error_occurred, &error);
214
215         if (api_ret != TRUE) {
216                 // dBUS-RPC is failed
217                 ret = FALSE;
218
219                 // We can not say only TS is killed. This is dBUS-RPC failure including TS crash case
220                 TAPI_LIB_DEBUG(LEVEL_ALERT, "dBUS-RPC is failed %d", api_ret);
221
222                 if (error != NULL) {
223                         // dBUS gives error cause
224                         TAPI_LIB_DEBUG(LEVEL_ALERT,
225                                         " D-Bus API failure: errCode[0x%x], message:[%s], service type=%d, service function=0x%x",
226                                         error_occurred, error->message, tapi_service, tapi_service_function);
227
228                         g_error_free(error);
229                 }
230                 else {
231                         // dBUS does not give error cause even though dBUS-RPC is failed
232                         TAPI_LIB_DEBUG(LEVEL_ALERT, "dBUS RPC is failed but error returned was NULL ");
233                 }
234         }
235         else {
236                 // dBUS-RPC is successfully done
237                 ret = TRUE;
238
239                 TAPI_LIB_DEBUG(LEVEL_INFO, "dBUS RPC is successfully done. service type=%d, service function=0x%x",
240                                 tapi_service, tapi_service_function);
241         }
242
243         g_array_free(in_param5, TRUE);
244
245         return ret;
246 }