| 1 | /* |
| 2 | * SPDX-FileName: generic.cxx |
| 3 | * SPDX-FileComment: generic protocol class |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | # include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include <string.h> // strstr() |
| 13 | #include <stdlib.h> // strtod(), atoi() |
| 14 | #include <cstdio> |
| 15 | |
| 16 | #include <simgear/debug/logstream.hxx> |
| 17 | #include <simgear/io/iochannel.hxx> |
| 18 | #include <simgear/structure/exception.hxx> |
| 19 | #include <simgear/misc/sg_path.hxx> |
| 20 | #include <simgear/misc/stdint.hxx> |
| 21 | #include <simgear/misc/strutils.hxx> |
| 22 | #include <simgear/props/props.hxx> |
| 23 | #include <simgear/props/props_io.hxx> |
| 24 | #include <simgear/math/SGMath.hxx> |
| 25 | |
| 26 | #include "generic.hxx" |
| 27 | #include <Main/fg_os.hxx> |
| 28 | #include <Main/fg_props.hxx> |
| 29 | #include <Main/globals.hxx> |
| 30 | #include <Main/sentryIntegration.hxx> |
| 31 | #include <Main/util.hxx> |
| 32 | |
| 33 | using simgear::strutils::unescape; |
| 34 | |
| 35 | class FGProtocolWrapper { |
| 36 | public: |
| 37 | virtual ~FGProtocolWrapper() {} |
| 38 | virtual int wrap( size_t n, uint8_t * buf ) = 0; |
| 39 | virtual int unwrap( size_t n, uint8_t * buf ) = 0; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * http://www.ka9q.net/papers/kiss.html |
| 44 | */ |
| 45 | class FGKissWrapper : public FGProtocolWrapper { |
| 46 | public: |
| 47 | virtual int wrap( size_t n, uint8_t * buf ); |
| 48 | virtual int unwrap( size_t n, uint8_t * buf ); |
| 49 | private: |
| 50 | static const uint8_t FEND; |
| 51 | static const uint8_t FESC; |
| 52 | static const uint8_t TFEND; |
| 53 | static const uint8_t TFESC; |
| 54 | }; |
| 55 | |
| 56 | const uint8_t FGKissWrapper::FEND = 0xC0; |
| 57 | const uint8_t FGKissWrapper::FESC = 0xDB; |
| 58 | const uint8_t FGKissWrapper::TFEND = 0xDC; |
| 59 | const uint8_t FGKissWrapper::TFESC = 0xDD; |
| 60 | |
| 61 | int FGKissWrapper::wrap( size_t n, uint8_t * buf ) |
| 62 | { |
| 63 | std::vector<uint8_t> dest; |
| 64 | uint8_t *sp = buf; |
| 65 | |
| 66 | dest.push_back(FEND); |
| 67 | dest.push_back(0); // command/channel always zero |
| 68 | for( size_t i = 0; i < n; i++ ) { |
| 69 | uint8_t c = *sp++; |
| 70 | switch( c ) { |
| 71 | case FESC: |
| 72 | dest.push_back(FESC); |
| 73 | dest.push_back(TFESC); |
| 74 | break; |
| 75 | |
| 76 | case FEND: |
| 77 | dest.push_back(FESC); |
| 78 | dest.push_back(TFEND); |
| 79 | break; |
| 80 | |
| 81 | default: |
| 82 | dest.push_back(c); |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | dest.push_back(FEND); |
| 87 | |
| 88 | memcpy( buf, dest.data(), dest.size() ); |
| 89 | return dest.size(); |
| 90 | } |
| 91 | |
| 92 | int FGKissWrapper::unwrap( size_t n, uint8_t * buf ) |
| 93 | { |
| 94 | uint8_t * sp = buf; |
| 95 | |
| 96 | // look for FEND |
| 97 | while( 0 < n && FEND != *sp ) { |
| 98 | sp++; |
| 99 | n--; |
| 100 | } |
| 101 | |
| 102 | // ignore all leading FEND |
| 103 | while( 0 < n && FEND == *sp ) { |
| 104 | sp++; |
| 105 | n--; |
| 106 | } |
| 107 | |
| 108 | if( 0 == n ) return 0; |
| 109 | |
| 110 | std::vector<uint8_t> dest; |
| 111 | { |
| 112 | bool escaped = false; |
| 113 | |
| 114 | while( 0 < n ) { |
| 115 | |
| 116 | n--; |
| 117 | uint8_t c = *sp++; |
| 118 | |
| 119 | if( escaped ) { |
| 120 | switch( c ) { |
| 121 | |
| 122 | case TFESC: |
| 123 | dest.push_back( FESC ); |
| 124 | break; |
| 125 | |
| 126 | case TFEND: |
| 127 | dest.push_back( FEND ); |
| 128 | break; |
| 129 | |
| 130 | default: // this is an error - ignore and continue |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | escaped = false; |
| 135 | |
| 136 | } else { |
| 137 | |
| 138 | switch( c ) { |
| 139 | case FESC: |
| 140 | escaped = true; |
| 141 | break; |
| 142 | |
| 143 | case FEND: |
| 144 | if( 0 != n ) { |
| 145 | SG_LOG(SG_IO, SG_WARN, |
| 146 | "KISS frame detected FEND before end of frame. Trailing data dropped." ); |
| 147 | } |
| 148 | n = 0; |
| 149 | break; |
| 150 | |
| 151 | default: |
| 152 | dest.push_back( c ); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | memcpy( buf, dest.data(), dest.size() ); |
| 160 | return dest.size(); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | class FGSTXETXWrapper : public FGProtocolWrapper { |
| 165 | public: |
| 166 | virtual int wrap( size_t n, uint8_t * buf ); |
| 167 | virtual int unwrap( size_t n, uint8_t * buf ); |
| 168 | |
| 169 | static const uint8_t STX; |
| 170 | static const uint8_t ETX; |
| 171 | static const uint8_t DLE; |
| 172 | }; |
| 173 | |
| 174 | const uint8_t FGSTXETXWrapper::STX = 0x02; |
| 175 | const uint8_t FGSTXETXWrapper::ETX = 0x03; |
| 176 | const uint8_t FGSTXETXWrapper::DLE = 0x00; |
| 177 | |
| 178 | int FGSTXETXWrapper::wrap( size_t n, uint8_t * buf ) |
| 179 | { |
| 180 | // stuff payload as |
| 181 | // <dle><stx>payload<dle><etx> |
| 182 | // if payload contains <dle>, stuff <dle> as <dle><dle> |
| 183 | std::vector<uint8_t> dest; |
| 184 | uint8_t *sp = buf; |
| 185 | |
| 186 | dest.push_back(DLE); |
| 187 | dest.push_back(STX); |
| 188 | |
| 189 | while( n > 0 ) { |
| 190 | n--; |
| 191 | |
| 192 | if( DLE == *sp ) |
| 193 | dest.push_back(DLE); |
| 194 | |
| 195 | dest.push_back(*sp++); |
| 196 | } |
| 197 | |
| 198 | dest.push_back(DLE); |
| 199 | dest.push_back(ETX); |
| 200 | |
| 201 | memcpy( buf, dest.data(), dest.size() ); |
| 202 | return dest.size(); |
| 203 | } |
| 204 | |
| 205 | int FGSTXETXWrapper::unwrap( size_t n, uint8_t * buf ) |
| 206 | { |
| 207 | return n; |
| 208 | } |
| 209 | |
| 210 | FGGeneric::FGGeneric(std::vector<std::string> tokens) : exitOnError(false), initOk(false), wrapper(NULL) |
| 211 | { |
| 212 | size_t configToken; |
| 213 | if (tokens[1] == "socket") { |
| 214 | configToken = 7; |
| 215 | } else if (tokens[1] == "file") { |
| 216 | configToken = 5; |
| 217 | } else { |
| 218 | configToken = 6; |
| 219 | } |
| 220 | |
| 221 | if ((configToken >= tokens.size())||(tokens[ configToken ] == "")) { |
| 222 | SG_LOG(SG_NETWORK, SG_ALERT, |
| 223 | "Not enough tokens passed for generic '" << tokens[1] << "' protocol. "); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | std::string config = tokens[ configToken ]; |
| 228 | file_name = config+".xml"; |
| 229 | set_direction(tokens[2]); |
| 230 | |
| 231 | if (get_direction() == SG_IO_NONE) { |
| 232 | SG_LOG(SG_NETWORK, SG_ALERT, "Unsupported protocol direction: " << tokens[2]); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | reinit(); |
| 237 | } |
| 238 | |
| 239 | FGGeneric::~FGGeneric() { |
| 240 | delete wrapper; |
| 241 | } |
| 242 | |
| 243 | union u32 { |
| 244 | uint32_t intVal; |
| 245 | float floatVal; |
| 246 | }; |
| 247 | |
| 248 | union u64 { |
| 249 | uint64_t longVal; |
| 250 | double doubleVal; |
| 251 | }; |
| 252 | |
| 253 | // generate the message |
| 254 | bool FGGeneric::gen_message_binary() { |
| 255 | std::string generic_sentence; |
| 256 | length = 0; |
| 257 | |
| 258 | double val; |
| 259 | for (unsigned int i = 0; i < _out_message.size(); i++) { |
| 260 | |
| 261 | switch (_out_message[i].type) { |
| 262 | case FG_INT: |
| 263 | { |
| 264 | val = _out_message[i].offset + |
| 265 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 266 | int32_t intVal = val; |
| 267 | if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) { |
| 268 | intVal = (int32_t) sg_bswap_32((uint32_t)intVal); |
| 269 | } |
| 270 | memcpy(&buf[length], &intVal, sizeof(int32_t)); |
| 271 | length += sizeof(int32_t); |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | case FG_BOOL: |
| 276 | buf[length] = (char) (_out_message[i].prop->getBoolValue() ? true : false); |
| 277 | length += 1; |
| 278 | break; |
| 279 | |
| 280 | case FG_FIXED: |
| 281 | { |
| 282 | val = _out_message[i].offset + |
| 283 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 284 | |
| 285 | int32_t fixed = (int)(val * 65536.0f); |
| 286 | if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) { |
| 287 | fixed = (int32_t) sg_bswap_32((uint32_t)fixed); |
| 288 | } |
| 289 | memcpy(&buf[length], &fixed, sizeof(int32_t)); |
| 290 | length += sizeof(int32_t); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | case FG_FLOAT: |
| 295 | { |
| 296 | val = _out_message[i].offset + |
| 297 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 298 | u32 tmpun32; |
| 299 | tmpun32.floatVal = static_cast<float>(val); |
| 300 | |
| 301 | if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) { |
| 302 | tmpun32.intVal = sg_bswap_32(tmpun32.intVal); |
| 303 | } |
| 304 | memcpy(&buf[length], &tmpun32.intVal, sizeof(uint32_t)); |
| 305 | length += sizeof(uint32_t); |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | case FG_DOUBLE: |
| 310 | { |
| 311 | val = _out_message[i].offset + |
| 312 | _out_message[i].prop->getDoubleValue() * _out_message[i].factor; |
| 313 | u64 tmpun64; |
| 314 | tmpun64.doubleVal = val; |
| 315 | |
| 316 | if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) { |
| 317 | tmpun64.longVal = sg_bswap_64(tmpun64.longVal); |
| 318 | } |
| 319 | memcpy(&buf[length], &tmpun64.longVal, sizeof(uint64_t)); |
| 320 | length += sizeof(uint64_t); |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | case FG_BYTE: |
| 325 | { |
| 326 | val = _out_message[i].offset + |
| 327 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 328 | int8_t byteVal = val; |
| 329 | memcpy(&buf[length], &byteVal, sizeof(int8_t)); |
| 330 | length += sizeof(int8_t); |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | case FG_WORD: |
| 335 | { |
| 336 | val = _out_message[i].offset + |
| 337 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 338 | int16_t wordVal = val; |
| 339 | memcpy(&buf[length], &wordVal, sizeof(int16_t)); |
| 340 | length += sizeof(int16_t); |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | default: // SG_STRING |
| 345 | std::string strdata = _out_message[i].prop->getStringValue(); |
| 346 | size_t strlength = strdata.length(); |
| 347 | |
| 348 | if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { |
| 349 | SG_LOG( SG_IO, SG_ALERT, "Generic protocol: " |
| 350 | "FG_STRING will be written in host byte order."); |
| 351 | } |
| 352 | /* Format for strings is |
| 353 | * [length as int, 4 bytes][ASCII data, length bytes] |
| 354 | */ |
| 355 | if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) { |
| 356 | strlength = sg_bswap_32(strlength); |
| 357 | } |
| 358 | memcpy(&buf[length], &strlength, sizeof(int32_t)); |
| 359 | length += sizeof(int32_t); |
| 360 | strncpy(&buf[length], strdata.c_str(), strlength); |
| 361 | length += strlength; |
| 362 | /* FIXME padding for alignment? Something like: |
| 363 | * length += (strlength % 4 > 0 ? sizeof(int32_t) - strlength % 4 : 0; |
| 364 | */ |
| 365 | break; |
| 366 | |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // add the footer to the packet ("line") |
| 371 | switch (binary_footer_type) { |
| 372 | case FOOTER_LENGTH: |
| 373 | binary_footer_value = length; |
| 374 | break; |
| 375 | |
| 376 | case FOOTER_MAGIC: |
| 377 | case FOOTER_NONE: |
| 378 | break; |
| 379 | } |
| 380 | |
| 381 | if (binary_footer_type != FOOTER_NONE) { |
| 382 | int32_t intValue = binary_footer_value; |
| 383 | if (binary_byte_order != BYTE_ORDER_MATCHES_NETWORK_ORDER) { |
| 384 | intValue = sg_bswap_32(binary_footer_value); |
| 385 | } |
| 386 | memcpy(&buf[length], &intValue, sizeof(int32_t)); |
| 387 | length += sizeof(int32_t); |
| 388 | } |
| 389 | |
| 390 | if( wrapper ) length = wrapper->wrap( length, reinterpret_cast<uint8_t*>(buf) ); |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | bool FGGeneric::gen_message_ascii() { |
| 396 | std::string generic_sentence; |
| 397 | char tmp[255]; |
| 398 | length = 0; |
| 399 | |
| 400 | double val; |
| 401 | for (unsigned int i = 0; i < _out_message.size(); i++) { |
| 402 | |
| 403 | if (i > 0) { |
| 404 | generic_sentence += var_separator; |
| 405 | } |
| 406 | |
| 407 | std::string format = simgear::strutils::sanitizePrintfFormat(_out_message[i].format); |
| 408 | |
| 409 | switch (_out_message[i].type) { |
| 410 | case FG_BYTE: |
| 411 | case FG_WORD: |
| 412 | case FG_INT: |
| 413 | val = _out_message[i].offset + |
| 414 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 415 | snprintf(tmp, 255, format.c_str(), (int)val); |
| 416 | break; |
| 417 | |
| 418 | case FG_BOOL: |
| 419 | snprintf(tmp, 255, format.c_str(), |
| 420 | _out_message[i].prop->getBoolValue()); |
| 421 | break; |
| 422 | |
| 423 | case FG_FIXED: |
| 424 | val = _out_message[i].offset + |
| 425 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 426 | snprintf(tmp, 255, format.c_str(), (float)val); |
| 427 | break; |
| 428 | |
| 429 | case FG_FLOAT: |
| 430 | val = _out_message[i].offset + |
| 431 | _out_message[i].prop->getFloatValue() * _out_message[i].factor; |
| 432 | snprintf(tmp, 255, format.c_str(), (float)val); |
| 433 | break; |
| 434 | |
| 435 | case FG_DOUBLE: |
| 436 | val = _out_message[i].offset + |
| 437 | _out_message[i].prop->getDoubleValue() * _out_message[i].factor; |
| 438 | snprintf(tmp, 255, format.c_str(), (double)val); |
| 439 | break; |
| 440 | |
| 441 | default: // SG_STRING |
| 442 | snprintf(tmp, 255, format.c_str(), |
| 443 | _out_message[i].prop->getStringValue().c_str()); |
| 444 | } |
| 445 | |
| 446 | generic_sentence += tmp; |
| 447 | } |
| 448 | |
| 449 | /* After each lot of variables has been added, put the line separator |
| 450 | * char/string |
| 451 | */ |
| 452 | generic_sentence += line_separator; |
| 453 | |
| 454 | length = generic_sentence.length(); |
| 455 | strncpy( buf, generic_sentence.c_str(), length ); |
| 456 | |
| 457 | return true; |
| 458 | } |
| 459 | |
| 460 | bool FGGeneric::gen_message() { |
| 461 | if (binary_mode) { |
| 462 | return gen_message_binary(); |
| 463 | } else { |
| 464 | return gen_message_ascii(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | bool FGGeneric::parse_message_binary(int length) { |
| 469 | char *p2, *p1 = buf; |
| 470 | int32_t tmp32; |
| 471 | int i = -1; |
| 472 | |
| 473 | p2 = p1 + length; |
| 474 | while ((++i < (int)_in_message.size()) && (p1 < p2)) { |
| 475 | |
| 476 | switch (_in_message[i].type) { |
| 477 | case FG_INT: |
| 478 | if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { |
| 479 | tmp32 = sg_bswap_32(*(int32_t *)p1); |
| 480 | } else { |
| 481 | tmp32 = *(int32_t *)p1; |
| 482 | } |
| 483 | updateValue(_in_message[i], (int)tmp32); |
| 484 | p1 += sizeof(int32_t); |
| 485 | break; |
| 486 | |
| 487 | case FG_BOOL: |
| 488 | updateValue(_in_message[i], p1[0] != 0); |
| 489 | p1 += 1; |
| 490 | break; |
| 491 | |
| 492 | case FG_FIXED: |
| 493 | if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { |
| 494 | tmp32 = sg_bswap_32(*(int32_t *)p1); |
| 495 | } else { |
| 496 | tmp32 = *(int32_t *)p1; |
| 497 | } |
| 498 | updateValue(_in_message[i], (float)tmp32 / 65536.0f); |
| 499 | p1 += sizeof(int32_t); |
| 500 | break; |
| 501 | |
| 502 | case FG_FLOAT: |
| 503 | u32 tmpun32; |
| 504 | if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { |
| 505 | tmpun32.intVal = sg_bswap_32(*(uint32_t *)p1); |
| 506 | } else { |
| 507 | tmpun32.floatVal = *(float *)p1; |
| 508 | } |
| 509 | updateValue(_in_message[i], tmpun32.floatVal); |
| 510 | p1 += sizeof(int32_t); |
| 511 | break; |
| 512 | |
| 513 | case FG_DOUBLE: |
| 514 | u64 tmpun64; |
| 515 | if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { |
| 516 | tmpun64.longVal = sg_bswap_64(*(uint64_t *)p1); |
| 517 | } else { |
| 518 | tmpun64.doubleVal = *(double *)p1; |
| 519 | } |
| 520 | updateValue(_in_message[i], tmpun64.doubleVal); |
| 521 | p1 += sizeof(int64_t); |
| 522 | break; |
| 523 | |
| 524 | case FG_BYTE: |
| 525 | tmp32 = *(int8_t *)p1; |
| 526 | updateValue(_in_message[i], (int)tmp32); |
| 527 | p1 += sizeof(int8_t); |
| 528 | break; |
| 529 | |
| 530 | case FG_WORD: |
| 531 | if (binary_byte_order == BYTE_ORDER_NEEDS_CONVERSION) { |
| 532 | tmp32 = sg_bswap_16(*(int16_t *)p1); |
| 533 | } else { |
| 534 | tmp32 = *(int16_t *)p1; |
| 535 | } |
| 536 | updateValue(_in_message[i], (int)tmp32); |
| 537 | p1 += sizeof(int16_t); |
| 538 | break; |
| 539 | |
| 540 | default: // SG_STRING |
| 541 | SG_LOG( SG_IO, SG_ALERT, "Generic protocol: " |
| 542 | "Ignoring unsupported binary input chunk type."); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | bool FGGeneric::parse_message_ascii(int length) { |
| 551 | char *p1 = buf; |
| 552 | int i = -1; |
| 553 | int chunks = _in_message.size(); |
| 554 | int line_separator_size = line_separator.size(); |
| 555 | |
| 556 | if (length < line_separator_size || |
| 557 | line_separator.compare(buf + length - line_separator_size) != 0) { |
| 558 | |
| 559 | SG_LOG(SG_IO, SG_WARN, |
| 560 | "Input line does not end with expected line separator." ); |
| 561 | } else { |
| 562 | buf[length - line_separator_size] = 0; |
| 563 | } |
| 564 | |
| 565 | size_t varsep_len = var_separator.length(); |
| 566 | while ((++i < chunks) && p1) { |
| 567 | char* p2 = NULL; |
| 568 | |
| 569 | if (varsep_len > 0) |
| 570 | { |
| 571 | p2 = strstr(p1, var_separator.c_str()); |
| 572 | if (p2) { |
| 573 | *p2 = 0; |
| 574 | p2 += varsep_len; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | switch (_in_message[i].type) { |
| 579 | case FG_BYTE: |
| 580 | case FG_WORD: |
| 581 | case FG_INT: |
| 582 | updateValue(_in_message[i], atoi(p1)); |
| 583 | break; |
| 584 | |
| 585 | case FG_BOOL: |
| 586 | updateValue(_in_message[i], atof(p1) != 0.0); |
| 587 | break; |
| 588 | |
| 589 | case FG_FIXED: |
| 590 | case FG_FLOAT: |
| 591 | updateValue(_in_message[i], (float)strtod(p1, 0)); |
| 592 | break; |
| 593 | |
| 594 | case FG_DOUBLE: |
| 595 | updateValue(_in_message[i], (double)strtod(p1, 0)); |
| 596 | break; |
| 597 | |
| 598 | default: // SG_STRING |
| 599 | _in_message[i].prop->setStringValue(p1); |
| 600 | break; |
| 601 | } |
| 602 | |
| 603 | p1 = p2; |
| 604 | } |
| 605 | |
| 606 | return true; |
| 607 | } |
| 608 | |
| 609 | bool FGGeneric::parse_message_len(int length) { |
| 610 | if (binary_mode) { |
| 611 | return parse_message_binary(length); |
| 612 | } else { |
| 613 | return parse_message_ascii(length); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | |
| 618 | // open hailing frequencies |
| 619 | bool FGGeneric::open() { |
| 620 | if ( is_enabled() ) { |
| 621 | SG_LOG( SG_IO, SG_ALERT, "This shouldn't happen, but the channel " |
| 622 | << "is already in use, ignoring" ); |
| 623 | return false; |
| 624 | } |
| 625 | |
| 626 | SGIOChannel *io = get_io_channel(); |
| 627 | |
| 628 | if ( ! io->open( get_direction() ) ) { |
| 629 | SG_LOG( SG_IO, SG_ALERT, "Error opening channel communication layer." ); |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | set_enabled( true ); |
| 634 | |
| 635 | if ( ((get_direction() == SG_IO_OUT )|| |
| 636 | (get_direction() == SG_IO_BI)) |
| 637 | && ! preamble.empty() ) { |
| 638 | if ( ! io->write( preamble.c_str(), preamble.size() ) ) { |
| 639 | SG_LOG( SG_IO, SG_WARN, "Error writing preamble." ); |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | return true; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | // process work for this port |
| 649 | bool FGGeneric::process() { |
| 650 | SGIOChannel *io = get_io_channel(); |
| 651 | |
| 652 | if ( (get_direction() == SG_IO_OUT) || |
| 653 | (get_direction() == SG_IO_BI) ) { |
| 654 | gen_message(); |
| 655 | if ( ! io->write( buf, length ) ) { |
| 656 | SG_LOG( SG_IO, SG_WARN, "Error writing data." ); |
| 657 | goto error_out; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | if (( get_direction() == SG_IO_IN ) || |
| 662 | (get_direction() == SG_IO_BI) ) { |
| 663 | if ( io->get_type() == sgFileType ) { |
| 664 | if (!binary_mode) { |
| 665 | length = io->readline( buf, FG_MAX_MSG_SIZE ); |
| 666 | if ( length > 0 ) { |
| 667 | parse_message_len( length ); |
| 668 | } else { |
| 669 | SG_LOG( SG_IO, SG_ALERT, "Error reading data." ); |
| 670 | return false; |
| 671 | } |
| 672 | } else { |
| 673 | length = io->read( buf, binary_record_length ); |
| 674 | if ( length == binary_record_length ) { |
| 675 | parse_message_len( length ); |
| 676 | } else { |
| 677 | SG_LOG( SG_IO, SG_ALERT, |
| 678 | "Generic protocol: Received binary " |
| 679 | "record of unexpected size, expected: " |
| 680 | << binary_record_length << " but received: " |
| 681 | << length); |
| 682 | } |
| 683 | } |
| 684 | } else { |
| 685 | if (!binary_mode) { |
| 686 | while ((length = io->readline( buf, FG_MAX_MSG_SIZE )) > 0 ) { |
| 687 | parse_message_len( length ); |
| 688 | } |
| 689 | } else { |
| 690 | while ((length = io->read( buf, binary_record_length )) |
| 691 | == binary_record_length ) { |
| 692 | parse_message_len( length ); |
| 693 | } |
| 694 | |
| 695 | if ( length > 0 ) { |
| 696 | SG_LOG( SG_IO, SG_ALERT, |
| 697 | "Generic protocol: Received binary " |
| 698 | "record of unexpected size, expected: " |
| 699 | << binary_record_length << " but received: " |
| 700 | << length); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | return true; |
| 706 | error_out: |
| 707 | if (exitOnError) { |
| 708 | fgOSExit(1); |
| 709 | return true; // should not get there, but please the compiler |
| 710 | } else |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | |
| 715 | // close the channel |
| 716 | bool FGGeneric::close() { |
| 717 | SGIOChannel *io = get_io_channel(); |
| 718 | |
| 719 | if ( ((get_direction() == SG_IO_OUT)|| |
| 720 | (get_direction() == SG_IO_BI)) |
| 721 | && ! postamble.empty() ) { |
| 722 | if ( ! io->write( postamble.c_str(), postamble.size() ) ) { |
| 723 | SG_LOG( SG_IO, SG_ALERT, "Error writing postamble." ); |
| 724 | return false; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | set_enabled( false ); |
| 729 | |
| 730 | if ( ! io->close() ) { |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | return true; |
| 735 | } |
| 736 | |
| 737 | |
| 738 | void |
| 739 | FGGeneric::reinit() |
| 740 | { |
| 741 | SGPath path = globals->findDataPath("Protocol/" + file_name); |
| 742 | if (!path.exists()) { |
| 743 | SG_LOG(SG_NETWORK, SG_WARN, "Couldn't find protocol file for '" << file_name << "'"); |
| 744 | return; |
| 745 | } |
| 746 | |
| 747 | SG_LOG(SG_NETWORK, SG_INFO, "Reading communication protocol from " << path); |
| 748 | |
| 749 | SGPropertyNode root; |
| 750 | try { |
| 751 | readProperties(path, &root); |
| 752 | } catch (const sg_exception & ex) { |
| 753 | SG_LOG(SG_NETWORK, SG_ALERT, |
| 754 | "Unable to load the protocol configuration file: " << ex.getFormattedMessage() ); |
| 755 | return; |
| 756 | } |
| 757 | |
| 758 | const auto dir = get_direction(); |
| 759 | if ((dir == SG_IO_OUT) || (dir == SG_IO_BI)) { |
| 760 | SGPropertyNode *output = root.getNode("generic/output"); |
| 761 | if (output) { |
| 762 | _out_message.clear(); |
| 763 | if (!read_config(output, _out_message)) |
| 764 | { |
| 765 | // bad configuration |
| 766 | return; |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if ((dir == SG_IO_IN) || (dir == SG_IO_BI)) { |
| 772 | SGPropertyNode *input = root.getNode("generic/input"); |
| 773 | if (input) { |
| 774 | _in_message.clear(); |
| 775 | if (!read_config(input, _in_message)) |
| 776 | { |
| 777 | // bad configuration |
| 778 | return; |
| 779 | } |
| 780 | if (!binary_mode && (line_separator.empty() || |
| 781 | *line_separator.rbegin() != '\n')) { |
| 782 | |
| 783 | SG_LOG(SG_IO, SG_WARN, |
| 784 | "Warning: Appending newline to line separator in generic input."); |
| 785 | line_separator.push_back('\n'); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | initOk = true; |
| 791 | } |
| 792 | |
| 793 | |
| 794 | bool |
| 795 | FGGeneric::read_config(SGPropertyNode *root, std::vector<_serial_prot> &msg) |
| 796 | { |
| 797 | binary_mode = root->getBoolValue("binary_mode"); |
| 798 | |
| 799 | if (!binary_mode) { |
| 800 | /* These variables specified in the $FG_ROOT/data/Protocol/xxx.xml |
| 801 | * file for each format |
| 802 | * |
| 803 | * var_sep_string = the string/charachter to place between variables |
| 804 | * line_sep_string = the string/charachter to place at the end of each |
| 805 | * lot of variables |
| 806 | */ |
| 807 | preamble = unescape(root->getStringValue("preamble")); |
| 808 | postamble = unescape(root->getStringValue("postamble")); |
| 809 | var_sep_string = unescape(root->getStringValue("var_separator")); |
| 810 | line_sep_string = unescape(root->getStringValue("line_separator")); |
| 811 | |
| 812 | if ( var_sep_string == "newline" ) { |
| 813 | var_separator = '\n'; |
| 814 | } else if ( var_sep_string == "tab" ) { |
| 815 | var_separator = '\t'; |
| 816 | } else if ( var_sep_string == "space" ) { |
| 817 | var_separator = ' '; |
| 818 | } else if ( var_sep_string == "formfeed" ) { |
| 819 | var_separator = '\f'; |
| 820 | } else if ( var_sep_string == "carriagereturn" ) { |
| 821 | var_sep_string = '\r'; |
| 822 | } else if ( var_sep_string == "verticaltab" ) { |
| 823 | var_separator = '\v'; |
| 824 | } else { |
| 825 | var_separator = var_sep_string; |
| 826 | } |
| 827 | |
| 828 | if ( line_sep_string == "newline" ) { |
| 829 | line_separator = '\n'; |
| 830 | } else if ( line_sep_string == "tab" ) { |
| 831 | line_separator = '\t'; |
| 832 | } else if ( line_sep_string == "space" ) { |
| 833 | line_separator = ' '; |
| 834 | } else if ( line_sep_string == "formfeed" ) { |
| 835 | line_separator = '\f'; |
| 836 | } else if ( line_sep_string == "carriagereturn" ) { |
| 837 | line_separator = '\r'; |
| 838 | } else if ( line_sep_string == "verticaltab" ) { |
| 839 | line_separator = '\v'; |
| 840 | } else { |
| 841 | line_separator = line_sep_string; |
| 842 | } |
| 843 | } else { |
| 844 | // default values: no footer and record_length = sizeof(representation) |
| 845 | binary_footer_type = FOOTER_NONE; |
| 846 | binary_record_length = -1; |
| 847 | |
| 848 | // default choice is network byte order (big endian) |
| 849 | if (sgIsLittleEndian()) { |
| 850 | binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION; |
| 851 | } else { |
| 852 | binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER; |
| 853 | } |
| 854 | |
| 855 | if ( root->hasValue("binary_footer") ) { |
| 856 | std::string footer_type = root->getStringValue("binary_footer"); |
| 857 | if ( footer_type == "length" ) { |
| 858 | binary_footer_type = FOOTER_LENGTH; |
| 859 | } else if ( footer_type.substr(0, 5) == "magic" ) { |
| 860 | binary_footer_type = FOOTER_MAGIC; |
| 861 | binary_footer_value = strtol(footer_type.substr(6, |
| 862 | footer_type.length() - 6).c_str(), (char**)0, 0); |
| 863 | } else if ( footer_type != "none" ) { |
| 864 | SG_LOG(SG_IO, SG_ALERT, |
| 865 | "generic protocol: Unknown generic binary protocol " |
| 866 | "footer '" << footer_type << "', using no footer."); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | if ( root->hasValue("record_length") ) { |
| 871 | binary_record_length = root->getIntValue("record_length"); |
| 872 | } |
| 873 | |
| 874 | if ( root->hasValue("byte_order") ) { |
| 875 | std::string byte_order = root->getStringValue("byte_order"); |
| 876 | if (byte_order == "network" ) { |
| 877 | if ( sgIsLittleEndian() ) { |
| 878 | binary_byte_order = BYTE_ORDER_NEEDS_CONVERSION; |
| 879 | } else { |
| 880 | binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER; |
| 881 | } |
| 882 | } else if ( byte_order == "host" ) { |
| 883 | binary_byte_order = BYTE_ORDER_MATCHES_NETWORK_ORDER; |
| 884 | } else { |
| 885 | SG_LOG(SG_IO, SG_ALERT, |
| 886 | "generic protocol: Undefined generic binary protocol" |
| 887 | "byte order, using HOST byte order."); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if( root->hasValue( "wrapper" ) ) { |
| 892 | std::string w = root->getStringValue( "wrapper" ); |
| 893 | if( w == "kiss" ) wrapper = new FGKissWrapper(); |
| 894 | else if( w == "stxetx" ) wrapper = new FGSTXETXWrapper(); |
| 895 | else SG_LOG(SG_IO, SG_ALERT, |
| 896 | "generic protocol: Undefined binary protocol wrapper '" + w + "' ignored" ); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | int record_length = 0; // Only used for binary protocols. |
| 901 | std::vector<SGPropertyNode_ptr> chunks = root->getChildren("chunk"); |
| 902 | |
| 903 | for (unsigned int i = 0; i < chunks.size(); i++) { |
| 904 | |
| 905 | _serial_prot chunk; |
| 906 | |
| 907 | // chunk.name = chunks[i]->getStringValue("name"); |
| 908 | chunk.format = unescape(chunks[i]->getStringValue("format", "%d")); |
| 909 | chunk.offset = chunks[i]->getDoubleValue("offset"); |
| 910 | chunk.factor = chunks[i]->getDoubleValue("factor", 1.0); |
| 911 | chunk.min = chunks[i]->getDoubleValue("min"); |
| 912 | chunk.max = chunks[i]->getDoubleValue("max"); |
| 913 | chunk.wrap = chunks[i]->getBoolValue("wrap"); |
| 914 | chunk.rel = chunks[i]->getBoolValue("relative"); |
| 915 | |
| 916 | if( chunks[i]->hasChild("const") ) { |
| 917 | chunk.prop = new SGPropertyNode(); |
| 918 | chunk.prop->setStringValue( chunks[i]->getStringValue("const", "" ) ); |
| 919 | } else { |
| 920 | std::string node = chunks[i]->getStringValue("node", "/null"); |
| 921 | chunk.prop = fgGetNode(node.c_str(), true); |
| 922 | } |
| 923 | |
| 924 | std::string type = chunks[i]->getStringValue("type"); |
| 925 | |
| 926 | // Note: officially the type is called 'bool' but for backward |
| 927 | // compatibility 'boolean' will also be supported. |
| 928 | if (type == "bool" || type == "boolean") { |
| 929 | chunk.type = FG_BOOL; |
| 930 | record_length += 1; |
| 931 | } else if (type == "float") { |
| 932 | chunk.type = FG_FLOAT; |
| 933 | record_length += sizeof(int32_t); |
| 934 | } else if (type == "double") { |
| 935 | chunk.type = FG_DOUBLE; |
| 936 | record_length += sizeof(int64_t); |
| 937 | } else if (type == "fixed") { |
| 938 | chunk.type = FG_FIXED; |
| 939 | record_length += sizeof(int32_t); |
| 940 | } else if (type == "string") { |
| 941 | chunk.type = FG_STRING; |
| 942 | } else if (type == "byte") { |
| 943 | chunk.type = FG_BYTE; |
| 944 | record_length += sizeof(int8_t); |
| 945 | } else if (type == "word") { |
| 946 | chunk.type = FG_WORD; |
| 947 | record_length += sizeof(int16_t); |
| 948 | } else { |
| 949 | chunk.type = FG_INT; |
| 950 | record_length += sizeof(int32_t); |
| 951 | } |
| 952 | msg.push_back(chunk); |
| 953 | |
| 954 | } |
| 955 | |
| 956 | if( !binary_mode ) |
| 957 | { |
| 958 | if ((chunks.size() > 1)&&(var_sep_string.length() == 0)) |
| 959 | { |
| 960 | // ASCII protocols really need a separator when there is more than one chunk per line |
| 961 | SG_LOG(SG_IO, SG_ALERT, |
| 962 | "generic protocol: Invalid configuration. " |
| 963 | "'var_separator' must not be empty for protocols which have more than one chunk per line."); |
| 964 | return false; |
| 965 | } |
| 966 | } |
| 967 | else |
| 968 | { |
| 969 | if (binary_record_length == -1) { |
| 970 | binary_record_length = record_length; |
| 971 | } else if (binary_record_length < record_length) { |
| 972 | SG_LOG(SG_IO, SG_ALERT, |
| 973 | "generic protocol: Requested binary record length shorter than " |
| 974 | " requested record representation."); |
| 975 | binary_record_length = record_length; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | return true; |
| 980 | } |
| 981 | |
| 982 | void FGGeneric::updateValue(FGGeneric::_serial_prot& prot, bool val) |
| 983 | { |
| 984 | if( prot.rel ) |
| 985 | { |
| 986 | // value inverted if received true, otherwise leave unchanged |
| 987 | if( val ) |
| 988 | setValue(prot.prop, !getValue<bool>(prot.prop)); |
| 989 | } |
| 990 | else |
| 991 | { |
| 992 | setValue(prot.prop, val); |
| 993 | } |
| 994 | } |