sensord: restructuring sensord directories
[platform/core/system/sensord.git] / src / test / src / api-test.c
1 /*
2  * sensord
3  *
4  * Copyright (c) 2015 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 <time.h>
20 #include <glib.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <sensor_internal.h>
24 #include <sensor_common.h>
25 #include <stdbool.h>
26 #include <sensor_common.h>
27 #include <unistd.h>
28
29 #define DEFAULT_EVENT_INTERVAL 100
30
31 static GMainLoop *mainloop;
32 FILE *fp;
33
34 void callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
35 {
36         g_main_loop_quit(mainloop);
37 }
38
39 bool check_sensor_api(unsigned int event_type, int cond_value)
40 {
41         int handle;
42
43         mainloop = g_main_loop_new(NULL, FALSE);
44
45         sensor_type_t sensor_type = event_type >> 16;
46         sensor_t sensor = sensord_get_sensor(sensor_type);
47
48         handle = sensord_connect(sensor);
49
50         if (handle < 0) {
51                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_connect\n", sensor_type, event_type);
52                 return false;
53         }
54
55         bool is_supported;
56         bool result_boolean = sensord_is_supported_event_type(sensor, event_type, &is_supported);
57         if (!result_boolean && !is_supported) {
58                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_is_supported_event\n", sensor_type, event_type);
59                 return false;
60         }
61
62         int output;
63         result_boolean = sensord_get_min_interval(sensor, &output);
64         if (!result_boolean) {
65                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_min_interval\n", sensor_type, event_type);
66                 return false;
67         }
68
69         float output3;
70         result_boolean = sensord_get_resolution(sensor, &output3);
71         if (!result_boolean) {
72                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_resolution\n", sensor_type, event_type);
73                 return false;
74         }
75
76         result_boolean = sensord_get_max_range(sensor, &output3);
77         if (!result_boolean) {
78                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_max_range\n", sensor_type, event_type);
79                 return false;
80         }
81
82         result_boolean = sensord_get_min_range(sensor, &output3);
83         if (!result_boolean) {
84                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_min_range\n", sensor_type, event_type);
85                 return false;
86         }
87
88         sensor_privilege_t output4;
89         result_boolean = sensord_get_privilege(sensor, &output4);
90         if (!result_boolean) {
91                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_privilege\n", sensor_type, event_type);
92                 return false;
93         }
94
95         const char* result_char = sensord_get_vendor(sensor);
96         if (!result_char) {
97                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_vendor\n", sensor_type, event_type);
98                 return false;
99         }
100
101         result_char = sensord_get_name(sensor);
102         if (!result_char) {
103                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_name\n", sensor_type, event_type);
104                 return false;
105         }
106
107         sensor_type_t output_type;
108         result_boolean = sensord_get_type(sensor, &output_type);
109         if (!result_boolean) {
110                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_type\n", sensor_type, event_type);
111                 return false;
112         }
113
114         unsigned int *output2;
115         result_boolean = sensord_get_supported_event_types(sensor, &output2, &output);
116         if (!result_boolean) {
117                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_supported_event_types\n", sensor_type, event_type);
118                 return false;
119         }
120
121         sensor_t *output_list;
122         result_boolean = sensord_get_sensor_list(sensor_type, &output_list, &output);
123         if (!result_boolean) {
124                 free(output2);
125                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_sensor_list\n", sensor_type, event_type);
126                 return false;
127         }
128
129         result_boolean = sensord_register_event(handle, event_type, cond_value, 0, callback, NULL);
130         if (!result_boolean) {
131                 free(output2);
132                 free(output_list);
133                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_register_event\n", sensor_type, event_type);
134                 return false;
135         }
136
137         result_boolean = sensord_start(handle, 1);
138         if (!result_boolean) {
139                 sensord_unregister_event(handle, event_type);
140                 sensord_disconnect(handle);
141                 free(output2);
142                 free(output_list);
143                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_start\n", sensor_type, event_type);
144                 return false;
145         }
146
147         sensor_data_t data;
148         result_boolean = sensord_get_data(handle, event_type, &data);
149         if (!result_boolean) {
150                 sensord_unregister_event(handle, event_type);
151                 sensord_disconnect(handle);
152                 free(output2);
153                 free(output_list);
154                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_get_data\n", sensor_type, event_type);
155                 return false;
156         }
157
158         g_main_loop_run(mainloop);
159         g_main_loop_unref(mainloop);
160
161         result_boolean = sensord_change_event_interval(handle, event_type, 101);
162         if (!result_boolean) {
163                 free(output2);
164                 free(output_list);
165                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_change_event_interval\n", sensor_type, event_type);
166                 return false;
167         }
168
169         result_boolean = sensord_set_option(handle, SENSOR_OPTION_ON_IN_SCREEN_OFF);
170         if (!result_boolean){
171                 free(output2);
172                 free(output_list);
173                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_change_sensor_option\n", sensor_type, event_type);
174                 return false;
175         }
176
177         result_boolean = sensord_unregister_event(handle, event_type);
178         if (!result_boolean) {
179                 free(output2);
180                 free(output_list);
181                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_unregister_event\n", sensor_type, event_type);
182                 return false;
183         }
184
185         result_boolean = sensord_stop(handle);
186         if (!result_boolean) {
187                 free(output2);
188                 free(output_list);
189                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_stop\n", sensor_type, event_type);
190                 return false;
191         }
192
193         result_boolean = sensord_disconnect(handle);
194         if (!result_boolean) {
195                 free(output2);
196                 free(output_list);
197                 fprintf(fp, "Sensor - %d, event - %d, failed at sensord_disconnect\n", sensor_type, event_type);
198                 return false;
199         }
200
201         free(output2);
202         free(output_list);
203
204         return true;
205 }
206
207 int main(int argc, char **argv)
208 {
209         bool result;
210
211         int interval = DEFAULT_EVENT_INTERVAL;
212         if (argc == 2)
213                 interval = atof(argv[1]);
214
215         fp = fopen("auto_test.output", "w+");
216
217         result = check_sensor_api(ACCELEROMETER_RAW_DATA_EVENT, interval);
218         fprintf(fp, "Accelerometer - RAW_DATA_REPORT_ON_TIME - %d\n", result);
219
220         result = check_sensor_api(GEOMAGNETIC_RAW_DATA_EVENT, interval);
221         fprintf(fp, "Geomagnetic - RAW_DATA_REPORT_ON_TIME - %d\n", result);
222
223         result = check_sensor_api(GRAVITY_RAW_DATA_EVENT, interval);
224         fprintf(fp, "Gravity - RAW_DATA_REPORT_ON_TIME - %d\n", result);
225
226         result = check_sensor_api(GYROSCOPE_RAW_DATA_EVENT, interval);
227         fprintf(fp, "Gyroscope - RAW_DATA_REPORT_ON_TIME - %d\n", result);
228
229         result = check_sensor_api(LIGHT_LUX_DATA_EVENT, interval);
230         fprintf(fp, "Light - RAW_DATA_REPORT_ON_TIME - %d\n", result);
231
232         result = check_sensor_api(LINEAR_ACCEL_RAW_DATA_EVENT, interval);
233         fprintf(fp, "Linear Accel - RAW_DATA_REPORT_ON_TIME - %d\n", result);
234
235         result = check_sensor_api(ORIENTATION_RAW_DATA_EVENT, interval);
236         fprintf(fp, "Orientation - RAW_DATA_REPORT_ON_TIME - %d\n", result);
237
238         result = check_sensor_api(TILT_RAW_DATA_EVENT, interval);
239         fprintf(fp, "Tilt - RAW_DATA_REPORT_ON_TIME - %d\n", result);
240
241         result = check_sensor_api(PRESSURE_RAW_DATA_EVENT, interval);
242         fprintf(fp, "Pressure - RAW_DATA_REPORT_ON_TIME - %d\n", result);
243
244         result = check_sensor_api(ROTATION_VECTOR_RAW_DATA_EVENT, interval);
245         fprintf(fp, "Rotation Vector - RAW_DATA_REPORT_ON_TIME - %d\n", result);
246
247         result = check_sensor_api(GEOMAGNETIC_RV_RAW_DATA_EVENT, interval);
248         fprintf(fp, "Geomagnetic Rotation Vector - RAW_DATA_REPORT_ON_TIME - %d\n", result);
249
250         result = check_sensor_api(GAMING_RV_RAW_DATA_EVENT, interval);
251         fprintf(fp, "Gaming Rotation Vector - RAW_DATA_REPORT_ON_TIME - %d\n", result);
252
253         result = check_sensor_api(GYROSCOPE_UNCAL_SENSOR, interval);
254         fprintf(fp, "Gyroscope Uncal Sensor - RAW_DATA_REPORT_ON_TIME - %d\n", result);
255
256         result = check_sensor_api(TEMPERATURE_RAW_DATA_EVENT, interval);
257         fprintf(fp, "Temperature - RAW_DATA_REPORT_ON_TIME - %d\n", result);
258
259         result = check_sensor_api(ULTRAVIOLET_RAW_DATA_EVENT, interval);
260         fprintf(fp, "ULTRAVIOLET - RAW_DATA_REPORT_ON_TIME - %d\n", result);
261
262         result = check_sensor_api(BIO_LED_RED_RAW_DATA_EVENT, interval);
263         fprintf(fp, "BIO_LED_RED - RAW_DATA_REPORT_ON_TIME - %d\n", result);
264
265         printf("Logs printed in ./auto_test.output\n");
266         fclose(fp);
267         return 0;
268 }