Merge "Updating preprocessing and aiding system documentation" into tizen
[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
20 #include <time.h>
21 #include <glib.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <sensor_internal.h>
25 #include <stdbool.h>
26
27 static GMainLoop *mainloop;
28
29 void callback(unsigned int event_type, sensor_event_data_t *event, void *user_data)
30 {
31         sensor_data_t *data = (sensor_data_t *)event->event_data;
32         printf("Proximity [%lld] [%6.6f]\n\n", data->timestamp, data->values[0]);
33 }
34
35 void printformat()
36 {
37         printf("Usage : ./proxi <event> <interval>(optional)\n\n");
38         printf("event:\n");
39         printf("EVENT_CHANGE_STATE\n");
40         printf("EVENT_STATE_REPORT_ON_TIME\n");
41         printf("EVENT_DISTANCE_DATA_REPORT_ON_TIME\n");
42         printf("interval:\n");
43         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");
44 }
45
46 int main(int argc,char **argv)
47 {
48         int result, handle, start_handle, stop_handle;
49         unsigned int event;
50
51         mainloop = g_main_loop_new(NULL, FALSE);
52         sensor_type_t type = PROXIMITY_SENSOR;
53         event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t));
54         event_condition->cond_op = CONDITION_EQUAL;
55         event_condition->cond_value1 = 100;
56
57         if (argc != 2 && argc != 3) {
58                 printformat();
59                 free(event_condition);
60                 return 0;
61         }
62
63         if (strcmp(argv[1], "EVENT_CHANGE_STATE") == 0) {
64                 event = PROXIMITY_EVENT_CHANGE_STATE;
65         }
66         else if (strcmp(argv[1], "EVENT_STATE_REPORT_ON_TIME") == 0) {
67                 event = PROXIMITY_EVENT_STATE_REPORT_ON_TIME;
68         }
69         else if (strcmp(argv[1], "EVENT_DISTANCE_DATA_REPORT_ON_TIME") == 0) {
70                 event = PROXIMITY_EVENT_DISTANCE_DATA_REPORT_ON_TIME;
71         }
72         else {
73                 printformat();
74                 free(event_condition);
75                 return 0;
76         }
77
78         if (argc == 3)
79                 event_condition->cond_value1 = atof(argv[2]);
80
81         handle = sf_connect(type);
82         result = sf_register_event(handle, event, event_condition, callback, NULL);
83
84         if (result < 0)
85                 printf("Can't register proximity sensor\n");
86
87         start_handle = sf_start(handle,0);
88
89         if (start_handle < 0) {
90                 printf("Error\n\n\n\n");
91                 sf_unregister_event(handle, event);
92                 sf_disconnect(handle);
93                 return -1;
94         }
95
96         g_main_loop_run(mainloop);
97         g_main_loop_unref(mainloop);
98
99         sf_unregister_event(handle, event);
100
101         stop_handle = sf_stop(handle);
102
103         if (stop_handle < 0) {
104                 printf("Failed to stop proximity sensor\n\n");
105                 return -1;
106         }
107
108         sf_disconnect(handle);
109
110         free(event_condition);
111
112         return 0;
113 }
114