2014-01-03 16:01:43 -05:00
#!/usr/bin/env python
#
2016-11-23 08:50:56 -05:00
###############################################################################
# Copyright (C) 2016 Cortney T. Buffington, N0MJS <n0mjs@me.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
###############################################################################
2013-10-29 08:09:18 -04:00
2016-11-23 17:37:37 -05:00
# This is a sample application to bridge traffic between IPSC systems. it uses
2014-08-31 12:27:00 -04:00
# one required (bridge_rules.py) and one optional (known_bridges.py) additional
# configuration files. Both files have their own documentation for use.
#
# "bridge_rules" contains the IPSC network, Timeslot and TGID matching rules to
2016-11-23 17:37:37 -05:00
# determine which voice calls are bridged between IPSC systems and which are
2014-08-31 12:27:00 -04:00
# not.
#
# "known_bridges" contains DMR radio ID numbers of known bridges. This file is
# used when you want bridge.py to be "polite" or serve as a backup bridge. If
# a known bridge exists in either a source OR target IPSC network, then no
2016-11-23 17:37:37 -05:00
# bridging between those IPSC systems will take place. This behavior is
2014-08-31 12:27:00 -04:00
# dynamic and updates each keep-alive interval (main configuration file).
# For faster failover, configure a short keep-alive time and a low number of
# missed keep-alives before timout. I recommend 5 sec keep-alive and 3 missed.
# That gives a worst-case scenario of 15 seconds to fail over. Recovery will
# typically happen with a single "blip" in the transmission up to about 5
# seconds.
#
# While this file is listed as Beta status, K0USY Group depends on this code
# for the bridigng of it's many repeaters. We consider it reliable, but you
# get what you pay for... as usual, no guarantees.
2016-09-18 09:39:42 -04:00
#
# Use to make test strings: #print('PKT:', "\\x".join("{:02x}".format(ord(c)) for c in _data))
2013-11-24 23:01:20 -05:00
2013-10-29 08:09:18 -04:00
from __future__ import print_function
from twisted . internet import reactor
2014-08-31 12:27:00 -04:00
from twisted . internet import task
2016-12-15 16:04:45 -05:00
from binascii import b2a_hex as ahex
2015-05-13 10:40:56 -04:00
from time import time
2016-12-15 17:26:48 -05:00
from importlib import import_module
2013-10-29 08:09:18 -04:00
2013-11-21 19:38:15 -05:00
import sys
2016-12-15 16:04:45 -05:00
from dmr_utils . utils import hex_str_3 , hex_str_4 , int_id
2016-12-16 09:10:33 -05:00
from dmrlink import IPSC , systems , config_reports
2016-11-23 17:55:39 -05:00
from ipsc . ipsc_const import BURST_DATA_TYPE
2016-11-23 17:37:37 -05:00
2013-10-31 22:45:57 -04:00
2016-11-23 08:50:56 -05:00
__author__ = ' Cortney T. Buffington, N0MJS '
__copyright__ = ' Copyright (c) 2013 - 2016 Cortney T. Buffington, N0MJS and the K0USY Group '
__credits__ = ' Adam Fast, KC0YLK; Dave Kierzkowski, KD8EYF; Steve Zingman, N4IRS; Mike Zingman, N4IRR '
__license__ = ' GNU GPLv3 '
__maintainer__ = ' Cort Buffington, N0MJS '
__email__ = ' n0mjs@me.com '
2014-01-03 16:01:43 -05:00
2013-11-21 19:50:01 -05:00
2016-05-01 11:49:30 -04:00
# Minimum time between different subscribers transmitting on the same TGID
#
2015-05-15 00:00:19 -04:00
TS_CLEAR_TIME = .2
2013-11-14 21:14:16 -05:00
2013-11-17 19:13:59 -05:00
# Import Bridging rules
2014-08-31 12:27:00 -04:00
# Note: A stanza *must* exist for any IPSC configured in the main
# configuration file and listed as "active". It can be empty,
# but it has to exist.
2013-11-17 19:13:59 -05:00
#
2016-12-16 09:10:33 -05:00
def build_rules ( _bridge_rules ) :
2016-12-15 17:26:48 -05:00
try :
2016-12-16 09:10:33 -05:00
rule_file = import_module ( _bridge_rules )
2016-12-15 17:26:48 -05:00
logger . info ( ' Bridge rules file found and rules imported ' )
except ImportError :
sys . exit ( ' Bridging rules file not found or invalid ' )
2013-11-17 19:13:59 -05:00
2016-12-15 17:26:48 -05:00
# Convert integer GROUP ID numbers from the config into hex strings
# we need to send in the actual data packets.
#
2014-08-31 12:27:00 -04:00
2016-12-16 09:10:33 -05:00
for _ipsc in rule_file . RULES :
for _rule in rule_file . RULES [ _ipsc ] [ ' GROUP_VOICE ' ] :
2016-12-15 17:26:48 -05:00
_rule [ ' SRC_GROUP ' ] = hex_str_3 ( _rule [ ' SRC_GROUP ' ] )
_rule [ ' DST_GROUP ' ] = hex_str_3 ( _rule [ ' DST_GROUP ' ] )
_rule [ ' SRC_TS ' ] = _rule [ ' SRC_TS ' ]
_rule [ ' DST_TS ' ] = _rule [ ' DST_TS ' ]
for i , e in enumerate ( _rule [ ' ON ' ] ) :
_rule [ ' ON ' ] [ i ] = hex_str_3 ( _rule [ ' ON ' ] [ i ] )
for i , e in enumerate ( _rule [ ' OFF ' ] ) :
_rule [ ' OFF ' ] [ i ] = hex_str_3 ( _rule [ ' OFF ' ] [ i ] )
_rule [ ' TIMEOUT ' ] = _rule [ ' TIMEOUT ' ] * 60
_rule [ ' TIMER ' ] = time ( ) + _rule [ ' TIMEOUT ' ]
if _ipsc not in CONFIG [ ' SYSTEMS ' ] :
sys . exit ( ' ERROR: Bridge rules found for an IPSC network not configured in main configuration ' )
for _ipsc in CONFIG [ ' SYSTEMS ' ] :
2016-12-16 09:10:33 -05:00
if _ipsc not in rule_file . RULES :
2016-12-15 17:26:48 -05:00
sys . exit ( ' ERROR: Bridge rules not found for all IPSC network configured ' )
2014-08-31 12:27:00 -04:00
2016-12-16 09:10:33 -05:00
return rule_file . RULES
2015-05-17 12:19:19 -04:00
2014-08-31 12:27:00 -04:00
# Import List of Bridges
# This is how we identify known bridges. If one of these is present
# and it's mode byte is set to bridge, we don't
#
2016-12-16 09:10:33 -05:00
def build_bridges ( _known_bridges ) :
try :
bridges_file = import_module ( _known_bridges )
logger . info ( ' Known bridges file found and bridge ID list imported ' )
2016-12-18 22:51:13 -05:00
return bridges_file . BRIDGES
2016-12-16 09:10:33 -05:00
except ImportError :
logger . critical ( ' \' known_bridges.py \' not found - backup bridge service will not be enabled ' )
2016-12-18 22:51:13 -05:00
return [ ]
2013-10-29 08:09:18 -04:00
2016-05-03 20:53:22 -04:00
# Import subscriber ACL
# ACL may be a single list of subscriber IDs
# Global action is to allow or deny them. Multiple lists with different actions and ranges
# are not yet implemented.
2016-12-16 09:10:33 -05:00
def build_acl ( _sub_acl ) :
try :
acl_file = import_module ( _sub_acl )
for i , e in enumerate ( acl_file . ACL ) :
acl_file . ACL [ i ] = hex_str_3 ( acl_file . ACL [ i ] )
logger . info ( ' ACL file found and ACL entries imported ' )
2016-12-19 19:43:42 -05:00
ACL_ACTION = acl_file . ACL_ACTION
ACL = acl_file . ACL_ACTION
2016-12-16 09:10:33 -05:00
except ImportError :
logger . info ( ' ACL file not found or invalid - all subscriber IDs are valid ' )
ACL_ACTION = ' NONE '
2016-12-19 19:43:42 -05:00
ACL = [ ]
2016-12-16 09:10:33 -05:00
# Depending on which type of ACL is used (PERMIT, DENY... or there isn't one)
# define a differnet function to be used to check the ACL
global allow_sub
2016-12-19 19:43:42 -05:00
if ACL_ACTION == ' PERMIT ' :
2016-12-16 09:10:33 -05:00
def allow_sub ( _sub ) :
if _sub in ACL :
return True
else :
return False
2016-12-19 19:43:42 -05:00
elif ACL_ACTION == ' DENY ' :
2016-12-16 09:10:33 -05:00
def allow_sub ( _sub ) :
if _sub not in ACL :
return True
else :
return False
else :
def allow_sub ( _sub ) :
2016-05-03 20:53:22 -04:00
return True
2016-12-16 09:10:33 -05:00
2016-12-19 19:43:42 -05:00
return ACL
2016-12-16 09:10:33 -05:00
2016-08-05 10:20:06 -04:00
# Run this every minute for rule timer updates
def rule_timer_loop ( ) :
2016-09-15 20:57:39 -04:00
logger . debug ( ' (ALL IPSC) Rule timer loop started ' )
2016-08-06 11:58:53 -04:00
_now = time ( )
for _network in RULES :
for _rule in RULES [ _network ] [ ' GROUP_VOICE ' ] :
if _rule [ ' TO_TYPE ' ] == ' ON ' :
if _rule [ ' ACTIVE ' ] == True :
if _rule [ ' TIMER ' ] < _now :
_rule [ ' ACTIVE ' ] = False
2016-11-19 11:11:59 -05:00
logger . info ( ' ( %s ) Rule timout DEACTIVATE: Rule name: %s , Target IPSC: %s , TS: %s , TGID: %s ' , _network , _rule [ ' NAME ' ] , _rule [ ' DST_NET ' ] , _rule [ ' DST_TS ' ] , int_id ( _rule [ ' DST_GROUP ' ] ) )
2016-09-15 20:57:39 -04:00
else :
timeout_in = _rule [ ' TIMER ' ] - _now
2016-11-19 11:11:59 -05:00
logger . info ( ' ( %s ) Rule ACTIVE with ON timer running: Timeout eligible in: %d s, Rule name: %s , Target IPSC: %s , TS: %s , TGID: %s ' , _network , timeout_in , _rule [ ' NAME ' ] , _rule [ ' DST_NET ' ] , _rule [ ' DST_TS ' ] , int_id ( _rule [ ' DST_GROUP ' ] ) )
2016-08-06 11:58:53 -04:00
elif _rule [ ' TO_TYPE ' ] == ' OFF ' :
if _rule [ ' ACTIVE ' ] == False :
if _rule [ ' TIMER ' ] < _now :
_rule [ ' ACTIVE ' ] = True
2016-11-19 11:11:59 -05:00
logger . info ( ' ( %s ) Rule timout ACTIVATE: Rule name: %s , Target IPSC: %s , TS: %s , TGID: %s ' , _network , _rule [ ' NAME ' ] , _rule [ ' DST_NET ' ] , _rule [ ' DST_TS ' ] , int_id ( _rule [ ' DST_GROUP ' ] ) )
2016-09-15 20:57:39 -04:00
else :
timeout_in = _rule [ ' TIMER ' ] - _now
2016-11-19 11:11:59 -05:00
logger . info ( ' ( %s ) Rule DEACTIVE with OFF timer running: Timeout eligible in: %d s, Rule name: %s , Target IPSC: %s , TS: %s , TGID: %s ' , _network , timeout_in , _rule [ ' NAME ' ] , _rule [ ' DST_NET ' ] , _rule [ ' DST_TS ' ] , int_id ( _rule [ ' DST_GROUP ' ] ) )
2016-08-06 11:58:53 -04:00
else :
logger . debug ( ' Rule timer loop made no rule changes ' )
2014-08-31 12:27:00 -04:00
2016-12-18 22:51:13 -05:00
2015-05-15 00:00:19 -04:00
class bridgeIPSC ( IPSC ) :
2016-12-16 09:10:33 -05:00
def __init__ ( self , _name , _config , _logger , _bridges ) :
2016-12-18 22:51:13 -05:00
IPSC . __init__ ( self , _name , _config , _logger )
2016-12-16 09:10:33 -05:00
self . BRIDGES = _bridges
if self . BRIDGES :
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Initializing backup/polite bridging ' , self . _system )
2014-08-31 12:27:00 -04:00
self . BRIDGE = False
2015-05-15 00:00:19 -04:00
else :
self . BRIDGE = True
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' Initializing standard bridging ' )
2015-05-17 12:19:19 -04:00
2015-05-15 00:00:19 -04:00
self . IPSC_STATUS = {
2016-11-19 11:11:59 -05:00
1 : { ' RX_GROUP ' : ' \x00 ' , ' TX_GROUP ' : ' \x00 ' , ' RX_TIME ' : 0 , ' TX_TIME ' : 0 , ' RX_SRC_SUB ' : ' \x00 ' , ' TX_SRC_SUB ' : ' \x00 ' } ,
2 : { ' RX_GROUP ' : ' \x00 ' , ' TX_GROUP ' : ' \x00 ' , ' RX_TIME ' : 0 , ' TX_TIME ' : 0 , ' RX_SRC_SUB ' : ' \x00 ' , ' TX_SRC_SUB ' : ' \x00 ' }
2015-05-15 00:00:19 -04:00
}
2016-09-10 11:51:04 -04:00
self . last_seq_id = ' \x00 '
2016-09-10 17:34:50 -04:00
self . call_start = 0
2016-09-10 11:41:42 -04:00
2016-12-18 22:51:13 -05:00
# Setup the backup/polite bridging maintenance loop (based on keep-alive timer)
2014-08-31 12:27:00 -04:00
2016-12-18 22:51:13 -05:00
def startProtocol ( self ) :
IPSC . startProtocol ( self )
2016-12-16 09:10:33 -05:00
if self . BRIDGES :
2016-12-18 22:51:13 -05:00
self . _bridge_presence = task . LoopingCall ( self . bridge_presence_loop )
self . _bridge_presence_loop = self . _bridge_presence . start ( self . _local [ ' ALIVE_TIMER ' ] )
2014-08-31 12:27:00 -04:00
2015-05-15 00:00:19 -04:00
# This is the backup/polite bridge maintenance loop
def bridge_presence_loop ( self ) :
2016-12-18 22:51:13 -05:00
self . _logger . debug ( ' ( %s ) Bridge presence loop initiated ' , self . _system )
2015-05-15 00:00:19 -04:00
_temp_bridge = True
2016-12-18 22:51:13 -05:00
for peer in self . BRIDGES :
2015-05-15 00:00:19 -04:00
_peer = hex_str_4 ( peer )
if _peer in self . _peers . keys ( ) and ( self . _peers [ _peer ] [ ' MODE_DECODE ' ] [ ' TS_1 ' ] or self . _peers [ _peer ] [ ' MODE_DECODE ' ] [ ' TS_2 ' ] ) :
_temp_bridge = False
2016-12-18 22:51:13 -05:00
self . _logger . debug ( ' ( %s ) Peer %s is an active bridge ' , self . _system , int_id ( _peer ) )
2015-05-15 00:00:19 -04:00
if _peer == self . _master [ ' RADIO_ID ' ] \
and self . _master [ ' STATUS ' ] [ ' CONNECTED ' ] \
and ( self . _master [ ' MODE_DECODE ' ] [ ' TS_1 ' ] or self . _master [ ' MODE_DECODE ' ] [ ' TS_2 ' ] ) :
_temp_bridge = False
2016-12-18 22:51:13 -05:00
self . _logger . debug ( ' ( %s ) Master %s is an active bridge ' , self . _system , int_id ( _peer ) )
2015-05-15 00:00:19 -04:00
if self . BRIDGE != _temp_bridge :
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Changing bridge status to: %s ' , self . _system , _temp_bridge )
2015-05-15 00:00:19 -04:00
self . BRIDGE = _temp_bridge
2015-05-13 21:35:18 -04:00
2015-05-15 00:00:19 -04:00
#************************************************
# CALLBACK FUNCTIONS FOR USER PACKET TYPES
#************************************************
#
2016-12-18 22:51:13 -05:00
def group_voice ( self , _src_sub , _dst_group , _ts , _end , _peerid , _data ) :
2016-05-03 20:53:22 -04:00
# Check for ACL match, and return if the subscriber is not allowed
if allow_sub ( _src_sub ) == False :
2016-12-18 22:51:13 -05:00
self . _logger . warning ( ' ( %s ) Group Voice Packet ***REJECTED BY ACL*** From: %s , IPSC Peer %s , Destination %s ' , self . _system , int_id ( _src_sub ) , int_id ( _peerid ) , int_id ( _dst_group ) )
2016-05-03 20:53:22 -04:00
return
# Process the packet
2016-12-18 22:51:13 -05:00
self . _logger . debug ( ' ( %s ) Group Voice Packet Received From: %s , IPSC Peer %s , Destination %s ' , self . _system , int_id ( _src_sub ) , int_id ( _peerid ) , int_id ( _dst_group ) )
2015-05-15 00:00:19 -04:00
_burst_data_type = _data [ 30 ] # Determine the type of voice packet this is (see top of file for possible types)
2016-09-10 11:41:42 -04:00
_seq_id = _data [ 5 ]
2016-04-26 19:56:45 -04:00
2016-05-01 08:47:46 -04:00
now = time ( ) # Mark packet arrival time -- we'll need this for call contention handling
2015-05-23 09:54:46 -04:00
2016-12-18 22:51:13 -05:00
for rule in RULES [ self . _system ] [ ' GROUP_VOICE ' ] :
2015-05-15 00:00:19 -04:00
_target = rule [ ' DST_NET ' ] # Shorthand to reduce length and make it easier to read
2016-11-23 17:37:37 -05:00
_status = systems [ _target ] . IPSC_STATUS # Shorthand to reduce length and make it easier to read
2016-12-18 22:51:13 -05:00
2016-09-15 20:36:57 -04:00
# This is the primary rule match to determine if the call will be routed.
2016-11-23 17:37:37 -05:00
if ( rule [ ' SRC_GROUP ' ] == _dst_group and rule [ ' SRC_TS ' ] == _ts and rule [ ' ACTIVE ' ] == True ) and ( self . BRIDGE == True or systems [ _target ] . BRIDGE == True ) :
2016-05-01 11:49:30 -04:00
#
# BEGIN CONTENTION HANDLING
#
# If this is an inter-DMRlink trunk, this isn't necessary
2016-12-18 22:51:13 -05:00
if RULES [ self . _system ] [ ' TRUNK ' ] == False :
2016-05-01 11:49:30 -04:00
# The rules for each of the 4 "ifs" below are listed here for readability. The Frame To Send is:
# From a different group than last RX from this IPSC, but it has been less than Group Hangtime
# From a different group than last TX to this IPSC, but it has been less than Group Hangtime
# From the same group as the last RX from this IPSC, but from a different subscriber, and it has been less than TS Clear Time
# From the same group as the last TX to this IPSC, but from a different subscriber, and it has been less than TS Clear Time
# The "continue" at the end of each means the next iteration of the for loop that tests for matching rules
#
2016-12-21 22:08:58 -05:00
if ( ( rule [ ' DST_GROUP ' ] != _status [ rule [ ' DST_TS ' ] ] [ ' RX_GROUP ' ] ) and ( ( now - _status [ rule [ ' DST_TS ' ] ] [ ' RX_TIME ' ] ) < RULES [ _target ] [ ' GROUP_HANGTIME ' ] ) ) :
2015-07-22 16:02:36 -04:00
if _burst_data_type == BURST_DATA_TYPE [ ' VOICE_HEAD ' ] :
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Call not bridged to TGID %s , target active or in group hangtime: IPSC: %s , TS: %s , TGID: %s ' , self . _system , int_id ( rule [ ' DST_GROUP ' ] ) , _target , rule [ ' DST_TS ' ] , int_id ( _status [ rule [ ' DST_TS ' ] ] [ ' RX_GROUP ' ] ) )
2016-05-01 11:49:30 -04:00
continue
2016-12-21 22:08:58 -05:00
if ( ( rule [ ' DST_GROUP ' ] != _status [ rule [ ' DST_TS ' ] ] [ ' TX_GROUP ' ] ) and ( ( now - _status [ rule [ ' DST_TS ' ] ] [ ' TX_TIME ' ] ) < RULES [ _target ] [ ' GROUP_HANGTIME ' ] ) ) :
2015-07-22 16:02:36 -04:00
if _burst_data_type == BURST_DATA_TYPE [ ' VOICE_HEAD ' ] :
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Call not bridged to TGID %s , target in group hangtime: IPSC: %s , TS: %s , TGID: %s ' , self . _system , int_id ( rule [ ' DST_GROUP ' ] ) , _target , rule [ ' DST_TS ' ] , int_id ( _status [ rule [ ' DST_TS ' ] ] [ ' TX_GROUP ' ] ) )
2015-08-06 23:51:34 -04:00
continue
2016-11-19 11:11:59 -05:00
if ( rule [ ' DST_GROUP ' ] == _status [ rule [ ' DST_TS ' ] ] [ ' RX_GROUP ' ] ) and ( ( now - _status [ rule [ ' DST_TS ' ] ] [ ' RX_TIME ' ] ) < TS_CLEAR_TIME ) :
2015-07-22 16:02:36 -04:00
if _burst_data_type == BURST_DATA_TYPE [ ' VOICE_HEAD ' ] :
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Call not bridged to TGID %s , matching call already active on target: IPSC: %s , TS: %s , TGID: %s ' , self . _system , int_id ( rule [ ' DST_GROUP ' ] ) , _target , rule [ ' DST_TS ' ] , int_id ( _status [ rule [ ' DST_TS ' ] ] [ ' RX_GROUP ' ] ) )
2015-08-06 23:51:34 -04:00
continue
2016-11-19 11:11:59 -05:00
if ( rule [ ' DST_GROUP ' ] == _status [ rule [ ' DST_TS ' ] ] [ ' TX_GROUP ' ] ) and ( _src_sub != _status [ rule [ ' DST_TS ' ] ] [ ' TX_SRC_SUB ' ] ) and ( ( now - _status [ rule [ ' DST_TS ' ] ] [ ' TX_TIME ' ] ) < TS_CLEAR_TIME ) :
2016-05-01 11:49:30 -04:00
if _burst_data_type == BURST_DATA_TYPE [ ' VOICE_HEAD ' ] :
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Call not bridged for subscriber %s , call bridge in progress on target: IPSC: %s , TS: %s , TGID: %s SUB: %s ' , self . _system , int_id ( _src_sub ) , _target , rule [ ' DST_TS ' ] , int_id ( _status [ rule [ ' DST_TS ' ] ] [ ' TX_GROUP ' ] ) , int_id ( _status [ rule [ ' DST_TS ' ] ] [ ' TX_SRC_SUB ' ] ) )
2016-05-01 11:49:30 -04:00
continue
#
# END CONTENTION HANDLING
#
#
# BEGIN FRAME FORWARDING
#
# Make a copy of the payload
2015-05-28 10:42:14 -04:00
_tmp_data = _data
2016-05-01 11:49:30 -04:00
2015-05-28 10:42:14 -04:00
# Re-Write the IPSC SRC to match the target network's ID
2016-12-18 22:51:13 -05:00
_tmp_data = _tmp_data . replace ( _peerid , self . _CONFIG [ ' SYSTEMS ' ] [ _target ] [ ' LOCAL ' ] [ ' RADIO_ID ' ] )
2016-05-01 11:49:30 -04:00
2015-05-28 10:42:14 -04:00
# Re-Write the destination Group ID
_tmp_data = _tmp_data . replace ( _dst_group , rule [ ' DST_GROUP ' ] )
# Re-Write IPSC timeslot value
_call_info = int_id ( _data [ 17 : 18 ] )
2016-11-19 11:11:59 -05:00
if rule [ ' DST_TS ' ] == 1 :
2015-05-28 10:42:14 -04:00
_call_info & = ~ ( 1 << 5 )
2016-11-19 11:11:59 -05:00
elif rule [ ' DST_TS ' ] == 2 :
2015-05-28 10:42:14 -04:00
_call_info | = 1 << 5
_call_info = chr ( _call_info )
_tmp_data = _tmp_data [ : 17 ] + _call_info + _tmp_data [ 18 : ]
# Re-Write DMR timeslot value
# Determine if the slot is present, so we can translate if need be
if _burst_data_type == BURST_DATA_TYPE [ ' SLOT1_VOICE ' ] or _burst_data_type == BURST_DATA_TYPE [ ' SLOT2_VOICE ' ] :
_slot_valid = True
else :
_slot_valid = False
# Re-Write timeslot if necessary...
if _slot_valid :
2016-11-19 11:11:59 -05:00
if rule [ ' DST_TS ' ] == 1 :
2015-05-28 10:42:14 -04:00
_burst_data_type = BURST_DATA_TYPE [ ' SLOT1_VOICE ' ]
elif rule [ ' DST_TS ' ] == 1 :
_burst_data_type = BURST_DATA_TYPE [ ' SLOT2_VOICE ' ]
_tmp_data = _tmp_data [ : 30 ] + _burst_data_type + _tmp_data [ 31 : ]
2016-09-09 23:05:47 -04:00
2015-05-28 10:42:14 -04:00
# Send the packet to all peers in the target IPSC
2016-11-23 17:37:37 -05:00
systems [ _target ] . send_to_ipsc ( _tmp_data )
2016-05-01 11:49:30 -04:00
#
# END FRAME FORWARDING
#
# Set values for the contention handler to test next time there is a frame to forward
2016-11-19 11:11:59 -05:00
_status [ _ts ] [ ' TX_GROUP ' ] = rule [ ' DST_GROUP ' ]
_status [ _ts ] [ ' TX_TIME ' ] = now
_status [ _ts ] [ ' TX_SRC_SUB ' ] = _src_sub
2016-05-01 11:49:30 -04:00
2015-05-28 10:42:14 -04:00
2016-05-01 11:49:30 -04:00
# Mark the group and time that a packet was recieved for the contention handler to use later
2016-11-19 11:11:59 -05:00
self . IPSC_STATUS [ _ts ] [ ' RX_GROUP ' ] = _dst_group
self . IPSC_STATUS [ _ts ] [ ' RX_TIME ' ] = now
2016-05-01 11:49:30 -04:00
#
# BEGIN IN-BAND SIGNALING BASED ON TGID & VOICE TERMINATOR FRAME
#
# Activate/Deactivate rules based on group voice activity -- PTT or UA for you c-Bridge dorks.
# This will ONLY work for symmetrical rules!!!
2016-08-06 21:51:05 -04:00
# Action happens on key up
if _burst_data_type == BURST_DATA_TYPE [ ' VOICE_HEAD ' ] :
2016-09-10 17:26:44 -04:00
if self . last_seq_id != _seq_id :
self . last_seq_id = _seq_id
2016-09-10 17:34:50 -04:00
self . call_start = time ( )
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) GROUP VOICE START: CallID: %s PEER: %s , SUB: %s , TS: %s , TGID: %s ' , self . _system , int_id ( _seq_id ) , int_id ( _peerid ) , int_id ( _src_sub ) , _ts , int_id ( _dst_group ) )
2016-08-06 21:51:05 -04:00
2016-05-01 11:49:30 -04:00
# Action happens on un-key
if _burst_data_type == BURST_DATA_TYPE [ ' VOICE_TERM ' ] :
2016-09-10 17:32:14 -04:00
if self . last_seq_id == _seq_id :
2016-09-10 17:34:50 -04:00
self . call_duration = time ( ) - self . call_start
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) GROUP VOICE END: CallID: %s PEER: %s , SUB: %s , TS: %s , TGID: %s Duration: %.2f s ' , self . _system , int_id ( _seq_id ) , int_id ( _peerid ) , int_id ( _src_sub ) , _ts , int_id ( _dst_group ) , self . call_duration )
2016-09-10 17:32:14 -04:00
else :
2016-12-18 22:51:13 -05:00
self . _logger . warning ( ' ( %s ) GROUP VOICE END WITHOUT MATCHING START: CallID: %s PEER: %s , SUB: %s , TS: %s , TGID: %s ' , self . _system , int_id ( _seq_id ) , int_id ( _peerid ) , int_id ( _src_sub ) , _ts , int_id ( _dst_group ) , )
2016-05-01 11:49:30 -04:00
# Iterate the rules dictionary
2016-12-18 22:51:13 -05:00
for rule in RULES [ self . _system ] [ ' GROUP_VOICE ' ] :
2016-08-06 20:02:28 -04:00
_target = rule [ ' DST_NET ' ]
# TGID matches a rule source, reset its timer
2016-09-15 20:36:57 -04:00
if _ts == rule [ ' SRC_TS ' ] and _dst_group == rule [ ' SRC_GROUP ' ] and ( ( rule [ ' TO_TYPE ' ] == ' ON ' and ( rule [ ' ACTIVE ' ] == True ) ) or ( rule [ ' TO_TYPE ' ] == ' OFF ' and rule [ ' ACTIVE ' ] == False ) ) :
2016-08-06 20:02:28 -04:00
rule [ ' TIMER ' ] = now + rule [ ' TIMEOUT ' ]
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Source group transmission match for rule \" %s \" . Reset timeout to %s ' , self . _system , rule [ ' NAME ' ] , rule [ ' TIMER ' ] )
2016-08-06 20:02:28 -04:00
# Scan for reciprocal rules and reset their timers as well.
for target_rule in RULES [ _target ] [ ' GROUP_VOICE ' ] :
if target_rule [ ' NAME ' ] == rule [ ' NAME ' ] :
target_rule [ ' TIMER ' ] = now + target_rule [ ' TIMEOUT ' ]
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Reciprocal group transmission match for rule \" %s \" on IPSC \" %s \" . Reset timeout to %s ' , self . _system , target_rule [ ' NAME ' ] , _target , rule [ ' TIMER ' ] )
2016-05-01 11:49:30 -04:00
# TGID matches an ACTIVATION trigger
if _dst_group in rule [ ' ON ' ] :
# Set the matching rule as ACTIVE
rule [ ' ACTIVE ' ] = True
2016-08-06 20:02:28 -04:00
rule [ ' TIMER ' ] = now + rule [ ' TIMEOUT ' ]
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Primary Bridge Rule \" %s \" changed to state: %s ' , self . _system , rule [ ' NAME ' ] , rule [ ' ACTIVE ' ] )
2016-05-01 11:49:30 -04:00
# Set reciprocal rules for other IPSCs as ACTIVE
for target_rule in RULES [ _target ] [ ' GROUP_VOICE ' ] :
if target_rule [ ' NAME ' ] == rule [ ' NAME ' ] :
target_rule [ ' ACTIVE ' ] = True
2016-08-06 20:02:28 -04:00
target_rule [ ' TIMER ' ] = now + target_rule [ ' TIMEOUT ' ]
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Reciprocal Bridge Rule \" %s \" in IPSC \" %s \" changed to state: %s ' , self . _system , target_rule [ ' NAME ' ] , _target , rule [ ' ACTIVE ' ] )
2016-05-01 11:49:30 -04:00
# TGID matches an DE-ACTIVATION trigger
if _dst_group in rule [ ' OFF ' ] :
# Set the matching rule as ACTIVE
rule [ ' ACTIVE ' ] = False
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Bridge Rule \" %s \" changed to state: %s ' , self . _system , rule [ ' NAME ' ] , rule [ ' ACTIVE ' ] )
2016-05-01 11:49:30 -04:00
# Set reciprocal rules for other IPSCs as ACTIVE
_target = rule [ ' DST_NET ' ]
for target_rule in RULES [ _target ] [ ' GROUP_VOICE ' ] :
if target_rule [ ' NAME ' ] == rule [ ' NAME ' ] :
target_rule [ ' ACTIVE ' ] = False
2016-12-18 22:51:13 -05:00
self . _logger . info ( ' ( %s ) Reciprocal Bridge Rule \" %s \" in IPSC \" %s \" changed to state: %s ' , self . _system , target_rule [ ' NAME ' ] , _target , rule [ ' ACTIVE ' ] )
2016-05-01 11:49:30 -04:00
#
# END IN-BAND SIGNALLING
#
2015-05-28 10:42:14 -04:00
2016-12-18 22:51:13 -05:00
def group_data ( self , _src_sub , _dst_sub , _ts , _end , _peerid , _data ) :
self . _logger . debug ( ' ( %s ) Group Data Packet Received From: %s , IPSC Peer %s , Destination %s ' , self . _system , int_id ( _src_sub ) , int_id ( _peerid ) , int_id ( _dst_sub ) )
2015-05-21 10:39:42 -04:00
2016-12-18 22:51:13 -05:00
for target in RULES [ self . _system ] [ ' GROUP_DATA ' ] :
2015-05-28 10:42:14 -04:00
2016-11-23 17:37:37 -05:00
if self . BRIDGE == True or systems [ target ] . BRIDGE == True :
2015-05-28 10:42:14 -04:00
_tmp_data = _data
# Re-Write the IPSC SRC to match the target network's ID
2016-12-18 22:51:13 -05:00
_tmp_data = _tmp_data . replace ( _peerid , self . _CONFIG [ target ] [ ' LOCAL ' ] [ ' RADIO_ID ' ] )
2015-05-28 10:42:14 -04:00
# Send the packet to all peers in the target IPSC
2016-11-23 17:37:37 -05:00
systems [ target ] . send_to_ipsc ( _tmp_data )
2015-05-28 10:42:14 -04:00
2016-12-18 22:51:13 -05:00
def private_data ( self , _src_sub , _dst_sub , _ts , _end , _peerid , _data ) :
self . _logger . debug ( ' ( %s ) Private Data Packet Received From: %s , IPSC Peer %s , Destination %s ' , self . _system , int_id ( _src_sub ) , int_id ( _peerid ) , int_id ( _dst_sub ) )
2015-05-21 10:39:42 -04:00
2016-12-18 22:51:13 -05:00
for target in RULES [ self . _system ] [ ' PRIVATE_DATA ' ] :
2015-08-14 09:36:29 -04:00
2016-11-23 17:37:37 -05:00
if self . BRIDGE == True or systems [ target ] . BRIDGE == True :
2015-05-28 10:42:14 -04:00
_tmp_data = _data
# Re-Write the IPSC SRC to match the target network's ID
2016-12-18 22:51:13 -05:00
_tmp_data = _tmp_data . replace ( _peerid , self . _CONFIG [ target ] [ ' LOCAL ' ] [ ' RADIO_ID ' ] )
2015-05-28 10:42:14 -04:00
# Send the packet to all peers in the target IPSC
2016-11-23 17:37:37 -05:00
systems [ target ] . send_to_ipsc ( _tmp_data )
2014-08-31 12:27:00 -04:00
2016-12-15 16:04:45 -05:00
2016-12-18 22:51:13 -05:00
if __name__ == ' __main__ ' :
2016-12-16 09:10:33 -05:00
import argparse
import os
import signal
from dmr_utils . utils import try_download , mk_id_dict
import dmrlink_log
import dmrlink_config
2016-12-15 16:04:45 -05:00
# Change the current directory to the location of the application
os . chdir ( os . path . dirname ( os . path . realpath ( sys . argv [ 0 ] ) ) )
# CLI argument parser - handles picking up the config file from the command line, and sending a "help" message
parser = argparse . ArgumentParser ( )
parser . add_argument ( ' -c ' , ' --config ' , action = ' store ' , dest = ' CFG_FILE ' , help = ' /full/path/to/config.file (usually dmrlink.cfg) ' )
cli_args = parser . parse_args ( )
if not cli_args . CFG_FILE :
cli_args . CFG_FILE = os . path . dirname ( os . path . abspath ( __file__ ) ) + ' /dmrlink.cfg '
# Call the external routine to build the configuration dictionary
2016-12-16 09:10:33 -05:00
CONFIG = dmrlink_config . build_config ( cli_args . CFG_FILE )
2016-12-15 16:04:45 -05:00
# Call the external routing to start the system logger
2016-12-16 09:10:33 -05:00
logger = dmrlink_log . config_logging ( CONFIG [ ' LOGGER ' ] )
2016-12-15 16:04:45 -05:00
config_reports ( CONFIG )
2015-05-13 10:40:56 -04:00
logger . info ( ' DMRlink \' bridge.py \' (c) 2013-2015 N0MJS & the K0USY Group - SYSTEM STARTING... ' )
2015-06-30 21:36:14 -04:00
2016-12-15 16:04:45 -05:00
# Shut ourselves down gracefully with the IPSC peers.
def sig_handler ( _signal , _frame ) :
logger . info ( ' *** DMRLINK IS TERMINATING WITH SIGNAL %s *** ' , str ( _signal ) )
for system in systems :
this_ipsc = systems [ system ]
logger . info ( ' De-Registering from IPSC %s ' , system )
de_reg_req_pkt = this_ipsc . hashed_packet ( this_ipsc . _local [ ' AUTH_KEY ' ] , this_ipsc . DE_REG_REQ_PKT )
this_ipsc . send_to_ipsc ( de_reg_req_pkt )
reactor . stop ( )
# Set signal handers so that we can gracefully exit if need be
for sig in [ signal . SIGTERM , signal . SIGINT , signal . SIGQUIT ] :
signal . signal ( sig , sig_handler )
2015-06-30 21:36:14 -04:00
2016-12-16 09:10:33 -05:00
# Build the routing rules file
RULES = build_rules ( ' bridge_rules ' )
# Build list of known bridge IDs
BRIDGES = build_bridges ( ' known_bridges ' )
2016-12-18 22:51:13 -05:00
2016-12-16 09:10:33 -05:00
# Build the Access Control List
ACL = build_acl ( ' sub_acl ' )
2016-12-15 16:04:45 -05:00
# INITIALIZE AN IPSC OBJECT (SELF SUSTAINING) FOR EACH CONFIGUED IPSC
for system in CONFIG [ ' SYSTEMS ' ] :
if CONFIG [ ' SYSTEMS ' ] [ system ] [ ' LOCAL ' ] [ ' ENABLED ' ] :
2016-12-18 22:51:13 -05:00
systems [ system ] = bridgeIPSC ( system , CONFIG , logger , BRIDGES )
2016-12-15 16:04:45 -05:00
reactor . listenUDP ( CONFIG [ ' SYSTEMS ' ] [ system ] [ ' LOCAL ' ] [ ' PORT ' ] , systems [ system ] , interface = CONFIG [ ' SYSTEMS ' ] [ system ] [ ' LOCAL ' ] [ ' IP ' ] )
2015-06-30 21:36:14 -04:00
# INITIALIZE THE REPORTING LOOP IF CONFIGURED
2016-11-23 17:55:39 -05:00
if CONFIG [ ' REPORTS ' ] [ ' REPORT_NETWORKS ' ] :
2016-12-18 22:51:13 -05:00
reporting_loop = config_reports ( CONFIG )
reporting = task . LoopingCall ( reporting_loop , logger )
2016-11-23 17:55:39 -05:00
reporting . start ( CONFIG [ ' REPORTS ' ] [ ' REPORT_INTERVAL ' ] )
2016-08-05 10:20:06 -04:00
# INITIALIZE THE REPORTING LOOP IF CONFIGURED
rule_timer = task . LoopingCall ( rule_timer_loop )
2016-09-15 20:57:39 -04:00
rule_timer . start ( 60 )
2016-12-15 16:04:45 -05:00
2014-04-29 23:00:38 -04:00
reactor . run ( )