Add sensor APIs related to batch event
[platform/core/system/sensord.git] / src / sensorctl / sensor_adapter.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_adapter.h"
21
22 #include <sensor_internal.h>
23
24 #include "log.h"
25 #include "mainloop.h"
26 #include "test_bench.h"
27
28 #define SENSOR_EVENT(type) ((type) << 16 | 0x1)
29
30 bool sensor_adapter::is_batch_mode = false;
31
32 bool sensor_adapter::is_supported(sensor_type_t type)
33 {
34         sensor_t sensor;
35
36         int ret = sensord_get_default_sensor(type, &sensor);
37         if (ret == 0)
38                 return true;
39
40         return false;
41 }
42
43 int sensor_adapter::get_count(sensor_type_t type)
44 {
45         sensor_t *sensors;
46         int count = 0;
47
48         if (sensord_get_sensors(type, &sensors, &count) == 0)
49                 free(sensors);
50
51         return count;
52 }
53
54 bool sensor_adapter::get_handle(sensor_info info, int &handle)
55 {
56         int err;
57         int count;
58         sensor_t *sensors;
59
60         err = sensord_get_sensors(info.type, &sensors, &count);
61         ASSERT_EQ(err, 0);
62
63         handle = sensord_connect(sensors[info.index]);
64         ASSERT_GE(handle, 0);
65
66         return true;
67 }
68
69 bool sensor_adapter::start(sensor_info info, int &handle)
70 {
71         sensor_t *sensors;
72         int count;
73         int err;
74         bool ret;
75
76         err = sensord_get_sensors(info.type, &sensors, &count);
77         ASSERT_EQ(err, 0);
78         ASSERT_LT(info.index, count);
79         ASSERT_GE(info.index, 0);
80
81         handle = sensord_connect(sensors[info.index]);
82         ASSERT_GE(handle, 0);
83
84         if (is_batch_mode) {
85                 ret = sensord_register_events(handle, SENSOR_EVENT(info.type), info.batch_latency, info.events_cb, NULL);
86         } else {
87                 ret = sensord_register_event(handle, SENSOR_EVENT(info.type), info.interval, info.batch_latency, info.cb, NULL);
88         }
89         ASSERT_TRUE(ret);
90
91         ret = sensord_start(handle, info.powersave);
92         ASSERT_TRUE(ret);
93
94         free(sensors);
95
96         return true;
97 }
98
99 bool sensor_adapter::stop(sensor_info info, int handle)
100 {
101         bool ret;
102
103         ret = sensord_stop(handle);
104         EXPECT_TRUE(ret);
105
106         if (is_batch_mode) {
107                 ret = sensord_unregister_events(handle, SENSOR_EVENT(info.type));
108         } else {
109                 ret = sensord_unregister_event(handle, SENSOR_EVENT(info.type));
110         }
111         EXPECT_TRUE(ret);
112
113         ret = sensord_disconnect(handle);
114         EXPECT_TRUE(ret);
115
116         return true;
117 }
118
119 bool sensor_adapter::change_interval(int handle, int interval)
120 {
121         return true;
122 }
123
124 bool sensor_adapter::change_batch_latency(int handle, int batch_latency)
125 {
126         return true;
127 }
128
129 bool sensor_adapter::change_powersave(int handle, int powersave)
130 {
131         return true;
132 }
133
134 bool sensor_adapter::set_attribute(int handle, int attribute, int value)
135 {
136         bool ret;
137
138         ret = sensord_set_attribute_int(handle, attribute, value);
139         ASSERT_TRUE(ret);
140
141         return true;
142 }
143
144 bool sensor_adapter::set_attribute(int handle, int attribute, char *value, int size)
145 {
146         int ret;
147
148         ret = sensord_set_attribute_str(handle, attribute, value, size);
149
150         return ((ret == 0) ? true : false);
151 }
152
153 bool sensor_adapter::get_data(int handle, sensor_type_t type, sensor_data_t &data)
154 {
155         bool ret;
156
157         ret = sensord_get_data(handle, SENSOR_EVENT(type), &data);
158         ASSERT_TRUE(ret);
159
160         return true;
161 }
162
163 bool sensor_adapter::flush(int handle)
164 {
165         bool ret;
166
167         ret = sensord_flush(handle);
168         ASSERT_TRUE(ret);
169
170         return true;
171 }
172