aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/EDSS1/Parser.php
blob: b5a906d07408566970fb745d5bc3a60e94d5396a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace callnotifier;

class EDSS1_Parser
{
    const PARAM = 0;
    const PARAMLENGTH = 1;
    const PARAMVAL = 2;

    public function parse($bytes)
    {
        $m = new EDSS1_Message();
        $m->sapi = ord($bytes{0}) >> 2;
        $m->callResponse = (int) ((ord($bytes{0}) & 2) == 2);
        $m->tei  = ord($bytes{1}) >> 1;

        $curpos = 4;
        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;
        do {
            //parameter type
            $curbit = $bytes{++$curpos};
            if ($curbit == "\xFF" && $bytes{$curpos + 1} == "\n") {
                $complete = true;
                break;
            }

            $paramType = ord($curbit);
            $param = $this->getParameterByType($paramType);
            $m->parameters[] = $param;

            //parameter length
            $curbit = $bytes{++$curpos};
            $param->length = ord($curbit);

            //parameter data
            $param->setData(substr($bytes, $curpos + 1, $param->length));
            $curpos += $param->length;
        } while ($curpos < strlen($bytes) - 1);

        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});
        if ($length != 0xFF) {
            $data = substr($bytes, $curpos + 1, $length);
        } else {
            $data = null;
        }
        return array($curpos + $length, $data, $length);
    }

    /**
     * @param integer $type Parameter type ID
     */
    public function getParameterByType($type)
    {
        $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);
    }
}

?>