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);
107 foreach (explode(' ', $bytestring) as $strbyte) {
108 $bytes .= chr(hexdec($strbyte));
111 $msgtype = $bytes{7};
112 static $interestingTyps = array(
113 EDSS1_Message::SETUP,
114 EDSS1_Message::CONNECT,
115 EDSS1_Message::INFORMATION
117 if (!in_array($msgtype, $interestingTyps)) {
121 $mp = new EDSS1_Parser();
122 $msg = $mp->parse($bytes);
124 $this->log('edss1msg', array('msg' => $msg));
127 protected function log($type, $arData)
129 if (!isset($this->logger[$type])) {
130 throw new \Exception('Unknown log type: ' . $type);
133 if (count($this->logger[$type])) {
134 foreach ($this->logger[$type] as $logger) {
135 $logger->log($type, $arData);
140 protected function prepareDump()
142 if ($this->config->dumpFile === null) {
145 $this->dumpHdl = fopen($this->config->dumpFile, 'w');
146 if (!$this->dumpHdl) {
147 throw new Exception('Cannot open replay file for reading');
151 protected function dump($msg)
153 fwrite($this->dumpHdl, $msg . "\n");