Merge "Adding AK8975 geo-sensor info in sensors.xml.in required by geo-plugin" into...
[platform/core/system/sensord.git] / test / src / accelerometer.c
1 #include <time.h>
2 #include <glib.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <sensor.h>
6 #include <stdbool.h>
7
8 static GMainLoop *mainloop;
9
10 void callback(unsigned int event_type, sensor_event_data_t *event, void *user_data)
11 {
12         sensor_data_t *data = (sensor_data_t *)event->event_data;
13         printf("Accelerometer [%6.6f] [%6.6f] [%6.6f] [%lld]\n\n", data->values[0], data->values[1], data->values[2], data->timestamp);
14 }
15
16 void printformat()
17 {
18         printf("Usage : ./accelerometer <event> <interval>(optional)\n\n");
19         printf("event:\n");
20         printf("ROTATION_CHECK\n");
21         printf("RAW_DATA_REPORT_ON_TIME\n");
22         printf("CALIBRATION_NEEDED\n");
23         printf("SET_HORIZON\n");
24         printf("SET_WAKEUP\n");
25         printf("ORIENTATION_DATA_REPORT_ON_TIME\n");
26         printf("LINEAR_ACCELERATION_DATA_REPORT_ON_TIME\n");
27         printf("GRAVITY_DATA_REPORT_ON_TIME\n\n");
28         printf("interval:\n");
29         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");
30 }
31
32 int main(int argc,char **argv)
33 {
34         int result, handle;
35         unsigned int event;
36         bool error_state = FALSE;
37
38         mainloop = g_main_loop_new(NULL, FALSE);
39         sensor_type_t type = ACCELEROMETER_SENSOR;
40         event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t));
41         event_condition->cond_op = CONDITION_EQUAL;
42         event_condition->cond_value1=100;
43
44         if (argc != 2 && argc != 3) {
45                 printformat();
46                 error_state = TRUE;
47         }
48         else {
49                 if (strcmp(argv[1], "ROTATION_CHECK") == 0)
50                         event = ACCELEROMETER_EVENT_ROTATION_CHECK;
51                 else if (strcmp(argv[1], "RAW_DATA_REPORT_ON_TIME") == 0)
52                         event = ACCELEROMETER_EVENT_RAW_DATA_REPORT_ON_TIME;
53                 else if (strcmp(argv[1], "CALIBRATION_NEEDED") == 0)
54                         event = ACCELEROMETER_EVENT_CALIBRATION_NEEDED;
55                 else if (strcmp(argv[1], "SET_HORIZON") == 0)
56                         event = ACCELEROMETER_EVENT_SET_HORIZON;
57                 else if (strcmp(argv[1], "SET_WAKEUP") == 0)
58                         event = ACCELEROMETER_EVENT_SET_WAKEUP;
59                 else if (strcmp(argv[1], "ORIENTATION_DATA_REPORT_ON_TIME") == 0)
60                         event = ACCELEROMETER_EVENT_ORIENTATION_DATA_REPORT_ON_TIME;
61                 else if (strcmp(argv[1], "LINEAR_ACCELERATION_DATA_REPORT_ON_TIME") == 0)
62                         event = ACCELEROMETER_EVENT_LINEAR_ACCELERATION_DATA_REPORT_ON_TIME;
63                 else if (strcmp(argv[1], "GRAVITY_DATA_REPORT_ON_TIME") == 0)
64                         event = ACCELEROMETER_EVENT_GRAVITY_DATA_REPORT_ON_TIME;
65                 else {
66                         printformat();
67                         error_state = TRUE;
68                 }
69                 if (argc == 3)
70                         event_condition->cond_value1 = atof(argv[2]);
71         }
72
73         if (!error_state) {
74                 handle = sf_connect(type);
75                 result = sf_register_event(handle, event, event_condition, callback, NULL);
76
77                 if (result < 0)
78                         printf("Can't register accelerometer\n");
79
80                 if (!(sf_start(handle,0) < 0)) {
81                         printf("Success start \n");
82                 }
83                 else {
84                         printf("Error\n\n\n\n");
85                         sf_unregister_event(handle, event);
86                         sf_disconnect(handle);
87                         return -1;
88                 }
89
90                 g_main_loop_run(mainloop);
91                 g_main_loop_unref(mainloop);
92
93                 sf_unregister_event(handle, event);
94
95                 if (!(sf_stop(handle) < 0))
96                         printf("Success stop \n");
97
98                 sf_disconnect(handle);
99         }
100
101         free(event_condition);
102
103         return 0;
104 }
105