Newer
Older
Import / projects / LGN-IP3870 / t / orig / ppp.py
import os
import runtime
import config
import status
import utils
from setting import setting
dspg = runtime.dspg

g_pppclosing = False

class PPP:
	def __init__(self,sw_update_mode=0):
		self.ppp_pid = None
		self.ppp_number = None
		self.ppp_use = True
		self.conf_file = "/usr/local/lgvp/vdci.cfg"
		self.ppp_data = "/tmp/ppp.log"
		self.ppp_speed = "/tmp/ppp_speed"
		self.up_speed = 0
		self.dn_speed = 0
	 	self.sw_update_mode = sw_update_mode
		self.connect_step = 1
		self.ntp_pid = 0
		self.tag_check_timer = None
		self.upd = 0#update if 1
		self.tag_check_timer2  = None
		self.tag_check_stop_ppp = None
		self.tag_play_message = None
		self.cb = None
		self.fail_cb = None
		self.timeout_cb = None

		try:
			os.unlink( "/tmp/ppp.log" )
		except OSError:
			pass
		try:
			os.unlink( "/tmp/ppp_speed" )
		except OSError:
			pass

	def call_ppp(self, nas_number, cb=None, fail_cb=None, timeout_cb=None,
			play_message=False, upd = 0):
		self.upd = upd
		try:
			os.unlink("/var/log/lgvp.log")
		except OSError:
			pass


		status.phone_status = status.PPPConnected
		self.cb = cb
		self.fail_cb = fail_cb
		self.timeout_cb = timeout_cb

	 	if (1 == self.sw_update_mode or 2 == self.sw_update_mode):
			runtime.modem.close()
			dspg.set_state(dspg.ST_V_SPK_OFFHK)

		if (0 == self.sw_update_mode):
			option_file = "/usr/local/ppp/options.client.sac12"
		elif (1 == self.sw_update_mode):
			option_file = "/usr/local/ppp/options.client.sac4"
		elif (2 == self.sw_update_mode):
			option_file = "/usr/local/ppp/options.client.sac5"

		if config.videocall_debug:
			import time, ntptime
			print "BEFORE FORK(): ", ntptime.ntime()
		self.ppp_pid = os.fork()
		if self.ppp_pid == 0:
			try:
				#all user wait T4 after release the audio call and then call PPP
				#sleep( ( config.timerT4 / 1000.0 ) )	# timerT4's unit is ms.
				#from setting import setting
				if setting.phone_extension:
					nas_number = setting.phone_extension + ',' + nas_number
				if config.videocall_debug:
					import time, ntptime
					print "AFTER FORK(): ", ntptime.ntime()
				os.execlp('ppp-call','ppp-call',nas_number,option_file)
			except:
				os._exit(1)
		self.tag_check_timer = utils.Timer( 1000, self.check_ppp )
		if self.upd:
			self.tag_check_timer2 = utils.Timer( config.timerPPPfail, self.timeout_cb)
		if play_message:
			self.tag_play_message = utils.Timer(1000, utils.player.play_message)
		return True

	# eicho add 06.02.07
	def stop_connecting_msg(self):
		#print 'eicho) ppp.py:: stop_connecting_msg() =='
		if self.tag_play_message  != None:
			self.tag_play_message = None
			utils.player.stop_message()
	# eicho end.


	def check_ppp(self): # check success of ppp connection
		if self.connect_step == 1:	# check modemhandshake
			if self.log_ppp( 1 ):
				utils.log_time('Modem handshake done')
				self.connect_step = 2
				return True
		else:	# check PPP negotiation
			try:
				fd = open( self.ppp_data )
				fd.close()
				if not self.log_ppp( 2 ):
					return True

				utils.log_time('PPP negotiation done')
				if self.tag_check_timer:
					self.tag_check_timer = None
				if self.upd:
					if self.tag_check_timer2:
						self.tag_check_timer2 = None
				self.connect_step = 1
				self.tag_check_stop_ppp = utils.Timer(1000, self.check_stop_ppp)
				if self.cb:
					self.cb()
					if self.upd:
						if self.tag_check_timer2:
							self.tag_check_timer2 = None
				return False	# Connection complete
			except IOError:
				import sys
				type, value, tb = sys.exc_info()
				if value.errno == 2:	# if file not found
					pass
				else:
					return False
		return True

	def check_stop_ppp(self):
		result = os.system( 'cat /proc/net/dev | grep ppp0 > /dev/null' )
		if result != 0:
			if self.fail_cb:
				self.fail_cb()
			return False
		return True


	def stop_ppp(self, download_cmd = False):
		global g_pppclosing
		g_pppclosing = True
		if self.tag_check_stop_ppp:
			self.tag_check_stop_ppp = None
		if self.tag_check_timer:
			self.tag_check_timer = None
		if self.tag_check_timer2:
			self.tag_check_timer2 = None
		if self.tag_play_message:
			self.tag_play_message = None
		if not self.ppp_use:
			return False

		import signal

		def clear_ppp():
			try:
				if self.ppp_pid:
					if not runtime.updComm:
						runtime.manager.isUpdating = False#ppp후 snmp kill 위해서 여기서 #처리

					os.system( "killall -9 msntp" )
					os.system( "killall -9 ppp-call" )
					os.system( "killall -9 pppd" )
					os.system( "killall -9 chat" )
					os.system( "killall -9 ppp-on-dialer" )
					os.kill(self.ppp_pid, signal.SIGKILL)
					os.waitpid(self.ppp_pid, 0)
				self.ppp_pid = None
			except OSError:
				pass

			# recovering modem
#			import time, ntptime
#			time.sleep(2)
			counter = 0

			# do hw_reset and comment out the previous method.
			while True:
				try:
					print 'eicho) clear_ppp:modem.monitor() -'
					runtime.modem.monitor(True)
					runtime.modem.leave_ppp_mode()
					break
				except IOError:
#MMW //2006. 1.21
					if counter == config.modem_recover_counter :
						#sw reset
						runtime.modem.sw_reset()
						break
					elif counter >= config.modem_recover_counter+1:
						#hw reset
						print 'eicho) clear_ppp:modem.hw_reset() -'
						runtime.modem.hw_reset()
						break
					counter += 1

#end of MMW

			status.phone_status = status.Disconnected

			global g_pppclosing
			g_pppclosing = False
			from mms_net_sendstage import Change_MMSEndStage
			Change_MMSEndStage()
			if status.modem_audio_state == status.HS:
				dspg.set_state(dspg.ST_HS_OFFHK)
				import model
				if self.upd:
					if model.OffHookStage:
						runtime.manager.change_stage(model.OffHookStage, True)
			else:
				if self.upd:
					if status.modem_audio_state == status.SPK:
						dspg.set_state(dspg.ST_SPK_OFFHK)
						import model
						if model.OffHookStage:
							runtime.manager.change_stage(model.OffHookStage, True)
				else:
					status.set_modem_audio_state(status.IDLE)
					dspg.set_state(dspg.ST_IDLE)
		try:
			if self.ppp_pid:
				os.system( "ppp-off" )
				runtime.evas.timeout_add( 3000, clear_ppp )
			else:

				e_counter = 0
				while True:
					try:
						runtime.modem.monitor(True)
						runtime.modem.leave_ppp_mode()

						if download_cmd == True:
							try:
# eicho modify 06.05.10
								if setting.pcm_upstream == 1:	# PCM upstream OFF
									runtime.modem.write('AT+PIG=1\r')
								else:				# PCM upstream ON
									runtime.modem.write('AT+PIG=0\r')
# eicho end.
							except:
								try:
									if not runtime.modem:
										runtime.manager.modem_init()


									else :
										try:
											runtime.modem.write_command('ATZ\r')
										except IOError:
											print 'MODEM SW reset fail'
											time.sleep(5)
											runtime.modem.write_command('ATZ\r')
											pass

										modem_patch = config.modem_patch_upstream_on

										for file in modem_patch:
											runtime.modem.load_patch(file)
										runtime.modem.reset()
								except:
									if not runtime.modem:
										runtime.manager.modem_init()

									else :
										if runtime.modem:
											runtime.modem.close()
										runtime.modem = None

										runtime.dspg.modem_reset()

										time.sleep(1)
										runtime.manager.modem_init()
						break
					except IOError:
						if counter >= config.modem_recover_counter:
							runtime.modem.hw_reset()
							runtime.modem.leave_ppp_mode()
							break
						e_counter += 1
				# eicho remove 06.02.07
				"""
				if status.modem_audio_state == status.HS:
					dspg.set_state(dspg.ST_HS_OFFHK)
				else:
					status.set_modem_audio_state(status.IDLE)
					dspg.set_state(dspg.ST_IDLE)
				"""
				# eicho end.
		except OSError:
			pass
		return True


	def parse_conf_file(self):
		fd = open( self.conf_file )
		for line in fd:
			try:
				var_name = line[:line.index('=')]
				value = line[line.index('=')+1:-1]
				if var_name[0] == '#':
					continue
				elif var_name == 'proxyAddress':
					self.ppp_use = False
			except ValueError:
				pass
		fd.close()

	def log_ppp(self,step):
		try:
			fd = open( self.ppp_speed )
			line = fd.readline().strip()
			if line.find("BUSY") != -1 or line.find("NO CARRIER") != -1:
				if self.timeout_cb:
					self.timeout_cb()
				return False
		except:
			pass

		if step == 1:
			try:
				fd = open( self.ppp_speed )
				line = fd.readline().strip()
				start_idx = line.find("MRR: ")
				if start_idx == -1:
					return False	# modem still handshaking
				end_idx = line.find(",")
				prenum = int(line[start_idx+5:end_idx])
				postnum = int(line[end_idx+1:])
				if prenum > postnum:
					self.up_speed = postnum
					self.dn_speed = prenum
				else:
					self.up_speed = prenum
					self.dn_speed = postnum
				log_mesg = "Modem download speed: %d, Modem upload speed: %d"%(self.dn_speed, self.up_speed)
				#runtime.log.log( "End of modem handshake: " + log_mesg )
				status.videocall_connecting = status.VideoCallConnectingNAS
				if runtime.manager.stage.get_name() == 'VideoCallConnecting':
					runtime.manager.stage.update_progress() #Progress bar step 1(Modem)
				return True	# modem handshake ended
			except IOError:
				return False

		else:
			dns1 = dns2 = wins1 = wins2 = None
			for line in file( self.ppp_data ):
				if line.find('#') >= 0:
					line = line[:line.find('#')]
				line = line.strip()
				if not line:
					continue
				var_name, value = [t.strip() for t in line.split('=',2)]
				if var_name == 'DNS1':
					dns1 = value
				elif var_name == 'DNS2':
					dns2 = value
				elif var_name == 'WINS1':
					wins1 = value
				elif var_name == 'WINS2':
					wins2 = value
			if dns1 == None or dns2 == None or wins1 == None or wins2 == None:
				return False
			log_mesg="DNS1: "+dns1+", DNS2: "+dns2+\
					 ", WIN1: "+wins1+", WINS2: "+wins2
			#runtime.log.log( "End of PPP negotiation: " + log_mesg )
			status.videocall_connecting = status.VideoCallConnectingPPP
			if runtime.manager.stage.get_name() == 'VideoCallConnecting':
				runtime.manager.stage.update_progress() #Progress bar step 2(PPP)
			return True
		return False