Statistics
| Branch: | Revision:

urtware / src / urt_core.c @ 64fde4ba

History | View | Annotate | Download (5.928 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
  //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 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 64fde4ba skenneweg
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 7d9678db skenneweg
131
/**
132 5198dfae skenneweg
 * @brief   Stop threads of all nodes of the Core.
133 7d9678db skenneweg
 *
134 5198dfae skenneweg
 * @param[in] reason  The reason why the function was called. For normal shutdown URT_STATUS_OK should be used.
135 7d9678db skenneweg
 *
136 5198dfae skenneweg
 * @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 7d9678db skenneweg
 */
139 64fde4ba skenneweg
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 7d9678db skenneweg
171
/**
172
 * @brief   Get the topic of the Core.
173
 *
174 5198dfae skenneweg
 * @param[in] id  Identifier of the topic to retrieve.
175 7d9678db skenneweg
 *
176 5198dfae skenneweg
 * @return  Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
177 7d9678db skenneweg
 */
178 64fde4ba skenneweg
#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 7d9678db skenneweg
183
/**
184
 * @brief   Get the service of the Core.
185
 *
186 5198dfae skenneweg
 * @param[in] id  Identifier of the service to retrieve.
187 7d9678db skenneweg
 *
188 5198dfae skenneweg
 * @return Returns a pointer to the requested service. Returns NULL if no service matches the given ID.
189 64fde4ba skenneweg
 */  
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