Rev 1596 | Rev 1599 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1596 | 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 "Protocol.h" |
||
| 22 | |||
| 23 | #include <stdexcept> |
||
| 1598 | runge | 24 | #include <string.h> |
| 1596 | runge | 25 | |
| 26 | #include <boost/lexical_cast.hpp> |
||
| 27 | |||
| 28 | namespace atom { |
||
| 29 | namespace can { |
||
| 30 | |||
| 31 | Protocol::Pointer Protocol::instance_ = Protocol::Pointer(new Protocol()); |
||
| 32 | |||
| 33 | Protocol::Protocol() : LOG("can::Protocol") |
||
| 34 | { |
||
| 35 | } |
||
| 36 | |||
| 37 | Protocol::~Protocol() |
||
| 38 | { |
||
| 39 | } |
||
| 40 | |||
| 41 | Protocol::Pointer Protocol::Instance() |
||
| 42 | { |
||
| 43 | return Protocol::instance_; |
||
| 44 | } |
||
| 45 | |||
| 46 | void Protocol::Delete() |
||
| 47 | { |
||
| 48 | Protocol::instance_.reset(); |
||
| 49 | } |
||
| 50 | |||
| 51 | bool Protocol::Load(std::string filename) |
||
| 52 | { |
||
| 53 | try |
||
| 54 | { |
||
| 55 | this->root_node_.LoadFile(filename); |
||
| 56 | } |
||
| 57 | catch (std::runtime_error& e) |
||
| 58 | { |
||
| 59 | LOG.Error(e.what()); |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | |||
| 1598 | runge | 63 | LOG.Info("Protocol loaded successfully."); |
| 1596 | runge | 64 | return true; |
| 65 | } |
||
| 66 | |||
| 67 | std::string Protocol::LookupClassName(unsigned int class_id) |
||
| 68 | { |
||
| 69 | try |
||
| 70 | { |
||
| 71 | return this->root_node_.FindChild("classes").SelectChild("id", boost::lexical_cast<std::string>(class_id)).GetAttributeValue("name"); |
||
| 72 | } |
||
| 73 | catch (std::runtime_error& e) |
||
| 74 | { |
||
| 75 | LOG.Error(e.what()); |
||
| 76 | throw std::runtime_error("No such class_id found, " + boost::lexical_cast<std::string>(class_id)); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | unsigned int Protocol::ResolveClassId(std::string class_name) |
||
| 81 | { |
||
| 82 | try |
||
| 83 | { |
||
| 84 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("classes").SelectChild("name", class_name).GetAttributeValue("id")); |
||
| 85 | } |
||
| 86 | catch (std::runtime_error& e) |
||
| 87 | { |
||
| 88 | LOG.Error(e.what()); |
||
| 89 | throw std::runtime_error("No such class_name found, " + class_name); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | std::string Protocol::LookupCommandName(unsigned int command_id, std::string module_name) |
||
| 94 | { |
||
| 95 | try |
||
| 96 | { |
||
| 97 | if (command_id >= 128) |
||
| 98 | { |
||
| 99 | return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id), "module", module_name).GetAttributeValue("name"); |
||
| 100 | } |
||
| 101 | |||
| 102 | return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
||
| 103 | } |
||
| 104 | catch (std::runtime_error& e) |
||
| 105 | { |
||
| 106 | LOG.Error(e.what()); |
||
| 107 | throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | unsigned int Protocol::ResolveCommandId(std::string command_name, std::string module_name) |
||
| 112 | { |
||
| 113 | try |
||
| 114 | { |
||
| 115 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).GetAttributeValue("id")); |
||
| 116 | } |
||
| 117 | catch (std::runtime_error& e) |
||
| 118 | { |
||
| 119 | } |
||
| 120 | |||
| 121 | try |
||
| 122 | { |
||
| 123 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name).GetAttributeValue("id")); |
||
| 124 | } |
||
| 125 | catch (std::runtime_error& e) |
||
| 126 | { |
||
| 127 | LOG.Error(e.what()); |
||
| 128 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | std::string Protocol::LookupNMTCommandName(unsigned int command_id) |
||
| 133 | { |
||
| 134 | try |
||
| 135 | { |
||
| 136 | return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name"); |
||
| 137 | } |
||
| 138 | catch (std::runtime_error& e) |
||
| 139 | { |
||
| 140 | LOG.Error(e.what()); |
||
| 141 | throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id)); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | unsigned int Protocol::ResolveNMTCommandId(std::string command_name) |
||
| 146 | { |
||
| 147 | try |
||
| 148 | { |
||
| 149 | return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id")); |
||
| 150 | } |
||
| 151 | catch (std::runtime_error& e) |
||
| 152 | { |
||
| 153 | LOG.Error(e.what()); |
||
| 154 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | std::string Protocol::GetDefineId(std::string name, std::string group) |
||
| 159 | { |
||
| 160 | try |
||
| 161 | { |
||
| 162 | return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id"); |
||
| 163 | } |
||
| 164 | catch (std::runtime_error& e) |
||
| 165 | { |
||
| 166 | LOG.Error(e.what()); |
||
| 167 | throw std::runtime_error("No such define found, name = " + name + ", group = " + group); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | std::string Protocol::LookupDirectionFlag(unsigned int direction_flag) |
||
| 172 | { |
||
| 173 | try |
||
| 174 | { |
||
| 175 | return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name"); |
||
| 176 | } |
||
| 177 | catch (std::runtime_error& e) |
||
| 178 | { |
||
| 179 | LOG.Error(e.what()); |
||
| 180 | throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag)); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | unsigned int Protocol::ResolveDirectionFlag(std::string direction_name) |
||
| 185 | { |
||
| 186 | try |
||
| 187 | { |
||
| 188 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id")); |
||
| 189 | } |
||
| 190 | catch (std::runtime_error& e) |
||
| 191 | { |
||
| 192 | LOG.Error(e.what()); |
||
| 193 | throw std::runtime_error("No such direction flag found, direction_name = " + direction_name); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | std::string Protocol::LookupModuleName(unsigned int module_id) |
||
| 198 | { |
||
| 1598 | runge | 199 | if (module_id == 0) |
| 200 | { |
||
| 201 | return ""; |
||
| 202 | } |
||
| 203 | |||
| 1596 | runge | 204 | try |
| 205 | { |
||
| 206 | return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name"); |
||
| 207 | } |
||
| 208 | catch (std::runtime_error& e) |
||
| 209 | { |
||
| 210 | LOG.Error(e.what()); |
||
| 211 | throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id)); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | unsigned int Protocol::ResolveModuleId(std::string module_name) |
||
| 216 | { |
||
| 1598 | runge | 217 | if (module_name == "") |
| 218 | { |
||
| 219 | return 0; |
||
| 220 | } |
||
| 221 | |||
| 1596 | runge | 222 | try |
| 223 | { |
||
| 224 | return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id")); |
||
| 225 | } |
||
| 226 | catch (std::runtime_error& e) |
||
| 227 | { |
||
| 228 | LOG.Error(e.what()); |
||
| 229 | throw std::runtime_error("No such module name found, " + module_name); |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name) |
||
| 234 | { |
||
| 235 | try |
||
| 236 | { |
||
| 237 | return this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).FindChild("variables").GetChildren(); |
||
| 238 | } |
||
| 239 | catch (std::runtime_error& e) |
||
| 240 | { |
||
| 241 | } |
||
| 242 | |||
| 243 | try |
||
| 244 | { |
||
| 245 | return this->root_node_.FindChild("commands").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
||
| 246 | } |
||
| 247 | catch (std::runtime_error& e) |
||
| 248 | { |
||
| 249 | LOG.Error(e.what()); |
||
| 250 | throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name) |
||
| 255 | { |
||
| 256 | try |
||
| 257 | { |
||
| 258 | return this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).FindChild("variables").GetChildren(); |
||
| 259 | } |
||
| 260 | catch (std::runtime_error& e) |
||
| 261 | { |
||
| 262 | LOG.Error(e.what()); |
||
| 263 | throw std::runtime_error("No such nmt command found, name = " + command_name); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | |||
| 1598 | runge | 267 | std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 1596 | runge | 268 | { |
| 1598 | runge | 269 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 270 | int raw_value = 0; |
||
| 1596 | runge | 271 | |
| 1598 | runge | 272 | memcpy(&raw_value, &raw_bit_value, sizeof(raw_value)); |
| 273 | |||
| 274 | return boost::lexical_cast<std::string>(raw_value); |
||
| 1596 | runge | 275 | } |
| 276 | |||
| 1598 | runge | 277 | void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 1596 | runge | 278 | { |
| 1598 | runge | 279 | unsigned long raw_bit_value = 0; |
| 280 | int raw_value = boost::lexical_cast<int>(value); |
||
| 1596 | runge | 281 | |
| 1598 | runge | 282 | memcpy(&raw_bit_value, &raw_value, sizeof(raw_value)); |
| 283 | |||
| 284 | bitset.Write(start_bit, bit_length, raw_bit_value); |
||
| 1596 | runge | 285 | } |
| 286 | |||
| 1598 | runge | 287 | std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 1596 | runge | 288 | { |
| 1598 | runge | 289 | unsigned long raw_bit_value = bitset.Read(start_bit, bit_length); |
| 290 | |||
| 291 | return boost::lexical_cast<std::string>((unsigned int)raw_bit_value); |
||
| 1596 | runge | 292 | } |
| 293 | |||
| 1598 | runge | 294 | void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 1596 | runge | 295 | { |
| 1598 | runge | 296 | unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value); |
| 1596 | runge | 297 | |
| 1598 | runge | 298 | bitset.Write(start_bit, bit_length, raw_bit_value); |
| 1596 | runge | 299 | } |
| 300 | |||
| 1598 | runge | 301 | std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 1596 | runge | 302 | { |
| 1598 | runge | 303 | float raw_value = 0; |
| 304 | bool negative = false;; |
||
| 1596 | runge | 305 | |
| 1598 | runge | 306 | if (bitset.Get(start_bit)) |
| 1596 | runge | 307 | { |
| 1598 | runge | 308 | negative = true; |
| 309 | } |
||
| 310 | |||
| 311 | for (int c = bit_length-1; c > 0; c--) |
||
| 312 | { |
||
| 313 | if (bitset.Get(start_bit + c) != negative) |
||
| 1596 | runge | 314 | { |
| 1598 | runge | 315 | raw_value += pow(2.0f, (int)(bit_length-1-c)); |
| 1596 | runge | 316 | } |
| 317 | } |
||
| 318 | |||
| 1598 | runge | 319 | raw_value /= 64.0f; |
| 1596 | runge | 320 | |
| 1598 | runge | 321 | if (negative) |
| 1596 | runge | 322 | { |
| 1598 | runge | 323 | raw_value = -raw_value; |
| 1596 | runge | 324 | } |
| 325 | |||
| 1598 | runge | 326 | return boost::lexical_cast<std::string>(raw_value); |
| 1596 | runge | 327 | } |
| 328 | |||
| 1598 | runge | 329 | void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 1596 | runge | 330 | { |
| 1598 | runge | 331 | // TODO: |
| 1596 | runge | 332 | } |
| 333 | |||
| 1598 | runge | 334 | std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
| 1596 | runge | 335 | { |
| 1598 | runge | 336 | std::string value; |
| 337 | char c = 0; |
||
| 1596 | runge | 338 | |
| 1598 | runge | 339 | for (unsigned int p = 0; p < bit_length; p += 8) |
| 1596 | runge | 340 | { |
| 1598 | runge | 341 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 8); |
| 342 | memcpy(&c, &raw_bit_value, sizeof(c)); |
||
| 343 | value += c; |
||
| 1596 | runge | 344 | } |
| 345 | |||
| 1598 | runge | 346 | return value; |
| 1596 | runge | 347 | } |
| 348 | |||
| 1598 | runge | 349 | void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
| 1596 | runge | 350 | { |
| 1598 | runge | 351 | // TODO |
| 352 | } |
||
| 353 | |||
| 354 | std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length) |
||
| 355 | { |
||
| 356 | std::string value; |
||
| 357 | char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
||
| 1596 | runge | 358 | |
| 1598 | runge | 359 | for (unsigned int p = 0; p < bit_length; p += 4) |
| 1596 | runge | 360 | { |
| 1598 | runge | 361 | unsigned long raw_bit_value = bitset.Read(start_bit + p, 4); |
| 1596 | runge | 362 | |
| 1598 | runge | 363 | if (raw_bit_value >= sizeof(hex)) |
| 1596 | runge | 364 | { |
| 1598 | runge | 365 | value += '-'; |
| 1596 | runge | 366 | } |
| 1598 | runge | 367 | else |
| 1596 | runge | 368 | { |
| 1598 | runge | 369 | value += hex[raw_bit_value]; |
| 1596 | runge | 370 | } |
| 371 | } |
||
| 372 | |||
| 1598 | runge | 373 | return value; |
| 1596 | runge | 374 | } |
| 1598 | runge | 375 | |
| 376 | void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value) |
||
| 377 | { |
||
| 378 | // TODO: |
||
| 379 | } |
||
| 380 | |||
| 1596 | runge | 381 | }; // namespace can |
| 382 | }; // namespace atom |