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