gisi: low-level modem scoping for sockets

This commit is contained in:
Rémi Denis-Courmont 2009-08-19 15:55:47 +03:00 committed by Aki Niemi
parent 21588cc5f8
commit b2ee53a99f
5 changed files with 49 additions and 10 deletions

View File

@ -1,10 +1,10 @@
AM_CFLAGS = @GLIB_CFLAGS@
AM_CFLAGS = @GLIB_CFLAGS@ -I$(top_srcdir)
noinst_LTLIBRARIES = libgisi.la
MAINTAINERCLEANFILES = Makefile.in
libgisi_la_SOURCES = \
phonet.h \
phonet.h isicommon.h \
netlink.h netlink.c \
socket.h socket.c \
client.h client.c \

View File

@ -112,7 +112,7 @@ GIsiClient *g_isi_client_create(uint8_t resource)
cl->next[254] = 0;
cl->prev[255] = cl->next[255] = 255;
channel = phonet_new(resource);
channel = phonet_new(NULL, resource);
if (channel == NULL) {
free(cl);
return NULL;
@ -264,7 +264,7 @@ static int g_isi_indication_init(GIsiClient *cl)
uint8_t msg[] = {
0, PNS_SUBSCRIBED_RESOURCES_IND, 1, cl->resource,
};
GIOChannel *channel = phonet_new(PN_COMMGR);
GIOChannel *channel = phonet_new(NULL, PN_COMMGR);
if (channel == NULL)
return errno;

28
gisi/modem.h Normal file
View File

@ -0,0 +1,28 @@
/**
* Copyright (C) 2009 Nokia Corporation. All rights reserved.
*
* 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 GISI_MODEM_H
#define GISI_MODEM_H
typedef struct _GIsiModem GIsiModem;
static inline unsigned g_isi_modem_index(GIsiModem *m)
{
return (uintptr_t)m;
}
#endif

View File

@ -30,35 +30,44 @@
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <net/if.h>
#include <fcntl.h>
#include "modem.h"
#include "phonet.h"
#include <glib.h>
#include "socket.h"
GIOChannel *phonet_new(uint8_t resource)
GIOChannel *phonet_new(GIsiModem *modem, uint8_t resource)
{
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 (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
close(fd);
return NULL;
}
if (if_indextoname(ifi, buf) == NULL ||
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, buf, IF_NAMESIZE))
goto error;
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)))
goto error;
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;
error:
close(fd);
return NULL;
}
size_t phonet_peek_length(GIOChannel *channel)

View File

@ -21,7 +21,9 @@
*
*/
GIOChannel *phonet_new(uint8_t resource);
#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);