Example #1
0
    def test_get_context(self):
        """Test extracting specific context encoded content.
        """
        if _debug: TestTagList._debug("test_get_context")

        tag_list_data = [
            ContextTag(0, xtob('00')),
            ContextTag(1, xtob('01')),
            OpeningTag(2),
            IntegerTag(3),
            OpeningTag(0),
            IntegerTag(4),
            ClosingTag(0),
            ClosingTag(2),
        ]
        taglist = TagList(tag_list_data)

        # known to be a simple context encoded element
        context_0 = taglist.get_context(0)
        if _debug: TestTagList._debug("    - context_0: %r", context_0)
        assert context_0 == tag_list_data[0]

        # known to be a simple context encoded list of element(s)
        context_2 = taglist.get_context(2)
        if _debug: TestTagList._debug("    - context_2: %r", context_2)
        assert context_2.tagList == tag_list_data[3:7]

        # known missing context
        context_3 = taglist.get_context(3)
        if _debug: TestTagList._debug("    - context_3: %r", context_3)
        assert taglist.get_context(3) is None
Example #2
0
def closing_decode(blob):
    """Build PDU from the string, decode the tag, convert to an object."""
    if _debug: closing_decode._debug("closing_decode %r", blob)

    data = PDUData(blob)
    tag = ClosingTag(data)
    return tag
Example #3
0
    def test_closing_tag(self):
        if _debug: TestClosingTag._debug("test_closing_tag")

        # test closing tag construction
        tag = ClosingTag(0)
        with self.assertRaises(TypeError):
            tag = ClosingTag()

        # test encoding, and decoding
        closing_endec(0, '0F')
        closing_endec(1, '1F')
        closing_endec(2, '2F')
        closing_endec(3, '3F')
        closing_endec(14, 'EF')
        closing_endec(15, 'FF0F')
        closing_endec(254, 'FFFE')
Example #4
0
    def test_tag_list(self):
        if _debug: TestExtendedTagList._debug("test_tag_list")

        compare_tag_list("""
            opening tag 1
            integer 2
            closing tag 3
            """,
            OpeningTag(1),
            tag_encode(Integer(2)),
            ClosingTag(3),
            )
Example #5
0
    def test_endec_3(self):
        """Test bracketed application tagged integer encoding and decoding."""
        if _debug: TestTagList._debug("test_endec_2")

        tag0 = OpeningTag(0)
        tag1 = IntegerTag(0x0102)
        tag2 = ClosingTag(0)
        taglist = TagList([tag0, tag1, tag2])

        data = PDUData()
        taglist.encode(data)
        assert data.pduData == xtob('0E3201020F')

        taglist = TagList()
        taglist.decode(data)
        assert taglist.tagList == [tag0, tag1, tag2]
Example #6
0
def closing_endec(tnum, x):
    """Convert the value (a primitive object) to a hex encoded string, 
    convert the hex encoded string to and object, and compare the results to
    each other."""
    if _debug: closing_endec._debug("closing_endec %r %r", tnum, x)

    # convert the hex string to a blob
    blob1 = xtob(x)

    # make a context tag
    tag1 = ClosingTag(tnum)
    if _debug: closing_endec._debug("    - tag1: %r", tag1)

    # decode the blob into a tag
    tag2 = closing_decode(blob1)
    if _debug: closing_endec._debug("    - tag2: %r", tag2)

    # encode the tag into a blob
    blob2 = closing_encode(tag1)
    if _debug: closing_endec._debug("    - blob2: %r", blob2)

    # compare the results
    assert tag1 == tag2
    assert blob1 == blob2
Example #7
0
    def do_write(self, args):
        """write <addr> <objid> <prop> [ <indx> ]"""
        args = args.split()
        if _debug: WriteSomethingConsoleCmd._debug("do_write %r", args)

        try:
            addr, obj_id, prop_id = args[:3]
            obj_id = ObjectIdentifier(obj_id).value
            if prop_id.isdigit():
                prop_id = int(prop_id)

            # build a request
            request = WritePropertyRequest(
                objectIdentifier=obj_id,
                propertyIdentifier=prop_id,
            )
            request.pduDestination = Address(addr)

            if len(args) == 4:
                request.propertyArrayIndex = int(args[3])

            # build a custom datastructure
            tag_list = TagList([
                OpeningTag(1),
                ContextTag(0, xtob('9c40')),
                ContextTag(1, xtob('02')),
                ContextTag(2, xtob('02')),
                ClosingTag(1)
            ])
            if _debug:
                WriteSomethingConsoleCmd._debug("    - tag_list: %r", tag_list)

            # stuff the tag list into an Any
            request.propertyValue = Any()
            request.propertyValue.decode(tag_list)

            if _debug:
                WriteSomethingConsoleCmd._debug("    - request: %r", request)

            # make an IOCB
            iocb = IOCB(request)
            if _debug: WriteSomethingConsoleCmd._debug("    - iocb: %r", iocb)

            # give it to the application
            deferred(this_application.request_io, iocb)

            # wait for it to complete
            iocb.wait()

            # do something for success
            if iocb.ioResponse:
                # should be an ack
                if not isinstance(iocb.ioResponse, SimpleAckPDU):
                    if _debug:
                        WriteSomethingConsoleCmd._debug("    - not an ack")
                    return

                sys.stdout.write("ack\n")

            # do something for error/reject/abort
            if iocb.ioError:
                sys.stdout.write(str(iocb.ioError) + '\n')

        except Exception as error:
            WriteSomethingConsoleCmd._exception("exception: %r", error)
    def do_write(self, args):
        """write <addr> <type> <inst> <prop> [ <indx> ]"""
        args = args.split()
        if _debug: WriteSomethingConsoleCmd._debug("do_write %r", args)

        try:
            addr = args[0]

            obj_type = 162  #int(obj_type)
            obj_inst = 1  #int(obj_inst)
            prop_id = 1034  #int(prop_id)
            idx = 2

            # build a request
            request = WritePropertyRequest(
                objectIdentifier=(obj_type, obj_inst),
                propertyIdentifier=prop_id,
            )
            request.pduDestination = Address(addr)

            if len(args) >= 5:
                request.propertyArrayIndex = 2  #int(args[4])

            request.propertyArrayIndex = idx

            if len(args) == 6:
                #convert ip to byte array and then to hex string
                proxy = bytearray(args[5])
                proxy_hex = str(proxy).encode('hex')

            # build a custom data structure... BACnet settings BCP object IP BBMD Foreign port eTCH 3.40
            # Context #0 inside Opening tag #9 is the IP Type 00=Regular, 01=Foreign, 02=BBMD
            # Context #2 inside Opening tag #9 is the Foreign IP in hex
            # Context #4 inside Opening tag #9 is the Proxy IP in hex
            tag_list = TagList([
                OpeningTag(0),
                ContextTag(0, xtob('19')),
                ContextTag(1, xtob('01')),
                ClosingTag(0),
                ContextTag(1, xtob('01')),
                ContextTag(2, xtob('00')),
                ContextTag(3, xtob('9c40')),
                ContextTag(4, xtob('00')),
                OpeningTag(5),
                ContextTag(0, xtob('00')),
                ContextTag(1, xtob('00')),
                ClosingTag(5),
                ContextTag(6, xtob('00')),
                ContextTag(7, xtob('00')),
                OpeningTag(8),
                ContextTag(0, xtob('00')),
                ContextTag(1, xtob('00')),
                ContextTag(2, xtob('00')),
                ContextTag(3, xtob('00')),
                ContextTag(4, xtob('00')),
                ContextTag(5, xtob('00')),
                ContextTag(6, xtob('00')),
                ContextTag(7, xtob('00')),
                ContextTag(8, xtob('ffffffff')),
                ClosingTag(8),
                OpeningTag(9),
                ContextTag(0, xtob('02')),
                ContextTag(1, xtob('bac0')),
                ContextTag(2, xtob('00')),
                ContextTag(3, xtob('3c')),
                ContextTag(4, xtob('480c600c')),
                ContextTag(5, xtob('00')),
                ContextTag(6, xtob('ffffffff')),
                ClosingTag(9),
                ContextTag(10, xtob('00')),
                ContextTag(11, xtob('00')),
                ContextTag(12, xtob('00')),
                ContextTag(13, xtob('00000000'))
            ])
            if _debug:
                WriteSomethingConsoleCmd._debug("    - tag_list: %r", tag_list)

            # stuff the tag list into an Any
            request.propertyValue = Any()
            request.propertyValue.decode(tag_list)

            if _debug:
                WriteSomethingConsoleCmd._debug("    - request: %r", request)

            # make an IOCB
            iocb = IOCB(request)
            if _debug: WriteSomethingConsoleCmd._debug("    - iocb: %r", iocb)

            # give it to the application
            this_application.request_io(iocb)

            # wait for it to complete
            iocb.wait()

            # do something for success
            if iocb.ioResponse:
                # should be an ack
                if not isinstance(iocb.ioResponse, SimpleAckPDU):
                    if _debug:
                        WriteSomethingConsoleCmd._debug("    - not an ack")
                    return

                sys.stdout.write("ack\n")

            # do something for error/reject/abort
            if iocb.ioError:
                sys.stdout.write(str(iocb.ioError) + '\n')

        except Exception as error:
            WriteSomethingConsoleCmd._exception("exception: %r", error)
Example #9
0
    def test_opening_closing_statements(self):
        if _debug: TestExtendedTagStatements._debug("test_opening_closing_statements")

        # test individual statements
        assert statement_to_tag("opening tag 1") == OpeningTag(1)
        assert statement_to_tag("closing tag 1") == ClosingTag(1)
Example #10
0
    def do_write(self, args):
        """write <addr> <type> <inst> <prop> [ <indx> ]"""
        args = args.split()
        if _debug: WriteSomethingConsoleCmd._debug("do_write %r", args)

        try:
            addr, obj_type, obj_inst, prop_id = args[:4]

            obj_type = int(obj_type)
            obj_inst = int(obj_inst)
            prop_id = int(prop_id)

            # build a request
            request = WritePropertyRequest(
                objectIdentifier=(obj_type, obj_inst),
                propertyIdentifier=prop_id,
            )
            request.pduDestination = Address(addr)

            if len(args) >= 5:
                request.propertyArrayIndex = int(args[4])

            if len(args) == 6:
                #convert ip to byte array and then to hex string
                proxy = bytearray(args[5])
                proxy_hex = str(proxy).encode('hex')

            # build a custom datastructure... BACnet settings IP BBMD Foreign port DSC 3.40
            tag_list = TagList([
                OpeningTag(1),
                ContextTag(0, xtob('9ca4')),
                ContextTag(1, xtob('02')),
                ContextTag(2, xtob('02')),
                ContextTag(3, xtob('003030302e3030302e3030302e303030')),
                ContextTag(4, xtob('3c')),
                ContextTag(5, xtob("00" + proxy_hex)),
                ContextTag(6, xtob('00')),
                ContextTag(7, xtob('00')),
                ContextTag(8, xtob('00')),
                ContextTag(9, xtob('bac0')),
                ContextTag(10, xtob('00')),
                ContextTag(11, xtob('00')),
                ContextTag(12, xtob('00')),
                ContextTag(13, xtob('ffffffff')),
                ContextTag(14, xtob('00')),
                ContextTag(15, xtob('00')),
                ContextTag(16, xtob('00')),
                ContextTag(17, xtob('00')),
                ClosingTag(1)
            ])
            if _debug:
                WriteSomethingConsoleCmd._debug("    - tag_list: %r", tag_list)

            # stuff the tag list into an Any
            request.propertyValue = Any()
            request.propertyValue.decode(tag_list)

            if _debug:
                WriteSomethingConsoleCmd._debug("    - request: %r", request)

            # make an IOCB
            iocb = IOCB(request)
            if _debug: WriteSomethingConsoleCmd._debug("    - iocb: %r", iocb)

            # give it to the application
            this_application.request_io(iocb)

            # wait for it to complete
            iocb.wait()

            # do something for success
            if iocb.ioResponse:
                # should be an ack
                if not isinstance(iocb.ioResponse, SimpleAckPDU):
                    if _debug:
                        WriteSomethingConsoleCmd._debug("    - not an ack")
                    return

                sys.stdout.write("ack\n")

            # do something for error/reject/abort
            if iocb.ioError:
                sys.stdout.write(str(iocb.ioError) + '\n')

        except Exception as error:
            WriteSomethingConsoleCmd._exception("exception: %r", error)
Example #11
0
def closing_tag_statement(value):
    if _debug: closing_tag_statement._debug("closing_tag_statement %r", value)

    return ClosingTag(int(value))