Rev 2082 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 2082 | Rev 2164 | ||
|---|---|---|---|
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Manager.h" |
21 | #include "Manager.h" |
| 22 | 22 | ||
| 23 | #include <boost/lexical_cast.hpp> |
23 | #include <boost/lexical_cast.hpp> |
| 24 | 24 | ||
| 25 | #include "broker/Manager.h" |
25 | #include "broker/Manager.h" |
| 26 | #include "broker/Message.h" |
26 | #include "broker/Message.h" |
| 27 | #include "common/common.h" |
27 | #include "common/common.h" |
| 28 | #include "timer/Manager.h" |
28 | #include "timer/Manager.h" |
| 29 | #include "storage/Manager.h" |
29 | #include "storage/Manager.h" |
| 30 | #include "common/log.h" |
30 | #include "common/log.h" |
| 31 | 31 | ||
| 32 | #include "can/Message.h" |
32 | #include "can/Message.h" |
| 33 | 33 | ||
| 34 | namespace atom { |
34 | namespace atom { |
| 35 | namespace control { |
35 | namespace control { |
| 36 | 36 | ||
| 37 | Manager::Pointer Manager::instance_; |
37 | Manager::Pointer Manager::instance_; |
| 38 | 38 | ||
| 39 | static const std::string log_module_ = "control::manager"; |
39 | static const std::string log_module_ = "control::manager"; |
| 40 | 40 | ||
| 41 | Manager::Manager() : broker::Subscriber(false) |
41 | Manager::Manager() : broker::Subscriber(false) |
| 42 | { |
42 | { |
| 43 | LOG_DEBUG_ENTER; |
43 | LOG_DEBUG_ENTER; |
| 44 | 44 | ||
| 45 | Node::SetupStateMachine(); |
45 | Node::SetupStateMachine(); |
| 46 | 46 | ||
| 47 | this->active_programming_node_id_ = ""; |
47 | this->active_programming_node_id_ = ""; |
| 48 | 48 | ||
| 49 | // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds |
49 | // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds |
| 50 | this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true); |
50 | this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true); |
| 51 | 51 | ||
| 52 | LOG_DEBUG_EXIT; |
52 | LOG_DEBUG_EXIT; |
| 53 | } |
53 | } |
| 54 | 54 | ||
| 55 | Manager::~Manager() |
55 | Manager::~Manager() |
| 56 | { |
56 | { |
| 57 | LOG_DEBUG_ENTER; |
57 | LOG_DEBUG_ENTER; |
| 58 | LOG_DEBUG_EXIT; |
58 | LOG_DEBUG_EXIT; |
| 59 | } |
59 | } |
| 60 | 60 | ||
| 61 | Manager::Pointer Manager::Instance() |
61 | Manager::Pointer Manager::Instance() |
| 62 | { |
62 | { |
| 63 | LOG_DEBUG_ENTER; |
63 | LOG_DEBUG_ENTER; |
| 64 | LOG_DEBUG_EXIT; |
64 | LOG_DEBUG_EXIT; |
| 65 | 65 | ||
| 66 | return Manager::instance_; |
66 | return Manager::instance_; |
| 67 | } |
67 | } |
| 68 | 68 | ||
| 69 | void Manager::Create() |
69 | void Manager::Create() |
| 70 | { |
70 | { |
| 71 | LOG_DEBUG_ENTER; |
71 | LOG_DEBUG_ENTER; |
| 72 | 72 | ||
| 73 | Manager::instance_ = Manager::Pointer(new Manager()); |
73 | Manager::instance_ = Manager::Pointer(new Manager()); |
| 74 | 74 | ||
| 75 | LOG_DEBUG_EXIT; |
75 | LOG_DEBUG_EXIT; |
| 76 | } |
76 | } |
| 77 | 77 | ||
| 78 | void Manager::Delete() |
78 | void Manager::Delete() |
| 79 | { |
79 | { |
| 80 | LOG_DEBUG_ENTER; |
80 | LOG_DEBUG_ENTER; |
| 81 | 81 | ||
| 82 | Manager::instance_.reset(); |
82 | Manager::instance_.reset(); |
| 83 | 83 | ||
| 84 | LOG_DEBUG_EXIT; |
84 | LOG_DEBUG_EXIT; |
| 85 | } |
85 | } |
| 86 | 86 | ||
| 87 | void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_) |
87 | void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_) |
| 88 | { |
88 | { |
| 89 | LOG_DEBUG_ENTER; |
89 | LOG_DEBUG_ENTER; |
| 90 | 90 | ||
| 91 | this->signal_on_node_change_.connect(signal_on_node_change_); |
91 | this->signal_on_node_change_.connect(signal_on_node_change_); |
| 92 | 92 | ||
| 93 | LOG_DEBUG_EXIT; |
93 | LOG_DEBUG_EXIT; |
| 94 | } |
94 | } |
| 95 | 95 | ||
| 96 | void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change) |
96 | void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change) |
| 97 | { |
97 | { |
| 98 | LOG_DEBUG_ENTER; |
98 | LOG_DEBUG_ENTER; |
| 99 | 99 | ||
| 100 | this->signal_on_module_change_.connect(slot_on_module_change); |
100 | this->signal_on_module_change_.connect(slot_on_module_change); |
| 101 | 101 | ||
| 102 | LOG_DEBUG_EXIT; |
102 | LOG_DEBUG_EXIT; |
| 103 | } |
103 | } |
| 104 | 104 | ||
| 105 | void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message) |
105 | void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message) |
| 106 | { |
106 | { |
| 107 | LOG_DEBUG_ENTER; |
107 | LOG_DEBUG_ENTER; |
| 108 | 108 | ||
| 109 | this->signal_on_module_message_.connect(slot_on_module_message); |
109 | this->signal_on_module_message_.connect(slot_on_module_message); |
| 110 | 110 | ||
| 111 | LOG_DEBUG_EXIT; |
111 | LOG_DEBUG_EXIT; |
| 112 | } |
112 | } |
| 113 | 113 | ||
| 114 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
114 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
| 115 | { |
115 | { |
| 116 | LOG_DEBUG_ENTER; |
116 | LOG_DEBUG_ENTER; |
| 117 | 117 | ||
| 118 | if (timer_id != this->timer_id_) |
118 | if (timer_id != this->timer_id_) |
| 119 | { |
119 | { |
| 120 | return; |
120 | return; |
| 121 | } |
121 | } |
| 122 | 122 | ||
| 123 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
123 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
| 124 | { |
124 | { |
| 125 | if (!it->second->CheckTimeout()) |
125 | if (!it->second->CheckTimeout()) |
| 126 | { |
126 | { |
| 127 | log::Info(log_module_, "Node %s has not sent anything in a long time, setting offline.", it->second->GetId().c_str()); |
127 | log::Info(log_module_, "Node %s has not sent anything in a long time, setting offline.", it->second->GetId().c_str()); |
| 128 | 128 | ||
| 129 | this->RemoveModules(it->second->GetId()); |
129 | this->RemoveModules(it->second->GetId()); |
| 130 | 130 | ||
| 131 | storage::Manager::Instance()->FlushStore("NodeList"); |
131 | storage::Manager::Instance()->FlushStore("NodeList"); |
| 132 | storage::Manager::Instance()->FlushStore("ModuleList"); |
132 | storage::Manager::Instance()->FlushStore("ModuleList"); |
| 133 | } |
133 | } |
| 134 | } |
134 | } |
| 135 | 135 | ||
| 136 | LOG_DEBUG_EXIT; |
136 | LOG_DEBUG_EXIT; |
| 137 | } |
137 | } |
| 138 | 138 | ||
| 139 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
139 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 140 | { |
140 | { |
| 141 | LOG_DEBUG_ENTER; |
141 | LOG_DEBUG_ENTER; |
| 142 | 142 | ||
| 143 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
143 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
| 144 | { |
144 | { |
| 145 | //LOG.Debug("Message received"); |
145 | //LOG.Debug("Message received"); |
| 146 | can::Message* payload = static_cast<can::Message*>(message->GetPayload().get()); |
146 | can::Message* payload = static_cast<can::Message*>(message->GetPayload().get()); |
| 147 | Node::Pointer node; |
147 | Node::Pointer node; |
| 148 | 148 | ||
| 149 | if (payload->GetClassName() == "nmt") |
149 | if (payload->GetClassName() == "nmt") |
| 150 | { |
150 | { |
| 151 | if (payload->GetCommandName() == "Bios_Start") |
151 | if (payload->GetCommandName() == "Bios_Start") |
| 152 | { |
152 | { |
| 153 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
153 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 154 | node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables()); |
154 | node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables()); |
| 155 | } |
155 | } |
| 156 | else if (payload->GetCommandName() == "App_Start") |
156 | else if (payload->GetCommandName() == "App_Start") |
| 157 | { |
157 | { |
| 158 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
158 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 159 | node->Trigger(Node::EVENT_APP_START, payload->GetVariables()); |
159 | node->Trigger(Node::EVENT_APP_START, payload->GetVariables()); |
| 160 | } |
160 | } |
| 161 | else if (payload->GetCommandName() == "Heartbeat") |
161 | else if (payload->GetCommandName() == "Heartbeat") |
| 162 | { |
162 | { |
| 163 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
163 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 164 | node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables()); |
164 | node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables()); |
| 165 | } |
165 | } |
| 166 | else if (payload->GetCommandName() == "Pgm_Ack") |
166 | else if (payload->GetCommandName() == "Pgm_Ack") |
| 167 | { |
167 | { |
| 168 | node = this->GetNode(this->active_programming_node_id_); |
168 | node = this->GetNode(this->active_programming_node_id_); |
| 169 | node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables()); |
169 | node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables()); |
| 170 | } |
170 | } |
| 171 | else if (payload->GetCommandName() == "Pgm_Nack") |
171 | else if (payload->GetCommandName() == "Pgm_Nack") |
| 172 | { |
172 | { |
| 173 | node = this->GetNode(this->active_programming_node_id_); |
173 | node = this->GetNode(this->active_programming_node_id_); |
| 174 | node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables()); |
174 | node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables()); |
| 175 | } |
175 | } |
| 176 | } |
176 | } |
| 177 | else if (payload->GetDirectionName() == "From_Owner") |
177 | else if (payload->GetDirectionName() == "From_Owner") |
| 178 | { |
178 | { |
| 179 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
179 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| 180 | 180 | ||
| 181 | if (payload->GetCommandName() == "List") |
181 | if (payload->GetCommandName() == "List") |
| 182 | { |
182 | { |
| 183 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
183 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 184 | node->ResetTimeout(); |
184 | node->ResetTimeout(); |
| 185 | 185 | ||
| 186 | module->SetNodeId(node->GetId()); |
186 | module->SetNodeId(node->GetId()); |
| 187 | 187 | ||
| 188 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
188 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
| 189 | { |
189 | { |
| 190 | node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables()); |
190 | node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables()); |
| 191 | } |
191 | } |
| 192 | 192 | ||
| 193 | log::Info(log_module_, "Module %s is available on node %s.", module->GetFullId().c_str(), node->GetId().c_str()); |
193 | log::Info(log_module_, "Module %s is available on node %s.", module->GetFullId().c_str(), node->GetId().c_str()); |
| 194 | this->signal_on_module_change_(module->GetFullId(), true); |
194 | this->signal_on_module_change_(module->GetFullId(), true); |
| 195 | } |
195 | } |
| 196 | else if (module->GetNodeId() != "") |
196 | else if (module->GetNodeId() != "") |
| 197 | { |
197 | { |
| 198 | node = this->GetNode(module->GetNodeId()); |
198 | node = this->GetNode(module->GetNodeId()); |
| 199 | node->ResetTimeout(); |
199 | node->ResetTimeout(); |
| 200 | 200 | ||
| 201 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
201 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
| 202 | } |
202 | } |
| 203 | else |
203 | else |
| 204 | { |
204 | { |
| 205 | log::Info(log_module_, "Nothing found, id=%d, modulename=%s, classname=%s", payload->GetId(), payload->GetModuleName().data(), payload->GetClassName().data()); |
205 | log::Info(log_module_, "Nothing found, id=%d, modulename=%s, classname=%s", payload->GetId(), payload->GetModuleName().data(), payload->GetClassName().data()); |
| 206 | } |
206 | } |
| 207 | } |
207 | } |
| 208 | } |
208 | } |
| 209 | 209 | ||
| 210 | LOG_DEBUG_EXIT; |
210 | LOG_DEBUG_EXIT; |
| 211 | } |
211 | } |
| 212 | 212 | ||
| 213 | void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state) |
213 | void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state) |
| 214 | { |
214 | { |
| 215 | LOG_DEBUG_ENTER; |
215 | LOG_DEBUG_ENTER; |
| 216 | 216 | ||
| 217 | if (target_state != Node::STATE_NORM_INITIALIZED) |
217 | if (target_state != Node::STATE_NORM_INITIALIZED) |
| 218 | { |
218 | { |
| 219 | this->RemoveModules(node_id); |
219 | this->RemoveModules(node_id); |
| 220 | } |
220 | } |
| 221 | 221 | ||
| 222 | if (current_state != target_state) |
222 | if (current_state != target_state) |
| 223 | { |
223 | { |
| 224 | this->signal_on_node_change_(node_id, (target_state == Node::STATE_NORM_INITIALIZED || target_state == Node::STATE_NORM_LIST || target_state == Node::STATE_NORM_ONLINE)); |
224 | this->signal_on_node_change_(node_id, (target_state == Node::STATE_NORM_INITIALIZED || target_state == Node::STATE_NORM_LIST || target_state == Node::STATE_NORM_ONLINE)); |
| 225 | } |
225 | } |
| 226 | 226 | ||
| 227 | if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE) |
227 | if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE) |
| 228 | { |
228 | { |
| 229 | this->active_programming_node_id_ = node_id; |
229 | this->active_programming_node_id_ = node_id; |
| 230 | } |
230 | } |
| 231 | else if (target_state == Node::STATE_NORM_OFFLINE) |
231 | else if (target_state == Node::STATE_NORM_OFFLINE) |
| 232 | { |
232 | { |
| 233 | this->active_programming_node_id_ = ""; |
233 | this->active_programming_node_id_ = ""; |
| 234 | } |
234 | } |
| 235 | 235 | ||
| 236 | LOG_DEBUG_EXIT; |
236 | LOG_DEBUG_EXIT; |
| 237 | } |
237 | } |
| 238 | 238 | ||
| 239 | void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
239 | void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
| 240 | { |
240 | { |
| 241 | LOG_DEBUG_ENTER; |
241 | LOG_DEBUG_ENTER; |
| 242 | 242 | ||
| 243 | ModuleList::iterator it = this->modules_.find(full_id); |
243 | ModuleList::iterator it = this->modules_.find(full_id); |
| 244 | 244 | ||
| 245 | if (it == this->modules_.end()) |
245 | if (it == this->modules_.end()) |
| 246 | { |
246 | { |
| 247 | log::Warning(log_module_, "Can not send message to %s , it is not available.", full_id.c_str()); |
247 | log::Warning(log_module_, "Can not send message to %s , it is not available.", full_id.c_str()); |
| 248 | return; |
248 | return; |
| 249 | } |
249 | } |
| 250 | 250 | ||
| 251 | try |
251 | try |
| 252 | { |
252 | { |
| 253 | can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
253 | can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
| 254 | payload->SetVariables(variables); |
254 | payload->SetVariables(variables); |
| 255 | 255 | ||
| 256 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
256 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 257 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
257 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
| 258 | } |
258 | } |
| 259 | catch (std::runtime_error& e) |
259 | catch (std::runtime_error& e) |
| 260 | { |
260 | { |
| 261 | log::Error(log_module_, "Failed to send message, %s", e.what()); |
261 | log::Error(log_module_, "Failed to send message, %s", e.what()); |
| 262 | } |
262 | } |
| 263 | 263 | ||
| 264 | LOG_DEBUG_EXIT; |
264 | LOG_DEBUG_EXIT; |
| 265 | } |
265 | } |
| 266 | 266 | ||
| 267 | void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables) |
267 | void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables) |
| 268 | { |
268 | { |
| 269 | LOG_DEBUG_ENTER; |
269 | LOG_DEBUG_ENTER; |
| 270 | 270 | ||
| 271 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
271 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
| 272 | 272 | ||
| 273 | LOG_DEBUG_EXIT; |
273 | LOG_DEBUG_EXIT; |
| 274 | } |
274 | } |
| 275 | 275 | ||
| 276 | common::StringList Manager::GetAvailableModules() |
276 | common::StringList Manager::GetAvailableModules() |
| 277 | { |
277 | { |
| 278 | LOG_DEBUG_ENTER; |
278 | LOG_DEBUG_ENTER; |
| 279 | 279 | ||
| 280 | common::StringList available_modules; |
280 | common::StringList available_modules; |
| 281 | 281 | ||
| 282 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
282 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 283 | { |
283 | { |
| 284 | available_modules.push_back(it->first); |
284 | available_modules.push_back(it->first); |
| 285 | } |
285 | } |
| 286 | 286 | ||
| 287 | LOG_DEBUG_EXIT; |
287 | LOG_DEBUG_EXIT; |
| 288 | 288 | ||
| 289 | return available_modules; |
289 | return available_modules; |
| 290 | } |
290 | } |
| 291 | 291 | ||
| 292 | common::StringList Manager::GetAvailableNodes() |
292 | common::StringList Manager::GetAvailableNodes() |
| 293 | { |
293 | { |
| 294 | LOG_DEBUG_ENTER; |
294 | LOG_DEBUG_ENTER; |
| 295 | 295 | ||
| 296 | common::StringList available_nodes; |
296 | common::StringList available_nodes; |
| 297 | 297 | ||
| 298 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
298 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
| 299 | { |
299 | { |
| 300 | std::string node = it->first; |
300 | std::string node = it->first; |
| 301 | 301 | ||
| 302 | for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++) |
302 | for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++) |
| 303 | { |
303 | { |
| 304 | if (it2->second->GetNodeId() == it->first) |
304 | if (it2->second->GetNodeId() == it->first) |
| 305 | { |
305 | { |
| 306 | node += "," + it2->first; |
306 | node += "," + it2->first; |
| 307 | } |
307 | } |
| 308 | } |
308 | } |
| 309 | 309 | ||
| 310 | available_nodes.push_back(node); |
310 | available_nodes.push_back(node); |
| 311 | } |
311 | } |
| 312 | 312 | ||
| 313 | LOG_DEBUG_EXIT; |
313 | LOG_DEBUG_EXIT; |
| 314 | 314 | ||
| 315 | return available_nodes; |
315 | return available_nodes; |
| 316 | } |
316 | } |
| 317 | 317 | ||
| 318 | bool Manager::ResetNode(Node::Id node_id) |
318 | bool Manager::ResetNode(Node::Id node_id) |
| 319 | { |
319 | { |
| 320 | LOG_DEBUG_ENTER; |
320 | LOG_DEBUG_ENTER; |
| 321 | 321 | ||
| 322 | NodeList::iterator it = this->nodes_.find(node_id); |
322 | NodeList::iterator it = this->nodes_.find(node_id); |
| 323 | 323 | ||
| 324 | if (it == this->nodes_.end()) |
324 | if (it == this->nodes_.end()) |
| 325 | { |
325 | { |
| 326 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
326 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
| 327 | return false; |
327 | return false; |
| 328 | } |
328 | } |
| 329 | 329 | ||
| 330 | it->second->Reset(); |
330 | it->second->Reset(); |
| 331 | 331 | ||
| 332 | LOG_DEBUG_EXIT; |
332 | LOG_DEBUG_EXIT; |
| 333 | 333 | ||
| 334 | return true; |
334 | return true; |
| - | 335 | } |
|
| - | 336 | ||
| - | 337 | unsigned int Manager::GetProgramProgress(Node::Id node_id) |
|
| - | 338 | { |
|
| - | 339 | LOG_DEBUG_ENTER; |
|
| - | 340 | ||
| - | 341 | NodeList::iterator it = this->nodes_.find(node_id); |
|
| - | 342 | ||
| - | 343 | if (it == this->nodes_.end()) |
|
| - | 344 | { |
|
| - | 345 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
|
| - | 346 | return 0; |
|
| - | 347 | } |
|
| - | 348 | ||
| - | 349 | return it->second->GetProgramProgress(); |
|
| - | 350 | ||
| - | 351 | LOG_DEBUG_EXIT; |
|
| 335 | } |
352 | } |
| 336 | 353 | ||
| 337 | bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename) |
354 | bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename) |
| 338 | { |
355 | { |
| 339 | LOG_DEBUG_ENTER; |
356 | LOG_DEBUG_ENTER; |
| 340 | 357 | ||
| 341 | NodeList::iterator it = this->nodes_.find(node_id); |
358 | NodeList::iterator it = this->nodes_.find(node_id); |
| 342 | 359 | ||
| 343 | if (it == this->nodes_.end()) |
360 | if (it == this->nodes_.end()) |
| 344 | { |
361 | { |
| 345 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
362 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
| 346 | return false; |
363 | return false; |
| 347 | } |
364 | } |
| 348 | 365 | ||
| 349 | Code::Pointer code = Code::Pointer(new Code()); |
366 | Code::Pointer code = Code::Pointer(new Code()); |
| 350 | if (!code->LoadIntelHexFile(filename)) |
367 | if (!code->LoadIntelHexFile(filename)) |
| 351 | { |
368 | { |
| 352 | log::Error(log_module_, "Could not parse %s.", filename.c_str()); |
369 | log::Error(log_module_, "Could not parse %s.", filename.c_str()); |
| 353 | return false; |
370 | return false; |
| 354 | } |
371 | } |
| 355 | 372 | ||
| 356 | if (is_bios) |
373 | if (is_bios) |
| 357 | { |
374 | { |
| 358 | log::Debug(log_module_, "is_bios true"); |
375 | log::Debug(log_module_, "is_bios true"); |
| 359 | it->second->ProgramBios(code); |
376 | it->second->ProgramBios(code); |
| 360 | } |
377 | } |
| 361 | else |
378 | else |
| 362 | { |
379 | { |
| 363 | log::Debug(log_module_, "is_bios false"); |
380 | log::Debug(log_module_, "is_bios false"); |
| 364 | it->second->ProgramApplication(code); |
381 | it->second->ProgramApplication(code); |
| 365 | } |
382 | } |
| 366 | 383 | ||
| 367 | LOG_DEBUG_EXIT; |
384 | LOG_DEBUG_EXIT; |
| 368 | 385 | ||
| 369 | return true; |
386 | return true; |
| 370 | } |
387 | } |
| 371 | 388 | ||
| 372 | /* Program node with hexdata as argument */ |
389 | /* Program node with hexdata as argument */ |
| 373 | bool Manager::ProgramNodeHex(Node::Id node_id, bool is_bios, std::string hex_data) |
390 | bool Manager::ProgramNodeHex(Node::Id node_id, bool is_bios, std::string hex_data) |
| 374 | { |
391 | { |
| 375 | LOG_DEBUG_ENTER; |
392 | LOG_DEBUG_ENTER; |
| 376 | 393 | ||
| 377 | NodeList::iterator it = this->nodes_.find(node_id); |
394 | NodeList::iterator it = this->nodes_.find(node_id); |
| 378 | 395 | ||
| 379 | if (it == this->nodes_.end()) |
396 | if (it == this->nodes_.end()) |
| 380 | { |
397 | { |
| 381 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
398 | log::Error(log_module_, "Could not find node %s.", node_id.c_str()); |
| 382 | return false; |
399 | return false; |
| 383 | } |
400 | } |
| 384 | 401 | ||
| 385 | Code::Pointer code = Code::Pointer(new Code()); |
402 | Code::Pointer code = Code::Pointer(new Code()); |
| 386 | 403 | ||
| 387 | if (!code->ParseIntelHex(hex_data)) |
404 | if (!code->ParseIntelHex(hex_data)) |
| 388 | { |
405 | { |
| 389 | log::Error(log_module_, "Could not parse hexdata %s.", hex_data.c_str()); |
406 | log::Error(log_module_, "Could not parse hexdata %s.", hex_data.c_str()); |
| 390 | return false; |
407 | return false; |
| 391 | } |
408 | } |
| 392 | 409 | ||
| 393 | if (is_bios) |
410 | if (is_bios) |
| 394 | { |
411 | { |
| 395 | log::Debug(log_module_, "is_bios true"); |
412 | log::Debug(log_module_, "is_bios true"); |
| 396 | it->second->ProgramBios(code); |
413 | it->second->ProgramBios(code); |
| 397 | } |
414 | } |
| 398 | else |
415 | else |
| 399 | { |
416 | { |
| 400 | log::Debug(log_module_, "is_bios false"); |
417 | log::Debug(log_module_, "is_bios false"); |
| 401 | it->second->ProgramApplication(code); |
418 | it->second->ProgramApplication(code); |
| 402 | } |
419 | } |
| 403 | 420 | ||
| 404 | LOG_DEBUG_EXIT; |
421 | LOG_DEBUG_EXIT; |
| 405 | 422 | ||
| 406 | return true; |
423 | return true; |
| 407 | } |
424 | } |
| 408 | 425 | ||
| 409 | Node::Information Manager::GetNodeInformation(Node::Id node_id) |
426 | Node::Information Manager::GetNodeInformation(Node::Id node_id) |
| 410 | { |
427 | { |
| 411 | LOG_DEBUG_ENTER; |
428 | LOG_DEBUG_ENTER; |
| 412 | 429 | ||
| 413 | NodeList::iterator it = this->nodes_.find(node_id); |
430 | NodeList::iterator it = this->nodes_.find(node_id); |
| 414 | 431 | ||
| 415 | if (it == this->nodes_.end()) |
432 | if (it == this->nodes_.end()) |
| 416 | { |
433 | { |
| 417 | throw std::runtime_error("No such node exists, " + node_id); |
434 | throw std::runtime_error("No such node exists, " + node_id); |
| 418 | } |
435 | } |
| 419 | 436 | ||
| 420 | LOG_DEBUG_EXIT; |
437 | LOG_DEBUG_EXIT; |
| 421 | 438 | ||
| 422 | return it->second->GetInformation(); |
439 | return it->second->GetInformation(); |
| 423 | } |
440 | } |
| 424 | 441 | ||
| 425 | Node::Pointer Manager::GetNode(Node::Id node_id) |
442 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 426 | { |
443 | { |
| 427 | LOG_DEBUG_ENTER; |
444 | LOG_DEBUG_ENTER; |
| 428 | 445 | ||
| 429 | Node::Pointer node; |
446 | Node::Pointer node; |
| 430 | NodeList::iterator it = this->nodes_.find(node_id); |
447 | NodeList::iterator it = this->nodes_.find(node_id); |
| 431 | 448 | ||
| 432 | if (it == this->nodes_.end()) |
449 | if (it == this->nodes_.end()) |
| 433 | { |
450 | { |
| 434 | node = Node::Pointer(new Node(node_id)); |
451 | node = Node::Pointer(new Node(node_id)); |
| 435 | node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_)); |
452 | node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_)); |
| 436 | this->nodes_[node_id] = node; |
453 | this->nodes_[node_id] = node; |
| 437 | } |
454 | } |
| 438 | else |
455 | else |
| 439 | { |
456 | { |
| 440 | node = it->second; |
457 | node = it->second; |
| 441 | } |
458 | } |
| 442 | 459 | ||
| 443 | LOG_DEBUG_EXIT; |
460 | LOG_DEBUG_EXIT; |
| 444 | 461 | ||
| 445 | return node; |
462 | return node; |
| 446 | } |
463 | } |
| 447 | 464 | ||
| 448 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
465 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
| 449 | { |
466 | { |
| 450 | LOG_DEBUG_ENTER; |
467 | LOG_DEBUG_ENTER; |
| 451 | 468 | ||
| 452 | Module::Pointer module; |
469 | Module::Pointer module; |
| 453 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
470 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
| 454 | 471 | ||
| 455 | if (it == this->modules_.end()) |
472 | if (it == this->modules_.end()) |
| 456 | { |
473 | { |
| 457 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
474 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
| 458 | this->modules_[module->GetFullId()] = module; |
475 | this->modules_[module->GetFullId()] = module; |
| 459 | } |
476 | } |
| 460 | else |
477 | else |
| 461 | { |
478 | { |
| 462 | module = it->second; |
479 | module = it->second; |
| 463 | } |
480 | } |
| 464 | 481 | ||
| 465 | LOG_DEBUG_EXIT; |
482 | LOG_DEBUG_EXIT; |
| 466 | 483 | ||
| 467 | return module; |
484 | return module; |
| 468 | } |
485 | } |
| 469 | 486 | ||
| 470 | void Manager::RemoveModules(Node::Id node_id) |
487 | void Manager::RemoveModules(Node::Id node_id) |
| 471 | { |
488 | { |
| 472 | LOG_DEBUG_ENTER; |
489 | LOG_DEBUG_ENTER; |
| 473 | 490 | ||
| 474 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
491 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 475 | { |
492 | { |
| 476 | if (it->second->GetNodeId() == node_id) |
493 | if (it->second->GetNodeId() == node_id) |
| 477 | { |
494 | { |
| 478 | log::Info(log_module_, "Module %s is unavailable.", it->second->GetFullId().c_str()); |
495 | log::Info(log_module_, "Module %s is unavailable.", it->second->GetFullId().c_str()); |
| 479 | this->signal_on_module_change_(it->second->GetFullId(), false); |
496 | this->signal_on_module_change_(it->second->GetFullId(), false); |
| 480 | 497 | ||
| 481 | this->modules_.erase(it); |
498 | this->modules_.erase(it); |
| 482 | break; |
499 | break; |
| 483 | } |
500 | } |
| 484 | } |
501 | } |
| 485 | 502 | ||
| 486 | LOG_DEBUG_EXIT; |
503 | LOG_DEBUG_EXIT; |
| 487 | } |
504 | } |
| 488 | 505 | ||
| 489 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
506 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
| 490 | { |
507 | { |
| 491 | LOG_DEBUG_ENTER; |
508 | LOG_DEBUG_ENTER; |
| 492 | 509 | ||
| 493 | unsigned int number_of_modules = 0; |
510 | unsigned int number_of_modules = 0; |
| 494 | 511 | ||
| 495 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
512 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
| 496 | { |
513 | { |
| 497 | if (it->second->GetNodeId() == node_id) |
514 | if (it->second->GetNodeId() == node_id) |
| 498 | { |
515 | { |
| 499 | number_of_modules++; |
516 | number_of_modules++; |
| 500 | } |
517 | } |
| 501 | } |
518 | } |
| 502 | 519 | ||
| 503 | LOG_DEBUG_EXIT; |
520 | LOG_DEBUG_EXIT; |
| 504 | 521 | ||
| 505 | return number_of_modules; |
522 | return number_of_modules; |
| 506 | } |
523 | } |
| 507 | 524 | ||
| 508 | }; // namespace control |
525 | }; // namespace control |
| 509 | }; // namespace atom |
526 | }; // namespace atom |
| 510 | 527 | ||