]> git.basschouten.com Git - openhab-addons.git/blob
051ac5407c11f01fede1624bf6e0ea1b04691d49
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.androidtv.internal.protocol.shieldtv;
14
15 import static org.openhab.binding.androidtv.internal.AndroidTVBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Contains static methods for constructing LEAP messages
21  *
22  * @author Ben Rosenblum - Initial contribution
23  */
24 @NonNullByDefault
25 public class ShieldTVRequest {
26
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);
33             reply.append(ch);
34         }
35         return reply.toString();
36     }
37
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) {
44                 sb.append('0');
45             }
46             sb.append(hexString);
47         }
48         return sb.toString();
49     }
50
51     public static String pinRequest(String pin) {
52         if (PIN_REQUEST.equals(pin)) {
53             String message = "080a120308cd08";
54             return message;
55         } else {
56             String prefix = "080a121f08d108121a0a06";
57             String encodedPin = decodeMessage(pin);
58             String suffix = "121036646564646461326639366635646261";
59             return prefix + encodedPin + suffix;
60         }
61     }
62
63     public static String loginRequest() {
64         String message = "0801121a0801121073616d73756e6720534d2d4739393855180128fbff04";
65         return message;
66     }
67
68     public static String keepAlive() {
69         String message = "080028fae0a6c0d130";
70         return message;
71     }
72
73     private static String fixMessage(String tempMsg) {
74         if (tempMsg.length() % 2 > 0) {
75             tempMsg = "0" + tempMsg;
76         }
77         return tempMsg;
78     }
79
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);
86         return reply;
87     }
88     // 080b120308cd08 - Longer Hostname Reply
89     // 08f30712020805 - Unknown
90     // 08f10712020800 - Get all apps
91     // 08ec0712020806 - Get current app
92
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;
101         return reply;
102     }
103 }