Support Vu+ Zero.
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / HDMICEC / components / HdmiCec.py
1 import struct
2 from config import config, ConfigSelection, ConfigYesNo, ConfigSubsection, ConfigText
3 from enigma import eHdmiCEC, eTimer
4 from Screens.Standby import inStandby
5 import Screens.Standby
6 from Tools import Notifications
7 import time
8 from os import system
9 from Tools.Directories import fileExists
10
11
12 class HdmiCec:
13         def __init__(self):
14                 config.hdmicec = ConfigSubsection()
15                 config.hdmicec.enabled = ConfigYesNo(default = False)
16                 config.hdmicec.logenabledserial = ConfigYesNo(default = False)
17                 config.hdmicec.logenabledfile = ConfigYesNo(default = False)
18                 config.hdmicec.tvstandby = ConfigYesNo(default = False)
19                 config.hdmicec.tvwakeup = ConfigYesNo(default = False)
20                 config.hdmicec.boxstandby = ConfigYesNo(default = False)
21                 config.hdmicec.enabletvrc = ConfigYesNo(default = True)
22                 config.hdmicec.active_source_reply = ConfigYesNo(default = True)
23                 config.hdmicec.standby_message = ConfigSelection(
24                         choices = {
25                         "standby,inactive": _("TV standby"),
26                         "standby,avpwroff,inactive,": _("TV + A/V standby"),
27                         "inactive": _("Source inactive"),
28                         "nothing": _("Nothing"),
29                         },
30                         default = "standby,inactive")
31                 config.hdmicec.deepstandby_message = ConfigSelection(
32                         choices = {
33                         "standby,inactive": _("TV standby"),
34                         "standby,avdeeppwroff,inactive": _("TV + A/V standby"),
35                         "inactive": _("Source inactive"),
36                         "nothing": _("Nothing"),
37                         },
38                         default = "standby,inactive")
39                 config.hdmicec.wakeup_message = ConfigSelection(
40                         choices = {
41                         "wakeup,active,activevu": _("TV wakeup"),
42                         "wakeup,avpwron,active,activevu": _("TV + A/V wakeup"),
43                         "active": _("Source active"),
44                         "nothing": _("Nothing"),
45                         },
46                         default = "wakeup,active,activevu")
47                 config.hdmicec.vustandby_message = ConfigSelection(
48                         choices = {
49                         "vustandby": _("VU standby"),
50                         "vudeepstandby": _("VU DeepStandby"),
51                         "vunothing": _("Nothing"),
52                         },
53                         default = "vustandby")
54                 config.hdmicec.vuwakeup_message = ConfigSelection(
55                         choices = {
56                         "vuwakeup": _("VU wakeup"),
57                         "vunothing": _("Nothing"),
58                         },
59                         default = "vuwakeup")
60                 config.hdmicec.tvinput = ConfigSelection(default = "1",
61                         choices = [
62                         ("1", _("HDMI 1")),
63                         ("2", _("HDMI 2")),
64                         ("3", _("HDMI 3")),
65                         ("4", _("HDMI 4")),
66                         ("5", _("HDMI 5"))])
67                 config.hdmicec.avinput = ConfigSelection(default ="0",
68                         choices = [
69                         ("0", _("no A/V Receiver")),
70                         ("1", _("HDMI 1")),
71                         ("2", _("HDMI 2")),
72                         ("3", _("HDMI 3")),
73                         ("4", _("HDMI 4")),
74                         ("5", _("HDMI 5"))])
75                 config.hdmicec.devicename = ConfigText(default = self.getDeviceName(), visible_width = 50, fixed_size = False)
76                 config.misc.standbyCounter.addNotifier(self.enterStandby, initial_call = False)
77                 config.misc.DeepStandbyOn.addNotifier(self.enterDeepStandby, initial_call = False)
78                 self.leaveDeepStandby()
79
80         def getDeviceName(self):
81                 deviceList = {
82                         "duo": "VU+ Duo",
83                         "solo": "VU+ Solo",
84                         "uno": "VU+ Uno",
85                         "ultimo": "VU+ Ultimo",
86                         "solo2": "VU+ Solo2",
87                         "duo2": "VU+ Duo2",
88                         "solose": "VU+ SoloSE",
89                         "zero": "VU+ Zero",
90                 }
91                 if fileExists("/proc/stb/info/vumodel"):
92                         vumodel = open("/proc/stb/info/vumodel")
93                         info=vumodel.read().strip()
94                         vumodel.close()
95                         return deviceList.setdefault(info, "VU+")
96                 else:
97                         return "VU+"
98
99         def sendMessages(self, messages):
100                 for message in messages.split(','):
101                         cmd = None
102                         logcmd = None
103                         addressvaluebroadcast = int("0F",16)
104                         addressvalue = int("0",16)
105                         addressvalueav = int("5",16)
106                         wakeupmessage = int("04",16)
107                         standbymessage=int("36",16)
108                         activesourcemessage=int("82",16)
109                         inactivesourcemessage=int("9D",16)
110                         sendkeymessage = int("44",16)
111                         sendkeypwronmessage = int("6D",16)
112                         sendkeypwroffmessage = int("6C",16)
113                         activevumessage=int("85",16)
114                         physaddress1 = int("0x" + str(config.hdmicec.tvinput.value) + str(config.hdmicec.avinput.value),16)
115                         physaddress2 = int("0x00",16)
116
117                         if message == "wakeup":
118                                 cmd = struct.pack('B', wakeupmessage)
119                                 logcmd = "[HDMI-CEC] ** WakeUpMessage ** send message: %x to address %x" % (wakeupmessage, addressvalue)
120                         elif message == "active":
121                                 addressvalue = addressvaluebroadcast
122                                 cmd = struct.pack('BBB', activesourcemessage,physaddress1,physaddress2)
123                                 logcmd = "[HDMI-CEC] ** ActiveSourceMessage ** send message: %x:%x:%x to address %x" % (activesourcemessage,physaddress1,physaddress2,addressvalue)
124                         elif message == "standby":
125                                 cmd = struct.pack('B', standbymessage)
126                                 logcmd = "[HDMI-CEC] ** StandByMessage ** send message: %x to address %x" % (standbymessage, addressvalue)
127                         elif message == "inactive":
128                                 addressvalue = addressvaluebroadcast
129                                 cmd = struct.pack('BBB', inactivesourcemessage,physaddress1,physaddress2)
130                                 logcmd = "[HDMI-CEC] ** InActiveSourceMessage ** send message: %x:%x:%x to address %x" % (inactivesourcemessage,physaddress1,physaddress2,addressvalue)
131                         elif message == "avpwron":
132                                 cmd = struct.pack('BB', sendkeymessage,sendkeypwronmessage)
133                                 addressvalue = addressvalueav
134                                 logcmd = "[HDMI-CEC] ** Power on A/V ** send message: %x:%x to address %x" % (sendkeymessage, sendkeypwronmessage, addressvalue)
135                         elif message == "avdeeppwroff":
136                                 cmd = struct.pack('BB',sendkeymessage,sendkeypwroffmessage)
137                                 addressvalue = addressvalueav
138                                 logcmd = "[HDMI-CEC] ** Standby A/V (Deepstandby)** send message: %x:%x to address %x" % (sendkeymessage,sendkeypwroffmessage, addressvalue)
139                         elif message == "avpwroff":
140                                 addressvalue = addressvalueav
141                                 cmd = struct.pack('BB',sendkeymessage,sendkeypwroffmessage)
142                                 logcmd = "[HDMI-CEC] ** Standby A/V ** send message: %x:%x to address %x" % (sendkeymessage,sendkeypwroffmessage, addressvalue)
143                         elif message == "activevu":
144                                 addressvalue = addressvaluebroadcast
145                                 cmd = struct.pack('B', activevumessage)
146                                 logcmd = "[HDMI-CEC] ** Active VU Message ** send message: %x to address %x" % (activevumessage,addressvalue)
147                         if cmd:
148                                 eHdmiCEC.getInstance().sendMessage(addressvalue, len(cmd), str(cmd))
149                                 time.sleep(1)
150                         if logcmd:
151                                 if config.hdmicec.logenabledserial.value:
152                                         print logcmd
153                                         if config.hdmicec.logenabledfile.value:
154                                                 filelog = "echo %s >> /tmp/hdmicec.log" % (logcmd)
155                                                 system(filelog)
156
157
158         def leaveStandby(self):
159                 if config.hdmicec.enabled.value is True:
160                         self.sendMessages(config.hdmicec.wakeup_message.value)
161
162         def enterStandby(self, configElement):
163                 from Screens.Standby import inStandby
164                 inStandby.onClose.append(self.leaveStandby)
165                 if config.hdmicec.enabled.value is True:
166                         self.sendMessages(config.hdmicec.standby_message.value)
167
168         def enterDeepStandby(self,configElement):
169                 if config.hdmicec.enabled.value is True:
170                         self.sendMessages(config.hdmicec.deepstandby_message.value)
171
172         def leaveDeepStandby(self):
173                 if config.hdmicec.enabled.value is True:
174                         self.sendMessages(config.hdmicec.wakeup_message.value)
175                         
176 ## not used
177         def activeSource(self):
178                 if config.hdmicec.enabled.value is True:
179                         physadress1 = "0x" + str(config.hdmicec.tvinput.value) + str(config.hdmicec.avinput.value)
180                         physadress2 = "0x00"
181                         cecmessage = int('0x82',16)
182                         address = int('0x0F',16)
183                         valuethree = int(physadress1,16)
184                         valuefour = int(physadress2,16)
185                         cmd = struct.pack('BBB',cecmessage,valuethree,valuefour)
186                         eHdmiCEC.getInstance().sendMessage(address, len(cmd), str(cmd))
187                         if config.hdmicec.enabletvrc.value:
188                                         cecmessage = int('0x8E',16)
189                                         address = int('0',16)
190                                         valuethree = int('0',16)
191                                         cmd = struct.pack('BB',cecmessage,valuethree)
192                                         eHdmiCEC.getInstance().sendMessage(address, len(cmd), str(cmd))
193
194 hdmi_cec = HdmiCec()