Merge branch 'devel/tizen' into tizen
[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;
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                 return true;
49
50         while (remains > 0) {
51                 remains = m_sensor->get_data(&data, &length);
52                 if (remains < 0) {
53                         _E("Failed to get sensor data");
54                         break;
55                 }
56
57                 if (m_sensor->on_event(data, length, remains) < 0) {
58                         free(data);
59                         continue;
60                 }
61
62                 info = m_sensor->get_sensor_info();
63
64                 //_I("[Data] allocate %p", data);
65                 if (m_sensor->notify(info.get_uri().c_str(), data, length) < 0) {
66                         free(data);
67                 }
68                 info.clear();
69         }
70
71         return true;
72 }