Merge branch 'master' of fraxinas@git.opendreambox.org:/git/enigma2
[enigma2.git] / lib / python / Plugins / SystemPlugins / Hotplug / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from twisted.internet.protocol import Protocol, Factory
3 from twisted.internet import reactor
4 from Components.Harddisk import harddiskmanager
5
6 DEVICEDB =  \
7         { "/devices/pci0000:00/0000:00:14.2/usb1/1-1/1-1:1.0/host0/target0:0:0/0:0:0:0": "CF Slot",
8           "/devices/pci0000:00/0000:00:14.2/usb1/1-1/1-1:1.0/host0/target1:0:0/0:0:0:0": "SD Slot"
9         }
10
11 hotplugNotifier = [ ]
12
13 class Hotplug(Protocol):
14         def getUserfriendlyDeviceName(self, phys):
15                 return DEVICEDB.get(phys, "USB Storage")
16
17         def connectionMade(self):
18                 self.received = ""
19
20         def dataReceived(self, data):
21                 self.received += data
22
23         def connectionLost(self, reason):
24                 data = self.received.split('\0')[:-1]
25
26                 v = {}
27
28                 for x in data:
29                         i = x.find('=')
30                         var, val = x[:i], x[i+1:]
31                         v[var] = val
32
33                 print "hotplug:", v
34
35                 action = v.get("ACTION")
36                 device = v.get("DEVPATH")
37                 physdevpath = v.get("PHYSDEVPATH")
38                 media_state = v.get("X_E2_MEDIA_STATUS")
39
40                 dev = device.split('/')[-1]
41
42                 if action is not None and action == "add":
43                         print "Medium found in", self.getUserfriendlyDeviceName(dev)
44                         harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
45                 elif action is not None and action == "remove":
46                         harddiskmanager.removeHotplugPartition(dev)
47                 elif media_state is not None:
48                         if media_state == '1':
49                                 harddiskmanager.removeHotplugPartition(dev)
50                                 harddiskmanager.addHotplugPartition(dev, self.getUserfriendlyDeviceName(physdevpath))
51                         elif media_state == '0':
52                                 harddiskmanager.removeHotplugPartition(dev)
53                 
54                 for callback in hotplugNotifier:
55                         try:
56                                 callback(dev, action or media_state)
57                         except AttributeError:
58                                 hotplugNotifier.remove(callback)
59
60 def autostart(reason, **kwargs):
61         if reason == 0:
62                 print "starting hotplug handler"
63                 factory = Factory()
64                 factory.protocol = Hotplug
65
66                 try:
67                         import os
68                         os.remove("/tmp/hotplug.socket")
69                 except OSError:
70                         pass
71
72                 reactor.listenUNIX("/tmp/hotplug.socket", factory)
73
74 def Plugins(**kwargs):
75         return PluginDescriptor(name = "Hotplug", description = "listens to hotplug events", where = PluginDescriptor.WHERE_AUTOSTART, fnc = autostart)