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