9045a88de3f89d9d23f9dd086e40d951c65a0fbe
[platform/core/system/sensord.git] / src / shared / channel_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 "channel_event_handler.h"
21
22 #include "channel.h"
23 #include "channel_handler.h"
24 #include "sensor_log.h"
25
26 using namespace ipc;
27
28 channel_event_handler::channel_event_handler(channel *ch, channel_handler *handler)
29 : m_ch(ch)
30 , m_handler(handler)
31 {
32 }
33
34 channel_event_handler::~channel_event_handler()
35 {
36         m_ch = NULL;
37         m_handler = NULL;
38 }
39
40 bool channel_event_handler::handle(int fd, event_condition condition)
41 {
42         message msg;
43
44         if (!m_ch || !m_ch->is_connected())
45                 return false;
46
47         if (condition & (EVENT_HUP)) {
48                 m_ch->disconnect();
49                 delete m_ch;
50                 m_ch = NULL;
51                 return false;
52         }
53
54         if (!m_ch->read_sync(msg)) {
55                 m_ch->disconnect();
56                 delete m_ch;
57                 m_ch = NULL;
58                 return false;
59         }
60
61         return true;
62 }
63
64 void channel_event_handler::connected(channel *ch)
65 {
66         if (m_handler)
67                 m_handler->connected(ch);
68 }
69
70 void channel_event_handler::disconnected(channel *ch)
71 {
72         if (m_handler)
73                 m_handler->disconnected(ch);
74 }
75
76 void channel_event_handler::read(channel *ch, message &msg)
77 {
78         if (m_handler)
79                 m_handler->read(ch, msg);
80 }
81
82 void channel_event_handler::read_complete(channel *ch)
83 {
84         if (m_handler)
85                 m_handler->read_complete(ch);
86 }
87
88 void channel_event_handler::error_caught(channel *ch, int error)
89 {
90         if (m_handler)
91                 m_handler->error_caught(ch, error);
92 }