|
| 1 | +# This file is part of icspacket. |
| 2 | +# Copyright (C) 2025-present MatrixEditor @ github |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU General Public License as published by |
| 6 | +# the Free Software Foundation, either version 3 of the License, or |
| 7 | +# (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +# pyright: reportUnusedCallResult=false, reportGeneralTypeIssues=false |
| 17 | +# |
| 18 | +# Description: |
| 19 | +# - Reads values from an outstation |
| 20 | +import logging |
| 21 | +import sys |
| 22 | +import argparse |
| 23 | +import textwrap |
| 24 | + |
| 25 | +from rich.console import Console |
| 26 | + |
| 27 | +from icspacket.core import logger |
| 28 | +from icspacket.proto.dnp3.const import FunctionCode |
| 29 | +from icspacket.proto.dnp3.master import DNP3_Master |
| 30 | +from icspacket.proto.dnp3.objects.coding import unpack_objects |
| 31 | +from icspacket.proto.dnp3.objects.util import new_class_data_request, as_variation0 |
| 32 | + |
| 33 | +from icspacket.examples.util import add_logging_options |
| 34 | +from icspacket.examples.dnp3dump import dump_objects |
| 35 | +from icspacket.proto.dnp3.objects.variations import get_group_name |
| 36 | + |
| 37 | + |
| 38 | +def parse_target(target_spec: str) -> tuple[int, str, int] | None: |
| 39 | + # format: <link_addr>@<host>[:<port>] |
| 40 | + if "@" not in target_spec: |
| 41 | + return logging.error( |
| 42 | + "Invalid target specification: %s, expected <link_addr>@<host>[:<port>]", |
| 43 | + target_spec, |
| 44 | + ) |
| 45 | + |
| 46 | + link_addr, target_spec = target_spec.split("@", 1) |
| 47 | + if ":" in target_spec: |
| 48 | + host, port = target_spec.split(":", 1) |
| 49 | + else: |
| 50 | + host = target_spec |
| 51 | + port = 20000 |
| 52 | + return int(link_addr), host, int(port) |
| 53 | + |
| 54 | + |
| 55 | +class DNP3Reader: |
| 56 | + def __init__(self, master: DNP3_Master) -> None: |
| 57 | + self.master = master |
| 58 | + self.console = Console() |
| 59 | + |
| 60 | + def run(self, args): |
| 61 | + classes = [] |
| 62 | + if args.class0 or args.all_classes: |
| 63 | + classes.append(0) |
| 64 | + if args.class1 or args.all_classes: |
| 65 | + classes.append(1) |
| 66 | + if args.class2 or args.all_classes: |
| 67 | + classes.append(2) |
| 68 | + if args.class3 or args.all_classes: |
| 69 | + classes.append(3) |
| 70 | + |
| 71 | + if args.group is not None and len(classes) > 0: |
| 72 | + logging.warning( |
| 73 | + "Group and class options are mutually exclusive, ignoring class options" |
| 74 | + ) |
| 75 | + |
| 76 | + if args.group is not None: |
| 77 | + group = get_group_name(args.group) |
| 78 | + group = ( |
| 79 | + f"[b]{group}[/] ({args.group})" |
| 80 | + if group is not None |
| 81 | + else str(args.group) |
| 82 | + ) |
| 83 | + |
| 84 | + objects = as_variation0(args.group) |
| 85 | + status = f"Reading data objects from group {group}..." |
| 86 | + else: |
| 87 | + if len(classes) == 0: |
| 88 | + logging.error("No classes specified") |
| 89 | + return |
| 90 | + |
| 91 | + objects = new_class_data_request(*classes) |
| 92 | + status = f"Reading data objects from classes {tuple(classes)}..." |
| 93 | + |
| 94 | + with self.console.status(status): |
| 95 | + apdu = self.master.request(FunctionCode.READ, objects) |
| 96 | + |
| 97 | + if apdu is None: |
| 98 | + logging.error("No response from outstation") |
| 99 | + return |
| 100 | + |
| 101 | + logging.debug( |
| 102 | + "Received response from outstation with code %s", apdu.function.name |
| 103 | + ) |
| 104 | + if apdu.iin is not None: |
| 105 | + if apdu.iin.no_func_code_support: |
| 106 | + return logging.error( |
| 107 | + "Outstation does not support function code %s", |
| 108 | + FunctionCode.READ, |
| 109 | + ) |
| 110 | + |
| 111 | + raw_objects = apdu.objects |
| 112 | + if not raw_objects: |
| 113 | + logging.info("No data returned from outstation") |
| 114 | + return |
| 115 | + |
| 116 | + try: |
| 117 | + objects = unpack_objects(apdu.objects) |
| 118 | + tree = dump_objects(objects) |
| 119 | + self.console.print(tree) |
| 120 | + except Exception as e: |
| 121 | + return logging.error("Failed to unpack objects: %s", e) |
| 122 | + |
| 123 | + |
| 124 | +def cli_main(): |
| 125 | + from icspacket import __version__ |
| 126 | + |
| 127 | + parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter) |
| 128 | + # fmt: off |
| 129 | + group = parser.add_argument_group("Connection Options") |
| 130 | + group.add_argument("-t", "--target", type=str, help="Target host (IP address or hostname) to establish the connection (default port is 20000)", metavar="<link_addr>@<host>[:<port>]", required=True) |
| 131 | + group.add_argument("-l", "--listen", type=int, help="Local link address to use, default is 1", metavar="LINK_ADDR", default=1) |
| 132 | + group.add_argument("--timeout", type=float, metavar="SEC", help="Timeout in seconds for link-level operations (default: None)", default=None) |
| 133 | + |
| 134 | + group = parser.add_argument_group("Request Options") |
| 135 | + group.add_argument("-G", "--group", type=int, help="DNP3 object group number", metavar="ID") |
| 136 | + class0_groups = """\ |
| 137 | + Group Description |
| 138 | + ----- ----------- |
| 139 | + 1 Binary Input |
| 140 | + 3 Double-bit Binary Input |
| 141 | + 10 Binary Output Status |
| 142 | + 20 Counter |
| 143 | + 21 Frozen Counter |
| 144 | + 30 Analog Input |
| 145 | + 31 Frozen Analog Input |
| 146 | + 40 Analog Output Status |
| 147 | + 87 Data Set |
| 148 | + 101 Binary-Coded Dec imal Integer |
| 149 | + 102 Unsigned Integer—8-bit |
| 150 | + 110 Octet String |
| 151 | + 121 Security Statistics |
| 152 | + """ |
| 153 | + group.add_argument("-class0", action="store_true", help=f"Request class 0 objects. The outstation will include some or all of the \nfollowing objects in its response:\n{textwrap.dedent(class0_groups)}\n", default=False) |
| 154 | + class1_groups = """\ |
| 155 | + Group Description |
| 156 | + ----- ----------- |
| 157 | + 2 Binary Input Event |
| 158 | + 4 Double-bit Binary Input Event |
| 159 | + 11 Binary Output Event |
| 160 | + 13 Binary Output Command Event |
| 161 | + 22 Counter Event |
| 162 | + 23 Frozen Counter Event |
| 163 | + 32 Analog Input Event |
| 164 | + 33 Frozen Analog Input Event |
| 165 | + 42 Analog Output Event |
| 166 | + 43 Analog Output Command Event |
| 167 | + 70 File Transfer |
| 168 | + 88 Data Set Event |
| 169 | + 111 Octet String Event |
| 170 | + 113 Virtual Terminal Event |
| 171 | + 120 Authentication |
| 172 | + 122 Security Statistics Event |
| 173 | + """ |
| 174 | + group.add_argument("-class1", action="store_true", help=f"Request class 1 objects. The response will include none (null response), some, \nor all of the following objects:\n{textwrap.dedent(class1_groups)}\n", default=False) |
| 175 | + class2_groups = """-- same as class 1 --""" |
| 176 | + group.add_argument("-class2", action="store_true", help=f"Request class 2 objects. The response will include none (null response), \nsome, or all of the following objects:\n{textwrap.dedent(class2_groups)}", default=False) |
| 177 | + class3_groups = """-- same as class 1 --""" |
| 178 | + group.add_argument("-class3", action="store_true", help=f"Request class 3 objects. The response will include none (null response), \nsome, or all of the following objects:\n{textwrap.dedent(class3_groups)}", default=False) |
| 179 | + group.add_argument("--all-classes", action="store_true", help="Request objects from all classes", default=False) |
| 180 | + # fmt: on |
| 181 | + add_logging_options(parser) |
| 182 | + |
| 183 | + args = parser.parse_args() |
| 184 | + |
| 185 | + logger.init_from_args(args.verbosity, args.quiet, args.ts) |
| 186 | + if args.verbosity > 0: |
| 187 | + print(f"icspacket v{__version__}\n") |
| 188 | + |
| 189 | + master = DNP3_Master(link_addr=args.listen) |
| 190 | + remote_addr = parse_target(args.target) |
| 191 | + if remote_addr is None: |
| 192 | + sys.exit(1) |
| 193 | + |
| 194 | + try: |
| 195 | + logging.info("Connecting to outstation (%04d) at %s:%d...", *remote_addr) |
| 196 | + master.associate(remote_addr) |
| 197 | + master.transport.link.sock.settimeout(args.timeout) |
| 198 | + except ConnectionError as e: |
| 199 | + logging.error("Could not connect to outstation: %s", e) |
| 200 | + sys.exit(1) |
| 201 | + else: |
| 202 | + logging.debug("Successfully connected to outstation (%04d)", remote_addr[0]) |
| 203 | + |
| 204 | + try: |
| 205 | + reader = DNP3Reader(master) |
| 206 | + reader.run(args) |
| 207 | + except KeyboardInterrupt: |
| 208 | + logging.warning("Operation cancelled by user") |
| 209 | + except Exception as e: |
| 210 | + logging.exception("Encountered an unexpected exception:", e) |
| 211 | + finally: |
| 212 | + logging.debug("Disconnecting from outstation...") |
| 213 | + master.release(0.1) |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == "__main__": |
| 217 | + cli_main() |
0 commit comments