Statistics
| Branch: | Revision:

urtware / src / urt_node.c @ 1bfc6b25

History | View | Annotate | Download (6.38 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
urt_node_t localNode; //TODO
41

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

    
46
/**
47
 * @brief  Main function of a node.
48
 *
49
 * @param[in] arg  Optional Argument to the thread main function.
50
 */
51
void _main(void* arg)
52
{
53
  urt_osEventMask_t mask;
54
  urt_osEventFlags_t flag;   //TODO: flag from core here
55
  urtEventRegister(core._evtSource, &localNode.listener, mask, flag);
56
  if (localNode.setupcallback != NULL)
57
  {
58
    mask = localNode.setupcallback(&localNode, arg);
59
    if (mask /*TODO: check single bits from mask to find out if bit for core is 1 */)
60
    {
61
        urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK);
62
    }
63
  }
64
  else
65
  {
66
     mask = 0xFFFFFFFF;
67
  }
68

    
69
  if (core._status == URT_STATUS_OK)
70
  {
71
    urtCoreSynchronizeNodes(&localNode);
72
  }
73

    
74
  bool terminated = false;
75
  if (terminated /*TODO: replace with function*/)
76
  {
77
    urt_osEventMask_t temp = urtEventWait(mask, URT_EVENT_WAIT_ONE, URT_DELAY_INFINITE);
78
    if (temp /*TODO: check single bits from mask to find out if bit for core is 1 */)
79
    {
80
      localNode.loopcallback(&localNode, mask, arg);
81
      #if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
82
        localNode.loops++;
83
      #endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */
84
      if (mask /*TODO: check single bits from mask to find out if bit for core is 1 */)
85
      {
86
        urtCoreStopNodes(URT_STATUS_NODE_INVALEVTMASK);
87
      }
88
    }
89
  }
90

    
91
  if (localNode.shutdowncallback)
92
  {
93
    localNode.shutdowncallback(&localNode, urtCoreGetStatus(), arg);
94
  }
95
  urtEventUnregister(core._evtSource, &localNode.listener);
96
  urt_osThread_t* threadToTerminate = localNode.thread;
97
  urt_osThread_t* threadToTerminateChild;
98
  while (threadToTerminate != NULL)
99
  {
100
    threadToTerminateChild = threadToTerminate->children;
101
    urtThreadTerminate(&threadToTerminate, URT_THREAD_TERMINATE_REQUEST);
102
    threadToTerminate = threadToTerminateChild;
103
  }
104
  urtThreadExit();
105
  return;
106
}
107

    
108
/******************************************************************************/
109
/* EXPORTED FUNCTIONS                                                         */
110
/******************************************************************************/
111

    
112
/**
113
 * @brief  Initalize a node.
114
 *
115
 * @param[in] node  The node to initialize. Must not be NULL.
116
 * @param[in] thread  The already initialized, exclusive thread for the node. Must not be NULL.
117
 * @param[in] setupcallback  Callback function to be executed during setup.
118
 *                           May be NULL if no custom setup is required.
119
 * @param[in] setupparams  Parameters for the setup callback function.
120
 *                         Must be NULL if no setup callback is specified.
121
 *                         May be NULL if the specified setup callback does not expect parameters.
122
 * @param[in] loopcallback  Callback function to be executed in a loop.
123
 * @param[in] loopparams  Parameters for the loop callback function.
124
 *                        May be NULL if the specified loop callback does not expect parameters.
125
 * @param[in] shutdowncallback  Callback function to be executed during shutdown.
126
 *                              May be NULL if no custom shutdown is required.
127
 * @param[in] shutdownparams  Parameters for the loop callback function.
128
 *                            Must be NULL if no shutdown callback is specified.
129
 *                            May be NULL if the specified shutdown callback does not expect parameters.
130
 */
131
void urtNodeInit(urt_node_t* node, urt_osThread_t* thread, urt_nodeSetupCallback_t* setupcallback,
132
                 void* setupparams, urt_nodeLoopCallback_t* loopcallback, void* loopparams,
133
                 urt_nodeShutdownCallback_t* shutdowncallback, void* shutdownparams)
134
{
135
  urtDebugAssert(node != NULL);
136
  urtDebugAssert(thread != NULL);
137
  if (setupcallback == NULL)
138
    urtDebugAssert(setupparams == NULL);
139

    
140
  node->next = NULL;
141
  node->thread = thread;
142
  node->setupcallback = setupcallback;
143
  node->setupparams = setupparams;
144
  node->loopcallback = loopcallback;
145
  node->loopparams = loopparams;
146
  node->shutdowncallback = shutdowncallback;
147
  node->shutdownparams = shutdownparams;
148
  node->stage = 0;
149
  urtEventListenerInit(node->listener);
150
  #if (URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING)
151
    node->loops = 0;
152
  #endif /* URT_CFG_PUBSUB_PROFILING || URT_CFG_RPC_PROFILING */
153
  urtMutexLock(&core._lock);
154
    node->next = core._nodes;
155
    core._nodes = node;
156
  urtMutexUnlock(&core._lock);
157
  localNode = *node;
158
  return;
159
}