| 1 | /* |
| 2 | * SPDX-FileName: generic.hxx |
| 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 | #pragma once |
| 9 | |
| 10 | #include <string> |
| 11 | |
| 12 | #include <simgear/compiler.h> |
| 13 | |
| 14 | #include "protocol.hxx" |
| 15 | |
| 16 | |
| 17 | class FGGeneric : public FGProtocol |
| 18 | { |
| 19 | public: |
| 20 | FGGeneric(std::vector<std::string>); |
| 21 | ~FGGeneric(); |
| 22 | |
| 23 | bool gen_message(); |
| 24 | bool parse_message_len(int length); |
| 25 | |
| 26 | // open hailing frequencies |
| 27 | bool open(); |
| 28 | |
| 29 | void reinit(); |
| 30 | |
| 31 | // process work for this port |
| 32 | bool process(); |
| 33 | |
| 34 | // close the channel |
| 35 | bool close(); |
| 36 | |
| 37 | void setExitOnError(bool val) { exitOnError = val; } |
| 38 | bool getExitOnError() { return exitOnError; } |
| 39 | bool getInitOk(void) { return initOk; } |
| 40 | |
| 41 | protected: |
| 42 | enum e_type { FG_BOOL = 0, |
| 43 | FG_INT, |
| 44 | FG_FLOAT, |
| 45 | FG_DOUBLE, |
| 46 | FG_STRING, |
| 47 | FG_FIXED, |
| 48 | FG_BYTE, |
| 49 | FG_WORD }; |
| 50 | |
| 51 | typedef struct { |
| 52 | std::string format; |
| 53 | e_type type; |
| 54 | double offset; |
| 55 | double factor; |
| 56 | double min, max; |
| 57 | bool wrap; |
| 58 | bool rel; |
| 59 | SGPropertyNode_ptr prop; |
| 60 | } _serial_prot; |
| 61 | |
| 62 | private: |
| 63 | std::string file_name; |
| 64 | |
| 65 | int length; |
| 66 | char buf[FG_MAX_MSG_SIZE]; |
| 67 | |
| 68 | std::string preamble; |
| 69 | std::string postamble; |
| 70 | std::string var_separator; |
| 71 | std::string line_separator; |
| 72 | std::string var_sep_string; |
| 73 | std::string line_sep_string; |
| 74 | std::vector<_serial_prot> _out_message; |
| 75 | std::vector<_serial_prot> _in_message; |
| 76 | |
| 77 | bool binary_mode; |
| 78 | enum { FOOTER_NONE, |
| 79 | FOOTER_LENGTH, |
| 80 | FOOTER_MAGIC } binary_footer_type; |
| 81 | int binary_footer_value; |
| 82 | int binary_record_length; |
| 83 | enum { BYTE_ORDER_NEEDS_CONVERSION, |
| 84 | BYTE_ORDER_MATCHES_NETWORK_ORDER } binary_byte_order; |
| 85 | |
| 86 | bool gen_message_ascii(); |
| 87 | bool gen_message_binary(); |
| 88 | bool parse_message_ascii(int length); |
| 89 | bool parse_message_binary(int length); |
| 90 | bool read_config(SGPropertyNode* root, std::vector<_serial_prot>& msg); |
| 91 | bool exitOnError; |
| 92 | bool initOk; |
| 93 | |
| 94 | class FGProtocolWrapper* wrapper; |
| 95 | |
| 96 | template <class T> |
| 97 | static void updateValue(_serial_prot& prot, const T& val) |
| 98 | { |
| 99 | T new_val = (prot.rel ? getValue<T>(prot.prop) : 0) + prot.offset + prot.factor * val; |
| 100 | |
| 101 | if (prot.max > prot.min) { |
| 102 | if (prot.wrap) |
| 103 | new_val = SGMisc<double>::normalizePeriodic(prot.min, prot.max, new_val); |
| 104 | else |
| 105 | new_val = SGMisc<T>::clip(new_val, prot.min, prot.max); |
| 106 | } |
| 107 | |
| 108 | setValue(prot.prop, new_val); |
| 109 | } |
| 110 | |
| 111 | // Special handling for bool (relative change = toggle, no min/max, no wrap) |
| 112 | static void updateValue(_serial_prot& prot, bool val); |
| 113 | }; |