dummy callref support
[auerswald-callnotifier.git] / src / callnotifier / EDSS1 / Parser.php
1 <?php
2 namespace callnotifier;
3
4 class EDSS1_Parser
5 {
6     const PARAM = 0;
7     const PARAMLENGTH = 1;
8     const PARAMVAL = 2;
9
10     public function parse($bytes)
11     {
12         $m = new EDSS1_Message();
13         $m->sapi = ord($bytes{0}) >> 2;
14         $m->callResponse = (int) ((ord($bytes{0}) & 2) == 2);
15         $m->tei  = ord($bytes{1}) >> 1;
16
17         $curpos = 4;
18         list($curpos, $cCallRef, $crLen) = $this->readLengthData($bytes, ++$curpos);
19         if ($crLen == 0xFF) {
20             return $m;
21         }
22         if ($crLen > 0) {
23             $m->callRefType = ord($cCallRef{0}) >> 7;
24             $nCallRef = ord($cCallRef{0}) & 127;
25             if ($crLen > 1) {
26                 $nCallRef = ord($cCallRef{1}) + ($nCallRef << 8);
27                 if ($crLen > 2) {
28                     $nCallRef = ord($cCallRef{2}) + ($nCallRef << 8);
29                 }
30             }
31             $m->callRef = $nCallRef;
32         }
33         $m->type = ord($bytes{++$curpos});
34
35         $complete = false;
36         do {
37             //parameter type
38             $curbit = $bytes{++$curpos};
39             if ($curbit == "\xFF" && $bytes{$curpos + 1} == "\n") {
40                 $complete = true;
41                 break;
42             }
43
44             $paramType = ord($curbit);
45             $param = $this->getParameterByType($paramType);
46             $m->parameters[] = $param;
47
48             //parameter length
49             $curbit = $bytes{++$curpos};
50             $param->length = ord($curbit);
51
52             //parameter data
53             $param->setData(substr($bytes, $curpos + 1, $param->length));
54             $curpos += $param->length;
55         } while ($curpos < strlen($bytes) - 1);
56
57         return $m;
58     }
59
60     /**
61      * Read a datablock preceded with a length byte.
62      *
63      * @return array Array with new cursor position, data and data length
64      */
65     public function readLengthData($bytes, $curpos)
66     {
67         //var_dump('old' . $curpos);
68         $length = ord($bytes{$curpos});
69         if ($length != 0xFF) {
70             $data = substr($bytes, $curpos + 1, $length);
71         } else {
72             $data = null;
73         }
74         return array($curpos + $length, $data, $length);
75     }
76
77     /**
78      * @param integer $type Parameter type ID
79      */
80     public function getParameterByType($type)
81     {
82         $supported = array(0x28, 0x2C, 0x4C, 0x6C, 0x70);
83         if (!in_array($type, $supported)) {
84             return new EDSS1_Parameter($type);
85         }
86
87         $typeHex = sprintf('%02X', $type);
88         $class = 'callnotifier\EDSS1_Parameter_' . $typeHex;
89
90         return new $class($type);
91     }
92 }
93
94 ?>