[Non-ACR][TFIVE-357]
[platform/core/system/sensord.git] / src / sensorctl / tester.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 "tester.h"
21
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sensor_internal.h>
26
27 #include "log.h"
28 #include "macro.h"
29 #include "mainloop.h"
30
31 std::vector<tester *> tester_manager::testers;
32
33 tester::tester(const char *name)
34 : m_name(name)
35 {
36         tester_manager::register_tester(this);
37 }
38
39 tester_manager::tester_manager()
40 {
41 }
42
43 tester_manager::~tester_manager()
44 {
45 }
46
47 void tester_manager::register_tester(tester *test)
48 {
49         testers.push_back(test);
50 }
51
52 bool tester_manager::run(int argc, char *argv[])
53 {
54         bool ret;
55         sensor_type_t type = ACCELEROMETER_SENSOR;
56
57         if (argc < TESTER_ARGC) {
58                 usage();
59                 return false;
60         }
61
62         if (strncmp(argv[2], "auto", 4) != 0)
63                 type = get_sensor_type(argv[2]);
64
65         tester *_tester = get_tester(argv[2]);
66         RETVM_IF(!_tester, false, "Cannot find matched tester\n");
67
68         ret = _tester->setup(type, argc, argv);
69         RETVM_IF(!ret, false, "Failed to setup injector\n");
70
71         ret = _tester->run(argc, argv);
72         RETVM_IF(!ret, false, "Failed to run tester\n");
73
74         ret = _tester->teardown();
75         RETVM_IF(!ret, false, "Failed to tear down tester\n");
76
77         return true;
78 }
79
80 tester *tester_manager::get_tester(const char *name)
81 {
82         int count = testers.size();
83
84         for (int i = 0; i < count; ++i) {
85                 if (strncmp(name, "auto", 4) == 0) {
86                         if (testers[i]->name() == "auto")
87                                 return testers[i];
88                 } else {
89                         if (testers[i]->name() == "manual")
90                                 return testers[i];
91                 }
92         }
93         return NULL;
94 }
95
96 void tester_manager::stop(void)
97 {
98         if (mainloop::is_running())
99                 mainloop::stop();
100 }
101
102 void tester_manager::usage(void)
103 {
104         _N("usage: sensorctl test auto [options] [--help]\n");
105         _N("usage: sensorctl test <sensor_type> [--help]\n");
106
107         usage_sensors();
108
109         _N("auto:\n");
110         _N("  Run all testcases automatically.\n");
111         _N("sensor_type:\n");
112         _N("  Run the specific sensor manually.\n");
113         _N("help: \n");
114         _N("  Prints the synopsis and a list of options.\n");
115 }
116