]> git.basschouten.com Git - openhab-addons.git/blob
b63032c6e3ccce8e0eee799af8216b8a2f295195
[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.googletv;
14
15 import static org.openhab.binding.androidtv.internal.protocol.googletv.GoogleTVConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Class responsible for parsing incoming GoogleTV messages. Calls back to an object implementing the
23  * GoogleTVMessageParserCallbacks interface.
24  *
25  * Adapted from Lutron Leap binding
26  *
27  * @author Ben Rosenblum - Initial contribution
28  */
29
30 @NonNullByDefault
31 public class GoogleTVMessageParser {
32     private final Logger logger = LoggerFactory.getLogger(GoogleTVMessageParser.class);
33
34     private final GoogleTVConnectionManager callback;
35
36     public GoogleTVMessageParser(GoogleTVConnectionManager callback) {
37         this.callback = callback;
38     }
39
40     public void handleMessage(String msg) {
41         if (msg.trim().isEmpty()) {
42             return; // Ignore empty lines
43         }
44
45         String thingId = callback.getThingID();
46         char[] charArray = msg.toCharArray();
47         String lenString = "" + charArray[0] + charArray[1];
48         int len = Integer.parseInt(lenString, 16);
49         msg = msg.substring(2);
50         charArray = msg.toCharArray();
51
52         logger.trace("{} - Received GoogleTV message - Length: {} Message: {}", thingId, len, msg);
53
54         callback.validMessageReceived();
55
56         try {
57             if (msg.startsWith(DELIMITER_1A)) {
58                 logger.warn("{} - GoogleTV Error Message: {}", thingId, msg);
59                 callback.getHandler().dispose();
60             } else if (msg.startsWith(DELIMITER_0A)) {
61                 // First message on connection from GTV
62                 //
63                 // 0a 5b08 ff 041256 0a 11 534849454c4420416e64726f6964205456 12 06 4e5649444941 18 01 22 02 3131 2a
64                 // ---------------------LEN-SHIELD Android TV--------------------LEN-NVIDIA---------LEN---LEN-Android
65                 // 24 636f6d2e676f6f676c652e616e64726f69642e74762e72656d6f74652e73657276696365 32
66                 // LEN-com.google.android.tv.remote.service
67                 // 0d 352e322e343733323534313333
68                 // LEN-5.2.473254133
69                 //
70                 // 0a 5308 ff 04124e 0a 0c 42524156494120344b204742 12 04 536f6e79 18 01 22 01 39 2a
71                 // ---------------------LEN-BRAVIA 4K GB---------------LEN-Sony-------LEN---LEN-Android Version
72                 // 24 636f6d2e676f6f676c652e616e64726f69642e74762e72656d6f74652e73657276696365 32
73                 // 0d 352e322e343733323534313333
74                 //
75                 // 0a 5408 ff 04124f 0a 0a 4368726f6d6563617374 12 06 476f6f676c65 18 01 22 02 3132 2a
76                 // ---------------------LEN-Chromecast-------------LEN-Google---------LEN---LEN-Android Version
77                 // 24 636f6d2e676f6f676c652e616e64726f69642e74762e72656d6f74652e73657276696365 32
78                 // 0d 352e322e343733323534313333
79                 //
80                 // 0a 5708 ff 041252 0a 0d 4368726f6d6563617374204844 12 06 476f6f676c65 18 01 22 02 3132 2a
81                 // ---------------------LEN-Chromecast HD----------------LEN-Google---------LEN---LEN-Android Version
82                 // 24 636f6d2e676f6f676c652e616e64726f69642e74762e72656d6f74652e73657276696365 32
83                 // 0d352e322e343733323534313333
84
85                 if (callback.getLoggedIn()) {
86                     logger.warn("{} - Unexpected Login Message: {}", thingId, msg);
87                 } else {
88                     callback.sendCommand(
89                             new GoogleTVCommand(GoogleTVRequest.encodeMessage(GoogleTVRequest.loginRequest(4))));
90                 }
91
92                 String st = "";
93                 int length = 0;
94                 StringBuilder preambleSb = new StringBuilder();
95                 StringBuilder manufacturerSb = new StringBuilder();
96                 StringBuilder modelSb = new StringBuilder();
97                 StringBuilder androidVersionSb = new StringBuilder();
98                 StringBuilder remoteServerSb = new StringBuilder();
99                 StringBuilder remoteServerVersionSb = new StringBuilder();
100
101                 int i = 0;
102                 int current = 0;
103
104                 for (; i < 14; i++) {
105                     preambleSb.append(charArray[i]);
106                 }
107
108                 i += 2; // 0a delimiter
109
110                 st = "" + charArray[i] + charArray[i + 1];
111                 length = Integer.parseInt(st, 16) * 2;
112                 i += 2;
113                 current = i;
114
115                 for (; i < current + length; i++) {
116                     modelSb.append(charArray[i]);
117                 }
118
119                 i += 2; // 12 delimiter
120
121                 st = "" + charArray[i] + charArray[i + 1];
122                 length = Integer.parseInt(st, 16) * 2;
123                 i += 2;
124                 current = i;
125
126                 for (; i < current + length; i++) {
127                     manufacturerSb.append(charArray[i]);
128                 }
129
130                 i += 6; // 18 01 22
131
132                 st = "" + charArray[i] + charArray[i + 1];
133                 length = Integer.parseInt(st, 16) * 2;
134                 i += 2;
135                 current = i;
136
137                 for (; i < current + length; i++) {
138                     androidVersionSb.append(charArray[i]);
139                 }
140
141                 i += 2; // 2a delimiter
142
143                 st = "" + charArray[i] + charArray[i + 1];
144                 length = Integer.parseInt(st, 16) * 2;
145                 i += 2;
146                 current = i;
147
148                 for (; i < current + length; i++) {
149                     remoteServerSb.append(charArray[i]);
150                 }
151
152                 i += 2; // 32 delimiter
153
154                 st = "" + charArray[i] + charArray[i + 1];
155                 length = Integer.parseInt(st, 16) * 2;
156                 i += 2;
157                 current = i;
158
159                 for (; i < current + length; i++) {
160                     remoteServerVersionSb.append(charArray[i]);
161                 }
162
163                 String preamble = preambleSb.toString();
164                 String model = GoogleTVRequest.encodeMessage(modelSb.toString());
165                 String manufacturer = GoogleTVRequest.encodeMessage(manufacturerSb.toString());
166                 String androidVersion = GoogleTVRequest.encodeMessage(androidVersionSb.toString());
167                 String remoteServer = GoogleTVRequest.encodeMessage(remoteServerSb.toString());
168                 String remoteServerVersion = GoogleTVRequest.encodeMessage(remoteServerVersionSb.toString());
169
170                 logger.debug("{} - {} \"{}\" \"{}\" {} {} {}", thingId, preamble, model, manufacturer, androidVersion,
171                         remoteServer, remoteServerVersion);
172
173                 callback.setModel(model);
174                 callback.setManufacturer(manufacturer);
175                 callback.setAndroidVersion(androidVersion);
176                 callback.setRemoteServer(remoteServer);
177                 callback.setRemoteServerVersion(remoteServerVersion);
178
179             } else if (msg.startsWith(DELIMITER_12)) {
180                 // Second message on connection from GTV
181                 // Login successful
182                 callback.sendCommand(
183                         new GoogleTVCommand(GoogleTVRequest.encodeMessage(GoogleTVRequest.loginRequest(5))));
184                 logger.info("{} - Login Successful", thingId);
185                 callback.setLoggedIn(true);
186             } else if (msg.startsWith(DELIMITER_92)) {
187                 // Third message on connection from GTV
188                 // Also sent on power state change (to ON only unless keypress triggers)i
189                 // 9203 21 08 02 10 02 1a 11 534849454c4420416e64726f6964205456 20 02 2800 30 0f 38 0e 40 00
190                 // --------DD----DD----DD-LEN-SHIELD Android TV
191                 // 9203 1e 08 9610 10 09 1a 0d 4368726f6d6563617374204844 20 02 2800 30 19 38 0a 40 00
192                 // --------DD------DD----DD-LEN-Chromecast HD
193                 // 9203 1a 08 f304 10 09 1a 11 534849454c4420416e64726f6964205456 20 01
194                 // 9203 1a 08 8205 10 09 1a 11 534849454c4420416e64726f6964205456 20 01
195                 // --------DD------DD----DD-LEN-SHIELD Android TV
196                 //
197                 // VOLUME:
198                 // ---------------DD----DD----DD-LEN-BRAVIA 4K GB------------DD---------DD-MAX---VOL---MUTE
199                 // 00 --- 9203 1c 08 03 10 06 1a 0c 42524156494120344b204742 20 02 2800 30 64 38 00 40 00
200                 // 01 --- 9203 1c 08 03 10 06 1a 0c 42524156494120344b204742 20 02 2800 30 64 38 01 40 00
201                 // 100 -- 9203 1c 08 03 10 06 1a 0c 42524156494120344b204742 20 02 2800 30 64 38 64 40 00
202                 // MUTE - 9203 1c 08 03 10 06 1a 0c 42524156494120344b204742 20 02 2800 30 64 38 00 40 01
203
204                 String st = "";
205                 int length = 0;
206
207                 StringBuilder preambleSb = new StringBuilder();
208                 StringBuilder modelSb = new StringBuilder();
209                 String volMax = "";
210                 String volCurr = "";
211                 String volMute = "";
212                 String audioMode = "";
213
214                 int i = 0;
215                 int current = 0;
216
217                 for (; i < 12; i++) {
218                     preambleSb.append(charArray[i]);
219                 }
220
221                 st = "" + charArray[i] + charArray[i + 1];
222                 do {
223                     if (!DELIMITER_1A.equals(st)) {
224                         preambleSb.append(st);
225                         i += 2;
226                         st = "" + charArray[i] + charArray[i + 1];
227                     }
228                 } while (!DELIMITER_1A.equals(st));
229
230                 i += 2; // 1a delimiter
231
232                 st = "" + charArray[i] + charArray[i + 1];
233                 length = Integer.parseInt(st, 16) * 2;
234                 i += 2;
235                 current = i;
236
237                 for (; i < current + length; i++) {
238                     modelSb.append(charArray[i]);
239                 }
240
241                 i += 2; // 20 delimiter
242
243                 st = "" + charArray[i] + charArray[i + 1];
244
245                 audioMode = st; // 01 remote audio - 02 local audio
246
247                 if (DELIMITER_02.equals(st)) {
248                     i += 2; // 02 longer message
249                     i += 4; // Unknown 2800 message
250                     i += 2; // 30 delimiter
251                     volMax = "" + charArray[i] + charArray[i + 1];
252                     i += 4; // volMax + 38 delimiter
253                     volCurr = "" + charArray[i] + charArray[i + 1];
254                     i += 4; // volCurr + 40 delimiter
255                     volMute = "" + charArray[i] + charArray[i + 1];
256
257                     callback.setVolMax(volMax);
258                     callback.setVolCurr(volCurr);
259                     callback.setVolMute(volMute);
260                 }
261
262                 String preamble = preambleSb.toString();
263                 String model = GoogleTVRequest.encodeMessage(modelSb.toString());
264                 logger.debug("{} - Device Update: {} \"{}\" {} {} {} {}", thingId, preamble, model, audioMode, volMax,
265                         volCurr, volMute);
266                 callback.setAudioMode(audioMode);
267
268             } else if (msg.startsWith(DELIMITER_08)) {
269                 // PIN Process Messages. Only used on 6467.
270                 if (msg.startsWith(MESSAGE_PINSUCCESS)) {
271                     // PIN Process Successful
272                     logger.debug("{} - PIN Process Successful!", thingId);
273                     callback.finishPinProcess();
274                 } else {
275                     // 080210c801a201081204080310061801
276                     // 080210c801fa0100
277                     logger.debug("{} - PIN Intermediary Message: {}", thingId, msg);
278                 }
279             } else if (msg.startsWith(DELIMITER_C2)) {
280                 // Power State
281                 // c202020800 - OFF
282                 // c202020801 - ON
283                 if (MESSAGE_POWEROFF.equals(msg)) {
284                     callback.setPower(false);
285                 } else if (MESSAGE_POWERON.equals(msg)) {
286                     callback.setPower(true);
287                 } else {
288                     logger.info("{} - Unknown power state received. {}", thingId, msg);
289                 }
290             } else if (msg.startsWith(DELIMITER_42)) {
291                 // Keepalive request
292                 callback.sendKeepAlive(msg);
293             } else if (msg.startsWith(DELIMITER_A2)) {
294                 // Current app name. Sent on keypress and power change.
295                 // a201 21 0a 1f 62 1d 636f6d2e676f6f676c652e616e64726f69642e796f75747562652e7476
296                 // -----------------LEN-com.google.android.youtube.tv
297                 // a201 21 0a 1f 62 1d 636f6d2e676f6f676c652e616e64726f69642e74766c61756e63686572
298                 // -----------------LEN-com.google.android.tvlauncher
299                 // a201 14 0a 12 62 10 636f6d2e736f6e792e6474762e747678
300                 // -----------------LEN-com.sony.dtv.tvx
301                 // a201 15 0a 13 62 11 636f6d2e6e6574666c69782e6e696e6a61
302                 // -----------------LEN-com.netflix.ninja
303
304                 StringBuilder preambleSb = new StringBuilder();
305                 StringBuilder appNameSb = new StringBuilder();
306                 int i = 0;
307                 int current = 0;
308
309                 for (; i < 10; i++) {
310                     preambleSb.append(charArray[i]);
311                 }
312
313                 i += 2; // 62 delimiter
314
315                 String st = "" + charArray[i] + charArray[i + 1];
316                 int length = Integer.parseInt(st, 16) * 2;
317                 i += 2;
318                 current = i;
319
320                 for (; i < current + length; i++) {
321                     appNameSb.append(charArray[i]);
322                 }
323
324                 String preamble = preambleSb.toString();
325                 String appName = GoogleTVRequest.encodeMessage(appNameSb.toString());
326
327                 logger.debug("{} - Current App: {} {}", thingId, preamble, appName);
328                 callback.setCurrentApp(appName);
329             } else {
330                 logger.info("{} - Unknown payload received. {} {}", thingId, len, msg);
331             }
332         } catch (Exception e) {
333             logger.warn("{} - Message Parser Exception on {}", thingId, msg);
334             logger.warn("{} - Message Parser Caught Exception", thingId, e);
335         }
336     }
337 }