605b773c3dc7fbf2d074699eccf6b6db15d5428c
[auerswald-callnotifier.git] / callnotifier.php
1 #!/usr/bin/env php
2 <?php
3 $ip = '192.168.3.95';
4 $port = 42225;
5
6 $socket = awcn_connect($ip, $port);
7 awcn_init($socket);
8 awcn_loop($socket);
9 awcn_disconnect($socket);
10
11
12 function awcn_connect($ip, $port) {
13     $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
14     if ($socket === false) {
15         echo "socket_create() failed: reason: "
16             . socket_strerror(socket_last_error()) . "\n";
17     } else {
18         echo "OK.\n";
19     }
20     echo "Attempting to connect to '$ip' on port '$port'...";
21     $result = socket_connect($socket, $ip, $port);
22     if ($result === false) {
23         echo "socket_connect() failed.\nReason: ($result) "
24             . socket_strerror(socket_last_error($socket)) . "\n";
25     } else {
26         echo "OK.\n";
27     }
28     return $socket;
29 }
30
31 function awcn_init($socket)
32 {
33     $msg = "\x00\x01DecoderV=1\n";
34     socket_write($socket, $msg, strlen($msg));
35     $res = awcn_read_response($socket);
36     socket_write($socket, "\x00\x02", 2);
37 }
38
39 function awcn_loop($socket)
40 {
41     while (true) {
42         $dbgmsg = awcn_read_response($socket);
43         //echo $dbgmsg . "\n";
44         awcn_handle_msg($dbgmsg);
45     }
46 }
47 function awcn_read_response($socket)
48 {
49     $res = socket_read($socket, 2048, PHP_NORMAL_READ);
50     return substr($res, 2, -1);
51 }
52
53 function awcn_disconnect($socket)
54 {
55     socket_write($socket, "\x00\x03", 2);
56     socket_close($socket);
57 }
58
59 function awcn_handle_msg($msg)
60 {
61     if (substr($msg, 0, 9) != '[DKANPROT') {
62         //unknown message type
63         return;
64     }
65     $regex = '#^\\[DKANPROT-([^ ]+) ([0-9]+)\\] (.*)$#';
66     if (!preg_match($regex, $msg, $matches)) {
67         //message should always be that way
68         return false;
69     }
70     list(, $type, $someid, $details) = $matches;
71
72     if ($type != 'Info') {
73         //we only want info messages
74         var_dump($type . ': ' . $details);
75         return;
76     }
77     //Vegw/Ets-Cref:[0xffef]/[0x64] - VEGW_SETUP from upper layer to internal destination: CGPN[**22]->CDPN[41], 
78     var_dump($details);
79     $regex = '#CGPN\\[([^\\]]+)\\]->CDPN\\[([^\\]]+)\\]#';
80     if (preg_match($regex, $details, $matches)) {
81         var_dump('a call!', $matches);
82     }
83 }
84
85 ?>