diff --git a/bridge_master.py b/bridge_master.py index ba3844c..9cb1578 100644 --- a/bridge_master.py +++ b/bridge_master.py @@ -103,6 +103,19 @@ DIAL_A_TG_PROHIBITED_DEFAULTS = [ 9990, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, ] DIAL_A_TG_MAX = 999999 +UA_TIMER_UNLIMITED = 35791394 +TIMER_OPTION_KEYS = ( + 'DEFAULT_UA_TIMER', + 'TG_TIMER', + 'DIAL_TIMER', + 'TS1_TIMER', + 'TS2_TIMER', + 'TS1_TG_TIMER', + 'TS2_TG_TIMER', + 'TS1_DIAL_TIMER', + 'TS2_DIAL_TIMER', +) +TIMER_EXPLICIT_KEY = '_TIMER_OPTIONS_SET' DMR_ID_MAX = 16777215 EMB_LC_TA_HEADER = 0x04 EMB_LC_TA_BLOCKS = {0x05: 1, 0x06: 2, 0x07: 3} @@ -182,6 +195,25 @@ def parse_default_reflector_option(value, default=None): return default return int(value) +def normalize_ua_timer(timer): + return UA_TIMER_UNLIMITED if timer == 0 else timer + +def route_timer_from(timer_config, slot, route_type): + slot_key = 'TS{}_TIMER'.format(slot) + specific_key = 'TS{}_{}_TIMER'.format(slot, route_type) + route_key = '{}_TIMER'.format(route_type) + explicit_keys = timer_config.get(TIMER_EXPLICIT_KEY) + if (explicit_keys is None or specific_key in explicit_keys) and specific_key in timer_config: + return timer_config[specific_key] + if (explicit_keys is None or slot_key in explicit_keys) and slot_key in timer_config: + return timer_config[slot_key] + if (explicit_keys is None or route_key in explicit_keys) and route_key in timer_config: + return timer_config[route_key] + return timer_config.get('DEFAULT_UA_TIMER', 10) + +def get_route_timer(system, slot, route_type): + return route_timer_from(CONFIG['SYSTEMS'][system], slot, route_type) + def valid_ident_override_tg(tg): parsed = parse_int_option(tg, default=None) if parsed is None or parsed <= 0 or parsed >= DMR_ID_MAX: @@ -414,16 +446,18 @@ def make_bridges(_rules): if e['SYSTEM'] == _confsystem and e['TS'] == 2: ts2 = True if _bridge[0:1] != '#': - _tmout = CONFIG['SYSTEMS'][_confsystem]['DEFAULT_UA_TIMER'] if ts1 == False: + _tmout = get_route_timer(_confsystem, 1, 'TG') _rules[_bridge].append({'SYSTEM': _confsystem, 'TS': 1, 'TGID': bytes_3(int(_bridge)),'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [bytes_3(int(_bridge)),],'RESET': [], 'TIMER': time()}) if ts2 == False: + _tmout = get_route_timer(_confsystem, 2, 'TG') _rules[_bridge].append({'SYSTEM': _confsystem, 'TS': 2, 'TGID': bytes_3(int(_bridge)),'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [bytes_3(int(_bridge)),],'RESET': [], 'TIMER': time()}) else: - _tmout = CONFIG['SYSTEMS'][_confsystem]['DEFAULT_UA_TIMER'] if ts1 == False: + _tmout = get_route_timer(_confsystem, 1, 'DIAL') _rules[_bridge].append({'SYSTEM': _confsystem, 'TS': 1, 'TGID': bytes_3(9),'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [bytes_3(4000)],'ON': [],'RESET': [], 'TIMER': time()}) if ts2 == False: + _tmout = get_route_timer(_confsystem, 2, 'DIAL') _rules[_bridge].append({'SYSTEM': _confsystem, 'TS': 2, 'TGID': bytes_3(9),'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [bytes_3(4000)],'ON': [],'RESET': [], 'TIMER': time()}) return _rules @@ -441,14 +475,20 @@ def make_single_bridge(_tgid,_sourcesystem,_slot,_tmout): #_tmout = CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] if _system == _sourcesystem: if _slot == 1: - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time() + (_tmout * 60)}) - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) + _ts1_tmout = _tmout if _tmout is not None else get_route_timer(_system, 1, 'TG') + _ts2_tmout = get_route_timer(_system, 2, 'TG') + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': _ts1_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time() + (_ts1_tmout * 60)}) + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _ts2_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) else: - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time() + (_tmout * 60)}) - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) + _ts2_tmout = _tmout if _tmout is not None else get_route_timer(_system, 2, 'TG') + _ts1_tmout = get_route_timer(_system, 1, 'TG') + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': _ts2_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time() + (_ts2_tmout * 60)}) + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _ts1_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) else: - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) + _ts1_tmout = get_route_timer(_system, 1, 'TG') + _ts2_tmout = get_route_timer(_system, 2, 'TG') + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _ts1_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _ts2_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) if _system[0:3] == 'OBP' and (int_id(_tgid) >= 79 and (int_id(_tgid) < 9990 or int_id(_tgid) > 9999)): BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': '','TO_TYPE': 'NONE','OFF': [],'ON': [],'RESET': [], 'TIMER': time()}) @@ -460,9 +500,10 @@ def make_stat_bridge(_tgid): for _system in CONFIG['SYSTEMS']: if _system[0:3] != 'OBP': if CONFIG['SYSTEMS'][_system]['MODE'] == 'MASTER': - _tmout = CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) - BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) + _ts1_tmout = get_route_timer(_system, 1, 'TG') + _ts2_tmout = get_route_timer(_system, 2, 'TG') + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _ts1_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) + BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 2, 'TGID': _tgid,'ACTIVE': False,'TIMEOUT': _ts2_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time()}) if _system[0:3] == 'OBP': BRIDGES[_tgid_s].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': '','TO_TYPE': 'STAT','OFF': [],'ON': [],'RESET': [], 'TIMER': time()}) @@ -491,7 +532,7 @@ def make_default_reflectors(): for slot, key in ((1, 'DEFAULT_DIAL_TS1'), (2, 'DEFAULT_DIAL_TS2')): _default_reflector = CONFIG['SYSTEMS'][system].get(key, 0) if valid_dial_a_tg_reflector(_default_reflector): - make_default_reflector(_default_reflector,CONFIG['SYSTEMS'][system]['DEFAULT_UA_TIMER'],system,slot) + make_default_reflector(_default_reflector,get_route_timer(system,slot,'DIAL'),system,slot) elif int(_default_reflector) > 0: logger.warning('(ROUTER) %s %s %s is invalid or prohibited, ignoring', system, key, _default_reflector) CONFIG['SYSTEMS'][system][key] = 0 @@ -502,11 +543,10 @@ def make_static_tgs(): for system in CONFIG['SYSTEMS']: if CONFIG['SYSTEMS'][system]['MODE'] != 'MASTER': continue - _tmout = CONFIG['SYSTEMS'][system]['DEFAULT_UA_TIMER'] for tg in parse_static_tgs(CONFIG['SYSTEMS'][system]['TS1_STATIC']): - make_static_tg(tg,1,_tmout,system) + make_static_tg(tg,1,get_route_timer(system,1,'TG'),system) for tg in parse_static_tgs(CONFIG['SYSTEMS'][system]['TS2_STATIC']): - make_static_tg(tg,2,_tmout,system) + make_static_tg(tg,2,get_route_timer(system,2,'TG'),system) def make_static_tg(tg,ts,_tmout,system): #_tmout = CONFIG['SYSTEMS'][system]['DEFAULT_UA_TIMER'] @@ -569,7 +609,8 @@ def reset_all_reflector_system(_tmout,resetSystem,resetSlot=None): logger.trace('RST: for %s in BRIDGES[%s]',bridgesystem,bridge) if bridgesystem['SYSTEM'] == resetSystem and (resetSlot is None or bridgesystem['TS'] == resetSlot): logger.trace('RST: MATCH: setting inactive for %s',bridgesystem['SYSTEM']) - bridgetemp.append({'SYSTEM': resetSystem, 'TS': bridgesystem['TS'], 'TGID': bytes_3(9),'ACTIVE': False,'TIMEOUT': _tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [bytes_3(int(bridge[1:])),],'RESET': [], 'TIMER': time() + (_tmout * 60)}) + _entry_tmout = _tmout if resetSlot is not None else get_route_timer(resetSystem, bridgesystem['TS'], 'DIAL') + bridgetemp.append({'SYSTEM': resetSystem, 'TS': bridgesystem['TS'], 'TGID': bytes_3(9),'ACTIVE': False,'TIMEOUT': _entry_tmout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [bytes_3(int(bridge[1:])),],'RESET': [], 'TIMER': time() + (_entry_tmout * 60)}) else: logger.trace('RST: NO MATCH: using existing: %s',bridgesystem) bridgetemp.append(bridgesystem) @@ -593,7 +634,7 @@ def make_single_reflector(_tgid,_tmout,_sourcesystem,_slot=2): #_tmout = CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] for _ts in (1, 2): _active = _system == _sourcesystem and _ts == _slot - _entry_timeout = _tmout if _system == _sourcesystem else CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] + _entry_timeout = _tmout if _system == _sourcesystem and _ts == _slot else get_route_timer(_system, _ts, 'DIAL') BRIDGES[_bridge].append({'SYSTEM': _system, 'TS': _ts, 'TGID': bytes_3(9),'ACTIVE': _active,'TIMEOUT': _entry_timeout * 60,'TO_TYPE': 'ON','OFF': [],'ON': [_tgid,],'RESET': [], 'TIMER': time() + (_entry_timeout * 60) if _active else time()}) if _system[0:3] == 'OBP' and (int_id(_tgid) >= 79 and (int_id(_tgid) < 9990 or int_id(_tgid) > 9999)): BRIDGES[_bridge].append({'SYSTEM': _system, 'TS': 1, 'TGID': _tgid,'ACTIVE': True,'TIMEOUT': '','TO_TYPE': 'NONE','OFF': [],'ON': [],'RESET': [], 'TIMER': time()}) @@ -640,18 +681,20 @@ def remove_bridge_system(remsystem): for bridge in bt: BRIDGES[bridge] = bt[bridge] -def update_timeout(system,_tmout): +def update_timeout(system,_tmout,slot=None,route_type=None): _bridgestemp = {} - _bridgetemp = {} for _bridge in BRIDGES: + _bridgetemp = [] for _bridgesystem in BRIDGES[_bridge]: - if _bridgesystem['SYSTEM'] != system: - continue - else: - if _bridge not in _bridgestemp: - _bridgestemp[_bridge] = [] + if ( + _bridgesystem['SYSTEM'] == system and + (slot is None or _bridgesystem['TS'] == slot) and + (route_type != 'DIAL' or _bridge[0:1] == '#') and + (route_type != 'TG' or _bridge[0:1] != '#') + ): _bridgesystem['TIMEOUT'] = _tmout * 60 - _bridgestemp[_bridge].append(_bridgesystem) + _bridgetemp.append(_bridgesystem) + _bridgestemp[_bridge] = _bridgetemp BRIDGES.update(_bridgestemp) @@ -766,7 +809,7 @@ def bridgeDebug(): for dialslot in (1, 2): if dialroll[dialslot] <= 1 or CONFIG['SYSTEMS'][system]['MODE'] != 'MASTER': continue - _tmout = CONFIG['SYSTEMS'][system]['DEFAULT_UA_TIMER'] + _tmout = get_route_timer(system,dialslot,'DIAL') logger.warning('(BRIDGEDEBUG) system %s has more than one active dial bridge on TS%s (%s) - fixing',system, dialslot, dialroll[dialslot]) times = {} for _bridge in BRIDGES: @@ -991,13 +1034,13 @@ def sendVoicePacket(self,pkt,_source_id,_dest_id,_slot): self.send_system(pkt) -def sendSpeech(self,speech): +def sendSpeech(self,speech,_slot_id=2): system = self._system logger.debug('(%s) Inside sendspeech thread',self._system) sleep(1) _nine = bytes_3(9) _source_id = bytes_3(5000) - _slot = systems[system].STATUS[2] + _slot = systems[system].STATUS[_slot_id] _prompt_token = _beginGeneratedVoice(_slot) while True: if _generatedVoiceCancelled(_slot, _prompt_token): @@ -1059,7 +1102,7 @@ def disconnectedVoice(system): logger.debug('(%s) disconnected voice thread end',system) _endGeneratedVoice(_slot, _prompt_token) -def playFileOnRequest(self,fileNumber): +def playFileOnRequest(self,fileNumber,_slot_id=2): system = self._system _lang = CONFIG['SYSTEMS'][system]['ANNOUNCEMENT_LANGUAGE'] _nine = bytes_3(9) @@ -1072,9 +1115,9 @@ def playFileOnRequest(self,fileNumber): except IOError: logger.warning('(%s) cannot read file for number %s',system,fileNumber) return - speech = pkt_gen(_source_id, _nine, bytes_4(9), 1, _say) + speech = pkt_gen(_source_id, _nine, bytes_4(9), _slot_id - 1, _say) sleep(1) - _slot = systems[system].STATUS[2] + _slot = systems[system].STATUS[_slot_id] _prompt_token = _beginGeneratedVoice(_slot) while True: if _generatedVoiceCancelled(_slot, _prompt_token): @@ -1290,6 +1333,14 @@ def options_config(): _options['DEFAULT_DIAL_TS2'] = start_ref_value if 'RelinkTime' in _options: _options['DEFAULT_UA_TIMER'] = _options.pop('RelinkTime') + if 'TS1Timer' in _options: + _options['TS1_TIMER'] = _options.pop('TS1Timer') + if 'TS2Timer' in _options: + _options['TS2_TIMER'] = _options.pop('TS2Timer') + if 'TGTimer' in _options: + _options['TG_TIMER'] = _options.pop('TGTimer') + if 'DialTimer' in _options: + _options['DIAL_TIMER'] = _options.pop('DialTimer') if 'TS1_1' in _options: _options['TS1_STATIC'] = _options.pop('TS1_1') if 'TS1_2' in _options: @@ -1329,6 +1380,10 @@ def options_config(): if 'UserLink' in _options: _options.pop('UserLink') + + _provided_timer_keys = { + _timer_key for _timer_key in TIMER_OPTION_KEYS if _timer_key in _options + } if 'TS1_STATIC' not in _options: _options['TS1_STATIC'] = False @@ -1346,6 +1401,9 @@ def options_config(): if 'DEFAULT_UA_TIMER' not in _options: _options['DEFAULT_UA_TIMER'] = CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] + for _timer_key in TIMER_OPTION_KEYS: + if _timer_key not in _options: + _options[_timer_key] = CONFIG['SYSTEMS'][_system].get(_timer_key, CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER']) if 'DIAL_A_TG' not in _options: _options['DIAL_A_TG'] = int(CONFIG['SYSTEMS'][_system].get('DIAL_A_TG', True)) if 'DYNAMIC_TG_ROUTING' not in _options: @@ -1366,6 +1424,21 @@ def options_config(): logger.debug('(OPTIONS) %s - DEFAULT_UA_TIMER is not an integer, ignoring',_system) _default_ua_timer = CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] + _timer_values = {} + _invalid_timer_keys = set() + for _timer_key in TIMER_OPTION_KEYS: + _timer_value = parse_int_option(_options[_timer_key], default=None) + if _timer_value is None: + logger.debug('(OPTIONS) %s - %s is not an integer, ignoring',_system,_timer_key) + _invalid_timer_keys.add(_timer_key) + _timer_value = CONFIG['SYSTEMS'][_system].get(_timer_key, CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER']) + _timer_values[_timer_key] = normalize_ua_timer(_timer_value) + _timer_values[TIMER_EXPLICIT_KEY] = ( + set(CONFIG['SYSTEMS'][_system].get(TIMER_EXPLICIT_KEY, set())) | + (_provided_timer_keys - _invalid_timer_keys) + ) + _default_ua_timer = _timer_values['DEFAULT_UA_TIMER'] + if 'VOICE' in _options: _voice_ident = parse_bool_option(_options['VOICE'], default=None) if _voice_ident is None: @@ -1422,9 +1495,20 @@ def options_config(): _tmout = _default_ua_timer - if ('_reloadoptions' in CONFIG['SYSTEMS'][_system] and CONFIG['SYSTEMS'][_system]['_reloadoptions']) or (_default_ua_timer != CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER']): - logger.debug('(OPTIONS) %s Updating DEFAULT_UA_TIMER for existing bridges.',_system) - update_timeout(_system,_tmout) + _timer_changed = any( + _timer_values[_timer_key] != CONFIG['SYSTEMS'][_system].get(_timer_key, CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER']) + for _timer_key in TIMER_OPTION_KEYS + ) or _timer_values[TIMER_EXPLICIT_KEY] != CONFIG['SYSTEMS'][_system].get(TIMER_EXPLICIT_KEY, set()) + if ('_reloadoptions' in CONFIG['SYSTEMS'][_system] and CONFIG['SYSTEMS'][_system]['_reloadoptions']) or _timer_changed: + logger.debug('(OPTIONS) %s Updating timers for existing bridges.',_system) + for _route_type in ('TG', 'DIAL'): + for _timer_slot in (1, 2): + update_timeout( + _system, + route_timer_from(_timer_values, _timer_slot, _route_type), + slot=_timer_slot, + route_type=_route_type, + ) for _default_slot, _default_key, _default_value in ( (1, 'DEFAULT_DIAL_TS1', _default_dial_ts1), @@ -1433,15 +1517,15 @@ def options_config(): if _default_value != CONFIG['SYSTEMS'][_system].get(_default_key, 0): if _default_value > 0 and not valid_dial_a_tg_reflector(_default_value): logger.debug('(OPTIONS) %s %s is in prohibited list, ignoring change',_system,_default_key) - reset_all_reflector_system(_tmout,_system,_default_slot) + reset_all_reflector_system(route_timer_from(_timer_values,_default_slot,'DIAL'),_system,_default_slot) _default_value = 0 elif _default_value > 0: logger.debug('(OPTIONS) %s %s changed, updating',_system,_default_key) - reset_all_reflector_system(_tmout,_system,_default_slot) - make_default_reflector(_default_value,_tmout,_system,_default_slot) + reset_all_reflector_system(route_timer_from(_timer_values,_default_slot,'DIAL'),_system,_default_slot) + make_default_reflector(_default_value,route_timer_from(_timer_values,_default_slot,'DIAL'),_system,_default_slot) else: logger.debug('(OPTIONS) %s %s disabled, updating',_system,_default_key) - reset_all_reflector_system(_tmout,_system,_default_slot) + reset_all_reflector_system(route_timer_from(_timer_values,_default_slot,'DIAL'),_system,_default_slot) if _default_slot == 1: _default_dial_ts1 = _default_value else: @@ -1460,24 +1544,26 @@ def options_config(): _tmout = _default_ua_timer logger.debug('(OPTIONS) %s TS1 static TGs changed, updating',_system) for tg in parse_static_tgs(CONFIG['SYSTEMS'][_system]['TS1_STATIC']): - reset_static_tg(tg,1,_tmout,_system) + reset_static_tg(tg,1,route_timer_from(_timer_values,1,'TG'),_system) for tg in parse_static_tgs(_options['TS1_STATIC']): - make_static_tg(tg,1,_tmout,_system) + make_static_tg(tg,1,route_timer_from(_timer_values,1,'TG'),_system) ts2 = [] if ('_reloadoptions' in CONFIG['SYSTEMS'][_system] and CONFIG['SYSTEMS'][_system]['_reloadoptions']) or (_options['TS2_STATIC'] != CONFIG['SYSTEMS'][_system]['TS2_STATIC']): _tmout = _default_ua_timer logger.debug('(OPTIONS) %s TS2 static TGs changed, updating',_system) for tg in parse_static_tgs(CONFIG['SYSTEMS'][_system]['TS2_STATIC']): - reset_static_tg(tg,2,_tmout,_system) + reset_static_tg(tg,2,route_timer_from(_timer_values,2,'TG'),_system) for tg in parse_static_tgs(_options['TS2_STATIC']): - make_static_tg(tg,2,_tmout,_system) + make_static_tg(tg,2,route_timer_from(_timer_values,2,'TG'),_system) CONFIG['SYSTEMS'][_system]['TS1_STATIC'] = _options['TS1_STATIC'] CONFIG['SYSTEMS'][_system]['TS2_STATIC'] = _options['TS2_STATIC'] CONFIG['SYSTEMS'][_system]['DEFAULT_DIAL_TS1'] = _default_dial_ts1 CONFIG['SYSTEMS'][_system]['DEFAULT_DIAL_TS2'] = _default_dial_ts2 CONFIG['SYSTEMS'][_system]['DEFAULT_REFLECTOR'] = _default_dial_ts2 - CONFIG['SYSTEMS'][_system]['DEFAULT_UA_TIMER'] = _default_ua_timer + for _timer_key in TIMER_OPTION_KEYS: + CONFIG['SYSTEMS'][_system][_timer_key] = _timer_values[_timer_key] + CONFIG['SYSTEMS'][_system][TIMER_EXPLICIT_KEY] = _timer_values[TIMER_EXPLICIT_KEY] CONFIG['SYSTEMS'][_system]['DIAL_A_TG'] = _dial_a_tg CONFIG['SYSTEMS'][_system]['DYNAMIC_TG_ROUTING'] = _dynamic_tg_routing @@ -2669,8 +2755,9 @@ class routerHBP(HBSYSTEM): if _dial_tg_enabled and _dial_tg_in_range and _int_dst_id != 8 and _int_dst_id != 9: _bridgename = ''.join(['#',str(_int_dst_id)]) if _dial_tg_can_link and _bridgename not in BRIDGES: - logger.info('(%s) [A] Dial-A-TG for TG %s does not exist. Creating as User Activated. Timeout: %s',self._system, _int_dst_id,CONFIG['SYSTEMS'][self._system]['DEFAULT_UA_TIMER']) - make_single_reflector(_dst_id,CONFIG['SYSTEMS'][self._system]['DEFAULT_UA_TIMER'],self._system,_dial_tg_slot) + _dial_tmout = get_route_timer(self._system,_dial_tg_slot,'DIAL') + logger.info('(%s) [A] Dial-A-TG for TG %s does not exist. Creating as User Activated. Timeout: %s',self._system, _int_dst_id,_dial_tmout) + make_single_reflector(_dst_id,_dial_tmout,self._system,_dial_tg_slot) if _dial_tg_can_link or _dial_tg_disconnect: for _bridge in BRIDGES: @@ -2809,7 +2896,7 @@ class routerHBP(HBSYSTEM): #Information services elif _int_dst_id >= 9991 and _int_dst_id <= 9999: self.STATUS[_slot]['_stopTgAnnounce'] = True - reactor.callInThread(playFileOnRequest,self,_int_dst_id) + reactor.callInThread(playFileOnRequest,self,_int_dst_id,_dial_tg_slot) #playFileOnRequest(self,_int_dst_id) @@ -2827,9 +2914,9 @@ class routerHBP(HBSYSTEM): _say.append(words[_lang][num]) if _say: - speech = pkt_gen(bytes_3(5000), _nine, bytes_4(9), 1, _say) + speech = pkt_gen(bytes_3(5000), _nine, bytes_4(9), _dial_tg_slot - 1, _say) #call speech in a thread as it contains sleep() and hence could block the reactor - reactor.callInThread(sendSpeech,self,speech) + reactor.callInThread(sendSpeech,self,speech,_dial_tg_slot) # Mark status variables for use later self.STATUS[_slot]['RX_PEER'] = _peer_id @@ -2911,8 +2998,9 @@ class routerHBP(HBSYSTEM): #Create default bridge for unknown TG if CONFIG['SYSTEMS'][self._system].get('DYNAMIC_TG_ROUTING', True) and int_id(_dst_id) >= 5 and int_id(_dst_id) != 9 and int_id(_dst_id) != 4000 and int_id(_dst_id) != 5000 and (str(int_id(_dst_id)) not in BRIDGES): - logger.info('(%s) Bridge for TG %s does not exist. Creating as User Activated. Timeout %s',self._system, int_id(_dst_id),CONFIG['SYSTEMS'][self._system]['DEFAULT_UA_TIMER']) - make_single_bridge(_dst_id,self._system,_slot,CONFIG['SYSTEMS'][self._system]['DEFAULT_UA_TIMER']) + _tg_tmout = get_route_timer(self._system,_slot,'TG') + logger.info('(%s) Bridge for TG %s does not exist. Creating as User Activated. Timeout %s',self._system, int_id(_dst_id),_tg_tmout) + make_single_bridge(_dst_id,self._system,_slot,_tg_tmout) self.STATUS[_slot]['packets'] = self.STATUS[_slot]['packets'] +1 diff --git a/config.py b/config.py index 552ef22..c182b82 100755 --- a/config.py +++ b/config.py @@ -44,6 +44,39 @@ __license__ = 'GNU GPLv3' __maintainer__ = 'Simon Adlem, G7RZU' __email__ = 'simon@gb7fr.org.uk' +def master_timer_config(config, section): + default_timer = config.getint(section, 'DEFAULT_UA_TIMER', fallback=10) + tg_timer = config.getint(section, 'TG_TIMER', fallback=default_timer) + dial_timer = config.getint(section, 'DIAL_TIMER', fallback=default_timer) + ts1_timer = config.getint(section, 'TS1_TIMER', fallback=default_timer) + ts2_timer = config.getint(section, 'TS2_TIMER', fallback=default_timer) + + return { + '_TIMER_OPTIONS_SET': { + key for key in ( + 'DEFAULT_UA_TIMER', + 'TG_TIMER', + 'DIAL_TIMER', + 'TS1_TIMER', + 'TS2_TIMER', + 'TS1_TG_TIMER', + 'TS2_TG_TIMER', + 'TS1_DIAL_TIMER', + 'TS2_DIAL_TIMER', + ) + if config.has_option(section, key) + }, + 'DEFAULT_UA_TIMER': default_timer, + 'TG_TIMER': tg_timer, + 'DIAL_TIMER': dial_timer, + 'TS1_TIMER': ts1_timer, + 'TS2_TIMER': ts2_timer, + 'TS1_TG_TIMER': config.getint(section, 'TS1_TG_TIMER', fallback=ts1_timer if config.has_option(section, 'TS1_TIMER') else tg_timer), + 'TS2_TG_TIMER': config.getint(section, 'TS2_TG_TIMER', fallback=ts2_timer if config.has_option(section, 'TS2_TIMER') else tg_timer), + 'TS1_DIAL_TIMER': config.getint(section, 'TS1_DIAL_TIMER', fallback=ts1_timer if config.has_option(section, 'TS1_TIMER') else dial_timer), + 'TS2_DIAL_TIMER': config.getint(section, 'TS2_DIAL_TIMER', fallback=ts2_timer if config.has_option(section, 'TS2_TIMER') else dial_timer), + } + # Processing of ALS goes here. It's separated from the acl_build function because this # code is hblink config-file format specific, and acl_build is abstracted def process_acls(_config): @@ -319,7 +352,7 @@ def build_config(_config_file): 'SUB_ACL': config.get(section, 'SUB_ACL', fallback=''), 'TG1_ACL': config.get(section, 'TGID_TS1_ACL', fallback=''), 'TG2_ACL': config.get(section, 'TGID_TS2_ACL', fallback=''), - 'DEFAULT_UA_TIMER': config.getint(section, 'DEFAULT_UA_TIMER', fallback=10), + **master_timer_config(config, section), 'SINGLE_MODE': config.getboolean(section, 'SINGLE_MODE', fallback=True), 'VOICE_IDENT': config.getboolean(section, 'VOICE_IDENT', fallback=True), 'DIAL_A_TG': config.getboolean(section, 'DIAL_A_TG', fallback=True), diff --git a/tests/harness/deterministic.py b/tests/harness/deterministic.py index 953f4bd..3aa1bea 100644 --- a/tests/harness/deterministic.py +++ b/tests/harness/deterministic.py @@ -301,7 +301,16 @@ def minimal_config(system_names: tuple[str, ...] = ("MASTER-A", "MASTER-B")) -> "SUB_ACL": acl_permit_all(), "TG1_ACL": acl_permit_all(), "TG2_ACL": acl_permit_all(), + "_TIMER_OPTIONS_SET": set(), "DEFAULT_UA_TIMER": 1, + "TG_TIMER": 1, + "DIAL_TIMER": 1, + "TS1_TIMER": 1, + "TS2_TIMER": 1, + "TS1_TG_TIMER": 1, + "TS2_TG_TIMER": 1, + "TS1_DIAL_TIMER": 1, + "TS2_DIAL_TIMER": 1, "SINGLE_MODE": True, "VOICE_IDENT": False, "DIAL_A_TG": True, diff --git a/tests/harness/udp_blackbox.py b/tests/harness/udp_blackbox.py index 7c74b12..a5dd9a3 100644 --- a/tests/harness/udp_blackbox.py +++ b/tests/harness/udp_blackbox.py @@ -469,6 +469,7 @@ def write_bridge_master_config( ts2_static: str = "91", dial_a_tg: bool = True, dynamic_tg_routing: bool = True, + master_extra_config: str = "", ) -> None: global_use_acl_text = "True" if global_use_acl else "False" dial_a_tg_text = "True" if dial_a_tg else "False" @@ -506,6 +507,7 @@ GENERATOR: 0 ALLOW_UNREG_ID: True PROXY_CONTROL: False OVERRIDE_IDENT_TG: +{master_extra_config} """ ) @@ -972,6 +974,7 @@ class UdpBlackBoxScenario: ts2_static: str = "91", dial_a_tg: bool = True, dynamic_tg_routing: bool = True, + master_extra_config: str = "", fbp_systems: dict[str, int] | None = None, fbp_proto_versions: dict[str, int] | None = None, ) -> None: @@ -994,6 +997,7 @@ class UdpBlackBoxScenario: "ts2_static": ts2_static, "dial_a_tg": dial_a_tg, "dynamic_tg_routing": dynamic_tg_routing, + "master_extra_config": master_extra_config, } def __enter__(self): diff --git a/tests/test_deterministic_harness.py b/tests/test_deterministic_harness.py index aa84a61..abb61f8 100644 --- a/tests/test_deterministic_harness.py +++ b/tests/test_deterministic_harness.py @@ -20,6 +20,7 @@ from tests.harness.deterministic import ( bytes_4, minimal_config, ) +from tests.harness.udp_blackbox import fbp_control_packet, fbp_packet_from_spec def embedded_lc_payloads(lc): @@ -562,6 +563,89 @@ DEFAULT_DIAL_TS2: 92 self.assertEqual(config["SYSTEMS"]["MASTER-A"]["DEFAULT_UA_TIMER"], 3) self.assertIn("DEFAULT_UA_TIMER is not an integer", "\n".join(logs.output)) + def test_options_timers_can_be_separate_by_slot_and_route_type(self): + config = minimal_config(("MASTER-A", "MASTER-B")) + config["SYSTEMS"]["MASTER-A"]["OPTIONS"] = ( + "TIMER=10;TG_TIMER=20;DIAL_TIMER=30;TS1_TIMER=40;" + "TS1_TG_TIMER=5;TS2_DIAL_TIMER=6;" + "TS1=91;TS2=92;DEFAULT_DIAL_TS1=235;DEFAULT_DIAL_TS2=236" + ) + + with DeterministicScenario(config=config) as scenario: + scenario.bm.options_config() + + ts1_tg = next( + entry for entry in scenario.bridge_state["91"] + if entry["SYSTEM"] == "MASTER-A" and entry["TS"] == 1 + ) + ts2_tg = next( + entry for entry in scenario.bridge_state["92"] + if entry["SYSTEM"] == "MASTER-A" and entry["TS"] == 2 + ) + ts1_dial = next( + entry for entry in scenario.bridge_state["#235"] + if entry["SYSTEM"] == "MASTER-A" and entry["TS"] == 1 + ) + ts2_dial = next( + entry for entry in scenario.bridge_state["#236"] + if entry["SYSTEM"] == "MASTER-A" and entry["TS"] == 2 + ) + + self.assertEqual(ts1_tg["TIMEOUT"], 5 * 60) + self.assertEqual(ts2_tg["TIMEOUT"], 20 * 60) + self.assertEqual(ts1_dial["TIMEOUT"], 40 * 60) + self.assertEqual(ts2_dial["TIMEOUT"], 6 * 60) + + def test_options_invalid_specific_timer_falls_back_without_blocking_valid_fields(self): + config = minimal_config(("MASTER-A", "MASTER-B")) + config["SYSTEMS"]["MASTER-A"]["OPTIONS"] = ( + "TIMER=10;TG_TIMER=20;TS1_TG_TIMER=A;TS1=91;DIAL=0" + ) + + with DeterministicScenario(config=config) as scenario: + with self.assertLogs(scenario.bm.logger, level="DEBUG") as logs: + scenario.bm.options_config() + + ts1_entry = next( + entry for entry in scenario.bridge_state["91"] + if entry["SYSTEM"] == "MASTER-A" and entry["TS"] == 1 + ) + + self.assertTrue(ts1_entry["ACTIVE"]) + self.assertEqual(ts1_entry["TIMEOUT"], 20 * 60) + self.assertIn("TS1_TG_TIMER is not an integer", "\n".join(logs.output)) + + def test_options_timer_update_preserves_other_system_bridge_entries(self): + config = minimal_config(("MASTER-A", "MASTER-B")) + config["SYSTEMS"]["MASTER-A"]["OPTIONS"] = "TIMER=2;DIAL=0" + bridges = active_bridge( + "91", + 91, + ( + ("MASTER-A", 2), + ("MASTER-B", 2), + ), + ) + + with DeterministicScenario(config=config, bridges=bridges) as scenario: + original_master_b = next( + dict(entry) for entry in scenario.bridge_state["91"] + if entry["SYSTEM"] == "MASTER-B" and entry["TS"] == 2 + ) + + scenario.bm.options_config() + + entries = scenario.bridge_state["91"] + master_a = next( + entry for entry in entries if entry["SYSTEM"] == "MASTER-A" and entry["TS"] == 2 + ) + master_b = next( + entry for entry in entries if entry["SYSTEM"] == "MASTER-B" and entry["TS"] == 2 + ) + + self.assertEqual(master_a["TIMEOUT"], 2 * 60) + self.assertEqual(master_b, original_master_b) + def test_ident_override_accepts_valid_string_tg(self): destinations = self.run_ident_with_override("91") @@ -759,6 +843,27 @@ DEFAULT_DIAL_TS2: 92 self.assertNotIn(b"linkedto", spoken[-1]) self.assertEqual(scenario.capture.packets, []) + def test_dial_a_tg_prompt_generation_uses_controlling_slot(self): + for rf_slot, generated_slot in ((1, 0), (2, 1)): + with self.subTest(rf_slot=rf_slot): + with DeterministicScenario() as scenario: + generated_slots = [] + scenario.bm.words["en_GB"].update( + { + "busy": b"busy", + "silence": b"silence", + } + ) + scenario.bm.pkt_gen = ( + lambda source_id, dst_id, peer_id, slot, say: + generated_slots.append(slot) or iter(()) + ) + + scenario.inject_hbp("MASTER-A", self.private_call(4001, slot=rf_slot)) + scenario.inject_hbp("MASTER-A", self.private_call_terminator(4001, slot=rf_slot)) + + self.assertEqual(generated_slots, [generated_slot]) + def test_dial_a_tg_disconnect_on_slot_1_does_not_deactivate_ts2_reflector(self): bridges = { "#235": [ @@ -1887,6 +1992,40 @@ DEFAULT_DIAL_TS2: 92 self.assertIn("v5 packet too short", "\n".join(logs.output)) self.assertEqual(scenario.capture.for_system("OBP-1"), []) + def test_obp_corrupt_unsupported_embedded_version_does_not_poison_session_version(self): + config = minimal_config(()) + add_openbridge_system(config, "OBP-1", network_id=3001) + + with DeterministicScenario(config=config) as scenario: + packet = fbp_packet_from_spec( + PacketSpec( + peer_id=3001, + rf_src=3120001, + dst_id=91, + slot=1, + stream_id=0x01020344, + ), + network_id=3001, + fbp_version=6, + source_server=9991, + source_rptr=1001, + corrupt_hash=True, + passphrase=config["SYSTEMS"]["OBP-1"]["PASSPHRASE"], + ) + + scenario.inject_datagram("OBP-1", packet) + scenario.inject_datagram( + "OBP-1", + fbp_control_packet( + b"BCVE", + b"\x05", + passphrase=config["SYSTEMS"]["OBP-1"]["PASSPHRASE"], + ), + ) + + self.assertEqual(config["SYSTEMS"]["OBP-1"]["VER"], 5) + self.assertEqual(scenario.capture.for_system("OBP-1"), []) + def test_hbp_master_parser_discards_truncated_dmrd_from_connected_peer(self): config = minimal_config(("MASTER-A",)) diff --git a/tests/test_udp_blackbox_harness.py b/tests/test_udp_blackbox_harness.py index c7ca613..4759612 100644 --- a/tests/test_udp_blackbox_harness.py +++ b/tests/test_udp_blackbox_harness.py @@ -79,6 +79,36 @@ class UdpBlackBoxHarnessTest(unittest.TestCase): self.assertEqual(captured.fields["slot"], 2) self.assertEqual(captured.fields["stream_id"], bytes_4(0x01020304)) + def test_startup_accepts_slot_and_route_specific_timer_config(self): + require_udp_integration_enabled() + + with UdpBlackBoxScenario( + ts1_static="91", + ts2_static="92", + master_extra_config=( + "TG_TIMER: 2\n" + "DIAL_TIMER: 3\n" + "TS1_TIMER: 4\n" + "TS2_TIMER: 5\n" + "TS1_TG_TIMER: 6\n" + "TS2_DIAL_TIMER: 7\n" + ), + ) as scenario: + master_a = scenario.repeater("MASTER-A", 1001) + master_b = scenario.repeater("MASTER-B", 1002) + try: + master_a.login() + master_b.login() + + master_a.send_dmr(PacketSpec(peer_id=1001, rf_src=3120001, dst_id=91, slot=1)) + captured = master_b.recv(timeout=2.0) + finally: + master_a.close() + master_b.close() + + self.assertEqual(captured.fields["dst_id"], bytes_3(91)) + self.assertEqual(captured.fields["slot"], 1) + def test_global_use_acl_false_does_not_apply_deny_acl(self): require_udp_integration_enabled() @@ -526,7 +556,7 @@ class UdpBlackBoxHarnessTest(unittest.TestCase): ) self.assertEqual(leaked, []) - def test_dial_a_tg_reserved_control_emits_local_tg9_ts2_prompt(self): + def test_dial_a_tg_reserved_control_emits_local_tg9_same_slot_prompt(self): require_udp_integration_enabled() with UdpBlackBoxScenario() as scenario: @@ -548,7 +578,7 @@ class UdpBlackBoxHarnessTest(unittest.TestCase): self.assertEqual(captured.packet[:4], b"DMRD") self.assertEqual(captured.fields["rf_src"], bytes_3(5000)) self.assertEqual(captured.fields["dst_id"], bytes_3(9)) - self.assertEqual(captured.fields["slot"], 2) + self.assertEqual(captured.fields["slot"], 1) self.assertEqual(leaked, []) def test_dial_a_tg_slot_1_routes_local_tg9_to_fbp_reflector_tg(self): @@ -640,7 +670,7 @@ class UdpBlackBoxHarnessTest(unittest.TestCase): self.assertEqual(prompt.fields["rf_src"], bytes_3(5000)) self.assertEqual(prompt.fields["dst_id"], bytes_3(9)) - self.assertEqual(prompt.fields["slot"], 2) + self.assertEqual(prompt.fields["slot"], 1) self.assertEqual(routed.fields["rf_src"], bytes_3(3120001)) self.assertEqual(routed.fields["dst_id"], bytes_3(91)) self.assertEqual(routed.fields["stream_id"], bytes_4(real_stream)) @@ -833,7 +863,6 @@ class UdpBlackBoxHarnessTest(unittest.TestCase): self.assertEqual(captured.fields["slot"], 2) self.assertEqual(captured.fields["stream_id"], bytes_4(0x0102031A)) - @unittest.expectedFailure def test_fbp_unsupported_embedded_packet_version_is_rejected_without_hbp_leak(self): require_udp_integration_enabled() @@ -863,6 +892,49 @@ class UdpBlackBoxHarnessTest(unittest.TestCase): finally: master_b.close() + def test_fbp_unsupported_embedded_packet_version_does_not_poison_bcve_session_version(self): + require_udp_integration_enabled() + + with UdpBlackBoxScenario(fbp_systems={"OBP-1": 3001}) as scenario: + master_a = scenario.repeater("MASTER-A", 1001) + fbp_peer = scenario.fbp_peer("OBP-1") + try: + master_a.login() + fbp_peer.send_bcka() + fbp_peer.send_bcve() + fbp_peer.drain(seconds=0.2) + + fbp_peer.send_fbp( + PacketSpec( + peer_id=3001, + rf_src=3120001, + dst_id=91, + slot=1, + stream_id=0x01020345, + ), + fbp_version=6, + source_server=9991, + source_rptr=1001, + ) + fbp_peer.send_bcve(version=5) + fbp_peer.drain(seconds=0.2) + + master_a.send_dmr( + PacketSpec( + peer_id=1001, + rf_src=3120001, + dst_id=91, + slot=2, + stream_id=0x01020346, + ) + ) + captured = fbp_peer.recv_dmre(timeout=2.0) + finally: + master_a.close() + + self.assertEqual(captured.fields["fbp_version"], 5) + self.assertEqual(captured.fields["source_rptr"], bytes_4(1001)) + def test_fbp_v4_packet_downgrades_session_to_v4_layout_for_compatibility(self): require_udp_integration_enabled()