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 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 [ <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 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, ('127.0.0.1', self.bacnet_server.server.server_port)) data = s.recvfrom(buf_size) 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.assertEquals(exp_pdu.pduData, received_data)
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(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 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() """ self._log.debug("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 self._log.debug("{:>12} {}".format("- request:", request)) iocb = self.this_application.request(request) # pass to the BACnet stack iocb.wait() return True except Exception as error: self._log.error("exception: {!r}".format(error)) 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: WriteSomethingConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) except Exception as err: WhoIsIAmConsoleCmd._exception("exception: %r", err)
def iam_request(args, console=None): """ This function creates a iam request. Usage: iam [ <address> ] :param args: list of parameters :param console: console object :return: request """ # check if console was provided if console is None: raise ValueError('console not found') address = None # check if too many arguments are defined if len(args) > 1: raise ValueError('too many arguments') elif len(args) == 1: # read address address = args[0] # create request request = IAmRequest() # check if address was specified if address is None: # send broadcast request.pduDestination = GlobalBroadcast() else: # send to specified address request.pduDestination = Address(address) # setup device parameters request.iAmDeviceIdentifier = console.device.objectIdentifier request.maxAPDULengthAccepted = console.device.maxApduLengthAccepted request.segmentationSupported = console.device.segmentationSupported request.vendorID = console.device.vendorIdentifier # return created request return request
def do_WhoIsRequest(self, apdu): """ This function responses to WhoIs requests. :param apdu: incoming message :return: None """ self._debug('do_WhoIsRequest %r', apdu) # check if restrictions exist if apdu.deviceInstanceRangeLowLimit is not None and \ apdu.deviceInstanceRangeHighLimit is not None: # get device identifier device_id = self.localDevice.objectIdentifier # check if identifier is of class object identifier if hasattr(device_id, 'get_tuple'): # make device identifier tuple device_id = device_id.get_tuple() # check if device id is below limit if device_id[1] < apdu.deviceInstanceRangeLowLimit or \ device_id[1] > apdu.deviceInstanceRangeHighLimit: # exit return # create IAm request request = IAmRequest() request.pduDestination = apdu.pduSource request.iAmDeviceIdentifier = self.localDevice.objectIdentifier request.maxAPDULengthAccepted = self.localDevice.maxApduLengthAccepted request.segmentationSupported = self.localDevice.segmentationSupported request.vendorID = self.localDevice.vendorIdentifier self._debug(' - request: %r', request) # send request to request queue - IAm is not a response! self.requests.put(request) # return return
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: BacnetConsoleCmd._debug("do_iam %r", args) try: # build a request request = IAmRequest() request.pduDestination = GlobalBroadcast() # set the parameters from the device object request.iAmDeviceIdentifier = thisDevice.objectIdentifier request.maxAPDULengthAccepted = thisDevice.maxApduLengthAccepted request.segmentationSupported = thisDevice.segmentationSupported request.vendorID = thisDevice.vendorIdentifier if _debug: BacnetConsoleCmd._debug(" - request: %r", request) # give it to the application thisApplication.request(request) except Exception, e: BacnetConsoleCmd._exception("exception: %r", e)
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)