2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <vconf-internal-bt-keys.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
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"
35 #include "bt-service-opp-client.h"
39 static GList *p_connection_list = NULL;
41 BLUETOOTH_NONE_CONNECTED = 0x00,
42 BLUETOOTH_HFP_CONNECTED ,
43 BLUETOOTH_A2DP_CONNECTED,
44 BLUETOOTH_ALL_CONNECTED,
45 } bluetooth_state_type_t;
48 bluetooth_state_type_t state;
49 bluetooth_device_info_t dev_info;
51 } bt_connection_node_info_t;
53 gboolean connection_local = FALSE;
56 void _bt_headset_set_local_connection(gboolean value)
58 BT_INFO("setting connection_local to %d", value);
59 connection_local = value;
62 gboolean _bt_headset_get_local_connection()
64 return connection_local;
67 //gint compare_state(GList *data, bluetooth_state_type_t state)
68 gint compare_state(gconstpointer list_data, gconstpointer conn_state)
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");
80 gboolean connect_remote_media_audio(gpointer user_data)
82 bt_connection_node_info_t *conn_info =
83 (bt_connection_node_info_t *) user_data;
85 bluetooth_state_type_t state;
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");
94 if (conn_info->connection_type == BT_AUDIO_A2DP) {
95 state = BLUETOOTH_A2DP_CONNECTED;
96 list = g_list_find_custom(p_connection_list,
97 &state, compare_state);
99 BT_INFO("Head Set didn't initiated a2dp connection");
100 BT_INFO("local device initiating A2DP connection");
102 _bt_audio_connect(0, BT_AUDIO_A2DP,
103 &conn_info->dev_info.device_address, NULL);
105 BT_INFO("A2DP Connection Already exists");
109 state = BLUETOOTH_HFP_CONNECTED;
110 list = g_list_find_custom(p_connection_list,
111 &state, compare_state);
113 BT_INFO("Headset didn't initiated HFP connection");
114 BT_INFO("local device intiating HFP Connection");
116 _bt_audio_connect(0, BT_AUDIO_HSP,
117 &conn_info->dev_info.device_address, NULL);
119 BT_INFO("HFP Connection Already exists");
126 void _bt_get_bluetooth_device_info(char *remote_address, bluetooth_device_info_t *device)
128 GArray *dev_list = NULL;
131 bluetooth_device_info_t info;
132 char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
137 dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
139 _bt_get_bonded_devices(&dev_list);
140 size = (dev_list->len) / sizeof(bluetooth_device_info_t);
141 for (i = 0; i < size; i++) {
142 info = g_array_index(dev_list, bluetooth_device_info_t, i);
143 _bt_convert_addr_type_to_string(bond_address, info.device_address.addr);
144 if (strcmp(bond_address, remote_address) == 0) {
145 BT_INFO("Match found");
146 memcpy(device, &info, sizeof(bluetooth_device_info_t));
147 g_array_free(dev_list, TRUE);
151 g_array_free(dev_list, TRUE);
155 void _bt_headset_add_timer_function(int connection_type, bluetooth_device_info_t *info)
157 bt_connection_node_info_t *pass_conn_info = NULL;
162 pass_conn_info = g_new0(bt_connection_node_info_t, 1);
163 pass_conn_info->connection_type = connection_type;
164 memcpy(&pass_conn_info->dev_info, info, sizeof(bluetooth_device_info_t));
165 /* This need to be freed in timer function */
166 g_timeout_add(CONNECT_TIMEOUT, connect_remote_media_audio,
171 void _bt_start_timer_for_connection(char *remote_address, int connection_type)
173 GArray *dev_list = NULL;
177 bluetooth_device_info_t info;
178 char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
179 dev_list = g_array_new(FALSE, FALSE, sizeof(gchar));
180 _bt_get_bonded_devices(&dev_list);
181 size = (dev_list->len) / sizeof(bluetooth_device_info_t);
183 for (i = 0; i < size; i++) {
184 info = g_array_index(dev_list, bluetooth_device_info_t, i);
186 _bt_convert_addr_type_to_string(bond_address,
187 info.device_address.addr);
188 if (strcmp(bond_address, remote_address) != 0)
190 BT_INFO("Device address Matched");
192 while (j != info.service_index) {
193 BT_INFO("UUID %s", info.uuids[j]);
194 if (connection_type == BT_AUDIO_A2DP) {
195 if (strcmp(info.uuids[j], A2DP_SINK_UUID) == 0) {
196 BT_INFO("Remote Device has A2DP Sink Support start timer");
197 _bt_headset_add_timer_function(BT_AUDIO_A2DP, &info);
201 if (strcmp(info.uuids[j], HFP_HS_UUID) == 0) {
202 BT_INFO("Remote Device has HFP Sink Support start timer");
203 _bt_headset_add_timer_function(BT_AUDIO_HSP, &info);
211 g_array_free(dev_list, TRUE);
214 void __bt_connection_manager_set_state(char *remote_address, int event)
216 bt_connection_node_info_t *info;
218 char bond_address[BT_ADDRESS_STRING_SIZE] = { 0 };
219 if (event == BLUETOOTH_EVENT_AG_CONNECTED) {
220 info = g_new0(bt_connection_node_info_t, 1);
221 info->state = BLUETOOTH_HFP_CONNECTED;
222 _bt_get_bluetooth_device_info(remote_address, &info->dev_info);
223 _bt_convert_addr_type_to_string(bond_address,
224 info->dev_info.device_address.addr);
225 BT_INFO("Adding HFP Connected device to list");
226 p_connection_list = g_list_append(p_connection_list, info);
227 } else if (event == BLUETOOTH_EVENT_AG_DISCONNECTED) {
228 /* Delete coresponding node */
229 BT_INFO("Deleting HFP Connected device from list");
231 bluetooth_state_type_t state;
232 bt_connection_node_info_t *h_conn;
233 state = BLUETOOTH_HFP_CONNECTED;
234 list = g_list_find_custom(p_connection_list,
235 &state, compare_state);
237 BT_INFO("Didn't found any device with HFP State");
241 p_connection_list = g_list_remove(p_connection_list, h_conn);
243 } else if (event == BLUETOOTH_EVENT_AV_CONNECTED) {
244 info = g_new0(bt_connection_node_info_t, 1);
245 info->state = BLUETOOTH_A2DP_CONNECTED;
246 _bt_get_bluetooth_device_info(remote_address, &info->dev_info);
247 _bt_convert_addr_type_to_string(bond_address,
248 info->dev_info.device_address.addr);
249 BT_INFO("Adding A2DP Connected device to list");
250 p_connection_list = g_list_append(p_connection_list, info);
251 } else if (event == BLUETOOTH_EVENT_AV_DISCONNECTED) {
252 BT_INFO("Deleting A2DP Connected device from list");
253 bt_connection_node_info_t *a_conn;
255 bluetooth_state_type_t state;
256 state = BLUETOOTH_A2DP_CONNECTED;
257 list = g_list_find_custom(p_connection_list,
258 &state, compare_state);
260 BT_INFO("Didn't found any device with A2DP State");
264 p_connection_list = g_list_remove(p_connection_list, a_conn);