CLIENT and MASTER are now one class

This commit is contained in:
Cort Buffington 2016-11-16 11:05:01 -06:00
parent 93ce2650f9
commit 25c240d0a3
2 changed files with 98 additions and 182 deletions

View File

@ -23,7 +23,7 @@ from twisted.internet import reactor
from twisted.internet import task from twisted.internet import task
# Things we import from the main hblink module # Things we import from the main hblink module
from hblink import CONFIG, HBMASTER, HBCLIENT, logger, systems, hex_str_3, int_id from hblink import CONFIG, HBSYSTEM, logger, systems, hex_str_3, int_id
import dec_dmr import dec_dmr
import bptc import bptc
import constants as const import constants as const
@ -71,10 +71,10 @@ __email__ = 'n0mjs@me.com'
__status__ = 'pre-alpha' __status__ = 'pre-alpha'
class routerMASTER(HBMASTER): class routerSYSTEM(HBSYSTEM):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
HBMASTER.__init__(self, *args, **kwargs) HBSYSTEM.__init__(self, *args, **kwargs)
# Status information for the system, TS1 & TS2 # Status information for the system, TS1 & TS2
# 1 & 2 are "timeslot" # 1 & 2 are "timeslot"
@ -126,11 +126,11 @@ class routerMASTER(HBMASTER):
# Is this a new call stream? # Is this a new call stream?
if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']):
if ((self.STATUS[_slot]['RX_TYPE'] != const.HBPF_SLT_VTERM) or (pkt_time < self.STATUS[_slot]['RX_TIME'] + const.STREAM_TO)): if ((self.STATUS[_slot]['RX_TYPE'] != const.HBPF_SLT_VTERM) or (pkt_time < self.STATUS[_slot]['RX_TIME'] + const.STREAM_TO)):
logger.warning('(%s) Packet received with STREAM ID: %s <FROM> SUB: %s REPEATER: %s <TO> TGID %s, SLOT %s collided with existing call', self._master, int_id(_stream_id), int_id(_rf_src), int_id(_radio_id), int_id(_dst_id), _slot) logger.warning('(%s) Packet received with STREAM ID: %s <FROM> SUB: %s REPEATER: %s <TO> TGID %s, SLOT %s collided with existing call', self._system, int_id(_stream_id), int_id(_rf_src), int_id(_radio_id), int_id(_dst_id), _slot)
return return
# This is a new call stream # This is a new call stream
logger.info('(%s) Call stream START with STREAM ID: %s <FROM> SUB: %s REPEATER: %s <TO> TGID %s, SLOT %s', self._master, int_id(_stream_id), int_id(_rf_src), int_id(_radio_id), int_id(_dst_id), _slot) logger.info('(%s) Call stream START with STREAM ID: %s <FROM> SUB: %s REPEATER: %s <TO> TGID %s, SLOT %s', self._system, int_id(_stream_id), int_id(_rf_src), int_id(_radio_id), int_id(_dst_id), _slot)
# If we can, use the LC from the voice header as to keep all options intact # If we can, use the LC from the voice header as to keep all options intact
if _frame_type == const.HBPF_DATA_SYNC and _dtype_vseq == const.HBPF_SLT_VHEAD: if _frame_type == const.HBPF_DATA_SYNC and _dtype_vseq == const.HBPF_SLT_VHEAD:
@ -144,7 +144,7 @@ class routerMASTER(HBMASTER):
for rule in RULES[self._master]['GROUP_VOICE']: for rule in RULES[self._system]['GROUP_VOICE']:
_target = rule['DST_NET'] _target = rule['DST_NET']
_target_status = systems[_target].STATUS _target_status = systems[_target].STATUS
@ -157,13 +157,13 @@ class routerMASTER(HBMASTER):
# From the same group as the last TX to the target HBP system, but stream ID is different, and it is less than stream timout # From the same group as the last TX to the target HBP system, but stream ID is different, and it is less than stream timout
# The "continue" at the end of each means the next iteration of the for loop that tests for matching rules # The "continue" at the end of each means the next iteration of the for loop that tests for matching rules
# #
if ((rule['DST_GROUP'] != _target_status[_slot]['TX_TGID']) and ((pkt_time - self.STATUS[_slot]['RX_TIME']) < RULES[self._master]['GROUP_HANGTIME'])): if ((rule['DST_GROUP'] != _target_status[_slot]['TX_TGID']) and ((pkt_time - self.STATUS[_slot]['RX_TIME']) < RULES[self._system]['GROUP_HANGTIME'])):
if const.HBPF_DATA_SYNC and _dtype_vseq == const.HBPF_SLT_VHEAD: if const.HBPF_DATA_SYNC and _dtype_vseq == const.HBPF_SLT_VHEAD:
logger.info('(%s) Call not routed, target active or in group hangtime: HBP system %s, TS%s, TGID%s', self._master, _target, _slot, int_id(rule['DST_GROUP'])) logger.info('(%s) Call not routed, target active or in group hangtime: HBP system %s, TS%s, TGID%s', self._system, _target, _slot, int_id(rule['DST_GROUP']))
continue continue
if (rule['DST_GROUP'] == self.STATUS[_slot]['TX_TGID']) and (_stream_id != self.STATUS[_slot]['TX_STREAM_ID']) and ((pkt_time - _status[_slot]['TX_TIME']) < const.STREAM_TO): if (rule['DST_GROUP'] == self.STATUS[_slot]['TX_TGID']) and (_stream_id != self.STATUS[_slot]['TX_STREAM_ID']) and ((pkt_time - _status[_slot]['TX_TIME']) < const.STREAM_TO):
if const.HBPF_DATA_SYNC and _dtype_vseq == const.HBPF_SLT_VHEAD: if const.HBPF_DATA_SYNC and _dtype_vseq == const.HBPF_SLT_VHEAD:
logger.info('(%s) Call not routed, call bridge in progress from %s, target: HBP system %s, TS%s, TGID%s', self._master, int_id(_src_sub), _target, _slot, int_id(rule['DST_GROUP'])) logger.info('(%s) Call not routed, call bridge in progress from %s, target: HBP system %s, TS%s, TGID%s', self._system, int_id(_src_sub), _target, _slot, int_id(rule['DST_GROUP']))
continue continue
# Set values for the contention handler to test next time there is a frame to forward # Set values for the contention handler to test next time there is a frame to forward
@ -173,7 +173,7 @@ class routerMASTER(HBMASTER):
_target_status[_slot]['TX_TGID'] = rule['DST_GROUP'] _target_status[_slot]['TX_TGID'] = rule['DST_GROUP']
_target_status[_slot]['TX_STREAM_ID'] = _stream_id _target_status[_slot]['TX_STREAM_ID'] = _stream_id
_target_status[_slot]['TX_LC'] = bptc.encode_header_lc(self.STATUS[_slot]['RX_LC'][0:3] + rule['DST_GROUP'] + _rf_src) _target_status[_slot]['TX_LC'] = bptc.encode_header_lc(self.STATUS[_slot]['RX_LC'][0:3] + rule['DST_GROUP'] + _rf_src)
print('new stream id, calcuate/store stuff', h(bptc.decode_full_lc(_target_status[_slot]['TX_LC']).tobytes())) #make EMB LC fragments next
# Handle any necessary re-writes for the destination # Handle any necessary re-writes for the destination
if rule['SRC_TS'] != rule['DST_TS']: if rule['SRC_TS'] != rule['DST_TS']:
@ -191,14 +191,14 @@ class routerMASTER(HBMASTER):
# Transmit the packet to the destination system # Transmit the packet to the destination system
systems[_target].send_system(_tmp_data) systems[_target].send_system(_tmp_data)
logger.debug('(%s) Packet routed to %s system: %s', self._master, CONFIG['SYSTEMS'][_target]['MODE'], _target) logger.debug('(%s) Packet routed by rule: %s to %s system: %s', self._system, rule['NAME'], CONFIG['SYSTEMS'][_target]['MODE'], _target)
# Final actions - Is this a voice terminator? # Final actions - Is this a voice terminator?
if (_frame_type == const.HBPF_DATA_SYNC) and (_dtype_vseq == const.HBPF_SLT_VTERM) and (self.STATUS[_slot]['RX_TYPE'] != const.HBPF_SLT_VTERM): if (_frame_type == const.HBPF_DATA_SYNC) and (_dtype_vseq == const.HBPF_SLT_VTERM) and (self.STATUS[_slot]['RX_TYPE'] != const.HBPF_SLT_VTERM):
self.STATUS[_slot]['LC'] = '' self.STATUS[_slot]['LC'] = ''
logger.info('(%s) Call stream END with STREAM ID: %s <FROM> SUB: %s REPEATER: %s <TO> TGID %s, SLOT %s', self._master, int_id(_stream_id), int_id(_rf_src), int_id(_radio_id), int_id(_dst_id), _slot) logger.info('(%s) Call stream END with STREAM ID: %s <FROM> SUB: %s REPEATER: %s <TO> TGID %s, SLOT %s', self._system, int_id(_stream_id), int_id(_rf_src), int_id(_radio_id), int_id(_dst_id), _slot)
# Mark status variables for use later # Mark status variables for use later
self.STATUS[_slot]['RX_TYPE'] = _dtype_vseq self.STATUS[_slot]['RX_TYPE'] = _dtype_vseq
@ -206,54 +206,6 @@ class routerMASTER(HBMASTER):
self.STATUS[_slot]['RX_TIME'] = pkt_time self.STATUS[_slot]['RX_TIME'] = pkt_time
self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id
class routerCLIENT(HBCLIENT):
def __init__(self, *args, **kwargs):
HBCLIENT.__init__(self, *args, **kwargs)
# Status information for the system, TS1 & TS2
# 1 & 2 are "timeslot"
# In TX_EMB_LC, 2-5 are burst B-E
self.STATUS = {
1: {
'RX_STREAM_ID': '\x00',
'TX_STREAM_ID': '\x00',
'RX_TGID': '\x00',
'TX_TGID': '\x00',
'RX_TIME': time(),
'TX_TIME': time(),
'RX_TYPE': const.HBPF_SLT_VTERM,
'RX_LC': '\x00',
'TX_LC': '\x00',
'TX_EMB_LC': {
2: '\x00',
3: '\x00',
4: '\x00',
5: '\x00',
}
},
2: {
'RX_STREAM_ID': '\x00',
'TX_STREAM_ID': '\x00',
'RX_TGID': '\x00',
'TX_TGID': '\x00',
'RX_TIME': time(),
'TX_TIME': time(),
'RX_TYPE': const.HBPF_SLT_VTERM,
'RX_LC': '\x00',
'TX_LC': '\x00',
'TX_EMB_LC': {
2: '\x00',
3: '\x00',
4: '\x00',
5: '\x00',
}
}
}
def dmrd_received(self, _radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data):
return
#************************************************ #************************************************
# MAIN PROGRAM LOOP STARTS HERE # MAIN PROGRAM LOOP STARTS HERE
@ -262,13 +214,11 @@ class routerCLIENT(HBCLIENT):
if __name__ == '__main__': if __name__ == '__main__':
logger.info('HBlink \'hb_router.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') logger.info('HBlink \'hb_router.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...')
# HBlink instance creation
# HBlink instance creation # HBlink instance creation
for system in CONFIG['SYSTEMS']: for system in CONFIG['SYSTEMS']:
if CONFIG['SYSTEMS'][system]['ENABLED']: if CONFIG['SYSTEMS'][system]['ENABLED']:
if CONFIG['SYSTEMS'][system]['MODE'] == 'MASTER': systems[system] = routerSYSTEM(system)
systems[system] = routerMASTER(system)
elif CONFIG['SYSTEMS'][system]['MODE'] == 'CLIENT':
systems[system] = routerCLIENT(system)
reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP'])
logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system])

202
hblink.py
View File

@ -212,42 +212,70 @@ class AMBE:
# HB MASTER CLASS # HB MASTER CLASS
#************************************************ #************************************************
class HBMASTER(DatagramProtocol): class HBSYSTEM(DatagramProtocol):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if len(args) == 1: if len(args) == 1:
# Define a few shortcuts to make the rest of the class more readable # Define a few shortcuts to make the rest of the class more readable
self._master = args[0] self._system = args[0]
self._system = self._master self._config = CONFIG['SYSTEMS'][self._system]
self._config = CONFIG['SYSTEMS'][self._master]
self._clients = CONFIG['SYSTEMS'][self._master]['CLIENTS'] # Define shortcuts and generic function names based on the type of system we are
if self._config['MODE'] == 'MASTER':
self._clients = CONFIG['SYSTEMS'][self._system]['CLIENTS']
self.send_system = self.send_clients
self.maintenance_loop = self.master_maintenance_loop
self.datagramReceived = self.master_datagramReceived
elif self._config['MODE'] == 'CLIENT':
self._stats = self._config['STATS']
self.send_system = self.send_master
self.maintenance_loop = self.client_maintenance_loop
self.datagramReceived = self.client_datagramReceived
# Configure for AMBE audio export if enabled # Configure for AMBE audio export if enabled
if self._config['EXPORT_AMBE']: if self._config['EXPORT_AMBE']:
self._ambe = AMBE() self._ambe = AMBE()
else: else:
# If we didn't get called correctly, log it and quit. # If we didn't get called correctly, log it and quit.
logger.error('(%s) HBMASTER was not called with an argument. Terminating', self._master) logger.error('(%s) HBMASTER was not called with an argument. Terminating', self._system)
sys.exit() sys.exit()
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._master_maintenance = task.LoopingCall(self.master_maintenance_loop) self._system_maintenance = task.LoopingCall(self.maintenance_loop)
self._master_maintenance_loop = self._master_maintenance.start(CONFIG['GLOBAL']['PING_TIME']) self._system_maintenance_loop = self._system_maintenance.start(CONFIG['GLOBAL']['PING_TIME'])
# Aliased in __init__ to maintenance_loop if system is a master
def master_maintenance_loop(self): def master_maintenance_loop(self):
logger.debug('(%s) Master maintenance loop started', self._master) logger.debug('(%s) Master maintenance loop started', self._system)
for client in self._clients: for client in self._clients:
_this_client = self._clients[client] _this_client = self._clients[client]
# Check to see if any of the clients have been quiet (no ping) longer than allowed # Check to see if any of the clients have been quiet (no ping) longer than allowed
if _this_client['LAST_PING']+CONFIG['GLOBAL']['PING_TIME']*CONFIG['GLOBAL']['MAX_MISSED'] < time(): if _this_client['LAST_PING']+CONFIG['GLOBAL']['PING_TIME']*CONFIG['GLOBAL']['MAX_MISSED'] < time():
logger.info('(%s) Client %s (%s) has timed out', self._master, _this_client['CALLSIGN'], _this_client['RADIO_ID']) logger.info('(%s) Client %s (%s) has timed out', self._system, _this_client['CALLSIGN'], _this_client['RADIO_ID'])
# Remove any timed out clients from the configuration # Remove any timed out clients from the configuration
del CONFIG['SYSTEMS'][self._master]['CLIENTS'][client] del CONFIG['SYSTEMS'][self._system]['CLIENTS'][client]
# Aliased in __init__ to maintenance_loop if system is a client
def client_maintenance_loop(self):
logger.debug('(%s) Client maintenance loop started', self._system)
# If we're not connected, zero out the stats and send a login request RPTL
if self._stats['CONNECTION'] == 'NO' or self._stats['CONNECTION'] == 'RTPL_SENT':
self._stats['PINGS_SENT'] = 0
self._stats['PINGS_ACKD'] = 0
self._stats['CONNECTION'] = 'RTPL_SENT'
self.send_master('RPTL'+self._config['RADIO_ID'])
logger.info('(%s) Sending login request to master %s:%s', self._system, self._config['MASTER_IP'], self._config['MASTER_PORT'])
# If we are connected, sent a ping to the master and increment the counter
if self._stats['CONNECTION'] == 'YES':
self.send_master('RPTPING'+self._config['RADIO_ID'])
self._stats['PINGS_SENT'] += 1
logger.debug('(%s) RPTPING Sent to Master. Pings Since Connected: %s', self._system, self._stats['PINGS_SENT'])
def send_clients(self, _packet): def send_clients(self, _packet):
for _client in self._clients: for _client in self._clients:
self.send_client(_client, _packet) self.send_client(_client, _packet)
#logger.debug('(%s) Packet sent to client %s', self._master, self._clients[_client]['RADIO_ID']) #logger.debug('(%s) Packet sent to client %s', self._system, self._clients[_client]['RADIO_ID'])
def send_client(self, _client, _packet): def send_client(self, _client, _packet):
_ip = self._clients[_client]['IP'] _ip = self._clients[_client]['IP']
@ -256,16 +284,18 @@ class HBMASTER(DatagramProtocol):
# KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!! # KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!!
#logger.debug('(%s) TX Packet to %s on port %s: %s', self._clients[_client]['RADIO_ID'], self._clients[_client]['IP'], self._clients[_client]['PORT'], h(_packet)) #logger.debug('(%s) TX Packet to %s on port %s: %s', self._clients[_client]['RADIO_ID'], self._clients[_client]['IP'], self._clients[_client]['PORT'], h(_packet))
# Alias for other programs to use a common name to send a packet def send_master(self, _packet):
# regardless of the system type (MASTER or CLIENT) self.transport.write(_packet, (self._config['MASTER_IP'], self._config['MASTER_PORT']))
send_system = send_clients # KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!!
#logger.debug('(%s) TX Packet to %s:%s -- %s', self._system, self._config['MASTER_IP'], self._config['MASTER_PORT'], h(_packet))
def dmrd_received(self, _radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data): def dmrd_received(self, _radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data):
pass pass
def datagramReceived(self, _data, (_host, _port)): # Aliased in __init__ to datagramReceived if system is a master
def master_datagramReceived(self, _data, (_host, _port)):
# Keep This Line Commented Unless HEAVILY Debugging! # Keep This Line Commented Unless HEAVILY Debugging!
#logger.debug('(%s) RX packet from %s:%s -- %s', self._master, _host, _port, h(_data)) #logger.debug('(%s) RX packet from %s:%s -- %s', self._system, _host, _port, h(_data))
# Extract the command, which is various length, all but one 4 significant characters -- RPTCL # Extract the command, which is various length, all but one 4 significant characters -- RPTCL
_command = _data[:4] _command = _data[:4]
@ -285,18 +315,18 @@ class HBMASTER(DatagramProtocol):
_frame_type = (_bits & 0x30) >> 4 _frame_type = (_bits & 0x30) >> 4
_dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F _dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F
_stream_id = _data[16:20] _stream_id = _data[16:20]
#logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._master, int_id(_seq), int_id(_rf_src), int_id(_dst_id)) #logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id))
# If AMBE audio exporting is configured... # If AMBE audio exporting is configured...
if self._config['EXPORT_AMBE']: if self._config['EXPORT_AMBE']:
self._ambe.parseAMBE(self._master, _data) self._ambe.parseAMBE(self._system, _data)
# The basic purpose of a master is to repeat to the clients # The basic purpose of a master is to repeat to the clients
if self._config['REPEAT'] == True: if self._config['REPEAT'] == True:
for _client in self._clients: for _client in self._clients:
if _client != _radio_id: if _client != _radio_id:
self.send_client(_client, _data) self.send_client(_client, _data)
logger.debug('(%s) Packet on TS%s from %s (%s) for destination ID %s repeated to client: %s (%s) [Stream ID: %s]', self._master, _slot, self._clients[_radio_id]['CALLSIGN'], int_id(_radio_id), int_id(_dst_id), self._clients[_client]['CALLSIGN'], int_id(_client), int_id(_stream_id)) logger.debug('(%s) Packet on TS%s from %s (%s) for destination ID %s repeated to client: %s (%s) [Stream ID: %s]', self._system, _slot, self._clients[_radio_id]['CALLSIGN'], int_id(_radio_id), int_id(_dst_id), self._clients[_client]['CALLSIGN'], int_id(_client), int_id(_stream_id))
# Userland actions -- typically this is the function you subclass for an application # Userland actions -- typically this is the function you subclass for an application
self.dmrd_received(_radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data) self.dmrd_received(_radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data)
@ -327,14 +357,14 @@ class HBMASTER(DatagramProtocol):
'SOFTWARE_ID': '', 'SOFTWARE_ID': '',
'PACKAGE_ID': '', 'PACKAGE_ID': '',
}}) }})
logger.info('(%s) Repeater Logging in with Radio ID: %s, %s:%s', self._master, int_id(_radio_id), _host, _port) logger.info('(%s) Repeater Logging in with Radio ID: %s, %s:%s', self._system, int_id(_radio_id), _host, _port)
_salt_str = hex_str_4(self._clients[_radio_id]['SALT']) _salt_str = hex_str_4(self._clients[_radio_id]['SALT'])
self.send_client(_radio_id, 'RPTACK'+_salt_str) self.send_client(_radio_id, 'RPTACK'+_salt_str)
self._clients[_radio_id]['CONNECTION'] = 'CHALLENGE_SENT' self._clients[_radio_id]['CONNECTION'] = 'CHALLENGE_SENT'
logger.info('(%s) Sent Challenge Response to %s for login: %s', self._master, int_id(_radio_id), self._clients[_radio_id]['SALT']) logger.info('(%s) Sent Challenge Response to %s for login: %s', self._system, int_id(_radio_id), self._clients[_radio_id]['SALT'])
else: else:
self.transport.write('MSTNAK'+_radio_id, (_host, _port)) self.transport.write('MSTNAK'+_radio_id, (_host, _port))
logger.warning('(%s) Invalid Login from Radio ID: %s', self._master, int_id(_radio_id)) logger.warning('(%s) Invalid Login from Radio ID: %s', self._system, int_id(_radio_id))
elif _command == 'RPTK': # Repeater has answered our login challenge elif _command == 'RPTK': # Repeater has answered our login challenge
_radio_id = _data[4:8] _radio_id = _data[4:8]
@ -350,14 +380,14 @@ class HBMASTER(DatagramProtocol):
if _sent_hash == _calc_hash: if _sent_hash == _calc_hash:
_this_client['CONNECTION'] = 'WAITING_CONFIG' _this_client['CONNECTION'] = 'WAITING_CONFIG'
self.send_client(_radio_id, 'RPTACK'+_radio_id) self.send_client(_radio_id, 'RPTACK'+_radio_id)
logger.info('(%s) Client %s has completed the login exchange successfully', self._master, _this_client['RADIO_ID']) logger.info('(%s) Client %s has completed the login exchange successfully', self._system, _this_client['RADIO_ID'])
else: else:
logger.info('(%s) Client %s has FAILED the login exchange successfully', self._master, _this_client['RADIO_ID']) logger.info('(%s) Client %s has FAILED the login exchange successfully', self._system, _this_client['RADIO_ID'])
self.transport.write('MSTNAK'+_radio_id, (_host, _port)) self.transport.write('MSTNAK'+_radio_id, (_host, _port))
del self._clients[_radio_id] del self._clients[_radio_id]
else: else:
self.transport.write('MSTNAK'+_radio_id, (_host, _port)) self.transport.write('MSTNAK'+_radio_id, (_host, _port))
logger.warning('(%s) Login challenge from Radio ID that has not logged in: %s', self._master, int_id(_radio_id)) logger.warning('(%s) Login challenge from Radio ID that has not logged in: %s', self._system, int_id(_radio_id))
elif _command == 'RPTC': # Repeater is sending it's configuraiton OR disconnecting elif _command == 'RPTC': # Repeater is sending it's configuraiton OR disconnecting
if _data[:5] == 'RPTCL': # Disconnect command if _data[:5] == 'RPTCL': # Disconnect command
@ -366,7 +396,7 @@ class HBMASTER(DatagramProtocol):
and self._clients[_radio_id]['CONNECTION'] == 'YES' \ and self._clients[_radio_id]['CONNECTION'] == 'YES' \
and self._clients[_radio_id]['IP'] == _host \ and self._clients[_radio_id]['IP'] == _host \
and self._clients[_radio_id]['PORT'] == _port: and self._clients[_radio_id]['PORT'] == _port:
logger.info('(%s) Client is closing down: %s (%s)', self._master, self._clients[_radio_id]['CALLSIGN'], int_id(_radio_id)) logger.info('(%s) Client is closing down: %s (%s)', self._system, self._clients[_radio_id]['CALLSIGN'], int_id(_radio_id))
self.transport.write('MSTNAK'+_radio_id, (_host, _port)) self.transport.write('MSTNAK'+_radio_id, (_host, _port))
del self._clients[_radio_id] del self._clients[_radio_id]
else: else:
@ -394,10 +424,10 @@ class HBMASTER(DatagramProtocol):
_this_client['PACKAGE_ID'] = _data[264:304] _this_client['PACKAGE_ID'] = _data[264:304]
self.send_client(_radio_id, 'RPTACK'+_radio_id) self.send_client(_radio_id, 'RPTACK'+_radio_id)
logger.info('(%s) Client %s (%s) has sent repeater configuration', self._master, _this_client['CALLSIGN'], _this_client['RADIO_ID']) logger.info('(%s) Client %s (%s) has sent repeater configuration', self._system, _this_client['CALLSIGN'], _this_client['RADIO_ID'])
else: else:
self.transport.write('MSTNAK'+_radio_id, (_host, _port)) self.transport.write('MSTNAK'+_radio_id, (_host, _port))
logger.warning('(%s) Client info from Radio ID that has not logged in: %s', self._master, int_id(_radio_id)) logger.warning('(%s) Client info from Radio ID that has not logged in: %s', self._system, int_id(_radio_id))
elif _command == 'RPTP': # RPTPing -- client is pinging us elif _command == 'RPTP': # RPTPing -- client is pinging us
_radio_id = _data[7:11] _radio_id = _data[7:11]
@ -407,70 +437,18 @@ class HBMASTER(DatagramProtocol):
and self._clients[_radio_id]['PORT'] == _port: and self._clients[_radio_id]['PORT'] == _port:
self._clients[_radio_id]['LAST_PING'] = time() self._clients[_radio_id]['LAST_PING'] = time()
self.send_client(_radio_id, 'MSTPONG'+_radio_id) self.send_client(_radio_id, 'MSTPONG'+_radio_id)
logger.debug('(%s) Received and answered RPTPING from client %s (%s)', self._master, self._clients[_radio_id]['CALLSIGN'], int_id(_radio_id)) logger.debug('(%s) Received and answered RPTPING from client %s (%s)', self._system, self._clients[_radio_id]['CALLSIGN'], int_id(_radio_id))
else: else:
self.transport.write('MSTNAK'+_radio_id, (_host, _port)) self.transport.write('MSTNAK'+_radio_id, (_host, _port))
logger.warning('(%s) Client info from Radio ID that has not logged in: %s', self._master, int_id(_radio_id)) logger.warning('(%s) Client info from Radio ID that has not logged in: %s', self._system, int_id(_radio_id))
else: else:
logger.error('(%s) Unrecognized command from: %s. Packet: %s', self._master, int_id(_radio_id), h(_data)) logger.error('(%s) Unrecognized command from: %s. Packet: %s', self._system, int_id(_radio_id), h(_data))
#************************************************ # Aliased in __init__ to datagramReceived if system is a client
# HB CLIENT CLASS def client_datagramReceived(self, _data, (_host, _port)):
#************************************************
class HBCLIENT(DatagramProtocol):
def __init__(self, *args, **kwargs):
if len(args) == 1:
self._client = args[0]
self._system = self._client
self._config = CONFIG['SYSTEMS'][self._client]
self._stats = self._config['STATS']
# Configure for AMBE audio export if enabled
if self._config['EXPORT_AMBE']:
self._ambe = AMBE()
else:
# If we didn't get called correctly, log it!
logger.error('(%s) HBCLIENT was not called with an argument. Terminating', self._client)
sys.exit()
def startProtocol(self):
# Set up periodic loop for sending pings to the master. Run every 'PING_TIME' seconds
self._client_maintenance = task.LoopingCall(self.client_maintenance_loop)
self._client_maintenance_loop = self._client_maintenance.start(CONFIG['GLOBAL']['PING_TIME'])
def client_maintenance_loop(self):
logger.debug('(%s) Client maintenance loop started', self._client)
# If we're not connected, zero out the stats and send a login request RPTL
if self._stats['CONNECTION'] == 'NO' or self._stats['CONNECTION'] == 'RTPL_SENT':
self._stats['PINGS_SENT'] = 0
self._stats['PINGS_ACKD'] = 0
self._stats['CONNECTION'] = 'RTPL_SENT'
self.send_master('RPTL'+self._config['RADIO_ID'])
logger.info('(%s) Sending login request to master %s:%s', self._client, self._config['MASTER_IP'], self._config['MASTER_PORT'])
# If we are connected, sent a ping to the master and increment the counter
if self._stats['CONNECTION'] == 'YES':
self.send_master('RPTPING'+self._config['RADIO_ID'])
self._stats['PINGS_SENT'] += 1
logger.debug('(%s) RPTPING Sent to Master. Pings Since Connected: %s', self._client, self._stats['PINGS_SENT'])
def send_master(self, _packet):
self.transport.write(_packet, (self._config['MASTER_IP'], self._config['MASTER_PORT']))
# KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!!
#logger.debug('(%s) TX Packet to %s:%s -- %s', self._client, self._config['MASTER_IP'], self._config['MASTER_PORT'], h(_packet))
# Alias for other programs to use a common name to send a packet
# regardless of the system type (MASTER or CLIENT)
send_system = send_master
def dmrd_received(self, _radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data):
pass
def datagramReceived(self, _data, (_host, _port)):
# Keep This Line Commented Unless HEAVILY Debugging! # Keep This Line Commented Unless HEAVILY Debugging!
# logger.debug('(%s) RX packet from %s:%s -- %s', self._client, _host, _port, h(_data)) # logger.debug('(%s) RX packet from %s:%s -- %s', self._system, _host, _port, h(_data))
# Validate that we receveived this packet from the master - security check! # Validate that we receveived this packet from the master - security check!
if self._config['MASTER_IP'] == _host and self._config['MASTER_PORT'] == _port: if self._config['MASTER_IP'] == _host and self._config['MASTER_PORT'] == _port:
@ -485,23 +463,14 @@ class HBCLIENT(DatagramProtocol):
_bits = int_id(_data[15]) _bits = int_id(_data[15])
_slot = 2 if (_bits & 0x80) else 1 _slot = 2 if (_bits & 0x80) else 1
_call_type = 'unit' if (_bits & 0x40) else 'group' _call_type = 'unit' if (_bits & 0x40) else 'group'
_raw_frame_type = (_bits & 0x30) >> 4 _frame_type = (_bits & 0x30) >> 4
if _raw_frame_type == 0b00:
_frame_type = 'voice'
elif _raw_frame_type == 0b01:
_frame_type = 'voice_sync'
elif _raw_frame_type == 0b10:
_frame_type = 'data_sync'
else:
_frame_type = 'none'
_dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F _dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F
_stream_id = _data[16:20] _stream_id = _data[16:20]
#logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id))
#logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._client, h(_seq), int_id(_rf_src), int_id(_dst_id))
# If AMBE audio exporting is configured... # If AMBE audio exporting is configured...
if self._config['EXPORT_AMBE']: if self._config['EXPORT_AMBE']:
self._ambe.parseAMBE(self._client, _data) self._ambe.parseAMBE(self._system, _data)
# Userland actions -- typically this is the function you subclass for an application # Userland actions -- typically this is the function you subclass for an application
self.dmrd_received(_radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data) self.dmrd_received(_radio_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data)
@ -509,14 +478,14 @@ class HBCLIENT(DatagramProtocol):
elif _command == 'MSTN': # Actually MSTNAK -- a NACK from the master elif _command == 'MSTN': # Actually MSTNAK -- a NACK from the master
_radio_id = _data[4:8] _radio_id = _data[4:8]
if _radio_id == self._config['RADIO_ID']: # Validate the source and intended target if _radio_id == self._config['RADIO_ID']: # Validate the source and intended target
logger.warning('(%s) MSTNAK Received', self._client) logger.warning('(%s) MSTNAK Received', self._system)
self._stats['CONNECTION'] = 'NO' # Disconnect ourselves and re-register self._stats['CONNECTION'] = 'NO' # Disconnect ourselves and re-register
elif _command == 'RPTA': # Actually RPTACK -- an ACK from the master elif _command == 'RPTA': # Actually RPTACK -- an ACK from the master
# Depending on the state, an RPTACK means different things, in each clause, we check and/or set the state # Depending on the state, an RPTACK means different things, in each clause, we check and/or set the state
if self._stats['CONNECTION'] == 'RTPL_SENT': # If we've sent a login request... if self._stats['CONNECTION'] == 'RTPL_SENT': # If we've sent a login request...
_login_int32 = _data[6:10] _login_int32 = _data[6:10]
logger.info('(%s) Repeater Login ACK Received with 32bit ID: %s', self._client, int_id(_login_int32)) logger.info('(%s) Repeater Login ACK Received with 32bit ID: %s', self._system, int_id(_login_int32))
_pass_hash = sha256(_login_int32+self._config['PASSPHRASE']).hexdigest() _pass_hash = sha256(_login_int32+self._config['PASSPHRASE']).hexdigest()
_pass_hash = a(_pass_hash) _pass_hash = a(_pass_hash)
self.send_master('RPTK'+self._config['RADIO_ID']+_pass_hash) self.send_master('RPTK'+self._config['RADIO_ID']+_pass_hash)
@ -524,7 +493,7 @@ class HBCLIENT(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...
if _data[6:10] == self._config['RADIO_ID']: if _data[6:10] == self._config['RADIO_ID']:
logger.info('(%s) Repeater Authentication Accepted', self._client) 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']+\
self._config['RX_FREQ']+\ self._config['RX_FREQ']+\
@ -543,32 +512,32 @@ class HBCLIENT(DatagramProtocol):
self.send_master('RPTC'+_config_packet) self.send_master('RPTC'+_config_packet)
self._stats['CONNECTION'] = 'CONFIG-SENT' self._stats['CONNECTION'] = 'CONFIG-SENT'
logger.info('(%s) Repeater Configuration Sent', self._client) logger.info('(%s) Repeater Configuration Sent', self._system)
else: else:
self._stats['CONNECTION'] = 'NO' self._stats['CONNECTION'] = 'NO'
logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._client) logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system)
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
if _data[6:10] == self._config['RADIO_ID']: if _data[6:10] == self._config['RADIO_ID']:
logger.info('(%s) Repeater Configuration Accepted', self._client) logger.info('(%s) Repeater Configuration Accepted', self._system)
self._stats['CONNECTION'] = 'YES' self._stats['CONNECTION'] = 'YES'
logger.info('(%s) Connection to Master Completed', self._client) logger.info('(%s) Connection to Master Completed', self._system)
else: else:
self._stats['CONNECTION'] = 'NO' self._stats['CONNECTION'] = 'NO'
logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._client) logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system)
elif _command == 'MSTP': # Actually MSTPONG -- a reply to RPTPING (send by client) elif _command == 'MSTP': # Actually MSTPONG -- a reply to RPTPING (send by client)
if _data [7:11] == self._config['RADIO_ID']: if _data [7:11] == self._config['RADIO_ID']:
self._stats['PINGS_ACKD'] += 1 self._stats['PINGS_ACKD'] += 1
logger.debug('(%s) MSTPONG Received. Pongs Since Connected: %s', self._client, self._stats['PINGS_ACKD']) logger.debug('(%s) MSTPONG Received. Pongs Since Connected: %s', self._system, self._stats['PINGS_ACKD'])
elif _command == 'MSTC': # Actually MSTCL -- notify us the master is closing down elif _command == 'MSTC': # Actually MSTCL -- notify us the master is closing down
if _data[5:9] == self._config['RADIO_ID']: if _data[5:9] == self._config['RADIO_ID']:
self._stats['CONNECTION'] = 'NO' self._stats['CONNECTION'] = 'NO'
logger.info('(%s) MSTCL Recieved', self._client) logger.info('(%s) MSTCL Recieved', self._system)
else: else:
logger.error('(%s) Received an invalid command in packet: %s', self._client, h(_data)) logger.error('(%s) Received an invalid command in packet: %s', self._system, h(_data))
#************************************************ #************************************************
@ -581,10 +550,7 @@ if __name__ == '__main__':
# HBlink instance creation # HBlink instance creation
for system in CONFIG['SYSTEMS']: for system in CONFIG['SYSTEMS']:
if CONFIG['SYSTEMS'][system]['ENABLED']: if CONFIG['SYSTEMS'][system]['ENABLED']:
if CONFIG['SYSTEMS'][system]['MODE'] == 'MASTER': systems[system] = HBSYSTEM(system)
systems[system] = HBMASTER(system)
elif CONFIG['SYSTEMS'][system]['MODE'] == 'CLIENT':
systems[system] = HBCLIENT(system)
reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP'])
logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system])