Rev 1646 | Rev 1666 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1598 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 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 |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 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., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include "Manager.h" |
||
| 22 | |||
| 23 | #include <boost/lexical_cast.hpp> |
||
| 24 | |||
| 25 | #include "broker/Manager.h" |
||
| 26 | #include "broker/Message.h" |
||
| 1642 | runge | 27 | #include "common/common.h" |
| 1601 | runge | 28 | #include "timer/Manager.h" |
| 1627 | runge | 29 | #include "storage/Manager.h" |
| 1601 | runge | 30 | |
| 1642 | runge | 31 | #include "can/Message.h" |
| 1598 | runge | 32 | |
| 33 | namespace atom { |
||
| 1642 | runge | 34 | namespace control { |
| 1598 | runge | 35 | |
| 1599 | runge | 36 | Manager::Pointer Manager::instance_; |
| 1598 | runge | 37 | |
| 1642 | runge | 38 | Manager::Manager() : broker::Subscriber(false), LOG("control::Manager") |
| 1598 | runge | 39 | { |
| 1627 | runge | 40 | Node::SetupStateMachine(); |
| 41 | |||
| 1657 | runge | 42 | this->active_programming_node_id_ = ""; |
| 1633 | runge | 43 | |
| 1619 | runge | 44 | // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds |
| 1646 | runge | 45 | this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true); |
| 1598 | runge | 46 | } |
| 47 | |||
| 48 | Manager::~Manager() |
||
| 49 | { |
||
| 50 | } |
||
| 51 | |||
| 52 | Manager::Pointer Manager::Instance() |
||
| 53 | { |
||
| 54 | return Manager::instance_; |
||
| 55 | } |
||
| 56 | |||
| 1599 | runge | 57 | void Manager::Create() |
| 58 | { |
||
| 59 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 60 | } |
||
| 61 | |||
| 1598 | runge | 62 | void Manager::Delete() |
| 63 | { |
||
| 64 | Manager::instance_.reset(); |
||
| 65 | } |
||
| 66 | |||
| 1657 | runge | 67 | void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_) |
| 1602 | runge | 68 | { |
| 1614 | runge | 69 | this->signal_on_node_change_.connect(signal_on_node_change_); |
| 1657 | runge | 70 | } |
| 71 | |||
| 72 | void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change) |
||
| 73 | { |
||
| 1602 | runge | 74 | this->signal_on_module_change_.connect(slot_on_module_change); |
| 1657 | runge | 75 | } |
| 76 | |||
| 77 | void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message) |
||
| 78 | { |
||
| 1602 | runge | 79 | this->signal_on_module_message_.connect(slot_on_module_message); |
| 80 | } |
||
| 81 | |||
| 1601 | runge | 82 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
| 83 | { |
||
| 84 | if (timer_id != this->timer_id_) |
||
| 85 | { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
||
| 90 | { |
||
| 91 | if (!it->second->CheckTimeout()) |
||
| 92 | { |
||
| 1657 | runge | 93 | LOG.Info("Node " +it->second->GetId() + " has not sent anything in a long time, setting offline."); |
| 1601 | runge | 94 | this->RemoveModules(it->second->GetId()); |
| 1627 | runge | 95 | |
| 96 | storage::Manager::Instance()->FlushStore("NodeList"); |
||
| 97 | storage::Manager::Instance()->FlushStore("ModuleList"); |
||
| 1601 | runge | 98 | } |
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 1598 | runge | 102 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 103 | { |
||
| 104 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
||
| 105 | { |
||
| 1604 | runge | 106 | //LOG.Debug("Message received"); |
| 1642 | runge | 107 | can::Message* payload = static_cast<can::Message*>(message->GetPayload().get()); |
| 1598 | runge | 108 | Node::Pointer node; |
| 109 | |||
| 110 | if (payload->GetClassName() == "nmt") |
||
| 111 | { |
||
| 112 | if (payload->GetCommandName() == "Bios_Start") |
||
| 113 | { |
||
| 1657 | runge | 114 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1627 | runge | 115 | node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables()); |
| 1598 | runge | 116 | } |
| 1601 | runge | 117 | else if (payload->GetCommandName() == "App_Start") |
| 1598 | runge | 118 | { |
| 1657 | runge | 119 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1627 | runge | 120 | node->Trigger(Node::EVENT_APP_START, payload->GetVariables()); |
| 1598 | runge | 121 | } |
| 122 | else if (payload->GetCommandName() == "Heartbeat") |
||
| 123 | { |
||
| 1657 | runge | 124 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1627 | runge | 125 | node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables()); |
| 1598 | runge | 126 | } |
| 1642 | runge | 127 | else if (payload->GetCommandName() == "Pgm_Ack") |
| 128 | { |
||
| 129 | node = this->GetNode(this->active_programming_node_id_); |
||
| 130 | node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables()); |
||
| 131 | } |
||
| 132 | else if (payload->GetCommandName() == "Pgm_Nack") |
||
| 133 | { |
||
| 134 | node = this->GetNode(this->active_programming_node_id_); |
||
| 135 | node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables()); |
||
| 136 | } |
||
| 1598 | runge | 137 | } |
| 1602 | runge | 138 | else if (payload->GetDirectionName() == "From_Owner") |
| 1598 | runge | 139 | { |
| 1602 | runge | 140 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| 141 | |||
| 142 | if (payload->GetCommandName() == "List") |
||
| 1598 | runge | 143 | { |
| 1657 | runge | 144 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1601 | runge | 145 | node->ResetTimeout(); |
| 1598 | runge | 146 | |
| 147 | module->SetNodeId(node->GetId()); |
||
| 148 | |||
| 149 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
||
| 150 | { |
||
| 1627 | runge | 151 | node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables()); |
| 1598 | runge | 152 | } |
| 153 | |||
| 1657 | runge | 154 | LOG.Info("Module " + module->GetFullId() + " is available on node " + node->GetId() + "."); |
| 1602 | runge | 155 | this->signal_on_module_change_(module->GetFullId(), true); |
| 1598 | runge | 156 | } |
| 1657 | runge | 157 | else if (module->GetNodeId() != "") |
| 1602 | runge | 158 | { |
| 1619 | runge | 159 | node = this->GetNode(module->GetNodeId()); |
| 160 | node->ResetTimeout(); |
||
| 161 | |||
| 1602 | runge | 162 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
| 163 | } |
||
| 1598 | runge | 164 | } |
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 1627 | runge | 168 | void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state) |
| 169 | { |
||
| 170 | if (target_state != Node::STATE_NORM_INITIALIZED) |
||
| 171 | { |
||
| 172 | this->RemoveModules(node_id); |
||
| 1657 | runge | 173 | |
| 174 | if (current_state != target_state) |
||
| 175 | { |
||
| 176 | this->signal_on_node_change_(node_id, false); |
||
| 177 | } |
||
| 1627 | runge | 178 | } |
| 1657 | runge | 179 | else |
| 180 | { |
||
| 181 | if (current_state != target_state) |
||
| 182 | { |
||
| 183 | this->signal_on_node_change_(node_id, true); |
||
| 184 | } |
||
| 185 | } |
||
| 1633 | runge | 186 | |
| 187 | if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE) |
||
| 188 | { |
||
| 189 | this->active_programming_node_id_ = node_id; |
||
| 190 | } |
||
| 191 | else if (target_state == Node::STATE_NORM_OFFLINE) |
||
| 192 | { |
||
| 1657 | runge | 193 | this->active_programming_node_id_ = ""; |
| 1633 | runge | 194 | } |
| 1627 | runge | 195 | } |
| 196 | |||
| 1642 | runge | 197 | void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
| 1602 | runge | 198 | { |
| 199 | ModuleList::iterator it = this->modules_.find(full_id); |
||
| 200 | |||
| 201 | if (it == this->modules_.end()) |
||
| 202 | { |
||
| 203 | LOG.Warning("Can not send message to " + full_id + ", it is not available"); |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | try |
||
| 208 | { |
||
| 1642 | runge | 209 | can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
| 1602 | runge | 210 | payload->SetVariables(variables); |
| 211 | |||
| 212 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
||
| 1607 | runge | 213 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
| 1602 | runge | 214 | } |
| 215 | catch (std::runtime_error& e) |
||
| 216 | { |
||
| 217 | LOG.Error("Failed to send message, " + std::string(e.what())); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 1642 | runge | 221 | void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables) |
| 1604 | runge | 222 | { |
| 223 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
||
| 224 | } |
||
| 225 | |||
| 1644 | runge | 226 | common::StringList Manager::GetAvailableModules() |
| 1604 | runge | 227 | { |
| 1644 | runge | 228 | common::StringList available_modules; |
| 229 | |||
| 230 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 231 | { |
||
| 232 | available_modules.push_back(it->first); |
||
| 233 | } |
||
| 234 | |||
| 235 | return available_modules; |
||
| 1604 | runge | 236 | } |
| 237 | |||
| 1657 | runge | 238 | common::StringList Manager::GetAvailableNodes() |
| 239 | { |
||
| 240 | common::StringList available_nodes; |
||
| 241 | |||
| 242 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
||
| 243 | { |
||
| 244 | std::string node = it->first; |
||
| 245 | |||
| 246 | for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++) |
||
| 247 | { |
||
| 248 | if (it2->second->GetNodeId() == it->first) |
||
| 249 | { |
||
| 250 | node += "," + it2->first; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | available_nodes.push_back(node); |
||
| 255 | } |
||
| 256 | |||
| 257 | return available_nodes; |
||
| 258 | } |
||
| 259 | |||
| 260 | bool Manager::ResetNode(Node::Id node_id) |
||
| 261 | { |
||
| 262 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 263 | |||
| 264 | if (it == this->nodes_.end()) |
||
| 265 | { |
||
| 266 | LOG.Error("Could not find node " + node_id); |
||
| 267 | return false; |
||
| 268 | } |
||
| 269 | |||
| 270 | it->second->Reset(); |
||
| 271 | return true; |
||
| 272 | } |
||
| 273 | |||
| 1642 | runge | 274 | bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename) |
| 275 | { |
||
| 276 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 277 | |||
| 278 | if (it == this->nodes_.end()) |
||
| 279 | { |
||
| 1657 | runge | 280 | LOG.Error("Could not find node " + node_id); |
| 1642 | runge | 281 | return false; |
| 282 | } |
||
| 283 | |||
| 284 | Code::Pointer code = Code::Pointer(new Code()); |
||
| 285 | if (!code->LoadIntelHexFile(filename)) |
||
| 286 | { |
||
| 287 | LOG.Error("Failed to parse " + filename); |
||
| 288 | return false; |
||
| 289 | } |
||
| 290 | |||
| 291 | if (is_bios) |
||
| 292 | { |
||
| 293 | LOG.Debug("is_bios true"); |
||
| 294 | it->second->ProgramBios(code); |
||
| 295 | } |
||
| 296 | else |
||
| 297 | { |
||
| 298 | LOG.Debug("is_bios false"); |
||
| 299 | it->second->ProgramApplication(code); |
||
| 300 | } |
||
| 301 | |||
| 302 | return true; |
||
| 303 | } |
||
| 304 | |||
| 1598 | runge | 305 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 306 | { |
||
| 307 | Node::Pointer node; |
||
| 308 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 309 | |||
| 310 | if (it == this->nodes_.end()) |
||
| 311 | { |
||
| 312 | node = Node::Pointer(new Node(node_id)); |
||
| 1627 | runge | 313 | node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_)); |
| 1598 | runge | 314 | this->nodes_[node_id] = node; |
| 315 | } |
||
| 316 | else |
||
| 317 | { |
||
| 318 | node = it->second; |
||
| 319 | } |
||
| 320 | |||
| 321 | return node; |
||
| 322 | } |
||
| 323 | |||
| 324 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
||
| 325 | { |
||
| 326 | Module::Pointer module; |
||
| 327 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
||
| 328 | |||
| 329 | if (it == this->modules_.end()) |
||
| 330 | { |
||
| 331 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
||
| 332 | this->modules_[module->GetFullId()] = module; |
||
| 333 | } |
||
| 334 | else |
||
| 335 | { |
||
| 336 | module = it->second; |
||
| 337 | } |
||
| 338 | |||
| 339 | return module; |
||
| 340 | } |
||
| 341 | |||
| 342 | void Manager::RemoveModules(Node::Id node_id) |
||
| 343 | { |
||
| 344 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 345 | { |
||
| 346 | if (it->second->GetNodeId() == node_id) |
||
| 347 | { |
||
| 348 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
||
| 1602 | runge | 349 | this->signal_on_module_change_(it->second->GetFullId(), false); |
| 1598 | runge | 350 | |
| 351 | this->modules_.erase(it); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
||
| 357 | { |
||
| 358 | unsigned int number_of_modules = 0; |
||
| 359 | |||
| 360 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 361 | { |
||
| 362 | if (it->second->GetNodeId() == node_id) |
||
| 363 | { |
||
| 364 | number_of_modules++; |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | return number_of_modules; |
||
| 369 | } |
||
| 370 | |||
| 1642 | runge | 371 | }; // namespace control |
| 1598 | runge | 372 | }; // namespace atom |