[SSM] Modify ommited arduino sample and soft sensor sample
[platform/upstream/iotivity.git] / service / soft-sensor-manager / SoftSensorPlugin / IndoorTrajectorySensor / src / tizen_CbleDevice.cpp
1 /******************************************************************
2 *
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
4 *
5 *
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 ******************************************************************/
20
21 #include <stdio.h>
22 #include "tizen_log.h"
23 #include "tizen_CbleDevice.h"
24
25 #define TIME_LIMITE     5       // unit : second
26
27 CbleDevice::CbleDevice() : time_count(0) { clear(); }
28 CbleDevice::~CbleDevice() { clear(); }
29
30 int CbleDevice::insert( CbleDevice::ble_value *discovery_info )
31 {
32     DBG("Enter.");
33     std::string addr = std::string(discovery_info->remote_address);
34
35     if ( find(addr) == false )
36     {
37         device[addr] = *discovery_info;
38
39         device[addr].remote_address = new char[ strlen(discovery_info->remote_address) + 1 ];
40         strcpy( device[addr].remote_address, discovery_info->remote_address);
41         device[addr].remote_name = new char[ strlen(discovery_info->remote_name) + 1 ];
42         strcpy( device[addr].remote_name, discovery_info->remote_name);
43         device[addr].service_uuid = NULL;
44         device[addr].service_count = 0;
45     }
46
47     return device.size();
48 }
49
50 CbleDevice::ble_value *CbleDevice::getNext( int seq )
51 {
52     DBG("Enter.");
53     CbleDeviceMAP::iterator itr = device.begin();
54
55     if ( seq < 0 || seq >= device.size())
56     {
57 #if __INTERNAL_DEBUG_
58         DBG("return null");
59 #endif
60         return NULL;
61     }
62
63     for (; seq ; seq--)
64         itr++;
65
66 #if __INTERNAL_DEBUG_
67     DBG("return %s pointer", itr->second.remote_address );
68 #endif
69     return &itr->second;
70 }
71
72 int CbleDevice::setRSSI( std::string addr, int rssi )
73 {
74     DBG("Enter.");
75     CbleDeviceMAP::iterator itr = device.find( addr );
76
77     if ( itr == device.end() )
78         return NULL;
79
80     ble_value *pdevice = &itr->second;
81
82     if ( rssi == NULL )
83     {
84         pdevice->service_count = NULL;
85         pdevice->rssi = 0;
86     }
87     else if ( pdevice->service_count == NULL )
88     {
89         pdevice->service_count = 1;
90         pdevice->rssi = rssi;
91     }
92     else
93         return NULL;
94
95     return pdevice->rssi;
96 }
97
98 CbleDevice::ble_value *CbleDevice::get( std::string addr )
99 {
100     DBG("Enter.");
101     CbleDeviceMAP::iterator itr = device.find( addr );
102
103     if ( itr == device.end() )
104         return NULL;
105
106     return &itr->second;
107 }
108
109 bool CbleDevice::find( std::string addr )
110 {
111     DBG("Enter.");
112     CbleDeviceMAP::iterator itr = device.find( addr );
113
114     if ( itr == device.end() )
115         return false;
116
117     return true;
118 }
119
120 int CbleDevice::size( void )
121 {
122     DBG("Enter.");
123     return device.size();
124 }
125
126 void CbleDevice::erase( CbleDeviceMAP::iterator itr )
127 {
128     ble_value *value = &itr->second;
129
130     if ( value->remote_address )
131         delete value->remote_address;
132     if ( value->remote_name )
133         delete value->remote_name;
134     if ( value->service_uuid )
135         delete [] value->service_uuid;
136
137     device.erase(itr);
138 }
139
140 void CbleDevice::clear( void )
141 {
142     CbleDeviceMAP::iterator itr = device.begin();
143
144     if ( itr != device.end() )
145     {
146         erase(itr);
147         itr++;
148     }
149 }
150
151 bool CbleDevice::isTimeOut( void )
152 {
153     DBG("Enter.");
154     if ( time_count > TIME_LIMITE )
155         return true;
156     else
157         return false;
158 }
159
160