Skip to content

Commit f5c2a03

Browse files
committed
improve v0.5.1: implement setIPAddrRTPS for mros2-esp32 and error check whether network can be connected
also check: #45
1 parent 365ec59 commit f5c2a03

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

include/mros2.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,16 @@ namespace mros2
8686

8787
void spin();
8888

89-
void setIPAddrRTPS(std::array<uint8_t, 4> ipaddr);
89+
#ifdef __MBED__
90+
int setIPAddrRTPS(std::array<uint8_t, 4> ipaddr);
91+
#endif /* __MBED__ */
9092

9193
} /* namespace mros2 */
9294

95+
#ifndef __MBED__
96+
extern "C" int mros2_setIPAddrRTPS(uint32_t ipaddr);
97+
#endif /* __MBED__ */
98+
9399
namespace message_traits
94100
{
95101
template <class T>

src/mros2.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,50 @@ namespace mros2
303303
}
304304
}
305305

306-
void setIPAddrRTPS(std::array<uint8_t, 4> ipaddr)
306+
/* implementation for mros2-mbed */
307+
#ifdef __MBED__
308+
int setIPAddrRTPS(std::array<uint8_t, 4> ipaddr)
307309
{
310+
/* check whether IP address has been obtained */
311+
if (!(ipaddr[0] + ipaddr[1] + ipaddr[2] + ipaddr[3]))
312+
{
313+
MROS2_ERROR("IP address has not been obtained!");
314+
return 0;
315+
}
316+
308317
rtps::Config::IP_ADDRESS = ipaddr;
318+
MROS2_DEBUG("[MROS2LIB] set \"%d.%d.%d.%d\" for RTPS communication",
319+
rtps::Config::IP_ADDRESS[0], rtps::Config::IP_ADDRESS[1], rtps::Config::IP_ADDRESS[2], rtps::Config::IP_ADDRESS[3]);
309320

310-
MROS2_DEBUG("[MROS2LIB] set IP address for RTPS communication");
321+
return 1;
311322
}
323+
#endif /* __MBED__ */
312324

313325
} /* namespace mros2 */
314326

327+
/* implementation for mros2-esp32 */
328+
#ifndef __MBED__
329+
extern "C" int mros2_setIPAddrRTPS(uint32_t ipaddr)
330+
{
331+
/* check whether IP address has been obtained */
332+
if (!ipaddr)
333+
{
334+
MROS2_ERROR("IP address has not been obtained!");
335+
return 0;
336+
}
337+
338+
std::array<uint8_t, 4> rtps_ipaddr;
339+
for (int i = 0; i < 4; i++)
340+
rtps_ipaddr[i] = ipaddr >> (i * 8);
341+
342+
rtps::Config::IP_ADDRESS = rtps_ipaddr;
343+
MROS2_DEBUG("[MROS2LIB] set \"%d.%d.%d.%d\" for RTPS communication",
344+
rtps::Config::IP_ADDRESS[0], rtps::Config::IP_ADDRESS[1], rtps::Config::IP_ADDRESS[2], rtps::Config::IP_ADDRESS[3]);
345+
346+
return 1;
347+
}
348+
#endif /* __MBED__ */
349+
315350
/*
316351
* Declaration for embeddedRTPS participants
317352
*/

0 commit comments

Comments
 (0)