This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
pub.c
627 lines (581 loc) · 22.7 KB
/
pub.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#include <stdio.h>
#include "freertps/pub.h"
#include "freertps/config.h"
#include "freertps/id.h"
#include "freertps/part.h"
#include "freertps/bswap.h"
#include <string.h>
#include "freertps/freertps.h"
#include "freertps/sedp.h"
#include "freertps/spdp.h"
frudp_writer_t g_frudp_writers[FRUDP_MAX_WRITERS];
uint32_t g_frudp_num_writers;
frudp_pub_t g_frudp_pubs[FRUDP_MAX_PUBS];
uint32_t g_frudp_num_pubs = 0;
static uint8_t g_pub_tx_buf[2048];
static uint8_t g_pub_user_tx_buf[2048];
///////////////////////////////////////////////////////////////////////////
frudp_pub_t *frudp_create_pub(const char *topic_name,
const char *type_name,
const frudp_eid_t writer_id,
frudp_submsg_data_t **data_submsgs,
const uint32_t num_data_submsgs)
{
//printf("create pub 0x%08x\n", htonl(writer_id.u));
if (g_frudp_num_pubs >= FRUDP_MAX_PUBS)
{
FREERTPS_ERROR("woah there partner. don't have space for more pubs.\n");
return NULL; // no room. sorry
}
frudp_pub_t *p = &g_frudp_pubs[g_frudp_num_pubs];
p->topic_name = topic_name;
p->type_name = type_name;
if (data_submsgs) // it's for a reliable connection
{
p->data_submsgs = data_submsgs;
p->num_data_submsgs = num_data_submsgs;
p->reliable = true;
}
else
{
// unreliable; fire-and-forget from the caller's memory
p->data_submsgs = NULL;
p->num_data_submsgs = 0;
p->reliable = false;
}
if (writer_id.u == g_frudp_eid_unknown.u)
{
FREERTPS_ERROR("AWAY WITH YOU! <point scepter>\n");
return NULL;
}
else
p->writer_eid.u = writer_id.u;
p->next_submsg_idx = 0;
p->next_sn.low = 1;
p->next_sn.high = 0;
g_frudp_num_pubs++;
return p;
}
frudp_pub_t *frudp_create_user_pub(const char *topic_name,
const char *type_name)
{
printf("create_user_pub(%s, %s)\r\n", topic_name, type_name);
frudp_pub_t *pub = frudp_create_pub(topic_name,
type_name,
frudp_create_user_id
(FRUDP_ENTITY_KIND_USER_WRITER_NO_KEY),
NULL,
0);
//sedp_publish_pub(pub); // can't do this yet; disco hasn't started
return pub;
}
static int sedp_hb_count = 0;
void frudp_send_sedp_hb(frudp_part_t *part, bool with_msgs)
{
if (with_msgs)
{
frudp_spdp_bcast();
// we have just found out about a new participant. blast our SPDP and SEDP messages
// at it to help it join quickly
}
// first, send the publications
if (g_sedp_pub_pub->next_submsg_idx)
{
frudp_msg_t *msg = frudp_init_msg((frudp_msg_t *)g_pub_tx_buf);
fr_time_t t = fr_time_now();
uint16_t submsg_wpos = 0;
frudp_submsg_t *ts_submsg = (frudp_submsg_t *)&msg->submsgs[submsg_wpos];
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
memcpy(ts_submsg->contents, &t, 8);
submsg_wpos += 4 + 8;
frudp_submsg_heartbeat_t *hb_submsg =
(frudp_submsg_heartbeat_t *)&msg->submsgs[submsg_wpos];
hb_submsg->header.id = FRUDP_SUBMSG_ID_HEARTBEAT;
hb_submsg->header.flags = 0x1; // todo: spell this out
hb_submsg->header.len = 28;
hb_submsg->reader_id = g_sedp_pub_pub->data_submsgs[0]->reader_id;
hb_submsg->writer_id = g_sedp_pub_pub->data_submsgs[0]->writer_id;
hb_submsg->first_sn.low = 1; // todo
hb_submsg->first_sn.high = 0; // todo
hb_submsg->last_sn.low = g_sedp_pub_pub->next_submsg_idx;
hb_submsg->last_sn.high = 0; // todo
hb_submsg->count = sedp_hb_count ++;
submsg_wpos += 4 + hb_submsg->header.len;
if (with_msgs)
{
#ifdef SEDP_VERBOSE
printf("sending %d SEDP publication catchup messages\n",
g_sedp_pub_pub->next_submsg_idx);
#endif // SEDP_VERBOSE
for (int i = 0; i < g_sedp_pub_pub->next_submsg_idx; i++)
{
// todo: make sure we don't overflow a single ethernet frame
// since that would be most non-triumphant
frudp_submsg_data_t *pub_submsg = g_sedp_pub_pub->data_submsgs[i];
memcpy(&msg->submsgs[submsg_wpos], pub_submsg,
4 + pub_submsg->header.len);
submsg_wpos += 4 + pub_submsg->header.len;
#ifdef SEDP_VERBOSE
printf("sending %d SEDP publication catchup messages\n",
g_sedp_pub_pub->next_submsg_idx);
printf("catchup SEDP msg %d addressed to reader EID 0x%08x\r\n",
i, freertps_htonl((unsigned)pub_submsg->reader_id.u));
#endif // SEDP_VERBOSE
}
}
int payload_len = &msg->submsgs[submsg_wpos] - ((uint8_t *)msg);
uint32_t dst_addr = part->metatraffic_unicast_locator.addr.udp4.addr;
uint16_t dst_port = part->metatraffic_unicast_locator.port;
#ifdef SEDP_VERBOSE
printf("sending %d bytes of SEDP pub catchup messages or HB to 0x%08x:%d\r\n",
payload_len, dst_addr, dst_port);
#endif // SEDP_VERBOSE
frudp_tx(dst_addr, dst_port, (const uint8_t *)msg, payload_len);
}
else
{
#ifdef SEDP_VERBOSE
if (with_msgs)
{
printf("no SEDP pub data to send to new participant\r\n");
}
#endif // SEDP_VERBOSE
}
// now, send the subscriptions
if (g_sedp_sub_pub->next_submsg_idx)
{
frudp_msg_t *msg = frudp_init_msg((frudp_msg_t *)g_pub_tx_buf);
fr_time_t t = fr_time_now();
uint16_t submsg_wpos = 0;
frudp_submsg_t *ts_submsg = (frudp_submsg_t *)&msg->submsgs[submsg_wpos];
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
memcpy(ts_submsg->contents, &t, 8);
submsg_wpos += 4 + 8;
frudp_submsg_heartbeat_t *hb_submsg =
(frudp_submsg_heartbeat_t *)&msg->submsgs[submsg_wpos];
hb_submsg->header.id = FRUDP_SUBMSG_ID_HEARTBEAT;
hb_submsg->header.flags = 0x1; // todo: spell this out
hb_submsg->header.len = 28;
hb_submsg->reader_id = g_sedp_sub_pub->data_submsgs[0]->reader_id;
hb_submsg->writer_id = g_sedp_sub_pub->data_submsgs[0]->writer_id;
hb_submsg->first_sn.low = 1; // todo
hb_submsg->first_sn.high = 0; // todo
hb_submsg->last_sn.low = g_sedp_sub_pub->next_submsg_idx;
hb_submsg->last_sn.high = 0; // todo
hb_submsg->count = sedp_hb_count ++;
submsg_wpos += 4 + hb_submsg->header.len;
if (with_msgs)
{
#ifdef SEDP_VERBOSE
printf("sending %d SEDP subscription catchup messages\n",
g_sedp_sub_pub->next_submsg_idx);
#endif // SEDP_VERBOSE
for (int i = 0; i < g_sedp_sub_pub->next_submsg_idx; i++)
{
// todo: make sure we don't overflow a single ethernet frame,
// since that would be most non-triumphant
frudp_submsg_data_t *sub_submsg = g_sedp_sub_pub->data_submsgs[i];
memcpy(&msg->submsgs[submsg_wpos], sub_submsg,
4 + sub_submsg->header.len);
submsg_wpos += 4 + sub_submsg->header.len;
#ifdef SEDP_VERBOSE
printf("catchup SEDP msg %d addressed to reader EID 0x%08x\r\n",
i, freertps_htonl((unsigned)sub_submsg->reader_id.u));
#endif // SEDP_VERBOSE
}
}
int payload_len = &msg->submsgs[submsg_wpos] - ((uint8_t *)msg);
uint32_t dst_addr = part->metatraffic_unicast_locator.addr.udp4.addr;
uint16_t dst_port = part->metatraffic_unicast_locator.port;
#ifdef SEDP_VERBOSE
printf("sending %d bytes of SEDP sub catchup messages or HB to 0x%08x:%d\r\n",
payload_len, dst_addr, dst_port);
#endif // SEDP_VERBOSE
frudp_tx(dst_addr, dst_port, (const uint8_t *)msg, payload_len);
}
else
{
#ifdef SEDP_VERBOSE
if (with_msgs)
{
printf("no SEDP pub data to send to new participant\r\n");
}
#endif // SEDP_VERBOSE
}
}
// currently this only gets called for SEDP messages
void frudp_publish(frudp_pub_t *pub, frudp_submsg_data_t *submsg)
{
// TODO: for best-effort connections, don't buffer it like this
// check to see if the publisher has a buffer or not, first....
// (todo: allow people to stuff the message directly in the pub and
// call this function with sample set to NULL to indicate this)
// find first place we can buffer this sample
pub->max_tx_sn_avail.low++; // TODO: care about sample counts over 32 bits
frudp_submsg_data_t *pub_submsg = pub->data_submsgs[pub->next_submsg_idx];
*pub_submsg = *submsg;
if (submsg->writer_sn.low == g_frudp_sn_unknown.low) // todo: 64 bits
{
pub_submsg->writer_sn = pub->next_sn;
pub->next_sn.low++; // todo: > 32 bits
}
memcpy(pub_submsg->data,
submsg->data,
submsg->header.len - sizeof(frudp_submsg_data_t) + 4);
//pub_sample->data_len = sample->data_len;
// TODO: now, send DATA and HEARTBEAT submessages
printf("frudp publish %d bytes, seq num %d:%d\r\n",
submsg->header.len,
(int)pub_submsg->writer_sn.high,
(int)pub_submsg->writer_sn.low);
/*
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//data_submsg->header.len = next_submsg_ptr - data_submsg->contents;
//printf("len = %d\n", data_submsg->header.len);
/////////////////////////////////////////////////////////////
//int payload_len = ((uint8_t *)param_list) - ((uint8_t *)msg->submsgs);
//int payload_len = ((uint8_t *)next_submsg_ptr) - ((uint8_t *)msg->submsgs);
int payload_len = ((uint8_t *)next_submsg_ptr) - ((uint8_t *)msg);
frudp_tx(inet_addr("239.255.0.1"), frudp_mcast_builtin_port(),
(const uint8_t *)msg, payload_len);
*/
frudp_msg_t *msg = frudp_init_msg((frudp_msg_t *)g_pub_tx_buf);
fr_time_t t = fr_time_now();
uint16_t submsg_wpos = 0;
frudp_submsg_t *ts_submsg = (frudp_submsg_t *)&msg->submsgs[submsg_wpos];
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
memcpy(ts_submsg->contents, &t, 8);
submsg_wpos += 4 + 8;
///////////////////////////////////////////////////////////////////////
//frudp_submsg_data_t *data_submsg = (frudp_submsg_data_t *)&msg->submsgs[submsg_wpos];
memcpy(&msg->submsgs[submsg_wpos], submsg, 4 + submsg->header.len);
//data_submsg, submsg, 4 + submsg->header.len);
submsg_wpos += 4 + submsg->header.len;
frudp_submsg_heartbeat_t *hb_submsg =
(frudp_submsg_heartbeat_t *)&msg->submsgs[submsg_wpos];
hb_submsg->header.id = FRUDP_SUBMSG_ID_HEARTBEAT;
hb_submsg->header.flags = 0x3; // todo: spell this out
hb_submsg->header.len = 28;
hb_submsg->reader_id = submsg->reader_id;
hb_submsg->writer_id = submsg->writer_id;
hb_submsg->first_sn.low = 1; // todo, should increase each time (?)
hb_submsg->first_sn.high = 0; // todo
hb_submsg->last_sn = hb_submsg->first_sn; //submsg->writer_sn;
/*
printf(" hb submsg publish last_sn = %d:%d\n",
(int)hb_submsg->last_sn.high,
(int)hb_submsg->last_sn.low);
*/
static int hb_count = 0;
hb_submsg->count = hb_count++;
submsg_wpos += 4 + hb_submsg->header.len;
int payload_len = &msg->submsgs[submsg_wpos] - ((uint8_t *)msg);
frudp_tx(freertps_htonl(FRUDP_DEFAULT_MCAST_GROUP),
frudp_mcast_builtin_port(),
(const uint8_t *)msg, payload_len);
pub->next_submsg_idx++;
/////////////////////////////////////////////////////////////
/*
ts_submsg = (frudp_submsg_t *)param_list;
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
memcpy(ts_submsg->contents, &t, 8);
uint8_t *next_submsg_ptr = ((uint8_t *)param_list) + 4 + 8;
*/
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
//data_submsg->header.len = next_submsg_ptr - data_submsg->contents;
//printf("len = %d\n", data_submsg->header.len);
/////////////////////////////////////////////////////////////
//int payload_len = ((uint8_t *)param_list) - ((uint8_t *)msg->submsgs);
//int payload_len = ((uint8_t *)next_submsg_ptr) - ((uint8_t *)msg->submsgs);
}
frudp_pub_t *frudp_pub_from_writer_id(const frudp_eid_t id)
{
//printf("pub from writer id 0x%08x\n", htonl(id.u));
for (int i = 0; i < g_frudp_num_pubs; i++)
{
frudp_pub_t *p = &g_frudp_pubs[i];
//printf(" comp %d: 0x%08x ?= 0x%08x\n",
// i, htonl(id.u), htonl(p->writer_id.u));
if (id.u == p->writer_eid.u)
return p;
}
return NULL;
}
void frudp_pub_rx_acknack(frudp_pub_t *pub,
frudp_submsg_acknack_t *acknack,
frudp_guid_prefix_t *guid_prefix)
{
// see if we have any of the requested messages
for (int req_seq_num = acknack->reader_sn_state.bitmap_base.low;
req_seq_num < acknack->reader_sn_state.bitmap_base.low +
acknack->reader_sn_state.num_bits + 1;
req_seq_num++)
{
//printf(" request for seq num %d\n", req_seq_num);
for (int msg_idx = 0; msg_idx < pub->num_data_submsgs; msg_idx++)
{
frudp_submsg_data_t *data = pub->data_submsgs[msg_idx];
//printf(" %d ?= %d\n", req_seq_num, data->writer_sn.low);
if (data->writer_sn.low == req_seq_num)
{
//printf(" found it in the history cache! now i need to tx\n");
//printf("about to look up guid prefix: ");
//print_guid_prefix(guid_prefix);
//printf("\n");
// look up the GUID prefix of this reader, so we can call them back
frudp_part_t *part = frudp_part_find(guid_prefix);
if (!part)
{
printf(" woah there partner. you from around these parts?\n");
return;
}
frudp_msg_t *msg = frudp_init_msg((frudp_msg_t *)g_pub_tx_buf);
fr_time_t t = fr_time_now();
uint16_t submsg_wpos = 0;
frudp_submsg_t *ts_submsg = (frudp_submsg_t *)&msg->submsgs[submsg_wpos];
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
memcpy(ts_submsg->contents, &t, 8);
submsg_wpos += 4 + 8;
///////////////////////////////////////////////////////////////////////
//frudp_submsg_data_t *data_submsg = (frudp_submsg_data_t *)&msg->submsgs[submsg_wpos];
memcpy(&msg->submsgs[submsg_wpos], data, 4 + data->header.len);
//data_submsg, submsg, 4 + submsg->header.len);
submsg_wpos += 4 + data->header.len;
frudp_submsg_heartbeat_t *hb_submsg = (frudp_submsg_heartbeat_t *)&msg->submsgs[submsg_wpos];
hb_submsg->header.id = FRUDP_SUBMSG_ID_HEARTBEAT;
hb_submsg->header.flags = 0x3; // todo: spell this out
hb_submsg->header.len = 28;
hb_submsg->reader_id = data->reader_id;
hb_submsg->writer_id = data->writer_id;
//printf("hb writer id = 0x%08x\n", htonl(data->writer_id.u));
hb_submsg->first_sn.low = 1; // todo
hb_submsg->first_sn.high = 0; // todo
hb_submsg->last_sn = data->writer_sn;
static int hb_count = 0;
hb_submsg->count = hb_count++;
submsg_wpos += 4 + hb_submsg->header.len;
int payload_len = &msg->submsgs[submsg_wpos] - ((uint8_t *)msg);
//printf(" sending %d bytes\n", payload_len);
frudp_tx(part->metatraffic_unicast_locator.addr.udp4.addr,
part->metatraffic_unicast_locator.port,
(const uint8_t *)msg, payload_len);
}
}
}
}
void frudp_add_writer(const frudp_writer_t *writer)
{
if (g_frudp_num_writers >= FRUDP_MAX_WRITERS)
return;
g_frudp_writers[g_frudp_num_writers] = *writer;
g_frudp_num_writers++;
printf("add_writer(%08x => ",
(unsigned)freertps_htonl(writer->writer_eid.u));
frudp_print_guid(&writer->reader_guid);
printf(")\n");
}
bool frudp_publish_user_msg_frag(
frudp_pub_t *pub,
const uint32_t frag_num,
const uint8_t *frag,
const uint32_t frag_len,
const uint32_t frag_used_len,
const uint32_t msg_len)
{
//printf("publish frag %d : %d bytes\n", frag_num, frag_len);
// todo: consolidate this with the non-fragmented TX function...
frudp_msg_t *msg = frudp_init_msg((frudp_msg_t *)g_pub_user_tx_buf);
uint16_t submsg_wpos = 0;
if (frag_num == 1)
{
// craft a tx packet and stuff it
frudp_submsg_t *ts_submsg = (frudp_submsg_t *)&msg->submsgs[submsg_wpos];
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
fr_time_t t = fr_time_now();
memcpy(ts_submsg->contents, &t, 8);
submsg_wpos += 4 + 8;
}
// append the data frag submessage ////////////////////////////////////////
frudp_submsg_data_frag_t *d =
(frudp_submsg_data_frag_t *)&msg->submsgs[submsg_wpos];
d->header.id = FRUDP_SUBMSG_ID_DATA_FRAG;
d->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
d->header.len = sizeof(frudp_submsg_data_frag_t) + frag_used_len;
d->extraflags = 0;
d->octets_to_inline_qos = 28;
d->writer_sn = pub->next_sn;
if (frag_num == 1)
pub->next_sn.low++; // todo: something smarter
d->fragment_starting_number = frag_num;
d->fragments_in_submessage = 1;
d->fragment_size = frag_len;
d->sample_size = msg_len; // + 4;
/*
if (frag_num == 1)
{
frudp_encapsulation_scheme_t *scheme =
(frudp_encapsulation_scheme_t *)((uint8_t *)d->data);
scheme->scheme = freertps_htons(FRUDP_SCHEME_CDR_LE);
scheme->options = 0;
}
*/
uint8_t *outbound_frag_payload = (uint8_t *)&d->data[0];
memcpy(outbound_frag_payload, frag, frag_used_len);
submsg_wpos += d->header.len + 4;
const int udp_payload_len =
(uint8_t *)&msg->submsgs[submsg_wpos] - (uint8_t *)msg;
//printf("rtps udp payload = %d bytes\n", (int)udp_payload_len);
// now, iterate through all matched-writers and send the message as needed
for (int i = 0; i < g_frudp_num_writers; i++)
{
frudp_writer_t *w = &g_frudp_writers[i];
if (w->writer_eid.u == pub->writer_eid.u)
{
// we want to send here. if we haven't already sent to the same
// locator, update the guid and send the message
// todo: figure out between sending to multicast and sending to unicast
// and don't re-multicast to the same domain
d->reader_id = w->reader_guid.eid; // todo copy here...
d->writer_id = w->writer_eid;
frudp_part_t *part = frudp_part_find(&w->reader_guid.prefix);
if (!part)
continue; // shouldn't happen; this implies inconsistency somewhere
frudp_tx(part->default_unicast_locator.addr.udp4.addr,
part->default_unicast_locator.port,
(const uint8_t *)msg,
udp_payload_len);
}
}
return true;
}
bool frudp_publish_user_msg(frudp_pub_t *pub,
const uint8_t *payload, const uint32_t payload_len)
{
//printf("publish user msg %d bytes\n", (int)payload_len);
if (pub->reliable)
{
// shouldn't be hard to push this through the reliable-comms machinery
// written for SEDP, but it's not done yet.
FREERTPS_ERROR("user reliable publishing not quite done yet.\n");
return false;
}
// craft a tx packet and stuff it
frudp_msg_t *msg = frudp_init_msg((frudp_msg_t *)g_pub_user_tx_buf);
fr_time_t t = fr_time_now();
uint16_t submsg_wpos = 0;
frudp_submsg_t *ts_submsg = (frudp_submsg_t *)&msg->submsgs[submsg_wpos];
ts_submsg->header.id = FRUDP_SUBMSG_ID_INFO_TS;
ts_submsg->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN;
ts_submsg->header.len = 8;
memcpy(ts_submsg->contents, &t, 8);
submsg_wpos += 4 + 8;
// now, append the data submessage ////////////////////////////////////////
frudp_submsg_data_t *d = (frudp_submsg_data_t *)&msg->submsgs[submsg_wpos];
d->header.id = FRUDP_SUBMSG_ID_DATA;
d->header.flags = FRUDP_FLAGS_LITTLE_ENDIAN |
FRUDP_FLAGS_DATA_PRESENT;
d->header.len = sizeof(frudp_submsg_data_t) /*+ 4*/ + payload_len;
d->extraflags = 0;
d->octets_to_inline_qos = 16;
d->writer_sn = pub->next_sn;
frudp_encapsulation_scheme_t *scheme =
(frudp_encapsulation_scheme_t *)((uint8_t *)d->data);
scheme->scheme = freertps_htons(FRUDP_SCHEME_CDR_LE);
scheme->options = 0;
uint8_t *outbound_payload = (uint8_t *)(&d->data[4]);
// todo: bounds checking
/*
printf("copying in payload:\n");
for (int j = 0; j < payload_len; j++)
{
printf("%02x", (int)(((uint8_t *)payload)[j]));
if (j % 8 == 3)
printf(" ");
else if (j % 8 == 7)
printf("\n");
}
printf("\n");
*/
memcpy(outbound_payload, payload, payload_len);
//memcpy(&msg->submsgs[submsg_wpos], submsg, 4 + submsg->header.len);
//data_submsg, submsg, 4 + submsg->header.len);
submsg_wpos += 4 + d->header.len;
///////////////////////////////////////////////////////////////////////
/*
frudp_submsg_heartbeat_t *hb =
(frudp_submsg_heartbeat_t *)&msg->submsgs[submsg_wpos];
hb->header.id = FRUDP_SUBMSG_ID_HEARTBEAT;
hb->header.flags = 0x3; // todo: spell this out
hb->header.len = 28;
hb->first_sn.low = 1; // todo
hb->first_sn.high = 0; // todo
hb->last_sn = d->writer_sn;
static int hb_count = 0;
hb->count = hb_count++;
submsg_wpos += 4 + hb->header.len;
*/
const int udp_payload_len =
(uint8_t *)&msg->submsgs[submsg_wpos] - (uint8_t *)msg;
//printf("rtps udp payload = %d bytes\n", (int)udp_payload_len);
// now, iterate through all matched-writers and send the message as needed
for (int i = 0; i < g_frudp_num_writers; i++)
{
frudp_writer_t *w = &g_frudp_writers[i];
if (w->writer_eid.u == pub->writer_eid.u)
{
// we want to send here. if we haven't already sent to the same
// locator, update the guid and send the message
// todo: figure out between sending to multicast and sending to unicast
// and don't re-multicast to the same domain
d->reader_id = w->reader_guid.eid; // todo copy here...
d->writer_id = w->writer_eid;
frudp_part_t *part = frudp_part_find(&w->reader_guid.prefix);
if (!part)
continue; // shouldn't happen; this implies inconsistency somewhere
// also, update the reader/writer ID's for the heartbeat submsg
// actually.. i don't think we have to send heartbeats to best-effort..
//hb->reader_id = d->reader_id;
//hb->writer_id = d->writer_id;
//frudp_locator_t *loc = part->default_unicast_locator;
// todo: more than ipv4
/*
for (int j = 0; j < udp_payload_len; j++)
{
printf("%02x", (int)(((uint8_t *)msg)[j]));
if (j % 8 == 3)
printf(" ");
else if (j % 8 == 7)
printf("\n");
}
printf("\n");
*/
/*
printf("tx %d bytes to ",
udp_payload_len);
frudp_print_guid(&w->reader_guid);
printf("\r\n");
*/
//printf("tx
frudp_tx(part->default_unicast_locator.addr.udp4.addr,
part->default_unicast_locator.port,
(const uint8_t *)msg,
udp_payload_len);
}
}
pub->next_sn.low++; // todo: 64-bit
return true;
}