coverity issues fix
[platform/core/system/sensord.git] / src / sensor / create.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <sensor_log.h>
19 #include <fusion_sensor.h>
20 #include <vector>
21
22 #ifdef ENABLE_AUTO_ROTATION
23 #include "auto_rotation/auto_rotation_sensor.h"
24 #endif
25 #ifdef ENABLE_GRAVITY
26 #include "gravity/gravity_lowpass_sensor.h"
27 #include "gravity/gravity_comp_sensor.h"
28 #endif
29 #ifdef ENABLE_LINEAR_ACCEL
30 #include "linear_accel/linear_accel_sensor.h"
31 #endif
32 #ifdef ENABLE_ROTATION_VECTOR
33 #include "rotation_vector/rv_sensor.h"
34 #include "rotation_vector/magnetic_rv_sensor.h"
35 #include "rotation_vector/gyro_rv_sensor.h"
36 #endif
37 #ifdef ENABLE_ORIENTATION
38 #include "orientation/orientation_sensor.h"
39 #endif
40 #ifdef ENABLE_PEDOMETER
41 #include "pedometer/pedometer_sensor.h"
42 #endif
43
44 static std::vector<fusion_sensor_t> sensors;
45
46 template<typename _sensor>
47 void create_sensor(const char *name)
48 {
49         fusion_sensor *instance = NULL;
50         try {
51                 instance = new _sensor;
52         } catch (std::exception &e) {
53                 _E("Failed to create %s sensor, exception: %s", name, e.what());
54                 return;
55         } catch (int err) {
56                 _ERRNO(err, _E, "Failed to create %s sensor device", name);
57                 return;
58         }
59
60         sensors.push_back(instance);
61 }
62
63 extern "C" int create(fusion_sensor_t **fsensors)
64 {
65 #ifdef ENABLE_AUTO_ROTATION
66         create_sensor<auto_rotation_sensor>("Accelerometer");
67 #endif
68
69 #ifdef ENABLE_GRAVITY
70         create_sensor<gravity_lowpass_sensor>("Gravity(Lowpass)");
71         create_sensor<gravity_comp_sensor>("Gravity(Complementary)");
72 #endif
73
74 #ifdef ENABLE_LINEAR_ACCEL
75         create_sensor<linear_accel_sensor>("Linear Acceleration Sensor");
76 #endif
77
78 #ifdef ENABLE_ROTATION_VECTOR
79         create_sensor<rv_sensor>("Rotation Vector Sensor");
80         create_sensor<magnetic_rv_sensor>("Magnetic Rotation Vector Sensor");
81         create_sensor<gyro_rv_sensor>("Gyroscope Rotation Vector Sensor");
82 #endif
83
84 #ifdef ENABLE_ORIENTATION
85         create_sensor<orientation_sensor>("Orientation Sensor");
86 #endif
87
88 #ifdef ENABLE_PEDOMETER
89         create_sensor<pedometer_sensor>("Pedometer");
90 #endif
91
92         *fsensors = &sensors[0];
93         return sensors.size();
94 }