aboutsummaryrefslogtreecommitdiff
path: root/src/callnotifier/Logger/Debug.php
blob: ef5d812c1728dd818b3eb37e1c1a38240f2f8814 (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
95
96
97
98
99
100
101
102
103
104
<?php
namespace callnotifier;

class Logger_Debug implements Logger
{
    public $edss1MsgOnly = false;

    public function __construct()
    {
        $cc = new \Console_Color2();
        $this->begin  = $cc->convert('%y');
        $this->end    = $cc->convert('%n');
        $this->blue   = $cc->convert('%b');
        $this->red    = $cc->convert('%r');
        $this->white  = $cc->convert('%w');
        $this->purple = $cc->convert('%p');
    }

    public function log($type, $arData)
    {
        if ($type == 'msgData') {
            $this->echoMsgData($arData);
        } else if ($type == 'edss1msg') {
            $this->echoEDSS1($arData['msg']);
        } else {
            //other data
            echo $this->blue . $type . $this->end . ': '
                . var_export($arData, true) . "\n";
        }
    }


    protected function echoMsgData($arData)
    {
        if ($this->edss1MsgOnly
            && substr($arData['details'], 16, 4) != ' 08 '
        ) {
            //only E-DSS-1, no other packets
            return;
        }

        echo $this->begin . $arData['type'] . $this->end
            . ': ' . $arData['details'] . "\n";

        //Show bytes of N01|N02|T01|T02 etc
        if (preg_match('#^[A-Z][0-9]{2}: (.+)$#', $arData['details'], $matches)) {
            $bytestring = $matches[1];
            $line = '';
            foreach (explode(' ', $bytestring) as $strbyte) {
                $line .= chr(hexdec($strbyte));
            }
            $line = preg_replace(
                '/[^[:print:]]/',
                $this->white . '?' . $this->end,
                $line
            );
            echo $this->red . '     bytes' . $this->end . ': ' . $line . "\n";
        }
    }

    protected function echoEDSS1($msg)
    {
        echo sprintf(
            $this->purple . 'EDSS1_Message' . $this->end
            . ' type 0x%02X '
            . $this->blue . '%s' . $this->end
            . ' SAPI %d, CR %d, TEI %d, call %d-%s'
            . ', %d parameters',
            $msg->type,
            $msg->getTypeName(),
            $msg->sapi,
            $msg->callResponse,
            $msg->tei,
            $msg->callRef,
            $msg->callRefType == 0 ? 'source' : 'target',
            count($msg->parameters)
        ) . "\n";

        foreach ($msg->parameters as $param) {
            echo sprintf(
                " Parameter type 0x%02X%s, %d bytes: %s\n",
                $param->type,
                $param->title
                ? ' ' . $this->purple . $param->title . $this->end
                : ' ' . $this->purple . $param->getTypeName() . $this->end,
                $param->length,
                preg_replace('/[^[:print:]]/', '?', $param->data)
                . (isset($param->number)
                   ? ' ' . $this->red . $param->number . $this->end
                   : ''
                )
            );
            if ($param instanceof EDSS1_Parameter_INumber) {
                echo sprintf(
                    "    Number type: %s, plan: %s\n",
                    EDSS1_Parameter_Names::$numberTypes[$param->numberType],
                    EDSS1_Parameter_Names::$numberingPlans[$param->numberingPlan]
                );
            }
        }
    }
}

?>