Skip to content

Commit 0f961ce

Browse files
committed
Refactor
Signed-off-by: Pablo Garrido <pablogs9@gmail.com>
1 parent e1118ce commit 0f961ce

6 files changed

Lines changed: 104 additions & 41 deletions

File tree

rmw_microxrcedds_c/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ find_package(rmw REQUIRED)
3535
# Build options
3636
option(BUILD_DOCUMENTATION "Use doxygen to create product documentation" OFF)
3737
option(RMW_UXRCE_GRAPH "Allows to perform graph-related operations to the user" OFF)
38+
option(RMW_UROS_ERROR_HANDLING "Provides error handling callback functionality to user-space" ON)
3839

3940
if(RMW_UXRCE_GRAPH)
4041
find_package(micro_ros_msgs REQUIRED)
@@ -190,10 +191,10 @@ set(SRCS
190191
src/rmw_microros/time_sync.c
191192
src/rmw_microros/ping.c
192193
src/rmw_microros/timing.c
193-
src/rmw_microros/memory_error_handling.c
194194
$<$<OR:$<BOOL:${RMW_UXRCE_TRANSPORT_UDP}>,$<BOOL:${RMW_UXRCE_TRANSPORT_TCP}>>:src/rmw_microros/discovery.c>
195195
$<$<BOOL:${RMW_UXRCE_TRANSPORT_CUSTOM}>:src/rmw_microros/custom_transport.c>
196196
$<$<BOOL:${RMW_UXRCE_GRAPH}>:src/rmw_graph.c>
197+
$<$<BOOL:${RMW_UROS_ERROR_HANDLING}>:src/rmw_microros/error_handling.c>
197198
)
198199

199200
add_library(${PROJECT_NAME}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2021 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file
17+
*/
18+
19+
#ifndef RMW_MICROROS__ERROR_HANDLING_H_
20+
#define RMW_MICROROS__ERROR_HANDLING_H_
21+
22+
#include <ucdr/microcdr.h>
23+
24+
#if defined(__cplusplus)
25+
extern "C"
26+
{
27+
#endif // if defined(__cplusplus)
28+
29+
#ifdef RMW_UROS_ERROR_HANDLING
30+
31+
typedef enum
32+
{
33+
RMW_UROS_ERROR_ON_UNKNOWN = 0,
34+
RMW_UROS_ERROR_ON_NODE,
35+
RMW_UROS_ERROR_ON_TOPIC,
36+
RMW_UROS_ERROR_ON_SERVICE,
37+
RMW_UROS_ERROR_ON_CLIENT,
38+
RMW_UROS_ERROR_ON_SUBSCRIPTION,
39+
RMW_UROS_ERROR_ON_PUBLISHER
40+
} rmw_uros_error_entity_type_t;
41+
42+
typedef enum {
43+
RMW_UROS_ERROR_ENTITY_CREATION = 0,
44+
RMW_UROS_ERROR_MIDDLEWARE_ALLOCATION,
45+
} rmw_uros_error_source_t;
46+
47+
typedef struct {
48+
const char * node;
49+
const char * namespace;
50+
const char * topic_name;
51+
const ucdrBuffer * ucdr;
52+
const size_t size;
53+
} rmw_uros_error_context_t;
54+
55+
typedef void (* rmw_uros_error_handling)(
56+
const rmw_uros_error_source_t source,
57+
const rmw_uros_error_entity_type_t entity,
58+
const rmw_uros_error_context_t context);
59+
60+
/** \addtogroup rmw micro-ROS RMW API
61+
* @{
62+
*/
63+
64+
/**
65+
* \brief Sets the callback functions for handling error in static memory handling
66+
*
67+
* \param[in] error_cb callback to be triggered on static memory failure
68+
*/
69+
void rmw_uros_set_error_handling_callback(
70+
rmw_uros_error_handling error_cb);
71+
72+
#endif // RMW_UROS_ERROR_HANDLING
73+
74+
/** @}*/
75+
76+
#if defined(__cplusplus)
77+
}
78+
#endif // if defined(__cplusplus)
79+
80+
#endif // RMW_MICROROS__ERROR_HANDLING_H_

rmw_microxrcedds_c/src/callbacks.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
// limitations under the License.
1414

1515
#include <rmw_microros_internal/callbacks.h>
16+
#include <rmw_microros_internal/memory_error_handling_internal.h>
1617
#include <rmw/error_handling.h>
17-
#include <rmw_microros/memory_error_handling.h>
18-
19-
extern rmw_uros_memory_error_handling memory_error_callback;
2018

2119
void on_status(
2220
struct uxrSession * session,
@@ -79,10 +77,7 @@ void on_topic(
7977
if (!memory_node) {
8078
RMW_SET_ERROR_MSG("Not available static buffer memory node");
8179
UXR_UNLOCK(&static_buffer_memory.mutex);
82-
if (NULL != memory_error_callback) {
83-
memory_error_callback(
84-
"Not available static_buffer_memory for subscription", ub, length);
85-
}
80+
RMW_UROS_TRACE_ERROR(RMW_UROS_ERROR_ON_SUBSCRIPTION, RMW_UROS_ERROR_MIDDLEWARE_ALLOCATION, .node = custom_subscription->owner_node->node_name, .namespace = custom_subscription->owner_node->node_namespace, .topic_name = custom_subscription->topic_name, .ucdr = ub, .size = length);
8681
return;
8782
}
8883

@@ -135,10 +130,7 @@ void on_request(
135130
if (!memory_node) {
136131
RMW_SET_ERROR_MSG("Not available static buffer memory node");
137132
UXR_UNLOCK(&static_buffer_memory.mutex);
138-
if (NULL != memory_error_callback) {
139-
memory_error_callback(
140-
"Not available static_buffer_memory for request", ub, length);
141-
}
133+
// RMW_UROS_TRACE_ERROR(RMW_UROS_ERROR_MIDDLEWARE_ALLOCATION, .node = custom_service->owner_node->node_name, .namespace = custom_service->owner_node->node_namespace, .topic = custom_subscription->topic_name, .ucdr = ub, .size = length);
142134
return;
143135
}
144136

@@ -193,10 +185,7 @@ void on_reply(
193185
if (!memory_node) {
194186
RMW_SET_ERROR_MSG("Not available static buffer memory node");
195187
UXR_UNLOCK(&static_buffer_memory.mutex);
196-
if (NULL != memory_error_callback) {
197-
memory_error_callback(
198-
"Not available static_buffer_memory for reply", ub, length);
199-
}
188+
// RMW_UROS_TRACE_ERROR(RMW_UROS_ERROR_MIDDLEWARE_ALLOCATION, ub, length);
200189
return;
201190
}
202191

rmw_microxrcedds_c/src/config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#cmakedefine RMW_UXRCE_USE_REFS
1313
#cmakedefine RMW_UXRCE_ALLOW_DYNAMIC_ALLOCATIONS
1414
#cmakedefine RMW_UXRCE_GRAPH
15+
#cmakedefine RMW_UROS_ERROR_HANDLING
1516

1617
#ifdef RMW_UXRCE_TRANSPORT_UDP
1718
#define RMW_UXRCE_MAX_TRANSPORT_MTU UXR_CONFIG_UDP_TRANSPORT_MTU

rmw_microxrcedds_c/src/rmw_microros/memory_error_handling.c renamed to rmw_microxrcedds_c/src/rmw_microros/error_handling.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
// limitations under the License.
1414

1515
#include <rmw/rmw.h>
16-
#include <rmw_microros/memory_error_handling.h>
16+
#include <rmw_microros_internal/memory_error_handling_internal.h>
1717

18-
rmw_uros_memory_error_handling memory_error_callback = NULL;
18+
rmw_uros_error_handling error_callback = NULL;
1919

20-
void rmw_uros_set_memory_error_handling_callback(
21-
rmw_uros_memory_error_handling memory_error_cb)
20+
void rmw_uros_set_error_handling_callback(
21+
rmw_uros_error_handling error_cb)
2222
{
23-
memory_error_callback = memory_error_cb;
23+
error_callback = error_cb;
2424
}

rmw_microxrcedds_c/include/rmw_microros/memory_error_handling.h renamed to rmw_microxrcedds_c/src/rmw_microros_internal/memory_error_handling_internal.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,29 @@
1616
* @file
1717
*/
1818

19-
#ifndef RMW_MICROROS__MEMORY_ERROR_HANDLING_H_
20-
#define RMW_MICROROS__MEMORY_ERROR_HANDLING_H_
19+
#ifndef RMW_MICROROS_INTERNAL__ERROR_HANDLING_INTERNAL_H_
20+
#define RMW_MICROROS_INTERNAL__ERROR_HANDLING_INTERNAL_H_
2121

22-
#include <ucdr/microcdr.h>
22+
#include <rmw_microxrcedds_c/config.h>
23+
#include <rmw_microros/error_handling.h>
2324

2425
#if defined(__cplusplus)
2526
extern "C"
2627
{
2728
#endif // if defined(__cplusplus)
2829

29-
typedef void (* rmw_uros_memory_error_handling)(
30-
char * identifier,
31-
ucdrBuffer * ucdr,
32-
size_t size);
30+
#ifdef RMW_UROS_ERROR_HANDLING
31+
extern rmw_uros_error_handling error_callback;
3332

34-
/** \addtogroup rmw micro-ROS RMW API
35-
* @{
36-
*/
37-
38-
/**
39-
* \brief Sets the callback functions for handling error in static memory handling
40-
*
41-
* \param[in] memory_error_cb callback to be triggered on static memory failure
42-
*/
43-
void rmw_uros_set_memory_error_handling_callback(
44-
rmw_uros_memory_error_handling memory_error_cb);
33+
// #define RMW_UROS_TRACE_ERROR(source, context) if(NULL != error_callback) {error_callback(source, (rmw_uros_error_context_t) context);}
34+
#define RMW_UROS_TRACE_ERROR(entity, source, ...) if(NULL != error_callback) {error_callback(entity, source, (rmw_uros_error_context_t){__VA_ARGS__});}
4535

46-
/** @}*/
36+
#else
37+
#define RMW_UROS_TRACE_ERROR(source, context)
38+
#endif // RMW_UROS_ERROR_HANDLING
4739

4840
#if defined(__cplusplus)
4941
}
5042
#endif // if defined(__cplusplus)
5143

52-
#endif // RMW_MICROROS__MEMORY_ERROR_HANDLING_H_
44+
#endif // RMW_MICROROS_INTERNAL__ERROR_HANDLING_INTERNAL_H_

0 commit comments

Comments
 (0)