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
def test_context_tag(self): if _debug: TestContextTag._debug("test_context_tag") # test context tag construction tag = ContextTag(0, xtob('')) with self.assertRaises(ValueError): tag = ContextTag() # test encoding and decoding context_endec(0, '', '08') context_endec(1, '01', '1901') context_endec(2, '0102', '2A0102') context_endec(3, '010203', '3B010203') context_endec(14, '010203', 'EB010203') context_endec(15, '010203', 'FB0F010203')
def context_decode(blob): """Build PDU from the string, decode the tag, convert to an object.""" if _debug: context_decode._debug("context_decode %r", blob) data = PDUData(blob) tag = ContextTag(data) return tag
def test_endec_2(self): """Test short tag list encoding and decoding, context tags. """ if _debug: TestTagList._debug("test_endec_2") tag0 = ContextTag(0, xtob('00')) tag1 = ContextTag(1, xtob('01')) taglist = TagList([tag0, tag1]) data = PDUData() taglist.encode(data) assert data.pduData == xtob('09001901') taglist = TagList() taglist.decode(data) assert taglist.tagList == [tag0, tag1]
def test_boolean_application_to_context(self): if _debug: TestApplicationTag._debug("test_boolean_application_to_context") # create an application tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0) if _debug: TestApplicationTag._debug(" - tag: %r", tag_tuple(tag)) # convert it to context tagged, context 0 ctag = tag.app_to_context(0) if _debug: TestApplicationTag._debug(" - ctag: %r", tag_tuple(ctag)) # create a context tag with the same shape ttag = ContextTag(0, xtob('00')) if _debug: TestApplicationTag._debug(" - ttag: %r", tag_tuple(ttag)) # check to see they are the same assert ctag == ttag # convert the context tag back to an application tag dtag = ctag.context_to_app(Tag.booleanAppTag) if _debug: TestApplicationTag._debug(" - dtag: %r", tag_tuple(dtag)) # check to see it round-tripped assert dtag == tag
def context_endec(tnum, x, y): """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: context_endec._debug("context_endec %r %r %r", tnum, x, y) # convert the hex strings to a blobs tdata = xtob(x) blob1 = xtob(y) # make a context tag tag1 = ContextTag(tnum, tdata) # decode the blob into a tag tag2 = context_decode(blob1) if _debug: context_endec._debug(" - tag: %r", tag) # encode the tag into a blob blob2 = context_encode(tag1) if _debug: context_endec._debug(" - blob2: %r", blob2) # compare the results assert tag1 == tag2 assert blob1 == blob2
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)
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)