X-Git-Url: https://git.cweiske.de/auerswald-callnotifier.git/blobdiff_plain/fc647788cf137595e84bea72edb16d86ab1d76a5..4bc203883b0b35be4892768679340effd1348a2f:/src/callnotifier/MessageHandler.php diff --git a/src/callnotifier/MessageHandler.php b/src/callnotifier/MessageHandler.php index 89d07e7..90cd87c 100644 --- a/src/callnotifier/MessageHandler.php +++ b/src/callnotifier/MessageHandler.php @@ -5,47 +5,13 @@ class MessageHandler { protected $dumpHdl; - /** - * Array of logger object arrays. - * Key is the notification type, value is an array of logger objects - * that want to get notified about the type. - * - * @var array - */ - protected $logger = array( - 'msgData' => array(), - 'incomingCall' => array() - ); - - public function __construct($config) + public function __construct($config, $log, $callMonitor) { $this->config = $config; $this->prepareDump(); - } - - /** - * Add a logger - * - * @param Logger $logger Logger object to register - * @param array|string $types Single notification type or array of such - * types. "*" means "register for all types". - * - * @return self - */ - public function addLogger(Logger $logger, $types) - { - if ($types == '*') { - $types = array_keys($this->logger); - } - $types = (array)$types; - - foreach ($types as $type) { - if (!isset($this->logger[$type])) { - throw new \Exception('Unknown log type: ' . $type); - } - $this->logger[$type][] = $logger; - } + $this->log = $log; + $this->callMonitor = $callMonitor; } public function handle($msg) @@ -63,7 +29,7 @@ class MessageHandler return false; } list(, $type, $someid, $details) = $matches; - $this->log( + $this->log->log( 'msgData', array( 'type' => $type, @@ -72,29 +38,47 @@ class MessageHandler ) ); - if ($type != 'Info') { - //we only want info messages - return; - } - //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], - $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#'; - if (preg_match($regex, $details, $matches)) { - list(, $from, $to) = $matches; - $this->log('incomingCall', array('from' => $from, 'to' => $to)); + if ($type == 'Debug') { + $msg = $this->parseEDSS1($details); + if (is_object($msg)) { + $this->log->log('edss1msg', array('msg' => $msg)); + $this->callMonitor->handle($msg); + } } } - protected function log($type, $arData) + /** + * Example string: "T02: 00 A3 06 0A 08 01 01 5A FF 0A" + * + * @param string $details Detail string of a debug message + * + * @return EDSS1_Message The retrieved message, NULL if none. + */ + protected function parseEDSS1($details) { - if (!isset($this->logger[$type])) { - throw new \Exception('Unknown log type: ' . $type); + if ($details{0} != 'T' && $details{0} != 'N') { + //we only want byte data + return; + } + if (substr($details, 16, 4) != ' 08 ') { + //only E-DSS-1, no other packets + return; } - if (count($this->logger[$type])) { - foreach ($this->logger[$type] as $logger) { - $logger->log($type, $arData); - } + $bytestring = substr($details, 5); + $bytes = static::getBytesFromHexString($bytestring); + + $mp = new EDSS1_Parser(); + return $mp->parse($bytes); + } + + public static function getBytesFromHexString($bytestring) + { + $bytes = ''; + foreach (explode(' ', $bytestring) as $strbyte) { + $bytes .= chr(hexdec($strbyte)); } + return $bytes; } protected function prepareDump() @@ -104,7 +88,7 @@ class MessageHandler } $this->dumpHdl = fopen($this->config->dumpFile, 'w'); if (!$this->dumpHdl) { - throw new Exception('Cannot open replay file for reading'); + throw new \Exception('Cannot open dump file for writing'); } }