Rev 1596 | Rev 1599 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1596 | Rev 1598 | ||
|---|---|---|---|
| Line 19... | Line 19... | ||
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "Protocol.h" |
21 | #include "Protocol.h" |
| 22 | 22 | ||
| 23 | #include <stdexcept> |
23 | #include <stdexcept> |
| - | 24 | #include <string.h> |
|
| 24 | 25 | ||
| 25 | #include <boost/lexical_cast.hpp> |
26 | #include <boost/lexical_cast.hpp> |
| 26 | 27 | ||
| 27 | namespace atom { |
28 | namespace atom { |
| 28 | namespace can { |
29 | namespace can { |
| Line 57... | Line 58... | ||
| 57 | { |
58 | { |
| 58 | LOG.Error(e.what()); |
59 | LOG.Error(e.what()); |
| 59 | return false; |
60 | return false; |
| 60 | } |
61 | } |
| 61 | 62 | ||
| 62 | LOG.Info("Protocol loaded successfully"); |
63 | LOG.Info("Protocol loaded successfully."); |
| 63 | return true; |
64 | return true; |
| 64 | } |
65 | } |
| 65 | 66 | ||
| 66 | std::string Protocol::LookupClassName(unsigned int class_id) |
67 | std::string Protocol::LookupClassName(unsigned int class_id) |
| 67 | { |
68 | { |
| Line 193... | Line 194... | ||
| 193 | } |
194 | } |
| 194 | } |
195 | } |
| 195 | 196 | ||
| 196 | std::string Protocol::LookupModuleName(unsigned int module_id) |
197 | std::string Protocol::LookupModuleName(unsigned int module_id) |
| 197 | { |
198 | { |
| - | 199 | if (module_id == 0) |
|
| - | 200 | { |
|
| - | 201 | return ""; |
|
| - | 202 | } |
|
| - | 203 | ||
| 198 | try |
204 | try |
| 199 | { |
205 | { |
| 200 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
206 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
| 201 | } |
207 | } |
| 202 | catch (std::runtime_error& e) |
208 | catch (std::runtime_error& e) |
| 203 | { |
209 | { |
| 204 | LOG.Error(e.what()); |
210 | LOG.Error(e.what()); |
| 205 | throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id)); |
211 | throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id)); |
| 206 | } |
212 | } |
| 207 | } |
213 | } |
| 208 | 214 | ||
| 209 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
215 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
| 210 | { |
216 | { |
| - | 217 | if (module_name == "") |
|
| - | 218 | { |
|
| - | 219 | return 0; |
|
| - | 220 | } |
|
| - | 221 | ||
| 211 | try |
222 | try |
| 212 | { |
223 | { |
| 213 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
224 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
| 214 | } |
225 | } |
| 215 | catch (std::runtime_error& e) |
226 | catch (std::runtime_error& e) |
| 216 | { |
227 | { |
| 217 | LOG.Error(e.what()); |
228 | LOG.Error(e.what()); |
| 218 | throw std::runtime_error("No such module name found, " + module_name); |
229 | throw std::runtime_error("No such module name found, " + module_name); |
| 219 | } |
230 | } |
| 220 | } |
231 | } |
| 221 | 232 | ||
| 222 | xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name) |
233 | xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name) |
| 223 | { |
234 | { |
| 224 | try |
235 | try |
| 225 | { |
236 | { |
| 226 | return this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).FindChild("variables").GetChildren(); |
237 | return this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).FindChild("variables").GetChildren(); |
| 227 | } |
238 | } |
| 228 | catch (std::runtime_error& e) |
239 | catch (std::runtime_error& e) |
| 229 | { |
240 | { |
| 230 | } |
241 | } |
| 231 | 242 | ||
| 232 | try |
243 | try |
| 233 | { |
244 | { |
| 234 | return this->root_node_.FindChild("commands").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
245 | return this->root_node_.FindChild("commands").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
| 235 | } |
246 | } |
| 236 | catch (std::runtime_error& e) |
247 | catch (std::runtime_error& e) |
| 237 | { |
248 | { |
| 238 | LOG.Error(e.what()); |
249 | LOG.Error(e.what()); |
| 239 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
250 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
| 240 | } |
251 | } |
| 241 | } |
252 | } |
| 242 | 253 | ||
| 243 | xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name) |
254 | xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name) |
| 244 | { |
255 | { |
| 245 | try |
256 | try |
| 246 | { |
257 | { |
| 247 | return this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
258 | return this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
| 248 | } |
259 | } |
| 249 | catch (std::runtime_error& e) |
260 | catch (std::runtime_error& e) |
| 250 | { |
261 | { |
| 251 | LOG.Error(e.what()); |
262 | LOG.Error(e.what()); |
| 252 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
263 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
| 253 | } |
264 | } |
| 254 | } |
265 | } |
| 255 | 266 | ||
| 256 | 267 | std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
|
| 257 | 268 | { |
|
| 258 | 269 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
|
| 259 | 270 | int raw_value = 0; |
|
| 260 | 271 | ||
| 261 | 272 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
|
| 262 | 273 | ||
| 263 | 274 | return boost::lexical_cast<std::string>(raw_value); |
|
| 264 | 275 | } |
|
| 265 | 276 | ||
| 266 | 277 | void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
|
| 267 |
|
278 | { |
| 268 | 279 | unsigned long raw_bit_value = 0; |
|
| 269 | 280 | int raw_value = boost::lexical_cast<int>(value); |
|
| 270 | 281 | ||
| 271 | 282 | memcpy(&raw_bit_value, &raw_value, sizeof(raw_value)); |
|
| 272 | 283 | ||
| 273 |
|
284 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 274 |
|
285 | } |
| 275 | std::vector<Node> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
286 | |
| 276 | 287 | std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
|
| 277 |
|
288 | { |
| 278 |
|
289 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 279 |
|
290 | |
| 280 | 291 | return boost::lexical_cast<std::string>((unsigned int)raw_bit_value); |
|
| 281 |
|
292 | } |
| 282 | { |
293 | |
| 283 |
|
294 | void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 284 | 295 | { |
|
| 285 |
|
296 | unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value); |
| 286 | |
297 | |
| 287 |
|
298 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 288 | } |
299 | } |
| 289 | } |
300 | |
| 290 |
|
301 | std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 291 | 302 | { |
|
| 292 |
|
303 | float raw_value = 0; |
| 293 |
|
304 | bool negative = false;; |
| 294 |
|
305 | |
| 295 | 306 | if (bitset.Get(start_bit)) |
|
| 296 |
|
307 | { |
| 297 |
|
308 | negative = true; |
| 298 | map<std::string, std::string> attributes = commandNodes[n].getAttributes(); |
- | |
| 299 | |
- | |
| 300 | bool correct = false; |
- | |
| 301 | |
- | |
| 302 | if (commandId >= 128) |
- | |
| 303 | { |
- | |
| 304 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
- | |
| 305 | { |
- | |
| 306 | correct = true; |
- | |
| 307 | } |
- | |
| 308 | } |
- | |
| 309 | else if (stoi(attributes["id"]) == commandId) |
- | |
| 310 | { |
- | |
| 311 | correct = true; |
- | |
| 312 | } |
- | |
| 313 | |
- | |
| 314 | if (correct) |
- | |
| 315 | { |
- | |
| 316 | Node varablesNode = commandNodes[n].findChild("variables"); |
- | |
| 317 | |
- | |
| 318 | makeDataValid(varablesNode.getChildren(), data); |
- | |
| 319 | |
- | |
| 320 | return; |
- | |
| 321 | } |
- | |
| 322 | } |
- | |
| 323 | } |
- | |
| 324 | - | ||
| 325 | void Protocol::makeDataValid(std::vector<Node> variableNodes, map<std::string, CanVariable> &data) |
- | |
| 326 | { |
- | |
| 327 | ///FIXME: Remove variables that do not exist in the xmlfile |
- | |
| 328 | for (int c = 0; c < variableNodes.size(); c++) |
- | |
| 329 | { |
- | |
| 330 | map<std::string, std::string> attributes = variableNodes[c].getAttributes(); |
- | |
| 331 | |
- | |
| 332 | if (data.find(attributes["name"]) != data.end()) |
- | |
| 333 | { |
- | |
| 334 | data[attributes["name"]].setType(attributes["type"]); |
- | |
| 335 | data[attributes["name"]].setUnit(attributes["unit"]); |
- | |
| 336 | data[attributes["name"]].setBitLength(stoi(attributes["bit_length"])); |
- | |
| 337 | data[attributes["name"]].setStartBit(stoi(attributes["start_bit"])); |
- | |
| 338 | |
- | |
| 339 | if (attributes["type"] == "enum") |
- | |
| 340 | { |
- | |
| 341 | std::vector<Node> values = variableNodes[c].getChildren(); |
- | |
| 342 | |
- | |
| 343 | for (int k = 0; k < values.size(); k++) |
- | |
| 344 | { |
- | |
| 345 | map<std::string, std::string> valueAttributes = values[k].getAttributes(); |
- | |
| 346 | data[attributes["name"]].addEnumValue(valueAttributes["id"], valueAttributes["name"]); |
- | |
| 347 | } |
- | |
| 348 | } |
- | |
| 349 | } |
- | |
| 350 | } |
- | |
| 351 | } |
- | |
| 352 | - | ||
| 353 | std::string Protocol::translateDataToHex(int commandId, std::string moduleName, map<std::string, CanVariable> &data) |
- | |
| 354 | { |
- | |
| 355 | makeDataValid(commandId, moduleName, data); |
- | |
| 356 | |
- | |
| 357 | return translateValidDataToHex(data); |
- | |
| 358 | } |
- | |
| 359 | - | ||
| 360 | - | ||
| 361 | map<std::string, CanVariable> Protocol::translateData(int commandId, std::string moduleName, std::string dataHex) |
- | |
| 362 | { |
- | |
| 363 | std::vector<Node> commandNodes = xmlNode.findChild("commands").getChildren(); |
- | |
| 364 | |
- | |
| 365 | for (int n = 0; n < commandNodes.size(); n++) |
- | |
| 366 | { |
- | |
| 367 | map<std::string, std::string> attributes = commandNodes[n].getAttributes(); |
- | |
| 368 | |
- | |
| 369 | bool correct = false; |
- | |
| 370 | |
- | |
| 371 | if (commandId >= 128) |
- | |
| 372 | { |
- | |
| 373 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
- | |
| 374 | { |
- | |
| 375 | correct = true; |
- | |
| 376 | } |
- | |
| 377 | } |
- | |
| 378 | else if (stoi(attributes["id"]) == commandId) |
- | |
| 379 | { |
- | |
| 380 | correct = true; |
- | |
| 381 | } |
- | |
| 382 | |
- | |
| 383 | if (correct) |
- | |
| 384 | { |
- | |
| 385 | Node varablesNode = commandNodes[n].findChild("variables"); |
- | |
| 386 | |
- | |
| 387 | return translateData(varablesNode.getChildren(), dataHex); |
- | |
| 388 | } |
- | |
| 389 | } |
- | |
| 390 | |
- | |
| 391 | map<std::string, CanVariable> variables; |
- | |
| 392 | return variables; |
- | |
| 393 | } |
- | |
| 394 | - | ||
| 395 | map<std::string, CanVariable> Protocol::translateNMTData(int commandId, std::string dataHex) |
- | |
| 396 | { |
- | |
| 397 | std::vector<Node> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
- | |
| 398 | |
- | |
| 399 | for (int n = 0; n < commandNodes.size(); n++) |
- | |
| 400 | { |
- | |
| 401 | map<std::string, std::string> attributes = commandNodes[n].getAttributes(); |
- | |
| 402 | |
- | |
| 403 | if (stoi(attributes["id"]) == commandId) |
- | |
| 404 | { |
- | |
| 405 | Node varablesNode = commandNodes[n].findChild("variables"); |
- | |
| 406 | |
- | |
| 407 | return translateData(varablesNode.getChildren(), dataHex); |
- | |
| 408 | } |
- | |
| 409 | } |
- | |
| 410 | |
- | |
| 411 | map<std::string, CanVariable> variables; |
- | |
| 412 | return variables; |
- | |
| 413 | } |
- | |
| 414 | - | ||
| 415 | std::string Protocol::translateNMTDataToHex(std::string commandName, map<std::string, CanVariable> &data) |
- | |
| 416 | { |
- | |
| 417 | makeNMTDataValid(commandName, data); |
- | |
| 418 | |
- | |
| 419 | return translateValidDataToHex(data); |
- | |
| 420 | } |
- | |
| 421 | - | ||
| 422 | - | ||
| 423 | template <class T> |
- | |
| 424 | bool from_std::string(T& t, |
- | |
| 425 | const std::std::string& s, |
- | |
| 426 | std::ios_base& (*f)(std::ios_base&)) |
- | |
| 427 | { |
- | |
| 428 | std::istd::stringstream iss(s); |
- | |
| 429 | return !(iss >> f >> t).fail(); |
- | |
| 430 | } |
- | |
| 431 | - | ||
| 432 | - | ||
| 433 | std::string Protocol::translateValidDataToHex(map<std::string, CanVariable> &data) |
- | |
| 434 | { |
- | |
| 435 | std::string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
- | |
| 436 | int highestBit = 0; |
- | |
| 437 | |
- | |
| 438 | for (map<std::string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++) |
- | |
| 439 | { |
- | |
| 440 | if (iter->second.getType() == "uint") |
- | |
| 441 | { |
- | |
| 442 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
- | |
| 443 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
- | |
| 444 | |
- | |
| 445 | unsigned int a = stou(iter->second.getValue()); |
- | |
| 446 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
- | |
| 447 | } |
- | |
| 448 | else if (iter->second.getType() == "int") |
- | |
| 449 | { |
- | |
| 450 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
- | |
| 451 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
- | |
| 452 | |
- | |
| 453 | unsigned int a = stou(iter->second.getValue()); |
- | |
| 454 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
- | |
| 455 | - | ||
| 456 | } |
- | |
| 457 | else if (iter->second.getType() == "float") |
- | |
| 458 | { |
- | |
| 459 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
- | |
| 460 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
- | |
| 461 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
- | |
| 462 | |
- | |
| 463 | } |
- | |
| 464 | else if (iter->second.getType() == "enum") |
- | |
| 465 | { |
- | |
| 466 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
- | |
| 467 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
- | |
| 468 | |
- | |
| 469 | std::string value = iter->second.getEnumIdValue(); |
- | |
| 470 | |
- | |
| 471 | if (value == "") |
- | |
| 472 | { |
- | |
| 473 | throw new CanMessageException("Got enum that could not be converterd to a value. value = \"" + iter->second.getValue() + "\" enumString is \"" + iter->second.getEnumsString() + "\"\n"); |
- | |
| 474 | } |
- | |
| 475 | else |
- | |
| 476 | { |
- | |
| 477 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(stou(value), iter->second.getBitLength())); |
- | |
| 478 | } |
- | |
| 479 | } |
- | |
| 480 | else if (iter->second.getType() == "ascii") |
- | |
| 481 | { |
- | |
| 482 | for (int n = 0; n < iter->second.getValue().length(); n++) |
- | |
| 483 | { |
- | |
| 484 | if (iter->second.getStartBit()+n*8 + 8 > highestBit) |
- | |
| 485 | highestBit = iter->second.getStartBit()+n*8 + 8; |
- | |
| 486 | |
- | |
| 487 | //bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
- | |
| 488 | unsigned int temp = iter->second.getValue()[n]; |
- | |
| 489 | if (temp > 0xff) |
- | |
| 490 | { |
- | |
| 491 | temp &= 0xff; |
- | |
| 492 | } |
- | |
| 493 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin(temp, 8)); |
- | |
| 494 | } |
- | |
| 495 | } |
- | |
| 496 | else if (iter->second.getType() == "hexstd::string") |
- | |
| 497 | { |
- | |
| 498 | for (int n = 0; n < iter->second.getValue().length(); n++) |
- | |
| 499 | { |
- | |
| 500 | if (iter->second.getStartBit()+n*4 + 4 > highestBit) |
- | |
| 501 | highestBit = iter->second.getStartBit()+n*4 + 4; |
- | |
| 502 | |
- | |
| 503 | std::string character; |
- | |
| 504 | character += iter->second.getValue()[n]; |
- | |
| 505 | |
- | |
| 506 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
- | |
| 507 | } |
- | |
| 508 | } |
- | |
| 509 | } |
- | |
| 510 | |
- | |
| 511 | try |
- | |
| 512 | { |
- | |
| 513 | while (highestBit%8 != 0) |
- | |
| 514 | { |
- | |
| 515 | highestBit++; |
- | |
| 516 | } |
- | |
| 517 | bin = bin.substr(0, highestBit); |
- | |
| 518 | } |
- | |
| 519 | catch (std::out_of_range& e) |
- | |
| 520 | { |
- | |
| 521 | cout << "DEBUG: translateValidDataToHex exception: " << e.what() << "\n"; |
- | |
| 522 | cout << "DEBUG: A bin=" << bin << " :: highestBit=" << highestBit << endl; |
- | |
| 523 | } |
- | |
| 524 | |
- | |
| 525 | return bin2hex(bin); |
- | |
| 526 | } |
- | |
| 527 | - | ||
| 528 | map<std::string, CanVariable> Protocol::translateData(std::vector<Node> variableNodes, std::string dataHex) |
- | |
| 529 | { |
- | |
| 530 | map<std::string, CanVariable> variables; |
- | |
| 531 | std::string dataBin = hex2bin(dataHex); |
- | |
| 532 | //dataBin = rpad(dataBin, 64, '0'); |
- | |
| 533 | |
- | |
| 534 | for (int c = 0; c < variableNodes.size(); c++) |
- | |
| 535 | { |
- | |
| 536 | map<std::string, std::string> attributes = variableNodes[c].getAttributes(); |
- | |
| 537 | |
- | |
| 538 | int bitStart = stoi(attributes["start_bit"]); |
- | |
| 539 | int bitLength = stoi(attributes["bit_length"]); |
- | |
| 540 | std::string bits; |
- | |
| 541 | |
- | |
| 542 | try |
- | |
| 543 | { |
- | |
| 544 | bits = dataBin.substr(bitStart, bitLength); |
- | |
| 545 | } |
- | |
| 546 | catch (std::out_of_range& e) |
- | |
| 547 | { |
- | |
| 548 | return variables; |
- | |
| 549 | } |
- | |
| 550 | |
- | |
| 551 | CanVariable variable; |
- | |
| 552 | |
- | |
| 553 | variable.setName(attributes["name"]); |
- | |
| 554 | variable.setType(attributes["type"]); |
- | |
| 555 | variable.setUnit(attributes["unit"]); |
- | |
| 556 | variable.setStartBit(bitStart); |
- | |
| 557 | variable.setBitLength(bitLength); |
- | |
| 558 | |
- | |
| 559 | if (attributes["type"] == "uint") |
- | |
| 560 | { |
- | |
| 561 | variable.setValue(utos(bin2uint(bits))); |
- | |
| 562 | } |
- | |
| 563 | else if (attributes["type"] == "int") |
- | |
| 564 | { |
- | |
| 565 | variable.setValue(itos(bin2int(bits))); |
- | |
| 566 | } |
- | |
| 567 | else if (attributes["type"] == "float") |
- | |
| 568 | { |
- | |
| 569 | variable.setValue(ftos(bin2float(bits))); |
- | |
| 570 | } |
- | |
| 571 | else if (attributes["type"] == "enum") |
- | |
| 572 | { |
- | |
| 573 | std::vector<Node> values = variableNodes[c].getChildren(); |
- | |
| 574 | |
- | |
| 575 | std::string value = utos(bin2uint(bits)); |
- | |
| 576 | |
- | |
| 577 | for (int k = 0; k < values.size(); k++) |
- | |
| 578 | { |
- | |
| 579 | map<std::string, std::string> valueAttributes = values[k].getAttributes(); |
- | |
| 580 | variable.addEnumValue(valueAttributes["id"], valueAttributes["name"]); |
- | |
| 581 | |
- | |
| 582 | if (valueAttributes["id"] == value) |
- | |
| 583 | { |
- | |
| 584 | variable.setValue(valueAttributes["name"]); |
- | |
| 585 | } |
- | |
| 586 | } |
- | |
| 587 | } |
- | |
| 588 | else if (attributes["type"] == "ascii") |
- | |
| 589 | { |
- | |
| 590 | std::string str; |
- | |
| 591 | |
- | |
| 592 | for (int k = 0; k < bits.length(); k += 8) |
- | |
| 593 | { |
- | |
| 594 | try |
- | |
| 595 | { |
- | |
| 596 | str += (char)bin2uint(bits.substr(k, 8)); |
- | |
| 597 | } |
- | |
| 598 | catch (std::out_of_range& e) |
- | |
| 599 | { |
- | |
| 600 | cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; |
- | |
| 601 | cout << "DEBUG: B bits=" << bits << " :: k=" << k << endl; |
- | |
| 602 | continue; |
- | |
| 603 | } |
- | |
| 604 | } |
309 | } |
| 605 | |
310 | |
| - | 311 | for (int c = bit_length-1; c > 0; c--) |
|
| - | 312 | { |
|
| - | 313 | if (bitset.Get(start_bit + c) != negative) |
|
| - | 314 | { |
|
| 606 |
|
315 | raw_value += pow(2.0f, (int)(bit_length-1-c)); |
| 607 | } |
316 | } |
| 608 | else if (attributes["type"] == "hexstd::string") |
- | |
| 609 |
|
317 | } |
| - | 318 | ||
| 610 |
|
319 | raw_value /= 64.0f; |
| 611 | |
320 | |
| 612 |
|
321 | if (negative) |
| 613 | { |
322 | { |
| - | 323 | raw_value = -raw_value; |
|
| - | 324 | } |
|
| - | 325 | ||
| - | 326 | return boost::lexical_cast<std::string>(raw_value); |
|
| - | 327 | } |
|
| - | 328 | ||
| - | 329 | void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
|
| - | 330 | { |
|
| - | 331 | // TODO: |
|
| - | 332 | } |
|
| - | 333 | ||
| - | 334 | std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
|
| - | 335 | { |
|
| - | 336 | std::string value; |
|
| - | 337 | char c = 0; |
|
| - | 338 | ||
| - | 339 | for (unsigned int p = 0; p < bit_length; p += 8) |
|
| - | 340 | { |
|
| - | 341 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 8); |
|
| - | 342 | memcpy(&c, &raw_bit_value, sizeof(c)); |
|
| 614 |
|
343 | value += c; |
| - | 344 | } |
|
| - | 345 | ||
| - | 346 | return value; |
|
| - | 347 | } |
|
| - | 348 | ||
| - | 349 | void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
|
| 615 | { |
350 | { |
| 616 | str += bin2hex(bits.substr(k, 4)); |
- | |
| 617 |
|
351 | // TODO |
| - | 352 | } |
|
| - | 353 | ||
| 618 |
|
354 | std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 619 | { |
355 | { |
| - | 356 | std::string value; |
|
| 620 |
|
357 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
| - | 358 | ||
| - | 359 | for (unsigned int p = 0; p < bit_length; p += 4) |
|
| - | 360 | { |
|
| 621 |
|
361 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 4); |
| - | 362 | ||
| - | 363 | if (raw_bit_value >= sizeof(hex)) |
|
| - | 364 | { |
|
| 622 |
|
365 | value += '-'; |
| - | 366 | } |
|
| - | 367 | else |
|
| - | 368 | { |
|
| - | 369 | value += hex[raw_bit_value]; |
|
| 623 | } |
370 | } |
| 624 | } |
371 | } |
| 625 | |
372 | |
| 626 | variable.setValue(str); |
- | |
| 627 | } |
- | |
| 628 | |
- | |
| 629 | variables[attributes["name"]] = variable; |
- | |
| 630 | } |
- | |
| 631 | |
- | |
| 632 |
|
373 | return value; |
| 633 | } |
374 | } |
| 634 | |
375 | |
| - | 376 | void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
|
| 635 | 377 | { |
|
| 636 |
|
378 | // TODO: |
| 637 | - | ||
| 638 | - | ||
| 639 | - | ||
| 640 | - | ||
| 641 | - | ||
| 642 | - | ||
| 643 | - | ||
| 644 | 379 | } |
|
| 645 | 380 | ||
| 646 | }; // namespace can |
381 | }; // namespace can |
| 647 | }; // namespace atom |
382 | }; // namespace atom |