""" This file is part of Mokonnect. Mokonnect is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License Version 3 as published by the Free Software Foundation. Mokonnect is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Mokonnect. If not, see . """ # # mkdev_gprs.py # mokonnect device gprs network # import mkbase import time import socket import struct import fcntl def _ifinfo(sock, addr, ifname): iface = struct.pack('256s', ifname[:15]) info = fcntl.ioctl(sock.fileno(), addr, iface) if addr == 0x8927: hwaddr = [] for char in info[18:24]: hwaddr.append(hex(ord(char))[2:]) return ':'.join(hwaddr) else: return socket.inet_ntoa(info[20:24]) def ifconfig(ifname): ifreq = {'ifname': ifname} sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: ifreq['addr'] = _ifinfo(sock, 0x8915, ifname) # SIOCGIFADDR ifreq['brdaddr'] = _ifinfo(sock, 0x8919, ifname) #SIOCGIFBRDADDR ifreq['netmask'] = _ifinfo(sock, 0x891b, ifname) #SIOCGIFNETMASK ifreq['hwaddr'] = _ifinfo(sock, 0x8927, ifname) #SIOCSIFHWADDR except: pass sock.close() return ifreq class IFacePanel(mkbase.MKPanel): def __init__(self,win): mkbase.MKPanel.__init__(self,win) self.gui = { "type": "frame", "label": "Interface Configuration", "align": (-1.0,0.0), "content": { "type": "box", "content": [ {"type":"check","label":"Default","config_link":"default"}, ] } } self.config = {"default":True} class GPRSPanel(mkbase.MKPanel): def __init__(self,win): mkbase.MKPanel.__init__(self,win) self.gui = { "type": "frame", "label": "GPRS Configuration", "align": (-1.0,0.0), "content": { "type": "table", "cols": 3, "content": [ [ {"type":"label","label":"APN"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"apn"} ], [ {"type":"label","label":"Username"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"user"} ], [ {"type":"label","label":"Password"}, {"type":"box","align":(-1.0,0.0),"weight":(1,1)}, {"type":"entry","config_link":"pass"} ], ] } } self.config = {"apn":"internet","user":"internet","pass":"internet"} class GPRSDevice(mkbase.MKDevice): def __init__(self,win,pager,qdbus): mkbase.MKDevice.__init__(self,win) self.panels = [IFacePanel(win),GPRSPanel(win)] self.name = "GPRS (Beta)" self.timeout_connect = 30 self.active_timer = None self.bus = qdbus def DbusSignal(self,*args,**kargs): print "debug signal! %s %s" % (str(args),str(kargs)) def _DbusReply(self,*args,**kargs): print "Got Reply..." print args print kargs return 0 def _DbusError(self,error): self.log("GPRS Error: %s" % error) return 0 def _TimePassed(self,*args,**kargs): self.log("Timeout reached, connection failed...") self.log("__DONE__") self.active_timer = None return False def _ConnectToGPRS(self): self.log("Starting GPRS Connection.") self.active_timer = mkbase.SetTimeout(self.timeout_connect,self._TimePassed) self.device.ActivateContext( self.panels[1].config["apn"], self.panels[1].config["user"], self.panels[1].config["pass"], reply_handler = self._DbusReply, error_handler = self._DbusError) def DbusSignal(self,*args,**kargs): #print "got signal! %s %s" % (str(args),str(kargs)) if kargs["signal"] == "ContextStatus": if args[1] == "release": if self.should_poweroff: self.log("GPRS Connection was released.") self.log("__DONE__") return if self.disconnect_first: self.disconnect_first = False time.sleep(2) # some delay... self._ConnectToGPRS() return if self.posted_fail_msg: return self.posted_fail_msg = True self.log("Connection Failed...") # cancel timeout if self.active_timer: self.active_timer.delete() self.active_timer = None self.log("__DONE__") if args[1] == "outgoing": if not self.posted_con_msg: self.posted_con_msg = True self.log("Connecting to '%s'..." % self.panels[1].config["apn"]) if args[1] == "active": self.log("Connection established.") # cancel timeout if self.active_timer: self.active_timer.delete() self.active_timer = None info = ifconfig("ppp0") self.log("IP: %s" % info["addr"]) self.log("Netmask: %s" % info["netmask"]) self.log("Done!") self.log("__DONE__") def Apply(self,log): self.log = log self.should_poweroff = False self.disconnect_first = False self.posted_con_msg = False self.posted_fail_msg = False print "connecting to pdp" self.device = self.bus["ogsmd"]["/org/freesmartphone/GSM/Device"]["PDP"] print "creating callback" self.bus["ogsmd"].AddSignalFunction(self.DbusSignal) print "asking for status" status = self.device.GetContextStatus() print "gprs status: %s" % status if status != "release": self.log("GPRS connection is taken, signaling release...") self.disconnect_first = True print "calling deactivate context" self.device.DeactivateContext() print "done calling it" # relegate the rest of the connection to callback return self._ConnectToGPRS() def PowerOff(self,log): self.log = log self.device = self.bus["ogsmd"]["/org/freesmartphone/GSM/Device"]["PDP"] status = self.device.GetContextStatus() print "gprs status: %s" % status if status == "release": log("GPRS not connected") log("__DONE__") return self.should_poweroff = True self.bus["ogsmd"].AddSignalFunction(self.DbusSignal) self.device.DeactivateContext() return True def PowerOn(self,log): return True