Statistics
| Branch: | Revision:

urtware / src / urt_core.c @ e566c39e

History | View | Annotate | Download (6.264 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
urt_core_t core;
37

    
38
/******************************************************************************/
39
/* LOCAL VARIABLES                                                            */
40
/******************************************************************************/
41

    
42
/******************************************************************************/
43
/* LOCAL FUNCTIONS                                                            */
44
/******************************************************************************/
45

    
46
/******************************************************************************/
47
/* EXPORTED FUNCTIONS                                                         */
48
/******************************************************************************/
49

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

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

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

    
94
/**
95
 * @brief   Synchronize all nodes of the core.
96
 *
97
 * @param[in] node  Pointer to a node to synchronize. Must not be NULL.
98
 *
99
 * @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
 */
104
urt_status_t urtCoreSynchronizeNodes(urt_node_t* node)
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 = 0; /*TODO: set to 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 = 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
  }
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