This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrmw_request.cpp
More file actions
133 lines (109 loc) · 4.24 KB
/
rmw_request.cpp
File metadata and controls
133 lines (109 loc) · 4.24 KB
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
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// 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 <rmw/rmw.h>
#include <rmw/error_handling.h>
#include "./utils.hpp"
rmw_ret_t
rmw_send_request(
const rmw_client_t * client,
const void * ros_request,
int64_t * sequence_id)
{
if (!is_ertps_rmw_identifier_valid(client->implementation_identifier)) {
RMW_SET_ERROR_MSG("Wrong implementation");
return RMW_RET_INCORRECT_RMW_IMPLEMENTATION;
}
rmw_ertps_client_t * custom_client = reinterpret_cast<rmw_ertps_client_t *>(client->data);
const rosidl_message_type_support_t * req_members =
custom_client->type_support_callbacks->request_members_();
const message_type_support_callbacks_t * functions =
reinterpret_cast<const message_type_support_callbacks_t *>(req_members->data);
static uint8_t buffer[RMW_ERTPS_MAX_OUTPUT_BUFFER_SIZE];
// Encapsulation
buffer[0] = 0;
buffer[1] = 1;
buffer[2] = 0;
buffer[3] = 0;
ucdrBuffer mb;
ucdr_init_buffer(&mb, &buffer[4], sizeof(buffer) - 4);
bool written = functions->cdr_serialize(ros_request, &mb);
if (written) {
size_t size = ucdr_buffer_length(&mb) + 4;
const rtps::CacheChange * cache_change = custom_client->writer->newChange(
rtps::ChangeKind_t::ALIVE,
buffer, size);
*sequence_id =
(int64_t)cache_change->sequenceNumber.high << 32 |
(int64_t) cache_change->sequenceNumber.low; // NO_LINT
} else if (!written) {
RMW_SET_ERROR_MSG("error send request");
return RMW_RET_ERROR;
}
return RMW_RET_OK;
}
rmw_ret_t
rmw_take_request(
const rmw_service_t * service,
rmw_service_info_t * request_header,
void * ros_request,
bool * taken)
{
if (taken != NULL) {
*taken = false;
}
if (!is_ertps_rmw_identifier_valid(service->implementation_identifier)) {
RMW_SET_ERROR_MSG("Wrong implementation");
return RMW_RET_INCORRECT_RMW_IMPLEMENTATION;
}
rmw_ertps_service_t * custom_service = reinterpret_cast<rmw_ertps_service_t *>(service->data);
// Find first related item in static buffer memory pool
rmw_ertps_mempool_item_t * static_buffer_item =
rmw_ertps_find_static_input_buffer_by_owner(reinterpret_cast<void *>(custom_service));
if (static_buffer_item == NULL) {
return RMW_RET_ERROR;
}
rmw_ertps_static_input_buffer_t * static_buffer =
reinterpret_cast<rmw_ertps_static_input_buffer_t *>(static_buffer_item->data);
std::copy(
static_buffer->writer_guid.prefix.id.begin(),
static_buffer->writer_guid.prefix.id.begin() + 12, request_header->request_id.writer_guid);
std::copy(
static_buffer->writer_guid.prefix.id.begin() + 12,
static_buffer->writer_guid.prefix.id.begin() + 15, &request_header->request_id.writer_guid[12]);
std::copy(
static_buffer->writer_guid.prefix.id.begin() + 15,
static_buffer->writer_guid.prefix.id.begin() + 16, &request_header->request_id.writer_guid[15]);
request_header->request_id.sequence_number =
((int64_t) static_buffer->sequence_number.high << 32) |
(int64_t) static_buffer->sequence_number.low; // NO_LINT
const rosidl_message_type_support_t * req_members =
custom_service->type_support_callbacks->request_members_();
const message_type_support_callbacks_t * functions =
reinterpret_cast<const message_type_support_callbacks_t *>(req_members->data);
ucdrBuffer temp_buffer;
ucdr_init_buffer(
&temp_buffer,
&static_buffer->buffer[4],
static_buffer->length - 4);
bool deserialize_rv = functions->cdr_deserialize(&temp_buffer, ros_request);
put_memory(&static_buffer_memory, static_buffer_item);
if (taken != NULL) {
*taken = deserialize_rv;
}
if (!deserialize_rv) {
RMW_SET_ERROR_MSG("Typesupport desserialize error.");
return RMW_RET_ERROR;
}
return RMW_RET_OK;
}