blob: e874918b18cf547218f902f9660155cae2d6729a [file] [log] [blame]
Ernesto Posse8a4f2962015-05-12 13:28:46 -04001// umlrtsignal.cc
2
3/*******************************************************************************
4* Copyright (c) 2014-2015 Zeligsoft (2009) Limited and others.
5* All rights reserved. This program and the accompanying materials
6* are made available under the terms of the Eclipse Public License v1.0
7* which accompanies this distribution, and is available at
8* http://www.eclipse.org/legal/epl-v10.html
9*******************************************************************************/
10
11#include <stdlib.h>
12#include "umlrtapi.hh"
13#include "umlrtcommsport.hh"
14#include "umlrtcommsportfarend.hh"
15#include "umlrtcontroller.hh"
16#include "umlrtframeservice.hh"
17#include "umlrtsignal.hh"
18#include "umlrtsignalelement.hh"
19#include "umlrtuserconfig.hh"
20#include "basefatal.hh"
21#include "basedebugtype.hh"
22#include "basedebug.hh"
23
24// See umlrtsignal.hh for documentation.
25
26// No signal element by default - equivalent to the 'invalid signal'.
27// An element is allocated when signal is 'initialized'.
28UMLRTSignal::UMLRTSignal() : element(NULL)
29{
30}
31
32UMLRTSignal::~UMLRTSignal()
33{
34 if (element)
35 {
36 element->decrementRefCount();
37 }
38}
39
40UMLRTSignal::UMLRTSignal( const UMLRTSignal &signal ) : element(signal.element)
41{
42 if (element)
43 {
44 element->incrementRefCount();
45 }
46}
47
48UMLRTSignal &UMLRTSignal::operator=( const UMLRTSignal &signal )
49{
50 if (&signal != this)
51 {
52 if (element)
53 {
54 element->decrementRefCount();
55 }
56 if (signal.element)
57 {
58 signal.element->incrementRefCount();
59 }
60 element = signal.element;
61 }
62 return *this;
63}
64
65bool UMLRTSignal::initialize( Id id, const UMLRTCommsPort * srcPort, size_t payloadSize, UMLRTSignalElement::Priority priority )
66{
67 BDEBUG(BD_SIGNAL, "initialize signal-qid[%d] id %d for slot %s port %s payloadSize %d\n",
68 getQid(),
69 id,
70 (srcPort != NULL) ? srcPort->slot->name : "(slot unknown)",
71 (srcPort != NULL) ? srcPort->role()->name : "(no source port)",
72 payloadSize);
73
74 // Initialize for a wired port. If the srcPort is 'the unbound port', there is no need for a signal element with a payload
75 // as nothing will be sent. The current protocol code pattern requires the element with a payload, so we allocate one.
76 // When Bug 231 is resolved, we can avoid allocating an element and payload when the srcPort is 'the unbound port'.
77 if (element)
78 {
79 FATAL("initializing signal that already has an element signal-qid[%d] allocated.", element->qid);
80 }
81 element = umlrt::SignalElementGetFromPool();
82
83 if (!element)
84 {
85 FATAL("failed to allocate signal element");
86 }
87 element->initialize( id, srcPort, payloadSize, priority );
88
89 if (srcPort == NULL)
90 {
91 FATAL("initializing a signal that has no srcPort - should at least be the 'unbound port'");
92 }
93 return !srcPort->unbound;
94}
95
96// Basic initialization. Currently used for initialize message to capsules.
97void UMLRTSignal::initialize( size_t payloadSize, UMLRTSignalElement::Priority priority )
98{
99 if (element)
100 {
101 FATAL("initializing signal that already has an element allocated.");
102 }
103 element = umlrt::SignalElementGetFromPool();
104
105 if (!element)
106 {
107 FATAL("failed to allocate signal element");
108 }
109 BDEBUG(BD_SIGNAL, "allocate no id, srcPort or payloadSize\n");
110
111 element->initialize( payloadSize, priority );
112}
113
114// Basic initialization. Currently used for in-signals.
115void UMLRTSignal::initialize( Id id, const UMLRTCommsPort * srcPort )
116{
117 if (element)
118 {
119 FATAL("initializing signal that already has an element allocated.");
120 }
121 element = umlrt::SignalElementGetFromPool();
122
123 if (!element)
124 {
125 FATAL("failed to allocate signal element");
126 }
127 BDEBUG(BD_SIGNAL, "allocate no id, srcPort or payloadSize\n");
128
129 element->initialize(id, srcPort);
130}
131
132
133// Get the payload buffer.
134uint8_t *UMLRTSignal::getPayload() const
135{
136 if (!element)
137 {
138 return NULL;
139 }
140 return element->getPayload();
141}
142
143// Get the payload buffer.
144size_t UMLRTSignal::getPayloadSize() const
145{
146 if (!element)
147 {
148 return 0;
149 }
150 return element->getPayloadSize();
151}
152
153const UMLRTCommsPort * UMLRTSignal::getSrcPort() const
154{
155 if (!element)
156 {
157 return NULL;
158 }
159 return element->getSrcPort();
160}
161
162UMLRTSignal::Id UMLRTSignal::getId() const
163{
164 if (!element)
165 {
166 return -1; // Another example of using '-1' when we don't have a signal id.
167 }
168 return element->getId();
169}