Statistics
| Branch: | Revision:

urtware / src / urt_core.c @ 5c6cb22f

History | View | Annotate | Download (7.544 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
 * @brief  core
38
 */
39
typedef struct urt_core
40
{
41
  urt_node_t* _nodes;
42
  urt_osEventSource_t _evtSource;
43
  urt_osMutex_t _lock;
44
  urt_status_t _status;
45
  #if (URT_CFG_PUBSUB_ENABLED)
46
    urt_topic_t* _topics;
47
  #endif /* URT_CFG_PUBSUB_ENABLED */
48
  #if (URT_CFG_RPC_ENABLED)
49
    urt_service_t* _services;
50
  #endif /* URT_CFG_RPC_ENABLED */
51
}urt_core_t;
52

    
53
static urt_core_t core;
54

    
55
/******************************************************************************/
56
/* LOCAL VARIABLES                                                            */
57
/******************************************************************************/
58

    
59
/******************************************************************************/
60
/* LOCAL FUNCTIONS                                                            */
61
/******************************************************************************/
62

    
63
/******************************************************************************/
64
/* EXPORTED FUNCTIONS                                                         */
65
/******************************************************************************/
66

    
67
/**
68
 * @brief   Initialize the Core.
69
 */
70
void urtCoreInit(void)
71
{
72
  core._nodes = NULL;
73
  core._status = URT_STATUS_OK;
74
  urtEventSourceInit(&core._evtSource);
75
  urtMutexInit(&core._lock);
76
  #if (URT_CFG_PUBSUB_ENABLED)
77
    core._topics = NULL;
78
  #endif /* URT_CFG_PUBSUB_ENABLED */
79
  #if (URT_CFG_RPC_ENABLED)
80
    core.urt_service_t = NULL;
81
  #endif /* URT_CFG_RPC_ENABLED */
82
  return;
83
}
84

    
85
/**
86
 * @brief   Get Core status.
87
 *
88
 * @return  Current system status.
89
 */
90
urt_status_t urtCoreGetStatus(void)
91
{
92
  return core._status;
93
}
94

    
95
urt_osEventMask_t urtCoreGetEventMask(void)
96
{
97
    return URT_EVENTMASK_MAXPRIO;
98
}
99

    
100
/**
101
 * @brief   Get Core mutex.
102
 *
103
 * @return  Current system mutex.
104
 */
105
urt_osMutex_t* urtCoreGetMutex(void)
106
{
107
    return &core._lock;
108
}
109

    
110
/**
111
 * @brief   Get Core event source.
112
 *
113
 * @return  Current system event source.
114
 */
115
urt_osEventSource_t* urtCoreGetEvtSource(void)
116
{
117
    return &core._evtSource;
118
}
119

    
120

    
121
/**
122
 * @brief   Get Core nodes.
123
 *
124
 * @return  Nodes registered to the core.
125
 */
126
urt_node_t* urtCoreGetNodes(void)
127
{
128
    return core._nodes;
129
}
130

    
131
void urtCoreSetNodes(urt_node_t* node)
132
{
133
    core._nodes = node;
134
    return;
135
}
136

    
137
/**
138
 * @brief   Start threads of all nodes of the Core.
139
 */
140
void urtCoreStartNodes(void)
141
{
142
  urtMutexLock(&core._lock);
143
  urt_node_t* node = core._nodes;
144
  while (node)
145
  {
146
    urtThreadStart(node->thread);
147
    node = node->next;
148
    urtThreadMSleep(100); //TODO: delete
149
  }
150
  urtMutexUnlock(&core._lock);
151
  return;
152
}
153

    
154
/**
155
 * @brief   Synchronize all nodes of the core.
156
 *
157
 * @param[in] node  Pointer to a node to synchronize. Must not be NULL.
158
 *
159
 * @return  Returns URT_STATUS_OK if all nodes are synchronized and proceed.
160
 *          Returns URT_STATUS_SYNC_ERROR if an exception occurred (faulty stage value detected).
161
 *          Returns URT_STATUS_SYNC_PENDING if there are nodes left to synchronize.
162
 *          In the latter case, the node thread must still wait for the control event (proceed) to synchronize.
163
 */
164
urt_status_t urtCoreSynchronizeNodes(urt_node_t* node)
165
{
166
  urtDebugAssert(node != NULL);
167

    
168
  urtMutexLock(&core._lock);
169
  node->stage++;
170
  urt_node_t* nodeFromCore = core._nodes;
171
  while (nodeFromCore && nodeFromCore->stage == node->stage)
172
  {
173
    nodeFromCore = nodeFromCore->next;
174
  }
175

    
176
  if (!nodeFromCore)
177
  {
178
    urt_osEventFlags_t flag = URT_EVENTFLAG_PROCEED;
179
    urtEventSourceBroadcast(&core._evtSource, flag);
180
    urtMutexUnlock(&core._lock);
181
    return URT_STATUS_OK;
182
  }
183
  else if (nodeFromCore->stage == (node->stage - 1))
184
  {
185
    urtMutexUnlock(&core._lock);
186
    return URT_STATUS_SYNC_PENDING;
187
  }
188
  else
189
  {
190
      urtCoreStopNodes(URT_STATUS_SYNC_ERROR);
191
      urtMutexUnlock(&core._lock);
192
      return URT_STATUS_SYNC_ERROR;
193
  }
194
}
195

    
196
/**
197
 * @brief   Stop threads of all nodes of the Core.
198
 *
199
 * @param[in] reason  The reason why the function was called. For normal shutdown URT_STATUS_OK should be used.
200
 *
201
 * @return  Returns URT_STATUS_OK if there was no call with another reason than URT_STATUS_OK before.
202
 *          If the function has been called before with a different reason, that reason is returned.
203
 */
204
urt_status_t urtCoreStopNodes(urt_status_t reason)
205
{
206
  urtMutexLock(&core._lock);
207
  bool priorityBoosted = false;
208
  urt_osThreadPrio_t oldPrio;
209

    
210
  if (core._status == URT_STATUS_OK)
211
  {
212
    if (core._nodes->thread->prio < URT_THREAD_PRIO_HIGH_MAX)
213
    {
214
      oldPrio = core._nodes->thread->prio;
215
      priorityBoosted = true;
216
      core._nodes->thread->prio = URT_THREAD_PRIO_HIGH_MAX;
217
    }
218
    core._status = reason;
219
    urt_node_t* node = core._nodes;
220
    while (node)
221
    {
222
      urtThreadTerminate(node->thread, URT_THREAD_TERMINATE_REQUEST);
223
      node = node->next;
224
    }
225
    urt_osEventFlags_t flag = URT_EVENTFLAG_TERMINATE;
226
    urtEventSourceBroadcast(&core._evtSource, flag);
227
    urtMutexUnlock(&core._lock);
228
    if (priorityBoosted)
229
      core._nodes->thread->prio = oldPrio;
230
    return URT_STATUS_OK;
231
  }
232
  else
233
  {
234
    urtMutexUnlock(&core._lock);
235
    return core._status;
236
  }
237
}
238

    
239
/**
240
 * @brief   Get the topic of the Core.
241
 *
242
 * @param[in] id  Identifier of the topic to retrieve.
243
 *
244
 * @return  Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
245
 */
246
#if (URT_CFG_PUBSUB_ENABLED)
247
  urt_topic_t* urtCoreGetTopic(urt_topicid_t id)
248
  {
249
      urtMutexLock(&core._lock);
250
      urt_topic_t* topic = core._topics;
251
      while (topic != NULL && topic->id < id)
252
          topic = topic->next;
253
      urtMutexUnlock(&core._lock);
254
      if (topic != NULL && topic->id == id)
255
          return topic;
256
      else
257
        return NULL;
258
  }
259
#endif /* URT_CFG_PUBSUB_ENABLED */
260

    
261

    
262
/**
263
 * @brief   Get the service of the Core.
264
 *
265
 * @param[in] id  Identifier of the service to retrieve.
266
 *
267
 * @return Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
268
 */  
269
#if (URT_CFG_RPC_ENABLED)
270
  urt_service_t urtCoreGetService(urt_serviceid_t id) {return urt_service_t;}
271
#endif /* URT_CFG_RPC_ENABLED */
272

    
273