2015-06-28 22:28:21 +02:00
|
|
|
#!/usr/bin/python
|
2015-07-13 10:19:45 +02:00
|
|
|
# -*- coding: UTF-8 -*-
|
2015-06-28 22:28:21 +02:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
POCSAG Decoder
|
|
|
|
|
|
|
|
|
|
@author: Bastian Schroll
|
|
|
|
|
@author: Jens Herrmann
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging # Global logger
|
|
|
|
|
import re # Regex for validation
|
|
|
|
|
|
2016-10-03 11:56:27 +02:00
|
|
|
from includes import globalVars # Global variables
|
2015-06-28 22:28:21 +02:00
|
|
|
from includes import doubleFilter # double alarm filter
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
|
#
|
|
|
|
|
# Simple local filter
|
|
|
|
|
#
|
|
|
|
|
def isAllowed(poc_id):
|
|
|
|
|
"""
|
|
|
|
|
Simple Filter Functions (Allowed, Denied and Range)
|
|
|
|
|
|
|
|
|
|
@type poc_id: string
|
|
|
|
|
@param poc_id: POCSAG Ric
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2016-12-03 14:49:22 +01:00
|
|
|
@return: Checks both allow/deny-rule and filter-range (suitable for signal-RIC)
|
2015-06-28 22:28:21 +02:00
|
|
|
@exception: none
|
|
|
|
|
"""
|
2017-02-12 10:04:21 +01:00
|
|
|
|
2016-12-03 14:49:22 +01:00
|
|
|
allowed = 0
|
2019-10-13 17:56:09 +02:00
|
|
|
has_geo = False
|
2017-02-12 10:04:21 +01:00
|
|
|
|
2016-12-22 22:36:35 +01:00
|
|
|
# 1.) If allowed RICs is set, only they will path,
|
|
|
|
|
# If RIC is the right one return True, else False
|
2016-10-03 12:02:18 +02:00
|
|
|
if globalVars.config.get("POC", "allow_ric"):
|
|
|
|
|
if poc_id in globalVars.config.get("POC", "allow_ric"):
|
2015-06-28 22:28:21 +02:00
|
|
|
logging.info("RIC %s is allowed", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
logging.info("RIC %s is not in the allowed list", poc_id)
|
2016-12-03 14:49:22 +01:00
|
|
|
allowed = 0
|
2016-12-22 22:36:35 +01:00
|
|
|
# 2.) If denied RIC, return False
|
2016-12-03 14:49:22 +01:00
|
|
|
if poc_id in globalVars.config.get("POC", "deny_ric"):
|
2015-06-28 22:28:21 +02:00
|
|
|
logging.info("RIC %s is denied by config.ini", poc_id)
|
2017-01-09 09:31:42 +01:00
|
|
|
return False # RIC is denied - strongest way to block
|
2016-12-22 22:36:35 +01:00
|
|
|
# 3.) Check Range, return False if outside def. range
|
|
|
|
|
if globalVars.config.getint("POC", "filter_range_start") < int(poc_id) < globalVars.config.getint("POC", "filter_range_end"):
|
2016-12-03 14:49:22 +01:00
|
|
|
logging.info("RIC %s in between filter range", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
logging.info("RIC %s out of filter range", poc_id)
|
2017-01-08 12:21:44 +01:00
|
|
|
allowed = 0
|
2016-12-22 22:36:35 +01:00
|
|
|
# 4.) Implementation for net identifiers
|
|
|
|
|
if globalVars.config.get("POC", "netIdent_ric"):
|
|
|
|
|
if poc_id in globalVars.config.get("POC", "netIdent_ric"):
|
|
|
|
|
logging.info("RIC %s as net identifier", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
allowed = 0
|
2018-02-22 14:20:33 +01:00
|
|
|
# 5.) Implementation for multicastAlarm
|
|
|
|
|
if globalVars.config.get("multicastAlarm", "multicastAlarm_delimiter_ric"):
|
|
|
|
|
if poc_id in globalVars.config.get("multicastAlarm", "multicastAlarm_delimiter_ric"):
|
|
|
|
|
logging.info("RIC %s as multicastAlarm delimiter", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
allowed = 0
|
|
|
|
|
if globalVars.config.get("multicastAlarm", "multicastAlarm_ric"):
|
|
|
|
|
if poc_id in globalVars.config.get("multicastAlarm", "multicastAlarm_ric"):
|
|
|
|
|
logging.info("RIC %s as multicastAlarm message", poc_id)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
allowed = 0
|
2017-02-12 10:04:21 +01:00
|
|
|
|
2016-12-03 14:49:22 +01:00
|
|
|
if allowed == 0:
|
2015-06-28 22:28:21 +02:00
|
|
|
return False
|
|
|
|
|
return True
|
|
|
|
|
##
|
2015-07-02 09:02:49 +02:00
|
|
|
#
|
2015-06-28 22:28:21 +02:00
|
|
|
# POCSAG decoder function
|
|
|
|
|
# validate -> check double alarm -> log
|
|
|
|
|
#
|
|
|
|
|
def decode(freq, decoded):
|
|
|
|
|
"""
|
2017-02-19 01:17:30 +01:00
|
|
|
Export POCSAG information from Multimon-NG string and call alarmHandler.processAlarmHandler()
|
2015-06-28 22:28:21 +02:00
|
|
|
|
|
|
|
|
@type freq: string
|
|
|
|
|
@param freq: frequency of the SDR Stick
|
|
|
|
|
@type decoded: string
|
|
|
|
|
@param decoded: RAW Information from Multimon-NG
|
|
|
|
|
|
|
|
|
|
@requires: Configuration has to be set in the config.ini
|
2015-07-02 09:02:49 +02:00
|
|
|
|
2015-06-28 22:28:21 +02:00
|
|
|
@return: nothing
|
|
|
|
|
@exception: Exception if POCSAG decode failed
|
|
|
|
|
"""
|
2019-10-13 23:18:03 +02:00
|
|
|
has_geo = False
|
|
|
|
|
|
2015-07-04 14:37:35 +02:00
|
|
|
try:
|
|
|
|
|
bitrate = 0
|
|
|
|
|
|
|
|
|
|
if "POCSAG512:" in decoded:
|
|
|
|
|
bitrate = 512
|
|
|
|
|
poc_id = decoded[20:27].replace(" ", "").zfill(7)
|
|
|
|
|
poc_sub = str(int(decoded[39])+1)
|
|
|
|
|
|
|
|
|
|
elif "POCSAG1200:" in decoded:
|
|
|
|
|
bitrate = 1200
|
|
|
|
|
poc_id = decoded[21:28].replace(" ", "").zfill(7)
|
|
|
|
|
poc_sub = str(int(decoded[40])+1)
|
|
|
|
|
|
|
|
|
|
elif "POCSAG2400:" in decoded:
|
|
|
|
|
bitrate = 2400
|
|
|
|
|
poc_id = decoded[21:28].replace(" ", "").zfill(7)
|
|
|
|
|
poc_sub = str(int(decoded[40])+1)
|
|
|
|
|
|
|
|
|
|
if bitrate is 0:
|
|
|
|
|
logging.warning("POCSAG Bitrate not found")
|
|
|
|
|
logging.debug(" - (%s)", decoded)
|
2015-06-28 22:28:21 +02:00
|
|
|
else:
|
2015-07-04 14:37:35 +02:00
|
|
|
logging.debug("POCSAG Bitrate: %s", bitrate)
|
|
|
|
|
|
|
|
|
|
if "Alpha:" in decoded: #check if there is a text message
|
2019-09-21 23:50:46 +02:00
|
|
|
poc_text = decoded.split('Alpha: ')[1].strip().replace('<NUL><NUL>','').replace('<NUL>','').replace('<NUL','').replace('< NUL>','').replace('<EOT>','').strip()
|
2019-09-21 00:35:28 +02:00
|
|
|
if globalVars.config.getint("POC","geo_enable"):
|
|
|
|
|
try:
|
2019-09-21 23:50:46 +02:00
|
|
|
logging.debug("Using %s to find geo-tag in %s", globalVars.config.get("POC","geo_format"),poc_text)
|
2019-09-21 00:35:28 +02:00
|
|
|
m = re.search(globalVars.config.get("POC","geo_format"),poc_text)
|
|
|
|
|
if m:
|
|
|
|
|
logging.debug("Found geo-tag in message, parsing...")
|
|
|
|
|
has_geo = True
|
|
|
|
|
geo_order = globalVars.config.get("POC","geo_order").split(',')
|
|
|
|
|
if geo_order[0].lower == "lon":
|
|
|
|
|
lat = m.group(1) + "." + m.group(2)
|
|
|
|
|
lon = m.group(3) + "." + m.group(4)
|
|
|
|
|
else:
|
|
|
|
|
lon = m.group(1) + "." + m.group(2)
|
|
|
|
|
lat = m.group(3) + "." + m.group(4)
|
|
|
|
|
logging.debug("Finished parsing geo; lon: %s, lat: %s", lon, lat)
|
|
|
|
|
else:
|
|
|
|
|
logging.debug("No geo-tag found")
|
|
|
|
|
has_geo = False
|
|
|
|
|
except:
|
|
|
|
|
has_geo = False
|
|
|
|
|
logging.error("Exception parsing geo-information",exc_info=true)
|
|
|
|
|
else:
|
|
|
|
|
has_geo = False
|
|
|
|
|
else:
|
|
|
|
|
poc_text = ""
|
2015-07-04 14:37:35 +02:00
|
|
|
if re.search("[0-9]{7}", poc_id) and re.search("[1-4]{1}", poc_sub): #if POC is valid
|
|
|
|
|
if isAllowed(poc_id):
|
2017-10-01 18:06:56 +02:00
|
|
|
|
2015-07-04 14:37:35 +02:00
|
|
|
# check for double alarm
|
|
|
|
|
if doubleFilter.checkID("POC", poc_id+poc_sub, poc_text):
|
2019-09-05 08:26:24 +02:00
|
|
|
data = {"ric":poc_id, "function":poc_sub, "msg":poc_text, "bitrate":bitrate, "description":poc_id, "has_geo":has_geo}
|
|
|
|
|
if has_geo == True:
|
|
|
|
|
data["lon"] = lon
|
|
|
|
|
data["lat"] = lat
|
2015-07-04 14:37:35 +02:00
|
|
|
# Add function as character a-d to dataset
|
|
|
|
|
data["functionChar"] = data["function"].replace("1", "a").replace("2", "b").replace("3", "c").replace("4", "d")
|
2017-10-01 18:06:56 +02:00
|
|
|
|
|
|
|
|
logging.info("POCSAG%s: %s %s %s ", data["bitrate"], data["ric"], data["function"], data["msg"])
|
|
|
|
|
|
2015-07-04 14:37:35 +02:00
|
|
|
# If enabled, look up description
|
2016-10-03 12:02:18 +02:00
|
|
|
if globalVars.config.getint("POC", "idDescribed"):
|
2015-07-04 14:37:35 +02:00
|
|
|
from includes import descriptionList
|
2017-10-01 18:06:56 +02:00
|
|
|
data["description"] = descriptionList.getDescription("POC", data["ric"]+data["functionChar"])
|
|
|
|
|
|
2017-10-05 21:09:24 +02:00
|
|
|
# multicastAlarm processing if enabled and a message without text or delimiter RIC or netIdent_ric received
|
2017-10-23 20:34:25 +02:00
|
|
|
if globalVars.config.getint("multicastAlarm", "multicastAlarm") and data["ric"] != globalVars.config.get("POC", "netIdent_ric") and (data["msg"] == "" or data["ric"] in globalVars.config.get("multicastAlarm", "multicastAlarm_delimiter_ric")):
|
2017-10-03 12:03:08 +02:00
|
|
|
logging.debug(" - multicastAlarm without msg")
|
2017-09-23 09:25:43 +02:00
|
|
|
from includes import multicastAlarm
|
2017-10-03 12:08:04 +02:00
|
|
|
multicastAlarm.newEntrymultiList(data)
|
2017-10-03 11:39:57 +02:00
|
|
|
|
2017-09-23 09:25:43 +02:00
|
|
|
# multicastAlarm processing if enabled and alarm message has been received
|
2017-10-23 20:34:25 +02:00
|
|
|
elif globalVars.config.getint("multicastAlarm", "multicastAlarm") and data["msg"] != "" and data["ric"] in globalVars.config.get("multicastAlarm", "multicastAlarm_ric"):
|
2017-10-03 12:03:08 +02:00
|
|
|
logging.debug(" - multicastAlarm with message")
|
2017-09-23 09:25:43 +02:00
|
|
|
from includes import multicastAlarm
|
2017-10-03 12:10:08 +02:00
|
|
|
multicastAlarm.multicastAlarmExec(freq, data)
|
2017-10-01 18:06:56 +02:00
|
|
|
|
2017-09-19 20:56:59 +02:00
|
|
|
else:
|
|
|
|
|
# processing the alarm
|
|
|
|
|
try:
|
|
|
|
|
from includes import alarmHandler
|
|
|
|
|
alarmHandler.processAlarmHandler("POC", freq, data)
|
|
|
|
|
except:
|
|
|
|
|
logging.error("processing alarm failed")
|
|
|
|
|
logging.debug("processing alarm failed", exc_info=True)
|
2015-07-04 14:37:35 +02:00
|
|
|
# in every time save old data for double alarm
|
|
|
|
|
doubleFilter.newEntry(poc_id+poc_sub, poc_text)
|
|
|
|
|
else:
|
|
|
|
|
logging.debug("POCSAG%s: %s is not allowed", bitrate, poc_id)
|
|
|
|
|
else:
|
|
|
|
|
logging.warning("No valid POCSAG%s RIC: %s SUB: %s", bitrate, poc_id, poc_sub)
|
|
|
|
|
except:
|
|
|
|
|
logging.error("error while decoding")
|
|
|
|
|
logging.debug("error while decoding", exc_info=True)
|