Common test folder and automated testing of sensor APIs - Feature merge from devel...
[platform/core/system/sensord.git] / test / src / tc-common.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 #define DEFAULT_EVENT_INTERVAL 100
30
31 static GMainLoop *mainloop;
32
33 void usage()
34 {
35         printf("Usage : ./tc-common <Sensor_type> <event>(optional) <interval>(optional)\n\n");
36
37         printf("Sensor_type: ");
38         printf("[accelerometer] ");
39         printf("[gyroscope] ");
40         printf("[pressure] ");
41         printf("[temperature] ");
42         printf("[geomagnetic] ");
43         printf("[orientation] ");
44         printf("[gravity] ");
45         printf("[linear_accel] ");
46         printf("[rotation_vector] ");
47         printf("[gaming_rotation_vector] ");
48         printf("[light]\n");
49         printf("event:");
50         printf("[RAW_DATA_REPORT_ON_TIME]\n");
51
52         printf("Sensor_type: ");
53         printf("[proximity]\n");
54         printf("event:");
55         printf("[EVENT_CHANGE_STATE]\n");
56
57         printf("interval:\n");
58         printf("The time interval should be entered based on the sampling frequency supported by accelerometer driver on the device in ms.If no value for sensor is entered default value by the driver will be used.\n");
59 }
60
61 unsigned int get_event_driven(sensor_type_t sensor_type, char str[])
62 {
63         switch (sensor_type) {
64         case ACCELEROMETER_SENSOR:
65                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
66                         return ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME;
67                 break;
68         case GYROSCOPE_SENSOR:
69                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
70                         return GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME;
71                 break;
72         case PRESSURE_SENSOR:
73                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
74                         return PRESSURE_EVENT_RAW_DATA_REPORT_ON_TIME;
75                 break;
76         case GEOMAGNETIC_SENSOR:
77                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
78                         return GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME;
79                 break;
80         case LIGHT_SENSOR:
81                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
82                         return LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME;
83                 break;
84         case TEMPERATURE_SENSOR:
85                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
86                         return TEMPERATURE_EVENT_RAW_DATA_REPORT_ON_TIME;
87                 break;
88         case PROXIMITY_SENSOR:
89                 if (strcmp(str, "EVENT_CHANGE_STATE") == 0)
90                         return PROXIMITY_EVENT_CHANGE_STATE;
91                 break;
92         case ORIENTATION_SENSOR:
93                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
94                         return ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME;
95                 break;
96         case GRAVITY_SENSOR:
97                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
98                         return GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME;
99                 break;
100         case LINEAR_ACCEL_SENSOR:
101                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
102                         return LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME;
103                 break;
104         case ROTATION_VECTOR_SENSOR:
105                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
106                         return ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME;
107                 break;
108         case GEOMAGNETIC_RV_SENSOR:
109                 if (strcmp(str, "RAW_DATA_REPORT_ON_TIME") == 0)
110                         return GEOMAGNETIC_RV_EVENT_RAW_DATA_REPORT_ON_TIME;
111                 break;
112         default:
113                 return -1;
114         }
115 }
116
117 void callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
118 {
119         sensor_type_t sensor_type = event_type >> 16;
120
121         switch (sensor_type) {
122         case ACCELEROMETER_SENSOR:
123                 printf("Accelerometer [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
124                 break;
125         case GYROSCOPE_SENSOR:
126                 printf("Gyroscope [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
127                 break;
128         case PRESSURE_SENSOR:
129                 printf("Pressure [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
130                 break;
131         case GEOMAGNETIC_SENSOR:
132                 printf("Geomagnetic [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
133                 break;
134         case LIGHT_SENSOR:
135                 printf("Light [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
136                 break;
137         case TEMPERATURE_SENSOR :
138                 printf("Temperature [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
139                 break;
140         case PROXIMITY_SENSOR:
141                 printf("Proximity [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
142                 break;
143         case ORIENTATION_SENSOR :
144                 printf("Orientation [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
145                 break;
146         case GRAVITY_SENSOR:
147                 printf("Gravity [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
148                 break;
149         case LINEAR_ACCEL_SENSOR:
150                 printf("Linear acceleration [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0], data->values[1], data->values[2]);
151                 break;
152         case ROTATION_VECTOR_SENSOR:
153                 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]);
154                 break;
155         case GEOMAGNETIC_RV_SENSOR:
156                 printf("Geomagnetic 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]);
157                 break;
158         default:
159                 return;
160         }
161 }
162
163 int main(int argc, char **argv)
164 {
165         int result, handle, start_handle, stop_handle, interval;
166         char *end1, *end2;
167         unsigned int event;
168         bool EVENT_NOT_ENTERED = TRUE;
169         sensor_type_t sensor_type;
170         mainloop = g_main_loop_new(NULL, FALSE);
171
172         if (argc < 2 || argc > 4) {
173                 printf("Wrong number of arguments\n");
174                 usage();
175                 return 0;
176         }
177
178         if (strcmp(argv[1], "accelerometer") == 0) {
179                  sensor_type = ACCELEROMETER_SENSOR;
180                  event = ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME;
181         }
182         else if (strcmp(argv[1], "gyroscope") == 0) {
183                  sensor_type = GYROSCOPE_SENSOR;
184                  event = GYROSCOPE_EVENT_RAW_DATA_REPORT_ON_TIME;
185         }
186         else if (strcmp(argv[1], "pressure") == 0) {
187                  sensor_type = PRESSURE_SENSOR;
188                  event = PRESSURE_EVENT_RAW_DATA_REPORT_ON_TIME;
189         }
190         else if (strcmp(argv[1], "temperature") == 0) {
191                  sensor_type = TEMPERATURE_SENSOR;
192                  event = TEMPERATURE_EVENT_RAW_DATA_REPORT_ON_TIME;
193         }
194         else if (strcmp(argv[1], "geomagnetic") == 0) {
195                  sensor_type = GEOMAGNETIC_SENSOR;
196                  event = GEOMAGNETIC_EVENT_RAW_DATA_REPORT_ON_TIME;
197         }
198         else if (strcmp(argv[1], "orientation") == 0) {
199                  sensor_type = ORIENTATION_SENSOR;
200                  event = ORIENTATION_EVENT_RAW_DATA_REPORT_ON_TIME;
201         }
202         else if (strcmp(argv[1], "gravity") == 0) {
203                  sensor_type = GRAVITY_SENSOR;
204                  event = GRAVITY_EVENT_RAW_DATA_REPORT_ON_TIME;
205         }
206         else if (strcmp(argv[1], "linear_accel") == 0) {
207                  sensor_type = LINEAR_ACCEL_SENSOR;
208                  event = LINEAR_ACCEL_EVENT_RAW_DATA_REPORT_ON_TIME;
209         }
210         else if (strcmp(argv[1], "rotation_vector") == 0) {
211                  sensor_type = ROTATION_VECTOR_SENSOR;
212                  event = ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME;
213         }
214         else if (strcmp(argv[1], "gaming_rotation_vector") == 0) {
215                  sensor_type = GEOMAGNETIC_RV_SENSOR;
216                  event = GEOMAGNETIC_RV_EVENT_RAW_DATA_REPORT_ON_TIME;
217         }
218         else if (strcmp(argv[1], "light") == 0) {
219                  sensor_type = LIGHT_SENSOR;
220                  event = LIGHT_EVENT_LUX_DATA_REPORT_ON_TIME;
221         }
222         else if (strcmp(argv[1], "proximity") == 0) {
223                  sensor_type = PROXIMITY_SENSOR;
224                  event = PROXIMITY_EVENT_CHANGE_STATE;
225         }
226         else {
227                  usage();
228         }
229
230         interval = DEFAULT_EVENT_INTERVAL;
231
232         if (argc > 2) {
233                 event = get_event_driven(sensor_type, argv[2]);
234
235                 if (event == -1) {
236                         usage();
237                         return -1;
238                 }
239
240                 EVENT_NOT_ENTERED = FALSE;
241         }
242
243         if (argc == 4) {
244                 interval = strtol(argv[3], &end1, 10);
245
246                 if (*end1) {
247                         printf("Conversion error, non-convertible part: %s\n", end1);
248                         return -1;
249                 }
250         }
251
252         if (argc == 3 && EVENT_NOT_ENTERED) {
253                 interval = strtol(argv[2], &end2, 10);
254
255                 if (*end2) {
256                         printf("Conversion error, non-convertible part: %s\n", end2);
257                         return -1;
258                 }
259         }
260
261         sensor_t sensor = sensord_get_sensor(sensor_type);
262         handle = sensord_connect(sensor);
263
264         result = sensord_register_event(handle, event, interval, 0, callback, NULL);
265
266         if (result < 0) {
267                 printf("Can't register %s\n", argv[1]);
268                 return -1;
269         }
270
271         start_handle = sensord_start(handle, 0);
272
273         if (start_handle < 0) {
274                 printf("Error\n\n\n\n");
275                 sensord_unregister_event(handle, event);
276                 sensord_disconnect(handle);
277                 return -1;
278         }
279
280         g_main_loop_run(mainloop);
281         g_main_loop_unref(mainloop);
282
283         sensord_unregister_event(handle, event);
284         stop_handle = sensord_stop(handle);
285
286         if (stop_handle < 0) {
287                 printf("Error\n\n");
288                 return -1;
289         }
290
291         sensord_disconnect(handle);
292
293         return 0;
294 }