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