Add dds_get_guid to get the GUID of a local entity

This is merely a more convenient way of obtaining it: otherwise one has
subscribe to the correct built-in topic, read the sample corresponding
to the entity's instance handle and get the "key" field.  That's a bit
of a detour to get the network-wide unique identifier.

Signed-off-by: Erik Boasson <eb@ilities.com>
This commit is contained in:
Erik Boasson 2020-04-09 13:49:10 +02:00 committed by eboasson
parent b2cf6921da
commit 45c0f432a9
3 changed files with 73 additions and 0 deletions

View file

@ -418,6 +418,26 @@ dds_get_mask(dds_entity_t condition, uint32_t *mask);
DDS_EXPORT dds_return_t DDS_EXPORT dds_return_t
dds_get_instance_handle(dds_entity_t entity, dds_instance_handle_t *ihdl); dds_get_instance_handle(dds_entity_t entity, dds_instance_handle_t *ihdl);
/**
* @brief Returns the GUID that represents the entity in the network,
* and therefore only supports participants, readers and writers.
*
* @param[in] entity Entity of which to get the instance handle.
* @param[out] guid Where to store the GUID.
*
* @returns A dds_return_t indicating success or failure.
*
* @retval DDS_RETCODE_OK
* Success.
* @retval DDS_RETCODE_ILLEGAL_OPERATION
* The operation is invoked on an inappropriate object.
* @retval DDS_RETCODE_ERROR
* An internal error has occurred.
*/
/* TODO: Check list of return codes is complete. */
DDS_EXPORT dds_return_t
dds_get_guid (dds_entity_t entity, dds_guid_t *guid);
/* /*
All entities have a set of "status conditions" (following the DCPS All entities have a set of "status conditions" (following the DCPS
spec), read peeks, take reads & resets (analogously to read & take spec), read peeks, take reads & resets (analogously to read & take

View file

@ -25,6 +25,7 @@
#include "dds/ddsi/ddsi_pmd.h" #include "dds/ddsi/ddsi_pmd.h"
#include "dds/ddsi/ddsi_xqos.h" #include "dds/ddsi/ddsi_xqos.h"
#include "dds/ddsi/q_transmit.h" #include "dds/ddsi/q_transmit.h"
#include "dds/ddsi/q_bswap.h"
extern inline dds_entity *dds_entity_from_handle_link (struct dds_handle_link *hdllink); extern inline dds_entity *dds_entity_from_handle_link (struct dds_handle_link *hdllink);
extern inline bool dds_entity_is_enabled (const dds_entity *e); extern inline bool dds_entity_is_enabled (const dds_entity *e);
@ -1285,6 +1286,36 @@ dds_return_t dds_get_instance_handle (dds_entity_t entity, dds_instance_handle_t
return ret; return ret;
} }
dds_return_t dds_get_guid (dds_entity_t entity, dds_guid_t *guid)
{
dds_entity *e;
dds_return_t ret;
if (guid == NULL)
return DDS_RETCODE_BAD_PARAMETER;
if ((ret = dds_entity_pin (entity, &e)) != DDS_RETCODE_OK)
return ret;
switch (dds_entity_kind (e))
{
case DDS_KIND_PARTICIPANT:
case DDS_KIND_READER:
case DDS_KIND_WRITER: {
DDSRT_STATIC_ASSERT (sizeof (dds_guid_t) == sizeof (ddsi_guid_t));
ddsi_guid_t tmp = nn_ntoh_guid (e->m_guid);
memcpy (guid, &tmp, sizeof (*guid));
ret = DDS_RETCODE_OK;
break;
}
default: {
ret = DDS_RETCODE_ILLEGAL_OPERATION;
break;
}
}
dds_entity_unpin(e);
return ret;
}
dds_return_t dds_entity_pin (dds_entity_t hdl, dds_entity **eptr) dds_return_t dds_entity_pin (dds_entity_t hdl, dds_entity **eptr)
{ {
dds_return_t hres; dds_return_t hres;

View file

@ -255,6 +255,28 @@ CU_Test(ddsc_entity, status, .init = create_entity, .fini = delete_entity)
CU_ASSERT_EQUAL_FATAL(status1, DDS_RETCODE_OK); CU_ASSERT_EQUAL_FATAL(status1, DDS_RETCODE_OK);
} }
CU_Test(ddsc_entity, guid, .init = create_entity, .fini = delete_entity)
{
dds_return_t status;
dds_guid_t guid, zero;
memset(&zero, 0, sizeof(zero));
/* Don't check actual handle contents. That's a job
* for the specific entity children, not for the generic part. */
/* Check getting Handle with bad parameters. */
status = dds_get_guid (0, NULL);
CU_ASSERT_EQUAL_FATAL(status, DDS_RETCODE_BAD_PARAMETER);
status = dds_get_guid (entity, NULL);
CU_ASSERT_EQUAL_FATAL(status, DDS_RETCODE_BAD_PARAMETER);
status = dds_get_guid (0, &guid);
CU_ASSERT_EQUAL_FATAL(status, DDS_RETCODE_BAD_PARAMETER);
/* Get Instance Handle, which should not be 0 for a participant. */
status = dds_get_guid (entity, &guid);
CU_ASSERT_EQUAL_FATAL(status, DDS_RETCODE_OK);
CU_ASSERT_FATAL(memcmp(&guid, &zero, sizeof(guid)) != 0);
}
CU_Test(ddsc_entity, instance_handle, .init = create_entity, .fini = delete_entity) CU_Test(ddsc_entity, instance_handle, .init = create_entity, .fini = delete_entity)
{ {