This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfunctions.cpp
2306 lines (2081 loc) · 72.5 KB
/
functions.cpp
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014-2017 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cassert>
#include <exception>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <stdexcept>
#include <string>
#ifndef _WIN32
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
# pragma GCC diagnostic ignored "-Wunused-parameter"
# ifdef __clang__
# pragma clang diagnostic ignored "-Wdeprecated-register"
# pragma clang diagnostic ignored "-Winfinite-recursion"
# pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
# endif
#endif
#include <ndds/ndds_cpp.h>
#include <ndds/ndds_requestreply_cpp.h>
#ifndef _WIN32
# pragma GCC diagnostic pop
#endif
#include "rcutils/format_string.h"
#include "rcutils/types.h"
#include "rcutils/split.h"
#include "rmw/rmw.h"
#include "rmw/allocators.h"
#include "rmw/error_handling.h"
#include "rmw/names_and_types.h"
#include "rmw/get_service_names_and_types.h"
#include "rmw/get_topic_names_and_types.h"
#include "rmw/types.h"
#include "rmw/impl/cpp/macros.hpp"
#include "rosidl_typesupport_connext_c/identifier.h"
#include "rosidl_typesupport_connext_cpp/identifier.hpp"
#include "rosidl_typesupport_connext_cpp/message_type_support.h"
#include "rosidl_typesupport_connext_cpp/service_type_support.h"
#include "rmw_connext_shared_cpp/shared_functions.hpp"
#include "rmw_connext_shared_cpp/types.hpp"
// Uncomment this to get extra console output about discovery.
// This affects code in this file, but there is a similar variable in:
// rmw_connext_shared_cpp/shared_functions.cpp
// #define DISCOVERY_DEBUG_LOGGING 1
#define RMW_CONNEXT_EXTRACT_MESSAGE_TYPESUPPORT(TYPE_SUPPORTS, TYPE_SUPPORT) \
if (!TYPE_SUPPORTS) { \
RMW_SET_ERROR_MSG("type supports handle is null"); \
return NULL; \
} \
const rosidl_message_type_support_t * TYPE_SUPPORT = \
get_message_typesupport_handle( \
TYPE_SUPPORTS, rosidl_typesupport_connext_c__identifier); \
if (!TYPE_SUPPORT) { \
TYPE_SUPPORT = get_message_typesupport_handle( \
TYPE_SUPPORTS, rosidl_typesupport_connext_cpp::typesupport_identifier); \
if (!TYPE_SUPPORT) { \
char __msg[1024]; \
snprintf( \
__msg, 1024, \
"type support handle implementation '%s' (%p) does not match valid type supports " \
"('%s' (%p), '%s' (%p))", \
TYPE_SUPPORTS->typesupport_identifier, \
static_cast<const void *>(TYPE_SUPPORTS->typesupport_identifier), \
rosidl_typesupport_connext_cpp::typesupport_identifier, \
static_cast<const void *>(rosidl_typesupport_connext_cpp::typesupport_identifier), \
rosidl_typesupport_connext_c__identifier, \
static_cast<const void *>(rosidl_typesupport_connext_c__identifier)); \
RMW_SET_ERROR_MSG(__msg); \
return NULL; \
} \
}
#define RMW_CONNEXT_EXTRACT_SERVICE_TYPESUPPORT(TYPE_SUPPORTS, TYPE_SUPPORT) \
if (!TYPE_SUPPORTS) { \
RMW_SET_ERROR_MSG("type supports handle is null"); \
return NULL; \
} \
const rosidl_service_type_support_t * TYPE_SUPPORT = \
get_service_typesupport_handle( \
TYPE_SUPPORTS, rosidl_typesupport_connext_c__identifier); \
if (!TYPE_SUPPORT) { \
TYPE_SUPPORT = get_service_typesupport_handle( \
TYPE_SUPPORTS, rosidl_typesupport_connext_cpp::typesupport_identifier); \
if (!TYPE_SUPPORT) { \
char __msg[1024]; \
snprintf( \
__msg, 1024, \
"type support handle implementation '%s' (%p) does not match valid type supports " \
"('%s' (%p), '%s' (%p))", \
TYPE_SUPPORTS->typesupport_identifier, \
static_cast<const void *>(TYPE_SUPPORTS->typesupport_identifier), \
rosidl_typesupport_connext_cpp::typesupport_identifier, \
static_cast<const void *>(rosidl_typesupport_connext_cpp::typesupport_identifier), \
rosidl_typesupport_connext_c__identifier, \
static_cast<const void *>(rosidl_typesupport_connext_c__identifier)); \
RMW_SET_ERROR_MSG(__msg); \
return NULL; \
} \
}
inline std::string
_create_type_name(
const message_type_support_callbacks_t * callbacks,
const std::string & sep)
{
return
std::string(callbacks->package_name) +
"::" + sep + "::dds_::" + callbacks->message_name + "_";
}
namespace std
{
std::string
to_string(DDS_InstanceHandle_t val)
{
std::stringstream ss;
for (size_t i = 0; i < val.keyHash.length; i++) {
ss << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(val.keyHash.value[i]);
if (i + 1 < val.keyHash.length) {
ss << ":";
}
}
return ss.str();
}
} // namespace std
extern "C"
{
// static for internal linkage
static const char * const rti_connext_identifier = "rmw_connext_cpp";
struct ConnextStaticPublisherInfo
{
DDSPublisher * dds_publisher_;
DDSDataWriter * topic_writer_;
const message_type_support_callbacks_t * callbacks_;
rmw_gid_t publisher_gid;
};
struct ConnextStaticSubscriberInfo
{
DDSSubscriber * dds_subscriber_;
DDSDataReader * topic_reader_;
DDSReadCondition * read_condition_;
bool ignore_local_publications;
const message_type_support_callbacks_t * callbacks_;
};
struct ConnextStaticClientInfo
{
void * requester_;
DDSDataReader * response_datareader_;
DDSReadCondition * read_condition_;
const service_type_support_callbacks_t * callbacks_;
};
struct ConnextStaticServiceInfo
{
void * replier_;
DDSDataReader * request_datareader_;
DDSReadCondition * read_condition_;
const service_type_support_callbacks_t * callbacks_;
};
const char *
rmw_get_implementation_identifier()
{
return rti_connext_identifier;
}
rmw_ret_t
rmw_init()
{
return init();
}
rmw_node_t *
rmw_create_node(
const char * name, const char * namespace_, size_t domain_id,
const rmw_node_security_options_t * security_options)
{
return create_node(
rti_connext_identifier, name, namespace_, domain_id, security_options);
}
rmw_ret_t
rmw_destroy_node(rmw_node_t * node)
{
return destroy_node(rti_connext_identifier, node);
}
bool
_process_topic_name(
const char * topic_name,
bool avoid_ros_namespace_conventions,
char ** topic_str,
char ** partition_str)
{
bool success = true;
rcutils_string_array_t name_tokens = rcutils_get_zero_initialized_string_array();
rcutils_allocator_t allocator = rcutils_get_default_allocator();
if (rcutils_split_last(topic_name, '/', allocator, &name_tokens) != RCUTILS_RET_OK) {
RMW_SET_ERROR_MSG(rcutils_get_error_string_safe())
success = false;
goto end;
}
if (name_tokens.size == 1) {
if (!avoid_ros_namespace_conventions) {
*partition_str = DDS_String_dup(ros_topic_prefix);
}
*topic_str = DDS_String_dup(name_tokens.data[0]);
} else if (name_tokens.size == 2) {
if (avoid_ros_namespace_conventions) {
// no ros_topic_prefix, so store the user's namespace directly
*partition_str = DDS_String_dup(name_tokens.data[0]);
} else {
// concat the ros_topic_prefix with the user's namespace
char * concat_str =
rcutils_format_string(allocator, "%s/%s", ros_topic_prefix, name_tokens.data[0]);
if (!concat_str) {
RMW_SET_ERROR_MSG("could not allocate memory for partition string")
success = false;
goto end;
}
*partition_str = DDS_String_dup(concat_str);
allocator.deallocate(concat_str, allocator.state);
}
// Connext will call deallocate on this, passing ownership to connext
*topic_str = DDS_String_dup(name_tokens.data[1]);
} else {
RMW_SET_ERROR_MSG("incorrectly formatted topic name")
success = false;
}
end:
// all necessary strings are copied into connext
// free that memory
if (rcutils_string_array_fini(&name_tokens) != RCUTILS_RET_OK) {
fprintf(stderr, "Failed to destroy the token string array\n");
}
return success;
}
rmw_publisher_t *
rmw_create_publisher(
const rmw_node_t * node,
const rosidl_message_type_support_t * type_supports,
const char * topic_name,
const rmw_qos_profile_t * qos_profile)
{
if (!node) {
RMW_SET_ERROR_MSG("node handle is null");
return NULL;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, rti_connext_identifier,
return NULL)
RMW_CONNEXT_EXTRACT_MESSAGE_TYPESUPPORT(type_supports, type_support)
if (!topic_name || strlen(topic_name) == 0) {
RMW_SET_ERROR_MSG("publisher topic is null or empty string");
return NULL;
}
if (!qos_profile) {
RMW_SET_ERROR_MSG("qos_profile is null");
return NULL;
}
auto node_info = static_cast<ConnextNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return NULL;
}
auto participant = static_cast<DDSDomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return NULL;
}
const message_type_support_callbacks_t * callbacks =
static_cast<const message_type_support_callbacks_t *>(type_support->data);
if (!callbacks) {
RMW_SET_ERROR_MSG("callbacks handle is null");
return NULL;
}
std::string type_name = _create_type_name(callbacks, "msg");
// Past this point, a failure results in unrolling code in the goto fail block.
bool registered;
DDS_DataWriterQos datawriter_qos;
DDS_PublisherQos publisher_qos;
DDS_ReturnCode_t status;
DDSPublisher * dds_publisher = nullptr;
DDSDataWriter * topic_writer = nullptr;
DDSTopic * topic = nullptr;
DDSTopicDescription * topic_description = nullptr;
void * buf = nullptr;
ConnextStaticPublisherInfo * publisher_info = nullptr;
rmw_publisher_t * publisher = nullptr;
std::string mangled_name = "";
// memory allocations for namespacing
char * partition_str = nullptr;
char * topic_str = nullptr;
// Begin initializing elements
publisher = rmw_publisher_allocate();
if (!publisher) {
RMW_SET_ERROR_MSG("failed to allocate publisher");
goto fail;
}
registered = callbacks->register_type(participant, type_name.c_str());
if (!registered) {
RMW_SET_ERROR_MSG("failed to register type");
goto fail;
}
status = participant->get_default_publisher_qos(publisher_qos);
if (status != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default publisher qos");
goto fail;
}
if (!_process_topic_name(
topic_name,
qos_profile->avoid_ros_namespace_conventions,
&topic_str,
&partition_str))
{
goto fail;
}
// we have to set the partition array to length 1
// and then set the partition_str in it
if (partition_str && strlen(partition_str) != 0) { // only set if not empty
publisher_qos.partition.name.ensure_length(1, 1);
publisher_qos.partition.name[0] = partition_str;
}
dds_publisher = participant->create_publisher(
publisher_qos, NULL, DDS_STATUS_MASK_NONE);
if (!dds_publisher) {
RMW_SET_ERROR_MSG("failed to create publisher");
goto fail;
}
topic_description = participant->lookup_topicdescription(topic_str);
if (!topic_description) {
DDS_TopicQos default_topic_qos;
status = participant->get_default_topic_qos(default_topic_qos);
if (status != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default topic qos");
goto fail;
}
topic = participant->create_topic(
topic_str, type_name.c_str(),
default_topic_qos, NULL, DDS_STATUS_MASK_NONE);
if (!topic) {
RMW_SET_ERROR_MSG("failed to create topic");
goto fail;
}
} else {
DDS_Duration_t timeout = DDS_Duration_t::from_seconds(0);
topic = participant->find_topic(topic_str, timeout);
if (!topic) {
RMW_SET_ERROR_MSG("failed to find topic");
goto fail;
}
}
if (!get_datawriter_qos(participant, *qos_profile, datawriter_qos)) {
// error string was set within the function
goto fail;
}
topic_writer = dds_publisher->create_datawriter(
topic, datawriter_qos, NULL, DDS_STATUS_MASK_NONE);
if (!topic_writer) {
RMW_SET_ERROR_MSG("failed to create datawriter");
goto fail;
}
// Allocate memory for the ConnextStaticPublisherInfo object.
buf = rmw_allocate(sizeof(ConnextStaticPublisherInfo));
if (!buf) {
RMW_SET_ERROR_MSG("failed to allocate memory");
goto fail;
}
// Use a placement new to construct the ConnextStaticPublisherInfo in the preallocated buffer.
RMW_TRY_PLACEMENT_NEW(publisher_info, buf, goto fail, ConnextStaticPublisherInfo, )
buf = nullptr; // Only free the publisher_info pointer; don't need the buf pointer anymore.
publisher_info->dds_publisher_ = dds_publisher;
publisher_info->topic_writer_ = topic_writer;
publisher_info->callbacks_ = callbacks;
publisher_info->publisher_gid.implementation_identifier = rti_connext_identifier;
static_assert(
sizeof(ConnextPublisherGID) <= RMW_GID_STORAGE_SIZE,
"RMW_GID_STORAGE_SIZE insufficient to store the rmw_connext_cpp GID implemenation."
);
// Zero the data memory.
memset(publisher_info->publisher_gid.data, 0, RMW_GID_STORAGE_SIZE);
{
auto publisher_gid =
reinterpret_cast<ConnextPublisherGID *>(publisher_info->publisher_gid.data);
publisher_gid->publication_handle = topic_writer->get_instance_handle();
}
publisher_info->publisher_gid.implementation_identifier = rti_connext_identifier;
publisher->implementation_identifier = rti_connext_identifier;
publisher->data = publisher_info;
publisher->topic_name = reinterpret_cast<const char *>(rmw_allocate(strlen(topic_name) + 1));
if (!publisher->topic_name) {
RMW_SET_ERROR_MSG("failed to allocate memory for node name");
goto fail;
}
memcpy(const_cast<char *>(publisher->topic_name), topic_name, strlen(topic_name) + 1);
if (!qos_profile->avoid_ros_namespace_conventions) {
mangled_name =
std::string(publisher_qos.partition.name[0]) +
"/" +
topic_writer->get_topic()->get_name();
} else {
mangled_name = topic_name;
}
node_info->publisher_listener->add_information(
dds_publisher->get_instance_handle(), mangled_name.c_str(), type_name, EntityType::Publisher);
node_info->publisher_listener->trigger_graph_guard_condition();
// TODO(karsten1987): replace this block with logging macros
#ifdef DISCOVERY_DEBUG_LOGGING
fprintf(stderr, "******* Creating Publisher Details: ********\n");
fprintf(stderr, "Publisher partition %s\n", publisher_qos.partition.name[0]);
fprintf(stderr, "Publisher topic %s\n", topic_writer->get_topic()->get_name());
fprintf(stderr, "Publisher address %p\n", static_cast<void *>(dds_publisher));
fprintf(stderr, "******\n");
#endif
return publisher;
fail:
if (publisher) {
rmw_publisher_free(publisher);
}
// Assumption: participant is valid.
if (dds_publisher) {
if (topic_writer) {
if (dds_publisher->delete_datawriter(topic_writer) != DDS_RETCODE_OK) {
std::stringstream ss;
ss << "leaking datawriter while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (participant->delete_publisher(dds_publisher) != DDS_RETCODE_OK) {
std::stringstream ss;
ss << "leaking publisher while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (publisher_info) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
publisher_info->~ConnextStaticPublisherInfo(), ConnextStaticPublisherInfo)
rmw_free(publisher_info);
}
if (buf) {
rmw_free(buf);
}
return NULL;
}
rmw_ret_t
rmw_destroy_publisher(rmw_node_t * node, rmw_publisher_t * publisher)
{
if (!node) {
RMW_SET_ERROR_MSG("node handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
if (!publisher) {
RMW_SET_ERROR_MSG("publisher handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
publisher handle,
publisher->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
auto node_info = static_cast<ConnextNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return RMW_RET_ERROR;
}
auto participant = static_cast<DDSDomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return RMW_RET_ERROR;
}
// TODO(wjwwood): need to figure out when to unregister types with the participant.
ConnextStaticPublisherInfo * publisher_info =
static_cast<ConnextStaticPublisherInfo *>(publisher->data);
if (publisher_info) {
node_info->publisher_listener->remove_information(
publisher_info->dds_publisher_->get_instance_handle(), EntityType::Publisher);
node_info->publisher_listener->trigger_graph_guard_condition();
DDSPublisher * dds_publisher = publisher_info->dds_publisher_;
if (dds_publisher) {
if (publisher_info->topic_writer_) {
if (dds_publisher->delete_datawriter(publisher_info->topic_writer_) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete datawriter");
return RMW_RET_ERROR;
}
publisher_info->topic_writer_ = nullptr;
}
if (participant->delete_publisher(dds_publisher) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete publisher");
return RMW_RET_ERROR;
}
publisher_info->dds_publisher_ = nullptr;
} else if (publisher_info->topic_writer_) {
RMW_SET_ERROR_MSG("cannot delete datawriter because the publisher is null");
return RMW_RET_ERROR;
}
RMW_TRY_DESTRUCTOR(
publisher_info->~ConnextStaticPublisherInfo(),
ConnextStaticPublisherInfo, return RMW_RET_ERROR)
rmw_free(publisher_info);
publisher->data = nullptr;
}
if (publisher->topic_name) {
rmw_free(const_cast<char *>(publisher->topic_name));
}
rmw_publisher_free(publisher);
return RMW_RET_OK;
}
rmw_ret_t
rmw_publish(const rmw_publisher_t * publisher, const void * ros_message)
{
if (!publisher) {
RMW_SET_ERROR_MSG("publisher handle is null");
return RMW_RET_ERROR;
}
if (publisher->implementation_identifier != rti_connext_identifier) {
RMW_SET_ERROR_MSG("publisher handle is not from this rmw implementation");
return RMW_RET_ERROR;
}
if (!ros_message) {
RMW_SET_ERROR_MSG("ros message handle is null");
return RMW_RET_ERROR;
}
ConnextStaticPublisherInfo * publisher_info =
static_cast<ConnextStaticPublisherInfo *>(publisher->data);
if (!publisher_info) {
RMW_SET_ERROR_MSG("publisher info handle is null");
return RMW_RET_ERROR;
}
const message_type_support_callbacks_t * callbacks = publisher_info->callbacks_;
if (!callbacks) {
RMW_SET_ERROR_MSG("callbacks handle is null");
return RMW_RET_ERROR;
}
DDSDataWriter * topic_writer = publisher_info->topic_writer_;
if (!topic_writer) {
RMW_SET_ERROR_MSG("topic writer handle is null");
return RMW_RET_ERROR;
}
bool published = callbacks->publish(topic_writer, ros_message);
if (!published) {
RMW_SET_ERROR_MSG("failed to publish message");
return RMW_RET_ERROR;
}
return RMW_RET_OK;
}
rmw_subscription_t *
rmw_create_subscription(const rmw_node_t * node,
const rosidl_message_type_support_t * type_supports,
const char * topic_name,
const rmw_qos_profile_t * qos_profile,
bool ignore_local_publications)
{
if (!node) {
RMW_SET_ERROR_MSG("node handle is null");
return NULL;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, rti_connext_identifier,
return NULL)
RMW_CONNEXT_EXTRACT_MESSAGE_TYPESUPPORT(type_supports, type_support)
if (!qos_profile) {
RMW_SET_ERROR_MSG("qos_profile is null");
return nullptr;
}
auto node_info = static_cast<ConnextNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return NULL;
}
auto participant = static_cast<DDSDomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return NULL;
}
const message_type_support_callbacks_t * callbacks =
static_cast<const message_type_support_callbacks_t *>(type_support->data);
if (!callbacks) {
RMW_SET_ERROR_MSG("callbacks handle is null");
return NULL;
}
std::string type_name = _create_type_name(callbacks, "msg");
// Past this point, a failure results in unrolling code in the goto fail block.
bool registered;
DDS_DataReaderQos datareader_qos;
DDS_SubscriberQos subscriber_qos;
DDS_ReturnCode_t status;
DDSSubscriber * dds_subscriber = nullptr;
DDSTopic * topic = nullptr;
DDSTopicDescription * topic_description = nullptr;
DDSDataReader * topic_reader = nullptr;
DDSReadCondition * read_condition = nullptr;
void * buf = nullptr;
ConnextStaticSubscriberInfo * subscriber_info = nullptr;
rmw_subscription_t * subscription = nullptr;
std::string mangled_name;
// memory allocations for namespacing
char * partition_str = nullptr;
char * topic_str = nullptr;
// Begin initializing elements.
subscription = rmw_subscription_allocate();
if (!subscription) {
RMW_SET_ERROR_MSG("failed to allocate subscription");
goto fail;
}
registered = callbacks->register_type(participant, type_name.c_str());
if (!registered) {
RMW_SET_ERROR_MSG("failed to register type");
goto fail;
}
status = participant->get_default_subscriber_qos(subscriber_qos);
if (status != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default subscriber qos");
goto fail;
}
if (!_process_topic_name(
topic_name,
qos_profile->avoid_ros_namespace_conventions,
&topic_str,
&partition_str))
{
goto fail;
}
// we have to set the partition array to length 1
// and then set the partition_str in it
if (partition_str && strlen(partition_str) != 0) { // only set if not empty
subscriber_qos.partition.name.ensure_length(1, 1);
subscriber_qos.partition.name[0] = partition_str;
}
dds_subscriber = participant->create_subscriber(
subscriber_qos, NULL, DDS_STATUS_MASK_NONE);
if (!dds_subscriber) {
RMW_SET_ERROR_MSG("failed to create subscriber");
goto fail;
}
topic_description = participant->lookup_topicdescription(topic_str);
if (!topic_description) {
DDS_TopicQos default_topic_qos;
status = participant->get_default_topic_qos(default_topic_qos);
if (status != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to get default topic qos");
goto fail;
}
topic = participant->create_topic(
topic_str, type_name.c_str(),
default_topic_qos, NULL, DDS_STATUS_MASK_NONE);
if (!topic) {
RMW_SET_ERROR_MSG("failed to create topic");
goto fail;
}
} else {
DDS_Duration_t timeout = DDS_Duration_t::from_seconds(0);
topic = participant->find_topic(topic_str, timeout);
if (!topic) {
RMW_SET_ERROR_MSG("failed to find topic");
goto fail;
}
}
if (!get_datareader_qos(participant, *qos_profile, datareader_qos)) {
// error string was set within the function
goto fail;
}
topic_reader = dds_subscriber->create_datareader(
topic, datareader_qos,
NULL, DDS_STATUS_MASK_NONE);
if (!topic_reader) {
RMW_SET_ERROR_MSG("failed to create datareader");
goto fail;
}
read_condition = topic_reader->create_readcondition(
DDS_ANY_SAMPLE_STATE, DDS_ANY_VIEW_STATE, DDS_ANY_INSTANCE_STATE);
if (!read_condition) {
RMW_SET_ERROR_MSG("failed to create read condition");
goto fail;
}
// Allocate memory for the ConnextStaticSubscriberInfo object.
buf = rmw_allocate(sizeof(ConnextStaticSubscriberInfo));
if (!buf) {
RMW_SET_ERROR_MSG("failed to allocate memory");
goto fail;
}
// Use a placement new to construct the ConnextStaticSubscriberInfo in the preallocated buffer.
RMW_TRY_PLACEMENT_NEW(subscriber_info, buf, goto fail, ConnextStaticSubscriberInfo, )
buf = nullptr; // Only free the subscriber_info pointer; don't need the buf pointer anymore.
subscriber_info->dds_subscriber_ = dds_subscriber;
subscriber_info->topic_reader_ = topic_reader;
subscriber_info->read_condition_ = read_condition;
subscriber_info->callbacks_ = callbacks;
subscriber_info->ignore_local_publications = ignore_local_publications;
subscription->implementation_identifier = rti_connext_identifier;
subscription->data = subscriber_info;
subscription->topic_name = reinterpret_cast<const char *>(
rmw_allocate(strlen(topic_name) + 1));
if (!subscription->topic_name) {
RMW_SET_ERROR_MSG("failed to allocate memory for node name");
goto fail;
}
memcpy(const_cast<char *>(subscription->topic_name), topic_name, strlen(topic_name) + 1);
if (!qos_profile->avoid_ros_namespace_conventions) {
mangled_name =
std::string(subscriber_qos.partition.name[0]) +
"/" +
topic_reader->get_topicdescription()->get_name();
} else {
mangled_name = topic_name;
}
node_info->subscriber_listener->add_information(
dds_subscriber->get_instance_handle(),
mangled_name.c_str(),
type_name,
EntityType::Subscriber);
node_info->subscriber_listener->trigger_graph_guard_condition();
// TODO(karsten1987): replace this block with logging macros
#ifdef DISCOVERY_DEBUG_LOGGING
fprintf(stderr, "******* Creating Subscriber Details: ********\n");
fprintf(stderr, "Subscriber partition %s\n", subscriber_qos.partition.name[0]);
fprintf(stderr, "Subscriber topic %s\n", topic_reader->get_topicdescription()->get_name());
fprintf(stderr, "Subscriber address %p\n", static_cast<void *>(dds_subscriber));
fprintf(stderr, "******\n");
#endif
return subscription;
fail:
if (subscription) {
rmw_subscription_free(subscription);
}
// Assumption: participant is valid.
if (dds_subscriber) {
if (topic_reader) {
if (read_condition) {
if (topic_reader->delete_readcondition(read_condition) != DDS_RETCODE_OK) {
std::stringstream ss;
ss << "leaking readcondition while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (dds_subscriber->delete_datareader(topic_reader) != DDS_RETCODE_OK) {
std::stringstream ss;
ss << "leaking datareader while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (participant->delete_subscriber(dds_subscriber) != DDS_RETCODE_OK) {
std::stringstream ss;
std::cerr << "leaking subscriber while handling failure at " <<
__FILE__ << ":" << __LINE__ << '\n';
(std::cerr << ss.str()).flush();
}
}
if (subscriber_info) {
RMW_TRY_DESTRUCTOR_FROM_WITHIN_FAILURE(
subscriber_info->~ConnextStaticSubscriberInfo(), ConnextStaticSubscriberInfo)
rmw_free(subscriber_info);
}
if (buf) {
rmw_free(buf);
}
return NULL;
}
rmw_ret_t
rmw_destroy_subscription(rmw_node_t * node, rmw_subscription_t * subscription)
{
if (!node) {
RMW_SET_ERROR_MSG("node handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
node handle,
node->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
if (!subscription) {
RMW_SET_ERROR_MSG("subscription handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
subscription handle,
subscription->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
auto node_info = static_cast<ConnextNodeInfo *>(node->data);
if (!node_info) {
RMW_SET_ERROR_MSG("node info handle is null");
return RMW_RET_ERROR;
}
auto participant = static_cast<DDSDomainParticipant *>(node_info->participant);
if (!participant) {
RMW_SET_ERROR_MSG("participant handle is null");
return RMW_RET_ERROR;
}
// TODO(wjwwood): need to figure out when to unregister types with the participant.
auto result = RMW_RET_OK;
ConnextStaticSubscriberInfo * subscriber_info =
static_cast<ConnextStaticSubscriberInfo *>(subscription->data);
if (subscriber_info) {
node_info->subscriber_listener->remove_information(
subscriber_info->dds_subscriber_->get_instance_handle(), EntityType::Subscriber);
node_info->subscriber_listener->trigger_graph_guard_condition();
auto dds_subscriber = subscriber_info->dds_subscriber_;
if (dds_subscriber) {
auto topic_reader = subscriber_info->topic_reader_;
if (topic_reader) {
auto read_condition = subscriber_info->read_condition_;
if (read_condition) {
if (topic_reader->delete_readcondition(read_condition) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete readcondition");
result = RMW_RET_ERROR;
}
subscriber_info->read_condition_ = nullptr;
}
if (dds_subscriber->delete_datareader(topic_reader) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete datareader");
result = RMW_RET_ERROR;
}
subscriber_info->topic_reader_ = nullptr;
} else if (subscriber_info->read_condition_) {
RMW_SET_ERROR_MSG("cannot delete readcondition because the datareader is null");
result = RMW_RET_ERROR;
}
if (participant->delete_subscriber(dds_subscriber) != DDS_RETCODE_OK) {
RMW_SET_ERROR_MSG("failed to delete subscriber");
result = RMW_RET_ERROR;
}
subscriber_info->dds_subscriber_ = nullptr;
} else if (subscriber_info->topic_reader_) {
RMW_SET_ERROR_MSG("cannot delete datareader because the subscriber is null");
result = RMW_RET_ERROR;
}
RMW_TRY_DESTRUCTOR(
subscriber_info->~ConnextStaticSubscriberInfo(),
ConnextStaticSubscriberInfo, result = RMW_RET_ERROR)
rmw_free(subscriber_info);
subscription->data = nullptr;
}
if (subscription->topic_name) {
rmw_free(const_cast<char *>(subscription->topic_name));
}
rmw_subscription_free(subscription);
return result;
}
rmw_ret_t
_take(const rmw_subscription_t * subscription, void * ros_message, bool * taken,
DDS_InstanceHandle_t * sending_publication_handle)
{
if (!subscription) {
RMW_SET_ERROR_MSG("subscription handle is null");
return RMW_RET_ERROR;
}
RMW_CHECK_TYPE_IDENTIFIERS_MATCH(
subscription handle,
subscription->implementation_identifier, rti_connext_identifier,
return RMW_RET_ERROR)
if (!ros_message) {
RMW_SET_ERROR_MSG("ros message handle is null");
return RMW_RET_ERROR;
}
if (!taken) {
RMW_SET_ERROR_MSG("taken handle is null");
return RMW_RET_ERROR;
}
ConnextStaticSubscriberInfo * subscriber_info =
static_cast<ConnextStaticSubscriberInfo *>(subscription->data);
if (!subscriber_info) {
RMW_SET_ERROR_MSG("subscriber info handle is null");
return RMW_RET_ERROR;
}
DDSDataReader * topic_reader = subscriber_info->topic_reader_;
if (!topic_reader) {
RMW_SET_ERROR_MSG("topic reader handle is null");
return RMW_RET_ERROR;
}
const message_type_support_callbacks_t * callbacks = subscriber_info->callbacks_;
if (!callbacks) {
RMW_SET_ERROR_MSG("callbacks handle is null");
return RMW_RET_ERROR;
}
bool success = callbacks->take(
topic_reader, subscriber_info->ignore_local_publications, ros_message, taken,
sending_publication_handle);
return success ? RMW_RET_OK : RMW_RET_ERROR;
}
rmw_ret_t
rmw_take(const rmw_subscription_t * subscription, void * ros_message, bool * taken)
{
return _take(subscription, ros_message, taken, nullptr);
}
rmw_ret_t
rmw_take_with_info(
const rmw_subscription_t * subscription,
void * ros_message,
bool * taken,
rmw_message_info_t * message_info)
{
if (!message_info) {
RMW_SET_ERROR_MSG("message info is null");
return RMW_RET_ERROR;
}