link to my blog post
[auerswald-callnotifier.git] / src / callnotifier / EDSS1 / Parser.php
index 64a072e91cb6fab891f9094a7265a4e61ad65a94..b5a906d07408566970fb745d5bc3a60e94d5396a 100644 (file)
@@ -10,11 +10,26 @@ class EDSS1_Parser
     public function parse($bytes)
     {
         $m = new EDSS1_Message();
-        $m->tei = ord($bytes{1}) >> 1;//1st bit is always 1 and needs to be removed
+        $m->sapi = ord($bytes{0}) >> 2;
+        $m->callResponse = (int) ((ord($bytes{0}) & 2) == 2);
+        $m->tei  = ord($bytes{1}) >> 1;
 
         $curpos = 4;
-        list($curpos, $m->callRef) = $this->readLengthDataInt($bytes, ++$curpos);
-        //var_dump($curpos, dechex($m->callRef));
+        list($curpos, $cCallRef, $crLen) = $this->readLengthData($bytes, ++$curpos);
+        if ($crLen == 0xFF) {
+            return $m;
+        }
+        if ($crLen > 0) {
+            $m->callRefType = ord($cCallRef{0}) >> 7;
+            $nCallRef = ord($cCallRef{0}) & 127;
+            if ($crLen > 1) {
+                $nCallRef = ord($cCallRef{1}) + ($nCallRef << 8);
+                if ($crLen > 2) {
+                    $nCallRef = ord($cCallRef{2}) + ($nCallRef << 8);
+                }
+            }
+            $m->callRef = $nCallRef;
+        }
         $m->type = ord($bytes{++$curpos});
 
         $complete = false;
@@ -25,16 +40,17 @@ class EDSS1_Parser
                 $complete = true;
                 break;
             }
-            $param = new EDSS1_Parameter();
+
+            $paramType = ord($curbit);
+            $param = $this->getParameterByType($paramType);
             $m->parameters[] = $param;
-            $param->type     = ord($curbit);
 
             //parameter length
             $curbit = $bytes{++$curpos};
             $param->length = ord($curbit);
 
             //parameter data
-            $param->data = substr($bytes, $curpos + 1, $param->length);
+            $param->setData(substr($bytes, $curpos + 1, $param->length));
             $curpos += $param->length;
         } while ($curpos < strlen($bytes) - 1);
 
@@ -50,20 +66,28 @@ class EDSS1_Parser
     {
         //var_dump('old' . $curpos);
         $length = ord($bytes{$curpos});
-        $data = substr($bytes, $curpos + 1, $length);
+        if ($length != 0xFF) {
+            $data = substr($bytes, $curpos + 1, $length);
+        } else {
+            $data = null;
+        }
         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
+     * @param integer $type Parameter type ID
      */
-    public function readLengthDataInt($bytes, $curpos)
+    public function getParameterByType($type)
     {
-        $ld = $this->readLengthData($bytes, $curpos);
-        $ld[1] = ord($ld[1]);
-        return $ld;
+        $supported = array(0x28, 0x2C, 0x4C, 0x6C, 0x70);
+        if (!in_array($type, $supported)) {
+            return new EDSS1_Parameter($type);
+        }
+
+        $typeHex = sprintf('%02X', $type);
+        $class = 'callnotifier\EDSS1_Parameter_' . $typeHex;
+
+        return new $class($type);
     }
 }