From 3ddc7ab7c3d997a93719185d39af5a1ba3a77f91 Mon Sep 17 00:00:00 2001 From: Pekka Pessi Date: Tue, 13 Apr 2010 15:02:49 +0300 Subject: [PATCH] gisi: add gisi server API --- Makefile.am | 1 + gisi/phonet.h | 2 + gisi/server.c | 349 ++++++++++++++++++++++++++++++++++++++++++++++++++ gisi/server.h | 72 +++++++++++ 4 files changed, 424 insertions(+) create mode 100644 gisi/server.c create mode 100644 gisi/server.h diff --git a/Makefile.am b/Makefile.am index a3438e9f..50b7d629 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,7 @@ gdbus_sources = gdbus/gdbus.h gdbus/mainloop.c gdbus/object.c gdbus/watch.c gisi_sources = gisi/phonet.h gisi/modem.h gisi/netlink.h gisi/netlink.c \ gisi/socket.h gisi/socket.c gisi/client.h gisi/client.c \ + gisi/server.h gisi/server.c \ gisi/pep.h gisi/pep.c gisi/pipe.h gisi/pipe.c gisi/iter.h \ gisi/iter.c gisi/verify.c diff --git a/gisi/phonet.h b/gisi/phonet.h index 6456e40b..c418970c 100644 --- a/gisi/phonet.h +++ b/gisi/phonet.h @@ -41,6 +41,8 @@ #define PNPIPE_ENCAP_IP 1 #define SIOCPNGETOBJECT (SIOCPROTOPRIVATE + 0) +#define SIOCPNADDRESOURCE (SIOCPROTOPRIVATE + 14) +#define SIOCPNDELRESOURCE (SIOCPROTOPRIVATE + 15) struct sockaddr_pn { sa_family_t spn_family; diff --git a/gisi/server.c b/gisi/server.c new file mode 100644 index 00000000..ef2d5dd0 --- /dev/null +++ b/gisi/server.c @@ -0,0 +1,349 @@ +/* + * This file is part of oFono - Open Source Telephony + * + * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). + * + * 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 +#include "phonet.h" +#include + +#include "socket.h" +#include "server.h" + +#define PN_NAMESERVICE 0xDB +#define PNS_NAME_ADD_REQ 0x05 + +struct _GIsiIncoming +{ + struct sockaddr_pn spn; + uint8_t id; +}; + +struct _GIsiServer { + GIsiModem *modem; + uint8_t resource; + struct { + int major; + int minor; + } version; + + /* Callbacks */ + int fd; + guint source; + GIsiRequestFunc func[256]; + void *data[256]; + + GIsiIncoming irq[1]; + + /* Debugging */ + GIsiDebugFunc debug_func; + void *debug_data; +}; + +static gboolean g_isi_server_callback(GIOChannel *channel, GIOCondition cond, + gpointer data); + +/** + * Create an ISI server. + * @param resource PhoNet resource ID for the server + * @return NULL on error (see errno), a GIsiServer pointer on success, + */ +GIsiServer *g_isi_server_create(GIsiModem *modem, uint8_t resource, + uint8_t major, uint8_t minor) +{ + void *ptr; + GIsiServer *self; + GIOChannel *channel; + + if (G_UNLIKELY(posix_memalign(&ptr, 256, sizeof(*self)))) + abort(); + + self = ptr; + memset (self, 0, sizeof *self); + self->resource = resource; + self->version.major = major; + self->version.minor = minor; + self->modem = modem; + self->debug_func = NULL; + + channel = phonet_new(modem, resource); + if (channel == NULL) { + free(self); + return NULL; + } + + self->fd = g_io_channel_unix_get_fd(channel); + self->source = g_io_add_watch(channel, + G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL, + g_isi_server_callback, self); + g_io_channel_unref(channel); + return self; +} + +/** + * Returns the resource associated with @a server + * @param server server for the resource + * @return PhoNet resource ID for the server + */ +uint8_t g_isi_server_resource(GIsiServer *server) +{ + return server->resource; +} + +/** + * Set a debugging function for @a server. This function will be + * called whenever an ISI protocol message is sent or received. + * @param server server to debug + * @param func debug function + * @param opaque user data + */ +void g_isi_server_set_debug(GIsiServer *server, GIsiDebugFunc func, + void *opaque) +{ + if (!server) + return; + + server->debug_func = func; + server->debug_data = opaque; +} + +/** + * Destroys an ISI server, cancels all pending transactions and subscriptions. + * @param server server to destroy + */ +void g_isi_server_destroy(GIsiServer *server) +{ + if (!server) + return; + + g_source_remove(server->source); + free(server); +} + +/** + * Request the server name from the name server. + */ +void +g_isi_server_add_name(GIsiServer *self) +{ + uint16_t object = 0; + + if (!self) + return; + + if (ioctl(self->fd, SIOCPNGETOBJECT, &object) < 0) { + g_warning("%s: %s", "ioctl(SIOCPNGETOBJECT)", strerror(errno)); + } else { + struct sockaddr_pn spn = { + .spn_family = PF_PHONET, + .spn_dev = 0, /* PN_DEV_HOST */ + .spn_resource = PN_NAMESERVICE, + }; + uint8_t req[] = { + 0, PNS_NAME_ADD_REQ, 0, 0, + 0, 0, 0, self->resource, /* name */ + object >> 8, object & 0xff, /* device/object */ + 0, 0, + }; + + if (sendto(self->fd, req, sizeof(req), 0, (void *)&spn, + sizeof(spn)) != sizeof(spn)) + return; + } +} + +/** + * Make an ISI request and register a callback to process the response(s) to + * the resulting transaction. + * @param self ISI server (from g_isi_server_create()) + * @param buf pointer to request payload + * @param len request payload byte length + * @param irq information from incoming request + */ +int g_isi_respond(GIsiServer *self, const void *data, size_t len, + GIsiIncoming *irq) +{ + const struct iovec iov = { + .iov_base = (void *)data, + .iov_len = len, + }; + + if (self->debug_func) + self->debug_func(data, len, self->debug_data); + + return g_isi_vrespond(self, &iov, 1, irq); +} + +/** + * Make an ISI request and register a callback to process the response(s) to + * the resulting transaction. + * @param self ISI server (from g_isi_server_create()) + * @param iov scatter-gather array to the request payload + * @param iovlen number of vectors in the scatter-gather array + * @param irq information from incoming request + */ +int g_isi_vrespond(GIsiServer *self, const struct iovec *iov, size_t iovlen, + GIsiIncoming *irq) +{ + struct iovec _iov[1 + iovlen]; + const struct msghdr msg = { + .msg_name = (void *)&irq->spn, + .msg_namelen = sizeof(irq->spn), + .msg_iov = (struct iovec *)_iov, + .msg_iovlen = 1 + iovlen, + .msg_control = NULL, + .msg_controllen = 0, + .msg_flags = 0, + }; + ssize_t ret; + size_t i, len; + + if (self == NULL) { + errno = EINVAL; + return -1; + } + + if (irq == NULL) { + errno = EINVAL; + return -1; + } + + _iov[0].iov_base = &irq->id; + _iov[0].iov_len = 1; + for (i = 0, len = 1; i < iovlen; i++) { + _iov[1 + i] = iov[i]; + len += iov[i].iov_len; + } + + ret = sendmsg(self->fd, &msg, MSG_NOSIGNAL); + + if (irq != self->irq) + g_free(irq); + + return ret; +} + +/** + * Prepare to handle given request type for the resource that an ISI server + * is associated with. If the same type was already handled, the old + * handler is overriden. + * @param self ISI server (fomr g_isi_server_create()) + * @param type request message type + * @param cb callback to process received requests + * @param data data for the callback + * @return 0 on success, -1 upon an error. + */ +int g_isi_server_handle(GIsiServer *self, uint8_t type, + GIsiRequestFunc cb, void *data) +{ + if (self == NULL || cb == NULL) { + errno = EINVAL; + return -1; + } + + self->func[type] = cb; + self->data[type] = data; + return 0; +} + +/** + * Remove handler from a given request type. + * @param server ISI server (from g_isi_server_create()) + * @param type indication type. + */ +void g_isi_server_unhandle(GIsiServer *self, uint8_t type) +{ + if (self) + self->func[type] = NULL; +} + +/* Data callback */ +static gboolean g_isi_server_callback(GIOChannel *channel, GIOCondition cond, + gpointer opaque) +{ + GIsiServer *self = opaque; + int len; + GIsiIncoming *irq = self->irq; + struct sockaddr_pn *addr = &irq->spn; + socklen_t addrlen = sizeof(irq->spn); + + if (cond & (G_IO_NVAL|G_IO_HUP)) { + g_warning("Unexpected event on Phonet channel %p", channel); + return FALSE; + } + + len = phonet_peek_length(channel); + { + uint32_t buf[(len + 3) / 4]; + uint8_t *msg; + uint8_t id; + uint8_t failure; + len = recvfrom(self->fd, buf, len, MSG_DONTWAIT, + (void *)addr, &addrlen); + + if (len < 2 || irq->spn.spn_resource != self->resource) + return TRUE; + + msg = (uint8_t *)buf; + + if (self->debug_func) + self->debug_func(msg + 1, len - 1, self->debug_data); + + irq->id = id = msg[1]; + + if (self->func[id]) { + irq = g_new0(GIsiIncoming, 1); + + if (irq) { + *irq = *self->irq; + self->func[id](self, msg + 1, len - 1, + irq, self->data[id]); + return TRUE; + } + g_free(irq); + failure = 0x14; + + } else { + + failure = 0x17; + + { + uint8_t common[] = { + 0xF0, failure, msg[1] + }; + g_isi_respond(self, common, sizeof(common), + irq); + } + } + } + + return TRUE; +} diff --git a/gisi/server.h b/gisi/server.h new file mode 100644 index 00000000..080573fe --- /dev/null +++ b/gisi/server.h @@ -0,0 +1,72 @@ +/* + * This file is part of oFono - Open Source Telephony + * + * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). + * + * 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_SERVER_H +#define __GISI_SERVER_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +struct _GIsiServer; +typedef struct _GIsiServer GIsiServer; + +struct _GIsiIncoming; +typedef struct _GIsiIncoming GIsiIncoming; + +typedef bool (*GIsiRequestFunc)(GIsiServer *server, + const void *restrict data, size_t len, + GIsiIncoming *, void *opaque); + +GIsiServer *g_isi_server_create(GIsiModem *modem, uint8_t resource, + uint8_t major, uint8_t minor); + +uint8_t g_isi_server_resource(GIsiServer *server); + +void g_isi_server_set_debug(GIsiServer *server, GIsiDebugFunc func, + void *opaque); + +void g_isi_server_destroy(GIsiServer *server); + +void g_isi_server_add_name(GIsiServer *self); + +int g_isi_respond(GIsiServer *server, const void *data, size_t len, + GIsiIncoming *irq); + +struct iovec; + +int g_isi_vrespond(GIsiServer *server, const struct iovec *iov, + size_t iovlen, GIsiIncoming *irq); + +int g_isi_server_handle(GIsiServer *server, uint8_t type, + GIsiRequestFunc func, void *opaque); + +void g_isi_server_unhandle(GIsiServer *server, uint8_t type); + +#ifdef __cplusplus +} +#endif + +#endif /* __GISI_SERVER_H */