a3422a73f2a0843aaf2a0a17cb5577d97b809b0d
[platform/core/system/sensord.git] / src / server / sensor_event_handler.cpp
1 /*
2  * sensord
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 "sensor_event_handler.h"
21
22 #include <sensor_log.h>
23 #include <sensor_utils.h>
24 #include <algorithm>
25
26 using namespace sensor;
27
28 static std::vector<uint32_t> ids;
29
30 sensor_event_handler::sensor_event_handler(physical_sensor_handler *sensor)
31 : m_sensor(sensor)
32 {
33 }
34
35 bool sensor_event_handler::handle(int fd, ipc::event_condition condition)
36 {
37         sensor_info info;
38         sensor_data_t *data = NULL;
39         int length = 0;
40         int remains = 1;
41
42         if (m_sensor->read_fd(ids) < 0)
43                 return true;
44
45         auto result = std::find(std::begin(ids), std::end(ids), m_sensor->get_hal_id());
46
47         if (result == std::end(ids))
48         {
49                 ids.clear();
50                 return true;
51         }
52
53         while (remains > 0) {
54                 remains = m_sensor->get_data(&data, &length);
55                 if (remains < 0) {
56                         _E("Failed to get sensor data");
57                         break;
58                 }
59
60                 if (m_sensor->on_event(data, length, remains) < 0) {
61                         free(data);
62                         data = NULL;
63                         continue;
64                 }
65
66                 info = m_sensor->get_sensor_info();
67
68                 //_I("[Data] allocate %p", data);
69                 if (data) {
70                         if (m_sensor->notify(info.get_uri().c_str(), data, length) < 0) {
71                                 free(data);
72                                 data = NULL;
73                         }
74                 }
75                 info.clear();
76         }
77
78         ids.clear();
79
80         return true;
81 }