read TEI and call reference
authorChristian Weiske <cweiske@cweiske.de>
Fri, 20 Jul 2012 05:45:53 +0000 (07:45 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 20 Jul 2012 05:45:53 +0000 (07:45 +0200)
src/callnotifier/EDSS1/Message.php
src/callnotifier/EDSS1/Parser.php
src/callnotifier/Logger/Echo.php
src/callnotifier/MessageHandler.php
tests/bootstrap.php [new file with mode: 0644]
tests/callnotifier/EDSS1/ParserTest.php [new file with mode: 0644]
tests/phpunit.xml [new file with mode: 0644]

index 3d22a8206594b0ed14c5e1cf3e920791cfcf1d25..08fa44f94f9bef1f24053cbc639f6b24948b8b38 100644 (file)
@@ -12,11 +12,32 @@ class EDSS1_Message
     const RELEASE = "\x4D";
     const RELEASE_COMPLETE = "\x5A";
     const INFORMATION = "\x7B";
     const RELEASE = "\x4D";
     const RELEASE_COMPLETE = "\x5A";
     const INFORMATION = "\x7B";
-    
+
     /**
     /**
+     * Message type, see the class constants
      * @var integer
      */
     public $type;
      * @var integer
      */
     public $type;
+
+    /**
+     * Call reference number to distinguish concurrent calls
+     *
+     * @var integer
+     */
+    public $callRef;
+
+    /**
+     * Terminal Endpoint Identifier (internal Telephone ID)
+     *
+     * @var integer
+     */
+    public $tei;
+
+    /**
+     * Array of EDSS1_Parameter objects
+     *
+     * @var array
+     */
     public $parameters = array();
 
 
     public $parameters = array();
 
 
index 5a298e5011b51d4a2b67d816782cb1b25f4c7ad5..64a072e91cb6fab891f9094a7265a4e61ad65a94 100644 (file)
@@ -10,9 +10,13 @@ class EDSS1_Parser
     public function parse($bytes)
     {
         $m = new EDSS1_Message();
     public function parse($bytes)
     {
         $m = new EDSS1_Message();
-        $m->type = ord($bytes{7});
+        $m->tei = ord($bytes{1}) >> 1;//1st bit is always 1 and needs to be removed
+
+        $curpos = 4;
+        list($curpos, $m->callRef) = $this->readLengthDataInt($bytes, ++$curpos);
+        //var_dump($curpos, dechex($m->callRef));
+        $m->type = ord($bytes{++$curpos});
 
 
-        $curpos = 7;
         $complete = false;
         do {
             //parameter type
         $complete = false;
         do {
             //parameter type
@@ -36,6 +40,31 @@ class EDSS1_Parser
 
         return $m;
     }
 
         return $m;
     }
+
+    /**
+     * Read a datablock preceded with a length byte.
+     *
+     * @return array Array with new cursor position, data and data length
+     */
+    public function readLengthData($bytes, $curpos)
+    {
+        //var_dump('old' . $curpos);
+        $length = ord($bytes{$curpos});
+        $data = substr($bytes, $curpos + 1, $length);
+        return array($curpos + $length, $data, $length);
+    }
+
+    /**
+     * Read a datablock preceded with a length byte, return integer data.
+     *
+     * @return array Array with new cursor position, integer data and data length
+     */
+    public function readLengthDataInt($bytes, $curpos)
+    {
+        $ld = $this->readLengthData($bytes, $curpos);
+        $ld[1] = ord($ld[1]);
+        return $ld;
+    }
 }
 
 ?>
 }
 
 ?>
index 9ce5b090d653672547accd0228ce9dd40079710a..f0a1a41dba2280bba70ddeb82171548799041567 100644 (file)
@@ -38,9 +38,12 @@ class Logger_Echo implements Logger
                 $this->purple . 'EDSS1_Message' . $this->end
                 . ' type %02X '
                 . $this->purple . '%s' . $this->end
                 $this->purple . 'EDSS1_Message' . $this->end
                 . ' type %02X '
                 . $this->purple . '%s' . $this->end
+                . ' TEI %d, call %d'
                 . ', %d parameters',
                 $msg->type,
                 $msg->getTypeName(),
                 . ', %d parameters',
                 $msg->type,
                 $msg->getTypeName(),
+                $msg->tei,
+                $msg->callRef,
                 count($msg->parameters)
             ) . "\n";
             foreach ($msg->parameters as $param) {
                 count($msg->parameters)
             ) . "\n";
             foreach ($msg->parameters as $param) {
index 6181e3a39a5e15545c17d49415278e7ce852e877..9e65b8138db75d50b9c02c9444ad1d871b2384c8 100644 (file)
@@ -103,10 +103,7 @@ class MessageHandler
         }
         
         $bytestring = substr($details, 5);
         }
         
         $bytestring = substr($details, 5);
-        $bytes = '';
-        foreach (explode(' ', $bytestring) as $strbyte) {
-            $bytes .= chr(hexdec($strbyte));
-        }
+        $bytes = static::getBytesFromHexString($bytestring);
 
         $msgtype = $bytes{7};
         static $interestingTyps = array(
 
         $msgtype = $bytes{7};
         static $interestingTyps = array(
@@ -137,6 +134,15 @@ class MessageHandler
         }
     }
 
         }
     }
 
+    public static function getBytesFromHexString($bytestring)
+    {
+        $bytes = '';
+        foreach (explode(' ', $bytestring) as $strbyte) {
+            $bytes .= chr(hexdec($strbyte));
+        }
+        return $bytes;
+    }
+
     protected function prepareDump()
     {
         if ($this->config->dumpFile === null) {
     protected function prepareDump()
     {
         if ($this->config->dumpFile === null) {
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644 (file)
index 0000000..a233690
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+set_include_path(
+    __DIR__ . '/../src/'
+    . PATH_SEPARATOR . get_include_path()
+);
+spl_autoload_register(
+    function ($class) {
+        $file = str_replace(array('\\', '_'), '/', $class) . '.php';
+        $hdl = @fopen($file, 'r', true);
+        if ($hdl !== false) {
+            fclose($hdl);
+            require $file;
+        }
+    }
+);
+
+?>
diff --git a/tests/callnotifier/EDSS1/ParserTest.php b/tests/callnotifier/EDSS1/ParserTest.php
new file mode 100644 (file)
index 0000000..117a8d0
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+namespace callnotifier;
+
+class EDSS1_ParserTest extends \PHPUnit_Framework_TestCase
+{
+
+    public function testParse()
+    {
+        $bs = '00 A3 02 02 08 01 01 7B 70 0C 81 30 31 36 33 34 37 37 39 38 37 38 FF 0A';
+        $p = new EDSS1_Parser();
+        $msg = $p->parse(MessageHandler::getBytesFromHexString($bs));
+
+        self::assertInstanceOf('callnotifier\EDSS1_Message', $msg);
+        self::assertEquals(81, $msg->tei, 'TEI is wrong');
+        self::assertEquals(123, $msg->type, 'Message type is wrong');
+        self::assertEquals(1, $msg->callRef, 'Call reference is wrong');
+        self::assertEquals(1, count($msg->parameters), 'Wrong parameter count');
+        $p = $msg->parameters[0];
+        self::assertInstanceOf('callnotifier\EDSS1_Parameter', $p);
+        self::assertEquals(112, $p->type, 'Type of 1st parameter is wrong');
+        self::assertEquals("\x8101634779878", $p->data, 'Parameter data is wrong');
+    }
+
+}
+
+?>
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
new file mode 100644 (file)
index 0000000..8bdd7cd
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<phpunit strict="true" colors="true"
+         bootstrap="bootstrap.php"
+>
+ <filter>
+  <whitelist addUncoveredFilesFromWhitelist="false">
+   <directory suffix=".php">../src/</directory>
+  </whitelist>
+ </filter>
+</phpunit>