hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright (C) 2009 Philippe Gerum <rpm@xenomai.org>.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *
 * BUFP-based client/server demo, using the read(2)/write(2)
 * system calls to exchange data over a socket.
 *
 * In this example, two sockets are created.  A server thread (reader)
 * is bound to a real-time port and receives a stream of bytes sent to
 * this port from a client thread (writer).
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <rtdm/ipc.h>
 
pthread_t svtid, cltid;
 
#define BUFP_PORT_LABEL  "bufp-demo"
 
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 *server(void *arg)
{
   struct rtipc_port_label plabel;
   struct sockaddr_ipc saddr;
   char buf[128];
   size_t bufsz;
   int ret, s;
 
   s = socket(AF_RTIPC, SOCK_DGRAM, IPCPROTO_BUFP);
   if (s < 0)
       fail("socket");
 
   /*
    * Set a 16k buffer for the server endpoint. This
    * configuration must be done prior to binding the socket to a
    * port.
    */
   bufsz = 16384; /* bytes */
   ret = setsockopt(s, SOL_BUFP, BUFP_BUFSZ,
            &bufsz, sizeof(bufsz));
   if (ret)
       fail("setsockopt");
 
   /*
    * Set a port label. This name will be registered when
    * binding, in addition to the port number (if given).
    */
   strcpy(plabel.label, BUFP_PORT_LABEL);
   ret = setsockopt(s, SOL_BUFP, BUFP_LABEL,
            &plabel, sizeof(plabel));
   if (ret)
       fail("setsockopt");
 
   /*
    * Bind the socket to the port. Assign that port a label, so
    * that peers may use a descriptive information to locate
    * it. Labeled ports will appear in the
    * /proc/xenomai/registry/rtipc/bufp directory once the socket
    * is bound.
    *
    * saddr.sipc_port specifies the port number to use. If -1 is
    * passed, the BUFP driver will auto-select an idle port.
    */
   saddr.sipc_family = AF_RTIPC;
   saddr.sipc_port = -1;
   ret = bind(s, (struct sockaddr *)&saddr, sizeof(saddr));
   if (ret)
       fail("bind");
 
   for (;;) {
       ret = read(s, buf, sizeof(buf));
       if (ret < 0) {
           close(s);
           fail("read");
       }
       printf("%s: received %d bytes, \"%.*s\"\n",
              __FUNCTION__, ret, ret, buf);
   }
 
   return NULL;
}
 
static void *client(void *arg)
{
   struct rtipc_port_label plabel;
   struct sockaddr_ipc svsaddr;
   int ret, s, n = 0, len;
   struct timespec ts;
 
   s = socket(AF_RTIPC, SOCK_DGRAM, IPCPROTO_BUFP);
   if (s < 0)
       fail("socket");
 
   /*
    * Set the port label. This name will be used to find the peer
    * when connecting, instead of the port number. The label must
    * be set _after_ the socket is bound to the port, so that
    * BUFP does not try to register this label for the client
    * port as well (like the server thread did).
    */
   strcpy(plabel.label, BUFP_PORT_LABEL);
   ret = setsockopt(s, SOL_BUFP, BUFP_LABEL,
            &plabel, sizeof(plabel));
   if (ret)
       fail("setsockopt");
 
   memset(&svsaddr, 0, sizeof(svsaddr));
   svsaddr.sipc_family = AF_RTIPC;
   svsaddr.sipc_port = -1;    /* Tell BUFP to search by label. */
   ret = connect(s, (struct sockaddr *)&svsaddr, sizeof(svsaddr));
   if (ret)
       fail("connect");
 
   for (;;) {
       len = strlen(msg[n]);
       ret = write(s, msg[n], len);
       if (ret < 0) {
           close(s);
           fail("write");
       }
       printf("%s: sent %d bytes, \"%.*s\"\n",
              __FUNCTION__, ret, ret, msg[n]);
       n = (n + 1) % (sizeof(msg) / sizeof(msg[0]));
       /*
        * We run in full real-time mode (i.e. primary mode),
        * so we have to let the system breathe between two
        * iterations.
        */
       ts.tv_sec = 0;
       ts.tv_nsec = 500000000; /* 500 ms */
       clock_nanosleep(CLOCK_REALTIME, 0, &ts, NULL);
   }
 
   return NULL;
}
 
int main(int argc, char **argv)
{
   struct sched_param svparam = {.sched_priority = 71 };
   struct sched_param clparam = {.sched_priority = 70 };
   pthread_attr_t svattr, clattr;
   sigset_t set;
   int sig;
 
   sigemptyset(&set);
   sigaddset(&set, SIGINT);
   sigaddset(&set, SIGTERM);
   sigaddset(&set, SIGHUP);
   pthread_sigmask(SIG_BLOCK, &set, NULL);
 
   pthread_attr_init(&svattr);
   pthread_attr_setdetachstate(&svattr, PTHREAD_CREATE_JOINABLE);
   pthread_attr_setinheritsched(&svattr, PTHREAD_EXPLICIT_SCHED);
   pthread_attr_setschedpolicy(&svattr, SCHED_FIFO);
   pthread_attr_setschedparam(&svattr, &svparam);
 
   errno = pthread_create(&svtid, &svattr, &server, NULL);
   if (errno)
       fail("pthread_create");
 
   pthread_attr_init(&clattr);
   pthread_attr_setdetachstate(&clattr, PTHREAD_CREATE_JOINABLE);
   pthread_attr_setinheritsched(&clattr, PTHREAD_EXPLICIT_SCHED);
   pthread_attr_setschedpolicy(&clattr, SCHED_FIFO);
   pthread_attr_setschedparam(&clattr, &clparam);
 
   errno = pthread_create(&cltid, &clattr, &client, NULL);
   if (errno)
       fail("pthread_create");
 
   __STD(sigwait(&set, &sig));
   pthread_cancel(svtid);
   pthread_cancel(cltid);
   pthread_join(svtid, NULL);
   pthread_join(cltid, NULL);
 
   return 0;
}