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.
18 #include <bluetooth.h>
26 #define LOG_TAG "BT_CHAT_SERVER"
31 static GMainLoop* g_mainloop = NULL;
32 static bt_adapter_visibility_mode_e visibility_mode = BT_ADAPTER_VISIBILITY_MODE_NON_DISCOVERABLE;
33 static bt_adapter_state_e bt_state = BT_ADAPTER_DISABLED;
34 static int service_socket = -1;
35 static int connected_socket = -1;
36 static char* quit_command = "Quit";
42 gboolean timeout_func(gpointer data)
44 LOGE("[%s] Timeout.", __FUNCTION__);
47 g_main_loop_quit((GMainLoop*)data);
53 void bt_state_changed_impl(int result, bt_adapter_state_e adapter_state, void* user_data)
55 if( adapter_state == BT_ADAPTER_ENABLED )
57 if( result == BT_ERROR_NONE )
59 LOGI("[%s] Callback: BT was enabled successfully.", __FUNCTION__);
60 bt_state = BT_ADAPTER_ENABLED;
64 LOGE("[%s] Callback: Failed to enable BT.", __FUNCTION__);
70 g_main_loop_quit(g_mainloop);
74 void bt_socket_connection_state_changed_impl(int result, bt_socket_connection_state_e connection_state,
75 bt_socket_connection_s *connection, void *user_data)
77 if( result == BT_ERROR_NONE )
79 LOGI("[%s] Callback: Result is BT_ERROR_NONE.", __FUNCTION__);
83 LOGI("[%s] Callback: Result is not BT_ERROR_NONE.", __FUNCTION__);
86 if( connection_state == BT_SOCKET_CONNECTED )
88 LOGI("[%s] Callback: Connected.", __FUNCTION__);
89 if( connection != NULL )
91 connected_socket = connection->socket_fd;
92 LOGI("[%s] Callback: Socket of connection - %d.", __FUNCTION__, connected_socket);
93 LOGI("[%s] Callback: Role of connection - %d.", __FUNCTION__, connection->local_role);
94 LOGI("[%s] Callback: Address of connection - %s.", __FUNCTION__, connection->remote_address);
99 LOGI("[%s] Callback: No connection data", __FUNCTION__);
104 LOGI("[%s] Callback: Disconnected.", __FUNCTION__);
105 LOGI("[%s] Callback: Socket of connection - %d.", __FUNCTION__, connected_socket);
106 LOGI("[%s] Callback: Role of connection - %d.", __FUNCTION__, connection->local_role);
107 LOGI("[%s] Callback: Address of connection - %s.", __FUNCTION__, connection->remote_address);
111 void bt_socket_data_received_impl(bt_socket_received_data_s *data, void *user_data)
113 if( data->data_size > 0 )
115 if( !strncmp(data->data, quit_command, data->data_size) )
117 LOGI("[%s] Callback: Quit command.", __FUNCTION__);
120 g_main_loop_quit(g_mainloop);
124 if( bt_socket_send_data(connected_socket, quit_command, strlen(quit_command)) == BT_ERROR_NONE )
126 LOGI("[%s] Callback: Send quit command.", __FUNCTION__);
130 LOGE("[%s] Callback: bt_socket_send_data() failed.", __FUNCTION__);
135 LOGE("[%s] Callback: No data.", __FUNCTION__);
142 g_mainloop = g_main_loop_new(NULL, FALSE);
143 const char* my_uuid="11011101-0000-1000-8000-00805F9B34FB";
146 LOGI("[%s] Server starts.", __FUNCTION__);
148 if(bt_initialize() != BT_ERROR_NONE)
150 LOGE("[%s] bt_initialize() failed.", __FUNCTION__);
154 if(bt_adapter_get_state(&bt_state) != BT_ERROR_NONE)
156 LOGE("[%s] bt_adapter_get_state() failed.", __FUNCTION__);
161 if(bt_state == BT_ADAPTER_DISABLED)
163 if(bt_adapter_set_state_changed_cb(bt_state_changed_impl, NULL) != BT_ERROR_NONE)
165 LOGE("[%s] bt_adapter_set_state_changed_cb() failed.", __FUNCTION__);
169 if(bt_adapter_enable() == BT_ERROR_NONE)
171 LOGI("[%s] bt_adapter_state_changed_cb will be called.", __FUNCTION__);
172 timeout_id = g_timeout_add (60000, timeout_func, g_mainloop);
173 g_main_loop_run(g_mainloop);
174 g_source_remove(timeout_id);
178 LOGE("[%s] bt_adapter_enable() failed.", __FUNCTION__);
184 LOGI("[%s] BT was already enabled.", __FUNCTION__);
187 // Set name as "chat_server"
188 if(bt_state == BT_ADAPTER_ENABLED)
191 if(bt_adapter_get_name(&name) != BT_ERROR_NONE)
193 LOGE("[%s] bt_adapter_get_name() failed.", __FUNCTION__);
197 if(strncmp(name, "chat_server", strlen(name)) != 0)
200 if(bt_adapter_set_name("chat_server") != BT_ERROR_NONE)
202 LOGE("[%s] bt_adapter_set_name() failed.", __FUNCTION__);
209 LOGE("[%s] BT is not enabled.", __FUNCTION__);
213 // Set visibility as BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE
214 if(bt_adapter_get_visibility(&visibility_mode) != BT_ERROR_NONE)
216 LOGE("[%s] bt_adapter_get_visibility() failed.", __FUNCTION__);
220 if(visibility_mode != BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE)
222 if(bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0) != BT_ERROR_NONE)
224 LOGE("[%s] bt_adapter_set_visibility() failed.", __FUNCTION__);
227 visibility_mode = BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE;
231 LOGI("[%s] Visibility mode was already set as BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE.", __FUNCTION__);
234 // Connecting socket as a server
235 if(bt_socket_create_rfcomm(my_uuid, &service_socket) != BT_ERROR_NONE)
237 LOGE("[%s] bt_socket_create_rfcomm() failed.", __FUNCTION__);
240 LOGI("[%s] socket is created - %d.", __FUNCTION__, service_socket);
242 if(bt_socket_set_connection_state_changed_cb(bt_socket_connection_state_changed_impl, NULL) != BT_ERROR_NONE)
244 LOGE("[%s] bt_socket_set_connection_state_changed_cb() failed.", __FUNCTION__);
248 if(bt_socket_set_data_received_cb(bt_socket_data_received_impl, NULL) != BT_ERROR_NONE)
250 LOGE("[%s] bt_socket_set_data_received_cb() failed.", __FUNCTION__);
254 if(bt_socket_listen_and_accept_rfcomm(service_socket, 5) == BT_ERROR_NONE)
256 LOGI("[%s] bt_socket_connection_state_changed_cb will be called.", __FUNCTION__);
257 g_main_loop_run(g_mainloop);
261 LOGE("[%s] bt_socket_listen_and_accept_rfcomm() failed.", __FUNCTION__);
265 sleep(5); // Wait for completing delivery
266 if(bt_socket_destroy_rfcomm(service_socket) != BT_ERROR_NONE)
268 LOGE("[%s] bt_socket_destroy_rfcomm() failed.", __FUNCTION__);
273 LOGE("[%s] bt_socket_destroy_rfcomm() succeeded.", __FUNCTION__);
278 LOGI("[%s] Server ends.", __FUNCTION__);