apply FSL(Flora Software License)
[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 \n", 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.\n");
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, "..\n");
121
122         vc_engine_send_via_dbus(gconnection, &bt_dbus_dest, 
123                         DBUS_TYPE_INT32, &bt_resp_info.param1,
124                         DBUS_TYPE_INT32, &bt_resp_info.param2,
125                         DBUS_TYPE_INT32, &bt_resp_info.param3,
126                         DBUS_TYPE_STRING, bt_resp_info.param4,
127                         DBUS_TYPE_INVALID);     
128 }
129
130 /* Handle all bluetooth relative signal */
131 static void vc_engine_on_dbus_parsing_bt(void *user_data, DBusMessage * message)
132 {
133         DBusMessageIter iter;
134         connectivity_bt_ag_param_info_t bt_event_info = { 0, };
135         call_vc_core_state_t *pcall_core = (call_vc_core_state_t *)vcall_engine_get_core_state();
136
137         if (dbus_message_iter_init(message, &iter))
138                 dbus_message_iter_get_basic(&iter, &bt_event_info.param1);
139
140         if (dbus_message_iter_next(&iter))
141                 dbus_message_iter_get_basic(&iter, &bt_event_info.param2);
142
143         if (dbus_message_iter_next(&iter))
144                 dbus_message_iter_get_basic(&iter, &bt_event_info.param3);
145
146         if (dbus_message_iter_next(&iter))
147                 dbus_message_iter_get_basic(&iter, &bt_event_info.param4);
148
149         CALL_ENG_DEBUG(ENG_DEBUG,"param1:[%d], param2[%d], param3[%d], param4[%s] \n",
150                         bt_event_info.param1, bt_event_info.param2, bt_event_info.param3, bt_event_info.param4);
151
152         _vc_bt_handle_bt_events(pcall_core, &bt_event_info);
153 }
154
155 /* Handle all dbus signal */
156 static DBusHandlerResult vc_engine_on_dbus_receive(DBusConnection * connection, DBusMessage * message, void *user_data)
157 {
158         int type;
159
160         const char *interface_name = dbus_message_get_interface(message);
161         const char *method_name = dbus_message_get_member(message);
162         const char *object_path = dbus_message_get_path(message);
163
164         type = dbus_message_get_type(message);
165         if (type != DBUS_MESSAGE_TYPE_SIGNAL) {
166                 /* 
167                  * INVALID: 0
168                  * METHOD_CALL: 1
169                  * METHOD_CALL_RETURN: 2
170                  * ERROR: 3
171                  * SIGNAL: 4 
172                  */
173                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
174         }
175
176         if (object_path == NULL) {
177                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
178         }
179         if (interface_name == NULL) {
180                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
181         }
182         if (method_name == NULL) {
183                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
184         }
185
186         /* Check BT Signal */
187         if ((strcmp(object_path, DBUS_BT_OBJECT_REQUEST) == 0) && (strcmp(interface_name, DBUS_BT_INTERFACE_REQUEST) == 0)) {
188                 CALL_ENG_DEBUG(ENG_DEBUG, "received DBus BT signal!\n");
189                 if (strcmp(method_name, DBUS_BT_METHOD_REQUEST) == 0) {
190                         CALL_ENG_DEBUG(ENG_DEBUG, "BT Method :[Request] \n");
191                         vc_engine_on_dbus_parsing_bt(user_data, message);
192                         return DBUS_HANDLER_RESULT_HANDLED;
193                 }
194         }
195
196         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
197 }
198
199 int vc_engine_dbus_receiver_setup()
200 {
201         GError *error = NULL;
202         DBusError derror;
203         int ret;
204
205         // connectio to dbus-daemon.    
206         gconnection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
207         if (!gconnection) {
208                 printf("Could not get connection: %s\n", error->message);
209                 return FALSE;
210         }
211         
212         dbus_error_init(&derror);
213
214         dbus_bus_add_match(dbus_g_connection_get_connection(gconnection), DBUS_BT_MATCH_RULE, &derror);
215         if (dbus_error_is_set(&derror)) // failure
216         {
217                 CALL_ENG_DEBUG(ENG_DEBUG, "Failed to dbus_bus_add_match(%s): %s\n", DBUS_BT_MATCH_RULE, derror.message);
218                 dbus_error_free(&derror);
219                 return FALSE;
220         }
221         // register event filter to handle received dbus-message.   
222         ret = dbus_connection_add_filter(dbus_g_connection_get_connection(gconnection), vc_engine_on_dbus_receive, NULL, NULL);
223         if (ret != TRUE) {
224                 CALL_ENG_DEBUG(ENG_DEBUG, "Failed to dbus_connection_add_filter");
225                 return FALSE;
226         }
227
228         return TRUE;
229 }
230