Git init
[framework/api/bluetooth.git] / test / bt_chat_server.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 <bluetooth.h>
19 #include <dlog.h>
20 #include <glib.h>
21 #include <string.h>
22
23 #ifdef LOG_TAG
24 #undef LOG_TAG
25 #endif
26 #define LOG_TAG "BT_CHAT_SERVER"
27
28 /**
29  *   Variables
30  **/
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";
37
38
39 /**
40  *   Callback functions
41  **/
42 gboolean timeout_func(gpointer data)
43 {
44         LOGE("[%s] Timeout.", __FUNCTION__);
45         if(g_mainloop)
46         {
47                 g_main_loop_quit((GMainLoop*)data);
48         }
49         
50         return FALSE;
51 }
52
53 void bt_state_changed_impl(int result, bt_adapter_state_e adapter_state, void* user_data)
54 {
55         if( adapter_state == BT_ADAPTER_ENABLED )
56         {
57                 if( result == BT_ERROR_NONE )
58                 {
59                         LOGI("[%s] Callback: BT was enabled successfully.", __FUNCTION__);
60                         bt_state = BT_ADAPTER_ENABLED;
61                 }
62                 else
63                 {
64                         LOGE("[%s] Callback: Failed to enable BT.", __FUNCTION__);
65                 }
66         }
67
68         if(g_mainloop)
69         {
70                 g_main_loop_quit(g_mainloop);                                
71         }       
72 }
73
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)
76 {
77         if( result == BT_ERROR_NONE )
78         {
79                 LOGI("[%s] Callback: Result is BT_ERROR_NONE.", __FUNCTION__);
80         }
81         else
82         {
83                 LOGI("[%s] Callback: Result is not BT_ERROR_NONE.", __FUNCTION__);
84         }
85         
86         if( connection_state == BT_SOCKET_CONNECTED )
87         {
88                 LOGI("[%s] Callback: Connected.", __FUNCTION__);                
89                 if( connection != NULL )
90                 {       
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);                   
95                         
96                 }
97                 else
98                 {
99                         LOGI("[%s] Callback: No connection data", __FUNCTION__);
100                 }
101         }
102         else
103         {
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);                   
108         }
109 }
110
111 void bt_socket_data_received_impl(bt_socket_received_data_s *data, void *user_data)
112 {
113         if( data->data_size > 0 )
114         {
115                 if( !strncmp(data->data, quit_command, data->data_size) )
116                 {
117                         LOGI("[%s] Callback: Quit command.", __FUNCTION__);     
118                         if(g_mainloop)
119                         {
120                                 g_main_loop_quit(g_mainloop);                                
121                         }               
122                 }
123
124                 if( bt_socket_send_data(connected_socket, quit_command, strlen(quit_command)) == BT_ERROR_NONE )
125                 {
126                         LOGI("[%s] Callback: Send quit command.", __FUNCTION__);                                                
127                 }
128                 else
129                 {
130                         LOGE("[%s] Callback: bt_socket_send_data() failed.", __FUNCTION__);
131                 }               
132         }
133         else
134         {
135                 LOGE("[%s] Callback: No data.", __FUNCTION__);
136         }
137 }
138
139
140 int main()
141 {       
142         g_mainloop = g_main_loop_new(NULL, FALSE);
143         const char* my_uuid="11011101-0000-1000-8000-00805F9B34FB";
144         int timeout_id = -1;
145
146         LOGI("[%s] Server starts.", __FUNCTION__);
147         
148         if(bt_initialize() != BT_ERROR_NONE)
149         {
150                 LOGE("[%s] bt_initialize() failed.", __FUNCTION__);
151                 return -1;
152         }
153
154         if(bt_adapter_get_state(&bt_state) != BT_ERROR_NONE)
155         {
156                 LOGE("[%s] bt_adapter_get_state() failed.", __FUNCTION__);
157                 return -1;
158         }
159
160         //      Enable BT
161         if(bt_state == BT_ADAPTER_DISABLED)
162         {
163                 if(bt_adapter_set_state_changed_cb(bt_state_changed_impl, NULL) != BT_ERROR_NONE)
164                 {
165                         LOGE("[%s] bt_adapter_set_state_changed_cb() failed.", __FUNCTION__);
166                         return -1;
167                 }
168
169                 if(bt_adapter_enable() == BT_ERROR_NONE)
170                 {
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);
175                 }
176                 else
177                 {
178                         LOGE("[%s] bt_adapter_enable() failed.", __FUNCTION__);
179                         return -1;
180                 }
181         }
182         else
183         {
184                 LOGI("[%s] BT was already enabled.", __FUNCTION__);
185         }
186
187         //  Set name as "chat_server"
188         if(bt_state == BT_ADAPTER_ENABLED)
189         {
190                 char* name = NULL; 
191                 if(bt_adapter_get_name(&name) != BT_ERROR_NONE)
192                 {
193                         LOGE("[%s] bt_adapter_get_name() failed.", __FUNCTION__);
194                         return -1;
195                 }
196
197                 if(strncmp(name, "chat_server", strlen(name)) != 0)
198                 {
199                         free(name);
200                         if(bt_adapter_set_name("chat_server") != BT_ERROR_NONE)
201                         {
202                                 LOGE("[%s] bt_adapter_set_name() failed.", __FUNCTION__);
203                                 return -1;
204                         }                       
205                 }
206         }
207         else
208         {
209                 LOGE("[%s] BT is not enabled.", __FUNCTION__);
210                 return -1;
211         }       
212                 
213         //  Set visibility as BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE 
214         if(bt_adapter_get_visibility(&visibility_mode) != BT_ERROR_NONE)
215         {
216                 LOGE("[%s] bt_adapter_get_visibility() failed.", __FUNCTION__);
217                 return -1;
218         }
219         
220         if(visibility_mode != BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE)
221         {               
222                 if(bt_adapter_set_visibility(BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE, 0) != BT_ERROR_NONE)
223                 {
224                         LOGE("[%s] bt_adapter_set_visibility() failed.", __FUNCTION__);
225                         return -1;
226                 }
227                 visibility_mode = BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE;
228         }
229         else
230         {                       
231                 LOGI("[%s] Visibility mode was already set as BT_ADAPTER_VISIBILITY_MODE_GENERAL_DISCOVERABLE.", __FUNCTION__);
232         }               
233
234         //      Connecting socket as a server
235         if(bt_socket_create_rfcomm(my_uuid, &service_socket) != BT_ERROR_NONE)
236         {
237                 LOGE("[%s] bt_socket_create_rfcomm() failed.", __FUNCTION__);
238                 return -1;
239         }
240         LOGI("[%s] socket is created - %d.", __FUNCTION__, service_socket);
241
242         if(bt_socket_set_connection_state_changed_cb(bt_socket_connection_state_changed_impl, NULL) != BT_ERROR_NONE)
243         {
244                 LOGE("[%s] bt_socket_set_connection_state_changed_cb() failed.", __FUNCTION__);
245                 return -1;
246         }
247
248         if(bt_socket_set_data_received_cb(bt_socket_data_received_impl, NULL) != BT_ERROR_NONE)
249         {
250                 LOGE("[%s] bt_socket_set_data_received_cb() failed.", __FUNCTION__);
251                 return -1;
252         }
253
254         if(bt_socket_listen_and_accept_rfcomm(service_socket, 5) == BT_ERROR_NONE)
255         {
256                 LOGI("[%s] bt_socket_connection_state_changed_cb will be called.", __FUNCTION__);
257                 g_main_loop_run(g_mainloop);                    
258         }
259         else
260         {
261                 LOGE("[%s] bt_socket_listen_and_accept_rfcomm() failed.", __FUNCTION__);                                
262                 return -1;
263         }
264
265         sleep(5);       // Wait for completing delivery
266         if(bt_socket_destroy_rfcomm(service_socket) != BT_ERROR_NONE)
267         {
268                 LOGE("[%s] bt_socket_destroy_rfcomm() failed.", __FUNCTION__);
269                 return -1;
270         }       
271         else
272         {
273                 LOGE("[%s] bt_socket_destroy_rfcomm() succeeded.", __FUNCTION__);
274         }
275
276         bt_deinitialize();
277         
278         LOGI("[%s] Server ends.", __FUNCTION__);
279         return 0;
280 }