From d1d2976e6aa0c09aa5b3b42ef01153ef6513681a Mon Sep 17 00:00:00 2001
From: klonfish <klonfish@git.blinkenbunt.org>
Date: Thu, 28 May 2015 23:44:35 +0200
Subject: [PATCH] Added config classes for phone interface

---
 phoneinterface.py | 61 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 55 insertions(+), 6 deletions(-)

diff --git a/phoneinterface.py b/phoneinterface.py
index 6ec420e..8dffcb2 100644
--- a/phoneinterface.py
+++ b/phoneinterface.py
@@ -3,6 +3,24 @@ import time
 import threading
 import subprocess
 
+class PhoneProxyConfiguration(object):
+    def __init__(self):
+        self.name = 'eventphone'
+        self.proxy = 'sip:hg.eventphone.de'
+        self.identity = 'sip:0815@hg.eventphone.de'
+        self.username = '0815'
+        self.password = 'secret'
+        self.realm = 'hg.eventphone.de'
+
+class PhoneConfiguration(object):
+    def __init__(self):
+        self.sound_device = 'default:CARD=Device'
+        self.incoming_timeout = 3*60
+        self.linphone_config = 'linphone.conf'
+        self.default_proxy = 'eventphone'
+        self.proxies = []
+        self.stun_server = 'sipgate.de'
+
 class PhoneEvent(object):
     RegInProgress,\
     RegSuccessfull,\
@@ -21,7 +39,7 @@ class PhoneEvent(object):
                 return k
 
 class PhoneInterface(object):
-    def __init__(self, config, roconfig):
+    def __init__(self, config):
         cbs = {
             'global_state_changed': self.__global_state_changed,
             'registration_state_changed': self.__registration_state_changed,
@@ -30,14 +48,38 @@ class PhoneInterface(object):
 
         self.__event_cbs = []
 
-        self.__core = linphone.Core.new(cbs, config, roconfig)
+        self.__config = config
+        self.__core = linphone.Core.new(cbs, None, config.linphone_config)
+
+        # Create and add all proxy configs
+        for p in config.proxies:
+            ainfo = self.__core.create_auth_info(p.username, p.username, p.password, None, p.realm, None)
+            self.__core.add_auth_info(ainfo)
+
+            pconf = self.__core.create_proxy_config()
+            pconf.edit()
+            pconf.identity = p.identity
+            pconf.publish_enabled = False
+            pconf.realm = p.realm
+            pconf.register_enabled = True
+            pconf.register_enabled = True
+            pconf.server_addr = p.proxy
+            self.__core.add_proxy_config(pconf)
+            pconf.done()
+
+            if p.name == config.default_proxy:
+                self.__core.default_proxy_config = pconf
 
         self.__audioproc = None
+        aplay = subprocess.Popen(['aplay', '-qD%s' % config.sound_device], stdin=subprocess.PIPE)
+        self.__ttsproc = subprocess.Popen(['espeak', '-p10', '--stdout'], stdin=subprocess.PIPE, stdout=aplay.stdin)
 
         # Set default parameters overriding the ones from the given config file
         self.__core.set_user_agent('FeTAp 615', '0.1')
+        self.__core.stun_server = config.stun_server
         self.__core.ringback = ''
         self.__core.max_calls = 1
+        self.__core.inc_timeout = config.incoming_timeout
         self.__core.set_call_error_tone(linphone.Reason.Busy, '')
         self.__core.disable_chat(linphone.Reason.None)
         self.__core.echo_cancellation_enabled = False
@@ -90,7 +132,7 @@ class PhoneInterface(object):
                 cb(evt)
         else:
             print 'Unhandled call state:', linphone.CallState.string(state)
-        
+
     def __pollthread(self):
         while self.__running:
             self.__core.iterate()
@@ -102,6 +144,9 @@ class PhoneInterface(object):
         t.start()
 
     def stop(self):
+        self.stop_playing()
+        if self.__ttsproc is not None:
+            self.__ttsproc.terminate()
         self.__running = False
 
     def add_event_cb(self, cb):
@@ -123,24 +168,28 @@ class PhoneInterface(object):
         self.stop_playing()
         self.__audioproc = subprocess.Popen(['play', '-nq', 'synth', 'sine', '425'],
                                             env = {'AUDIODRIVER': 'alsa',
-                                                   'AUDIODEV': 'default:CARD=Device'})
+                                                   'AUDIODEV': self.__config.sound_device})
 
     def play_ringback_tone(self):
         self.stop_playing()
         self.__audioproc = subprocess.Popen(['play', '-nq', 'synth', '1', 'sine', '425', 'pad', '4@1', 'repeat', '1000'],
                                             env = {'AUDIODRIVER': 'alsa',
-                                                   'AUDIODEV': 'default:CARD=Device'})
+                                                   'AUDIODEV': self.__config.sound_device})
 
     def play_busy_tone(self):
         self.stop_playing()
         self.__audioproc = subprocess.Popen(['play', '-nq', 'synth', '0.48', 'sine', '425', 'pad', '0.48@0.48', 'repeat', '1000'],
                                             env = {'AUDIODRIVER': 'alsa',
-                                                   'AUDIODEV': 'default:CARD=Device'})
+                                                   'AUDIODEV': self.__config.sound_device})
 
     def stop_playing(self):
         if self.__audioproc is not None:
             self.__audioproc.terminate()
 
+    def read_text(self, text):
+        self.__ttsproc.stdin.write(text + '\n')
+        self.__ttsproc.stdin.flush()
+
 if __name__ == '__main__':
     def event_cb(evt):
         print 'Got event:', PhoneEvent.string(evt)