Skip to content

Commit

Permalink
Add TCP transport bases on WifiClient API
Browse files Browse the repository at this point in the history
  • Loading branch information
nhjschulz committed Oct 25, 2024
1 parent 694ff9e commit 7a7ec91
Show file tree
Hide file tree
Showing 11 changed files with 1,194 additions and 242 deletions.
188 changes: 7 additions & 181 deletions lib/APPTurtle/src/CustomRosTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
DESCRIPTION
*******************************************************************************/
/**
* @brief Custom Micro-ROS transport.
* @author Gabryel Reyes <[email protected]>
* @brief Custom Micro-ROS transport class selector
* @author Norbert Schulz <[email protected]>
*
* @addtogroup Application
*
Expand All @@ -38,185 +38,11 @@
/******************************************************************************
* Compile Switches
*****************************************************************************/

/******************************************************************************
* Includes
*****************************************************************************/
#include <IPAddress.h>
#include <rmw_microros/rmw_microros.h>
#include <WiFiUdp.h>

/******************************************************************************
* Macros
*****************************************************************************/

/******************************************************************************
* Types and Classes
*****************************************************************************/

/**
* Micro-ROS custom transport adaption.
*
* The static functions are used as these are called from C-language
*/
class CustomRosTransport
{
public:
/**
* Constructs a custom Micro-ROS transport.
*/
CustomRosTransport() : m_udpClient(), m_address(), m_port(DEFAULT_PORT)
{
}

/**
* Destroys custom Micro-ROS transport.
*
*/
~CustomRosTransport()
{
}

/**
* Initialize custom ROS transport with agent address and port.
*
* @param[in] addr Micro-ROS agent IP-address
* @param[in] port Micro-ROS agent port
*/
void init(const IPAddress& addr, uint16_t port)
{
m_address = addr;
m_port = port;
}

/**
* Get IP-address of Micro-ROS agent.
*
* @return IP-address
*/
const IPAddress& getIPAddress() const
{
return m_address;
}

/**
* Get IP-address of Micro-ROS agent as string
*
* @return IP-address as string
*/
const String getIPAddressAsStr() const
{
return m_address.toString();
}

/**
* Get port of Micro-ROS agent.
*
* @return Port
*/
uint16_t getPort() const
{
return m_port;
}

/**
* Open and initialize the custom transport (C-Entry Point).
* https://micro.ros.org/docs/tutorials/advanced/create_custom_transports/
*
* @param[in] transport The arguments passed through uxr_init_custom_transport.
*
* @return A boolean indicating if the opening was successful.
*/
static bool open(uxrCustomTransport* transport);

/**
* Close the custom transport (C-Entry Point).
* https://micro.ros.org/docs/tutorials/advanced/create_custom_transports/
*
* @param[in] transport The arguments passed through uxr_init_custom_transport.
*
* @return A boolean indicating if the closing was successful.
*/
static bool close(uxrCustomTransport* transport);

/**
* Write data to the custom transport (C-Entry Point).
* https://micro.ros.org/docs/tutorials/advanced/create_custom_transports/
*
* @param[in] transport The arguments passed through uxr_init_custom_transport.
* @param[in] buffer The buffer to write.
* @param[in] size The size of the buffer.
* @param[out] errorCode The error code.
*
* @return The number of bytes written.
*/
static size_t write(uxrCustomTransport* transport, const uint8_t* buffer, size_t size, uint8_t* errorCode);

/**
* Read data from the custom transport (C-Entry Point).
* https://micro.ros.org/docs/tutorials/advanced/create_custom_transports/
*
* @param[in] transport The arguments passed through uxr_init_custom_transport.
* @param[out] buffer The buffer to read into.
* @param[in] size The size of the buffer.
* @param[in] timeout The timeout in milliseconds.
* @param[out] errorCode The error code.
*
* @return The number of bytes read.
*/
static size_t read(uxrCustomTransport* transport, uint8_t* buffer, size_t size, int timeout, uint8_t* errorCode);

private:
/**
* Open and initialize the custom transport.
*
* @return A boolean indicating if the opening was successful.
*/
bool open(void);

/**
* Close the custom transport.
*
* @return A boolean indicating if the closing was successful.
*/
bool close(void);

/**
* Write data to the custom transport.
*
* @param[in] buffer The buffer to write.
* @param[in] size The size of the buffer.
* @param[out] errorCode The error code.
*
* @return The number of bytes written.
*/
size_t write(const uint8_t* buffer, size_t size, uint8_t* errorCode);

/**
* Read data from the custom transport.
*
* @param[out] buffer The buffer to read into.
* @param[in] size The size of the buffer.
* @param[in] timeout The timeout in milliseconds.
* @param[out] errorCode The error code.
*
* @return The number of bytes read.
*/
size_t read(uint8_t* buffer, size_t size, int timeout, uint8_t* errorCode);

/**
* Default Micro-ROS agent port.
*/
static const int DEFAULT_PORT = 8888;

WiFiUDP m_udpClient; /**< UDP client */
IPAddress m_address; /**< IP address of the Micro-ROS agent */
uint16_t m_port; /**< Port of the Micro-ROS agent */
};

/******************************************************************************
* Functions
*****************************************************************************/
#if defined(TRANSPORT_USE_TCP) && TRANSPORT_USE_TCP != 0
#include "Transports/CustomRosTransportTcp.h"
#else
#include "Transports/CustomRosTransportUdp.h"
#endif /* else defined(TRANSPORT_USE_TCP) && TRANSPORT_USE_TCP != 0 */

#endif /* CUSTOM_ROS_TRANSPORT_H */
/** @} */
10 changes: 7 additions & 3 deletions lib/APPTurtle/src/MicroRosClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ bool MicroRosClient::setupCustomTransport(const IPAddress& ipAddress, uint16_t p

m_customRosTransport.init(ipAddress, port);

if (RCL_RET_OK != rmw_uros_set_custom_transport(false, (void*)&m_customRosTransport, CustomRosTransport::open,
CustomRosTransport::close, CustomRosTransport::write,
CustomRosTransport::read))
if (RCL_RET_OK != rmw_uros_set_custom_transport(
false,
&m_customRosTransport,
CustomRosTransportBase::open,
CustomRosTransportBase::close,
CustomRosTransportBase::write,
CustomRosTransportBase::read))
{
LOG_ERROR("Failed to set custom transport for Micro-ROS.");
}
Expand Down
128 changes: 128 additions & 0 deletions lib/APPTurtle/src/Transports/CustomRosTransportBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* MIT License
*
* Copyright (c) 2023 - 2024 Andreas Merkle <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/*******************************************************************************
DESCRIPTION
*******************************************************************************/
/**
* @brief Custom Transport class with C-language interface for microros.
* @author Norbert Schulz <[email protected]>
/******************************************************************************
* Includes
*****************************************************************************/
#include "CustomRosTransportBase.h"

#include <Logging.h>
/******************************************************************************
* Compiler Switches
*****************************************************************************/

/******************************************************************************
* Macros
*****************************************************************************/

/******************************************************************************
* Types and classes
*****************************************************************************/

/******************************************************************************
* Prototypes
*****************************************************************************/

/**
* Unwrap pointer to CustomRosTransportBase from uxrCustomTransport structure.
*
* This is used by the static C-Languge entry points to forward the request
* to the matching C++ member function.
*
* @param[in] transport The custom transport data structure pointer.
*
* @return The this pointer to transport owning CustomRosTransportBase class.
*/
static inline CustomRosTransportBase* toThis(const uxrCustomTransport* transport);

/******************************************************************************
* Local Variables
*****************************************************************************/

/******************************************************************************
* Public Methods
*****************************************************************************/

bool CustomRosTransportBase::open(uxrCustomTransport* transport)
{
CustomRosTransportBase * tp = toThis(transport);
return (nullptr != tp) ? tp->open() : false;
}

bool CustomRosTransportBase::close(uxrCustomTransport* transport)
{
CustomRosTransportBase * tp = toThis(transport);
return (nullptr != tp) ? tp->close() : false;
}

size_t CustomRosTransportBase::write(uxrCustomTransport* transport, const uint8_t* buffer, size_t size, uint8_t* errorCode)
{
CustomRosTransportBase * tp = toThis(transport);
return (nullptr != tp) ? tp->write(buffer, size, errorCode) : 0U;
}

size_t CustomRosTransportBase::read(uxrCustomTransport* transport, uint8_t* buffer, size_t size, int timeout,
uint8_t* errorCode)
{
CustomRosTransportBase * tp = toThis(transport);
return (nullptr != tp) ? tp->read(buffer, size, timeout, errorCode) : 0U;
}

/******************************************************************************
* Protected Methods
*****************************************************************************/

/******************************************************************************
* Private Methods
*****************************************************************************/

/******************************************************************************
* External Functions
*****************************************************************************/

/******************************************************************************
* Local Functions
*****************************************************************************/

static inline CustomRosTransportBase* toThis(const uxrCustomTransport* transport)
{
CustomRosTransportBase* transportClass = nullptr;

if (nullptr == transport)
{
LOG_FATAL("Invalid uxrCustomTransport pointer.");
}
else
{
transportClass = reinterpret_cast<CustomRosTransportBase*>(transport->args);
}

return transportClass;
}
Loading

0 comments on commit 7a7ec91

Please sign in to comment.