sensorctl: change scanf to cin for security issue
[platform/core/system/sensord.git] / src / sensorctl / tester_manual.cpp
1 /*
2  * sensorctl
3  *
4  * Copyright (c) 2017 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 <iostream>
21 #include <getopt.h>
22
23 #include "tester.h"
24 #include "mainloop.h"
25 #include "log.h"
26 #include "test_bench.h"
27 #include "sensor_adapter.h"
28
29 #define MAX_COUNT 999999
30 #define TEST_DEFAULT_INTERVAL 100
31 #define TEST_DEFAULT_LATENCY 0
32 #define TEST_DEFAULT_POWERSAVE_OPTION SENSOR_OPTION_ALWAYS_ON
33
34 #define TEST_MANUAL "manual"
35
36 static sensor_type_t stype;
37 static int interval;
38 static int latency;
39 static int powersave;
40 //static int repeat;
41
42 class tester_manual : public tester {
43 public:
44         tester_manual(const char *name);
45         virtual ~tester_manual();
46
47         bool setup(sensor_type_t type, int argc, char *argv[]);
48         bool teardown(void);
49
50         bool run(int argc, char *argv[]);
51         void usage(void);
52 };
53
54 tester_manual::tester_manual(const char *name)
55 : tester(name)
56 {
57 }
58
59 tester_manual::~tester_manual()
60 {
61 }
62
63 bool tester_manual::setup(sensor_type_t type, int argc, char *argv[])
64 {
65         stype = type;
66         interval = TEST_DEFAULT_INTERVAL;
67         latency = TEST_DEFAULT_LATENCY;
68         powersave = TEST_DEFAULT_POWERSAVE_OPTION;
69
70         if (!test_option::set_options(argc, argv)) {
71                 usage();
72                 return false;
73         }
74
75         interval = test_option::interval;
76         latency = test_option::latency;
77         powersave = test_option::powersave;
78
79         if (interval == -1)
80                 interval = TEST_DEFAULT_INTERVAL;
81         if (latency == -1)
82                 latency = TEST_DEFAULT_LATENCY;
83         if (powersave == -1)
84                 powersave = TEST_DEFAULT_POWERSAVE_OPTION;
85
86         return true;
87 }
88
89 bool tester_manual::teardown(void)
90 {
91         return true;
92 }
93
94 bool tester_manual::run(int argc, char *argv[])
95 {
96         test_option::filter = "^manual[\\w\\.]+";
97         test_bench::run_all_testcases();
98
99         return true;
100 }
101
102 void tester_manual::usage(void)
103 {
104         _N("Usage : sensorctl test <sensor_type>\n");
105         _N("          [--interval=NUMBER] [--batch_latency=NUMBER] [--powersave=TYPE]\n");
106         _N("          [--repeat=NUMBER] [--output=FILE_PATH] [--help] [--verbose]\n");
107
108         _N("Examples:\n");
109         _N("  sensorctl test accelerometer\n");
110         _N("  sensorctl test accelerometer --interval=100 --batch_latency=1000\n");
111         _N("  sensorctl test accelerometer --i 100 --b 1000 --p 0\n");
112         _N("  sensorctl test accelerometer --i 100 --shuffle\n");
113         _N("  sensorctl test accelerometer --i 100 --verbose\n");
114         _N("  sensorctl test accelerometer --i 100 --output=results.log\n");
115 }
116
117 static void test_cb(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data)
118 {
119         _N("%llu ", data->timestamp);
120         for (int i = 0; i < data->value_count; ++i)
121                 _N(" %10f", data->values[i]);
122         _N("\n");
123 }
124
125 TESTCASE(manual_test, sensor)
126 {
127         int handle;
128         bool ret;
129         int index = 0;
130         sensor_data_t data;
131
132         if (sensor_adapter::get_count(stype) > 1) {
133                 _N("There are more than 2 sensors. please enter the index : ");
134                 std::cin >> index;
135         }
136
137         sensor_info info(stype, index, interval, latency, powersave, test_cb, NULL);
138
139         ret = sensor_adapter::start(info, handle);
140         ASSERT_TRUE(ret);
141
142         ret = sensor_adapter::get_data(handle, stype, data);
143         EXPECT_TRUE(ret);
144
145         mainloop::run();
146
147         ret = sensor_adapter::stop(info, handle);
148         ASSERT_TRUE(ret);
149
150         return true;
151 }
152
153 REGISTER_TESTER(TEST_MANUAL, tester_manual);