Rev 1597 | Rev 1599 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1597 | Rev 1598 | ||
|---|---|---|---|
| Line 43... | Line 43... | ||
| 43 | PACKET_START = 253, |
43 | PACKET_START = 253, |
| 44 | PACKET_END = 250, |
44 | PACKET_END = 250, |
| 45 | PACKET_PING = 251 |
45 | PACKET_PING = 251 |
| 46 | }; |
46 | }; |
| 47 | 47 | ||
| 48 | Network::Network(std::string address): Subscriber(false), LOG("can::Network"), buffer_(2048) |
48 | Network::Network(std::string address): broker::Subscriber(false), LOG("can::Network"), buffer_(2048) |
| 49 | { |
49 | { |
| 50 | this->address_ = address; |
50 | this->address_ = address; |
| 51 | this->client_id_ = 0; |
51 | this->client_id_ = 0; |
| 52 | - | ||
| 53 | net::Manager::Instance()->ConnectSlots(net::Client::SignalOnNewState::slot_type(&Network::SlotOnNewState, this, _1, _2, _3).track(this->tracker_), |
- | |
| 54 | net::Client::SignalOnNewData::slot_type(&Network::SlotOnNewData, this, _1, _2, _3).track(this->tracker_)); |
- | |
| 55 | 52 | ||
| 56 | // Examples of address |
53 | // Examples of address |
| 57 | // udp:192.168.1.250:1100 |
54 | // udp:192.168.1.250:1100 |
| 58 | // serial:/dev/ttyUSB0:38400 |
55 | // serial:/dev/ttyUSB0:38400 |
| 59 | 56 | ||
| 60 | std::vector<std::string> parts; |
57 | std::vector<std::string> parts; |
| 61 | boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off); |
58 | boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off); |
| 62 | 59 | ||
| 63 | if (parts.size() < 3) |
60 | if (parts.size() < 3) |
| 64 | { |
61 | { |
| 65 | LOG.Error("Malformed address string: " + address); |
62 | LOG.Error("Malformed address string: " + address); |
| 66 | return; |
63 | return; |
| 67 | } |
64 | } |
| 68 | 65 | ||
| 69 | boost::algorithm::to_lower(parts[0]); |
66 | boost::algorithm::to_lower(parts[0]); |
| 70 | 67 | ||
| 71 | if (parts[0] == "udp") |
68 | if (parts[0] == "udp") |
| 72 | { |
69 | { |
| 73 | this->protocol_ = net::PROTOCOL_UDP; |
70 | this->protocol_ = net::PROTOCOL_UDP; |
| 74 | } |
71 | } |
| 75 | else if (parts[0] == "serial") |
72 | else if (parts[0] == "serial") |
| 76 | { |
73 | { |
| 77 | this->protocol_ = net::PROTOCOL_SERIAL; |
74 | this->protocol_ = net::PROTOCOL_SERIAL; |
| 78 | } |
75 | } |
| 79 | else |
76 | else |
| 80 | { |
77 | { |
| Line 82... | Line 79... | ||
| 82 | return; |
79 | return; |
| 83 | } |
80 | } |
| 84 | 81 | ||
| 85 | this->address_ = parts[1]; |
82 | this->address_ = parts[1]; |
| 86 | this->port_or_baud_ = boost::lexical_cast<unsigned int>(parts[2]); |
83 | this->port_or_baud_ = boost::lexical_cast<unsigned int>(parts[2]); |
| 87 | 84 | ||
| 88 | try |
85 | try |
| 89 | { |
86 | { |
| 90 | this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_); |
87 | this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_); |
| 91 | LOG.Info("Connected to " + address); |
88 | LOG.Info("Connected to " + address); |
| 92 | 89 | ||
| 93 | LOG.Info("Sending ping."); |
90 | LOG.Info("Sending ping."); |
| 94 | type::Byteset buffer(1); |
91 | type::Byteset buffer(1); |
| 95 | 92 | ||
| 96 | buffer[0] = PACKET_PING; |
93 | buffer[0] = PACKET_PING; |
| 97 | 94 | ||
| 98 | net::Manager::Instance()->SendTo(this->client_id_, buffer); |
95 | net::Manager::Instance()->SendTo(this->client_id_, buffer); |
| 99 | } |
96 | } |
| 100 | catch (std::exception e) |
97 | catch (std::exception e) |
| 101 | { |
98 | { |
| 102 | LOG.Error(e.what()); |
99 | LOG.Error(e.what()); |
| 103 | } |
100 | } |
| 104 | } |
101 | } |
| 105 | 102 | ||
| 106 | Network::~Network() |
103 | Network::~Network() |
| 107 | { |
104 | { |
| 108 | net::Manager::Instance()->Disconnect(this->client_id_); |
105 | net::Manager::Instance()->Disconnect(this->client_id_); |
| 109 | } |
- | |
| 110 | - | ||
| 111 | void Network::SlotOnNewData(net::ClientId client_id, net::ServerId server_id, type::Byteset data) |
- | |
| 112 | { |
- | |
| 113 | if (client_id == this->client_id_) |
- | |
| 114 | { |
- | |
| 115 | type::Byteset temp_buffer = data; |
- | |
| 116 | this->io_service_.post(boost::bind(&Network::SlotOnNewDataHandler, this, client_id, server_id, temp_buffer)); |
- | |
| 117 | } |
- | |
| 118 | } |
- | |
| 119 | - | ||
| 120 | void Network::SlotOnNewState(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
- | |
| 121 | { |
- | |
| 122 | if (client_id == this->client_id_) |
- | |
| 123 | { |
- | |
| 124 | this->io_service_.post(boost::bind(&Network::SlotOnNewStateHandler, this, client_id, server_id, client_state)); |
- | |
| 125 | } |
- | |
| 126 | } |
106 | } |
| 127 | 107 | ||
| 128 | void Network::SlotOnTimeout(timer::TimerId timer_id) |
108 | void Network::SlotOnTimeout(timer::TimerId timer_id) |
| 129 | { |
109 | { |
| 130 | if (timer_id == this->timer_id_) |
110 | if (timer_id == this->timer_id_) |
| Line 136... | Line 116... | ||
| 136 | void Network::SlotOnMessageHandler(broker::Message::Pointer message) |
116 | void Network::SlotOnMessageHandler(broker::Message::Pointer message) |
| 137 | { |
117 | { |
| 138 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
118 | if (message->GetType() == broker::Message::CAN_MESSAGE) |
| 139 | { |
119 | { |
| 140 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
120 | Message* payload = static_cast<Message*>(message->GetPayload().get()); |
| - | 121 | type::Byteset data(17); |
|
| 141 | 122 | ||
| - | 123 | data[0] = PACKET_START; |
|
| 142 | 124 | ||
| - | 125 | unsigned int class_id = Protocol::Instance()->ResolveClassId(payload->GetClassName()); |
|
| - | 126 | data[4] = class_id << 1; |
|
| 143 | 127 | ||
| - | 128 | if (payload->GetClassName() == "nmt") |
|
| - | 129 | { |
|
| - | 130 | unsigned int command_id = Protocol::Instance()->ResolveNMTCommandId(payload->GetCommandName()); |
|
| - | 131 | data[3] = command_id; |
|
| - | 132 | } |
|
| - | 133 | else |
|
| - | 134 | { |
|
| - | 135 | unsigned int direction_flag = Protocol::Instance()->ResolveDirectionFlag(payload->GetDirectionName()); |
|
| - | 136 | data[4] |= (direction_flag & 0x01); |
|
| - | 137 | ||
| - | 138 | unsigned int module_id = Protocol::Instance()->ResolveModuleId(payload->GetModuleName()); |
|
| - | 139 | data[3] = module_id; |
|
| - | 140 | ||
| - | 141 | data[2] = payload->GetId(); |
|
| - | 142 | ||
| - | 143 | unsigned int command_id = Protocol::Instance()->ResolveCommandId(payload->GetCommandName(), payload->GetModuleName()); |
|
| - | 144 | data[1] = command_id; |
|
| - | 145 | } |
|
| - | 146 | ||
| - | 147 | unsigned int highest_bit = 0; |
|
| - | 148 | type::Bitset databits(64); |
|
| - | 149 | ||
| - | 150 | xml::Node::NodeList variable_nodes; |
|
| - | 151 | unsigned int start_bit; |
|
| - | 152 | unsigned int bit_length; |
|
| - | 153 | std::string type; |
|
| - | 154 | std::string value; |
|
| - | 155 | ||
| - | 156 | if (payload->GetClassName() == "nmt") |
|
| - | 157 | { |
|
| - | 158 | variable_nodes = Protocol::Instance()->GetNMTCommandVariables(payload->GetCommandName()); |
|
| - | 159 | } |
|
| - | 160 | else |
|
| - | 161 | { |
|
| - | 162 | variable_nodes = Protocol::Instance()->GetCommandVariables(payload->GetCommandName(), payload->GetModuleName()); |
|
| - | 163 | } |
|
| - | 164 | ||
| - | 165 | for (unsigned int n = 0; n < variable_nodes.size(); n++) |
|
| - | 166 | { |
|
| - | 167 | value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name")); |
|
| - | 168 | ||
| - | 169 | if (value == "") |
|
| - | 170 | { |
|
| - | 171 | continue; |
|
| - | 172 | } |
|
| - | 173 | ||
| - | 174 | start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit")); |
|
| - | 175 | bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length")); |
|
| - | 176 | type = variable_nodes[n].GetAttributeValue("type"); |
|
| - | 177 | ||
| - | 178 | if (highest_bit < start_bit + bit_length - 1) |
|
| - | 179 | { |
|
| - | 180 | highest_bit = start_bit + bit_length - 1; |
|
| - | 181 | } |
|
| - | 182 | ||
| - | 183 | if (type == "int") |
|
| - | 184 | { |
|
| - | 185 | Protocol::Instance()->EncodeInt(databits, start_bit, bit_length, value); |
|
| - | 186 | } |
|
| - | 187 | else if (type == "float") |
|
| - | 188 | { |
|
| - | 189 | Protocol::Instance()->EncodeFloat(databits, start_bit, bit_length, value); |
|
| - | 190 | } |
|
| - | 191 | else if (type == "ascii") |
|
| - | 192 | { |
|
| - | 193 | Protocol::Instance()->EncodeAscii(databits, start_bit, bit_length, value); |
|
| - | 194 | } |
|
| - | 195 | else if (type == "hexstring") |
|
| - | 196 | { |
|
| - | 197 | Protocol::Instance()->EncodeHexstring(databits, start_bit, bit_length, value); |
|
| - | 198 | } |
|
| - | 199 | else if (type == "enum") |
|
| - | 200 | { |
|
| - | 201 | value = variable_nodes[n].SelectChild("name", value).GetAttributeValue("id"); |
|
| - | 202 | Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value); |
|
| - | 203 | } |
|
| - | 204 | else// if (type == "uint") |
|
| - | 205 | { |
|
| - | 206 | Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value); |
|
| - | 207 | } |
|
| - | 208 | } |
|
| - | 209 | ||
| - | 210 | //LOG.Debug(databits.ToDebugString()); |
|
| - | 211 | ||
| - | 212 | unsigned int length = ceil((float)highest_bit / 8.0f); |
|
| - | 213 | ||
| - | 214 | data[5] = 1; |
|
| - | 215 | data[6] = 0; |
|
| - | 216 | ||
| - | 217 | data[7] = length; |
|
| - | 218 | ||
| - | 219 | for (unsigned int n = 0; n < 8; n++) |
|
| - | 220 | { |
|
| - | 221 | data[8 + n] = databits.GetBytes()[n]; |
|
| - | 222 | } |
|
| - | 223 | ||
| - | 224 | data[16] = PACKET_END; |
|
| - | 225 | ||
| - | 226 | data.SetSize(17); |
|
| - | 227 | ||
| - | 228 | //LOG.Debug("Bytes: " + data.ToDebugString()); |
|
| - | 229 | net::Manager::Instance()->SendTo(this->client_id_, data); |
|
| - | 230 | } |
|
| 144 | } |
231 | } |
| 145 | } |
- | |
| 146 | 232 | ||
| 147 | void Network::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data) |
233 | void Network::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, type::Byteset data) |
| 148 | { |
234 | { |
| - | 235 | if (client_id != this->client_id_) |
|
| - | 236 | { |
|
| - | 237 | return; |
|
| - | 238 | } |
|
| - | 239 | ||
| 149 | static bool have_start = false; |
240 | static bool have_start = false; |
| 150 | 241 | ||
| 151 | for (unsigned int n = 0; n < data.GetSize(); n++) |
242 | for (unsigned int n = 0; n < data.GetSize(); n++) |
| 152 | { |
243 | { |
| 153 | if (have_start) |
244 | if (have_start) |
| 154 | { |
245 | { |
| 155 | if (data[n] == PACKET_END && this->buffer_.GetSize() == 15) |
246 | if (data[n] == PACKET_END && this->buffer_.GetSize() == 15) |
| 156 | { |
247 | { |
| 157 | //LOG.Debug("Received packet end and size is 15."); |
- | |
| 158 | this->ProcessBuffer(); |
248 | this->ProcessBuffer(); |
| 159 | have_start = false; |
249 | have_start = false; |
| 160 | } |
250 | } |
| 161 | else |
251 | else |
| 162 | { |
252 | { |
| 163 | this->buffer_.Append(data[n]); |
253 | this->buffer_.Append(data[n]); |
| 164 | } |
254 | } |
| 165 | } |
255 | } |
| 166 | else if (data[n] == PACKET_START) |
256 | else if (data[n] == PACKET_START) |
| 167 | { |
257 | { |
| 168 | //LOG.Debug("Received packet start."); |
- | |
| 169 | this->buffer_.Clear(); |
258 | this->buffer_.Clear(); |
| 170 | have_start = true; |
259 | have_start = true; |
| 171 | } |
260 | } |
| 172 | else if (data[n] == PACKET_PING) |
261 | else if (data[n] == PACKET_PING) |
| 173 | { |
262 | { |
| Line 176... | Line 265... | ||
| 176 | } |
265 | } |
| 177 | } |
266 | } |
| 178 | 267 | ||
| 179 | void Network::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
268 | void Network::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state) |
| 180 | { |
269 | { |
| - | 270 | if (client_id != this->client_id_) |
|
| - | 271 | { |
|
| - | 272 | return; |
|
| - | 273 | } |
|
| - | 274 | ||
| 181 | if (client_state == net::CLIENT_STATE_DISCONNECTED) |
275 | if (client_state == net::CLIENT_STATE_DISCONNECTED) |
| 182 | { |
276 | { |
| 183 | LOG.Warning("Got disconnected, setting reconnect timer..."); |
277 | LOG.Warning("Got disconnected, setting reconnect timer..."); |
| 184 | 278 | ||
| 185 | this->timer_id_ = timer::Manager::Instance()->Set(10000, true); |
279 | this->timer_id_ = timer::Manager::Instance()->Set(10000, true); |
| Line 217... | Line 311... | ||
| 217 | std::string module_name = ""; |
311 | std::string module_name = ""; |
| 218 | unsigned int id = 0; |
312 | unsigned int id = 0; |
| 219 | std::string command_name = ""; |
313 | std::string command_name = ""; |
| 220 | 314 | ||
| 221 | unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F; |
315 | unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F; |
| 222 | - | ||
| 223 | //LOG.Debug("class_id=" + boost::lexical_cast<std::string>(class_id) + ", byte[3] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[3])); |
- | |
| 224 | - | ||
| 225 | class_name = Protocol::Instance()->LookupClassName(class_id); |
316 | class_name = Protocol::Instance()->LookupClassName(class_id); |
| 226 | 317 | ||
| 227 | //LOG.Debug("class_name=" + class_name); |
- | |
| 228 | - | ||
| 229 | if (class_name == "nmt") |
318 | if (class_name == "nmt") |
| 230 | { |
319 | { |
| 231 | unsigned int command_id = this->buffer_[2]; |
320 | unsigned int command_id = this->buffer_[2]; |
| 232 | //LOG.Debug("command_id=" + boost::lexical_cast<std::string>(command_id) + ", byte[2] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[2])); |
- | |
| 233 | - | ||
| 234 | command_name = Protocol::Instance()->LookupNMTCommandName(command_id); |
321 | command_name = Protocol::Instance()->LookupNMTCommandName(command_id); |
| 235 | //LOG.Debug("command_name=" + command_name); |
- | |
| 236 | } |
322 | } |
| 237 | else |
323 | else |
| 238 | { |
324 | { |
| 239 | unsigned int direction_flag = this->buffer_[3] & 0x01; |
325 | unsigned int direction_flag = this->buffer_[3] & 0x01; |
| 240 | //LOG.Debug("direction_flag=" + boost::lexical_cast<std::string>(direction_flag) + ", byte[3] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[3])); |
- | |
| 241 | - | ||
| 242 | direction_name = Protocol::Instance()->LookupDirectionFlag(direction_flag); |
326 | direction_name = Protocol::Instance()->LookupDirectionFlag(direction_flag); |
| 243 | //LOG.Debug("direction_name=" + direction_name); |
- | |
| 244 | 327 | ||
| 245 | unsigned int module_id = this->buffer_[2]; |
328 | unsigned int module_id = this->buffer_[2]; |
| 246 | //LOG.Debug("module_id=" + boost::lexical_cast<std::string>(module_id) + ", byte[2] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[2])); |
- | |
| 247 | - | ||
| 248 | module_name = Protocol::Instance()->LookupModuleName(module_id); |
329 | module_name = Protocol::Instance()->LookupModuleName(module_id); |
| 249 | //LOG.Debug("module_name=" + module_name); |
- | |
| 250 | 330 | ||
| 251 | id = this->buffer_[1]; |
331 | id = this->buffer_[1]; |
| 252 | //LOG.Debug("id=" + boost::lexical_cast<std::string>(id) + ", byte[1] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[1])); |
- | |
| 253 | 332 | ||
| 254 | unsigned int command_id = this->buffer_[0]; |
333 | unsigned int command_id = this->buffer_[0]; |
| 255 | //LOG.Debug("command_id=" + boost::lexical_cast<std::string>(command_id) + ", byte[0] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[0])); |
- | |
| 256 | - | ||
| 257 | command_name = Protocol::Instance()->LookupCommandName(command_id, module_name); |
334 | command_name = Protocol::Instance()->LookupCommandName(command_id, module_name); |
| 258 | //LOG.Debug("command_name=" + command_name); |
- | |
| 259 | } |
335 | } |
| 260 | 336 | ||
| 261 | Message* payload = new Message(class_name, direction_name, module_name, id, command_name); |
337 | Message* payload = new Message(class_name, direction_name, module_name, id, command_name); |
| 262 | 338 | ||
| 263 | unsigned int length = this->buffer_[6]; |
339 | unsigned int length = this->buffer_[6]; |
| 264 | //LOG.Debug("Data length = " + boost::lexical_cast<std::string>(length)); |
- | |
| 265 | 340 | ||
| 266 | type::Byteset data_set(length); |
341 | type::Byteset data_set(length); |
| 267 | 342 | ||
| 268 | for (unsigned int n = 0; n < length; n++) |
343 | for (unsigned int n = 0; n < length; n++) |
| 269 | { |
344 | { |
| 270 | //LOG.Debug("add byte[" + boost::lexical_cast<std::string>(n + 7) + "] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[n + 7])); |
- | |
| 271 | data_set.Append(this->buffer_[n + 7]); |
345 | data_set.Append(this->buffer_[n + 7]); |
| 272 | } |
- | |
| 273 | /* |
- | |
| 274 | for (unsigned int n = 0; n < data_set.GetSize(); n++) |
- | |
| 275 | { |
- | |
| 276 | LOG.Debug("byte[" + boost::lexical_cast<std::string>(n) + "] = " + boost::lexical_cast<std::string>((unsigned int)data_set[n])); |
- | |
| 277 | } |
- | |
| 278 | */ |
- | |
| 279 | type::Bitset databits(data_set); |
- | |
| 280 | - | ||
| 281 | /* |
- | |
| 282 | std::string temp = ""; |
- | |
| 283 | std::string temp2 = ""; |
- | |
| 284 | for (unsigned int n = 0; n < databits.GetCount(); n ++) |
- | |
| 285 | { |
- | |
| 286 | |
- | |
| 287 | temp2 += boost::lexical_cast<std::string>((unsigned int)databits.bytes_[n/8]); |
- | |
| 288 | temp += boost::lexical_cast<std::string>(databits.Get(n)); |
- | |
| 289 | } |
346 | } |
| 290 | |
347 | |
| 291 |
|
348 | type::Bitset databits(data_set); |
| 292 |
|
349 | xml::Node::NodeList variable_nodes; |
| - | 350 | unsigned int start_bit; |
|
| - | 351 | unsigned int bit_length; |
|
| 293 | 352 | std::string type; |
|
| 294 |
|
353 | std::string value; |
| 295 | 354 | ||
| 296 | if (class_name == "nmt") |
355 | if (class_name == "nmt") |
| 297 | { |
356 | { |
| 298 | variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name); |
357 | variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name); |
| 299 | } |
358 | } |
| 300 | else |
359 | else |
| 301 | { |
360 | { |
| 302 | variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name); |
361 | variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name); |
| 303 | } |
362 | } |
| 304 | 363 | ||
| 305 | for (unsigned int n = 0; n < variable_nodes.size(); n++) |
364 | for (unsigned int n = 0; n < variable_nodes.size(); n++) |
| 306 | { |
365 | { |
| 307 |
|
366 | start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit")); |
| 308 |
|
367 | bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length")); |
| 309 |
|
368 | type = variable_nodes[n].GetAttributeValue("type"); |
| 310 | - | ||
| 311 | //LOG.Debug("name=" + variable_nodes[n].GetAttributeValue("name")); |
- | |
| 312 | //LOG.Debug("type=" + type); |
- | |
| 313 | - | ||
| 314 | std::string value = ""; |
- | |
| 315 | 369 | ||
| 316 | if (type == "int") |
370 | if (type == "int") |
| 317 | { |
371 | { |
| 318 |
|
372 | value = Protocol::Instance()->DecodeInt(databits, start_bit, bit_length); |
| 319 | int raw_value = 0; |
- | |
| 320 | - | ||
| 321 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
- | |
| 322 | value = boost::lexical_cast<std::string>(raw_value); |
- | |
| 323 | } |
373 | } |
| 324 | else if (type == "float") |
374 | else if (type == "float") |
| 325 | { |
375 | { |
| 326 | float raw_value = 0; |
- | |
| 327 | bool negative = false;; |
- | |
| 328 | - | ||
| 329 |
|
376 | value = Protocol::Instance()->DecodeFloat(databits, start_bit, bit_length); |
| 330 |
|
377 | } |
| 331 |
|
378 | else if (type == "ascii") |
| 332 | } |
- | |
| 333 | - | ||
| 334 | for (int c = bit_length-1; c > 0; c--) |
- | |
| 335 | { |
379 | { |
| 336 | if (databits.Get(start_bit + c) != negative) |
- | |
| 337 | { |
- | |
| 338 |
|
380 | value = Protocol::Instance()->DecodeAscii(databits, start_bit, bit_length); |
| 339 | } |
- | |
| 340 | } |
381 | } |
| 341 | - | ||
| 342 | raw_value /= 64.0f; |
- | |
| 343 | - | ||
| 344 |
|
382 | else if (type == "hexstring") |
| 345 | { |
383 | { |
| 346 | raw_value = -raw_value; |
- | |
| 347 | } |
- | |
| 348 | - | ||
| 349 | value = boost::lexical_cast<std::string>(raw_value); |
- | |
| 350 | //LOG.Debug("float:value="+value); |
- | |
| 351 | } |
- | |
| 352 | else if (type == "ascii") |
- | |
| 353 | { |
- | |
| 354 | char c = 0; |
- | |
| 355 | - | ||
| 356 | for (unsigned int p = 0; p < bit_length; p += 8) |
- | |
| 357 | { |
- | |
| 358 |
|
384 | value = Protocol::Instance()->DecodeHexstring(databits, start_bit, bit_length); |
| 359 | memcpy(&c, &raw_bit_value, sizeof(c)); |
- | |
| 360 | value += c; |
- | |
| 361 | } |
- | |
| 362 | } |
- | |
| 363 | else if (type == "hexstring") |
- | |
| 364 | { |
- | |
| 365 | for (unsigned int p = 0; p < bit_length; p += 4) |
- | |
| 366 | { |
- | |
| 367 | unsigned long raw_bit_value = databits.Read(start_bit + p, 4); |
- | |
| 368 | - | ||
| 369 | //value += boost::lexical_cast<std::string>(raw_bit_value); |
- | |
| 370 | } |
385 | } |
| 371 | } |
- | |
| 372 | else if (type == "enum") |
386 | else if (type == "enum") |
| 373 | { |
387 | { |
| 374 | value = |
388 | value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length); |
| 375 | value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name"); |
389 | value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name"); |
| 376 | } |
390 | } |
| 377 | else// if (type == "uint") |
391 | else// if (type == "uint") |
| 378 | { |
392 | { |
| 379 |
|
393 | value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length); |
| 380 | unsigned int raw_value = 0; |
- | |
| 381 | - | ||
| 382 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
- | |
| 383 | value = boost::lexical_cast<std::string>(raw_value); |
- | |
| 384 | } |
394 | } |
| 385 | - | ||
| 386 | //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ",bit_length=" + boost::lexical_cast<std::string>(bit_length)); |
- | |
| 387 | 395 | ||
| 388 | payload->SetVariable(variable_nodes[n].GetAttributeValue("name"), value); |
396 | payload->SetVariable(variable_nodes[n].GetAttributeValue("name"), value); |
| 389 | //LOG.Debug("Variable:" + variable_nodes[n].GetAttributeValue("name") + " = " + value); |
- | |
| 390 | } |
397 | } |
| 391 | 398 | ||
| 392 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
399 | broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this))); |
| 393 | } |
400 | } |
| 394 | catch (std::runtime_error& e) |
401 | catch (std::runtime_error& e) |
| 395 | { |
402 | { |
| 396 | std::string bytestring; |
- | |
| 397 | - | ||
| 398 | for (unsigned int n = 0; n < this->buffer_.GetSize(); n++) |
- | |
| 399 | { |
- | |
| 400 | bytestring += boost::lexical_cast<std::string>((unsigned int)this->buffer_[n]) + ","; |
- | |
| 401 | } |
- | |
| 402 | - | ||
| 403 | LOG.Debug("Bytes: " + bytestring); |
- | |
| 404 | - | ||
| 405 | LOG.Error("Malformed message received, " + std::string(e.what())); |
403 | LOG.Error("Malformed message received, " + std::string(e.what())); |
| - | 404 | LOG.Debug("Bytes: " + this->buffer_.ToDebugString()); |
|
| 406 | } |
405 | } |
| 407 | 406 | ||
| 408 | this->buffer_.Clear(); |
407 | this->buffer_.Clear(); |
| 409 | } |
408 | } |
| 410 | 409 | ||