Updating test files to support the tilt sensor
[platform/core/system/sensord.git] / test / src / check-sensor.c
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014-15 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 <stdbool.h>
25 #include <sensor_common.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 #include "check-sensor.h"
30
31 static GMainLoop *mainloop;
32
33 void printpollinglogs(sensor_type_t type,sensor_data_t data)
34 {
35         switch(type) {
36         case(ACCELEROMETER_SENSOR):
37                 printf("Accelerometer [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
38                 break;
39         case(GYROSCOPE_SENSOR):
40                 printf("Gyroscope [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
41                 break;
42         case(PRESSURE_SENSOR):
43                 printf("Pressure [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
44                 break;
45         case(GEOMAGNETIC_SENSOR):
46                 printf("Geomagnetic [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
47                 break;
48         case(LIGHT_SENSOR):
49                 printf("Light [%lld] [%6.6f]\n\n", data.timestamp, data.values[0]);
50                 break;
51         case(TEMPERATURE_SENSOR):
52                 printf("Temperature [%lld] [%6.6f]\n\n", data.timestamp, data.values[0]);
53                 break;
54         case(PROXIMITY_SENSOR):
55                 printf("Proximity [%lld] [%6.6f]\n\n", data.timestamp, data.values[0]);
56                 break;
57         case(ORIENTATION_SENSOR):
58                 printf("Orientation [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
59                 break;
60         case(TILT_SENSOR):
61                 printf("Tilt [%lld] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1]);
62                 break;
63         case(GRAVITY_SENSOR):
64                 printf("Gravity [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
65                 break;
66         case(LINEAR_ACCEL_SENSOR):
67                 printf("Linear Acceleration [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
68                 break;
69         case(ROTATION_VECTOR_SENSOR):
70                 printf("Rotation Vector [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2], data.values[3]);
71                 break;
72         case(GEOMAGNETIC_RV_SENSOR):
73                 printf("Geomagnetic Rv [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2], data.values[3]);
74                 break;
75         case(GAMING_RV_SENSOR):
76                 printf("Gaming Rv [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2], data.values[3]);
77                 break;
78         default:
79                 return;
80         }
81 }
82
83 int get_event(sensor_type_t sensor_type, char str[])
84 {
85         switch (sensor_type) {
86         case ACCELEROMETER_SENSOR:
87                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
88                         return ACCELEROMETER_RAW_DATA_EVENT;
89                 break;
90         case GYROSCOPE_SENSOR:
91                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
92                         return GYROSCOPE_RAW_DATA_EVENT;
93                 break;
94         case PRESSURE_SENSOR:
95                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
96                         return PRESSURE_RAW_DATA_EVENT;
97                 break;
98         case GEOMAGNETIC_SENSOR:
99                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
100                         return GEOMAGNETIC_RAW_DATA_EVENT;
101                 break;
102         case LIGHT_SENSOR:
103                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
104                         return LIGHT_LUX_DATA_EVENT;
105                 break;
106         case TEMPERATURE_SENSOR:
107                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
108                         return TEMPERATURE_RAW_DATA_EVENT;
109                 break;
110         case PROXIMITY_SENSOR:
111                 if (strcmp(str, "CHANGE_STATE_EVENT") == 0)
112                         return PROXIMITY_CHANGE_STATE_EVENT;
113                 break;
114         case ORIENTATION_SENSOR:
115                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
116                         return ORIENTATION_RAW_DATA_EVENT;
117                 break;
118         case TILT_SENSOR:
119                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
120                         return TILT_RAW_DATA_EVENT;
121                 break;
122         case GRAVITY_SENSOR:
123                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
124                         return GRAVITY_RAW_DATA_EVENT;
125                 break;
126         case LINEAR_ACCEL_SENSOR:
127                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
128                         return LINEAR_ACCEL_RAW_DATA_EVENT;
129                 break;
130         case ROTATION_VECTOR_SENSOR:
131                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
132                         return ROTATION_VECTOR_RAW_DATA_EVENT;
133                 break;
134         case GEOMAGNETIC_RV_SENSOR:
135                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
136                         return GEOMAGNETIC_RV_RAW_DATA_EVENT;
137                 break;
138         case GAMING_RV_SENSOR:
139                 if (strcmp(str, "RAW_DATA_EVENT") == 0)
140                         return GAMING_RV_RAW_DATA_EVENT;
141                 break;
142         }
143         return -1;
144 }
145
146 void callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
147 {
148         sensor_type_t sensor_type = event_type >> 16;
149
150         switch (sensor_type) {
151         case ACCELEROMETER_SENSOR:
152                 printf("Accelerometer [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
153                 break;
154         case GYROSCOPE_SENSOR:
155                 printf("Gyroscope [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
156                 break;
157         case PRESSURE_SENSOR:
158                 printf("Pressure [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
159                 break;
160         case GEOMAGNETIC_SENSOR:
161                 printf("Geomagnetic [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
162                 break;
163         case LIGHT_SENSOR:
164                 printf("Light [%lld] [%6.6f]\n", data->timestamp, data->values[0]);
165                 break;
166         case TEMPERATURE_SENSOR :
167                 printf("Temperature [%lld] [%6.6f]\n", data->timestamp, data->values[0]);
168                 break;
169         case PROXIMITY_SENSOR:
170                 printf("Proximity [%lld] [%6.6f]\n", data->timestamp, data->values[0]);
171                 break;
172         case ORIENTATION_SENSOR :
173                 printf("Orientation [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
174                 break;
175         case TILT_SENSOR :
176                 printf("Tilt [%lld] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1]);
177                 break;
178         case GRAVITY_SENSOR:
179                 printf("Gravity [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
180                 break;
181         case LINEAR_ACCEL_SENSOR:
182                 printf("Linear acceleration [%lld] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
183                 break;
184         case ROTATION_VECTOR_SENSOR:
185                 printf("Rotation vector [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2], data->values[3]);
186                 break;
187         case GEOMAGNETIC_RV_SENSOR:
188                 printf("Geomagnetic RV [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2], data->values[3]);
189                 break;
190         case GAMING_RV_SENSOR:
191                 printf("Gaming RV [%lld] [%6.6f] [%6.6f] [%6.6f] [%6.6f]\n", data->timestamp, data->values[0], data->values[1], data->values[2], data->values[3]);
192                 break;
193         default:
194                 return;
195         }
196 }
197
198 int check_sensor(sensor_type_t sensor_type, unsigned int event, int interval)
199 {
200         int handle, result, start_handle, stop_handle;
201
202         mainloop = g_main_loop_new(NULL, FALSE);
203
204         sensor_t sensor = sensord_get_sensor(sensor_type);
205         handle = sensord_connect(sensor);
206
207         result = sensord_register_event(handle, event, interval, 0, callback, NULL);
208
209         if (result < 0) {
210                 printf("Can't register sensor\n");
211                 return -1;
212         }
213
214         start_handle = sensord_start(handle, 0);
215
216         if (start_handle < 0) {
217                 printf("Error\n\n\n\n");
218                 sensord_unregister_event(handle, event);
219                 sensord_disconnect(handle);
220                 return -1;
221         }
222
223         g_main_loop_run(mainloop);
224         g_main_loop_unref(mainloop);
225
226         result = sensord_unregister_event(handle, event);
227
228         if (result < 0) {
229                 printf("Error\n\n");
230                 return -1;
231         }
232
233         stop_handle = sensord_stop(handle);
234
235         if (stop_handle < 0) {
236                 printf("Error\n\n");
237                 return -1;
238         }
239
240         sensord_disconnect(handle);
241
242         return 0;
243 }
244
245 int polling_sensor(sensor_type_t sensor_type, unsigned int event)
246 {
247         int result, handle;
248         printf("Polling based\n");
249
250         handle = sf_connect(sensor_type);
251         result = sf_start(handle, 1);
252
253         if (result < 0) {
254                 printf("Can't start the sensor\n");
255                 printf("Error\n\n\n\n");
256                 return -1;
257         }
258
259         sensor_data_t data;
260
261         while(1) {
262                 result = sf_get_data(handle, event , &data);
263                 printpollinglogs(sensor_type, data);
264                 usleep(100000);
265         }
266
267         result = sf_disconnect(handle);
268
269         if (result < 0) {
270                 printf("Can't disconnect sensor\n");
271                 printf("Error\n\n\n\n");
272                 return -1;
273         }
274
275         return 0;
276 }