Fix the coding style errors (bt-service)
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-headset-connection.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 <glib.h>
19 #include <string.h>
20 #include <dlog.h>
21 #include <vconf.h>
22 #include <vconf-internal-bt-keys.h>
23
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
26
27 #include "bt-service-common.h"
28 #include "bt-service-event.h"
29 #include "bt-service-main.h"
30 #include "bt-service-adapter.h"
31 #include "bt-service-device.h"
32 #include "bt-service-audio.h"
33 #include "bt-service-headset-connection.h"
34
35 #include "bt-service-opp-client.h"
36
37
38
39 static GList *p_connection_list = NULL;
40 typedef enum {
41         BLUETOOTH_NONE_CONNECTED = 0x00,
42         BLUETOOTH_HFP_CONNECTED ,
43         BLUETOOTH_A2DP_CONNECTED,
44         BLUETOOTH_ALL_CONNECTED,
45 } bluetooth_state_type_t;
46
47 typedef struct {
48         bluetooth_state_type_t state;
49         bluetooth_device_info_t dev_info;
50         int connection_type;
51 } bt_connection_node_info_t;
52
53 gboolean connection_local = FALSE;
54
55
56 void _bt_headset_set_local_connection(gboolean value)
57 {
58         BT_INFO("setting connection_local to %d", value);
59         connection_local = value;
60 }
61
62 gboolean _bt_headset_get_local_connection()
63 {
64         return connection_local;
65 }
66
67 //gint compare_state(GList *data, bluetooth_state_type_t state)
68 gint compare_state(gconstpointer list_data, gconstpointer conn_state)
69 {
70         GList *data = (GList *) list_data;
71         bluetooth_state_type_t *state = (bluetooth_state_type_t *)conn_state;
72         bt_connection_node_info_t *p_data = (bt_connection_node_info_t *) data;
73         if (p_data->state == *state) {
74                 BT_INFO("State Already Added");
75                 return 0;
76         }
77         return 1;
78 }
79
80 gboolean connect_remote_media_audio(gpointer user_data)
81 {
82         bt_connection_node_info_t *conn_info =
83                         (bt_connection_node_info_t *) user_data;
84         GList *list = NULL;
85         bluetooth_state_type_t state;
86
87         char remote_address[BT_ADDRESS_STRING_SIZE] = { 0 };
88         _bt_convert_addr_type_to_string(remote_address, conn_info->dev_info.device_address.addr);
89         if (p_connection_list == NULL) {
90                 BT_INFO("None of device connected and this hasbeen triggered");
91                 return FALSE;
92         }
93         if (conn_info->connection_type == BT_AUDIO_A2DP) {
94                 state = BLUETOOTH_A2DP_CONNECTED;
95                 list = g_list_find_custom(p_connection_list,
96                                 &state, compare_state);
97                 if (list == NULL) {
98                         BT_INFO("Head Set didn't initiated a2dp connection");
99                         BT_INFO("local device initiating A2DP connection");
100                         _bt_audio_connect(0, BT_AUDIO_A2DP,
101                                         &conn_info->dev_info.device_address, NULL);
102                 } else {
103                         BT_INFO("A2DP Connection Already exists");
104                 }
105                 g_free(conn_info);
106         } else {
107                 state = BLUETOOTH_HFP_CONNECTED;
108                 list = g_list_find_custom(p_connection_list,
109                                 &state, compare_state);
110                 if (list == NULL) {
111                         BT_INFO("Headset didn't initiated HFP connection");
112                         BT_INFO("local device intiating HFP Connection");
113                         _bt_audio_connect(0, BT_AUDIO_HSP,
114                                         &conn_info->dev_info.device_address, NULL);
115                 } else {
116                         BT_INFO("HFP Connection Already exists");
117                 }
118                 g_free(conn_info);
119         }
120         return FALSE;
121 }
122
123 void _bt_get_bluetooth_device_info(char *remote_address, bluetooth_device_info_t *device)
124 {
125         GArray *dev_list = NULL;
126         int size = 0;
127         int i = 0;
128         bluetooth_device_info_t info;
129         char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
130         dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
131         if (device == NULL)
132                 return;
133         _bt_get_bonded_devices(&dev_list);
134         size = (dev_list->len) / sizeof(bluetooth_device_info_t);
135         for (i = 0; i < size; i++) {
136                 info = g_array_index(dev_list, bluetooth_device_info_t, i);
137                 _bt_convert_addr_type_to_string(bond_address, info.device_address.addr);
138                 if (strcmp(bond_address, remote_address) == 0) {
139                         BT_INFO("Match found");
140                         memcpy(device, &info, sizeof(bluetooth_device_info_t));
141                         g_array_free(dev_list, TRUE);
142                         return;
143                 }
144         }
145         g_array_free(dev_list, TRUE);
146         return;
147 }
148
149 void _bt_headset_add_timer_function(int connection_type, bluetooth_device_info_t *info)
150 {
151         bt_connection_node_info_t *pass_conn_info = NULL;
152
153         if (info == NULL)
154                 return;
155
156         pass_conn_info = g_new0(bt_connection_node_info_t, 1);
157         pass_conn_info->connection_type = connection_type;
158         memcpy(&pass_conn_info->dev_info, info, sizeof(bluetooth_device_info_t));
159         /* This need to be freed in timer function */
160         g_timeout_add(CONNECT_TIMEOUT, connect_remote_media_audio,
161                 pass_conn_info);
162         return;
163 }
164
165 void _bt_start_timer_for_connection(char *remote_address, int connection_type)
166 {
167         GArray *dev_list = NULL;
168         int size;
169         int i;
170         int j;
171         bluetooth_device_info_t info;
172         char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
173         dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
174         _bt_get_bonded_devices(&dev_list);
175         size = (dev_list->len) / sizeof(bluetooth_device_info_t);
176
177         for (i = 0; i < size; i++) {
178                 info = g_array_index(dev_list, bluetooth_device_info_t, i);
179                 j = 0;
180                 _bt_convert_addr_type_to_string(bond_address,
181                                 info.device_address.addr);
182                 if (strcmp(bond_address, remote_address) != 0)
183                         continue;
184                 BT_INFO("Device address Matched");
185
186                 while (j != info.service_index) {
187                         BT_INFO("UUID %s", info.uuids[j]);
188                         if (connection_type == BT_AUDIO_A2DP) {
189                                 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
190                                         BT_INFO("Remote Device has A2DP Sink Support start timer");
191                                         _bt_headset_add_timer_function(BT_AUDIO_A2DP, &info);
192                                         goto end;
193                                 }
194                         } else {
195                                 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
196                                         BT_INFO("Remote Device has HFP Sink Support start timer");
197                                         _bt_headset_add_timer_function(BT_AUDIO_HSP, &info);
198                                         goto end;
199                                 }
200                         }
201                         j++;
202                 }
203         }
204 end:
205         g_array_free(dev_list, TRUE);
206 }
207
208 void __bt_connection_manager_set_state(char *remote_address, int event)
209 {
210         bt_connection_node_info_t *info = g_new0(bt_connection_node_info_t, 1);
211
212         char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
213         if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
214                 info->state = BLUETOOTH_HFP_CONNECTED;
215                 _bt_get_bluetooth_device_info(remote_address, &info->dev_info);
216                 _bt_convert_addr_type_to_string(bond_address,
217                                 info->dev_info.device_address.addr);
218                 BT_INFO("Adding HFP Connected device to list");
219                 p_connection_list = g_list_append(p_connection_list, info);
220         } else if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
221                 /* Delete coresponding node */
222                 BT_INFO("Deleting HFP Connected device from list");
223                 GList *list = NULL;
224                 bluetooth_state_type_t state;
225                 bt_connection_node_info_t *h_conn;
226                 state = BLUETOOTH_HFP_CONNECTED;
227                 list = g_list_find_custom(p_connection_list,
228                                 &state, compare_state);
229                 if (list == NULL) {
230                         BT_INFO("Didn't found any device with HFP State");
231                         return;
232                 }
233                 h_conn = list->data;
234                 p_connection_list = g_list_remove(p_connection_list, h_conn);
235                 g_free(h_conn);
236         } else if (event == BLUETOOTH_EVENT_AV_CONNECTED) {
237                 info->state = BLUETOOTH_A2DP_CONNECTED;
238                 _bt_get_bluetooth_device_info(remote_address, &info->dev_info);
239                 _bt_convert_addr_type_to_string(bond_address,
240                                 info->dev_info.device_address.addr);
241                 BT_INFO("Adding A2DP Connected device to list");
242                 p_connection_list = g_list_append(p_connection_list, info);
243         } else if (event == BLUETOOTH_EVENT_AV_DISCONNECTED) {
244                 BT_INFO("Deleting A2DP Connected device from list");
245                 bt_connection_node_info_t *a_conn;
246                 GList *list = NULL;
247                 bluetooth_state_type_t state;
248                 state = BLUETOOTH_A2DP_CONNECTED;
249                 list = g_list_find_custom(p_connection_list,
250                                 &state, compare_state);
251                 if (list == NULL) {
252                         BT_INFO("Didn't found any device with A2DP State");
253                         return;
254                 }
255                 a_conn = list->data;
256                 p_connection_list = g_list_remove(p_connection_list, a_conn);
257                 g_free(a_conn);
258         }
259 }
260