|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) 2013, Eduard Broecker |
| 3 | +# With contributions 2025, Gabriele Omodeo Vanone |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that |
| 8 | +# the following conditions are met: |
| 9 | +# |
| 10 | +# Redistributions of source code must retain the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer. |
| 12 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | +# POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +import attr |
| 29 | +import warnings |
| 30 | +import typing |
| 31 | +from canmatrix.exceptions import ArbitrationIdOutOfRange, J1939NeedsExtendedIdentifier |
| 32 | +@attr.s |
| 33 | +class ArbitrationId(object): |
| 34 | + standard_id_mask = ((1 << 11) - 1) |
| 35 | + extended_id_mask = ((1 << 29) - 1) |
| 36 | + compound_extended_mask = (1 << 31) |
| 37 | + |
| 38 | + id = attr.ib(default=None) |
| 39 | + extended = attr.ib(default=False) # type: bool |
| 40 | + |
| 41 | + def __attrs_post_init__(self): |
| 42 | + if self.extended is None: |
| 43 | + # Mimicking old behaviour for now -- remove in the future |
| 44 | + self.extended = True |
| 45 | + warnings.warn( |
| 46 | + "Please set 'extended' attribute as a boolean instead of " |
| 47 | + "None when creating an instance of ArbitrationId class", |
| 48 | + DeprecationWarning |
| 49 | + ) |
| 50 | + if self.extended: |
| 51 | + mask = self.extended_id_mask |
| 52 | + else: |
| 53 | + mask = self.standard_id_mask |
| 54 | + |
| 55 | + if self.id != self.id & mask: |
| 56 | + raise ArbitrationIdOutOfRange('ID out of range') |
| 57 | + |
| 58 | + @property |
| 59 | + def j1939_pgn(self): |
| 60 | + return self.pgn |
| 61 | + |
| 62 | + @property |
| 63 | + def pgn(self): |
| 64 | + if not self.extended: |
| 65 | + raise J1939NeedsExtendedIdentifier |
| 66 | + # PGN is bits 8-25 of the 29-Bit Extended CAN-ID |
| 67 | + # Made up of PDU-S (8-15), PDU-F (16-23), Data Page (24) & Extended Data Page (25) |
| 68 | + # If PDU-F >= 240 the PDU-S is interpreted as Group Extension |
| 69 | + # If PDU-F < 240 the PDU-S is interpreted as a Destination Address |
| 70 | + _pgn = 0 |
| 71 | + if self.j1939_pdu_format == 2: |
| 72 | + _pgn += self.j1939_ps |
| 73 | + _pgn += self.j1939_pf << 8 |
| 74 | + _pgn += self.j1939_dp << 16 |
| 75 | + _pgn += self.j1939_edp << 17 |
| 76 | + |
| 77 | + return _pgn |
| 78 | + |
| 79 | + @pgn.setter |
| 80 | + def pgn(self, value): # type: (int) -> None |
| 81 | + self.extended = True |
| 82 | + _pgn = value & 0x3FFFF |
| 83 | + self.id &= 0xfc0000ff |
| 84 | + self.id |= (_pgn << 8 & 0x3FFFF00) # default pgn is None -> mypy reports error |
| 85 | + |
| 86 | + @property |
| 87 | + def j1939_tuple(self): # type: () -> typing.Tuple[int, int, int] |
| 88 | + """Get tuple (destination, PGN, source) |
| 89 | +
|
| 90 | + :rtype: tuple""" |
| 91 | + |
| 92 | + return self.j1939_destination, self.pgn, self.j1939_source |
| 93 | + |
| 94 | + @property |
| 95 | + def j1939_destination(self): |
| 96 | + if not self.extended: |
| 97 | + raise J1939NeedsExtendedIdentifier |
| 98 | + if self.j1939_pdu_format == 1: |
| 99 | + destination = self.j1939_ps |
| 100 | + else: |
| 101 | + destination = None |
| 102 | + return destination |
| 103 | + |
| 104 | + @property |
| 105 | + def j1939_source(self): |
| 106 | + if not self.extended: |
| 107 | + raise J1939NeedsExtendedIdentifier |
| 108 | + return self.id & 0xFF |
| 109 | + |
| 110 | + @j1939_source.setter |
| 111 | + def j1939_source(self, value): # type: (int) -> None |
| 112 | + self.extended = True |
| 113 | + self.id = (self.id & 0xffffff00) | (value & 0xff) |
| 114 | + |
| 115 | + @property |
| 116 | + def j1939_ps(self): |
| 117 | + if not self.extended: |
| 118 | + raise J1939NeedsExtendedIdentifier |
| 119 | + return (self.id >> 8) & 0xFF |
| 120 | + |
| 121 | + @property |
| 122 | + def j1939_pf(self): |
| 123 | + if not self.extended: |
| 124 | + raise J1939NeedsExtendedIdentifier |
| 125 | + return (self.id >> 16) & 0xFF |
| 126 | + |
| 127 | + @property |
| 128 | + def j1939_pdu_format(self): |
| 129 | + return 1 if (self.j1939_pf < 240) else 2 |
| 130 | + |
| 131 | + @property |
| 132 | + def j1939_dp(self): |
| 133 | + if not self.extended: |
| 134 | + raise J1939NeedsExtendedIdentifier |
| 135 | + return (self.id >> 24) & 0x1 |
| 136 | + |
| 137 | + @property |
| 138 | + def j1939_edp(self): |
| 139 | + if not self.extended: |
| 140 | + raise J1939NeedsExtendedIdentifier |
| 141 | + return (self.id >> 25) & 0x1 |
| 142 | + |
| 143 | + @property |
| 144 | + def j1939_priority(self): |
| 145 | + if not self.extended: |
| 146 | + raise J1939NeedsExtendedIdentifier |
| 147 | + return (self.id >> 26) & 0x7 |
| 148 | + |
| 149 | + @j1939_priority.setter |
| 150 | + def j1939_priority(self, value): # type: (int) -> None |
| 151 | + self.extended = True |
| 152 | + self.id = (self.id & 0x3ffffff) | ((value & 0x7) << 26) |
| 153 | + |
| 154 | + @property |
| 155 | + def j1939_str(self): # type: () -> str |
| 156 | + return "DA:0x{da:02X} PGN:0x{pgn:04X} SA:0x{sa:02X}".format( |
| 157 | + da=self.j1939_destination, pgn=self.pgn, sa=self.j1939_source) |
| 158 | + |
| 159 | + |
| 160 | + @classmethod |
| 161 | + def from_compound_integer(cls, i): # type: (typing.Any) -> ArbitrationId |
| 162 | + return cls( |
| 163 | + id=i & cls.extended_id_mask, |
| 164 | + extended=(i & cls.compound_extended_mask) != 0, |
| 165 | + ) |
| 166 | + |
| 167 | + @classmethod |
| 168 | + def from_pgn(cls, pgn): # type: (int) -> ArbitrationId |
| 169 | + return cls( |
| 170 | + id = (pgn << 8), extended = True |
| 171 | + ) |
| 172 | + |
| 173 | + def to_compound_integer(self): |
| 174 | + if self.extended: |
| 175 | + return self.id | self.compound_extended_mask |
| 176 | + else: |
| 177 | + return self.id |
| 178 | + |
| 179 | + def __eq__(self, other): |
| 180 | + return ( |
| 181 | + self.id == other.id |
| 182 | + and ( |
| 183 | + self.extended is None |
| 184 | + or other.extended is None |
| 185 | + or self.extended == other.extended |
| 186 | + ) |
| 187 | + ) |
0 commit comments