Merge "Adding a new function for priority list insertion" into tizen
[platform/core/system/sensord.git] / test / src / temperature.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("Temperature [%lld] [%6.6f] \n\n", data->timestamp, data->values[0]);
35 }
36
37 void printformat()
38 {
39         printf("Usage : ./temperature <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("[RAW_DATA_REPORT_ON_TIME]\n");
47
48         printf("interval:\n");
49         printf("The time interval should be entered based on the sampling frequency supported by temperature driver on the device in ms.If no value for sensor is entered default value by the driver will be used.\n");
50 }
51
52 int main(int argc,char **argv)
53 {
54         int result, handle, start_handle, stop_handle;
55         unsigned int event;
56         mainloop = g_main_loop_new(NULL, FALSE);
57         event = TEMPERATURE_EVENT_RAW_DATA_REPORT_ON_TIME;
58         event_condition_t *event_condition = (event_condition_t*) malloc(sizeof(event_condition_t));
59         event_condition->cond_op = CONDITION_EQUAL;
60
61         sensor_type_t type = TEMPERATURE_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 && strcmp(argv[2], "RAW_DATA_REPORT_ON_TIME") == 0) {
70                 printf("Polling based\n");
71                 handle = sf_connect(type);
72                 result = sf_start(handle, 1);
73
74                 if (result < 0) {
75                         printf("Can't start temperature SENSOR\n");
76                         printf("Error\n\n\n\n");
77                         free(event_condition);
78                         return -1;
79                 }
80
81                 sensor_data_t data;
82
83                 while(1) {
84                         result = sf_get_data(handle, TEMPERATURE_BASE_DATA_SET , &data);
85                         printf("Temperature [%6.6f] \n\n", data.values[0]);
86                         usleep(100000);
87                 }
88
89                 result = sf_disconnect(handle);
90
91                 if (result < 0) {
92                         printf("Can't disconnect temperature sensor\n");
93                         printf("Error\n\n\n\n");
94                         free(event_condition);
95                         return -1;
96                 }
97         }
98
99         else if (strcmp(argv[1], "RAW_DATA_REPORT_ON_TIME") == 0) {
100                 printf("Event based\n");
101
102                 event_condition->cond_value1 = 100;
103                 if (argc == 3)
104                         event_condition->cond_value1 = atof(argv[2]);
105
106                 handle = sf_connect(type);
107                 result = sf_register_event(handle, event, event_condition, callback, NULL);
108
109                 if (result < 0)
110                         printf("Can't register temperature\n");
111
112                 start_handle = sf_start(handle,0);
113
114                 if (start_handle < 0) {
115                         printf("Error\n\n\n\n");
116                         sf_unregister_event(handle, event);
117                         sf_disconnect(handle);
118                         free(event_condition);
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                         free(event_condition);
132                         return -1;
133                 }
134
135                 sf_disconnect(handle);
136                 free(event_condition);
137         }
138
139         else {
140                 printformat();
141         }
142
143         return 0;
144 }
145