Gaming rotation vector - Feature merge from devel/tizen branch
[platform/core/system/sensord.git] / test / src / proxi.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("Proximity [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
35 }
36
37 void printformat()
38 {
39         printf("Usage : ./proxi <mode>(optional) <event> <interval>(optional)\n\n");
40
41         printf("mode:");
42         printf("[-p]\n");
43         printf("p is for polling based,default mode is event driven\n");
44
45         printf("event:");
46         printf("[EVENT_CHANGE_STATE] ");
47         printf("[EVENT_STATE_REPORT_ON_TIME] ");
48         printf("[EVENT_DISTANCE_DATA_REPORT_ON_TIME]\n");
49
50         printf("interval:\n");
51         printf("The time interval should be entered based on the sampling frequency supported by proximity driver on the device in ms.If no value for sensor is entered default value by the driver will be used.\n");
52 }
53
54 int main(int argc,char **argv)
55 {
56         int result, handle, start_handle, stop_handle;
57         unsigned int event;
58         mainloop = g_main_loop_new(NULL, FALSE);
59         event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t));
60         event_condition->cond_op = CONDITION_EQUAL;
61         sensor_type_t type = PROXIMITY_SENSOR;
62
63         if (argc != 2 && argc != 3 && argc!=4) {
64                 printformat();
65                 free(event_condition);
66                 return 0;
67         }
68
69         else if (argc>=3 && strcmp(argv[1], "-p") == 0) {
70                 printf("Polling based\n");
71
72                 if (strcmp(argv[2], "EVENT_CHANGE_STATE") == 0) {
73                         event = PROXIMITY_BASE_DATA_SET;
74                 }
75                 else if (strcmp(argv[2], "EVENT_STATE_REPORT_ON_TIME") == 0) {
76                         event = PROXIMITY_DISTANCE_DATA_SET;
77                 }
78                 else if (strcmp(argv[2], "EVENT_DISTANCE_DATA_REPORT_ON_TIME") == 0) {
79                         event = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
80                 }
81                 else {
82                         printformat();
83                         free(event_condition);
84                         return 0;
85                 }
86
87                 handle = sf_connect(type);
88                 result = sf_start(handle, 1);
89
90                 if (result < 0) {
91                         printf("Can't start proximity SENSOR\n");
92                         printf("Error\n\n\n\n");
93                         free(event_condition);
94                         return -1;
95                 }
96
97                 sensor_data_t data;
98
99                 while(1) {
100                         result = sf_get_data(handle, event , &data);
101                         printf("Proximity [%6.6f]\n\n", data.values[0]);
102                         usleep(100000);
103                 }
104
105                 result = sf_disconnect(handle);
106
107                 if (result < 0) {
108                         printf("Can't disconnect proximity sensor\n");
109                         printf("Error\n\n\n\n");
110                         free(event_condition);
111                         return -1;
112                 }
113         }
114
115         else if (argc == 2 || argc ==3) {
116                 printf("Event based\n");
117
118                 if (strcmp(argv[1], "EVENT_CHANGE_STATE") == 0) {
119                         event = PROXIMITY_EVENT_CHANGE_STATE;
120                 }
121                 else if (strcmp(argv[1], "EVENT_STATE_REPORT_ON_TIME") == 0) {
122                         event = PROXIMITY_EVENT_STATE_REPORT_ON_TIME;
123                 }
124                 else if (strcmp(argv[1], "EVENT_DISTANCE_DATA_REPORT_ON_TIME") == 0) {
125                         event = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
126                 }
127                 else {
128                         printformat();
129                         free(event_condition);
130                         return 0;
131                 }
132
133                 event_condition->cond_value1 = 100;
134                 if (argc == 3)
135                         event_condition->cond_value1 = atof(argv[2]);
136
137                 handle = sf_connect(type);
138                 result = sf_register_event(handle, event, event_condition, callback, NULL);
139
140                 if (result < 0)
141                         printf("Can't register proximity\n");
142
143                 start_handle = sf_start(handle,0);
144
145                 if (start_handle < 0) {
146                         printf("Error\n\n\n\n");
147                         sf_unregister_event(handle, event);
148                         sf_disconnect(handle);
149                         free(event_condition);
150                         return -1;
151                 }
152
153                 g_main_loop_run(mainloop);
154                 g_main_loop_unref(mainloop);
155
156                 sf_unregister_event(handle, event);
157
158                 stop_handle = sf_stop(handle);
159
160                 if (stop_handle < 0) {
161                         printf("Error\n\n");
162                         free(event_condition);
163                         return -1;
164                 }
165
166                 sf_disconnect(handle);
167                 free(event_condition);
168         }
169
170         else {
171                 printformat();
172         }
173
174         return 0;
175 }
176