Merge pull request #20 from marrold/Toggle_validation
Refactor validation to optimise code
This commit is contained in:
		
						commit
						290cfa2ac0
					
				@ -102,7 +102,7 @@ def build_config(_config_file):
 | 
				
			|||||||
                    CONFIG['SYSTEMS'].update({section: {
 | 
					                    CONFIG['SYSTEMS'].update({section: {
 | 
				
			||||||
                        'MODE': config.get(section, 'MODE'),
 | 
					                        'MODE': config.get(section, 'MODE'),
 | 
				
			||||||
                        'ENABLED': config.getboolean(section, 'ENABLED'),
 | 
					                        'ENABLED': config.getboolean(section, 'ENABLED'),
 | 
				
			||||||
                        'STRICT': config.getboolean(section, 'STRICT'),
 | 
					                        'LOOSE': config.getboolean(section, 'LOOSE'),
 | 
				
			||||||
                        'EXPORT_AMBE': config.getboolean(section, 'EXPORT_AMBE'),
 | 
					                        'EXPORT_AMBE': config.getboolean(section, 'EXPORT_AMBE'),
 | 
				
			||||||
                        'IP': gethostbyname(config.get(section, 'IP')),
 | 
					                        'IP': gethostbyname(config.get(section, 'IP')),
 | 
				
			||||||
                        'PORT': config.getint(section, 'PORT'),
 | 
					                        'PORT': config.getint(section, 'PORT'),
 | 
				
			||||||
 | 
				
			|||||||
@ -100,7 +100,7 @@ GROUP_HANGTIME: 5
 | 
				
			|||||||
[REPEATER-1]
 | 
					[REPEATER-1]
 | 
				
			||||||
MODE: CLIENT
 | 
					MODE: CLIENT
 | 
				
			||||||
ENABLED: True
 | 
					ENABLED: True
 | 
				
			||||||
STRICT: True
 | 
					LOOSE: False
 | 
				
			||||||
EXPORT_AMBE: False
 | 
					EXPORT_AMBE: False
 | 
				
			||||||
IP: 
 | 
					IP: 
 | 
				
			||||||
PORT: 54001
 | 
					PORT: 54001
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										24
									
								
								hblink.py
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								hblink.py
									
									
									
									
									
								
							@ -161,16 +161,6 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
        if self._config['EXPORT_AMBE']:
 | 
					        if self._config['EXPORT_AMBE']:
 | 
				
			||||||
            self._ambe = AMBE()
 | 
					            self._ambe = AMBE()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def validate_radio_id(self, _id):
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if self._config['STRICT']:
 | 
					 | 
				
			||||||
            if _id == self._config['RADIO_ID']:
 | 
					 | 
				
			||||||
                return True
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                return False
 | 
					 | 
				
			||||||
        else:
 | 
					 | 
				
			||||||
            return True
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    def startProtocol(self):
 | 
					    def startProtocol(self):
 | 
				
			||||||
        # Set up periodic loop for tracking pings from clients. Run every 'PING_TIME' seconds
 | 
					        # Set up periodic loop for tracking pings from clients. Run every 'PING_TIME' seconds
 | 
				
			||||||
        self._system_maintenance = task.LoopingCall(self.maintenance_loop)
 | 
					        self._system_maintenance = task.LoopingCall(self.maintenance_loop)
 | 
				
			||||||
@ -407,7 +397,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
            _command = _data[:4]
 | 
					            _command = _data[:4]
 | 
				
			||||||
            if   _command == 'DMRD':    # DMRData -- encapsulated DMR data frame
 | 
					            if   _command == 'DMRD':    # DMRData -- encapsulated DMR data frame
 | 
				
			||||||
                _radio_id = _data[11:15]
 | 
					                _radio_id = _data[11:15]
 | 
				
			||||||
                if self.validate_radio_id(_radio_id): # Validate the source and intended target
 | 
					                if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                    _seq = _data[4:5]
 | 
					                    _seq = _data[4:5]
 | 
				
			||||||
                    _rf_src = _data[5:8]
 | 
					                    _rf_src = _data[5:8]
 | 
				
			||||||
                    _dst_id = _data[8:11]
 | 
					                    _dst_id = _data[8:11]
 | 
				
			||||||
@ -428,7 +418,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            elif _command == 'MSTN':    # Actually MSTNAK -- a NACK from the master
 | 
					            elif _command == 'MSTN':    # Actually MSTNAK -- a NACK from the master
 | 
				
			||||||
                _radio_id = _data[6:10]
 | 
					                _radio_id = _data[6:10]
 | 
				
			||||||
                if self.validate_radio_id(_radio_id):  # Validate the source and intended target
 | 
					                if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                    self._logger.warning('(%s) MSTNAK Received. Resetting connection to the Master.', self._system)
 | 
					                    self._logger.warning('(%s) MSTNAK Received. Resetting connection to the Master.', self._system)
 | 
				
			||||||
                    self._stats['CONNECTION'] = 'NO' # Disconnect ourselves and re-register
 | 
					                    self._stats['CONNECTION'] = 'NO' # Disconnect ourselves and re-register
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -444,7 +434,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                elif self._stats['CONNECTION'] == 'AUTHENTICATED': # If we've sent the login challenge...
 | 
					                elif self._stats['CONNECTION'] == 'AUTHENTICATED': # If we've sent the login challenge...
 | 
				
			||||||
                    _radio_id = _data[6:10]
 | 
					                    _radio_id = _data[6:10]
 | 
				
			||||||
                    if self.validate_radio_id(_radio_id):  # Validate the source and intended target
 | 
					                    if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                        self._logger.info('(%s) Repeater Authentication Accepted', self._system)
 | 
					                        self._logger.info('(%s) Repeater Authentication Accepted', self._system)
 | 
				
			||||||
                        _config_packet =  self._config['RADIO_ID']+\
 | 
					                        _config_packet =  self._config['RADIO_ID']+\
 | 
				
			||||||
                                          self._config['CALLSIGN']+\
 | 
					                                          self._config['CALLSIGN']+\
 | 
				
			||||||
@ -471,7 +461,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                elif self._stats['CONNECTION'] == 'CONFIG-SENT': # If we've sent out configuration to the master
 | 
					                elif self._stats['CONNECTION'] == 'CONFIG-SENT': # If we've sent out configuration to the master
 | 
				
			||||||
                    _radio_id = _data[6:10]
 | 
					                    _radio_id = _data[6:10]
 | 
				
			||||||
                    if self.validate_radio_id(_radio_id):  # Validate the source and intended target:
 | 
					                    if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                        self._logger.info('(%s) Repeater Configuration Accepted', self._system)
 | 
					                        self._logger.info('(%s) Repeater Configuration Accepted', self._system)
 | 
				
			||||||
                        if self._config['OPTIONS']:
 | 
					                        if self._config['OPTIONS']:
 | 
				
			||||||
                            self.send_master('RPTO'+self._config['RADIO_ID']+self._config['OPTIONS'])
 | 
					                            self.send_master('RPTO'+self._config['RADIO_ID']+self._config['OPTIONS'])
 | 
				
			||||||
@ -486,7 +476,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                elif self._stats['CONNECTION'] == 'OPTIONS-SENT': # If we've sent out options to the master
 | 
					                elif self._stats['CONNECTION'] == 'OPTIONS-SENT': # If we've sent out options to the master
 | 
				
			||||||
                    _radio_id = _data[6:10]
 | 
					                    _radio_id = _data[6:10]
 | 
				
			||||||
                    if self.validate_radio_id(_radio_id):  # Validate the source and intended target:
 | 
					                    if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                        self._logger.info('(%s) Repeater Options Accepted', self._system)
 | 
					                        self._logger.info('(%s) Repeater Options Accepted', self._system)
 | 
				
			||||||
                        self._stats['CONNECTION'] = 'YES'
 | 
					                        self._stats['CONNECTION'] = 'YES'
 | 
				
			||||||
                        self._logger.info('(%s) Connection to Master Completed with options', self._system)
 | 
					                        self._logger.info('(%s) Connection to Master Completed with options', self._system)
 | 
				
			||||||
@ -496,7 +486,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            elif _command == 'MSTP':    # Actually MSTPONG -- a reply to RPTPING (send by client)
 | 
					            elif _command == 'MSTP':    # Actually MSTPONG -- a reply to RPTPING (send by client)
 | 
				
			||||||
                _radio_id = _data[7:11]
 | 
					                _radio_id = _data[7:11]
 | 
				
			||||||
                if self.validate_radio_id(_radio_id):  # Validate the source and intended target:
 | 
					                if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                    self._stats['PING_OUTSTANDING'] = False
 | 
					                    self._stats['PING_OUTSTANDING'] = False
 | 
				
			||||||
                    self._stats['NUM_OUTSTANDING'] = 0
 | 
					                    self._stats['NUM_OUTSTANDING'] = 0
 | 
				
			||||||
                    self._stats['PINGS_ACKD'] += 1
 | 
					                    self._stats['PINGS_ACKD'] += 1
 | 
				
			||||||
@ -504,7 +494,7 @@ class HBSYSTEM(DatagramProtocol):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            elif _command == 'MSTC':    # Actually MSTCL -- notify us the master is closing down
 | 
					            elif _command == 'MSTC':    # Actually MSTCL -- notify us the master is closing down
 | 
				
			||||||
                _radio_id = _data[5:9]
 | 
					                _radio_id = _data[5:9]
 | 
				
			||||||
                if self.validate_radio_id(_radio_id):  # Validate the source and intended target:
 | 
					                if self._config['LOOSE'] or _radio_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation
 | 
				
			||||||
                    self._stats['CONNECTION'] = 'NO'
 | 
					                    self._stats['CONNECTION'] = 'NO'
 | 
				
			||||||
                    self._logger.info('(%s) MSTCL Recieved', self._system)
 | 
					                    self._logger.info('(%s) MSTCL Recieved', self._system)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user