[Non-ACR][TFIVE-357]
[platform/core/system/sensord.git] / src / sensorctl / injector.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 "injector.h"
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <sensor_internal.h>
25
26 #include "log.h"
27 #include "dbus_util.h"
28
29 std::vector<injector *> injector_manager::injectors;
30
31 injector::injector(sensor_type_t sensor_type, const char *event_name)
32 : m_type(sensor_type)
33 , m_name(event_name)
34 {
35         injector_manager::register_injector(this);
36 }
37
38 injector_manager::injector_manager()
39 {
40         if (!dbus_init()) {
41                 _E("Failed to init dbus");
42                 throw;
43         }
44 }
45
46 injector_manager::~injector_manager()
47 {
48         dbus_fini();
49 }
50
51 void injector_manager::register_injector(injector *inject)
52 {
53         injectors.push_back(inject);
54 }
55
56 bool injector_manager::run(int argc, char *argv[])
57 {
58         sensor_type_t type;
59         bool result;
60
61         if (argc < INJECTOR_ARGC) {
62                 usage();
63                 return false;
64         }
65
66         /* 1. get sensor type */
67         type = get_sensor_type(argv[2]);
68         RETVM_IF(type == UNKNOWN_SENSOR, false, "Invalid argument : %s\n", argv[2]);
69
70         /* 2. set up injector */
71         injector *_injector = get_injector(type, argv[3]);
72         RETVM_IF(!_injector, false, "Cannot find matched injector\n");
73
74         /* 3. set up injector */
75         result = _injector->setup();
76         RETVM_IF(!result, false, "Failed to init injector\n");
77
78         /* 4. inject event */
79         result = _injector->inject(argc, argv);
80         RETVM_IF(!result, false, "Failed to run injector\n");
81
82         /* 5. tear down injector */
83         result = _injector->teardown();
84         RETVM_IF(!result, false, "Failed to tear down injector\n");
85
86         return true;
87 }
88
89 injector *injector_manager::get_injector(sensor_type_t type, const char *name)
90 {
91         int count;
92
93         count = injectors.size();
94
95         for (int i = 0; i < count; ++i) {
96                 if (type == injectors[i]->type() &&
97                     (injectors[i]->name() == name))
98                         return injectors[i];
99         }
100         return NULL;
101 }
102
103 void injector_manager::usage(void)
104 {
105         _N("usage: sensorctl inject <sensor_type> [<event_type>] [<options>]\n\n");
106
107         usage_sensors();
108 }