2016-10-17 18:29:05 +02:00
|
|
|
/* Simple Plugin API
|
|
|
|
|
* Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library 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
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
#include "serialize.h"
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
#define MAX_BUFFER_SIZE 1024
|
2016-10-17 18:29:05 +02:00
|
|
|
#define MAX_FDS 28
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2016-10-18 11:11:38 +02:00
|
|
|
uint8_t *buffer_data;
|
|
|
|
|
size_t buffer_size;
|
|
|
|
|
size_t buffer_maxsize;
|
|
|
|
|
int fds[MAX_FDS];
|
|
|
|
|
unsigned int n_fds;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmd cmd;
|
2016-10-17 18:29:05 +02:00
|
|
|
off_t offset;
|
|
|
|
|
void *data;
|
|
|
|
|
size_t size;
|
2016-10-18 11:11:38 +02:00
|
|
|
|
|
|
|
|
bool update;
|
2016-10-17 18:29:05 +02:00
|
|
|
} ConnectionBuffer;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
struct _PinosConnection {
|
2016-10-17 18:29:05 +02:00
|
|
|
ConnectionBuffer in, out;
|
|
|
|
|
int fd;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#if 0
|
2016-10-18 18:19:04 +02:00
|
|
|
#define PINOS_DEBUG_CONTROL(format,args...) g_debug(format,##args)
|
2016-10-17 18:29:05 +02:00
|
|
|
#else
|
2016-10-18 18:19:04 +02:00
|
|
|
#define PINOS_DEBUG_CONTROL(format,args...)
|
2016-10-17 18:29:05 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
|
read_length (uint8_t * data, unsigned int size, size_t * length, size_t * skip)
|
|
|
|
|
{
|
|
|
|
|
uint8_t b;
|
|
|
|
|
|
|
|
|
|
/* start reading the length, we need this to skip to the data later */
|
2016-10-18 11:11:38 +02:00
|
|
|
*length = *skip = 0;
|
2016-10-17 18:29:05 +02:00
|
|
|
do {
|
2016-10-18 11:11:38 +02:00
|
|
|
if (*skip >= size)
|
2016-10-17 18:29:05 +02:00
|
|
|
return false;
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
b = data[(*skip)++];
|
|
|
|
|
*length = (*length << 7) | (b & 0x7f);
|
2016-10-17 18:29:05 +02:00
|
|
|
} while (b & 0x80);
|
|
|
|
|
|
|
|
|
|
/* check remaining command size */
|
2016-10-18 11:11:38 +02:00
|
|
|
if (size - *skip < *length)
|
2016-10-17 18:29:05 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_parse_node_update (PinosConnection *conn, PinosControlCmdNodeUpdate *nu)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (nu, conn->in.data, sizeof (PinosControlCmdNodeUpdate));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (nu->props)
|
2016-10-19 17:18:30 +02:00
|
|
|
nu->props = pinos_serialize_props_deserialize (conn->in.data, SPA_PTR_TO_INT (nu->props));
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_parse_port_update (PinosConnection *conn, PinosControlCmdPortUpdate *pu)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
void *p;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (pu, conn->in.data, sizeof (PinosControlCmdPortUpdate));
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
p = conn->in.data;
|
|
|
|
|
|
|
|
|
|
if (pu->possible_formats)
|
|
|
|
|
pu->possible_formats = SPA_MEMBER (p,
|
|
|
|
|
SPA_PTR_TO_INT (pu->possible_formats), SpaFormat *);
|
|
|
|
|
for (i = 0; i < pu->n_possible_formats; i++) {
|
|
|
|
|
if (pu->possible_formats[i]) {
|
2016-10-19 17:18:30 +02:00
|
|
|
pu->possible_formats[i] = pinos_serialize_format_deserialize (p,
|
2016-10-17 18:29:05 +02:00
|
|
|
SPA_PTR_TO_INT (pu->possible_formats[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pu->format)
|
2016-10-19 17:18:30 +02:00
|
|
|
pu->format = pinos_serialize_format_deserialize (p, SPA_PTR_TO_INT (pu->format));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (pu->props)
|
2016-10-19 17:18:30 +02:00
|
|
|
pu->props = pinos_serialize_props_deserialize (p, SPA_PTR_TO_INT (pu->props));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (pu->info)
|
2016-10-19 17:18:30 +02:00
|
|
|
pu->info = pinos_serialize_port_info_deserialize (p, SPA_PTR_TO_INT (pu->info));
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_parse_set_format (PinosConnection *conn, PinosControlCmdSetFormat *cmd)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (cmd, conn->in.data, sizeof (PinosControlCmdSetFormat));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (cmd->format)
|
2016-10-19 17:18:30 +02:00
|
|
|
cmd->format = pinos_serialize_format_deserialize (conn->in.data, SPA_PTR_TO_INT (cmd->format));
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_parse_use_buffers (PinosConnection *conn, PinosControlCmdUseBuffers *cmd)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
|
|
p = conn->in.data;
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (cmd, p, sizeof (PinosControlCmdUseBuffers));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (cmd->buffers)
|
2016-10-18 18:19:04 +02:00
|
|
|
cmd->buffers = SPA_MEMBER (p, SPA_PTR_TO_INT (cmd->buffers), PinosControlMemRef);
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_parse_node_event (PinosConnection *conn, PinosControlCmdNodeEvent *cmd)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
void *p = conn->in.data;
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (cmd, p, sizeof (PinosControlCmdNodeEvent));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (cmd->event)
|
|
|
|
|
cmd->event = SPA_MEMBER (p, SPA_PTR_TO_INT (cmd->event), SpaNodeEvent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_parse_node_command (PinosConnection *conn, PinosControlCmdNodeCommand *cmd)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
void *p = conn->in.data;
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (cmd, p, sizeof (PinosControlCmdNodeCommand));
|
2016-10-17 18:29:05 +02:00
|
|
|
if (cmd->command)
|
|
|
|
|
cmd->command = SPA_MEMBER (p, SPA_PTR_TO_INT (cmd->command), SpaNodeCommand);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
static void
|
|
|
|
|
connection_parse_port_command (PinosConnection *conn, PinosControlCmdPortCommand *cmd)
|
|
|
|
|
{
|
|
|
|
|
void *p = conn->in.data;
|
|
|
|
|
memcpy (cmd, p, sizeof (PinosControlCmdPortCommand));
|
|
|
|
|
if (cmd->command)
|
|
|
|
|
cmd->command = SPA_MEMBER (p, SPA_PTR_TO_INT (cmd->command), SpaNodeCommand);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 18:29:05 +02:00
|
|
|
static void *
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_ensure_size (PinosConnection *conn, ConnectionBuffer *buf, size_t size)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 11:11:38 +02:00
|
|
|
if (buf->buffer_size + size > buf->buffer_maxsize) {
|
|
|
|
|
buf->buffer_maxsize = buf->buffer_size + MAX_BUFFER_SIZE * ((size + MAX_BUFFER_SIZE-1) / MAX_BUFFER_SIZE);
|
2016-10-19 17:18:30 +02:00
|
|
|
g_debug ("connection %p: resize buffer to %zd", conn, buf->buffer_maxsize);
|
2016-10-25 11:15:15 +02:00
|
|
|
buf->buffer_data = realloc (buf->buffer_data, buf->buffer_maxsize);
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
2016-10-18 11:11:38 +02:00
|
|
|
return (uint8_t *) buf->buffer_data + buf->buffer_size;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void *
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_cmd (PinosConnection *conn, PinosControlCmd cmd, size_t size)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
uint8_t *p;
|
|
|
|
|
unsigned int plen;
|
2016-10-18 11:11:38 +02:00
|
|
|
ConnectionBuffer *buf = &conn->out;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
plen = 1;
|
|
|
|
|
while (size >> (7 * plen))
|
|
|
|
|
plen++;
|
|
|
|
|
|
|
|
|
|
/* 1 for cmd, plen for size and size for payload */
|
2016-10-18 11:11:38 +02:00
|
|
|
p = connection_ensure_size (conn, buf, 1 + plen + size);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->cmd = cmd;
|
|
|
|
|
buf->offset = buf->buffer_size;
|
|
|
|
|
buf->buffer_size += 1 + plen + size;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
*p++ = cmd;
|
|
|
|
|
/* write length */
|
|
|
|
|
while (plen) {
|
|
|
|
|
plen--;
|
|
|
|
|
*p++ = ((plen > 0) ? 0x80 : 0) | ((size >> (7 * plen)) & 0x7f);
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_node_update (PinosConnection *conn, PinosControlCmdNodeUpdate *nu)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
void *p;
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmdNodeUpdate *d;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* calc len */
|
2016-10-18 18:19:04 +02:00
|
|
|
len = sizeof (PinosControlCmdNodeUpdate);
|
2016-10-19 17:18:30 +02:00
|
|
|
len += pinos_serialize_props_get_size (nu->props);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_NODE_UPDATE, len);
|
|
|
|
|
memcpy (p, nu, sizeof (PinosControlCmdNodeUpdate));
|
2016-10-17 18:29:05 +02:00
|
|
|
d = p;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = SPA_MEMBER (d, sizeof (PinosControlCmdNodeUpdate), void);
|
2016-10-17 18:29:05 +02:00
|
|
|
if (nu->props) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len = pinos_serialize_props_serialize (p, nu->props);
|
2016-10-17 18:29:05 +02:00
|
|
|
d->props = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
} else {
|
|
|
|
|
d->props = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_port_update (PinosConnection *conn, PinosControlCmdPortUpdate *pu)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
void *p;
|
|
|
|
|
int i;
|
|
|
|
|
SpaFormat **bfa;
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmdPortUpdate *d;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* calc len */
|
2016-10-18 18:19:04 +02:00
|
|
|
len = sizeof (PinosControlCmdPortUpdate);
|
2016-10-17 18:29:05 +02:00
|
|
|
len += pu->n_possible_formats * sizeof (SpaFormat *);
|
|
|
|
|
for (i = 0; i < pu->n_possible_formats; i++) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len += pinos_serialize_format_get_size (pu->possible_formats[i]);
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
2016-10-19 17:18:30 +02:00
|
|
|
len += pinos_serialize_format_get_size (pu->format);
|
|
|
|
|
len += pinos_serialize_props_get_size (pu->props);
|
2016-10-17 18:29:05 +02:00
|
|
|
if (pu->info)
|
2016-10-19 17:18:30 +02:00
|
|
|
len += pinos_serialize_port_info_get_size (pu->info);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_PORT_UPDATE, len);
|
|
|
|
|
memcpy (p, pu, sizeof (PinosControlCmdPortUpdate));
|
2016-10-17 18:29:05 +02:00
|
|
|
d = p;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = SPA_MEMBER (d, sizeof (PinosControlCmdPortUpdate), void);
|
2016-10-17 18:29:05 +02:00
|
|
|
bfa = p;
|
|
|
|
|
if (pu->n_possible_formats)
|
|
|
|
|
d->possible_formats = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
else
|
|
|
|
|
d->possible_formats = 0;
|
|
|
|
|
|
|
|
|
|
p = SPA_MEMBER (p, sizeof (SpaFormat*) * pu->n_possible_formats, void);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < pu->n_possible_formats; i++) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len = pinos_serialize_format_serialize (p, pu->possible_formats[i]);
|
2016-10-17 18:29:05 +02:00
|
|
|
bfa[i] = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
p = SPA_MEMBER (p, len, void);
|
|
|
|
|
}
|
|
|
|
|
if (pu->format) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len = pinos_serialize_format_serialize (p, pu->format);
|
2016-10-17 18:29:05 +02:00
|
|
|
d->format = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
p = SPA_MEMBER (p, len, void);
|
|
|
|
|
} else {
|
|
|
|
|
d->format = 0;
|
|
|
|
|
}
|
|
|
|
|
if (pu->props) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len = pinos_serialize_props_serialize (p, pu->props);
|
2016-10-17 18:29:05 +02:00
|
|
|
d->props = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
p = SPA_MEMBER (p, len, void);
|
|
|
|
|
} else {
|
|
|
|
|
d->props = 0;
|
|
|
|
|
}
|
|
|
|
|
if (pu->info) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len = pinos_serialize_port_info_serialize (p, pu->info);
|
2016-10-17 18:29:05 +02:00
|
|
|
d->info = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
p = SPA_MEMBER (p, len, void);
|
|
|
|
|
} else {
|
|
|
|
|
d->info = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_set_format (PinosConnection *conn, PinosControlCmdSetFormat *sf)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
|
|
/* calculate length */
|
|
|
|
|
/* port_id + format + mask */
|
2016-10-19 17:18:30 +02:00
|
|
|
len = sizeof (PinosControlCmdSetFormat) + pinos_serialize_format_get_size (sf->format);
|
2016-10-18 18:19:04 +02:00
|
|
|
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_SET_FORMAT, len);
|
|
|
|
|
memcpy (p, sf, sizeof (PinosControlCmdSetFormat));
|
2016-10-17 18:29:05 +02:00
|
|
|
sf = p;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = SPA_MEMBER (sf, sizeof (PinosControlCmdSetFormat), void);
|
2016-10-17 18:29:05 +02:00
|
|
|
if (sf->format) {
|
2016-10-19 17:18:30 +02:00
|
|
|
len = pinos_serialize_format_serialize (p, sf->format);
|
2016-10-17 18:29:05 +02:00
|
|
|
sf->format = SPA_INT_TO_PTR (SPA_PTRDIFF (p, sf));
|
|
|
|
|
} else
|
|
|
|
|
sf->format = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_use_buffers (PinosConnection *conn, PinosControlCmdUseBuffers *ub)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
int i;
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmdUseBuffers *d;
|
|
|
|
|
PinosControlMemRef *mr;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* calculate length */
|
2016-10-18 18:19:04 +02:00
|
|
|
len = sizeof (PinosControlCmdUseBuffers);
|
|
|
|
|
len += ub->n_buffers * sizeof (PinosControlMemRef);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
d = connection_add_cmd (conn, PINOS_CONTROL_CMD_USE_BUFFERS, len);
|
|
|
|
|
memcpy (d, ub, sizeof (PinosControlCmdUseBuffers));
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
mr = SPA_MEMBER (d, sizeof (PinosControlCmdUseBuffers), void);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
if (d->n_buffers)
|
|
|
|
|
d->buffers = SPA_INT_TO_PTR (SPA_PTRDIFF (mr, d));
|
|
|
|
|
else
|
|
|
|
|
d->buffers = 0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < ub->n_buffers; i++)
|
2016-10-18 18:19:04 +02:00
|
|
|
memcpy (&mr[i], &ub->buffers[i], sizeof (PinosControlMemRef));
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_node_event (PinosConnection *conn, PinosControlCmdNodeEvent *ev)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
void *p;
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmdNodeEvent *d;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* calculate length */
|
2016-10-18 18:19:04 +02:00
|
|
|
len = sizeof (PinosControlCmdNodeEvent);
|
2016-10-17 18:29:05 +02:00
|
|
|
len += ev->event->size;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_NODE_EVENT, len);
|
|
|
|
|
memcpy (p, ev, sizeof (PinosControlCmdNodeEvent));
|
2016-10-17 18:29:05 +02:00
|
|
|
d = p;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = SPA_MEMBER (d, sizeof (PinosControlCmdNodeEvent), void);
|
2016-10-17 18:29:05 +02:00
|
|
|
d->event = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
|
2016-10-24 15:30:15 +02:00
|
|
|
memcpy (p, ev->event, ev->event->size);
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2016-10-18 18:19:04 +02:00
|
|
|
connection_add_node_command (PinosConnection *conn, PinosControlCmdNodeCommand *cm)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
void *p;
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmdNodeCommand *d;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* calculate length */
|
2016-10-18 18:19:04 +02:00
|
|
|
len = sizeof (PinosControlCmdNodeCommand);
|
2016-10-17 18:29:05 +02:00
|
|
|
len += cm->command->size;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_NODE_COMMAND, len);
|
|
|
|
|
memcpy (p, cm, sizeof (PinosControlCmdNodeCommand));
|
2016-10-17 18:29:05 +02:00
|
|
|
d = p;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
p = SPA_MEMBER (d, sizeof (PinosControlCmdNodeCommand), void);
|
2016-10-17 18:29:05 +02:00
|
|
|
d->command = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
|
2016-10-24 15:30:15 +02:00
|
|
|
memcpy (p, cm->command, cm->command->size);
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
static void
|
|
|
|
|
connection_add_port_command (PinosConnection *conn, PinosControlCmdPortCommand *cm)
|
|
|
|
|
{
|
|
|
|
|
size_t len;
|
|
|
|
|
void *p;
|
|
|
|
|
PinosControlCmdPortCommand *d;
|
|
|
|
|
|
|
|
|
|
/* calculate length */
|
|
|
|
|
len = sizeof (PinosControlCmdPortCommand);
|
|
|
|
|
len += cm->command->size;
|
|
|
|
|
|
|
|
|
|
p = connection_add_cmd (conn, PINOS_CONTROL_CMD_PORT_COMMAND, len);
|
|
|
|
|
memcpy (p, cm, sizeof (PinosControlCmdPortCommand));
|
|
|
|
|
d = p;
|
|
|
|
|
|
|
|
|
|
p = SPA_MEMBER (d, sizeof (PinosControlCmdPortCommand), void);
|
|
|
|
|
d->command = SPA_INT_TO_PTR (SPA_PTRDIFF (p, d));
|
|
|
|
|
|
|
|
|
|
memcpy (p, cm->command, cm->command->size);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
static gboolean
|
|
|
|
|
refill_buffer (PinosConnection *conn, ConnectionBuffer *buf)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
ssize_t len;
|
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
struct msghdr msg = {0};
|
|
|
|
|
struct iovec iov[1];
|
|
|
|
|
char cmsgbuf[CMSG_SPACE (MAX_FDS * sizeof (int))];
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
iov[0].iov_base = buf->buffer_data + buf->buffer_size;
|
|
|
|
|
iov[0].iov_len = buf->buffer_maxsize - buf->buffer_size;
|
2016-10-17 18:29:05 +02:00
|
|
|
msg.msg_iov = iov;
|
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
|
msg.msg_controllen = sizeof (cmsgbuf);
|
|
|
|
|
msg.msg_flags = MSG_CMSG_CLOEXEC;
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
len = recvmsg (conn->fd, &msg, msg.msg_flags);
|
|
|
|
|
if (len < 0) {
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
else
|
|
|
|
|
goto recv_error;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (len < 4)
|
2016-10-18 18:19:04 +02:00
|
|
|
return FALSE;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->buffer_size += len;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* handle control messages */
|
|
|
|
|
for (cmsg = CMSG_FIRSTHDR (&msg); cmsg != NULL; cmsg = CMSG_NXTHDR (&msg, cmsg)) {
|
|
|
|
|
if (cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS)
|
|
|
|
|
continue;
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->n_fds = (cmsg->cmsg_len - ((char *)CMSG_DATA (cmsg) - (char *)cmsg)) / sizeof (int);
|
|
|
|
|
memcpy (buf->fds, CMSG_DATA (cmsg), buf->n_fds * sizeof (int));
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
2016-10-20 16:26:55 +02:00
|
|
|
PINOS_DEBUG_CONTROL ("connection %p: %d read %zd bytes and %d fds", conn, conn->fd, len, buf->n_fds);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
|
recv_error:
|
|
|
|
|
{
|
2016-10-19 17:18:30 +02:00
|
|
|
g_warning ("could not recvmsg on fd %d: %s", conn->fd, strerror (errno));
|
2016-10-18 18:19:04 +02:00
|
|
|
return FALSE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
static void
|
|
|
|
|
clear_buffer (ConnectionBuffer *buf)
|
|
|
|
|
{
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < buf->n_fds; i++) {
|
|
|
|
|
if (buf->fds[i] > 0) {
|
|
|
|
|
if (close (buf->fds[i]) < 0)
|
|
|
|
|
perror ("close");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buf->n_fds = 0;
|
2016-10-18 18:19:04 +02:00
|
|
|
buf->cmd = PINOS_CONTROL_CMD_INVALID;
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->offset = 0;
|
|
|
|
|
buf->size = 0;
|
|
|
|
|
buf->buffer_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosConnection *
|
|
|
|
|
pinos_connection_new (int fd)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosConnection *c;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
c = calloc (1, sizeof (PinosConnection));
|
2016-10-17 18:29:05 +02:00
|
|
|
c->fd = fd;
|
2016-10-18 11:11:38 +02:00
|
|
|
c->out.buffer_data = malloc (MAX_BUFFER_SIZE);
|
|
|
|
|
c->out.buffer_maxsize = MAX_BUFFER_SIZE;
|
|
|
|
|
c->in.buffer_data = malloc (MAX_BUFFER_SIZE);
|
|
|
|
|
c->in.buffer_maxsize = MAX_BUFFER_SIZE;
|
|
|
|
|
c->in.update = true;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
void
|
2016-10-18 18:19:04 +02:00
|
|
|
pinos_connection_free (PinosConnection *conn)
|
2016-10-18 11:11:38 +02:00
|
|
|
{
|
|
|
|
|
free (conn->out.buffer_data);
|
|
|
|
|
free (conn->in.buffer_data);
|
|
|
|
|
free (conn);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 18:29:05 +02:00
|
|
|
/**
|
2016-10-18 18:19:04 +02:00
|
|
|
* pinos_connection_has_next:
|
|
|
|
|
* @iter: a #PinosConnection
|
2016-10-17 18:29:05 +02:00
|
|
|
*
|
|
|
|
|
* Move to the next packet in @conn.
|
|
|
|
|
*
|
2016-10-18 18:19:04 +02:00
|
|
|
* Returns: %TRUE if more packets are available.
|
2016-10-17 18:29:05 +02:00
|
|
|
*/
|
2016-10-18 18:19:04 +02:00
|
|
|
gboolean
|
|
|
|
|
pinos_connection_has_next (PinosConnection *conn)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
size_t len, size, skip;
|
|
|
|
|
uint8_t *data;
|
2016-10-18 11:11:38 +02:00
|
|
|
ConnectionBuffer *buf;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, FALSE);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
buf = &conn->in;
|
|
|
|
|
|
2016-10-17 18:29:05 +02:00
|
|
|
/* move to next packet */
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->offset += buf->size;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
again:
|
|
|
|
|
if (buf->update) {
|
|
|
|
|
refill_buffer (conn, buf);
|
|
|
|
|
buf->update = false;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* now read packet */
|
2016-10-18 11:11:38 +02:00
|
|
|
data = buf->buffer_data;
|
|
|
|
|
size = buf->buffer_size;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
if (buf->offset >= size) {
|
|
|
|
|
clear_buffer (buf);
|
|
|
|
|
buf->update = true;
|
2016-10-18 18:19:04 +02:00
|
|
|
return FALSE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
data += buf->offset;
|
|
|
|
|
size -= buf->offset;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->cmd = *data;
|
2016-10-17 18:29:05 +02:00
|
|
|
data++;
|
|
|
|
|
size--;
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
if (!read_length (data, size, &len, &skip)) {
|
|
|
|
|
connection_ensure_size (conn, buf, len + skip);
|
|
|
|
|
buf->update = true;
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
|
|
|
|
buf->size = len;
|
|
|
|
|
buf->data = data + skip;
|
|
|
|
|
buf->offset += 1 + skip;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
PinosControlCmd
|
|
|
|
|
pinos_connection_get_cmd (PinosConnection *conn)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, PINOS_CONTROL_CMD_INVALID);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
return conn->in.cmd;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
gboolean
|
|
|
|
|
pinos_connection_parse_cmd (PinosConnection *conn,
|
|
|
|
|
gpointer command)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, FALSE);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
switch (conn->in.cmd) {
|
|
|
|
|
/* C -> S */
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_UPDATE:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_parse_node_update (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_PORT_UPDATE:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_parse_port_update (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_PORT_STATUS_CHANGE:
|
2016-10-19 17:18:30 +02:00
|
|
|
g_warning ("implement iter of %d", conn->in.cmd);
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_STATE_CHANGE:
|
|
|
|
|
if (conn->in.size < sizeof (PinosControlCmdNodeStateChange))
|
|
|
|
|
return FALSE;
|
|
|
|
|
memcpy (command, conn->in.data, sizeof (PinosControlCmdNodeStateChange));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* S -> C */
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_ADD_PORT:
|
|
|
|
|
if (conn->in.size < sizeof (PinosControlCmdAddPort))
|
|
|
|
|
return FALSE;
|
|
|
|
|
memcpy (command, conn->in.data, sizeof (PinosControlCmdAddPort));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_REMOVE_PORT:
|
|
|
|
|
if (conn->in.size < sizeof (PinosControlCmdRemovePort))
|
|
|
|
|
return FALSE;
|
|
|
|
|
memcpy (command, conn->in.data, sizeof (PinosControlCmdRemovePort));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_SET_FORMAT:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_parse_set_format (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_SET_PROPERTY:
|
2016-10-19 17:18:30 +02:00
|
|
|
g_warning ("implement iter of %d", conn->in.cmd);
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* bidirectional */
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_ADD_MEM:
|
|
|
|
|
if (conn->in.size < sizeof (PinosControlCmdAddMem))
|
|
|
|
|
return FALSE;
|
|
|
|
|
memcpy (command, conn->in.data, sizeof (PinosControlCmdAddMem));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_USE_BUFFERS:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_parse_use_buffers (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_PROCESS_BUFFER:
|
|
|
|
|
if (conn->in.size < sizeof (PinosControlCmdProcessBuffer))
|
|
|
|
|
return FALSE;
|
|
|
|
|
memcpy (command, conn->in.data, sizeof (PinosControlCmdProcessBuffer));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_EVENT:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_parse_node_event (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_COMMAND:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_parse_node_command (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
case PINOS_CONTROL_CMD_PORT_COMMAND:
|
|
|
|
|
connection_parse_port_command (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_INVALID:
|
|
|
|
|
return FALSE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-18 18:19:04 +02:00
|
|
|
* pinos_connection_get_fd:
|
|
|
|
|
* @conn: a #PinosConnection
|
2016-10-17 18:29:05 +02:00
|
|
|
* @index: an index
|
|
|
|
|
* @steal: steal the fd
|
|
|
|
|
*
|
|
|
|
|
* Get the file descriptor at @index in @conn.
|
|
|
|
|
*
|
|
|
|
|
* Returns: a file descriptor at @index in @conn. The file descriptor
|
|
|
|
|
* is not duplicated in any way. -1 is returned on error.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-10-18 18:19:04 +02:00
|
|
|
pinos_connection_get_fd (PinosConnection *conn,
|
|
|
|
|
guint index,
|
|
|
|
|
gboolean close)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, -1);
|
|
|
|
|
g_return_val_if_fail (index < conn->in.n_fds, -1);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
fd = conn->in.fds[index];
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
fd = -fd;
|
|
|
|
|
conn->in.fds[index] = close ? fd : -fd;
|
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-18 18:19:04 +02:00
|
|
|
* pinos_connection_add_fd:
|
|
|
|
|
* @conn: a #PinosConnection
|
2016-10-17 18:29:05 +02:00
|
|
|
* @fd: a valid fd
|
|
|
|
|
* @close: if the descriptor should be closed when sent
|
|
|
|
|
*
|
|
|
|
|
* Add the file descriptor @fd to @builder.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the index of the file descriptor in @builder.
|
|
|
|
|
*/
|
|
|
|
|
int
|
2016-10-18 18:19:04 +02:00
|
|
|
pinos_connection_add_fd (PinosConnection *conn,
|
|
|
|
|
int fd,
|
|
|
|
|
gboolean close)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
int index, i;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, -1);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
for (i = 0; i < conn->out.n_fds; i++) {
|
|
|
|
|
if (conn->out.fds[i] == fd || conn->out.fds[i] == -fd)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index = conn->out.n_fds;
|
|
|
|
|
conn->out.fds[index] = close ? fd : -fd;
|
|
|
|
|
conn->out.n_fds++;
|
|
|
|
|
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-10-18 18:19:04 +02:00
|
|
|
* pinos_connection_add_cmd:
|
|
|
|
|
* @conn: a #PinosConnection
|
|
|
|
|
* @cmd: a #PinosControlCmd
|
2016-10-17 18:29:05 +02:00
|
|
|
* @command: a command
|
|
|
|
|
*
|
|
|
|
|
* Add a @cmd to @conn with data from @command.
|
|
|
|
|
*
|
2016-10-18 18:19:04 +02:00
|
|
|
* Returns: %TRUE on success.
|
2016-10-17 18:29:05 +02:00
|
|
|
*/
|
2016-10-18 18:19:04 +02:00
|
|
|
gboolean
|
|
|
|
|
pinos_connection_add_cmd (PinosConnection *conn,
|
|
|
|
|
PinosControlCmd cmd,
|
|
|
|
|
gpointer command)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
void *p;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (command != NULL, FALSE);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
|
/* C -> S */
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_UPDATE:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_add_node_update (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_PORT_UPDATE:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_add_port_update (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_PORT_STATUS_CHANGE:
|
2016-10-17 18:29:05 +02:00
|
|
|
p = connection_add_cmd (conn, cmd, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_STATE_CHANGE:
|
|
|
|
|
p = connection_add_cmd (conn, cmd, sizeof (PinosControlCmdNodeStateChange));
|
|
|
|
|
memcpy (p, command, sizeof (PinosControlCmdNodeStateChange));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* S -> C */
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_ADD_PORT:
|
|
|
|
|
p = connection_add_cmd (conn, cmd, sizeof (PinosControlCmdAddPort));
|
|
|
|
|
memcpy (p, command, sizeof (PinosControlCmdAddPort));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_REMOVE_PORT:
|
|
|
|
|
p = connection_add_cmd (conn, cmd, sizeof (PinosControlCmdRemovePort));
|
|
|
|
|
memcpy (p, command, sizeof (PinosControlCmdRemovePort));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_SET_FORMAT:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_add_set_format (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_SET_PROPERTY:
|
2016-10-19 17:18:30 +02:00
|
|
|
g_warning ("implement builder of %d", cmd);
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
/* bidirectional */
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_ADD_MEM:
|
|
|
|
|
p = connection_add_cmd (conn, cmd, sizeof (PinosControlCmdAddMem));
|
|
|
|
|
memcpy (p, command, sizeof (PinosControlCmdAddMem));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_USE_BUFFERS:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_add_use_buffers (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_PROCESS_BUFFER:
|
|
|
|
|
p = connection_add_cmd (conn, cmd, sizeof (PinosControlCmdProcessBuffer));
|
|
|
|
|
memcpy (p, command, sizeof (PinosControlCmdProcessBuffer));
|
2016-10-17 18:29:05 +02:00
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_EVENT:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_add_node_event (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_NODE_COMMAND:
|
2016-10-17 18:29:05 +02:00
|
|
|
connection_add_node_command (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
case PINOS_CONTROL_CMD_PORT_COMMAND:
|
|
|
|
|
connection_add_port_command (conn, command);
|
|
|
|
|
break;
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
case PINOS_CONTROL_CMD_INVALID:
|
|
|
|
|
return FALSE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
gboolean
|
|
|
|
|
pinos_connection_flush (PinosConnection *conn)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
|
|
|
|
ssize_t len;
|
|
|
|
|
struct msghdr msg = {0};
|
|
|
|
|
struct iovec iov[1];
|
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
|
char cmsgbuf[CMSG_SPACE (MAX_FDS * sizeof (int))];
|
|
|
|
|
int *cm, i, fds_len;
|
2016-10-18 11:11:38 +02:00
|
|
|
ConnectionBuffer *buf;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, FALSE);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
buf = &conn->out;
|
|
|
|
|
|
|
|
|
|
if (buf->buffer_size == 0)
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
fds_len = buf->n_fds * sizeof (int);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
iov[0].iov_base = buf->buffer_data;
|
|
|
|
|
iov[0].iov_len = buf->buffer_size;
|
2016-10-17 18:29:05 +02:00
|
|
|
msg.msg_iov = iov;
|
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
if (buf->n_fds > 0) {
|
2016-10-17 18:29:05 +02:00
|
|
|
msg.msg_control = cmsgbuf;
|
|
|
|
|
msg.msg_controllen = CMSG_SPACE (fds_len);
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
|
cmsg->cmsg_len = CMSG_LEN (fds_len);
|
|
|
|
|
cm = (int*)CMSG_DATA (cmsg);
|
2016-10-18 11:11:38 +02:00
|
|
|
for (i = 0; i < buf->n_fds; i++)
|
|
|
|
|
cm[i] = buf->fds[i] > 0 ? buf->fds[i] : -buf->fds[i];
|
2016-10-17 18:29:05 +02:00
|
|
|
msg.msg_controllen = cmsg->cmsg_len;
|
|
|
|
|
} else {
|
|
|
|
|
msg.msg_control = NULL;
|
|
|
|
|
msg.msg_controllen = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
len = sendmsg (conn->fd, &msg, 0);
|
|
|
|
|
if (len < 0) {
|
|
|
|
|
if (errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
else
|
|
|
|
|
goto send_error;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-10-18 11:11:38 +02:00
|
|
|
buf->buffer_size -= len;
|
|
|
|
|
buf->n_fds = 0;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-20 16:26:55 +02:00
|
|
|
PINOS_DEBUG_CONTROL ("connection %p: %d written %zd bytes and %u fds", conn, conn->fd, len, buf->n_fds);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
|
send_error:
|
|
|
|
|
{
|
2016-10-19 17:18:30 +02:00
|
|
|
g_warning ("could not sendmsg: %s", strerror (errno));
|
2016-10-18 18:19:04 +02:00
|
|
|
return FALSE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
gboolean
|
|
|
|
|
pinos_connection_clear (PinosConnection *conn)
|
2016-10-17 18:29:05 +02:00
|
|
|
{
|
2016-10-18 18:19:04 +02:00
|
|
|
g_return_val_if_fail (conn != NULL, FALSE);
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 11:11:38 +02:00
|
|
|
clear_buffer (&conn->out);
|
|
|
|
|
clear_buffer (&conn->in);
|
|
|
|
|
conn->in.update = true;
|
2016-10-17 18:29:05 +02:00
|
|
|
|
2016-10-18 18:19:04 +02:00
|
|
|
return TRUE;
|
2016-10-17 18:29:05 +02:00
|
|
|
}
|