sensord: move .service/.socket to packaging
[platform/core/system/sensord.git] / test / src / fusion-data-collection.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 MAXSIZE 4
30
31 static GMainLoop *mainloop;
32 FILE* file_output[MAXSIZE];
33
34 void callback(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
35 {
36         sensor_type_t sensor_type = event_type >> 16;
37
38         switch (sensor_type) {
39         case ACCELEROMETER_SENSOR:
40                 fprintf(file_output[0], "%6.6f %6.6f %6.6f %lld\n", data->values[0], data->values[1], data->values[2], data->timestamp);
41                 fflush(file_output[0]);
42                 break;
43         case GEOMAGNETIC_SENSOR:
44                 fprintf(file_output[1], "%6.6f %6.6f %6.6f %lld\n", data->values[0], data->values[1], data->values[2], data->timestamp);
45                 fflush(file_output[1]);
46                 break;
47         case GYROSCOPE_SENSOR:
48                 fprintf(file_output[2], "%6.6f %6.6f %6.6f %lld\n", data->values[0], data->values[1], data->values[2], data->timestamp);
49                 fflush(file_output[2]);
50                 break;
51         case PROXIMITY_SENSOR:
52                 fprintf(file_output[MAXSIZE-1], "%6.6f %lld\n", data->values[0], data->timestamp);
53                 fflush(file_output[MAXSIZE-1]);
54                 break;
55         default:
56                 return;
57         }
58 }
59
60 void usage()
61 {
62         printf("Usage : ./fusion-data-collection <interval>\n\n");
63
64         printf("interval:\n");
65         printf("The sampling interval in ms.\n");
66         exit(-1);
67 }
68
69 int main(int argc, char **argv)
70 {
71         int interval;
72
73         if (argc == 2) {
74                 interval = atoi(argv[1]);
75                 if (interval <= 0)
76                         usage();
77         }
78         else
79                 usage();
80
81         int i;
82
83         int handle[MAXSIZE];
84         bool result[MAXSIZE], start_handle[MAXSIZE];
85         unsigned int event[MAXSIZE];
86         int sensors[MAXSIZE];
87
88         sensors[0] = ACCELEROMETER_SENSOR;
89         sensors[1] = GEOMAGNETIC_SENSOR;
90         sensors[2] = GYROSCOPE_SENSOR;
91         sensors[MAXSIZE-1] = PROXIMITY_SENSOR;
92
93         mainloop = g_main_loop_new(NULL, FALSE);
94
95         char file_name[50];
96
97         for (i = 0; i < MAXSIZE; i++) {
98                 snprintf(file_name, sizeof(file_name), "output_%d", sensors[i]);
99                 file_output[i] = fopen(file_name, "w+");
100                 sensor_t sensor = sensord_get_sensor(sensors[i]);
101                 handle[i] = sensord_connect(sensor);
102                 event[i] = (sensors[i] << 16) | 0x0001;
103                 result[i] = sensord_register_event(handle[i], event[i], interval, 0, callback, NULL);
104
105                 if (!result[i]) {
106                         printf("error: unable to register sensor\n");
107                         return -1;
108                 }
109                 start_handle[i] = sensord_start(handle[i], 1);
110
111                 if (!start_handle[i]) {
112                         printf("error: unable to start handle\n");
113                         sensord_unregister_event(handle[i], event[i]);
114                         sensord_disconnect(handle[i]);
115                         return -1;
116                 }
117         }
118
119         g_main_loop_run(mainloop);
120         g_main_loop_unref(mainloop);
121
122         for (i = 0; i < MAXSIZE; i++) {
123                 sensord_unregister_event(handle[i], event[i]);
124                 sensord_stop(handle[i]);
125                 sensord_disconnect(handle[i]);
126         }
127
128         return 0;
129 }