summaryrefslogtreecommitdiffstats
path: root/gisi
diff options
context:
space:
mode:
authorAki Niemi <aki.niemi@nokia.com>2010-11-14 18:30:50 +0200
committerAki Niemi <aki.niemi@nokia.com>2010-12-22 17:13:46 +0200
commit0cc1bbd86505b1aee1b08f7baef075da077ee0f4 (patch)
treed041db95c56a5cc0d499be8d50a725a159043d53 /gisi
parent59e61fdd6a95f1063111e2afa6f47a2267ef7456 (diff)
downloadofono-0cc1bbd86505b1aee1b08f7baef075da077ee0f4.tar.bz2
gisi: Adapt and refactor socket module
Clean up and fix naming.
Diffstat (limited to 'gisi')
-rw-r--r--gisi/socket.c32
-rw-r--r--gisi/socket.h10
2 files changed, 18 insertions, 24 deletions
diff --git a/gisi/socket.c b/gisi/socket.c
index 4eb8b773..758e4241 100644
--- a/gisi/socket.c
+++ b/gisi/socket.c
@@ -30,33 +30,32 @@
#include <unistd.h>
#include <net/if.h>
#include <fcntl.h>
-#include "modem.h"
-#include "phonet.h"
#include <glib.h>
+#include "phonet.h"
#include "socket.h"
-GIOChannel *phonet_new(GIsiModem *modem, uint8_t resource)
+GIOChannel *g_isi_phonet_new(unsigned ifindex)
{
GIOChannel *channel;
struct sockaddr_pn addr = {
.spn_family = AF_PHONET,
- .spn_resource = resource,
};
- unsigned ifi = g_isi_modem_index(modem);
char buf[IF_NAMESIZE];
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 (ifi == 0)
- g_warning("Unspecified GIsiModem!");
- else if (if_indextoname(ifi, buf) == NULL ||
+ if (ifindex == 0)
+ g_warning("Unspecified modem interface index");
+ else if (if_indextoname(ifindex, buf) == NULL ||
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, IF_NAMESIZE))
goto error;
+
if (bind(fd, (void *)&addr, sizeof(addr)))
goto error;
@@ -65,33 +64,30 @@ GIOChannel *phonet_new(GIsiModem *modem, uint8_t resource)
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, FALSE);
return channel;
+
error:
close(fd);
return NULL;
}
-size_t phonet_peek_length(GIOChannel *channel)
+size_t g_isi_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)
+ssize_t g_isi_phonet_read(GIOChannel *channel, void *restrict buf, size_t len,
+ struct sockaddr_pn *addr)
{
- struct sockaddr_pn addr;
- socklen_t addrlen = sizeof(addr);
+ socklen_t addrlen = sizeof(struct sockaddr_pn);
ssize_t ret;
ret = recvfrom(g_io_channel_unix_get_fd(channel), buf, len,
- MSG_DONTWAIT, (void *)&addr, &addrlen);
+ MSG_DONTWAIT, (void *)addr, &addrlen);
if (ret == -1)
return -1;
- if (obj != NULL)
- *obj = (addr.spn_dev << 8) | addr.spn_obj;
- if (res != NULL)
- *res = addr.spn_resource;
return ret;
}
diff --git a/gisi/socket.h b/gisi/socket.h
index e2618092..d983e4b9 100644
--- a/gisi/socket.h
+++ b/gisi/socket.h
@@ -19,9 +19,7 @@
*
*/
-#include "modem.h"
-
-GIOChannel *phonet_new(GIsiModem *, uint8_t resource);
-size_t phonet_peek_length(GIOChannel *io);
-ssize_t phonet_read(GIOChannel *io, void *restrict buf, size_t len,
- uint16_t *restrict obj, uint8_t *restrict res);
+GIOChannel *g_isi_phonet_new(unsigned int ifindex);
+size_t g_isi_phonet_peek_length(GIOChannel *io);
+ssize_t g_isi_phonet_read(GIOChannel *io, void *restrict buf, size_t len,
+ struct sockaddr_pn *addr);