1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 #include "tizen_log.h"
27 #include "tizen_public_ble.h"
28 #include "tizen_ble_bgthread.h"
29 #include "tizen_CbleDevice.h"
31 CbleBGthread *CbleBGthread::singleton = NULL;
32 CbleBGthread::workfunc CbleBGthread::workProcess = NULL;
33 bool CbleBGthread::ble_adapter_flag = false;
34 int CbleBGthread::ble_discover_flag = PROCESS_FREE;
35 int CbleBGthread::ble_bond_flag = PROCESS_FREE;
36 bool CbleBGthread::isThreadRun = false;
37 int CbleBGthread::reg_bond_cnt = 0;
38 pthread_t CbleBGthread::Thandle = 0;
40 // tizen ble init function.
41 void ble_init( CbleBGthread::workfunc func )
45 CbleBGthread *ble = CbleBGthread::GetInstance( func );
48 DBG("Error : function is failed.");
52 BT_ERROR_CHECK( bt_initialize() );
54 BT_ERROR_CHECK( bt_adapter_set_state_changed_cb(CbleBGthread::CB_adapter_state_change, NULL) );
57 bt_error_e returnValue;
58 returnValue = (bt_error_e)bt_adapter_enable();
59 if (returnValue != BT_ERROR_NONE )
61 what_return_value( returnValue );
62 if ( returnValue != BT_ERROR_ALREADY_DONE)
67 DBG("return == BT_ERROR_ALREADY_DONE");
69 CbleBGthread::set_adapter_state_flag();
74 DBG("Register discovery callback func.");
76 static CbleDevice device;
77 BT_ERROR_CHECK( bt_adapter_set_device_discovery_state_changed_cb(
78 CbleBGthread::CB_discover_state_change, &device) );
81 // DBG("Register bond create callback func.");
82 // BT_ERROR_CHECK( bt_device_set_bond_created_cb( CbleBGthread::CB_bond_create , &device) );
85 ble->isThreadRun = true;
86 pthread_create(&ble->Thandle, NULL, CbleBGthread::ble_thread, &device);
92 DBG("Error Exception. Exit.");
96 //void workProcess( void )
98 // DBG("WorkProcess run.");
101 /*********************************
102 * CbleBGthread class definition.
104 CbleBGthread::CbleBGthread(workfunc func)
107 ble_adapter_flag = false;
108 ble_discover_flag = PROCESS_FREE;
109 ble_bond_flag = PROCESS_FREE;
115 CbleBGthread::~CbleBGthread()
123 CbleBGthread *CbleBGthread::GetInstance( workfunc func )
128 if (singleton == NULL)
129 singleton = new CbleBGthread(func);
134 /*****************************
135 * BLE processing thread.
137 void *CbleBGthread::ble_thread(void *param)
140 CbleDevice *pdevice = (CbleDevice *)param;
145 pdevice->time_count++;
147 if ( ble_adapter_flag )
150 if ( ble_discover_flag == PROCESS_FREE)
152 if ( ble_discovery() == false)
155 ble_discover_flag = PROCESS_TRYING;
159 // if( ble_discover_flag == PROCESS_COMPLETE && ble_bond_flag == PROCESS_FREE )
163 // DBG( "Try bond referenceThing List. size=%d, cnt=%d", pdevice->size(), reg_bond_cnt );
164 // if( reg_bond_cnt < pdevice->size() )
166 // BT_ERROR_CHECK(bt_device_create_bond( (pdevice->getNext(reg_bond_cnt))->remote_address ));
167 // ble_bond_flag = PROCESS_TRYING;
169 //// for( int i = 0; i < pdevice->size(); i++ )
170 //// BT_ERROR_CHECK(bt_device_create_bond( (pdevice->getNext(i))->remote_address ));
174 // if( ble_bond_flag == PROCESS_COMPLETE )
175 if ( ble_discover_flag == PROCESS_COMPLETE )
182 } // if ( ble_adapter_flag )
183 }// while(isThreadRun )
189 workProcess( (void *)pdevice );
197 /******************************
198 * Callback Function definition.
201 // Called by adapter_enable / adapter_disable function.
202 void CbleBGthread::CB_adapter_state_change(int result, bt_adapter_state_e adapter_state,
205 DBG("callback called.");
207 set_adapter_state_flag();
210 // Called by bt_adapter_start_device_discovery / bt_adapter_stop_device_discovery function.
211 void CbleBGthread::CB_discover_state_change(int result,
212 bt_adapter_device_discovery_state_e discovery_state, \
213 bt_adapter_device_discovery_info_s *discovery_info, void *user_data)
215 DBG("callback called.");
216 #if __INTERNAL_DEBUG_
217 DBG("result = %d", result);
220 if ( discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FOUND )
222 CbleDevice *pdevice = (CbleDevice *)user_data;
224 if ( discovery_info == NULL)
227 #if __INTERNAL_DEBUG_
228 printf("=========== discovered device =============\n");
229 printf("= address \t : %s \n", discovery_info->remote_address);
230 printf("= name \t : %s \n", discovery_info->remote_name);
231 printf("= rssi \t : %d \n", (discovery_info->rssi - 65535) );
232 printf("= is_bonded\t : %d \n", discovery_info->is_bonded);
233 printf("= svc_count\t : %d \n", discovery_info->service_count);
234 printf("= dev_type \t : %d \n", discovery_info->device_type);
235 printf("===========================================\n");
238 find_referenceThing( discovery_info , pdevice );
240 if ( pdevice->isTimeOut() == true )
242 #if __INTERNAL_DEBUG_
243 DBG("Stop searching device.");
245 BT_ERROR_CHECK( bt_adapter_stop_device_discovery() );
248 else if ( discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED )
250 #if __INTERNAL_DEBUG_
251 DBG("state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED");
254 ble_discover_flag = PROCESS_COMPLETE;
263 // (*bt_device_bond_created_cb)
264 void CbleBGthread::CB_bond_create(int result, bt_device_info_s *device_info, void *user_data)
266 DBG("callback called.");
267 if ( (bt_error_e)result != BT_ERROR_NONE )
269 what_return_value( (bt_error_e)result );
270 if ( (bt_error_e)result == BT_ERROR_REMOTE_DEVICE_NOT_FOUND )
271 ble_bond_flag = PROCESS_FREE;
276 CbleDevice *pdevice = (CbleDevice *)user_data;
280 #if __INTERNAL_DEBUG_
281 printf("=========== bonded device =============\n");
282 printf("= address \t : %s \n", device_info->remote_address);
283 printf("= name \t : %s \n", device_info->remote_name);
284 printf("= is_bonded\t : %d \n", device_info->is_bonded);
285 printf("= is_connec\t : %d \n", device_info->is_connected);
286 printf("= is_autho \t : %d \n", device_info->is_authorized);
287 printf("= svc_count\t : %d \n", device_info->service_count);
288 printf("===========================================\n");
291 if ( pdevice->find( std::string(device_info->remote_address) ) )
294 ble_bond_flag = PROCESS_FREE;
295 #if __INTERNAL_DEBUG_
296 DBG( "bonded_cnt = %d", reg_bond_cnt );
300 if ( reg_bond_cnt == pdevice->size() )
302 #if __INTERNAL_DEBUG_
303 DBG("bond_flag is PROCESS_COMPLETE");
306 ble_bond_flag = PROCESS_COMPLETE;
315 /********************************
316 * ETC. function definition.
318 void CbleBGthread::set_adapter_state_flag(void)
322 // Get adapter state.
323 bt_adapter_state_e state;
324 BT_ERROR_CHECK( bt_adapter_get_state(&state) );
326 if ( state == BT_ADAPTER_ENABLED)
327 ble_adapter_flag = true;
329 ble_adapter_flag = false;
334 DBG("Error Exception. Exit.");
337 // return : true --> OK
339 bool CbleBGthread::ble_discovery( void )
341 DBG("is Searching device.");
342 bool isDiscovery = false;
343 BT_ERROR_CHECK(bt_adapter_is_discovering(&isDiscovery));
345 if ( isDiscovery == false )
347 #if __INTERNAL_DEBUG_
348 DBG("Start searching device.");
350 BT_ERROR_CHECK( bt_adapter_start_device_discovery() );
359 bool CbleBGthread::find_referenceThing( bt_adapter_device_discovery_info_s *discovery_info ,
360 CbleDevice *pdevice )
364 if ( strstr( discovery_info->remote_name, REF_NAME ) )
366 #if __INTERNAL_DEBUG_
367 DBG("Find %s device.", REF_NAME);
368 DBG("Register %s device to reference-Thing.", discovery_info->remote_name );
370 pdevice->insert( discovery_info );
376 void what_return_value( bt_error_e returnValue )
379 DBG("returnValue=%d", returnValue);
384 DBG("return value is BT_ERROR_NONE\n");
386 case BT_ERROR_CANCELLED:
387 DBG("return value is BT_ERROR_CANCELLED\n");
389 case BT_ERROR_INVALID_PARAMETER:
390 DBG("return value is BT_ERROR_INVALID_PARAMETER\n");
392 case BT_ERROR_OUT_OF_MEMORY:
393 DBG("return value is BT_ERROR_OUT_OF_MEMORY\n");
395 case BT_ERROR_RESOURCE_BUSY:
396 DBG("return value is BT_ERROR_RESOURCE_BUSY\n");
398 case BT_ERROR_TIMED_OUT:
399 DBG("return value is BT_ERROR_TIMED_OUT\n");
401 case BT_ERROR_NOW_IN_PROGRESS:
402 DBG("return value is BT_ERROR_NOW_IN_PROGRESS\n");
404 case BT_ERROR_NOT_INITIALIZED:
405 DBG("return value is BT_ERROR_NOT_INITIALIZED\n");
407 case BT_ERROR_NOT_ENABLED:
408 DBG("return value is BT_ERROR_NOT_ENABLED\n");
410 case BT_ERROR_ALREADY_DONE:
411 DBG("return value is BT_ERROR_ALREADY_DONE\n");
413 case BT_ERROR_OPERATION_FAILED:
414 DBG("return value is BT_ERROR_OPERATION_FAILED\n");
416 case BT_ERROR_NOT_IN_PROGRESS:
417 DBG("return value is BT_ERROR_NOT_IN_PROGRESS\n");
419 case BT_ERROR_REMOTE_DEVICE_NOT_BONDED:
420 DBG("return value is BT_ERROR_REMOTE_DEVICE_NOT_BONDED\n");
422 case BT_ERROR_AUTH_REJECTED:
423 DBG("return value is BT_ERROR_AUTH_REJECTED\n");
425 case BT_ERROR_AUTH_FAILED:
426 DBG("return value is BT_ERROR_AUTH_FAILED\n");
428 case BT_ERROR_REMOTE_DEVICE_NOT_FOUND:
429 DBG("return value is BT_ERROR_REMOTE_DEVICE_NOT_FOUND\n");
431 case BT_ERROR_SERVICE_SEARCH_FAILED:
432 DBG("return value is BT_ERROR_SERVICE_SEARCH_FAILED\n");
434 case BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED:
435 DBG("return value is BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED\n");
437 case BT_ERROR_PERMISSION_DENIED:
438 DBG("return value is BT_ERROR_PERMISSION_DENIED\n");
444 //int bt_adapter_get_le_state ( bt_adapter_le_state_e * adapter_le_state )
445 //bt_adapter_le_enable()
446 // bt_adapter_get_state()
448 // bt_adapter_start_device_discovery().
449 // bt_adapter_device_discovery_state_changed_cb().
450 // bt_device_connect_le().
451 // bt_device_gatt_state_changed_cb().
452 // bt_device_read_rssi_value(const char *address);
455 //int bt_device_set_connection_state_changed_cb(bt_device_connection_state_changed_cb callback, void *user_data);
456 //typedef void (*bt_device_connection_state_changed_cb)(bool connected, const char *remote_address, void *user_data);
459 // bt_adapter_start_device_discovery().
460 // bt_adapter_device_discovery_state_changed_cb().
461 // bt_device_create_bond()
462 // bt_device_bond_created_cb().
463 // bt_device_set_alias(address,name)