/*
µRtWare is a lightweight publish/subscribe middleware for real-time
applications. It was developed as part of the software habitat for the
Autonomous Mini Robot [1] (AMiRo) but can be used for other purposes as well.
Copyright (C) 2018..2020 Thomas Schöpping et al.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
#include
/******************************************************************************/
/* LOCAL DEFINITIONS */
/******************************************************************************/
/******************************************************************************/
/* EXPORTED VARIABLES */
/******************************************************************************/
/******************************************************************************/
/* LOCAL TYPES */
/******************************************************************************/
/******************************************************************************/
/* LOCAL VARIABLES */
/******************************************************************************/
/******************************************************************************/
/* LOCAL FUNCTIONS */
/******************************************************************************/
/******************************************************************************/
/* EXPORTED FUNCTIONS */
/******************************************************************************/
/**
* @brief Initialize the topic.
*
* @param[in] topic The topic to initialize. Must not be NULL.
* @param[in] id Unique, numeric identifier of the topic.
* @param[in] mandatoryMessage Starting message of the topic. Must not be NULL.
*
* @return Returns URT_STATUS_OK on success.
* Returns URT_STATUS_TOPIC_DUPLICATE if another topic with the same identifier already exists.
*/
urt_status_t urtTopicInit(urt_topic_t* topic, urt_topicid_t id, urt_message_t* mandatoryMessage)
{
urtDebugAssert(topic);
urtDebugAssert(mandatoryMessage);
topic->next = NULL;
topic->id = id;
urtMutexInit(&topic->lock);
urtEventSourceInit(&topic->evtSource);
topic->numHrtSubscribers = 0;
urtCondvarInit(&topic->hrtReleased);
topic->mandatoryMessage = *mandatoryMessage;
topic->latestMessage = &topic->mandatoryMessage;
#if (URT_CFG_PUBSUB_QOS_RATECHECKS)
topic->hrtSubscribers = nullptr;
//add later: timer init;
#endif /* URT_CFG_PUBSUB_QOS_RATECHECKS */
#if (URT_CFG_PUBSUB_PROFILING)
topic->numMessagesPublished = 0;
topic->numMessagesDiscarded = 0;
topic->numSubscribers = 0;
#endif /* URT_CFG_PUBSUB_PROFILING */
topic->latestMessage->next = topic->latestMessage;
urtMutexLock(&topic->lock);
urt_topic_t* topicTemp = topic;
while (topicTemp != NULL && topicTemp->id < id)
topicTemp = topicTemp->next;
if (topicTemp == NULL)
{
urtCoreAddTopic(topic);
urtMutexUnlock(&topic->lock);
return URT_STATUS_OK;
}
else if (topicTemp->id > id)
{
topicTemp->next = topic;
urtMutexUnlock(&topic->lock);
return URT_STATUS_OK;
}
else
{
urtMutexUnlock(&topic->lock);
return URT_STATUS_TOPIC_DUPLICATE;
}
}