2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.magentatv.internal.handler;
15 import static org.openhab.binding.magentatv.internal.MagentaTVBindingConstants.*;
16 import static org.openhab.binding.magentatv.internal.MagentaTVUtil.*;
18 import java.nio.charset.StandardCharsets;
19 import java.security.MessageDigest;
20 import java.security.NoSuchAlgorithmException;
21 import java.text.MessageFormat;
22 import java.util.HashMap;
23 import java.util.StringTokenizer;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jetty.client.HttpClient;
27 import org.openhab.binding.magentatv.internal.MagentaTVException;
28 import org.openhab.binding.magentatv.internal.config.MagentaTVDynamicConfig;
29 import org.openhab.binding.magentatv.internal.network.MagentaTVHttp;
30 import org.openhab.binding.magentatv.internal.network.MagentaTVNetwork;
31 import org.openhab.binding.magentatv.internal.network.MagentaTVOAuth;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link MagentaTVControl} implements the control functions for the
39 * @author Markus Michels - Initial contribution
42 public class MagentaTVControl {
43 private final Logger logger = LoggerFactory.getLogger(MagentaTVControl.class);
44 private static final HashMap<String, String> KEY_MAP = new HashMap<>();
46 private final MagentaTVNetwork network;
47 private final MagentaTVHttp http = new MagentaTVHttp();
48 private final MagentaTVOAuth oauth;
49 private final MagentaTVDynamicConfig config;
50 private boolean initialized = false;
51 private String thingId = "";
53 public MagentaTVControl() {
54 config = new MagentaTVDynamicConfig();
55 network = new MagentaTVNetwork();
56 oauth = new MagentaTVOAuth(new HttpClient());
59 public MagentaTVControl(MagentaTVDynamicConfig config, MagentaTVNetwork network, HttpClient httpClient) {
60 this.thingId = config.getFriendlyName();
61 this.network = network;
62 this.oauth = new MagentaTVOAuth(httpClient);
64 this.config.setTerminalID(computeMD5(network.getLocalMAC().toUpperCase() + config.getUDN()));
65 this.config.setLocalIP(network.getLocalIP());
66 this.config.setLocalMAC(network.getLocalMAC());
70 public boolean isInitialized() {
74 public void setThingId(String thingId) {
75 this.thingId = thingId;
79 * Returns the thingConfig - the Control class adds various attributes of the
80 * discovered device (like the MR model)
84 public MagentaTVDynamicConfig getConfig() {
89 * Initiate OAuth authentication
91 * @param accountName T-Online user id
92 * @param accountPassword T-Online password
93 * @return true: successful, false: failed
95 * @throws MagentaTVException
97 public String getUserId(String accountName, String accountPassword) throws MagentaTVException {
98 return oauth.getUserId(accountName, accountPassword);
102 * Retries the device properties. This will result in an Exception if the device
105 * Response is returned in XMl format, e.g.:
106 * <?xml version="1.0"?> <root xmlns="urn:schemas-upnp-org:device-1-0">
107 * <specVersion><major>1</major><minor>0</minor></specVersion> <device>
108 * <UDN>uuid:70dff25c-1bdf-5731-a283-XXXXXXXX</UDN>
109 * <friendlyName>DMS_XXXXXXXXXXXX</friendlyName>
110 * <deviceType>urn:schemas-upnp-org:device:tvdevice:1</deviceType>
111 * <manufacturer>Zenterio</manufacturer> <modelName>MR401B</modelName>
112 * <modelNumber>R01A5</modelNumber> <productVersionNumber>" 334
113 * "</productVersionNumber> <productType>stb</productType>
114 * <serialNumber></serialNumber> <X_wakeOnLan>0</X_wakeOnLan> <serviceList>
115 * <service> <serviceType>urn:dial-multiscreen-org:service:dial:1</serviceType>
116 * <serviceId>urn:dial-multiscreen-org:service:dial</serviceId> </service>
117 * </serviceList> </device> </root>
119 * @return true: device is online, false: device is offline
120 * @throws MagentaTVException
122 public boolean checkDev() throws MagentaTVException {
123 logger.debug("{}: Check device {} ({}:{})", thingId, config.getTerminalID(), config.getIpAddress(),
126 String url = MessageFormat.format(CHECKDEV_URI, config.getIpAddress(), config.getPort(),
127 config.getDescriptionUrl());
128 String result = http.httpGet(buildHost(), url, "");
129 if (result.contains("<modelName>")) {
130 config.setModel(substringBetween(result, "<modelName>", "</modelName>"));
132 if (result.contains("<modelNumber>")) {
133 config.setHardwareVersion(substringBetween(result, "<modelNumber>", "</modelNumber>"));
135 if (result.contains("<X_wakeOnLan>")) {
136 String wol = substringBetween(result, "<X_wakeOnLan>", "</X_wakeOnLan>");
137 config.setWakeOnLAN(wol);
138 logger.debug("{}: Wake-on-LAN is {}", thingId, "0".equals(wol) ? "disabled" : "enabled");
140 if (result.contains("<productVersionNumber>")) {
142 if (result.contains("<productVersionNumber>" ")) {
143 version = substringBetween(result, "<productVersionNumber>" ", " "</productVersionNumber>");
145 version = substringBetween(result, "<productVersionNumber>", "</productVersionNumber>");
147 config.setFirmwareVersion(version);
149 if (result.contains("<friendlyName>")) {
150 String friendlyName = result.substring(result.indexOf("<friendlyName>") + "<friendlyName>".length(),
151 result.indexOf("</friendlyName>"));
152 config.setFriendlyName(friendlyName);
154 if (result.contains("<UDN>uuid:")) {
155 String udn = result.substring(result.indexOf("<UDN>uuid:") + "<UDN>uuid:".length(),
156 result.indexOf("</UDN>"));
157 if (config.getUDN().isEmpty()) {
161 logger.trace("{}: Online status verified for device {}:{}, UDN={}", thingId, config.getIpAddress(),
162 config.getPort(), config.getUDN());
168 * Sends a SUBSCRIBE request to the MR. This also defines the local callback url
169 * used by the MR to return the pairing code and event information.
171 * Subscripbe to event channel a) receive the pairing code b) receive
172 * programInfo and playStatus events after successful paring
174 * SUBSCRIBE /upnp/service/X-CTC_RemotePairing/Event HTTP/1.1\r\n HOST:
175 * $remote_ip:$remote_port CALLBACK: <http://$local_ip:$local_port/>\r\n // NT:
176 * upnp:event\r\n // TIMEOUT: Second-300\r\n // CONNECTION: close\r\n // \r\n
178 * @throws MagentaTVException
180 public void subscribeEventChannel() throws MagentaTVException {
182 logger.debug("{}: Subscribe Event Channel (terminalID={}, {}:{}", thingId, config.getTerminalID(),
183 config.getIpAddress(), config.getPort());
184 String subscribe = MessageFormat.format(PAIRING_SUBSCRIBE, config.getIpAddress(), config.getPort(),
185 network.getLocalIP(), network.getLocalPort(), PAIRING_NOTIFY_URI, PAIRING_TIMEOUT_SEC);
186 String response = http.sendData(config.getIpAddress(), config.getPort(), subscribe);
187 if (!response.contains("200 OK")) {
188 response = substringBefore(response, "SERVER");
189 throw new MagentaTVException("Unable to subscribe to pairing channel: " + response);
191 if (!response.contains(NOTIFY_SID)) {
192 throw new MagentaTVException("Unable to subscribe to pairing channel, SID missing: " + response);
195 StringTokenizer tokenizer = new StringTokenizer(response, "\r\n");
196 while (tokenizer.hasMoreElements()) {
197 String str = tokenizer.nextToken();
198 if (!str.isEmpty()) {
199 if (str.contains(NOTIFY_SID)) {
200 sid = str.substring("SID: uuid:".length());
201 logger.debug("{}: SUBSCRIBE returned SID {}", thingId, sid);
209 * Send Pairing Request to the Media Receiver. The method waits for the
210 * response, but the pairing code will be received via the NOTIFY callback (see
213 * XML format for Pairing Request: <s:Envelope
214 * xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"
215 * <s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <s:Body>\n
216 * <u:X-pairingRequest
217 * xmlns:u=\"urn:schemas-upnp-org:service:X-CTC_RemotePairing:1\">\n
218 * <pairingDeviceID>$pairingDeviceID</pairingDeviceID>\n
219 * <friendlyName>$friendlyName</friendlyName>\n <userID>$userID</userID>\n
220 * </u:X-pairingRequest>\n </s:Body> </s:Envelope>
222 * @returns true: pairing successful
223 * @throws MagentaTVException
225 public boolean sendPairingRequest() throws MagentaTVException {
226 logger.debug("{}: Send Pairing Request (deviceID={}, type={}, userID={})", thingId, config.getTerminalID(),
227 DEF_FRIENDLY_NAME, config.getUserId());
230 String soapBody = MessageFormat.format(PAIRING_SOAP_BODY, config.getTerminalID(), DEF_FRIENDLY_NAME,
232 String soapXml = MessageFormat.format(SOAP_ENVELOPE, soapBody);
233 String response = http.httpPOST(buildHost(), buildReceiverUrl(PAIRING_CONTROL_URI), soapXml,
234 PAIRING_SOAP_ACTION, CONNECTION_CLOSE);
236 // pairingCode will be received by the Servlet, is calls onPairingResult()
237 // Exception if request failed (response code != HTTP_OK)
238 if (!response.contains("X-pairingRequestResponse") || !response.contains("<result>")) {
239 throw new MagentaTVException("Unexpected result for pairing response: " + response);
242 String result = substringBetween(response, "<result>", "</result>");
243 if (!"0".equals(result)) {
244 throw new MagentaTVException("Pairing failed, result=" + result);
247 logger.debug("{}: Pairing initiated (deviceID={}).", thingId, config.getTerminalID());
252 * Calculates the verifificationCode to complete pairing. This will be triggered
253 * as a result after receiving the pairing code provided by the MR. The
254 * verification code is the MD5 hash of <Pairing Code><Terminal-ID><User ID>
256 * @param pairingCode Pairing code received from the MR
257 * @return true: a new code has been generated, false: the code matches a
260 public boolean generateVerificationCode(String pairingCode) {
261 if (config.getPairingCode().equals(pairingCode) && !config.getVerificationCode().isEmpty()) {
262 logger.debug("{}: Pairing code ({}) refreshed, verificationCode={}", thingId, pairingCode,
263 config.getVerificationCode());
266 config.setPairingCode(pairingCode);
267 String md5Input = pairingCode + config.getTerminalID() + config.getUserId();
268 config.setVerificationCode(computeMD5(md5Input).toUpperCase());
269 logger.debug("{}: VerificationCode({}): Input={}, code={}", thingId, config.getTerminalID(), md5Input,
270 config.getVerificationCode());
275 * Send a pairing verification request to the receiver. This is important to
276 * complete the pairing process. You should see a message like "Connected to
277 * openHAB" on your TV screen.
279 * @return true: successful, false: a non-critical error occured, caller handles
281 * @throws MagentaTVException
283 public boolean verifyPairing() throws MagentaTVException {
284 logger.debug("{}: Verify pairing (id={}, code={}", thingId, config.getTerminalID(),
285 config.getVerificationCode());
286 String soapBody = MessageFormat.format(PAIRCHECK_SOAP_BODY, config.getTerminalID(),
287 config.getVerificationCode());
288 String soapXml = MessageFormat.format(SOAP_ENVELOPE, soapBody);
289 String response = http.httpPOST(buildHost(), buildReceiverUrl(PAIRCHECK_URI), soapXml, PAIRCHECK_SOAP_ACTION,
292 // Exception if request failed (response code != HTTP_OK)
293 if (!response.contains("<pairingResult>")) {
294 throw new MagentaTVException("Unexpected result for pairing verification: " + response);
297 String result = getXmlValue(response, "pairingResult");
298 if (!"0".equals(result)) {
299 logger.debug("{}: Pairing failed or pairing no longer valid, result={}", thingId, result);
301 // let the caller decide how to proceed
305 if (!config.isMR400()) {
306 String enable4K = getXmlValue(response, "Enable4K");
307 String enableSAT = getXmlValue(response, "EnableSAT");
308 logger.debug("{}: Features: Enable4K:{}, EnableSAT:{}", thingId, enable4K, enableSAT);
315 * @return true if pairing is completed (verification code was generated)
317 public boolean isPaired() {
318 // pairing was completed successful if we have the verification code
319 return !config.getVerificationCode().isEmpty();
323 * Reset pairing information (e.g. when verification failed)
325 public void resetPairing() {
326 // pairing no longer valid
327 config.setPairingCode("");
328 config.setVerificationCode("");
332 * Send key code to the MR (via SOAP request). A key code could be send by it's
333 * code (0x.... notation) or with a symbolic namne, which will first be mapped
336 * XML format for Send Key
338 * <s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"
339 * s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> <s:Body>\n
341 * xmlns:u=\"urn:schemas-upnp-org:service:X-CTC_RemoteControl:1\">\n
342 * <InstanceID>0</InstanceID>\n
343 * <KeyCode>keyCode=$keyCode^$pairingDeviceID:$verificationCode^userID:$userID</KeyCode>\n
344 * </u:X_CTC_RemoteKey>\n </s:Body></s:Envelope>
347 * @return true: successful, false: failed, e.g. unkown key code
348 * @throws MagentaTVException
350 public boolean sendKey(String keyName) throws MagentaTVException {
351 String keyCode = getKeyCode(keyName);
352 logger.debug("{}: Send Key {} (keyCode={}, tid={})", thingId, keyName, keyCode, config.getTerminalID());
353 if (keyCode.length() <= "0x".length()) {
354 logger.debug("{}: Key {} is unkown!", thingId, keyCode);
358 String soapBody = MessageFormat.format(SENDKEY_SOAP_BODY, keyCode, config.getTerminalID(),
359 config.getVerificationCode(), config.getUserId());
360 String soapXml = MessageFormat.format(SOAP_ENVELOPE, soapBody);
361 logger.debug("{}: send keyCode={} to {}:{}", thingId, keyCode, config.getIpAddress(), config.getPort());
362 logger.trace("{}: sendKey terminalid={}, pairingCode={}, verificationCode={}, userId={}", thingId,
363 config.getTerminalID(), config.getPairingCode(), config.getVerificationCode(), config.getUserId());
364 http.httpPOST(buildHost(), buildReceiverUrl(SENDKEY_URI), soapXml, SENDKEY_SOAP_ACTION, CONNECTION_CLOSE);
365 // Exception if request failed (response code != HTTP_OK)
366 // pairingCode will be received by the Servlet, is calls onPairingResult()
371 * Select channel for TV
373 * @param channel new channel (a sequence of numbers, which will be send one by one)
374 * @return true:ok, false: failed
376 public boolean selectChannel(String channel) throws MagentaTVException {
377 logger.debug("{}: Select channel {}", thingId, channel);
378 for (int i = 0; i < channel.length(); i++) {
379 if (!sendKey("" + channel.charAt(i))) {
384 } catch (InterruptedException e) {
391 * Get key code to send to receiver
393 * @param key Key for which to get the key code
396 private String getKeyCode(String key) {
397 if (key.contains("0x")) {
401 String code = KEY_MAP.get(key);
402 return code != null ? code : "";
406 * Map playStatus code to string for a list of codes see
407 * http://support.huawei.com/hedex/pages/DOC1100366313CEH0713H/01/DOC1100366313CEH0713H/01/resources/dsv_hdx_idp/DSV/en/en-us_topic_0094619231.html
409 * @param playStatus Integer code parsed form json (see EV_PLAYCHG_XXX)
410 * @return playStatus as String
412 public String getPlayStatus(int playStatus) {
413 switch (playStatus) {
414 case EV_PLAYCHG_PLAY:
416 case EV_PLAYCHG_STOP:
418 case EV_PLAYCHG_PAUSE:
420 case EV_PLAYCHG_TRICK:
422 case EV_PLAYCHG_MC_PLAY:
423 return "playing (MC)";
424 case EV_PLAYCHG_UC_PLAY:
425 return "playing (UC)";
426 case EV_PLAYCHG_BUFFERING:
429 return Integer.toString(playStatus);
434 * Map runningStatus code to string for a list of codes see
435 * http://support.huawei.com/hedex/pages/DOC1100366313CEH0713H/01/DOC1100366313CEH0713H/01/resources/dsv_hdx_idp/DSV/en/en-us_topic_0094619523.html
437 * @param runStatus Integer code parsed form json (see EV_EITCHG_RUNNING_XXX)
438 * @return runningStatus as String
440 public String getRunStatus(int runStatus) {
442 case EV_EITCHG_RUNNING_NOT_RUNNING:
444 case EV_EITCHG_RUNNING_STARTING:
446 case EV_EITCHG_RUNNING_PAUSING:
448 case EV_EITCHG_RUNNING_RUNNING:
451 return Integer.toString(runStatus);
456 * builds url from the discovered IP address/port and the requested uri
458 * @param uri requested URI
459 * @return the complete URL
461 public String buildReceiverUrl(String uri) {
462 return MessageFormat.format("http://{0}:{1}{2}", config.getIpAddress(), config.getPort(), uri);
468 * @return formatted string (<ip_address>:<port>)
470 private String buildHost() {
471 return config.getIpAddress() + ":" + config.getPort();
475 * Given a string, return the MD5 hash of the String.
477 * @param unhashed The string contents to be hashed.
478 * @return MD5 Hashed value of the String. Null if there is a problem hashing
481 public static String computeMD5(String unhashed) {
483 byte[] bytesOfMessage = unhashed.getBytes(StandardCharsets.UTF_8);
485 MessageDigest md5 = MessageDigest.getInstance(HASH_ALGORITHM_MD5);
486 byte[] hash = md5.digest(bytesOfMessage);
487 StringBuilder sb = new StringBuilder(2 * hash.length);
488 for (byte b : hash) {
489 sb.append(String.format("%02x", b & 0xff));
492 return sb.toString();
493 } catch (NoSuchAlgorithmException e) {
499 * Helper to parse a Xml tag value from string without using a complex XML class
501 * @param xml Input string in the format <tag>value</tag>
502 * @param tagName The tag to find
503 * @return Tag value (between <tag> and </tag>)
505 public static String getXmlValue(String xml, String tagName) {
506 String open = "<" + tagName + ">";
507 String close = "</" + tagName + ">";
508 if (xml.contains(open) && xml.contains(close)) {
509 return substringBetween(xml, open, close);
515 * Initialize key map (key name -> key code)
517 * for a list of valid key codes see
518 * http://support.huawei.com/hedex/pages/DOC1100366313CEH0713H/01/DOC1100366313CEH0713H/01/resources/dsv_hdx_idp/DSV/en/en-us_topic_0094619112.html
521 KEY_MAP.put("POWER", "0x0100");
522 KEY_MAP.put("MENU", "0x0110");
523 KEY_MAP.put("EPG", "0x0111");
524 KEY_MAP.put("TVMENU", "0x0454");
525 KEY_MAP.put("VODMENU", "0x0455");
526 KEY_MAP.put("TVODMENU", "0x0456");
527 KEY_MAP.put("NVODMENU", "0x0458");
528 KEY_MAP.put("INFO", "0x010C");
529 KEY_MAP.put("TTEXT", "0x0560");
530 KEY_MAP.put("0", "0x0030");
531 KEY_MAP.put("1", "0x0031");
532 KEY_MAP.put("2", "0x0032");
533 KEY_MAP.put("3", "0x0033");
534 KEY_MAP.put("4", "0x0034");
535 KEY_MAP.put("5", "0x0035");
536 KEY_MAP.put("6", "0x0036");
537 KEY_MAP.put("7", "0x0037");
538 KEY_MAP.put("8", "0x0038");
539 KEY_MAP.put("9", "0x0039");
540 KEY_MAP.put("SPACE", "0x0020");
541 KEY_MAP.put("POUND", "0x0069");
542 KEY_MAP.put("STAR", "0x006A");
543 KEY_MAP.put("UP", "0x0026");
544 KEY_MAP.put("DOWN", "0x0028");
545 KEY_MAP.put("LEFT", "0x0025");
546 KEY_MAP.put("RIGHT", "0x0027");
547 KEY_MAP.put("PGUP", "0x0021");
548 KEY_MAP.put("PGDOWN", "0x0022");
549 KEY_MAP.put("DELETE", "0x0008");
550 KEY_MAP.put("ENTER", "0x000D");
551 KEY_MAP.put("SEARCH", "0x0451");
552 KEY_MAP.put("RED", "0x0113");
553 KEY_MAP.put("GREEN", "0x0114");
554 KEY_MAP.put("YELLOW", "0x0115");
555 KEY_MAP.put("BLUE", "0x0116");
556 KEY_MAP.put("OPTION", "0x0460");
557 KEY_MAP.put("OK", "0x000D");
558 KEY_MAP.put("BACK", "0x0008");
559 KEY_MAP.put("EXIT", "0x045D");
560 KEY_MAP.put("PORTAL", "0x0110");
561 KEY_MAP.put("VOLUP", "0x0103");
562 KEY_MAP.put("VOLDOWN", "0x0104");
563 KEY_MAP.put("INTER", "0x010D");
564 KEY_MAP.put("HELP", "0x011C");
565 KEY_MAP.put("SETTINGS", "0x011D");
566 KEY_MAP.put("MUTE", "0x0105");
567 KEY_MAP.put("CHUP", "0x0101");
568 KEY_MAP.put("CHDOWN", "0x0102");
569 KEY_MAP.put("REWIND", "0x0109");
570 KEY_MAP.put("PLAY", "0x0107");
571 KEY_MAP.put("PAUSE", "0x0107");
572 KEY_MAP.put("FORWARD", "0x0108");
573 KEY_MAP.put("TRACK", "0x0106");
574 KEY_MAP.put("LASTCH", "0x045E");
575 KEY_MAP.put("PREVCH", "0x010B");
576 KEY_MAP.put("NEXTCH", "0x0107");
577 KEY_MAP.put("RECORD", "0x0461");
578 KEY_MAP.put("STOP", "0x010E");
579 KEY_MAP.put("BEGIN", "0x010B");
580 KEY_MAP.put("END", "0x010A");
581 KEY_MAP.put("REPLAY", "0x045B");
582 KEY_MAP.put("SKIP", "0x045C");
583 KEY_MAP.put("SUBTITLE", "0x236");
584 KEY_MAP.put("RECORDINGS", "0x045F");
585 KEY_MAP.put("FAV", "0x0119");
586 KEY_MAP.put("SOURCE", "0x0083");
587 KEY_MAP.put("SWITCH", "0x0118");
588 KEY_MAP.put("IPTV", "0x0081");
589 KEY_MAP.put("PC", "0x0082");
590 KEY_MAP.put("PIP", "0x0084");
591 KEY_MAP.put("MULTIVIEW", "0x0562");
592 KEY_MAP.put("F1", "0x0070");
593 KEY_MAP.put("F2", "0x0071");
594 KEY_MAP.put("F3", "0x0072");
595 KEY_MAP.put("F4", "0x0073");
596 KEY_MAP.put("F5", "0x0074");
597 KEY_MAP.put("F6", "0x0075");
598 KEY_MAP.put("F7", "0x0076");
599 KEY_MAP.put("F8", "0x0077");
600 KEY_MAP.put("F9", "0x0078");
601 KEY_MAP.put("F10", "0x0079");
602 KEY_MAP.put("F11", "0x007A");
603 KEY_MAP.put("F12", "0x007B");
604 KEY_MAP.put("F13", "0x007C");
605 KEY_MAP.put("F14", "0x007D");
606 KEY_MAP.put("F15", "0x007E");
607 KEY_MAP.put("F16", "0x007F");
609 KEY_MAP.put("PVR", "0x0461");
610 KEY_MAP.put("RADIO", "0x0462");
612 // Those key codes are missing and not included in the spec
613 // KEY_MAP.put("TV", "0x");
614 // KEY_MAP.put("RADIO", "0x");
615 // KEY_MAP.put("MOVIES", "0x");