Rev 1887 | Rev 1916 | 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" |
| 1914 | linlun | 32 | #include "can/CanDaemon.cpp" |
| 1598 | runge | 33 | |
| 34 | namespace atom { |
||
| 1642 | runge | 35 | namespace control { |
| 1598 | runge | 36 | |
| 1599 | runge | 37 | Manager::Pointer Manager::instance_; |
| 1598 | runge | 38 | |
| 1642 | runge | 39 | Manager::Manager() : broker::Subscriber(false), LOG("control::Manager") |
| 1598 | runge | 40 | { |
| 1627 | runge | 41 | Node::SetupStateMachine(); |
| 42 | |||
| 1657 | runge | 43 | this->active_programming_node_id_ = ""; |
| 1633 | runge | 44 | |
| 1619 | runge | 45 | // Nyquist–Shannon sampling theorem state that we need to double the time, modules send every 10 seconds |
| 1646 | runge | 46 | this->timer_id_ = timer::Manager::Instance()->SetTimer(20000, true); |
| 1598 | runge | 47 | } |
| 48 | |||
| 49 | Manager::~Manager() |
||
| 50 | { |
||
| 51 | } |
||
| 52 | |||
| 53 | Manager::Pointer Manager::Instance() |
||
| 54 | { |
||
| 55 | return Manager::instance_; |
||
| 56 | } |
||
| 57 | |||
| 1599 | runge | 58 | void Manager::Create() |
| 59 | { |
||
| 60 | Manager::instance_ = Manager::Pointer(new Manager()); |
||
| 61 | } |
||
| 62 | |||
| 1598 | runge | 63 | void Manager::Delete() |
| 64 | { |
||
| 65 | Manager::instance_.reset(); |
||
| 66 | } |
||
| 67 | |||
| 1657 | runge | 68 | void Manager::ConnectSlotNode(const SignalOnNodeChange::slot_type& signal_on_node_change_) |
| 1602 | runge | 69 | { |
| 1614 | runge | 70 | this->signal_on_node_change_.connect(signal_on_node_change_); |
| 1657 | runge | 71 | } |
| 72 | |||
| 73 | void Manager::ConnectSlotModule(const SignalOnModuleChange::slot_type& slot_on_module_change) |
||
| 74 | { |
||
| 1602 | runge | 75 | this->signal_on_module_change_.connect(slot_on_module_change); |
| 1657 | runge | 76 | } |
| 77 | |||
| 78 | void Manager::ConnectSlotModule(const SignalOnModuleMessage::slot_type& slot_on_module_message) |
||
| 79 | { |
||
| 1602 | runge | 80 | this->signal_on_module_message_.connect(slot_on_module_message); |
| 81 | } |
||
| 82 | |||
| 1601 | runge | 83 | void Manager::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat) |
| 84 | { |
||
| 85 | if (timer_id != this->timer_id_) |
||
| 86 | { |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
||
| 91 | { |
||
| 92 | if (!it->second->CheckTimeout()) |
||
| 93 | { |
||
| 1657 | runge | 94 | LOG.Info("Node " +it->second->GetId() + " has not sent anything in a long time, setting offline."); |
| 1601 | runge | 95 | this->RemoveModules(it->second->GetId()); |
| 1627 | runge | 96 | |
| 97 | storage::Manager::Instance()->FlushStore("NodeList"); |
||
| 98 | storage::Manager::Instance()->FlushStore("ModuleList"); |
||
| 1601 | runge | 99 | } |
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 1598 | runge | 103 | void Manager::SlotOnMessageHandler(broker::Message::Pointer message) |
| 104 | { |
||
| 105 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
||
| 106 | { |
||
| 1604 | runge | 107 | //LOG.Debug("Message received"); |
| 1642 | runge | 108 | can::Message* payload = static_cast<can::Message*>(message->GetPayload().get()); |
| 1598 | runge | 109 | Node::Pointer node; |
| 110 | |||
| 111 | if (payload->GetClassName() == "nmt") |
||
| 112 | { |
||
| 113 | if (payload->GetCommandName() == "Bios_Start") |
||
| 114 | { |
||
| 1657 | runge | 115 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1627 | runge | 116 | node->Trigger(Node::EVENT_BIOS_START, payload->GetVariables()); |
| 1598 | runge | 117 | } |
| 1601 | runge | 118 | else if (payload->GetCommandName() == "App_Start") |
| 1598 | runge | 119 | { |
| 1657 | runge | 120 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1627 | runge | 121 | node->Trigger(Node::EVENT_APP_START, payload->GetVariables()); |
| 1598 | runge | 122 | } |
| 123 | else if (payload->GetCommandName() == "Heartbeat") |
||
| 124 | { |
||
| 1657 | runge | 125 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1627 | runge | 126 | node->Trigger(Node::EVENT_HEARTBEAT, payload->GetVariables()); |
| 1598 | runge | 127 | } |
| 1642 | runge | 128 | else if (payload->GetCommandName() == "Pgm_Ack") |
| 129 | { |
||
| 130 | node = this->GetNode(this->active_programming_node_id_); |
||
| 131 | node->Trigger(Node::EVENT_PGM_ACK, payload->GetVariables()); |
||
| 132 | } |
||
| 133 | else if (payload->GetCommandName() == "Pgm_Nack") |
||
| 134 | { |
||
| 135 | node = this->GetNode(this->active_programming_node_id_); |
||
| 136 | node->Trigger(Node::EVENT_PGM_NACK, payload->GetVariables()); |
||
| 137 | } |
||
| 1598 | runge | 138 | } |
| 1602 | runge | 139 | else if (payload->GetDirectionName() == "From_Owner") |
| 1598 | runge | 140 | { |
| 1602 | runge | 141 | Module::Pointer module = this->GetModule(boost::lexical_cast<unsigned int>(payload->GetId()), payload->GetModuleName(), payload->GetClassName()); |
| 142 | |||
| 143 | if (payload->GetCommandName() == "List") |
||
| 1598 | runge | 144 | { |
| 1657 | runge | 145 | node = this->GetNode(common::ToHex(boost::lexical_cast<unsigned int>(payload->GetVariables()["HardwareId"]))); |
| 1601 | runge | 146 | node->ResetTimeout(); |
| 1598 | runge | 147 | |
| 148 | module->SetNodeId(node->GetId()); |
||
| 149 | |||
| 150 | if (this->GetNumberOfModules(node->GetId()) == boost::lexical_cast<unsigned int>(payload->GetVariables()["NumberOfModules"])) |
||
| 151 | { |
||
| 1627 | runge | 152 | node->Trigger(Node::EVENT_LIST_DONE, payload->GetVariables()); |
| 1598 | runge | 153 | } |
| 154 | |||
| 1657 | runge | 155 | LOG.Info("Module " + module->GetFullId() + " is available on node " + node->GetId() + "."); |
| 1602 | runge | 156 | this->signal_on_module_change_(module->GetFullId(), true); |
| 1598 | runge | 157 | } |
| 1657 | runge | 158 | else if (module->GetNodeId() != "") |
| 1602 | runge | 159 | { |
| 1619 | runge | 160 | node = this->GetNode(module->GetNodeId()); |
| 161 | node->ResetTimeout(); |
||
| 162 | |||
| 1602 | runge | 163 | this->signal_on_module_message_(module->GetFullId(), payload->GetCommandName(), payload->GetVariables()); |
| 164 | } |
||
| 1598 | runge | 165 | } |
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 1627 | runge | 169 | void Manager::SlotOnNewState(Node::Id node_id, Node::State current_state, Node::State target_state) |
| 170 | { |
||
| 171 | if (target_state != Node::STATE_NORM_INITIALIZED) |
||
| 172 | { |
||
| 173 | this->RemoveModules(node_id); |
||
| 174 | } |
||
| 1666 | runge | 175 | |
| 176 | if (current_state != target_state) |
||
| 1657 | runge | 177 | { |
| 1666 | runge | 178 | 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)); |
| 1657 | runge | 179 | } |
| 1633 | runge | 180 | |
| 181 | if (target_state == Node::STATE_BPGM_OFFLINE || target_state == Node::STATE_APGM_OFFLINE) |
||
| 182 | { |
||
| 183 | this->active_programming_node_id_ = node_id; |
||
| 184 | } |
||
| 185 | else if (target_state == Node::STATE_NORM_OFFLINE) |
||
| 186 | { |
||
| 1657 | runge | 187 | this->active_programming_node_id_ = ""; |
| 1633 | runge | 188 | } |
| 1627 | runge | 189 | } |
| 190 | |||
| 1642 | runge | 191 | void Manager::SendMessageHandler(std::string full_id, std::string command, common::StringMap variables) |
| 1602 | runge | 192 | { |
| 193 | ModuleList::iterator it = this->modules_.find(full_id); |
||
| 194 | |||
| 195 | if (it == this->modules_.end()) |
||
| 196 | { |
||
| 197 | LOG.Warning("Can not send message to " + full_id + ", it is not available"); |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | |||
| 201 | try |
||
| 202 | { |
||
| 1642 | runge | 203 | can::Message* payload = new can::Message(it->second->GetClassName(), "To_Owner", it->second->GetName(), it->second->GetId(), command); |
| 1602 | runge | 204 | payload->SetVariables(variables); |
| 205 | |||
| 206 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
||
| 1607 | runge | 207 | //LOG.Debug("Command " + command + " sent to " + it->second->GetName()); |
| 1602 | runge | 208 | } |
| 209 | catch (std::runtime_error& e) |
||
| 210 | { |
||
| 211 | LOG.Error("Failed to send message, " + std::string(e.what())); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 1642 | runge | 215 | void Manager::SendMessage(std::string full_id, std::string command, common::StringMap variables) |
| 1604 | runge | 216 | { |
| 217 | this->io_service_.post(boost::bind(&Manager::SendMessageHandler, this, full_id, command, variables)); |
||
| 218 | } |
||
| 219 | |||
| 1644 | runge | 220 | common::StringList Manager::GetAvailableModules() |
| 1604 | runge | 221 | { |
| 1644 | runge | 222 | common::StringList available_modules; |
| 223 | |||
| 224 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 225 | { |
||
| 226 | available_modules.push_back(it->first); |
||
| 227 | } |
||
| 228 | |||
| 229 | return available_modules; |
||
| 1604 | runge | 230 | } |
| 231 | |||
| 1657 | runge | 232 | common::StringList Manager::GetAvailableNodes() |
| 233 | { |
||
| 234 | common::StringList available_nodes; |
||
| 235 | |||
| 236 | for (NodeList::iterator it = this->nodes_.begin(); it != this->nodes_.end(); it++) |
||
| 237 | { |
||
| 238 | std::string node = it->first; |
||
| 239 | |||
| 240 | for (ModuleList::iterator it2 = this->modules_.begin(); it2 != this->modules_.end(); it2++) |
||
| 241 | { |
||
| 242 | if (it2->second->GetNodeId() == it->first) |
||
| 243 | { |
||
| 244 | node += "," + it2->first; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | available_nodes.push_back(node); |
||
| 249 | } |
||
| 250 | |||
| 251 | return available_nodes; |
||
| 252 | } |
||
| 253 | |||
| 254 | bool Manager::ResetNode(Node::Id node_id) |
||
| 255 | { |
||
| 256 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 257 | |||
| 258 | if (it == this->nodes_.end()) |
||
| 259 | { |
||
| 260 | LOG.Error("Could not find node " + node_id); |
||
| 261 | return false; |
||
| 262 | } |
||
| 263 | |||
| 264 | it->second->Reset(); |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | |||
| 1642 | runge | 268 | bool Manager::ProgramNode(Node::Id node_id, bool is_bios, std::string filename) |
| 269 | { |
||
| 270 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 271 | |||
| 272 | if (it == this->nodes_.end()) |
||
| 273 | { |
||
| 1657 | runge | 274 | LOG.Error("Could not find node " + node_id); |
| 1642 | runge | 275 | return false; |
| 276 | } |
||
| 277 | |||
| 278 | Code::Pointer code = Code::Pointer(new Code()); |
||
| 279 | if (!code->LoadIntelHexFile(filename)) |
||
| 280 | { |
||
| 281 | LOG.Error("Failed to parse " + filename); |
||
| 282 | return false; |
||
| 283 | } |
||
| 284 | |||
| 285 | if (is_bios) |
||
| 286 | { |
||
| 287 | LOG.Debug("is_bios true"); |
||
| 288 | it->second->ProgramBios(code); |
||
| 289 | } |
||
| 290 | else |
||
| 291 | { |
||
| 292 | LOG.Debug("is_bios false"); |
||
| 293 | it->second->ProgramApplication(code); |
||
| 294 | } |
||
| 295 | |||
| 296 | return true; |
||
| 297 | } |
||
| 298 | |||
| 1887 | arune | 299 | /* Program node with hexdata as argument */ |
| 300 | bool Manager::ProgramNodeHex(Node::Id node_id, bool is_bios, std::string hex_data) |
||
| 301 | { |
||
| 302 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 303 | |||
| 304 | if (it == this->nodes_.end()) |
||
| 305 | { |
||
| 306 | LOG.Error("Could not find node " + node_id); |
||
| 307 | return false; |
||
| 308 | } |
||
| 309 | |||
| 310 | Code::Pointer code = Code::Pointer(new Code()); |
||
| 311 | if (!code->ParseIntelHex(hex_data)) |
||
| 312 | { |
||
| 313 | LOG.Error("Failed to parse hexdata " + hex_data); |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | |||
| 317 | if (is_bios) |
||
| 318 | { |
||
| 319 | LOG.Debug("is_bios true"); |
||
| 320 | it->second->ProgramBios(code); |
||
| 321 | } |
||
| 322 | else |
||
| 323 | { |
||
| 324 | LOG.Debug("is_bios false"); |
||
| 325 | it->second->ProgramApplication(code); |
||
| 326 | } |
||
| 327 | |||
| 328 | return true; |
||
| 329 | } |
||
| 330 | |||
| 1665 | runge | 331 | Node::Information Manager::GetNodeInformation(Node::Id node_id) |
| 332 | { |
||
| 333 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 334 | |||
| 335 | if (it == this->nodes_.end()) |
||
| 336 | { |
||
| 337 | throw std::runtime_error("No such node exists, " + node_id); |
||
| 338 | } |
||
| 339 | |||
| 340 | return it->second->GetInformation(); |
||
| 341 | } |
||
| 342 | |||
| 1598 | runge | 343 | Node::Pointer Manager::GetNode(Node::Id node_id) |
| 344 | { |
||
| 345 | Node::Pointer node; |
||
| 346 | NodeList::iterator it = this->nodes_.find(node_id); |
||
| 347 | |||
| 348 | if (it == this->nodes_.end()) |
||
| 349 | { |
||
| 350 | node = Node::Pointer(new Node(node_id)); |
||
| 1627 | runge | 351 | node->ConnectSlots(Node::SignalOnNewState::slot_type(&Manager::SlotOnNewState, this, _1, _2, _3).track(this->tracker_)); |
| 1598 | runge | 352 | this->nodes_[node_id] = node; |
| 353 | } |
||
| 354 | else |
||
| 355 | { |
||
| 356 | node = it->second; |
||
| 357 | } |
||
| 358 | |||
| 359 | return node; |
||
| 360 | } |
||
| 361 | |||
| 362 | Module::Pointer Manager::GetModule(Module::Id module_id, std::string module_name, std::string class_name) |
||
| 363 | { |
||
| 364 | Module::Pointer module; |
||
| 365 | ModuleList::iterator it = this->modules_.find(Module::MakeFullId(module_id, module_name)); |
||
| 366 | |||
| 367 | if (it == this->modules_.end()) |
||
| 368 | { |
||
| 369 | module = Module::Pointer(new Module(module_id, module_name, class_name)); |
||
| 370 | this->modules_[module->GetFullId()] = module; |
||
| 371 | } |
||
| 372 | else |
||
| 373 | { |
||
| 374 | module = it->second; |
||
| 375 | } |
||
| 376 | |||
| 377 | return module; |
||
| 378 | } |
||
| 379 | |||
| 380 | void Manager::RemoveModules(Node::Id node_id) |
||
| 381 | { |
||
| 382 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 383 | { |
||
| 384 | if (it->second->GetNodeId() == node_id) |
||
| 385 | { |
||
| 386 | LOG.Info("Module " + it->second->GetFullId() + " is unavailable."); |
||
| 1602 | runge | 387 | this->signal_on_module_change_(it->second->GetFullId(), false); |
| 1598 | runge | 388 | |
| 389 | this->modules_.erase(it); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | unsigned int Manager::GetNumberOfModules(Node::Id node_id) |
||
| 395 | { |
||
| 396 | unsigned int number_of_modules = 0; |
||
| 397 | |||
| 398 | for (ModuleList::iterator it = this->modules_.begin(); it != this->modules_.end(); it++) |
||
| 399 | { |
||
| 400 | if (it->second->GetNodeId() == node_id) |
||
| 401 | { |
||
| 402 | number_of_modules++; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | return number_of_modules; |
||
| 407 | } |
||
| 408 | |||
| 1642 | runge | 409 | }; // namespace control |
| 1598 | runge | 410 | }; // namespace atom |