Beispiel #1
0
    def to_bytes(self) -> bytes:
        rlrq_data = bytearray()
        # default value of protocol_version is 1. Only decode if other than 1

        if self.reason is not None:
            rlrq_data.extend(BER.encode(0x80, self.reason.value.to_bytes(1, "big")))
        if self.user_information is not None:
            rlrq_data.extend(BER.encode(0xBE, self.user_information.to_bytes()))
        return BER.encode(self.TAG, rlrq_data)
Beispiel #2
0
 def from_bytes(cls, source_bytes: bytes):
     tag, length, data = BER.decode(source_bytes)
     if tag != cls.TAG.to_bytes(1, "big"):
         raise ValueError(
             f"Data provided is not of the correct type. Tag is {tag} but should "
             f"be {cls.TAG}"
         )
     return cls(value=int.from_bytes(data, byteorder="big"))
Beispiel #3
0
    def from_bytes(cls, _bytes):
        tag, length, data = BER.decode(_bytes)
        if tag == b"\x80":
            password_type = "chars"
        elif tag == b"\x81":
            password_type = "bits"
        else:
            raise ValueError(f"Tag {tag} is not vaild for password")

        return cls(password=data, password_type=password_type)
Beispiel #4
0
    def from_bytes(cls, _bytes):
        tag, length, data = BER.decode(_bytes)

        if tag != DLMSObjectIdentifier.TAG:
            raise ValueError(f"Tag of {tag} is not a valid tag for "
                             f"ObjectIdentifiers")

        context_id = data[-1]
        if context_id not in AppContextName.valid_context_ids:
            raise ValueError(f"context_id of {context_id} is not valid")

        total_prefix = bytes(data[:-1])
        if total_prefix != (DLMSObjectIdentifier.PREFIX +
                            bytes([AppContextName.app_context])):
            raise ValueError(f"Static part of object id it is not correct"
                             f" according to DLMS: {total_prefix}")
        settings_dict = AppContextName.get_settings_by_context_id(context_id)
        return cls(**settings_dict)
Beispiel #5
0
    def to_bytes(self):
        aarq_data = bytearray()
        # There is no need to encode the version since it is always v1. (int 0)
        if self.application_context_name is not None:
            aarq_data.extend(
                BER.encode(161, self.application_context_name.to_bytes()))
        if self.called_ap_title is not None:
            aarq_data.extend(BER.encode(162, self.called_ap_title))
        if self.called_ae_qualifier is not None:
            aarq_data.extend(BER.encode(163, self.called_ae_qualifier))
        if self.called_ap_invocation_identifier is not None:
            aarq_data.extend(
                BER.encode(164, self.called_ap_invocation_identifier))
        if self.called_ae_invocation_identifier is not None:
            aarq_data.extend(
                BER.encode(165, self.called_ae_invocation_identifier))
        if self.system_title is not None:
            aarq_data.extend(BER.encode(166, BER.encode(4, self.system_title)))
        if self.public_cert is not None:
            aarq_data.extend(BER.encode(167, BER.encode(4, self.public_cert)))
        if self.calling_ap_invocation_identifier is not None:
            aarq_data.extend(
                BER.encode(168, self.calling_ap_invocation_identifier))
        if self.calling_ae_invocation_identifier is not None:
            aarq_data.extend(
                BER.encode(169, self.calling_ae_invocation_identifier))
        if self.sender_acse_requirements is not None:
            aarq_data.extend(
                BER.encode(0x8A, self.sender_acse_requirements.to_bytes()))
        if self.mechanism_name is not None:
            aarq_data.extend(BER.encode(0x8B, self.mechanism_name.to_bytes()))
        if self.authentication_value is not None:
            aarq_data.extend(
                BER.encode(
                    0xAC,
                    acse_base.AuthenticationValue(
                        password=self.authentication_value).to_bytes(),
                ))
        if self.implementation_information is not None:
            aarq_data.extend(BER.encode(0xBD, self.implementation_information))
        if self.user_information is not None:
            aarq_data.extend(BER.encode(0xBE,
                                        self.user_information.to_bytes()))

        return BER.encode(self.TAG, aarq_data)
Beispiel #6
0
 def to_bytes(self):
     return BER.encode(self.TAG, self.value.to_bytes(1, byteorder="big"))
Beispiel #7
0
    def to_bytes(self) -> bytes:

        aare_data = bytearray()
        # default value of protocol_version is 1. Only decode if other than 1
        # No need to use protocol version
        # if self.protocol_version != 1:
        #    aare_data.extend(BER.encode(160, bytes(self.protocol_version)))
        if self.application_context_name is not None:
            aare_data.extend(BER.encode(161, self.application_context_name.to_bytes()))
        if self.result is not None:
            aare_data.extend(
                BER.encode(162, Asn1Integer(value=self.result.value).to_bytes())
            )
        if self.result_source_diagnostics is not None:
            if isinstance(
                self.result_source_diagnostics, enumerations.AcseServiceUserDiagnostics
            ):
                aare_data.extend(
                    BER.encode(
                        163,
                        ResultSourceDiagnostics(
                            name="acse-service-user",
                            value=self.result_source_diagnostics.value,
                        ).dump(),
                    )
                )
            elif isinstance(
                self.result_source_diagnostics,
                enumerations.AcseServiceProviderDiagnostics,
            ):
                aare_data.extend(
                    BER.encode(
                        163,
                        ResultSourceDiagnostics(
                            name="acse-service-provider",
                            value=self.result_source_diagnostics.value,
                        ).dump(),
                    )
                )

        if self.system_title is not None:
            aare_data.extend(BER.encode(164, BER.encode(4, self.system_title)))
        if self.public_cert is not None:
            aare_data.extend(BER.encode(165, BER.encode(4, self.public_cert)))
        if self.responding_ap_invocation_id is not None:
            aare_data.extend(BER.encode(166, self.responding_ap_invocation_id))
        if self.responding_ae_invocation_id is not None:
            aare_data.extend(BER.encode(167, self.responding_ae_invocation_id))
        if self.responder_acse_requirements is not None:
            aare_data.extend(
                BER.encode(0x88, self.responder_acse_requirements.to_bytes())
            )
        if self.mechanism_name is not None:
            aare_data.extend(BER.encode(0x89, self.mechanism_name.to_bytes()))
        if self.authentication_value is not None:
            aare_data.extend(
                BER.encode(
                    170,
                    acse_base.AuthenticationValue(
                        password=self.authentication_value
                    ).to_bytes(),
                )
            )

        if self.implementation_information is not None:
            aare_data.extend(BER.encode(189, self.implementation_information))
        if self.user_information is not None:
            aare_data.extend(BER.encode(0xBE, self.user_information.to_bytes()))

        return BER.encode(self.TAG, aare_data)
Beispiel #8
0
 def to_bytes(self):
     total_data = self.PREFIX + bytes([self.app_context, self.context_id])
     return BER.encode(self.TAG, total_data)
Beispiel #9
0
 def to_bytes(self):
     if self.password_type == "chars":
         return BER.encode(0x80, self.password)
     elif self.password_type == "bits":
         return BER.encode(0x81, self.password)