AastraAsterisk: update AMI functions

Split the send_request function so we can send a request without waiting, add
Hangup and PlayDTMF events, allow specification of the event mask, really set
the timeout on the listen streams, add a timed wait_event() function which
waits for either an event or the timeout.

Signed-off-by: James Bottomley <JBottomley@Parallels.com>
diff --git a/include/AastraAsterisk.class.php b/include/AastraAsterisk.class.php
index 6e59a4e..ddb4866 100644
--- a/include/AastraAsterisk.class.php
+++ b/include/AastraAsterisk.class.php
@@ -69,19 +69,29 @@
 	      	$handler = '';
       		if(isset($this->event_handlers[$e])) $handler = $this->event_handlers[$e];
       		elseif(isset($this->event_handlers['*'])) $handler = $this->event_handlers['*'];
-      		if(function_exists($handler)) $ret = $handler($e, $parameters, $this->server, $this->port);
+      		if(is_callable($handler)) $ret = call_user_func($handler, $e, $parameters, $this->server, $this->port);
       		return $ret;
 	}
 
-    	function send_request($action, $parameters=array())
+    	function send_request_nowait($action, $parameters=array())
     	{
       		$req="Action: $action\r\n";
 	      	foreach($parameters as $var=>$val) $req.="$var: $val\r\n";
 	      	$req.="\r\n";
       		fwrite($this->socket, $req);
-      		return $this->wait_response();
     	}
 
+	function send_request($action, $parameters=array())
+	{
+		$this->send_request_nowait($action, $parameters);
+      		return $this->wait_response();
+	}
+
+	function events($mask='off')
+	{
+		return $this->send_request('Events', array('EventMask' => $mask));
+	}
+
 	function get_socket()
 	{
 		if($this->socket) return(True);
@@ -154,7 +164,7 @@
       		return $parameters;
     	}
 
-	function listen($timeout='60')
+	function wait_event($timeout='60')
     	{
      		$exit=False;
 		$time1=time();
@@ -162,6 +172,7 @@
       			{
         		$type='';
         		$parameters=array();
+			stream_set_timeout($this->socket, $timeout);
         		$temp=fgets($this->socket, 4096);
 			if($temp===False) 
 				{
@@ -206,6 +217,7 @@
         			{
           			case 'event':
             				$this->process_event($parameters);
+					$exit=True;
             				break;
           			default:
             				if($type!='') $this->log('Unhandled response packet from Manager: '.$type);
@@ -219,6 +231,14 @@
       		return $parameters;
     	}
 
+	function listen($timeout=60)
+	{
+		$time1=time();
+		do {
+			$this->wait_event($timeout);
+		} while (time() < $time1+$timeout);
+	}
+
 
     	function connect($server=NULL,$username=NULL,$secret=NULL,$display=True)
     	{
@@ -323,6 +343,19 @@
 		return $retval;
 	}
 
+	function hangup($channel) {
+		$parameters=array('Channel'=>$channel);
+		return $this->send_request('Hangup', $parameters);
+	}
+
+	function playDTMF($channel, $digit) {
+		$parameters=array('Channel'=>$channel,'Digit'=>$digit);
+		$this->log_array('PLAY DTMF', $parameters);
+		$err = $this->send_request('PlayDTMF', $parameters);
+		$this->log_array('PLAY DTMF RETURNS', $err);
+		return $err;
+	}
+
 	function get_user() {
 		$ret = $this->Command('sip show peers');
 		$ret = split("\n", $ret['data']);
@@ -391,6 +424,13 @@
 #		Aastra_debug($message);
     	}
 
+	function log_array($msg, $a) {
+		 foreach ($a as $k=>$v) {
+		 	 $msg.= ' '.$k.'=>'.$v;
+		 }
+		 error_log($msg);
+	}
+
 	function database_show($family='') 
 	{
 		$r=$this->command('database show '.$family);