def iam(self): """ Creation of a iam request Iam requests are sent when whois requests ask for it Content is defined by the script (deviceId, vendor, etc...) :returns: bool Example:: iam() """ if _debug: WhoisIAm._debug("do_iam") try: # build a request request = IAmRequest() request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = self.this_device.objectIdentifier request.maxAPDULengthAccepted = self.this_device.maxApduLengthAccepted request.segmentationSupported = self.this_device.segmentationSupported request.vendorID = self.this_device.vendorIdentifier if _debug: WhoisIAm._debug(" - request: %r" % request) # give it to the application self.this_application.request(request) return True except Exception as e: WhoisIAm._exception("exception: %r" % e) return False
def do_iam(self, args): """iam""" args = args.split() if _debug: WhoIsIAmConsoleCmd._debug("do_iam %r", args) try: # build a request request = IAmRequest() request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = this_device.objectIdentifier request.maxAPDULengthAccepted = this_device.maxApduLengthAccepted request.segmentationSupported = this_device.segmentationSupported request.vendorID = this_device.vendorIdentifier if _debug: WhoIsIAmConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: WhoIsIAmConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) except Exception as err: WhoIsIAmConsoleCmd._exception("exception: %r", err)
def test_whoIs(self): request = WhoIsRequest( deviceInstanceRangeLowLimit=500, deviceInstanceRangeHighLimit=50000 ) apdu = APDU() request.encode(apdu) pdu = PDU() apdu.encode(pdu) buf_size = 1024 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.sendto(pdu.pduData, self.address) data = s.recvfrom(buf_size) s.close() received_data = data[0] expected = IAmRequest() expected.pduDestination = GlobalBroadcast() expected.iAmDeviceIdentifier = 36113 expected.maxAPDULengthAccepted = 1024 expected.segmentationSupported = "segmentedBoth" expected.vendorID = 15 exp_apdu = APDU() expected.encode(exp_apdu) exp_pdu = PDU() exp_apdu.encode(exp_pdu) self.assertEqual(exp_pdu.pduData, received_data)
def whoIs(self, request, address, invoke_key, device): # Limits are optional (but if used, must be paired) execute = False try: if (request.deviceInstanceRangeLowLimit is not None) and \ (request.deviceInstanceRangeHighLimit is not None): if (request.deviceInstanceRangeLowLimit > list( self.objectIdentifier.keys())[0][1] > request.deviceInstanceRangeHighLimit): logger.info('Bacnet WhoHasRequest out of range') else: execute = True else: execute = True except AttributeError: execute = True if execute: self._response_service = 'IAmRequest' self._response = IAmRequest() self._response.pduDestination = GlobalBroadcast() self._response.iAmDeviceIdentifier = self.deviceIdentifier # self._response.objectIdentifier = list(self.objectIdentifier.keys())[0][1] self._response.maxAPDULengthAccepted = int( getattr(self.localDevice, 'maxApduLengthAccepted')) self._response.segmentationSupported = getattr( self.localDevice, 'segmentationSupported') self._response.vendorID = int( getattr(self.localDevice, 'vendorIdentifier'))
def iam(self): """ Build an IAm response. IAm are sent in response to a WhoIs request that; matches our device ID, whose device range includes us, or is a broadcast. Content is defined by the script (deviceId, vendor, etc...) :returns: bool Example:: iam() """ log_debug(WhoisIAm, "do_iam") try: # build a response request = IAmRequest() request.pduDestination = GlobalBroadcast() # fill the response with details about us (from our device object) request.iAmDeviceIdentifier = self.this_device.objectIdentifier request.maxAPDULengthAccepted = self.this_device.maxApduLengthAccepted request.segmentationSupported = self.this_device.segmentationSupported request.vendorID = self.this_device.vendorIdentifier log_debug(WhoisIAm, " - request: %r" % request) iocb = self.this_application.request(request) # pass to the BACnet stack iocb.wait() return True except Exception as error: log_exception("exception: %r" % error) return False
def do_iam(self, args): """ iam [ <addr> ] Send an I-Am request. If the address is unspecified the message is locally broadcast. """ args = args.split() if _debug: DiscoverConsoleCmd._debug("do_iam %r", args) try: # build a request request = IAmRequest() if (len(args) == 1): request.pduDestination = Address(args[0]) else: request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = this_device.objectIdentifier request.maxAPDULengthAccepted = this_device.maxApduLengthAccepted request.segmentationSupported = this_device.segmentationSupported request.vendorID = this_device.vendorIdentifier if _debug: DiscoverConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: DiscoverConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) except Exception as err: DiscoverConsoleCmd._exception("exception: %r", err)
def do_iam(self, args): """iam [ <addr> ]""" args = args.split() if _debug: TestConsoleCmd._debug("do_iam %r", args) try: # build a request request = IAmRequest() if (len(args) == 1): request.pduDestination = Address(args[0]) del args[0] else: request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = this_device.objectIdentifier request.maxAPDULengthAccepted = this_device.maxApduLengthAccepted request.segmentationSupported = this_device.segmentationSupported request.vendorID = this_device.vendorIdentifier if _debug: TestConsoleCmd._debug(" - request: %r", request) # give it to the application this_application.request(request) except Exception, e: TestConsoleCmd._exception("exception: %r", e)
def _iam_request(self, destination=None): """ Build the IOCB request for a I Am """ try: # build a response request = IAmRequest() request.pduDestination = destination if destination else GlobalBroadcast( ) # fill the response with details about us (from our device object) request.iAmDeviceIdentifier = self.this_device.objectIdentifier request.maxAPDULengthAccepted = self.this_device.maxApduLengthAccepted request.segmentationSupported = self.this_device.segmentationSupported request.vendorID = self.this_device.vendorIdentifier self._log.debug("{:>12} {}".format("- request:", request)) return request except Exception as error: self._log.error("exception: {!r}".format(error)) raise
def do_iam(self, args): """iam""" args = args.split() if _debug: TestConsoleCmd._debug("do_iam %r", args) try: # build a request request = IAmRequest() request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = vlan_device_1.objectIdentifier request.maxAPDULengthAccepted = vlan_device_1.maxApduLengthAccepted request.segmentationSupported = vlan_device_1.segmentationSupported request.vendorID = vlan_device_1.vendorIdentifier if _debug: TestConsoleCmd._debug(" - request: %r", request) # give it to the application vlan_app_1.request(request) except Exception, e: TestConsoleCmd._exception("exception: %r", e)
def do_iam(self, args): """iam""" args = args.split() if _debug: self._debug("do_iam %r", args) try: # build a request request = IAmRequest() request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = self.objectIdentifier request.maxAPDULengthAccepted = self.maxApduLengthAccepted request.segmentationSupported = self.segmentationSupported request.vendorID = self.vendorIdentifier if _debug: self._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: self._debug(" - iocb: %r", iocb) # give it to the application self.request_io(iocb) # build a key from the source, just use the instance number # Cache the key key = ( str(request.pduSource), request.iAmDeviceIdentifier[1], ) try: self.iam_counter[key] += 1 except KeyError: self.iam_counter[key] = 1 except Exception as err: self._exception("exception: %r", err)
# pass the device object to the state machine access point so it # can know if it should support segmentation test_smap = StateMachineAccessPoint(test_device) # state machine test_server = ServerStateMachine() # bind everything together bind(test_application, test_asap, test_smap, test_server) # ============================================================================== i_am_request = IAmRequest( iAmDeviceIdentifier=('device', 100), maxAPDULengthAccepted=1024, segmentationSupported='segmentedBoth', vendorID=15, ) print("i_am_request") i_am_request.debug_contents() print("") test_apdu = APDU() i_am_request.encode(test_apdu) print("test_apdu") test_apdu.debug_contents() print("") print("modify test_apdu") test_apdu.pduData = test_apdu.pduData[5:]