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.androidtv.internal.protocol.shieldtv;
15 import static org.openhab.binding.androidtv.internal.AndroidTVBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * Contains static methods for constructing LEAP messages
22 * @author Ben Rosenblum - Initial contribution
25 public class ShieldTVRequest {
27 public static String encodeMessage(String message) {
28 StringBuilder reply = new StringBuilder();
29 char[] charArray = message.toCharArray();
30 for (int i = 0; i < charArray.length; i = i + 2) {
31 String st = "" + charArray[i] + "" + charArray[i + 1];
32 char ch = (char) Integer.parseInt(st, 16);
35 return reply.toString();
38 public static String decodeMessage(String message) {
39 StringBuilder sb = new StringBuilder();
40 char ch[] = message.toCharArray();
41 for (int i = 0; i < ch.length; i++) {
42 String hexString = Integer.toHexString(ch[i]);
43 if (hexString.length() % 2 > 0) {
51 public static String pinRequest(String pin) {
52 if (PIN_REQUEST.equals(pin)) {
53 String message = "080a120308cd08";
56 String prefix = "080a121f08d108121a0a06";
57 String encodedPin = decodeMessage(pin);
58 String suffix = "121036646564646461326639366635646261";
59 return prefix + encodedPin + suffix;
63 public static String loginRequest() {
64 String message = "0801121a0801121073616d73756e6720534d2d4739393855180128fbff04";
68 public static String keepAlive() {
69 String message = "080028fae0a6c0d130";
73 private static String fixMessage(String tempMsg) {
74 if (tempMsg.length() % 2 > 0) {
75 tempMsg = "0" + tempMsg;
80 public static String startApp(String message) {
81 int length = message.length();
82 String len1 = fixMessage(Integer.toHexString(length + 6));
83 String len2 = fixMessage(Integer.toHexString(length + 2));
84 String len3 = fixMessage(Integer.toHexString(length));
85 String reply = "08f10712" + len1 + "080212" + len2 + "0a" + len3 + decodeMessage(message);
88 // 080b120308cd08 - Longer Hostname Reply
89 // 08f30712020805 - Unknown
90 // 08f10712020800 - Get all apps
91 // 08ec0712020806 - Get current app
93 public static String keyboardEntry(String entry) {
94 // 08ec07120d08081205616263646532020a0a
95 // 08ec0712 0d 0808 12 05 6162636465 3202 0a0a
96 int length = entry.length();
97 String len1 = fixMessage(Integer.toHexString(length + 8));
98 String len2 = fixMessage(Integer.toHexString(length));
99 String len3 = fixMessage(Integer.toHexString(length * 2));
100 String reply = "08ec0712" + len1 + "080812" + len2 + decodeMessage(entry) + "3202" + len3 + len3;