35444827f5930d7c6b44f74579cd1115dae4bbff
[platform/hal/backend/tm1/sensor-tm1.git] / src / hal-backend-sensor.cpp
1 /*
2  * Copyright (c) 2021 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 #include <errno.h>
18 #include <hal/hal-sensor-interface.h>
19 #include <sensor_log.h>
20 #include <stdlib.h>
21
22 #include <vector>
23
24 #include "accel/accel_device.h"
25 #include "proxi/proxi_device.h"
26 #include "sensorhub/sensorhub.h"
27
28 static std::vector<sensor_device_t> devs;
29
30 template <typename _sensor>
31 void create_sensor(const char *name) {
32   sensor_device *instance = NULL;
33   try {
34     instance = new _sensor;
35   } catch (std::exception &e) {
36     ERR("Failed to create %s sensor device, exception: %s", name, e.what());
37     return;
38   } catch (int err) {
39     _ERRNO(err, _E, "Failed to create %s sensor device", name);
40     return;
41   }
42
43   devs.push_back(instance);
44 }
45
46 static int sensor_tm1_create(sensor_device_t **devices) {
47 #ifdef ENABLE_ACCEL
48   create_sensor<accel_device>("Accelerometer");
49 #endif
50 #ifdef ENABLE_PROXIMITY
51   create_sensor<proxi_device>("Proximity");
52 #endif
53 #ifdef ENABLE_SENSORHUB
54   create_sensor<sensorhub_device>("Sensorhub");
55 #endif
56
57   *devices = &devs[0];
58   return devs.size();
59 }
60
61 static int sensor_tm1_init(void **data) {
62   _I("init hal backend sensor");
63   hal_backend_sensor_funcs *funcs;
64
65   funcs =
66       (hal_backend_sensor_funcs *)calloc(1, sizeof(hal_backend_sensor_funcs));
67   if (!funcs) return -ENOMEM;
68
69   funcs->create = sensor_tm1_create;
70
71   *data = (void *)funcs;
72
73   return 0;
74 }
75
76 static int sensor_tm1_exit(void *data) {
77   if (!data) return -EINVAL;
78   free(data);
79
80   return 0;
81 }
82
83 extern "C" hal_backend hal_backend_sensor_data = {
84     .name = "sensor-tm1",
85     .vendor = "Tizen",
86     .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
87     .init = sensor_tm1_init,
88     .exit = sensor_tm1_exit,
89 };