ed526c3d672ab68649eae203dc5e462fcfe4934e
[platform/core/system/sensord.git] / src / sensorctl / testcase / accelerometer.cpp
1 /*
2  * sensorctl
3  *
4  * Copyright (c) 2017 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
20 #include <sensor_internal.h>
21
22 #include "log.h"
23 #include "mainloop.h"
24 #include "test_bench.h"
25 #include "sensor_adapter.h"
26
27 static void basic_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
28 {
29         EXPECT_GT(data->timestamp, 0);
30         EXPECT_NEAR(data->values[0], 0, 19.6);
31         EXPECT_NEAR(data->values[1], 0, 19.6);
32         EXPECT_NEAR(data->values[2], 0, 19.6);
33
34         mainloop::stop();
35 }
36
37 TESTCASE(accelerometer_basic, start_stop_p)
38 {
39         bool ret;
40         int handle;
41
42         sensor_info info(ACCELEROMETER_SENSOR, 0,
43                         100, 1000, SENSOR_OPTION_ALWAYS_ON, basic_cb, NULL);
44
45         ret = sensor_adapter::start(info, handle);
46         ASSERT_TRUE(ret);
47
48         mainloop::run();
49
50         ret = sensor_adapter::stop(info, handle);
51         ASSERT_TRUE(ret);
52
53         return true;
54 }
55
56 static void get_data_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
57 {
58         mainloop::stop();
59 }
60
61 TESTCASE(accelerometer_basic, get_data_p)
62 {
63         bool ret;
64         int handle;
65         sensor_data_t data;
66
67         sensor_info info(ACCELEROMETER_SENSOR, 0,
68                         100, 1000, SENSOR_OPTION_ALWAYS_ON, get_data_cb, NULL);
69
70         ret = sensor_adapter::start(info, handle);
71         ASSERT_TRUE(ret);
72
73         mainloop::run();
74
75         ret = sensor_adapter::get_data(handle, info.type, data);
76         ASSERT_TRUE(ret);
77
78         ret = sensor_adapter::stop(info, handle);
79         ASSERT_TRUE(ret);
80
81         return true;
82 }
83
84 static unsigned long long prev_prev_ts;
85 static unsigned long long prev_ts;
86 static int event_count;
87
88 static void accel_regular_interval_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
89 {
90         int prev_gap;
91         int current_gap;
92         if (prev_prev_ts == 0) {
93                 prev_prev_ts = data->timestamp;
94                 return;
95         }
96
97         if (prev_ts == 0) {
98                 prev_ts = data->timestamp;
99                 return;
100         }
101
102         prev_gap = prev_ts - prev_prev_ts;
103         current_gap = data->timestamp - prev_ts;
104
105         EXPECT_NEAR(current_gap, prev_gap, 10000);
106         prev_prev_ts = prev_ts;
107         prev_ts = data->timestamp;
108
109         if (event_count++ > 3)
110                 mainloop::stop();
111 }
112
113 TESTCASE(accelerometer_interval, regular_interval_p)
114 {
115         bool ret;
116         int handle;
117         prev_prev_ts = 0;
118         prev_ts = 0;
119         event_count = 0;
120
121         sensor_info info(ACCELEROMETER_SENSOR, 0,
122                         100, 1000, SENSOR_OPTION_ALWAYS_ON, accel_regular_interval_cb, NULL);
123
124         ret = sensor_adapter::start(info, handle);
125         ASSERT_TRUE(ret);
126
127         mainloop::run();
128
129         ret = sensor_adapter::stop(info, handle);
130         ASSERT_TRUE(ret);
131
132         return true;
133 }
134
135 static void accel_interval_100ms_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
136 {
137         if (prev_ts == 0) {
138                 prev_ts = data->timestamp;
139                 return;
140         }
141
142         /* 100ms + 20ms(error) */
143         EXPECT_LE(data->timestamp - prev_ts, 120000);
144         prev_ts = data->timestamp;
145
146         if (event_count++ > 3)
147                 mainloop::stop();
148 }
149
150 TESTCASE(accelerometer_interval, 100ms_interval_p)
151 {
152         bool ret;
153         int handle;
154
155         prev_ts = 0;
156         event_count = 0;
157
158         sensor_info info(ACCELEROMETER_SENSOR, 0,
159                         100, 1000, SENSOR_OPTION_ALWAYS_ON, accel_interval_100ms_cb, NULL);
160
161         ret = sensor_adapter::start(info, handle);
162         ASSERT_TRUE(ret);
163
164         mainloop::run();
165
166         ret = sensor_adapter::stop(info, handle);
167         ASSERT_TRUE(ret);
168
169         return true;
170 }