Use sensor_error_e of hal API
[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   if (devs.empty()) {
48 #ifdef ENABLE_ACCEL
49   create_sensor<accel_device>("Accelerometer");
50 #endif
51 #ifdef ENABLE_PROXIMITY
52   create_sensor<proxi_device>("Proximity");
53 #endif
54 #ifdef ENABLE_SENSORHUB
55   create_sensor<sensorhub_device>("Sensorhub");
56 #endif
57   }
58
59   *devices = &devs[0];
60   return devs.size();
61 }
62
63 static int sensor_tm1_init(void **data) {
64   _I("init hal backend sensor");
65   hal_backend_sensor_funcs *funcs;
66
67   funcs =
68       (hal_backend_sensor_funcs *)calloc(1, sizeof(hal_backend_sensor_funcs));
69   if (!funcs) return -ENOMEM;
70
71   funcs->create = sensor_tm1_create;
72
73   *data = (void *)funcs;
74
75   return 0;
76 }
77
78 static int sensor_tm1_exit(void *data) {
79   if (!data) return -EINVAL;
80   free(data);
81
82   return 0;
83 }
84
85 extern "C" hal_backend hal_backend_sensor_data = {
86     .name = "sensor-tm1",
87     .vendor = "Tizen",
88     .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
89     .init = sensor_tm1_init,
90     .exit = sensor_tm1_exit,
91 };