4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include <client_common.h>
21 #include <sensor_handle_info.h>
26 unsigned long long sensor_handle_info::m_event_id = 0;
28 attribute_info::attribute_info()
34 attribute_info::~attribute_info()
43 bool attribute_info::set(const char *value, int len)
45 retvm_if(len < 0, false, "Invalid length");
50 m_attr = new(std::nothrow) char[len];
51 retvm_if(!m_attr, false, "Failed to allocate memory");
53 memcpy(m_attr, value, (unsigned int)len);
59 char *attribute_info::get(void)
64 unsigned int attribute_info::size(void)
69 sensor_handle_info::sensor_handle_info()
71 , m_sensor_id(UNKNOWN_SENSOR)
72 , m_sensor_state(SENSOR_STATE_UNKNOWN)
73 , m_pause_policy(SENSORD_PAUSE_ALL)
74 , m_bad_accuracy(false)
77 , m_accuracy_user_data(NULL)
82 sensor_handle_info::~sensor_handle_info()
87 reg_event_info* sensor_handle_info::get_reg_event_info(unsigned int event_type)
89 auto it_event = m_reg_event_infos.find(event_type);
91 if (it_event == m_reg_event_infos.end())
94 return &(it_event->second);
97 void sensor_handle_info::get_reg_event_types(event_type_vector &event_types)
99 auto it_event = m_reg_event_infos.begin();
101 while (it_event != m_reg_event_infos.end()) {
102 event_types.push_back(it_event->first);
107 bool sensor_handle_info::add_reg_event_info(unsigned int event_type, unsigned int interval, unsigned int latency, void *cb, void *user_data)
109 reg_event_info event_info;
111 auto it_event = m_reg_event_infos.find(event_type);
113 if (it_event != m_reg_event_infos.end()) {
114 _E("Event %s[%#x] is already registered for client %s", get_event_name(event_type), event_type, get_client_name());
118 event_info.m_id = renew_event_id();
119 event_info.m_handle = m_handle;
120 event_info.type = event_type;
121 event_info.m_interval = interval;
122 event_info.m_latency = latency;
123 event_info.m_cb = cb;
124 event_info.m_user_data = user_data;
126 m_reg_event_infos.insert(pair<unsigned int, reg_event_info>(event_type, event_info));
131 bool sensor_handle_info::delete_reg_event_info(unsigned int event_type)
133 auto it_event = m_reg_event_infos.find(event_type);
135 if (it_event == m_reg_event_infos.end()) {
136 _E("Event %s[%#x] is not registered for client %s", get_event_name(event_type), event_type, get_client_name());
140 m_reg_event_infos.erase(it_event);
145 void sensor_handle_info::clear(void)
147 sensor_attribute_str_map::iterator it_attr;
149 for (it_attr = attributes_str.begin(); it_attr != attributes_str.end(); ++it_attr)
150 delete it_attr->second;
152 attributes_int.clear();
153 attributes_str.clear();
156 void sensor_handle_info::clear_all_events(void)
158 m_reg_event_infos.clear();
161 unsigned long long sensor_handle_info::renew_event_id(void)
166 bool sensor_handle_info::change_reg_event_batch(unsigned int event_type, unsigned int interval, unsigned int latency)
168 auto it_event = m_reg_event_infos.find(event_type);
170 if (it_event == m_reg_event_infos.end()) {
171 _E("Event %s[%#x] is not registered for client %s", get_event_name(event_type), event_type, get_client_name());
175 it_event->second.m_id = renew_event_id();
176 it_event->second.m_interval = interval;
177 it_event->second.m_latency = latency;
182 void sensor_handle_info::get_batch(unsigned int &interval, unsigned int &latency)
184 if (m_reg_event_infos.empty()) {
185 _D("No events are registered for client %s", get_client_name());
186 interval = POLL_10HZ_MS;
191 unsigned int min_interval = POLL_MAX_HZ_MS;
192 unsigned int min_latency = std::numeric_limits<unsigned int>::max();
194 unsigned int _interval;
195 unsigned int _latency;
197 auto it_event = m_reg_event_infos.begin();
199 while (it_event != m_reg_event_infos.end()) {
200 _interval = it_event->second.m_interval;
201 _latency = it_event->second.m_latency;
203 min_interval = (_interval < min_interval) ? _interval : min_interval;
204 min_latency = (_latency < min_latency) ? _latency : min_latency;
208 interval = min_interval;
209 latency = min_latency;
212 unsigned int sensor_handle_info::get_reg_event_count(void)
214 return m_reg_event_infos.size();
217 bool sensor_handle_info::get_passive_mode(void)
222 void sensor_handle_info::set_passive_mode(bool passive)
227 bool sensor_handle_info::is_started(void)
229 return (m_sensor_state == SENSOR_STATE_STARTED) || m_passive;