Statistics
| Branch: | Revision:

urtware / src / urt_core.c @ 64fde4ba

History | View | Annotate | Download (5.928 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
  //TODO: Lock core
84
  urt_node_t* node = core._nodes;
85
  while (node)
86
  {
87
    urtThreadStart(node->thread);
88
    node = node->next;
89
  }
90
  //TODO: Unlock core
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 urtCoreSynchronize(urt_node_t* node)
105
{
106
  //TODO: Lock core
107
  //TODO: Increment node's stage value
108
  urt_node_t* nodeFromCore = core._nodes;
109
  while (nodeFromCore /*stage equals stage*/)
110
  {
111
    nodeFromCore = nodeFromCore->next;
112
  }
113
  bool temp = false;
114
  if (temp/*all nodes are the same stage*/)
115
  {
116
    //TODO: Unlock core
117
    return URT_STATUS_OK;
118
  }
119
  else if (temp/*stage of the last checked node was one less than the argument*/)
120
  {
121
    //TODO: Unlock core
122
    return URT_STATUS_SYNC_PENDING;
123
  }
124
  else
125
  {
126
      urtCoreStopNodes(URT_STATUS_SYNC_ERROR);
127
      return URT_STATUS_SYNC_ERROR;
128
  }
129
}
130

    
131
/**
132
 * @brief   Stop threads of all nodes of the Core.
133
 *
134
 * @param[in] reason  The reason why the function was called. For normal shutdown URT_STATUS_OK should be used.
135
 *
136
 * @return  Returns URT_STATUS_OK if there was no call with another reason than URT_STATUS_OK before.
137
 *          If the function has been called before with a different reason, that reason is returned.
138
 */
139
urt_status_t urtCoreStopNodes(urt_status_t reason)
140
{
141
  //TODO: Lock core
142
  if (core._status == URT_STATUS_OK)
143
  {
144
    if (core._nodes->thread->prio < URT_THREAD_PRIO_HIGH_MAX)
145
    {
146
      core._nodes->thread->prio = URT_THREAD_PRIO_HIGH_MAX;
147
    }
148
    else
149
    {
150
      core._status = reason;
151
      urt_node_t* node = core._nodes;
152
      while (node)
153
      {
154
        urt_node_t* tempNode = node;
155
        tempNode = NULL;
156
        node = node->next;
157
      }
158
      //TODO: broadcast control event (terminate)
159
      //TODO:Unlock core
160
      //TODO: Thread boosted its priority?
161
    }
162
  }
163
  else
164
  {
165
      //TODO:Unlock core
166
      return core._status;
167
  }
168
  return URT_STATUS_OK;
169
}
170

    
171
/**
172
 * @brief   Get the topic of the Core.
173
 *
174
 * @param[in] id  Identifier of the topic to retrieve.
175
 *
176
 * @return  Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
177
 */
178
#if (URT_CFG_PUBSUB_ENABLED)
179
  urt_topic_t* urtCoreGetTopic(urt_topicid_t id) {return urt_topic_t;}
180
#endif /* URT_CFG_PUBSUB_ENABLED */
181

    
182

    
183
/**
184
 * @brief   Get the service of the Core.
185
 *
186
 * @param[in] id  Identifier of the service to retrieve.
187
 *
188
 * @return Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
189
 */  
190
#if (URT_CFG_RPC_ENABLED)
191
  urt_service_t urtCoreGetService(urt_serviceid_t id) {return urt_service_t;}
192
#endif /* URT_CFG_RPC_ENABLED */
193

    
194