2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file Accelerometer.cpp
18 * @author Qi Xiangguo (xiangguo.qi@samsung.com)
24 #include <Accelerometer/EventGetCurrentAcceleration.h>
25 #include <Commons/Exception.h>
26 #include "Accelerometer.h"
30 namespace WrtDeviceApis {
31 namespace Accelerometer {
37 * Callback method called by platform as interval time.
38 * @param event_type Platform accelerometer evet type
39 * @param event Platform sensor event data
40 * @param this_ User data pointer.
42 static void accelerationChangedCallback(unsigned int event_type,
43 sensor_event_data_t *event,
46 LogDebug("Callback accelerationChangedCallback.");
48 if (event_type == ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME &&
50 ((Accelerometer::Watcher*)watcher_ptr)->accelerationHasChanged(
54 LogError("Callback private data is NULL.");
59 Accelerometer::Accelerometer() :
64 LogDebug("Platform::Accelerometer Enter");
67 Accelerometer::~Accelerometer()
69 LogDebug("Platform::~Accelerometer Enter");
71 if (m_initialized == true) {
75 LogDebug("All watchers : (" << m_watchers.size() << ")");
76 std::vector<WatcherPtr>::iterator it;
77 for (it = m_watchers.begin(); it != m_watchers.end(); ++it) {
78 stop((*it)->getHandle(), true);
81 LogDebug("All watchers were cleared. (" << m_watchers.size() << ")");
84 void Accelerometer::getCurrentAcceleration(
85 const EventGetCurrentAccelerationPtr& event)
87 if (m_initialized == false) {
88 m_handle = initialize();
92 EventRequestReceiver<EventGetCurrentAcceleration>::PostRequest(event);
95 void Accelerometer::OnRequestReceived(
96 const EventGetCurrentAccelerationPtr& event)
100 memset(&data, 0, sizeof(sensor_data_t));
102 *Invoke platform API sf_get_data gets raw data from a sensor with connecting the sensor-server.
103 * The type of sensor is supplied and return data is stored in the output parameter values [].
105 if ((sf_get_data(m_handle, ACCELEROMETER_BASE_DATA_SET, &data) < 0) ||
106 (data.values_num < 3)) {
107 LogError("Can't get current Acceleration. ");
108 Throw(Commons::PlatformException);
111 event->setXAxis(static_cast<double>(data.values[0]));
112 event->setYAxis(static_cast<double>(data.values[1]));
113 event->setZAxis(static_cast<double>(data.values[2]));
116 long Accelerometer::watchAcceleration(
117 const EventAccelerationChangedEmitterPtr& emitter,
118 long minNotificationInterval)
121 int new_handle = initialize();
123 event_condition_t* condition = NULL;
124 event_condition_t conditionData;
126 if (minNotificationInterval > 0) {
127 conditionData.cond_op = CONDITION_EQUAL;
128 conditionData.cond_value1 = minNotificationInterval;
129 condition = &conditionData;
133 * Invoke platform API sf_register_event registers a user defined callback function with a connected sensor for a particular event.
134 **This callback function will be called when there is a change in the state of respective sensor.
135 **cb_data will be the parameter used during the callback call.
136 **Callback interval can be adjusted using even_contion_t argument.
139 WatcherPtr watcher(new Watcher(new_handle, emitter));
141 int sensor_state = sf_register_event(
142 watcher->getHandle(),
143 ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME, condition,
144 accelerationChangedCallback, watcher.Get());
145 if (sensor_state < 0) {
146 stop(watcher->getHandle());
147 Throw(Commons::PlatformException);
150 start(watcher->getHandle());
153 m_watchers.push_back(watcher);
154 LogDebug("Watcher is added. (" << m_watchers.size() << ")");
155 watcher->getCurrentAccelerationForWatch();
157 return static_cast<long>(emitter->getId());
160 void Accelerometer::clearWatch(EventAccelerationChangedEmitter::IdType id)
164 std::vector<WatcherPtr>::iterator it;
165 for (it = m_watchers.begin(); it != m_watchers.end(); ++it) {
166 if (id == (*it)->getEmitter()->getId()) {
167 stop((*it)->getHandle(), true);
169 m_watchers.erase(it);
170 LogDebug("Watcher is removed. (" << m_watchers.size() << ")");
172 if (m_watchers.size() == 0) {
180 void Accelerometer::getCurrentAccelerationForWatch(void)
184 memset(&data, 0, sizeof(sensor_data_t));
185 if ((sf_get_data(m_handle, ACCELEROMETER_BASE_DATA_SET, &data) < 0) ||
186 (data.values_num < 3)) {
187 Throw(Commons::PlatformException);
190 EventAccelerationChangedPtr event(new EventAccelerationChanged());
191 AccelerationProperties props;
192 props.xAxis = static_cast<double>(data.values[0]);
193 props.yAxis = static_cast<double>(data.values[1]);
194 props.zAxis = static_cast<double>(data.values[2]);
196 event->setAccelerationProperties(props);
198 m_AccelerationEmitters.emit(event);
201 void Accelerometer::accelerationHasChanged(sensor_event_data_t *sensorEvent)
204 sensor_data_t *sensor_data = NULL;
205 sensor_data = (sensor_data_t *)sensorEvent->event_data;
206 if (sensor_data == NULL ||
207 (sensor_data->values_num < 3)) {
208 LogError("Can't get current Acceleration. ");
209 Throw(Commons::PlatformException);
213 EventAccelerationChangedPtr event(new EventAccelerationChanged());
214 AccelerationProperties props;
215 props.xAxis = static_cast<double>(sensor_data->values[0]);
216 props.yAxis = static_cast<double>(sensor_data->values[1]);
217 props.zAxis = static_cast<double>(sensor_data->values[2]);
219 "xAxis:" << sensor_data->values[0] << ",yAxis:" <<
220 sensor_data->values[1] << ",zAxis:" << sensor_data->values[2]);
222 event->setAccelerationProperties(props);
224 m_AccelerationEmitters.emit(event);
227 int Accelerometer::initialize()
230 DPL::Mutex::ScopedLock lock(&m_initializationMutex);
231 int handle = sf_connect(ACCELEROMETER_SENSOR);
234 LogError("Could not connect with accelerometer sensor.");
235 Throw(Commons::UnsupportedException);
241 void Accelerometer::restart(int& handle)
247 void Accelerometer::start(int handle)
250 if (sf_start(handle, 0) < 0) {
251 LogError("Could not start communication with sensor.");
253 sf_disconnect(handle);
254 Throw(Commons::UnsupportedException);
259 void Accelerometer::stop(int& handle,
260 bool isWatcherHandle)
265 if (isWatcherHandle == true) {
266 LogError("unregister event sensor.");
267 sf_unregister_event(handle,
268 ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME);
271 sf_disconnect(handle);
277 void Accelerometer::Watcher::getCurrentAccelerationForWatch()
281 memset(&data, 0, sizeof(sensor_data_t));
282 if ((sf_get_data(m_handle, ACCELEROMETER_BASE_DATA_SET, &data) < 0) ||
283 (data.values_num < 3)) {
284 Throw(Commons::PlatformException);
287 EventAccelerationChangedPtr event(new EventAccelerationChanged());
288 AccelerationProperties props;
289 props.xAxis = static_cast<double>(data.values[0]);
290 props.yAxis = static_cast<double>(data.values[1]);
291 props.zAxis = static_cast<double>(data.values[2]);
293 event->setAccelerationProperties(props);
299 void Accelerometer::Watcher::accelerationHasChanged(
300 sensor_event_data_t *sensorEvent)
303 sensor_data_t *sensor_data = NULL;
304 sensor_data = (sensor_data_t *)sensorEvent->event_data;
305 if (sensor_data == NULL ||
306 (sensor_data->values_num < 3)) {
307 LogError("Can't get current Acceleration. ");
308 Throw(Commons::PlatformException);
312 EventAccelerationChangedPtr event(new EventAccelerationChanged());
313 AccelerationProperties props;
314 props.xAxis = static_cast<double>(sensor_data->values[0]);
315 props.yAxis = static_cast<double>(sensor_data->values[1]);
316 props.zAxis = static_cast<double>(sensor_data->values[2]);
318 "xAxis:" << sensor_data->values[0] << ",yAxis:" <<
319 sensor_data->values[1] << ",zAxis:" << sensor_data->values[2]);
321 event->setAccelerationProperties(props);