From 18cac67b337abb279a63bf4fcc4180662fbc8579 Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Fri, 24 Aug 2012 15:22:40 +0200 Subject: [PATCH] Introduce transport over UNIX domain sockets. Signed-off-by: Richard Cochran --- makefile | 4 +- transport.c | 3 ++ transport.h | 1 + uds.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ uds.h | 37 +++++++++++++++ 5 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 uds.c create mode 100644 uds.h diff --git a/makefile b/makefile index 4099d51..fe0dcbd 100644 --- a/makefile +++ b/makefile @@ -24,7 +24,7 @@ CFLAGS = -Wall $(INC) $(DEBUG) $(EXTRA_CFLAGS) LDLIBS = -lm -lrt $(EXTRA_LDFLAGS) PRG = ptp4l pmc phc2sys hwstamp_ctl OBJ = bmc.o clock.o config.o fsm.o ptp4l.o mave.o msg.o phc.o pi.o port.o \ - print.o raw.o servo.o sk.o tlv.o tmtab.o transport.o udp.o udp6.o util.o + print.o raw.o servo.o sk.o tlv.o tmtab.o transport.o udp.o udp6.o uds.o util.o OBJECTS = $(OBJ) pmc.o phc2sys.o hwstamp_ctl.o SRC = $(OBJECTS:.o=.c) @@ -36,7 +36,7 @@ all: $(PRG) ptp4l: $(OBJ) -pmc: pmc.o msg.o print.o raw.o sk.o tlv.o transport.o udp.o udp6.o util.o +pmc: pmc.o msg.o print.o raw.o sk.o tlv.o transport.o udp.o udp6.o uds.o util.o phc2sys: phc2sys.o diff --git a/transport.c b/transport.c index b0c80f4..713f018 100644 --- a/transport.c +++ b/transport.c @@ -22,6 +22,7 @@ #include "raw.h" #include "udp.h" #include "udp6.h" +#include "uds.h" int transport_close(struct transport *t, struct fdarray *fda) { @@ -65,6 +66,8 @@ struct transport *transport_create(enum transport_type type) case TRANS_CONTROLNET: case TRANS_PROFINET: break; + case TRANS_UDS: + return uds_transport_create(); } return NULL; } diff --git a/transport.h b/transport.h index 6a50816..caa07a3 100644 --- a/transport.h +++ b/transport.h @@ -31,6 +31,7 @@ enum transport_type { TRANS_DEVICENET, TRANS_CONTROLNET, TRANS_PROFINET, + TRANS_UDS, }; enum timestamp_type { diff --git a/uds.c b/uds.c new file mode 100644 index 0000000..4ca5590 --- /dev/null +++ b/uds.c @@ -0,0 +1,129 @@ +/** + * @file uds.c + * @note Copyright (C) 2012 Richard Cochran + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "contain.h" +#include "print.h" +#include "transport_private.h" +#include "uds.h" + +#define UDS_FILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) /*0660*/ + +struct uds { + struct transport t; + struct sockaddr_un sa; + socklen_t len; +}; + +static int uds_close(struct transport *t, struct fdarray *fda) +{ + close(fda->fd[FD_GENERAL]); + return 0; +} + +static int uds_open(struct transport *t, char *name, struct fdarray *fda, + enum timestamp_type tt) +{ + int fd, err; + struct sockaddr_un sa; + struct uds *uds = container_of(t, struct uds, t); + + fd = socket(AF_LOCAL, SOCK_DGRAM, 0); + if (fd < 0) { + pr_err("uds: failed to create socket: %m"); + return -1; + } + memset(&sa, 0, sizeof(sa)); + sa.sun_family = AF_LOCAL; + strcpy(sa.sun_path, name); + + unlink(name); + + err = bind(fd, (struct sockaddr *) &sa, sizeof(sa)); + if (err < 0) { + pr_err("uds: bind failed: %m"); + close(fd); + return -1; + } + + /* For client use, pre load the server path. */ + memset(&sa, 0, sizeof(sa)); + sa.sun_family = AF_LOCAL; + strcpy(sa.sun_path, UDS_PATH); + uds->sa = sa; + uds->len = sizeof(sa); + + chmod(name, UDS_FILEMODE); + fda->fd[FD_GENERAL] = fd; + return 0; +} + +static int uds_recv(struct transport *t, int fd, void *buf, int buflen, + struct hw_timestamp *hwts) +{ + int cnt; + struct uds *uds = container_of(t, struct uds, t); + socklen_t *len = &uds->len; + uds->len = sizeof(uds->sa); + cnt = recvfrom(fd, buf, buflen, 0, (struct sockaddr *) &uds->sa, len); + if (cnt <= 0) { + pr_err("uds: recvfrom failed: %m"); + } + return cnt; +} + +static int uds_send(struct transport *t, struct fdarray *fda, int event, + int peer, void *buf, int buflen, struct hw_timestamp *hwts) +{ + int cnt, fd = fda->fd[FD_GENERAL]; + struct uds *uds = container_of(t, struct uds, t); + cnt = sendto(fd, buf, buflen, 0, (struct sockaddr *) &uds->sa, uds->len); + if (cnt <= 0) { + pr_err("uds: sendto failed: %m"); + } + return cnt; +} + +static void uds_release(struct transport *t) +{ + struct uds *uds = container_of(t, struct uds, t); + free(uds); +} + +struct transport *uds_transport_create(void) +{ + struct uds *uds; + uds = calloc(1, sizeof(*uds)); + if (!uds) + return NULL; + uds->t.close = uds_close; + uds->t.open = uds_open; + uds->t.recv = uds_recv; + uds->t.send = uds_send; + uds->t.release = uds_release; + return &uds->t; +} + diff --git a/uds.h b/uds.h new file mode 100644 index 0000000..3760eb8 --- /dev/null +++ b/uds.h @@ -0,0 +1,37 @@ +/** + * @file uds.h + * @brief Implements a management interface via UNIX domain sockets. + * @note Copyright (C) 2012 Richard Cochran + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef HAVE_UDS_H +#define HAVE_UDS_H + +#include "fd.h" +#include "transport.h" + +/** + * Address of the server. + */ +#define UDS_PATH "/tmp/ptp4l" + +/** + * Allocate an instance of a UDS transport. + * @return Pointer to a new transport instance on success, NULL otherwise. + */ +struct transport *uds_transport_create(void); + +#endif