Rev 2090 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1595 | runge | 1 | /* |
| 2081 | runge | 2 | * |
| 1595 | runge | 3 | * Copyright (C) 2010 Mattias Runge |
| 2081 | runge | 4 | * |
| 1595 | runge | 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. |
||
| 2081 | runge | 9 | * |
| 1595 | runge | 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. |
||
| 2081 | runge | 14 | * |
| 1595 | runge | 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. |
||
| 2081 | runge | 18 | * |
| 1595 | runge | 19 | */ |
| 20 | |||
| 21 | #include "Network.h" |
||
| 22 | |||
| 23 | #include <vector> |
||
| 24 | |||
| 25 | #include <boost/algorithm/string.hpp> |
||
| 26 | #include <boost/lexical_cast.hpp> |
||
| 27 | |||
| 28 | #include "net/Manager.h" |
||
| 29 | |||
| 1596 | runge | 30 | #include "broker/Manager.h" |
| 31 | |||
| 32 | #include "Protocol.h" |
||
| 33 | #include "Message.h" |
||
| 34 | |||
| 1642 | runge | 35 | #include "common/Bitset.h" |
| 36 | #include "common/common.h" |
||
| 1987 | runge | 37 | #include "common/log.h" |
| 1596 | runge | 38 | |
| 1595 | runge | 39 | namespace atom { |
| 40 | namespace can { |
||
| 41 | |||
| 1596 | runge | 42 | enum |
| 1595 | runge | 43 | { |
| 1596 | runge | 44 | PACKET_START = 253, |
| 45 | PACKET_END = 250, |
||
| 46 | PACKET_PING = 251 |
||
| 47 | }; |
||
| 1987 | runge | 48 | |
| 49 | static const std::string log_module_ = "can::network"; |
||
| 50 | |||
| 51 | Network::Network(std::string address): broker::Subscriber(false), LOG("can::Network") |
||
| 1596 | runge | 52 | { |
| 1595 | runge | 53 | this->address_ = address; |
| 54 | this->client_id_ = 0; |
||
| 2081 | runge | 55 | |
| 1595 | runge | 56 | // Examples of address |
| 57 | // udp:192.168.1.250:1100 |
||
| 58 | // serial:/dev/ttyUSB0:38400 |
||
| 2081 | runge | 59 | |
| 1642 | runge | 60 | common::StringList parts; |
| 1595 | runge | 61 | boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off); |
| 2081 | runge | 62 | |
| 1595 | runge | 63 | if (parts.size() < 3) |
| 64 | { |
||
| 65 | LOG.Error("Malformed address string: " + address); |
||
| 66 | return; |
||
| 67 | } |
||
| 2081 | runge | 68 | |
| 1595 | runge | 69 | boost::algorithm::to_lower(parts[0]); |
| 2081 | runge | 70 | |
| 1595 | runge | 71 | if (parts[0] == "udp") |
| 72 | { |
||
| 1989 | runge | 73 | this->protocol_ = net::TRANSPORT_PROTOCOL_UDP; |
| 1595 | runge | 74 | } |
| 75 | else if (parts[0] == "serial") |
||
| 76 | { |
||
| 1989 | runge | 77 | this->protocol_ = net::TRANSPORT_PROTOCOL_SERIAL; |
| 1595 | runge | 78 | } |
| 79 | else |
||
| 80 | { |
||
| 81 | LOG.Error("Unknown protocol, only support udp and serial, got " + parts[0]); |
||
| 82 | return; |
||
| 83 | } |
||
| 2081 | runge | 84 | |
| 1595 | runge | 85 | this->address_ = parts[1]; |
| 86 | this->port_or_baud_ = boost::lexical_cast<unsigned int>(parts[2]); |
||
| 2081 | runge | 87 | |
| 1595 | runge | 88 | try |
| 89 | { |
||
| 90 | this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_); |
||
| 1596 | runge | 91 | LOG.Info("Connected to " + address); |
| 2081 | runge | 92 | |
| 1599 | runge | 93 | LOG.Info("Sending ping..."); |
| 94 | |||
| 1642 | runge | 95 | common::Byteset buffer(1); |
| 1596 | runge | 96 | buffer[0] = PACKET_PING; |
| 2081 | runge | 97 | |
| 1596 | runge | 98 | net::Manager::Instance()->SendTo(this->client_id_, buffer); |
| 1595 | runge | 99 | } |
| 1606 | runge | 100 | catch (std::runtime_error& e) |
| 1595 | runge | 101 | { |
| 102 | LOG.Error(e.what()); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | Network::~Network() |
||
| 107 | { |
||
| 108 | net::Manager::Instance()->Disconnect(this->client_id_); |
||
| 109 | } |
||
| 110 | |||
| 1596 | runge | 111 | void Network::SlotOnTimeout(timer::TimerId timer_id) |
| 112 | { |
||
| 113 | if (timer_id == this->timer_id_) |
||
| 114 | { |
||
| 115 | this->io_service_.post(boost::bind(&Network::SlotOnTimeoutHandler, this, timer_id)); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 1595 | runge | 119 | void Network::SlotOnMessageHandler(broker::Message::Pointer message) |
| 120 | { |
||
| 2216 | runge | 121 | if (message->GetType() == broker::Message::CAN_RAW_BYTES) |
| 1596 | runge | 122 | { |
| 2216 | runge | 123 | net::Manager::Instance()->SendTo(this->client_id_, message->GetRawData()); |
| 124 | } |
||
| 125 | else if (message->GetType() == broker::Message::CAN_MESSAGE) |
||
| 126 | { |
||
| 1596 | runge | 127 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
| 1642 | runge | 128 | common::Byteset data(17); |
| 2081 | runge | 129 | |
| 1598 | runge | 130 | data[0] = PACKET_START; |
| 2081 | runge | 131 | |
| 1598 | runge | 132 | unsigned int class_id = Protocol::Instance()->ResolveClassId(payload->GetClassName()); |
| 133 | data[4] = class_id << 1; |
||
| 134 | |||
| 135 | if (payload->GetClassName() == "nmt") |
||
| 136 | { |
||
| 137 | unsigned int command_id = Protocol::Instance()->ResolveNMTCommandId(payload->GetCommandName()); |
||
| 138 | data[3] = command_id; |
||
| 139 | } |
||
| 140 | else |
||
| 141 | { |
||
| 142 | unsigned int direction_flag = Protocol::Instance()->ResolveDirectionFlag(payload->GetDirectionName()); |
||
| 143 | data[4] |= (direction_flag & 0x01); |
||
| 2081 | runge | 144 | |
| 1598 | runge | 145 | unsigned int module_id = Protocol::Instance()->ResolveModuleId(payload->GetModuleName()); |
| 146 | data[3] = module_id; |
||
| 2081 | runge | 147 | |
| 1598 | runge | 148 | data[2] = payload->GetId(); |
| 2081 | runge | 149 | |
| 1598 | runge | 150 | unsigned int command_id = Protocol::Instance()->ResolveCommandId(payload->GetCommandName(), payload->GetModuleName()); |
| 151 | data[1] = command_id; |
||
| 152 | } |
||
| 2081 | runge | 153 | |
| 1598 | runge | 154 | unsigned int highest_bit = 0; |
| 1642 | runge | 155 | common::Bitset databits(64); |
| 2081 | runge | 156 | |
| 1598 | runge | 157 | xml::Node::NodeList variable_nodes; |
| 158 | unsigned int start_bit; |
||
| 159 | unsigned int bit_length; |
||
| 160 | std::string type; |
||
| 161 | std::string value; |
||
| 2081 | runge | 162 | |
| 1598 | runge | 163 | if (payload->GetClassName() == "nmt") |
| 164 | { |
||
| 165 | variable_nodes = Protocol::Instance()->GetNMTCommandVariables(payload->GetCommandName()); |
||
| 166 | } |
||
| 167 | else |
||
| 168 | { |
||
| 169 | variable_nodes = Protocol::Instance()->GetCommandVariables(payload->GetCommandName(), payload->GetModuleName()); |
||
| 170 | } |
||
| 2081 | runge | 171 | |
| 1598 | runge | 172 | for (unsigned int n = 0; n < variable_nodes.size(); n++) |
| 173 | { |
||
| 174 | value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name")); |
||
| 2081 | runge | 175 | //LOG.Debug(variable_nodes[n].GetAttributeValue("name") + "=" + value); |
| 1598 | runge | 176 | if (value == "") |
| 177 | { |
||
| 178 | continue; |
||
| 179 | } |
||
| 2081 | runge | 180 | |
| 1598 | runge | 181 | start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit")); |
| 182 | bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length")); |
||
| 183 | type = variable_nodes[n].GetAttributeValue("type"); |
||
| 2081 | runge | 184 | |
| 1598 | runge | 185 | if (type == "int") |
| 186 | { |
||
| 187 | Protocol::Instance()->EncodeInt(databits, start_bit, bit_length, value); |
||
| 188 | } |
||
| 189 | else if (type == "float") |
||
| 190 | { |
||
| 191 | Protocol::Instance()->EncodeFloat(databits, start_bit, bit_length, value); |
||
| 192 | } |
||
| 193 | else if (type == "ascii") |
||
| 194 | { |
||
| 195 | Protocol::Instance()->EncodeAscii(databits, start_bit, bit_length, value); |
||
| 1610 | runge | 196 | bit_length = value.length() * 8; |
| 1598 | runge | 197 | } |
| 198 | else if (type == "hexstring") |
||
| 199 | { |
||
| 200 | Protocol::Instance()->EncodeHexstring(databits, start_bit, bit_length, value); |
||
| 1610 | runge | 201 | bit_length = value.length() * 4; |
| 1598 | runge | 202 | } |
| 203 | else if (type == "enum") |
||
| 204 | { |
||
| 205 | value = variable_nodes[n].SelectChild("name", value).GetAttributeValue("id"); |
||
| 206 | Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value); |
||
| 207 | } |
||
| 208 | else// if (type == "uint") |
||
| 209 | { |
||
| 210 | Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value); |
||
| 211 | } |
||
| 1840 | linlun | 212 | |
| 213 | if (highest_bit < start_bit + bit_length) |
||
| 1610 | runge | 214 | { |
| 1840 | linlun | 215 | highest_bit = start_bit + bit_length; |
| 1610 | runge | 216 | } |
| 1598 | runge | 217 | } |
| 2081 | runge | 218 | |
| 1598 | runge | 219 | //LOG.Debug(databits.ToDebugString()); |
| 2081 | runge | 220 | |
| 1646 | runge | 221 | unsigned int length = std::min((int)ceil((float)highest_bit / 8.0f), 8); |
| 2081 | runge | 222 | |
| 1598 | runge | 223 | data[5] = 1; |
| 224 | data[6] = 0; |
||
| 2081 | runge | 225 | |
| 1598 | runge | 226 | data[7] = length; |
| 2081 | runge | 227 | |
| 1598 | runge | 228 | for (unsigned int n = 0; n < 8; n++) |
| 229 | { |
||
| 230 | data[8 + n] = databits.GetBytes()[n]; |
||
| 231 | } |
||
| 2081 | runge | 232 | |
| 1598 | runge | 233 | data[16] = PACKET_END; |
| 2081 | runge | 234 | |
| 1598 | runge | 235 | //LOG.Debug("Bytes: " + data.ToDebugString()); |
| 236 | net::Manager::Instance()->SendTo(this->client_id_, data); |
||
| 2081 | runge | 237 | } |
| 238 | else if (message->GetType() == broker::Message::CAN_RAW_MESSAGE) |
||
| 1917 | linlun | 239 | { |
| 2081 | runge | 240 | std::string* payload_str; |
| 1917 | linlun | 241 | payload_str = static_cast<std::string*>(message->GetPayload().get()); |
| 242 | std::string line = *payload_str; |
||
| 2081 | runge | 243 | |
| 2087 | runge | 244 | if (line.length() < 16) |
| 2081 | runge | 245 | { |
| 2090 | runge | 246 | log::Error(log_module_, "Packet was to short: \"%s\", length was %u.", line.data(), line.length()); |
| 2081 | runge | 247 | return; |
| 248 | } |
||
| 249 | |||
| 250 | |||
| 1917 | linlun | 251 | common::Byteset data(17); |
| 1919 | linlun | 252 | //LOG.Info("Got "+ line + "end"); |
| 2081 | runge | 253 | |
| 1917 | linlun | 254 | data[0] = PACKET_START; |
| 255 | std::string value = line.substr(4,2); |
||
| 1918 | linlun | 256 | //LOG.Info("<"+ value + ">"); |
| 1919 | linlun | 257 | data[4] = common::FromHex(value); |
| 1918 | linlun | 258 | //LOG.Info("id1: " + value + " data: ");//+ data[1]); |
| 2081 | runge | 259 | |
| 1917 | linlun | 260 | value = line.substr(6,2); |
| 1918 | linlun | 261 | //LOG.Info("<"+ value + ">"); |
| 2081 | runge | 262 | |
| 1919 | linlun | 263 | data[3] = common::FromHex(value); |
| 1918 | linlun | 264 | //LOG.Info("id2: " + value + " data: ");//+ data[2]); |
| 2081 | runge | 265 | |
| 1917 | linlun | 266 | value = line.substr(8,2); |
| 1919 | linlun | 267 | data[2] = common::FromHex(value); |
| 1918 | linlun | 268 | //LOG.Info("id3: " + value + " data: ");//+ data[3]); |
| 2081 | runge | 269 | |
| 1917 | linlun | 270 | value = line.substr(10,2); |
| 1919 | linlun | 271 | data[1] = common::FromHex(value); |
| 1918 | linlun | 272 | //LOG.Info("id4: " + value + " data: ");//+ data[4]); |
| 2081 | runge | 273 | |
| 1917 | linlun | 274 | value = line.substr(13,1); |
| 1918 | linlun | 275 | data[5] = common::FromHex(value); |
| 276 | //LOG.Info("1: " + value + " data: ");//+ data[5]); |
||
| 2081 | runge | 277 | |
| 1917 | linlun | 278 | value = line.substr(15,1); |
| 1918 | linlun | 279 | data[6] = common::FromHex(value); |
| 280 | //LOG.Info("1: " + value + " data: ");//+ data[6]); |
||
| 1917 | linlun | 281 | |
| 282 | unsigned char length = 0; |
||
| 1919 | linlun | 283 | unsigned char index = 0; |
| 1917 | linlun | 284 | while (length < 8 && index + 16 < (unsigned char)line.length()) |
| 285 | { |
||
| 1918 | linlun | 286 | value = line.substr(index+17,2); |
| 287 | data[8+length] = common::FromHex(value); |
||
| 288 | //LOG.Info("data: " + value + " data: ");//+ data[6]); |
||
| 1917 | linlun | 289 | index += 3; |
| 290 | length++; |
||
| 291 | } |
||
| 2081 | runge | 292 | |
| 1917 | linlun | 293 | data[7] = length; |
| 294 | data[16] = PACKET_END; |
||
| 1987 | runge | 295 | |
| 2081 | runge | 296 | |
| 1919 | linlun | 297 | //LOG.Info("Bytes: " + data.ToDebugString()); |
| 1917 | linlun | 298 | net::Manager::Instance()->SendTo(this->client_id_, data); |
| 1596 | runge | 299 | } |
| 1595 | runge | 300 | } |
| 301 | |||
| 1986 | runge | 302 | void Network::SlotOnNewDataHandler(net::SocketId client_id, common::Byteset data) |
| 1595 | runge | 303 | { |
| 1999 | runge | 304 | if (client_id != this->client_id_) |
| 305 | { |
||
| 306 | return; |
||
| 307 | } |
||
| 2081 | runge | 308 | |
| 2216 | runge | 309 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_BYTES, data, this))); |
| 310 | |||
| 1999 | runge | 311 | static bool have_start = false; |
| 2081 | runge | 312 | |
| 1999 | runge | 313 | for (unsigned int n = 0; n < data.size(); n++) |
| 314 | { |
||
| 2066 | runge | 315 | log::Extreme(log_module_, "data[%u] = %u, have_start = %s, this->buffer_.size() = %u", n, (unsigned int)data[n], have_start ? "true" : "false", this->buffer_.size()); |
| 2081 | runge | 316 | |
| 1999 | runge | 317 | if (have_start) |
| 1596 | runge | 318 | { |
| 2081 | runge | 319 | if (data[n] == PACKET_END && this->buffer_.size() >= 15) |
| 1999 | runge | 320 | { |
| 2066 | runge | 321 | log::Extreme(log_module_, "PACKET_END"); |
| 2081 | runge | 322 | |
| 1999 | runge | 323 | while (this->buffer_.size() < 15) |
| 1596 | runge | 324 | { |
| 1999 | runge | 325 | this->buffer_.push_back(0); |
| 1596 | runge | 326 | } |
| 2081 | runge | 327 | |
| 1999 | runge | 328 | for (unsigned int k = 0; k < this->buffer_.size(); k++) |
| 1596 | runge | 329 | { |
| 2066 | runge | 330 | log::Extreme(log_module_, "this->buffer_[%u]=%u", k, (unsigned int)this->buffer_[k]); |
| 1596 | runge | 331 | } |
| 2081 | runge | 332 | |
| 1999 | runge | 333 | this->ProcessBuffer(); |
| 2081 | runge | 334 | |
| 1999 | runge | 335 | have_start = false; |
| 336 | } |
||
| 337 | else |
||
| 338 | { |
||
| 339 | this->buffer_.push_back(data[n]); |
||
| 340 | } |
||
| 1596 | runge | 341 | } |
| 1999 | runge | 342 | else if (data[n] == PACKET_START) |
| 343 | { |
||
| 2066 | runge | 344 | log::Extreme(log_module_, "PACKET_START"); |
| 2081 | runge | 345 | |
| 1999 | runge | 346 | common::Byteset empty_vector; |
| 347 | this->buffer_.swap(empty_vector); |
||
| 348 | |||
| 349 | have_start = true; |
||
| 350 | } |
||
| 351 | else if (data[n] == PACKET_PING) |
||
| 352 | { |
||
| 353 | log::Info(log_module_, "Got Pong!"); |
||
| 354 | } |
||
| 355 | } |
||
| 1595 | runge | 356 | } |
| 357 | |||
| 1986 | runge | 358 | void Network::SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id) |
| 1595 | runge | 359 | { |
| 1986 | runge | 360 | |
| 361 | } |
||
| 362 | |||
| 363 | void Network::SlotOnNewStateHandler(net::SocketId client_id, net::ClientState client_state) |
||
| 364 | { |
||
| 1598 | runge | 365 | if (client_id != this->client_id_) |
| 366 | { |
||
| 367 | return; |
||
| 368 | } |
||
| 2081 | runge | 369 | |
| 1595 | runge | 370 | if (client_state == net::CLIENT_STATE_DISCONNECTED) |
| 371 | { |
||
| 1596 | runge | 372 | LOG.Warning("Got disconnected, setting reconnect timer..."); |
| 2081 | runge | 373 | |
| 1646 | runge | 374 | this->timer_id_ = timer::Manager::Instance()->SetTimer(10000, true); |
| 1595 | runge | 375 | this->client_id_ = 0; |
| 1596 | runge | 376 | } |
| 377 | else |
||
| 378 | { |
||
| 379 | LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state)); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | void Network::SlotOnTimeoutHandler(timer::TimerId timer_id) |
||
| 384 | { |
||
| 385 | try |
||
| 386 | { |
||
| 387 | this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_); |
||
| 388 | LOG.Info("Connected again."); |
||
| 2081 | runge | 389 | |
| 1596 | runge | 390 | timer::Manager::Instance()->Cancel(timer_id); |
| 391 | this->timer_id_ = 0; |
||
| 392 | } |
||
| 1606 | runge | 393 | catch (std::runtime_error& e) |
| 1596 | runge | 394 | { |
| 395 | LOG.Error(e.what()); |
||
| 396 | LOG.Warning("Will try again soon..."); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | void Network::ProcessBuffer() |
||
| 401 | { |
||
| 1987 | runge | 402 | LOG_DEBUG_ENTER; |
| 2081 | runge | 403 | |
| 1596 | runge | 404 | try |
| 405 | { |
||
| 406 | std::string class_name = ""; |
||
| 407 | std::string direction_name = ""; |
||
| 408 | std::string module_name = ""; |
||
| 409 | unsigned int id = 0; |
||
| 410 | std::string command_name = ""; |
||
| 2081 | runge | 411 | |
| 412 | std::string PKTstring = "PKT " + |
||
| 413 | atom::common::ToHex8bit((unsigned int)this->buffer_[3]) + |
||
| 1916 | runge | 414 | atom::common::ToHex8bit((unsigned int)this->buffer_[2]) + |
| 2081 | runge | 415 | atom::common::ToHex8bit((unsigned int)this->buffer_[1]) + |
| 1916 | runge | 416 | atom::common::ToHex8bit((unsigned int)this->buffer_[0]) + |
| 417 | " " + |
||
| 2081 | runge | 418 | atom::common::ToHex4bit((unsigned int)this->buffer_[4]) + |
| 1916 | runge | 419 | " " + |
| 420 | atom::common::ToHex4bit((unsigned int)this->buffer_[5]); |
||
| 2081 | runge | 421 | |
| 1916 | runge | 422 | for (unsigned int index = 7; index < 7 + (unsigned int)this->buffer_[6]; index++) |
| 423 | { |
||
| 424 | PKTstring += " " + atom::common::ToHex8bit((unsigned int)this->buffer_[index]) ; |
||
| 1914 | linlun | 425 | } |
| 2081 | runge | 426 | |
| 1914 | linlun | 427 | PKTstring += "\n"; |
| 2081 | runge | 428 | |
| 2066 | runge | 429 | LOG.Extreme(PKTstring); |
| 2081 | runge | 430 | |
| 1990 | runge | 431 | for (unsigned int n = 0; n < this->buffer_.size(); n++) |
| 432 | { |
||
| 2066 | runge | 433 | log::Extreme(log_module_, "this->buffer_[%u]=%u", n, (unsigned int)this->buffer_[n]); |
| 1990 | runge | 434 | } |
| 2081 | runge | 435 | |
| 1914 | linlun | 436 | std::string* payload_str = new std::string(PKTstring); |
| 437 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_MESSAGE, broker::Message::PayloadPointer(payload_str), this))); |
||
| 1916 | runge | 438 | |
| 2081 | runge | 439 | |
| 1596 | runge | 440 | unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F; |
| 2066 | runge | 441 | LOG.Extreme("class_id=" + boost::lexical_cast<std::string>(class_id)); |
| 1596 | runge | 442 | class_name = Protocol::Instance()->LookupClassName(class_id); |
| 2081 | runge | 443 | |
| 1596 | runge | 444 | if (class_name == "nmt") |
| 1595 | runge | 445 | { |
| 1596 | runge | 446 | unsigned int command_id = this->buffer_[2]; |
| 2066 | runge | 447 | LOG.Extreme("command_id=" + boost::lexical_cast<std::string>(command_id)); |
| 1596 | runge | 448 | command_name = Protocol::Instance()->LookupNMTCommandName(command_id); |
| 1595 | runge | 449 | } |
| 1596 | runge | 450 | else |
| 1595 | runge | 451 | { |
| 1596 | runge | 452 | unsigned int direction_flag = this->buffer_[3] & 0x01; |
| 2066 | runge | 453 | LOG.Extreme("direction_flag=" + boost::lexical_cast<std::string>(direction_flag)); |
| 1596 | runge | 454 | direction_name = Protocol::Instance()->LookupDirectionFlag(direction_flag); |
| 2081 | runge | 455 | |
| 1596 | runge | 456 | unsigned int module_id = this->buffer_[2]; |
| 2066 | runge | 457 | LOG.Extreme("module_id=" + boost::lexical_cast<std::string>(module_id)); |
| 1596 | runge | 458 | module_name = Protocol::Instance()->LookupModuleName(module_id); |
| 2066 | runge | 459 | LOG.Extreme("module_name=" + module_name); |
| 1596 | runge | 460 | id = this->buffer_[1]; |
| 2081 | runge | 461 | |
| 1596 | runge | 462 | unsigned int command_id = this->buffer_[0]; |
| 2066 | runge | 463 | LOG.Extreme("command_id=" + boost::lexical_cast<std::string>(command_id)); |
| 1596 | runge | 464 | command_name = Protocol::Instance()->LookupCommandName(command_id, module_name); |
| 2066 | runge | 465 | LOG.Extreme("command_name=" + command_name); |
| 1595 | runge | 466 | } |
| 1596 | runge | 467 | |
| 468 | Message* payload = new Message(class_name, direction_name, module_name, id, command_name); |
||
| 469 | |||
| 470 | unsigned int length = this->buffer_[6]; |
||
| 2081 | runge | 471 | |
| 1990 | runge | 472 | common::Byteset data_set; |
| 473 | data_set.reserve(length); |
||
| 2081 | runge | 474 | |
| 1596 | runge | 475 | for (unsigned int n = 0; n < length; n++) |
| 476 | { |
||
| 1987 | runge | 477 | data_set.push_back(this->buffer_[n + 7]); |
| 1596 | runge | 478 | } |
| 2081 | runge | 479 | |
| 1990 | runge | 480 | for (unsigned int n = 0; n < data_set.size(); n++) |
| 481 | { |
||
| 2066 | runge | 482 | log::Extreme(log_module_, "data_set[%u]=%u", n, (unsigned int)data_set[n]); |
| 1990 | runge | 483 | } |
| 1598 | runge | 484 | |
| 1642 | runge | 485 | common::Bitset databits(data_set); |
| 1596 | runge | 486 | xml::Node::NodeList variable_nodes; |
| 1598 | runge | 487 | unsigned int start_bit; |
| 1607 | runge | 488 | int bit_length; |
| 1598 | runge | 489 | std::string type; |
| 490 | std::string value; |
||
| 1607 | runge | 491 | std::string name; |
| 2081 | runge | 492 | |
| 1596 | runge | 493 | if (class_name == "nmt") |
| 494 | { |
||
| 495 | variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name); |
||
| 496 | } |
||
| 497 | else |
||
| 498 | { |
||
| 499 | variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name); |
||
| 500 | } |
||
| 2081 | runge | 501 | |
| 1596 | runge | 502 | for (unsigned int n = 0; n < variable_nodes.size(); n++) |
| 503 | { |
||
| 1607 | runge | 504 | name = variable_nodes[n].GetAttributeValue("name"); |
| 1598 | runge | 505 | start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit")); |
| 506 | bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length")); |
||
| 2081 | runge | 507 | |
| 1607 | runge | 508 | if (databits.GetCount() < start_bit + bit_length) |
| 509 | { |
||
| 510 | bit_length = databits.GetCount() - start_bit; |
||
| 2081 | runge | 511 | |
| 1607 | runge | 512 | if (bit_length <= 0) |
| 513 | { |
||
| 1610 | runge | 514 | LOG.Warning("Can not read variable " + name + " for command " + command_name + ", message is to short, is there a match between the module and the protocol XML file?"); |
| 1990 | runge | 515 | LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length) + ", databits.GetCount()=" + boost::lexical_cast<std::string>(databits.GetCount())); |
| 1607 | runge | 516 | continue; |
| 517 | } |
||
| 518 | } |
||
| 2081 | runge | 519 | |
| 1598 | runge | 520 | type = variable_nodes[n].GetAttributeValue("type"); |
| 2066 | runge | 521 | LOG.Extreme("type: type=" + type); |
| 2081 | runge | 522 | |
| 1597 | runge | 523 | if (type == "int") |
| 1596 | runge | 524 | { |
| 1598 | runge | 525 | value = Protocol::Instance()->DecodeInt(databits, start_bit, bit_length); |
| 1597 | runge | 526 | } |
| 527 | else if (type == "float") |
||
| 528 | { |
||
| 1598 | runge | 529 | value = Protocol::Instance()->DecodeFloat(databits, start_bit, bit_length); |
| 1597 | runge | 530 | } |
| 531 | else if (type == "ascii") |
||
| 532 | { |
||
| 1598 | runge | 533 | value = Protocol::Instance()->DecodeAscii(databits, start_bit, bit_length); |
| 1597 | runge | 534 | } |
| 535 | else if (type == "hexstring") |
||
| 536 | { |
||
| 1598 | runge | 537 | value = Protocol::Instance()->DecodeHexstring(databits, start_bit, bit_length); |
| 1597 | runge | 538 | } |
| 539 | else if (type == "enum") |
||
| 540 | { |
||
| 2066 | runge | 541 | LOG.Extreme("Enum: command name=" + command_name); |
| 1598 | runge | 542 | value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length); |
| 2066 | runge | 543 | LOG.Extreme("Enum: value=" + boost::lexical_cast<std::string>(value)); |
| 544 | LOG.Extreme("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length)); |
||
| 2081 | runge | 545 | |
| 1596 | runge | 546 | value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name"); |
| 547 | } |
||
| 1597 | runge | 548 | else// if (type == "uint") |
| 549 | { |
||
| 1598 | runge | 550 | value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length); |
| 1597 | runge | 551 | } |
| 2081 | runge | 552 | |
| 2066 | runge | 553 | LOG.Extreme("value=\"" + value + "\""); |
| 2081 | runge | 554 | |
| 1607 | runge | 555 | payload->SetVariable(name, value); |
| 1596 | runge | 556 | } |
| 2081 | runge | 557 | |
| 1596 | runge | 558 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 1595 | runge | 559 | } |
| 1596 | runge | 560 | catch (std::runtime_error& e) |
| 1595 | runge | 561 | { |
| 1596 | runge | 562 | LOG.Error("Malformed message received, " + std::string(e.what())); |
| 1987 | runge | 563 | LOG.Debug("Bytes: " + std::string(this->buffer_.begin(), this->buffer_.end())); |
| 1595 | runge | 564 | } |
| 2081 | runge | 565 | |
| 1987 | runge | 566 | common::Byteset empty_vector; |
| 567 | this->buffer_.swap(empty_vector); |
||
| 2081 | runge | 568 | |
| 569 | |||
| 1987 | runge | 570 | LOG_DEBUG_EXIT; |
| 1595 | runge | 571 | } |
| 2081 | runge | 572 | |
| 1595 | runge | 573 | }; // namespace can |
| 574 | }; // namespace atom |