| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License as published by |
| 4 |
* the Free Software Foundation; either version 2 of the License, or |
| 5 |
* (at your option) any later version. |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU Library General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 |
* |
| 16 |
* PortManager.cpp |
| 17 |
* Enables the Patching of Ports |
| 18 |
* Copyright (C) 2005 Simon Newton |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "olad/plugin_api/PortManager.h" |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
#include "ola/Logging.h" |
| 25 |
#include "ola/StringUtils.h" |
| 26 |
#include "olad/Port.h" |
| 27 |
|
| 28 |
namespace ola { |
| 29 |
|
| 30 |
using std::vector; |
| 31 |
|
| 32 |
bool PortManager::PatchPort(InputPort *port, |
| 33 |
unsigned int universe) { |
| 34 |
return GenericPatchPort(port, universe); |
| 35 |
} |
| 36 |
|
| 37 |
bool PortManager::PatchPort(OutputPort *port, |
| 38 |
unsigned int universe) { |
| 39 |
return GenericPatchPort(port, universe); |
| 40 |
} |
| 41 |
|
| 42 |
bool PortManager::UnPatchPort(InputPort *port) { |
| 43 |
return GenericUnPatchPort(port); |
| 44 |
} |
| 45 |
|
| 46 |
bool PortManager::UnPatchPort(OutputPort *port) { |
| 47 |
return GenericUnPatchPort(port); |
| 48 |
} |
| 49 |
|
| 50 |
bool PortManager::SetPriorityInherit(Port *port) { |
| 51 |
if (port->PriorityCapability() != CAPABILITY_FULL) |
| 52 |
return true; |
| 53 |
|
| 54 |
if (port->GetPriorityMode() != PRIORITY_MODE_INHERIT) { |
| 55 |
port->SetPriorityMode(PRIORITY_MODE_INHERIT); |
| 56 |
} |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
bool PortManager::SetPriorityStatic(Port *port, uint8_t value) { |
| 62 |
if (port->PriorityCapability() == CAPABILITY_NONE) |
| 63 |
return true; |
| 64 |
|
| 65 |
if (port->PriorityCapability() == CAPABILITY_FULL && |
| 66 |
port->GetPriorityMode() != PRIORITY_MODE_STATIC) |
| 67 |
port->SetPriorityMode(PRIORITY_MODE_STATIC); |
| 68 |
|
| 69 |
if (value > ola::dmx::SOURCE_PRIORITY_MAX) { |
| 70 |
OLA_WARN << "Priority " << static_cast<int>(value) |
| 71 |
<< " is greater than the max priority (" |
| 72 |
<< static_cast<int>(ola::dmx::SOURCE_PRIORITY_MAX) << ")"; |
| 73 |
value = ola::dmx::SOURCE_PRIORITY_MAX; |
| 74 |
} |
| 75 |
|
| 76 |
if (port->GetPriority() != value) |
| 77 |
port->SetPriority(value); |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
template<class PortClass> |
| 83 |
bool PortManager::GenericPatchPort(PortClass *port, |
| 84 |
unsigned int new_universe_id) { |
| 85 |
if (!port) |
| 86 |
return false; |
| 87 |
|
| 88 |
Universe *universe = port->GetUniverse(); |
| 89 |
if (universe && universe->UniverseId() == new_universe_id) |
| 90 |
return true; |
| 91 |
|
| 92 |
AbstractDevice *device = port->GetDevice(); |
| 93 |
if (device) { |
| 94 |
if (!device->AllowLooping()) { |
| 95 |
// check ports of the opposite type |
| 96 |
if (CheckLooping<PortClass>(device, new_universe_id)) |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if (!device->AllowMultiPortPatching()) { |
| 101 |
if (CheckMultiPort<PortClass>(device, new_universe_id)) |
| 102 |
return false; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// unpatch if required |
| 107 |
if (universe) { |
| 108 |
OLA_DEBUG << "Port " << port->UniqueId() << " is bound to universe " << |
| 109 |
universe->UniverseId(); |
| 110 |
m_broker->RemovePort(port); |
| 111 |
universe->RemovePort(port); |
| 112 |
} |
| 113 |
|
| 114 |
universe = m_universe_store->GetUniverseOrCreate(new_universe_id); |
| 115 |
if (!universe) |
| 116 |
return false; |
| 117 |
|
| 118 |
if (port->SetUniverse(universe)) { |
| 119 |
OLA_INFO << "Patched " << port->UniqueId() << " to universe " << |
| 120 |
universe->UniverseId(); |
| 121 |
m_broker->AddPort(port); |
| 122 |
universe->AddPort(port); |
| 123 |
} else { |
| 124 |
if (!universe->IsActive()) |
| 125 |
m_universe_store->AddUniverseGarbageCollection(universe); |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
template<class PortClass> |
| 132 |
bool PortManager::GenericUnPatchPort(PortClass *port) { |
| 133 |
if (!port) |
| 134 |
return false; |
| 135 |
|
| 136 |
Universe *universe = port->GetUniverse(); |
| 137 |
m_broker->RemovePort(port); |
| 138 |
if (universe) { |
| 139 |
universe->RemovePort(port); |
| 140 |
port->SetUniverse(NULL); |
| 141 |
OLA_INFO << "Unpatched " << port->UniqueId() << " from uni " |
| 142 |
<< universe->UniverseId(); |
| 143 |
} |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
template <class PortClass> |
| 149 |
bool PortManager::CheckLooping(const AbstractDevice *device, |
| 150 |
unsigned int new_universe_id) const { |
| 151 |
return CheckOutputPortsForUniverse(device, new_universe_id); |
| 152 |
} |
| 153 |
|
| 154 |
template <> |
| 155 |
bool PortManager::CheckLooping<OutputPort>( |
| 156 |
const AbstractDevice *device, |
| 157 |
unsigned int new_universe_id) const { |
| 158 |
return CheckInputPortsForUniverse(device, new_universe_id); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
template <class PortClass> |
| 163 |
bool PortManager::CheckMultiPort(const AbstractDevice *device, |
| 164 |
unsigned int new_universe_id) const { |
| 165 |
return CheckInputPortsForUniverse(device, new_universe_id); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
template <> |
| 170 |
bool PortManager::CheckMultiPort<OutputPort>( |
| 171 |
const AbstractDevice *device, |
| 172 |
unsigned int new_universe_id) const { |
| 173 |
return CheckOutputPortsForUniverse(device, new_universe_id); |
| 174 |
} |
| 175 |
|
| 176 |
bool PortManager::CheckInputPortsForUniverse(const AbstractDevice *device, |
| 177 |
unsigned int universe_id) const { |
| 178 |
vector<InputPort*> ports; |
| 179 |
device->InputPorts(&ports); |
| 180 |
return CheckForPortMatchingUniverse(ports, universe_id); |
| 181 |
} |
| 182 |
|
| 183 |
bool PortManager::CheckOutputPortsForUniverse(const AbstractDevice *device, |
| 184 |
unsigned int universe_id) const { |
| 185 |
vector<OutputPort*> ports; |
| 186 |
device->OutputPorts(&ports); |
| 187 |
return CheckForPortMatchingUniverse(ports, universe_id); |
| 188 |
} |
| 189 |
|
| 190 |
template<class PortClass> |
| 191 |
bool PortManager::CheckForPortMatchingUniverse( |
| 192 |
const vector<PortClass*> &ports, |
| 193 |
unsigned int universe_id) const { |
| 194 |
typename vector<PortClass*>::const_iterator iter; |
| 195 |
for (iter = ports.begin(); iter != ports.end(); ++iter) { |
| 196 |
if ((*iter)->GetUniverse() && |
| 197 |
(*iter)->GetUniverse()->UniverseId() == universe_id) { |
| 198 |
OLA_INFO << "Port " << (*iter)->PortId() << " is already patched to " |
| 199 |
<< universe_id; |
| 200 |
return true; |
| 201 |
} |
| 202 |
} |
| 203 |
return false; |
| 204 |
} |
| 205 |
} // namespace ola |