sensord: remove unnecessary usleep()
[platform/core/system/sensord.git] / src / sensorctl / sensorctl.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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <signal.h>
24
25 #include "log.h"
26 #include "sensor_manager.h"
27 #include "tester.h"
28 #include "injector.h"
29 #include "info.h"
30 #include "loopback.h"
31
32 static sensor_manager *manager;
33
34 static void usage(void)
35 {
36         _N("usage: sensorctl <command> <sensor_type> [<args>]\n");
37
38         _N("The sensorctl commands are:\n");
39         _N("  test:   test sensor(s)\n");
40         _N("  inject: inject the event to sensor\n");
41         _N("  info:   show sensor infos\n");
42 }
43
44 static sensor_manager *create_manager(char *command)
45 {
46         sensor_manager *manager = NULL;
47
48         if (!strcmp(command, "test"))
49                 manager = new(std::nothrow) tester_manager;
50         if (!strcmp(command, "inject"))
51                 manager = new(std::nothrow) injector_manager;
52         if (!strcmp(command, "info"))
53                 manager = new(std::nothrow) info_manager;
54         if (!strcmp(command, "loopback"))
55                 manager = new(std::nothrow) loopback_manager;
56
57         if (!manager) {
58                 _E("failed to allocate memory for manager\n");
59                 return NULL;
60         }
61
62         return manager;
63 }
64
65 static void destroy_manager(sensor_manager *manager)
66 {
67         if (!manager)
68                 return;
69
70         delete manager;
71         manager = NULL;
72 }
73
74 static void signal_handler(int signo)
75 {
76         _E("\nReceived SIGNAL(%d)\n", signo);
77
78         manager->stop();
79 }
80
81 int main(int argc, char *argv[])
82 {
83         signal(SIGINT,  signal_handler);
84         signal(SIGHUP,  signal_handler);
85         signal(SIGTERM, signal_handler);
86         signal(SIGQUIT, signal_handler);
87         signal(SIGABRT, signal_handler);
88         signal(SIGTSTP, signal_handler);
89
90         if (argc < 2) {
91                 usage();
92                 return EXIT_SUCCESS;
93         }
94
95         manager = create_manager(argv[1]);
96         if (!manager) {
97                 usage();
98                 return EXIT_SUCCESS;
99         }
100
101         manager->run(argc, argv);
102
103         destroy_manager(manager);
104
105         return EXIT_SUCCESS;
106 }