summaryrefslogtreecommitdiffstats
path: root/gisi/socket.c
diff options
context:
space:
mode:
authorAki Niemi <aki.niemi@nokia.com>2009-06-15 11:17:30 +0300
committerAki Niemi <aki.niemi@nokia.com>2009-06-15 11:27:16 +0300
commite5f0290ddb17f368cf21bc0262a531e67486c409 (patch)
tree6d2c2a7452ed116d5092933c200909044d7df880 /gisi/socket.c
parent3a2821307228f688656b4ddce32d13b02725b5ad (diff)
downloadofono-e5f0290ddb17f368cf21bc0262a531e67486c409.tar.bz2
Rename ISI client and PhoNet netlink APIs
- Add g_ prefix to functions - Add G-prefix and use CamelCasing in types
Diffstat (limited to 'gisi/socket.c')
-rw-r--r--gisi/socket.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/gisi/socket.c b/gisi/socket.c
new file mode 100644
index 00000000..6f72120e
--- /dev/null
+++ b/gisi/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 <remi.denis-courmont@nokia.com>
+ *
+ * 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 <config.h>
+#endif
+
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <linux/types.h>
+#include <linux/phonet.h>
+#include <glib.h>
+
+#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;
+}