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"
26 #include "tizen_proximity.h"
27 #include "tizen_CbleDevice.h"
28 #include "lib_proximity.h"
32 CProximity *prox = NULL;
33 ITSName::Things *output = NULL;
34 int CProximity::get_rssi_cnt = 0;
35 int CProximity::num_set_rssi = 0;
36 int CProximity::loop_flag = JOB_GET_RSSI;
37 CProximity::rssiMAP CProximity::map_rssi;
39 // Proximity variables
46 void proximity_init( void *(*func)(void *) )
51 prox = new CProximity( func );
55 void proximity_loop( void *param )
57 // DBG("called. main loop function.");
58 CbleDevice *pdevice = (CbleDevice *)param;
60 if ( prox == NULL || pdevice == NULL )
62 DBG("Error : proximity_loop is occurred error.");
66 // Get BLE RSSI and Calculate Proximity value. ( choi job )
67 if ( prox->loop_flag == JOB_GET_RSSI )
69 prox->GetRSSI( pdevice );
70 prox->loop_flag = JOB_BUSY;
73 if ( prox->loop_flag == JOB_GET_PROX )
75 prox->GetProximity( pdevice );
76 prox->loop_flag = JOB_PUT_OUTPUT;
79 if ( prox->loop_flag == JOB_PUT_OUTPUT )
81 // Result Setting and transmit data from proximity to ITS
82 prox->set_output( pdevice );
83 pthread_create(&prox->Thandle, NULL, prox->threadFunc, output);
84 prox->loop_flag = JOB_GET_RSSI;
94 /****************************************
96 * CProximity class definition.
100 CProximity::CProximity( workfunc func )
102 #if __INTERNAL_DEBUG_
105 if ( set_threadFunc(func) == false)
106 DBG("Error: set_threadFunc is failed.");
108 loop_flag = JOB_GET_RSSI;
118 CProximity::~CProximity( void )
120 #if __INTERNAL_DEBUG_
161 bool CProximity::find_referenceThing( bt_adapter_device_discovery_info_s *discovery_info ,
162 CbleDevice *pdevice )
164 #if __INTERNAL_DEBUG_
167 if ( pdevice->find(discovery_info->remote_address) == false )
171 #if __INTERNAL_DEBUG_
172 DBG("Find %s device.", discovery_info->remote_address);
174 if ( pdevice->setRSSI( discovery_info->remote_address,
175 discovery_info->rssi ) == discovery_info->rssi )
177 map_rssi[ discovery_info->remote_address ].init( RSSI_EA );
178 map_rssi[ discovery_info->remote_address ].rssi[get_rssi_cnt - 1] = discovery_info->rssi - 65535;
179 #if __INTERNAL_DEBUG_
180 DBG("Get RSSI. %d", map_rssi[ discovery_info->remote_address ].rssi[get_rssi_cnt - 1] );
188 // Called by bt_adapter_start_device_discovery / bt_adapter_stop_device_discovery function.
189 void CProximity::CB_discover_state_change(int result,
190 bt_adapter_device_discovery_state_e discovery_state, \
191 bt_adapter_device_discovery_info_s *discovery_info, void *user_data)
193 DBG("callback called.");
194 #if __INTERNAL_DEBUG_
195 DBG("result = %d", result);
198 CbleDevice *pdevice = (CbleDevice *)user_data;
200 if ( discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FOUND )
202 if ( discovery_info == NULL)
204 #if __INTERNAL_DEBUG_
205 printf("=========== Get RSSI =============\n");
206 printf("= address \t : %s \n", discovery_info->remote_address);
207 printf("= name \t : %s \n", discovery_info->remote_name);
208 printf("= rssi \t : %d \n", (discovery_info->rssi - 65535) );
209 printf("===========================================\n");
211 find_referenceThing( discovery_info , pdevice );
213 if ( num_set_rssi == pdevice->size() )
215 #if __INTERNAL_DEBUG_
216 DBG("Stop searching device.");
218 BT_ERROR_CHECK( bt_adapter_stop_device_discovery() );
221 else if ( discovery_state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED )
223 DBG("state == BT_ADAPTER_DEVICE_DISCOVERY_FINISHED");
226 if ( get_rssi_cnt == RSSI_EA )
229 loop_flag = JOB_GET_PROX;
232 loop_flag = JOB_GET_RSSI;
242 bool CProximity::GetRSSI(CbleDevice *pdevice)
244 #if __INTERNAL_DEBUG_
247 if ( num_set_rssi == 0 && get_rssi_cnt == 0)
253 for ( int i = 0; i < pdevice->size(); i++ )
254 pdevice->setRSSI( (pdevice->getNext(i))->remote_address, 0);
256 BT_ERROR_CHECK( bt_adapter_set_device_discovery_state_changed_cb(
257 CProximity::CB_discover_state_change, pdevice) );
259 BT_ERROR_CHECK( bt_adapter_start_device_discovery() );
267 bool is_first = true;
268 void CProximity::GetProximity(CbleDevice *pdevice)
270 #if __INTERNAL_DEBUG_
273 //#if __INTERNAL_DEBUG_
274 DBG("thing ea = %d", map_rssi.size() );
275 DBG("RSSI ea = %d", RSSI_EA );
277 for (int num = 0; num < map_rssi.size(); num++ )
279 std::string addr = pdevice->getNext(num)->remote_address;
280 DBG("Addr -> %s", addr.c_str() );
281 for (int i = 0; i < RSSI_EA ; i++)
282 DBG(" %d th. rssi = %d", i, map_rssi[addr].rssi[i] );
286 reference_ea = map_rssi.size();
287 float *avg = new float[reference_ea];
290 prox_first_setting( reference_ea );
292 for (int i = 0; i < reference_ea; i++)
294 std::string addr = pdevice->getNext(i)->remote_address;
296 if ( rssicnt[i] > arraysize - 1)
297 rssicnt[i] = rssicnt[i] % arraysize;
299 for (int j = 0; j < RSSI_EA; j++)
301 rssi[i][rssicnt[i]] = map_rssi[addr].rssi[j];
305 avg[i] = CalculateExponentialAverage(RSSI_EA, rssi[i], startindex[i], flag[i]);
306 m_distance[i] = calculateDistance(avg[i], -58);
308 if ( m_distance[i] <= 1.0 )
312 else if (m_distance[i] <= 2)
321 DBG("distance = %f", m_distance[i]);
322 DBG("proximity = %d", m_proximity[i]);
323 //Serial.println(calculateDistance(avg[i], -58));
324 startindex[i] += RSSI_EA;
326 if (startindex[i] >= arraysize)
331 if (flag[i] < (arraysize / RSSI_EA))
340 bool CProximity::set_threadFunc(workfunc func)
342 #if __INTERNAL_DEBUG_
347 DBG("Error: func is null.");
355 CProximity::workfunc CProximity::get_threadFunc(void)
360 void CProximity::set_output( CbleDevice *pdevice )
362 #if __INTERNAL_DEBUG_
372 if ( output == NULL )
374 output = new ITSName::Things(reference_ea);
377 if ( output == NULL )
379 DBG("Error : output is NULL.");
384 for (int i = 0; i < reference_ea; i++)
386 output->things[i].ID = pdevice->getNext(i)->remote_address;
387 output->things[i].SD = 0.0;
388 output->things[i].distance = m_distance[i];
389 output->things[i].proximity = m_proximity[i];
396 void CProximity::prox_first_setting( int reference_ea )
400 rssi = new int *[reference_ea];
402 for (int i = 0; i < reference_ea; i++)
404 rssi[i] = new int[arraysize];
405 for (int j = 0; j < arraysize; j++)
410 if ( rssicnt == NULL )
412 rssicnt = new int[reference_ea];
413 for (int j = 0; j < reference_ea; j++)
417 if ( startindex == NULL )
419 startindex = new int[reference_ea];
420 for (int j = 0; j < reference_ea; j++)
426 flag = new int[reference_ea];
427 for (int j = 0; j < reference_ea; j++)
431 if ( m_distance == NULL )
433 m_distance = new float[reference_ea];
434 for (int j = 0; j < reference_ea; j++)
438 if ( m_proximity == NULL )
440 m_proximity = new int[reference_ea];
441 for (int j = 0; j < reference_ea; j++)