2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "launchpad_io_channel.h"
24 #include "log_private.h"
26 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
33 io_channel_event_cb callback;
37 struct io_condition_s {
38 io_condition_e io_cond;
39 GIOCondition g_io_cond;
42 static struct io_condition_s __cond_map[] = {
65 .g_io_cond = G_IO_NVAL,
69 static io_condition_e __convert_g_io_condition(GIOCondition cond)
71 io_condition_e condition = 0;
74 for (i = 0; i < ARRAY_SIZE(__cond_map); i++) {
75 if (__cond_map[i].g_io_cond & cond)
76 condition |= __cond_map[i].io_cond;
82 static GIOCondition __convert_io_condition(io_condition_e cond)
84 GIOCondition condition = 0;
87 for (i = 0; i < ARRAY_SIZE(__cond_map); i++) {
88 if (__cond_map[i].io_cond & cond)
89 condition |= __cond_map[i].g_io_cond;
95 static gboolean __io_event_cb(GIOChannel *source, GIOCondition condition,
98 io_channel_h channel = (io_channel_h)data;
99 io_condition_e cond = __convert_g_io_condition(condition);
100 int fd = g_io_channel_unix_get_fd(source);
102 if (!channel->callback(fd, cond, channel->user_data))
103 return G_SOURCE_REMOVE;
105 return G_SOURCE_CONTINUE;
108 io_channel_h _io_channel_create(int fd, io_condition_e cond,
109 io_channel_event_cb callback, void *user_data)
111 struct io_channel_s *channel;
113 if (fd < 3 || !callback) {
114 _E("Invalid parameter");
118 channel = calloc(1, sizeof(struct io_channel_s));
124 channel->io = g_io_channel_unix_new(fd);
126 _E("Failed to create GIOChannel");
127 _io_channel_destroy(channel);
131 channel->tag = g_io_add_watch(channel->io, __convert_io_condition(cond),
132 __io_event_cb, channel);
134 _E("Failed to add GIO watch");
135 _io_channel_destroy(channel);
139 channel->do_close = true;
141 channel->callback = callback;
142 channel->user_data = user_data;
147 void _io_channel_destroy(io_channel_h channel)
153 g_source_remove(channel->tag);
156 g_io_channel_unref(channel->io);
158 if (channel->do_close && channel->fd > 0)
164 int _io_channel_set_close_on_destroy(io_channel_h channel, bool do_close)
167 _E("Invalid parameter");
171 channel->do_close = do_close;