4 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sensor_internal.h>
26 #include <sensorctl_log.h>
27 #include "tester_sensor.h"
29 #define DEFAULT_INTERVAL 100
30 #define DEFAULT_LATENCY 0
31 #define DEFAULT_TEST_COUNT 1
32 #define DEFAULT_EVENT_COUNT 9999
34 #define SENSOR_SHIFT_TYPE 16
36 static GMainLoop *mainloop;
37 static int check_loop;
39 static const char *result_str(bool result) {
40 if (result) return KGRN"[PASS]"RESET;
41 else return KRED"[FAIL]"RESET;
44 bool tester_sensor::init(void)
49 void tester_sensor::test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
54 sensord_get_type(sensor, &type);
56 cnt_event = (int *)user_data;
58 if (check_loop++ >= *cnt_event) {
62 g_main_loop_quit(mainloop);
63 g_main_loop_unref(mainloop);
68 PRINT("[%llu] %s:", data->timestamp, sensord_get_name(sensor));
70 if (type == GESTURE_WRIST_UP_SENSOR) {
71 PRINT("[%d]\n", ((sensorhub_data_t *)data)->hub_data[0]);
75 for (int i = 0; i < data->value_count; ++i)
76 PRINT(" [%f]", data->values[i]);
80 void tester_sensor::test_sensor(sensor_type_t type, int interval, int latency, int cnt_test, int cnt_event)
84 unsigned int event_id;
89 event_id = type << SENSOR_SHIFT_TYPE | 0x1;
91 while (count++ < cnt_test) {
92 mainloop = g_main_loop_new(NULL, FALSE);
95 PRINT("=======================================\n");
96 PRINT("TEST(%d/%d)\n", count, cnt_test);
97 PRINT("=======================================\n");
99 sensor = sensord_get_sensor(type);
100 PRINT("%s sensord_get_sensor: sensor(%p)\n", result_str(sensor==NULL?0:1), sensor);
102 handle = sensord_connect(sensor);
103 PRINT("%s sensord_connect: handle(%d)\n", result_str((handle >= 0)), handle);
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));
108 result = sensord_start(handle, 3);
109 PRINT("%s sensord_start\n", result_str(result));
111 result = sensord_get_data(handle, event_id, &data);
112 PRINT("%s sensord_get_data\n", result_str(result));
114 result = sensord_flush(handle);
115 PRINT("%s sensord_flush\n", result_str(result));
118 for (int i = 0; i < data.value_count; ++i)
119 PRINT("[%f] ", data.values[i]);
123 g_main_loop_run(mainloop);
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);
134 bool tester_sensor::test(sensor_type_t type, int option_count, char *options[])
136 int interval = DEFAULT_INTERVAL;
137 int latency = DEFAULT_LATENCY;
138 int cnt_test = DEFAULT_TEST_COUNT;
139 int cnt_event = DEFAULT_EVENT_COUNT;
141 sensor_type_t sensor_type = type;
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]);
150 test_sensor(sensor_type, interval, latency, cnt_test, cnt_event);