sensorctl test
[platform/core/system/sensord.git] / src / sensorctl / tester_sensor.cpp
1 /*
2  * sensorctl
3  *
4  * Copyright (c) 2015 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 <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <glib.h>
24 #include <gio/gio.h>
25 #include <sensor_internal.h>
26 #include <sensorctl_log.h>
27 #include "tester_sensor.h"
28
29 #define DEFAULT_INTERVAL 100
30 #define DEFAULT_LATENCY 0
31 #define DEFAULT_TEST_COUNT 1
32 #define DEFAULT_EVENT_COUNT 9999
33
34 #define SENSOR_SHIFT_TYPE 16
35
36 static GMainLoop *mainloop;
37 static int check_loop;
38
39 static const char *result_str(bool result) {
40         if (result)             return KGRN"[PASS]"RESET;
41         else                    return KRED"[FAIL]"RESET;
42 }
43
44 bool tester_sensor::init(void)
45 {
46         return true;
47 }
48
49 void tester_sensor::test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
50 {
51         sensor_type_t type;
52         int *cnt_event;
53
54         sensord_get_type(sensor, &type);
55
56         cnt_event = (int *)user_data;
57
58         if (check_loop++ >= *cnt_event) {
59                 if (!mainloop)
60                         return;
61
62                 g_main_loop_quit(mainloop);
63                 g_main_loop_unref(mainloop);
64                 mainloop = NULL;
65                 return;
66         }
67
68         PRINT("[%llu] %s:", data->timestamp, sensord_get_name(sensor));
69
70         if (type == GESTURE_WRIST_UP_SENSOR) {
71                 PRINT("[%d]\n", ((sensorhub_data_t *)data)->hub_data[0]);
72                 return;
73         }
74
75         for (int i = 0; i < data->value_count; ++i)
76                 PRINT(" [%f]", data->values[i]);
77         PRINT("\n");
78 }
79
80 void tester_sensor::test_sensor(sensor_type_t type, int interval, int latency, int cnt_test, int cnt_event)
81 {
82         bool result;
83         sensor_t sensor;
84         unsigned int event_id;
85         sensor_data_t data;
86         int handle;
87         int count = 0;
88
89         event_id = type << SENSOR_SHIFT_TYPE | 0x1;
90
91         while (count++ < cnt_test) {
92                 mainloop = g_main_loop_new(NULL, FALSE);
93                 check_loop = 0;
94
95                 PRINT("=======================================\n");
96                 PRINT("TEST(%d/%d)\n", count, cnt_test);
97                 PRINT("=======================================\n");
98
99                 sensor = sensord_get_sensor(type);
100                 PRINT("%s sensord_get_sensor: sensor(%p)\n", result_str(sensor==NULL?0:1), sensor);
101
102                 handle = sensord_connect(sensor);
103                 PRINT("%s sensord_connect: handle(%d)\n", result_str((handle >= 0)), handle);
104
105                 result = sensord_register_event(handle, event_id, interval, latency, test_cb, (void *)&cnt_event);
106                 PRINT("%s sensord_register_event\n", result_str(result));
107
108                 result = sensord_start(handle, 3);
109                 PRINT("%s sensord_start\n", result_str(result));
110
111                 result = sensord_get_data(handle, event_id, &data);
112                 PRINT("%s sensord_get_data\n", result_str(result));
113
114                 result = sensord_flush(handle);
115                 PRINT("%s sensord_flush\n", result_str(result));
116
117                 if (result) {
118                         for (int i = 0; i < data.value_count; ++i)
119                                 PRINT("[%f] ", data.values[i]);
120                         PRINT("\n");
121                 }
122
123                 g_main_loop_run(mainloop);
124
125                 result = sensord_unregister_event(handle, event_id);
126                 PRINT("%s sensord_unregister_event: handle(%d)\n", result_str(result), handle);
127                 result = sensord_stop(handle);
128                 PRINT("%s sensord_stop: handle(%d)\n", result_str(result), handle);
129                 result = sensord_disconnect(handle);
130                 PRINT("%s sensord_disconnect: handle(%d)\n", result_str(result), handle);
131         }
132 }
133
134 bool tester_sensor::test(sensor_type_t type, int option_count, char *options[])
135 {
136         int interval = DEFAULT_INTERVAL;
137         int latency = DEFAULT_LATENCY;
138         int cnt_test = DEFAULT_TEST_COUNT;
139         int cnt_event = DEFAULT_EVENT_COUNT;
140
141         sensor_type_t sensor_type = type;
142
143         if (option_count >= 1)
144                 interval = atoi(options[0]);
145         if (option_count >= 2)
146                 cnt_event = atoi(options[1]);
147         if (option_count >= 3)
148                 cnt_test = atoi(options[2]);
149
150         test_sensor(sensor_type, interval, latency, cnt_test, cnt_event);
151         return true;
152 }