be9adef1015d0234ddaa3af392aa3d918fe56c05
[platform/core/system/sensord.git] / test / src / rotation_vector.c
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 static GMainLoop *mainloop;
30
31 void callback(unsigned int event_type, sensor_event_data_t *event, void *user_data)
32 {
33         sensor_data_t *data = (sensor_data_t *)event->event_data;
34         printf("Rotation Vector [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data->timestamp, data->values[0],
35                         data->values[1], data->values[2]);
36 }
37
38 void printformat()
39 {
40         printf("Usage : ./rotation_vector <mode>(optional) <event> <interval>(optional)\n\n");
41
42         printf("mode:");
43         printf("[-p]\n");
44         printf("p is for polling based,default mode is event driven\n");
45
46         printf("event:");
47         printf("[RAW_DATA_REPORT_ON_TIME]\n");
48
49         printf("interval:\n");
50         printf("The time interval should be entered based on the sampling frequencies supported by "
51                         "accelerometer, gyroscope and geomagnetic sensors driver on the device in ms. If "
52                         "no value for rotation vector sensor is entered, a default value will be used.\n");
53 }
54
55 int main(int argc,char **argv)
56 {
57         int result, handle, start_handle, stop_handle;
58         unsigned int event;
59         mainloop = g_main_loop_new(NULL, FALSE);
60         event = ROTATION_VECTOR_EVENT_RAW_DATA_REPORT_ON_TIME;
61         event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t));
62         event_condition->cond_op = CONDITION_EQUAL;
63
64         sensor_type_t type = ROTATION_VECTOR_SENSOR;
65
66         if (argc != 2 && argc != 3 && argc!=4) {
67                 printformat();
68                 free(event_condition);
69                 return 0;
70         }
71
72         else if (argc>=3 && strcmp(argv[1], "-p") == 0 && strcmp(argv[2], "RAW_DATA_REPORT_ON_TIME") == 0) {
73                 printf("Polling based\n");
74                 handle = sf_connect(type);
75                 result = sf_start(handle, 1);
76
77                 if (result < 0) {
78                         printf("Can't start Rotation Vector SENSOR\n");
79                         printf("Error\n\n\n\n");
80                         return -1;
81                 }
82
83                 sensor_data_t data;
84
85                 while(1) {
86                         result = sf_get_data(handle, ACCELEROMETER_BASE_DATA_SET , &data);
87                         printf("Rotation Vector [%lld] [%6.6f] [%6.6f] [%6.6f]\n\n", data.timestamp, data.values[0], data.values[1], data.values[2]);
88                         usleep(100000);
89                 }
90
91                 result = sf_disconnect(handle);
92
93                 if (result < 0) {
94                         printf("Can't disconnect ROTATION VECTOR sensor\n");
95                         printf("Error\n\n\n\n");
96                         return -1;
97                 }
98         }
99
100         else if (strcmp(argv[1], "RAW_DATA_REPORT_ON_TIME") == 0) {
101                 printf("Event based\n");
102
103                 event_condition->cond_value1 = 100;
104                 if (argc == 3)
105                         event_condition->cond_value1 = atof(argv[2]);
106
107                 handle = sf_connect(type);
108                 result = sf_register_event(handle, event, event_condition, callback, NULL);
109
110                 if (result < 0)
111                         printf("Can't register rotation vector event\n");
112
113                 start_handle = sf_start(handle,0);
114
115                 if (start_handle < 0) {
116                         printf("Error\n\n\n\n");
117                         sf_unregister_event(handle, event);
118                         sf_disconnect(handle);
119                         return -1;
120                 }
121
122                 g_main_loop_run(mainloop);
123                 g_main_loop_unref(mainloop);
124
125                 sf_unregister_event(handle, event);
126
127                 stop_handle = sf_stop(handle);
128
129                 if (stop_handle < 0) {
130                         printf("Error\n\n");
131                         return -1;
132                 }
133
134                 sf_disconnect(handle);
135                 free(event_condition);
136         }
137
138         else {
139                 printformat();
140         }
141
142         return 0;
143 }
144