#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <rtdk.h>
pthread_t rt, nrt;
#define XDDP_PORT 0
static const char *msg[] = {
"Surfing With The Alien",
"Lords of Karma",
"Banana Mango",
"Psycho Monkey",
"Luminous Flesh Giants",
"Moroccan Sunset",
"Satch Boogie",
"Flying In A Blue Dream",
"Ride",
"Summer Song",
"Speed Of Light",
"Crystal Planet",
"Raspberry Jam Delta-V",
"Champagne?",
"Clouds Race Across The Sky",
"Engines Of Creation"
};
static void fail(const char *reason)
{
perror(reason);
exit(EXIT_FAILURE);
}
static void *realtime_thread(void *arg)
{
int ret, s, n = 0, len, b;
struct timespec ts;
size_t streamsz;
char buf[128];
if (s < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
streamsz = 1024;
&streamsz, sizeof(streamsz));
if (ret)
fail("setsockopt");
memset(&saddr, 0, sizeof(saddr));
saddr.sipc_family = AF_RTIPC;
saddr.sipc_port = XDDP_PORT;
ret = bind(s, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret)
fail("bind");
for (;;) {
len = strlen(msg[n]);
for (b = 0; b < len; b++) {
ret = sendto(s, msg[n] + b, 1, MSG_MORE, NULL, 0);
if (ret != 1)
fail("sendto");
}
rt_printf("%s: sent (scattered) %d-bytes message, \"%.*s\"\n",
__FUNCTION__, len, len, msg[n]);
ret = recvfrom(s, buf, sizeof(buf), 0, NULL, 0);
if (ret <= 0)
fail("recvfrom");
rt_printf(" => \"%.*s\" echoed by peer\n", ret, buf);
n = (n + 1) % (sizeof(msg) / sizeof(msg[0]));
ts.tv_sec = 0;
ts.tv_nsec = 500000000;
}
return NULL;
}
static void *regular_thread(void *arg)
{
char buf[128], *devname;
int fd, ret;
if (asprintf(&devname, "/dev/rtp%d", XDDP_PORT) < 0)
fail("asprintf");
fd = open(devname, O_RDWR);
free(devname);
if (fd < 0)
fail("open");
for (;;) {
ret = read(fd, buf, sizeof(buf));
if (ret <= 0)
fail("read");
ret = write(fd, buf, ret);
if (ret <= 0)
fail("write");
}
return NULL;
}
static void cleanup_upon_sig(int sig)
{
signal(sig, SIG_DFL);
}
int main(int argc, char **argv)
{
struct sched_param rtparam = { .sched_priority = 42 };
pthread_attr_t rtattr, regattr;
sigset_t mask, oldmask;
mlockall(MCL_CURRENT | MCL_FUTURE);
signal(SIGINT, cleanup_upon_sig);
signal(SIGTERM, cleanup_upon_sig);
signal(SIGHUP, cleanup_upon_sig);
rt_print_auto_init(1);
if (errno)
fail("pthread_create");
if (errno)
fail("pthread_create");
sigsuspend(&oldmask);
return 0;
}