coverity issues fix
[platform/core/system/sensord.git] / src / sensor / gesture / face_down_alg_impl.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <cmath>
20 #include <climits>
21 #include <sensor_log.h>
22 #include <face_down_alg_impl.h>
23
24 #define GRAVITY 9.80665
25 #define TWENTY_DEGREES 0.349066
26 #define ONE_SIXTY_DEGREES 2.79253
27 #define WINDOW_SIZE (2000*1000)
28
29 face_down_alg_impl::face_down_alg_impl()
30 {
31         m_current_time = 0;
32         m_last_event_time = 0;
33         m_latest_down_time = 0;
34 }
35
36 face_down_alg_impl::~face_down_alg_impl()
37 {
38 }
39
40 void face_down_alg_impl::push_event(const sensor_event_t & event)
41 {
42         //_I("face_down_alg: %llu acc[2]: %f",event.data->timestamp,event.data->values[2]);
43         m_current_time = event.data->timestamp;
44         remove_old_up_time();
45
46         if (event.data->values[2] < (GRAVITY * cos(ONE_SIXTY_DEGREES)))
47                 m_latest_down_time = event.data->timestamp;
48
49         if (event.data->values[2] > (GRAVITY * cos(TWENTY_DEGREES)))
50                 m_oldest_up_time.push(event.data->timestamp);
51 }
52
53 void face_down_alg_impl::remove_old_up_time(void)
54 {
55         while (m_oldest_up_time.size() > 0 && (m_current_time - m_oldest_up_time.front() > WINDOW_SIZE))
56                 m_oldest_up_time.pop();
57 }
58
59 bool face_down_alg_impl::get_face_down(void)
60 {
61         unsigned long long down = is_facing_down();
62         unsigned long long up = was_facing_up();
63         //_I("face_down_alg: down: %llu, up: %llu", down, up);
64
65         if (down < up)
66                 return false;
67
68         if (m_current_time - m_last_event_time < WINDOW_SIZE)
69                 return false;
70
71         m_last_event_time = m_current_time;
72         return true;
73 }
74
75 unsigned long long face_down_alg_impl::is_facing_down(void)
76 {
77         if (m_current_time - m_latest_down_time < WINDOW_SIZE)
78                 return m_latest_down_time;
79         return 0;
80 }
81
82 unsigned long long face_down_alg_impl::was_facing_up(void)
83 {
84         remove_old_up_time();
85         if (m_oldest_up_time.size() == 0)
86                 return ULLONG_MAX;
87         return m_oldest_up_time.front();
88 }