2 namespace callnotifier;
9 * Array of logger object arrays.
10 * Key is the notification type, value is an array of logger objects
11 * that want to get notified about the type.
15 protected $logger = array(
17 'incomingCall' => array(),
18 'edss1msg' => array(),
22 public function __construct($config)
24 $this->config = $config;
31 * @param Logger $logger Logger object to register
32 * @param array|string $types Single notification type or array of such
33 * types. "*" means "register for all types".
37 public function addLogger(Logger $logger, $types)
40 $types = array_keys($this->logger);
42 $types = (array)$types;
44 foreach ($types as $type) {
45 if (!isset($this->logger[$type])) {
46 throw new \Exception('Unknown log type: ' . $type);
48 $this->logger[$type][] = $logger;
52 public function handle($msg)
54 if ($this->config->dumpFile !== null) {
57 if (substr($msg, 0, 9) != '[DKANPROT') {
58 //unknown message type
61 $regex = '#^\\[DKANPROT-([^ ]+) ([0-9]+)\\] (.*)$#';
62 if (!preg_match($regex, $msg, $matches)) {
63 //message should always be that way
66 list(, $type, $someid, $details) = $matches;
76 if ($type != 'Info') {
77 $this->parseEDSS1($details);
80 //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41],
81 $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
82 if (preg_match($regex, $details, $matches)) {
83 list(, $from, $to) = $matches;
84 $this->log('incomingCall', array('from' => $from, 'to' => $to));
89 * Example string: "T02: 00 A3 06 0A 08 01 01 5A FF 0A"
91 * @param string $details Detail string of a debug message
94 protected function parseEDSS1($details)
96 if ($details{0} != 'T' && $details{0} != 'N') {
97 //we only want byte data
100 if (substr($details, 16, 4) != ' 08 ') {
101 //only E-DSS-1, no other packets
105 $bytestring = substr($details, 5);
106 $bytes = static::getBytesFromHexString($bytestring);
108 $msgtype = $bytes{7};
109 static $interestingTyps = array(
110 EDSS1_Message::SETUP,
111 EDSS1_Message::CONNECT,
112 EDSS1_Message::INFORMATION
114 if (!in_array($msgtype, $interestingTyps)) {
118 $mp = new EDSS1_Parser();
119 $msg = $mp->parse($bytes);
121 $this->log('edss1msg', array('msg' => $msg));
124 protected function log($type, $arData)
126 if (!isset($this->logger[$type])) {
127 throw new \Exception('Unknown log type: ' . $type);
130 if (count($this->logger[$type])) {
131 foreach ($this->logger[$type] as $logger) {
132 $logger->log($type, $arData);
137 public static function getBytesFromHexString($bytestring)
140 foreach (explode(' ', $bytestring) as $strbyte) {
141 $bytes .= chr(hexdec($strbyte));
146 protected function prepareDump()
148 if ($this->config->dumpFile === null) {
151 $this->dumpHdl = fopen($this->config->dumpFile, 'w');
152 if (!$this->dumpHdl) {
153 throw new Exception('Cannot open replay file for reading');
157 protected function dump($msg)
159 fwrite($this->dumpHdl, $msg . "\n");