2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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.
19 #include "ActivitySensor.h"
21 #define ACTIVITY_BIT(X) (0x1 << (int)(X))
22 #define ACTIVITY_ENUM(X) ((int)log2f((X)))
24 using namespace motion;
26 class ActivityListener : public ISensorListener {
36 void onEvent(double timestamp, float* values, int accuracy)
38 ActivitySensor::onEvent(timestamp, values, accuracy);
43 static ActivityListener __activityListener;
45 std::set<ActivitySensor*> ActivitySensor::__sensorSet;
46 SensorAdapter ActivitySensor::__sensorAdapter(&__activityListener);
49 ActivitySensor::ActivitySensor() :
50 __activityType(static_cast<activity_type_e>(UNDEFINED)),
55 __sensorAdapter.setSensor(ACTIVITY_TRACKER_SENSOR);
56 __sensorAdapter.setPowerSave(false);
57 __sensorSet.insert(this);
60 ActivitySensor::~ActivitySensor()
63 __sensorSet.erase(this);
66 bool ActivitySensor::setActivity(activity_type_e type)
68 __activityType = type;
72 void ActivitySensor::setCallback(activity_recognition_cb cb)
77 void ActivitySensor::setUserData(void* data)
82 bool ActivitySensor::start()
85 int activityFilter = 0;
87 for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
88 started = started | (*iter)->__started;
89 if ((*iter)->__started || (*iter) == this)
90 activityFilter |= ACTIVITY_BIT((*iter)->__activityType);
94 IF_FAIL_RETURN(__sensorAdapter.start(), false);
96 __sensorAdapter.setAttribute(SENSOR_ATTR_ACTIVITY, activityFilter);
102 bool ActivitySensor::stop()
104 IF_FAIL_RETURN(__started, false);
106 bool started = false;
107 int activityFilter = 0;
111 for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
112 started = started | (*iter)->__started;
113 if ((*iter)->__started)
114 activityFilter |= ACTIVITY_BIT((*iter)->__activityType);
118 __sensorAdapter.setAttribute(SENSOR_ATTR_ACTIVITY, activityFilter);
120 __sensorAdapter.stop();
125 bool ActivitySensor::isSupported(activity_type_e type)
127 return SensorAdapter::isSupported(ACTIVITY_TRACKER_SENSOR);
130 void ActivitySensor::onEvent(double timestamp, float* values, int accuracy)
132 _activity_data_s data;
133 data.activity = ACTIVITY_ENUM(values[0]);
134 data.accuracy = accuracy;
136 for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
137 if (!(*iter)->__started)
139 if (static_cast<int>((*iter)->__activityType) == data.activity && (*iter)->__callback) {
140 activity_recognition_cb cb = (*iter)->__callback;
141 cb((*iter)->__activityType, &data, timestamp, ACTIVITY_ERROR_NONE, (*iter)->__userData);