Statistics
| Branch: | Revision:

urtware / src / urt_core.c @ 1bfc6b25

History | View | Annotate | Download (6.265 KB)

1
/*
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
#include <urtware.h>
23

    
24
/******************************************************************************/
25
/* LOCAL DEFINITIONS                                                          */
26
/******************************************************************************/
27

    
28
/******************************************************************************/
29
/* EXPORTED VARIABLES                                                         */
30
/******************************************************************************/
31

    
32
/******************************************************************************/
33
/* LOCAL TYPES                                                                */
34
/******************************************************************************/
35

    
36
/******************************************************************************/
37
/* LOCAL VARIABLES                                                            */
38
/******************************************************************************/
39

    
40
/******************************************************************************/
41
/* LOCAL FUNCTIONS                                                            */
42
/******************************************************************************/
43

    
44
/******************************************************************************/
45
/* EXPORTED FUNCTIONS                                                         */
46
/******************************************************************************/
47

    
48
/**
49
 * @brief   Initialize the Core.
50
 */
51
void urtCoreInit(void)
52
{
53
  core._nodes = NULL;
54
  core._status = URT_STATUS_OK;
55
  urtEventSourceInit(core._evtSource);
56
  urtMutexInit(&core._lock);
57
  #if (URT_CFG_PUBSUB_ENABLED)
58
    core._topics = NULL;
59
  #endif /* URT_CFG_PUBSUB_ENABLED */
60
  #if (URT_CFG_RPC_ENABLED)
61
    core.urt_service_t = NULL;
62
  #endif /* URT_CFG_RPC_ENABLED */
63
  return;
64
}
65

    
66
/**
67
 * @brief   Get Core status.
68
 *
69
 * @return  Current system status.
70
 */
71
urt_status_t urtCoreGetStatus(void)
72
{
73
  return core._status;
74
}
75

    
76
/**
77
 * @brief   Start threads of all nodes of the Core.
78
 */
79
void urtCoreStartNodes(void)
80
{
81
  urtMutexLock(&core._lock);
82
  urt_node_t* node = core._nodes;
83
  while (node)
84
  {
85
    urtThreadStart(node->thread);
86
    node = node->next;
87
  }
88
  urtMutexUnlock(&core._lock);
89
  return;
90
}
91

    
92
/**
93
 * @brief   Synchronize all nodes of the core.
94
 *
95
 * @param[in] node  Pointer to a node to synchronize. Must not be NULL.
96
 *
97
 * @return  Returns URT_STATUS_OK if all nodes are synchronized and proceed.
98
 *          Returns URT_STATUS_SYNC_ERROR if an exception occurred (faulty stage value detected).
99
 *          Returns URT_STATUS_SYNC_PENDING if there are nodes left to synchronize.
100
 *          In the latter case, the node thread must still wait for the control event (proceed) to synchronize.
101
 */
102
urt_status_t urtCoreSynchronizeNodes(urt_node_t* node)
103
{
104
  urtDebugAssert(node != NULL);
105

    
106
  urtMutexLock(&core._lock);
107
  node->stage -= 1;
108
  urt_node_t* nodeFromCore = core._nodes;
109
  while (nodeFromCore && nodeFromCore->stage == node->stage)
110
  {
111
    nodeFromCore = nodeFromCore->next;
112
  }
113
  if (nodeFromCore)
114
  {
115
    urt_osEventFlags_t flag = URT_EVENTFLAG_PROCEED;
116
    urtEventSourceBroadcast(core._evtSource, flag);
117
    urtMutexUnlock(&core._lock);
118
    return URT_STATUS_OK;
119
  }
120
  else if (nodeFromCore->stage == (node->stage - 1))
121
  {
122
    urtMutexUnlock(&core._lock);
123
    return URT_STATUS_SYNC_PENDING;
124
  }
125
  else
126
  {
127
      urtCoreStopNodes(URT_STATUS_SYNC_ERROR);
128
      urtMutexUnlock(&core._lock);
129
      return URT_STATUS_SYNC_ERROR;
130
  }
131
}
132

    
133
/**
134
 * @brief   Stop threads of all nodes of the Core.
135
 *
136
 * @param[in] reason  The reason why the function was called. For normal shutdown URT_STATUS_OK should be used.
137
 *
138
 * @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
 */
141
urt_status_t urtCoreStopNodes(urt_status_t reason)
142
{
143
  urtMutexLock(&core._lock);
144
  bool priorityBoosted = false;
145
  urt_osThreadPrio_t oldPrio;
146

    
147
  if (core._status == URT_STATUS_OK)
148
  {
149
    if (core._nodes->thread->prio < URT_THREAD_PRIO_HIGH_MAX)
150
    {
151
      oldPrio = core._nodes->thread->prio;
152
      priorityBoosted = true;
153
      core._nodes->thread->prio = URT_THREAD_PRIO_HIGH_MAX;
154
    }
155
    core._status = reason;
156
    urt_node_t* node = core._nodes;
157
    while (node)
158
    {
159
      urtThreadTerminate(node->thread, URT_THREAD_TERMINATE_REQUEST);
160
      node = node->next;
161
    }
162
    urt_osEventFlags_t flag = URT_EVENTFLAG_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
  }
169
  else
170
  {
171
    urtMutexUnlock(&core._lock);
172
    return core._status;
173
  }
174
}
175

    
176
/**
177
 * @brief   Get the topic of the Core.
178
 *
179
 * @param[in] id  Identifier of the topic to retrieve.
180
 *
181
 * @return  Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
182
 */
183
#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

    
188
/**
189
 * @brief   Get the service of the Core.
190
 *
191
 * @param[in] id  Identifier of the service to retrieve.
192
 *
193
 * @return Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
194
 */  
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

    
199