urtware / src / urt_core.c @ e566c39e
History | View | Annotate | Download (6.264 KB)
| 1 | 1fb06240 | skenneweg | /*
|
|---|---|---|---|
| 2 | µRtWare is a lightweight publish/subscribe middleware for real-time
|
||
| 3 | applications. It was developed as part of the software habitat for the
|
||
| 4 | Autonomous Mini Robot [1] (AMiRo) but can be used for other purposes as well.
|
||
| 5 | |||
| 6 | Copyright (C) 2018..2020 Thomas Schöpping et al.
|
||
| 7 | |||
| 8 | This program is free software: you can redistribute it and/or modify
|
||
| 9 | it under the terms of the GNU General Public License as published by
|
||
| 10 | the Free Software Foundation, either version 3 of the License, or
|
||
| 11 | (at your option) any later version.
|
||
| 12 | |||
| 13 | This program is distributed in the hope that it will be useful,
|
||
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 16 | GNU General Public License for more details.
|
||
| 17 | |||
| 18 | You should have received a copy of the GNU General Public License
|
||
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 20 | */
|
||
| 21 | |||
| 22 | 7d9678db | skenneweg | #include <urtware.h> |
| 23 | |||
| 24 | 1fb06240 | skenneweg | /******************************************************************************/
|
| 25 | /* LOCAL DEFINITIONS */
|
||
| 26 | /******************************************************************************/
|
||
| 27 | |||
| 28 | /******************************************************************************/
|
||
| 29 | /* EXPORTED VARIABLES */
|
||
| 30 | /******************************************************************************/
|
||
| 31 | |||
| 32 | /******************************************************************************/
|
||
| 33 | /* LOCAL TYPES */
|
||
| 34 | /******************************************************************************/
|
||
| 35 | |||
| 36 | 64fde4ba | skenneweg | urt_core_t core; |
| 37 | |||
| 38 | 1fb06240 | skenneweg | /******************************************************************************/
|
| 39 | /* LOCAL VARIABLES */
|
||
| 40 | /******************************************************************************/
|
||
| 41 | |||
| 42 | /******************************************************************************/
|
||
| 43 | /* LOCAL FUNCTIONS */
|
||
| 44 | /******************************************************************************/
|
||
| 45 | |||
| 46 | /******************************************************************************/
|
||
| 47 | /* EXPORTED FUNCTIONS */
|
||
| 48 | /******************************************************************************/
|
||
| 49 | 7d9678db | skenneweg | |
| 50 | /**
|
||
| 51 | * @brief Initialize the Core.
|
||
| 52 | */
|
||
| 53 | 1f7ffcff | skenneweg | void urtCoreInit(void) |
| 54 | {
|
||
| 55 | 64fde4ba | skenneweg | core._nodes = NULL;
|
| 56 | core._status = URT_STATUS_OK; |
||
| 57 | urtEventSourceInit(core._evtSource); |
||
| 58 | urtMutexInit(&core._lock); |
||
| 59 | 1f7ffcff | skenneweg | #if (URT_CFG_PUBSUB_ENABLED)
|
| 60 | 64fde4ba | skenneweg | core._topics = NULL;
|
| 61 | 1f7ffcff | skenneweg | #endif /* URT_CFG_PUBSUB_ENABLED */ |
| 62 | #if (URT_CFG_RPC_ENABLED)
|
||
| 63 | 64fde4ba | skenneweg | core.urt_service_t = NULL;
|
| 64 | 1f7ffcff | skenneweg | #endif /* URT_CFG_RPC_ENABLED */ |
| 65 | return;
|
||
| 66 | } |
||
| 67 | 7d9678db | skenneweg | |
| 68 | /**
|
||
| 69 | * @brief Get Core status.
|
||
| 70 | *
|
||
| 71 | 5198dfae | skenneweg | * @return Current system status.
|
| 72 | 7d9678db | skenneweg | */
|
| 73 | 64fde4ba | skenneweg | urt_status_t urtCoreGetStatus(void)
|
| 74 | {
|
||
| 75 | return core._status;
|
||
| 76 | } |
||
| 77 | 7d9678db | skenneweg | |
| 78 | /**
|
||
| 79 | 5198dfae | skenneweg | * @brief Start threads of all nodes of the Core.
|
| 80 | 7d9678db | skenneweg | */
|
| 81 | 64fde4ba | skenneweg | void urtCoreStartNodes(void) |
| 82 | {
|
||
| 83 | e566c39e | skenneweg | urtMutexLock(&core._lock); |
| 84 | 64fde4ba | skenneweg | urt_node_t* node = core._nodes; |
| 85 | while (node)
|
||
| 86 | {
|
||
| 87 | urtThreadStart(node->thread); |
||
| 88 | node = node->next; |
||
| 89 | } |
||
| 90 | e566c39e | skenneweg | urtMutexUnlock(&core._lock); |
| 91 | 64fde4ba | skenneweg | return;
|
| 92 | } |
||
| 93 | 7d9678db | skenneweg | |
| 94 | /**
|
||
| 95 | 5198dfae | skenneweg | * @brief Synchronize all nodes of the core.
|
| 96 | 7d9678db | skenneweg | *
|
| 97 | 5198dfae | skenneweg | * @param[in] node Pointer to a node to synchronize. Must not be NULL.
|
| 98 | 7d9678db | skenneweg | *
|
| 99 | 5198dfae | skenneweg | * @return Returns URT_STATUS_OK if all nodes are synchronized and proceed.
|
| 100 | * Returns URT_STATUS_SYNC_ERROR if an exception occurred (faulty stage value detected).
|
||
| 101 | * Returns URT_STATUS_SYNC_PENDING if there are nodes left to synchronize.
|
||
| 102 | * In the latter case, the node thread must still wait for the control event (proceed) to synchronize.
|
||
| 103 | 7d9678db | skenneweg | */
|
| 104 | e566c39e | skenneweg | urt_status_t urtCoreSynchronizeNodes(urt_node_t* node) |
| 105 | 64fde4ba | skenneweg | {
|
| 106 | e566c39e | skenneweg | urtMutexLock(&core._lock); |
| 107 | node->stage -= 1;
|
||
| 108 | 64fde4ba | skenneweg | urt_node_t* nodeFromCore = core._nodes; |
| 109 | e566c39e | skenneweg | while (nodeFromCore && nodeFromCore->stage == node->stage)
|
| 110 | 64fde4ba | skenneweg | {
|
| 111 | nodeFromCore = nodeFromCore->next; |
||
| 112 | } |
||
| 113 | e566c39e | skenneweg | if (nodeFromCore)
|
| 114 | 64fde4ba | skenneweg | {
|
| 115 | e566c39e | skenneweg | urt_osEventFlags_t flag = 0; /*TODO: set to proceed? */ |
| 116 | urtEventSourceBroadcast(core._evtSource, flag); |
||
| 117 | urtMutexUnlock(&core._lock); |
||
| 118 | 64fde4ba | skenneweg | return URT_STATUS_OK;
|
| 119 | } |
||
| 120 | e566c39e | skenneweg | else if (nodeFromCore->stage == (node->stage - 1)) |
| 121 | 64fde4ba | skenneweg | {
|
| 122 | e566c39e | skenneweg | urtMutexUnlock(&core._lock); |
| 123 | 64fde4ba | skenneweg | return URT_STATUS_SYNC_PENDING;
|
| 124 | } |
||
| 125 | else
|
||
| 126 | {
|
||
| 127 | urtCoreStopNodes(URT_STATUS_SYNC_ERROR); |
||
| 128 | e566c39e | skenneweg | urtMutexUnlock(&core._lock); |
| 129 | 64fde4ba | skenneweg | return URT_STATUS_SYNC_ERROR;
|
| 130 | } |
||
| 131 | } |
||
| 132 | 7d9678db | skenneweg | |
| 133 | /**
|
||
| 134 | 5198dfae | skenneweg | * @brief Stop threads of all nodes of the Core.
|
| 135 | 7d9678db | skenneweg | *
|
| 136 | 5198dfae | skenneweg | * @param[in] reason The reason why the function was called. For normal shutdown URT_STATUS_OK should be used.
|
| 137 | 7d9678db | skenneweg | *
|
| 138 | 5198dfae | skenneweg | * @return Returns URT_STATUS_OK if there was no call with another reason than URT_STATUS_OK before.
|
| 139 | * If the function has been called before with a different reason, that reason is returned.
|
||
| 140 | 7d9678db | skenneweg | */
|
| 141 | 64fde4ba | skenneweg | urt_status_t urtCoreStopNodes(urt_status_t reason) |
| 142 | {
|
||
| 143 | e566c39e | skenneweg | urtMutexLock(&core._lock); |
| 144 | bool priorityBoosted = false; |
||
| 145 | urt_osThreadPrio_t oldPrio; |
||
| 146 | |||
| 147 | 64fde4ba | skenneweg | if (core._status == URT_STATUS_OK)
|
| 148 | {
|
||
| 149 | if (core._nodes->thread->prio < URT_THREAD_PRIO_HIGH_MAX)
|
||
| 150 | {
|
||
| 151 | e566c39e | skenneweg | oldPrio = core._nodes->thread->prio; |
| 152 | priorityBoosted = true;
|
||
| 153 | 64fde4ba | skenneweg | core._nodes->thread->prio = URT_THREAD_PRIO_HIGH_MAX; |
| 154 | } |
||
| 155 | e566c39e | skenneweg | core._status = reason; |
| 156 | urt_node_t* node = core._nodes; |
||
| 157 | while (node)
|
||
| 158 | 64fde4ba | skenneweg | {
|
| 159 | e566c39e | skenneweg | urtThreadTerminate(node->thread, URT_THREAD_TERMINATE_REQUEST); |
| 160 | node = node->next; |
||
| 161 | 64fde4ba | skenneweg | } |
| 162 | e566c39e | skenneweg | urt_osEventFlags_t flag = 0; /*TODO: set to terminate? */ |
| 163 | urtEventSourceBroadcast(core._evtSource, flag); |
||
| 164 | urtMutexUnlock(&core._lock); |
||
| 165 | if (priorityBoosted)
|
||
| 166 | core._nodes->thread->prio = oldPrio; |
||
| 167 | return URT_STATUS_OK;
|
||
| 168 | 64fde4ba | skenneweg | } |
| 169 | else
|
||
| 170 | {
|
||
| 171 | e566c39e | skenneweg | urtMutexUnlock(&core._lock); |
| 172 | return core._status;
|
||
| 173 | 64fde4ba | skenneweg | } |
| 174 | } |
||
| 175 | 7d9678db | skenneweg | |
| 176 | /**
|
||
| 177 | * @brief Get the topic of the Core.
|
||
| 178 | *
|
||
| 179 | 5198dfae | skenneweg | * @param[in] id Identifier of the topic to retrieve.
|
| 180 | 7d9678db | skenneweg | *
|
| 181 | 5198dfae | skenneweg | * @return Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
|
| 182 | 7d9678db | skenneweg | */
|
| 183 | 64fde4ba | skenneweg | #if (URT_CFG_PUBSUB_ENABLED)
|
| 184 | urt_topic_t* urtCoreGetTopic(urt_topicid_t id) {return urt_topic_t;}
|
||
| 185 | #endif /* URT_CFG_PUBSUB_ENABLED */ |
||
| 186 | |||
| 187 | 7d9678db | skenneweg | |
| 188 | /**
|
||
| 189 | * @brief Get the service of the Core.
|
||
| 190 | *
|
||
| 191 | 5198dfae | skenneweg | * @param[in] id Identifier of the service to retrieve.
|
| 192 | 7d9678db | skenneweg | *
|
| 193 | 5198dfae | skenneweg | * @return Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
|
| 194 | 64fde4ba | skenneweg | */
|
| 195 | #if (URT_CFG_RPC_ENABLED)
|
||
| 196 | urt_service_t urtCoreGetService(urt_serviceid_t id) {return urt_service_t;}
|
||
| 197 | #endif /* URT_CFG_RPC_ENABLED */ |
||
| 198 |