remove gesture code which is deprecated
[platform/core/api/motion.git] / src / ActivitySensor.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <cmath>
19 #include "ActivitySensor.h"
20
21 #define ACTIVITY_BIT(X) (0x1 << (int)(X))
22 #define ACTIVITY_ENUM(X) ((int)log2f((X)))
23
24 using namespace motion;
25
26 class ActivityListener : public ISensorListener {
27 public:
28         ActivityListener()
29         {
30         }
31
32         ~ActivityListener()
33         {
34         }
35         //LCOV_EXCL_START
36         void onEvent(double timestamp, float* values, int accuracy)
37         {
38                 ActivitySensor::onEvent(timestamp, values, accuracy);
39         }
40         //LCOV_EXCL_STOP
41 };
42
43 static ActivityListener __activityListener;
44
45 std::set<ActivitySensor*> ActivitySensor::__sensorSet;
46 SensorAdapter ActivitySensor::__sensorAdapter(&__activityListener);
47
48 //LCOV_EXCL_START
49 ActivitySensor::ActivitySensor() :
50         __activityType(static_cast<activity_type_e>(UNDEFINED)),
51         __callback(NULL),
52         __userData(NULL),
53         __started(false)
54 {
55         __sensorAdapter.setSensor(ACTIVITY_TRACKER_SENSOR);
56         __sensorAdapter.setPowerSave(false);
57         __sensorSet.insert(this);
58 }
59
60 ActivitySensor::~ActivitySensor()
61 {
62         stop();
63         __sensorSet.erase(this);
64 }
65
66 bool ActivitySensor::setActivity(activity_type_e type)
67 {
68         __activityType = type;
69         return true;
70 }
71
72 void ActivitySensor::setCallback(activity_recognition_cb cb)
73 {
74         __callback = cb;
75 }
76
77 void ActivitySensor::setUserData(void* data)
78 {
79         __userData = data;
80 }
81
82 bool ActivitySensor::start()
83 {
84         bool started = false;
85         int activityFilter = 0;
86
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);
91         }
92
93         if (!started)
94                 IF_FAIL_RETURN(__sensorAdapter.start(), false);
95
96         __sensorAdapter.setAttribute(SENSOR_ATTR_ACTIVITY, activityFilter);
97
98         __started = true;
99         return true;
100 }
101
102 bool ActivitySensor::stop()
103 {
104         IF_FAIL_RETURN(__started, false);
105
106         bool started = false;
107         int activityFilter = 0;
108
109         __started = false;
110
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);
115         }
116
117         if (started)
118                 __sensorAdapter.setAttribute(SENSOR_ATTR_ACTIVITY, activityFilter);
119         else
120                 __sensorAdapter.stop();
121
122         return true;
123 }
124
125 bool ActivitySensor::isSupported(activity_type_e type)
126 {
127         return SensorAdapter::isSupported(ACTIVITY_TRACKER_SENSOR);
128 }
129
130 void ActivitySensor::onEvent(double timestamp, float* values, int accuracy)
131 {
132         _activity_data_s data;
133         data.activity = ACTIVITY_ENUM(values[0]);
134         data.accuracy = accuracy;
135
136         for (std::set<ActivitySensor*>::iterator iter = __sensorSet.begin(); iter != __sensorSet.end(); ++iter) {
137                 if (!(*iter)->__started)
138                         continue;
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);
142                 }
143         }
144 }
145 //LCOV_EXCL_STOP