1 def getDefaultGateway():
2 f = open("/proc/net/route", "r")
4 for line in f.readlines():
5 tokens = line.split('\t')
6 if tokens[1] == '00000000': #dest 0.0.0.0
7 return int(tokens[2], 16)
11 f = open("/etc/ppp/options", "r")
13 for line in f.readlines():
14 if line.find('connect') == 0:
15 line = line[line.find(' ')+1:]
16 line = line[line.find(' ')+1:]
17 line = line[:line.find('"')]
21 def setOptions(tel, user):
22 f = open("/etc/ppp/options", "r+")
27 if line.find('connect') == 0:
29 p = line.find(' ', p+1)
31 f.write(line+tel+'"\n')
32 elif line.find('user') == 0:
33 f.write('user '+user+'\n')
37 def getSecretString():
38 f = open("/etc/ppp/pap-secrets", "r")
40 for line in f.readlines():
41 if line[0] == '#' or line.find('*') == -1:
43 for ch in (' ', '\t', '\n', '"'):
44 line = line.replace(ch, '')
48 def setSecretString(secret):
49 f = open("/etc/ppp/pap-secrets", 'r+')
54 if line[0] == '#' or line.find('*') == -1:
59 from Screens.Screen import Screen
60 from Plugins.Plugin import PluginDescriptor
61 from enigma import eConsoleAppContainer, eTimer
62 from Components.Label import Label
63 from Components.Button import Button
64 from Components.ConfigList import ConfigList
65 from Components.config import ConfigText, ConfigPassword, KEY_LEFT, KEY_RIGHT, KEY_0, KEY_DELETE, KEY_BACKSPACE
66 from Components.ActionMap import NumberActionMap, ActionMap
78 print "modem disconnected", ret
80 #FIXMEEE... hardcoded for little endian!!
81 system("route add default gw %d.%d.%d.%d" %(gateway&0xFF, (gateway>>8)&0xFF, (gateway>>16)&0xFF, (gateway>>24)&0xFF))
84 conn = eConsoleAppContainer()
85 conn.appClosed.append(pppdClosed)
87 class ModemSetup(Screen):
89 <screen position="180,100" size="320,300" title="Modem" >
90 <ePixmap pixmap="skin_default/buttons/green.png" position="10,10" size="140,40" alphatest="on" />
91 <ePixmap pixmap="skin_default/buttons/red.png" position="160,10" size="140,40" alphatest="on" />
92 <widget name="key_green" position="10,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
93 <widget name="key_red" position="160,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
94 <widget name="list" position="10,60" size="300,120" />
95 <widget name="state" position="10,210" size="300,80" font="Regular;20" />
101 def __init__(self, session, args = None):
104 self.skin = ModemSetup.skin
105 secret = getSecretString()
106 user = secret[:secret.find('*')]
107 password = secret[secret.find('*')+1:]
108 self.username = ConfigText(user, fixed_size=False)
109 self.password = ConfigPassword(password, fixed_size=False)
110 self.phone = ConfigText(getTelephone(), fixed_size=False)
111 self.phone.setUseableChars(u"0123456789")
112 lst = [ (_("Username"), self.username),
113 (_("Password"), self.password),
114 (_("Phone number"), self.phone) ]
115 self["list"] = ConfigList(lst)
116 self["key_green"] = Button("")
117 self["key_red"] = Button("")
118 self["state"] = Label("")
119 self["actions"] = NumberActionMap(["ModemActions"],
121 "cancel": self.close,
122 "left": self.keyLeft,
123 "right": self.keyRight,
124 "connect": self.connect,
125 "disconnect": self.disconnect,
126 "deleteForward": self.deleteForward,
127 "deleteBackward": self.deleteBackward,
140 self["ListActions"] = ActionMap(["ListboxDisableActions"],
142 "moveUp": self.nothing,
143 "moveDown": self.nothing,
144 "moveTop": self.nothing,
145 "moveEnd": self.nothing,
146 "pageUp": self.nothing,
147 "pageDown": self.nothing
150 self.stateTimer = eTimer()
151 self.stateTimer.callback.append(self.stateLoop)
153 conn.appClosed.append(self.pppdClosed)
154 conn.dataAvail.append(self.dataAvail)
156 Screen.__init__(self, session)
157 self.onClose.append(self.__closed)
158 self.onLayoutFinish.append(self.__layoutFinished)
160 def __layoutFinished(self):
163 self["state"].setText(_("Connected!"));
164 self.green_function = NONE
165 self.red_function = DISCONNECT
167 self.green_function = CONNECT
168 self.red_function = NONE
173 conn.appClosed.remove(self.pppdClosed)
174 conn.dataAvail.remove(self.dataAvail)
177 setOptions(self.phone.getText(), self.username.getText())
178 setSecretString(self.username.getText() + ' * ' + self.password.getText())
181 txt = self["state"].getText()
183 self["state"].setText(txt)
186 if self.green_function == CONNECT:
188 gateway = getDefaultGateway()
189 self["state"].setText(_("Dialing:"))
190 system("route del default")
191 system("modprobe ppp_async");
192 self.stateTimer.start(1000,False)
193 setOptions(self.phone.getText(), self.username.getText())
194 setSecretString(self.username.getText() + ' * ' + self.password.getText())
195 ret = conn.execute("pppd", "pppd", "-d", "-detach")
197 print "execute pppd failed!"
200 self.green_function = NONE
201 self.red_function = ABORT
204 def disconnect(self):
206 self.red_function = NONE
210 if self.green_function == CONNECT:
211 self["list"].handleKey(KEY_LEFT)
214 if self.green_function == CONNECT:
215 self["list"].handleKey(KEY_RIGHT)
217 def keyNumber(self, number):
218 if self.green_function == CONNECT:
219 self["list"].handleKey(KEY_0 + number)
221 def deleteForward(self):
222 if self.green_function == CONNECT:
223 self["list"].handleKey(KEY_DELETE)
225 def deleteBackward(self):
226 if self.green_function == CONNECT:
227 self["list"].handleKey(KEY_BACKSPACE)
229 def pppdClosed(self, retval):
231 self.stateTimer.stop()
232 self.red_function = NONE
233 self.green_function = CONNECT
234 self["state"].setText("")
238 def dataAvail(self, text):
239 if text.find("Serial connection established") != -1:
240 tmp = self["state"].getText()
242 self["state"].setText(tmp)
243 if text.find("PAP authentication succeeded") != -1:
244 tmp = self["state"].getText()
246 self["state"].setText(tmp)
247 self.stateTimer.stop()
248 if text.find("ip-up finished") != -1:
250 tmp = self["state"].getText()
251 tmp += "Connected :)\n"
252 self["state"].setText(tmp)
253 self.red_function = DISCONNECT
255 if text.find("Connect script failed") != -1:
256 tmp = self["state"].getText()
258 self["state"].setText(tmp)
259 self.stateTimer.stop()
260 self.red_function = NONE
261 self.green_function = CONNECT
265 if self.red_function == NONE:
266 self["key_red"].setText("")
267 elif self.red_function == DISCONNECT:
268 self["key_red"].setText(_("Disconnect"))
269 elif self.red_function == ABORT:
270 self["key_red"].setText(_("Abort"))
271 if self.green_function == NONE:
272 self["key_green"].setText("")
273 elif self.green_function == CONNECT:
274 self["key_green"].setText(_("Connect"))
275 focus_enabled = self.green_function == CONNECT
276 self["list"].instance.setSelectionEnable(focus_enabled)
277 self["ListActions"].setEnabled(not focus_enabled)
279 def main(session, **kwargs):
280 session.open(ModemSetup)
282 def Plugins(**kwargs):
283 return PluginDescriptor(name="Modem", description="plugin to connect to internet via builtin modem", where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main)