-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcecclient.py
executable file
·275 lines (211 loc) · 8.1 KB
/
cecclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#! /usr/bin/python3
import cec
import logging
import time
import argparse
argparser = argparse.ArgumentParser(description="CEC client")
argparser.add_argument('--debug', '-d', dest='debug', help='Enable debugging', default=False, action='store_const', const=True)
class CecClient:
cecconfig = cec.libcec_configuration()
lib = None
evtCallback = None
enabled = True
src_port = None
def __init__(self, name):
self.cecconfig.strDeviceName = name
self.cecconfig.bActivateSource = 0
self.cecconfig.deviceTypes.Add(cec.CEC_DEVICE_TYPE_AUDIO_SYSTEM)
self.cecconfig.clientVersion = cec.LIBCEC_VERSION_CURRENT
self.cecconfig.SetLogCallback(self._cecLogCallback)
try:
self.cecconfig.SetCommandHandlerCallback(self._cmdCallback)
except:
print("This client requires a patched version of libcec. The official version doesn't allow the implementation of this features.")
print("See https://github.com/Pulse-Eight/libcec/pull/617\n\n")
raise Exception("Unsupported libcec")
self.evtCallback = self._dummyCecCallback
self.logger = logging.getLogger("CecClient")
def _cecLogCallback(self, level, time, message):
if "device vendor id (87)" in message or "f:87:" in message:
# Don't log spammy vendor ID commands
return
self.logger.debug("CEC: " + message)
def open(self):
self.lib = cec.ICECAdapter.Create(self.cecconfig)
self.logger.info("libCEC version " + self.lib.VersionToString(self.cecconfig.serverVersion) + " loaded: " + self.lib.GetLibInfo())
#search for adapters
adapters = self.lib.DetectAdapters()
if len(adapters) == 0:
self.logger.critical("Not adapters found :-/")
return False
adapter = adapters[0]
self.logger.debug("Found adapter " + adapter.strComName)
self.lib.Open(adapter.strComName)
def reportAudioStatus(self, level, mute):
status = int(level)
if mute:
status += 0x80
cmd = "7A:{:02x}".format(status)
self.sendCommand(cmd)
def enable(self):
if self.enabled:
return
self.enabled = True
self.logger.info("Enabling CEC ARC")
# Power on
self.sendCommand("90:00")
# System audio mode status on
self.sendCommand("72:01")
self.logger.info("CEC ARC enabled")
def disable(self):
if not self.enabled:
return
self.enabled = False
self.logger.info("Disabling CEC ARC")
# Power off
self.sendCommand("90:01")
# System audio mode status off
self.sendCommand("72:00")
self.logger.info("CEC ARC disabled")
def is_enabled(self):
return self.enabled
def get_src_port(self):
return self.src_port
def _cmdCallback(self, cmd):
cmd=cmd[3:]
# Discard source and dest
if cmd[1] != 'f' and cmd[1] != '5':
self.logger.debug("Ignoring command as it's not destined for broadcast or audio-system")
return 0
src = cmd[0]
dst = cmd[1]
cmd = cmd[3:]
if cmd.startswith('87:'):
# Ignore Vendor ID command which are sent at regular interval
return 1
self.logger.debug("Got CEC command " + cmd)
# Key presset event
if cmd.startswith("44:"):
# Parse key press
key = cmd[3:]
if key == '41':
self.logger.debug("Received key : Volume up")
self.evtCallback("level_up")
elif key == '42':
self.logger.debug("Received key : Volume down")
self.evtCallback("level_down")
elif key == '43':
self.logger.debug("Received key : Mute")
self.evtCallback("mute")
else:
return 1
# Key press released
elif cmd == "45":
self.logger.debug("Key released")
# Vendor ID
#elif cmd.startswith("87"):
# self.logger.debug("Received vendor ID : " + cmd[3:] + " from device " + src)
# ARC initiated
elif cmd == "c1":
self.logger.debug("Received: ARC initiated")
self.evtCallback("arc_start")
# ARC terminated
elif cmd == "c2":
self.logger.debug("Recived: ARC terminated")
self.evtCallback("arc_stop")
# ARC start
elif cmd == "c3":
self.logger.debug("Received : ARC start")
self.sendCommand("c0", dst=src)
# ARC end
elif cmd == "c4":
self.logger.debug("Received : ARC stop")
self.sendCommand("c5", dst=src)
# TV in standby
elif cmd == "36":
# Standby
self.logger.debug("Received : TV Standby")
self.evtCallback("standby")
# Get CEC Version
elif cmd == "9f":
self.logger.debug("Received : Get CEC Version")
self.send("9e:05", dst=src) # Version 1.4
# Give audio status
elif cmd == "71":
self.logger.debug("Received : Give audio status")
self.evtCallback("give_audio_status")
# Abort vendor commands
elif cmd == "89":
self.sendCommand("00:89:00", dst=src)
# Give system audio mode status
elif cmd == "7d":
if self.enabled:
# Audio status on
self.sendCommand("7e:01", dst=src)
else:
# Audio status off
self.sendCommand("7e:00", dst=src)
# Give power status
elif cmd == "8f":
if self.enabled:
# Power on
self.sendCommand("90:00", dst=src)
else:
# Standby
self.sendCommand("90:01", dst=src)
# System audio mode request
elif cmd.startswith("70"):
if self.enabled:
self.sendCommand("72:01", dst=src)
else:
self.sendCommand("72:00", dst=src)
# One touch play active source
elif cmd.startswith("82:"):
src_port = cmd[3] + '.' + cmd[4] + '.' + cmd[6] + '.' + cmd[7]
self.logger.debug("Received one touch play on HDMI port " + src_port)
if src_port != self.src_port:
self.src_port = src_port
self.evtCallback("src_changed")
# Routing change
elif cmd.startswith("80:"):
src_port = cmd[9] + '.' + cmd[10] + '.' + cmd[12] + '.' + cmd[13]
self.logger.debug("Received routing change to new address " + src_port)
if src_port != self.src_port:
self.src_port = src_port
self.evtCallback("src_changed")
# Vendor specific command
elif cmd.startswith("a0:"):
self.logger.debug("Aborting vendor specific command")
if dst != 'f':
self.sendCommand("00:" + cmd[0:2] + ":00", dst=src)
# Feature abort
else:
self.logger.debug("Command " + cmd + " not handled")
return 0
self.logger.debug("Command " + cmd + " handled")
return 1
def sendCommand(self, data, src='5', dst='0'):
cmd_str = src + dst + ':' + data
self.logger.debug("Sending command : " + cmd_str)
cmd = self.lib.CommandFromString(cmd_str)
if not self.lib.Transmit(cmd):
self.logger.warning("Error while sending CEC command")
# else:
# self.logger.debug("Sent : " + cmd_str)
def setEventCallback(self, callback):
self.evtCallback = callback
def _dummyCecCallback(self, evt):
self.logger.debug("Got event " + evt)
if evt == "give_audio_status":
# Report dummy status
self.reportAudioStatus(10, False)
if __name__ == '__main__':
cecClient = CecClient("Z906")
cecClient.open()
args = argparser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
while True:
time.sleep(1)