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