From 667655df7995feda7cd07504f1ba30f397578644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= Date: Tue, 9 Jun 2009 16:26:26 +0300 Subject: [PATCH] Phonet datagram socket helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémi Denis-Courmont --- isi/Makefile.am | 3 +- isi/socket.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ isi/socket.h | 31 +++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 isi/socket.c create mode 100644 isi/socket.h diff --git a/isi/Makefile.am b/isi/Makefile.am index e5e85961..e7e13e7e 100644 --- a/isi/Makefile.am +++ b/isi/Makefile.am @@ -4,5 +4,6 @@ noinst_LTLIBRARIES = libgisi.la MAINTAINERCLEANFILES = Makefile.in libgisi_la_SOURCES = \ - netlink.h netlink.c + netlink.h netlink.c \ + socket.h socket.c libgisi_la_LIBADD = @GLIB_LIBS@ diff --git a/isi/socket.c b/isi/socket.c new file mode 100644 index 00000000..6f72120e --- /dev/null +++ b/isi/socket.c @@ -0,0 +1,93 @@ +/* + * This file is part of oFono - Open Source Telephony + * + * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: Rémi Denis-Courmont + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef AF_PHONET +#define AF_PHONET 35 +#define PF_PHONET AF_PHONET +#endif +#include "socket.h" + +GIOChannel *phonet_new(uint8_t resource) +{ + GIOChannel *channel; + struct sockaddr_pn addr = { + .spn_family = AF_PHONET, + .spn_resource = resource, + }; + + int fd = socket(PF_PHONET, SOCK_DGRAM, 0); + if (fd == -1) + return NULL; + fcntl(fd, F_SETFD, FD_CLOEXEC); + /* Use blocking mode on purpose. */ + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) { + close(fd); + return NULL; + } + + channel = g_io_channel_unix_new(fd); + g_io_channel_set_close_on_unref(channel, TRUE); + g_io_channel_set_encoding(channel, NULL, NULL); + g_io_channel_set_buffered(channel, FALSE); + return channel; +} + +size_t phonet_peek_length(GIOChannel *channel) +{ + int len; + int fd = g_io_channel_unix_get_fd(channel); + return ioctl(fd, FIONREAD, &len) ? 0 : len; +} + +ssize_t phonet_read(GIOChannel *channel, void *restrict buf, size_t len, + uint16_t *restrict obj, uint8_t *restrict res) +{ + struct sockaddr_pn addr; + socklen_t addrlen = sizeof(addr); + ssize_t ret; + + ret = recvfrom(g_io_channel_unix_get_fd(channel), buf, len, + MSG_DONTWAIT, (struct sockaddr *)&addr, &addrlen); + if (ret == -1) + return -1; + + if (obj != NULL) + *obj = pn_sockaddr_get_object(&addr); + if (res != NULL) + *res = addr.spn_resource; + return ret; +} diff --git a/isi/socket.h b/isi/socket.h new file mode 100644 index 00000000..206154e5 --- /dev/null +++ b/isi/socket.h @@ -0,0 +1,31 @@ +/* + * This file is part of oFono - Open Source Telephony + * + * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). + * + * Contact: Rémi Denis-Courmont + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA + * + */ + +#ifndef OFONO_PHONET_SOCKET_H +#define OFONO_PHONET_SOCKET_H 1 + +GIOChannel *phonet_new(uint8_t resource); +size_t phonet_peek_length(GIOChannel *); +ssize_t phonet_read(GIOChannel *, void *restrict, size_t, uint16_t *restrict, + uint8_t *restrict); +#endif