Make TIZEN 2.0
[apps/home/call.git] / call-engine / voice-call-dbus.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <dbus/dbus-protocol.h>
19 #include <dbus/dbus-glib.h>
20 #include <dbus/dbus-glib-bindings.h>
21 #include <dbus/dbus-glib-lowlevel.h>
22
23 #include "voice-call-dbus.h"
24 #include "voice-call-engine.h"
25
26 #include <aul.h>
27
28 /*  Voice Call  <-- BT */
29 #define DBUS_BT_MATCH_RULE                      "type='signal',path='/org/projectx/bluetooth_event',interface='User.Bluetooth.AG'"
30 #define DBUS_BT_OBJECT_REQUEST          "/org/projectx/bluetooth_event"
31 #define DBUS_BT_INTERFACE_REQUEST       "User.Bluetooth.AG"
32 #define DBUS_BT_METHOD_REQUEST          "Request"
33
34 /*  Voice Call  --> BT */
35 #define DBUS_BT_SERVICE                         "org.projectx.bluetooth"
36 #define DBUS_BT_OBJECT_RESPONSE         "/org/projectx/btcall_event"
37 #define DBUS_BT_INTERFACE_RESPONSE      "User.Bluetooth.AG"
38 #define DBUS_BT_METHOD_RESPONSE         "Response"
39 /*#define DBUS_BT_METHOD_CONNECT                "Connect"*/
40
41 #define BT_PKG  "org.tizen.bluetooth"
42
43 static DBusGConnection *gconnection = NULL;
44
45 typedef struct _dbus_dest_t {
46         char *service;
47         char *object_path;
48         char *interface;
49         char *method;
50 } dbus_dest_t;
51
52 static int vc_engine_send_via_dbus(DBusGConnection *conn, dbus_dest_t *dest, int first_arg_type, ...)
53 {
54         DBusMessage *msg;
55         DBusMessageIter iter;
56         va_list list;
57         int type;
58         dbus_bool_t ret;
59
60         dbus_int32_t val_int;
61         char *val_str;
62
63         CALL_ENG_DEBUG(ENG_DEBUG, "path:%s, interface:%s, method:%s", dest->object_path, dest->interface, dest->method);
64         msg = dbus_message_new_signal(dest->object_path, dest->interface, dest->method);
65         if (msg == NULL) {
66                 CALL_ENG_DEBUG(ENG_DEBUG, "dbus_message_new_signal failed.");
67                 return VC_ERROR;
68         }
69         dbus_message_set_destination(msg, dest->service);
70
71         dbus_message_iter_init_append(msg, &iter);
72
73         type = first_arg_type;
74
75         va_start(list, first_arg_type);
76         while (type != DBUS_TYPE_INVALID) {
77                 switch (type) {
78                 case DBUS_TYPE_INT32:
79                         val_int = *(int *)(va_arg(list, dbus_int32_t));
80                         dbus_message_iter_append_basic(&iter, type, &val_int);
81                         break;
82
83                 case DBUS_TYPE_STRING:
84                         val_str = va_arg(list, char *);
85                         dbus_message_iter_append_basic(&iter, type, &val_str);
86                         break;
87                 }
88                 type = va_arg(list, int);
89         }
90         va_end(list);
91
92         ret = dbus_connection_send(dbus_g_connection_get_connection(conn), msg, NULL);
93         dbus_connection_flush(dbus_g_connection_get_connection(conn));
94         dbus_message_unref(msg);
95
96         if (ret != TRUE)
97                 return VC_ERROR;
98
99         return VC_NO_ERROR;
100 }
101
102 void vc_engine_on_dbus_send_connect_to_bt(void)
103 {
104         bundle *kb;
105         kb = bundle_create();
106         bundle_add(kb, "launch-type", "call");
107         aul_launch_app(BT_PKG, kb);
108         bundle_free(kb);
109         CALL_ENG_DEBUG(ENG_DEBUG, "End..");
110 }
111
112 void vc_engine_on_dbus_send_response_to_bt(connectivity_bt_ag_param_info_t bt_resp_info)
113 {
114         dbus_dest_t bt_dbus_dest = {
115                 DBUS_BT_SERVICE,
116                 DBUS_BT_OBJECT_RESPONSE,
117                 DBUS_BT_INTERFACE_RESPONSE,
118                 DBUS_BT_METHOD_RESPONSE
119         };
120         CALL_ENG_DEBUG(ENG_DEBUG, "..");
121
122         vc_engine_send_via_dbus(gconnection, &bt_dbus_dest, DBUS_TYPE_INT32, &bt_resp_info.param1, \
123                         DBUS_TYPE_INT32, &bt_resp_info.param2, DBUS_TYPE_INT32, &bt_resp_info.param3, DBUS_TYPE_STRING, bt_resp_info.param4, \
124                         DBUS_TYPE_INVALID);
125 }
126
127 /* Handle all bluetooth relative signal */
128 static void vc_engine_on_dbus_parsing_bt(void *user_data, DBusMessage *message)
129 {
130         DBusMessageIter iter;
131         connectivity_bt_ag_param_info_t bt_event_info = { 0, };
132         call_vc_core_state_t *pcall_core = (call_vc_core_state_t *)vcall_engine_get_core_state();
133
134         if (dbus_message_iter_init(message, &iter))
135                 dbus_message_iter_get_basic(&iter, &bt_event_info.param1);
136
137         if (dbus_message_iter_next(&iter))
138                 dbus_message_iter_get_basic(&iter, &bt_event_info.param2);
139
140         if (dbus_message_iter_next(&iter))
141                 dbus_message_iter_get_basic(&iter, &bt_event_info.param3);
142
143         if (dbus_message_iter_next(&iter))
144                 dbus_message_iter_get_basic(&iter, &bt_event_info.param4);
145
146         CALL_ENG_DEBUG(ENG_DEBUG, "param1:[%d], param2[%d], param3[%d], param4[%s]",
147                         bt_event_info.param1, bt_event_info.param2, bt_event_info.param3, bt_event_info.param4);
148
149         _vc_bt_handle_bt_events(pcall_core, &bt_event_info);
150 }
151
152 /* Handle all dbus signal */
153 static DBusHandlerResult vc_engine_on_dbus_receive(DBusConnection *connection, DBusMessage *message, void *user_data)
154 {
155         int type;
156
157         const char *interface_name = dbus_message_get_interface(message);
158         const char *method_name = dbus_message_get_member(message);
159         const char *object_path = dbus_message_get_path(message);
160
161         type = dbus_message_get_type(message);
162         if (type != DBUS_MESSAGE_TYPE_SIGNAL) {
163                 /*
164                  * INVALID: 0
165                  * METHOD_CALL: 1
166                  * METHOD_CALL_RETURN: 2
167                  * ERROR: 3
168                  * SIGNAL: 4
169                  */
170                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
171         }
172
173         if (object_path == NULL) {
174                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
175         }
176         if (interface_name == NULL) {
177                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178         }
179         if (method_name == NULL) {
180                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
181         }
182
183         /* Check BT Signal */
184         if ((strcmp(object_path, DBUS_BT_OBJECT_REQUEST) == 0) && (strcmp(interface_name, DBUS_BT_INTERFACE_REQUEST) == 0)) {
185                 CALL_ENG_DEBUG(ENG_DEBUG, "received DBus BT signal!");
186                 if (strcmp(method_name, DBUS_BT_METHOD_REQUEST) == 0) {
187                         CALL_ENG_DEBUG(ENG_DEBUG, "BT Method :[Request]");
188                         vc_engine_on_dbus_parsing_bt(user_data, message);
189                         return DBUS_HANDLER_RESULT_HANDLED;
190                 }
191         }
192
193         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
194 }
195
196 int vc_engine_dbus_receiver_setup()
197 {
198         GError *error = NULL;
199         DBusError derror;
200         int ret;
201
202         /*connectio to dbus-daemon.*/
203         gconnection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
204         if (!gconnection) {
205                 CALL_ENG_DEBUG(ENG_ERR,"Could not get connection: %s", error->message);
206                 return FALSE;
207         }
208
209         dbus_error_init(&derror);
210
211         dbus_bus_add_match(dbus_g_connection_get_connection(gconnection), DBUS_BT_MATCH_RULE, &derror);
212         if (dbus_error_is_set(&derror)) {       /*failure*/
213                 CALL_ENG_DEBUG(ENG_ERR, "Failed to dbus_bus_add_match(%s): %s", DBUS_BT_MATCH_RULE, derror.message);
214                 dbus_error_free(&derror);
215                 return FALSE;
216         }
217         /*register event filter to handle received dbus-message.*/
218         ret = dbus_connection_add_filter(dbus_g_connection_get_connection(gconnection), vc_engine_on_dbus_receive, NULL, NULL);
219         if (ret != TRUE) {
220                 CALL_ENG_DEBUG(ENG_ERR, "Failed to dbus_connection_add_filter");
221                 return FALSE;
222         }
223
224         return TRUE;
225 }
226