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 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> ] 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: ClientConsoleCmd._debug("do_iam %r", args) global this_device 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: ClientConsoleCmd._debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: ClientConsoleCmd._debug(" - iocb: %r", iocb) # give it to the application this_application.request_io(iocb) except Exception as err: ClientConsoleCmd._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 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 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 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 _iam_request(self, destination=None): """ Build the IOCB request for a I Am """ try: # build a response request = IAmRequest() if destination: request.pduDestination = destination else: 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)) 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: 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) # give it to the application this_application.request(request) except Exception as err: WhoIsIAmConsoleCmd._exception("exception: %r", err)
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 do_iam(self): """iam""" 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: logger.debug(" - request: %r", request) # make an IOCB iocb = IOCB(request) if _debug: logger.debug(" - iocb: %r", iocb) # give it to the application self.request_io(iocb) except Exception as err: logger.debug("exception: %r", err)
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)
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)
# 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:]