forked from Greg-MM/C4-AndroidTV
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver.lua
617 lines (507 loc) · 24.1 KB
/
driver.lua
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
require "debugging"
require "keycodes"
require "helpers"
require "protobuf"
require "pairing"
local DriverVersion = "1.0.4"
EventID_CurrentAppChanged = 1
EX_CMD = {}
LUA_ACTION = {}
function OnDriverInit(driverInitType)
for k,v in pairs(Properties) do
OnPropertyChanged(k)
end
C4:AddVariable("CURRENT_APP", "", "STRING")
local RoomID = C4:RoomGetId()
C4:RegisterVariableListener(RoomID, 1000) -- Current_Selected_Device
if (driverInitType == "DIT_ADDING") then
-- Initialization needed only when the driver is added to a project
elseif (driverInitType == "DIT_STARTUP") then
-- Initialization needed only during initial startup
elseif (driverInitType == "DIT_UPDATING") then
-- Initialization needed only after a driver update
end
end
function ExecuteCommand(strCommand, tQueryParams)
DebugHeader("ExecuteCommand(" .. strCommand .. ")")
Debug(tQueryParams)
local trimmedCommand = string.gsub(strCommand, " ", "")
if (EX_CMD[strCommand] ~= nil and type(EX_CMD[strCommand]) == "function") then
EX_CMD[strCommand](tQueryParams)
elseif (EX_CMD[trimmedCommand] ~= nil and type(EX_CMD[trimmedCommand]) == "function") then
EX_CMD[trimmedCommand](tQueryParams)
elseif (EX_CMD[strCommand] ~= nil) then -- handle the command
QueueCommand(EX_CMD[strCommand])
else
Debug("ExecuteCommand: Unhandled Command = " .. strCommand)
end
end
function EX_CMD.LUA_ACTION(tQueryParams)
if tQueryParams ~= nil then
for cmd,cmdv in pairs(tQueryParams) do
if cmd == "ACTION" then
if (LUA_ACTION[cmdv] ~= nil) then
LUA_ACTION[cmdv](tQueryParams)
else
Debug("Undefined Action")
Debug("Key: " .. cmd .. " Value: " .. cmdv)
end
else
Debug("Undefined Command")
Debug("Key: " .. cmd .. " Value: " .. cmdv)
end
end
else
Debug("LUA_ACTION: tQueryParams IS NULL!")
end
end
function OnVariableChanged(strName)
Debug("OnVariableChanged: " .. strName)
end
function OnPropertyChanged(strProperty)
Debug("OnPropertyChanged: ".. strProperty);
if (strProperty == "Debug Mode") then
Debug("Debug Mode Set To: " .. Properties[strProperty])
if (Properties[strProperty] == "OFF") then
DebugPrint = false
DebugTimerID = 0
elseif (Properties[strProperty] == "ON") then
DebugPrint = true
StartDebugTimer()
end
end
end
function OnBindingChanged (idBinding, strClass, bIsBound, otherDeviceID, otherBindingID)
DebugHeader("OnBindingChanged(" .. idBinding .. ", " .. strClass .. ", " .. tostring(bIsBound) .. ", " .. otherDeviceID .. ", " .. otherBindingID .. ")")
end
function OnTimerExpired(TimerID)
DebugHeader("OnTimerExpired: " .. TimerID)
if (TimerID == DebugTimerID) then
Debug("Turning Debug Mode OFF")
C4:UpdateProperty("Debug Mode", "OFF")
DebugTimerID = C4:KillTimer(TimerID)
elseif(TimerID == PingTimerID) then
--Debug("Ping Timer")
ExecutePingTimer()
else
Debug("Unknown Timer: " .. TimerID)
C4:KillTimer(TimerID)
end
end
function GetVariableName(DeviceID, VariableID)
for k,v in pairs(C4:GetDeviceVariables(DeviceID)) do
if(k == VariableID) then
return v.name
end
end
return ""
end
function OnWatchedVariableChanged(DeviceID, VariableID, strValue)
DebugHeader("OnWatchedVariableChanged")
Debug("• Device: " .. DeviceID)
Debug("• Variable ID: " .. VariableID)
if(DebugPrint) then -- do not call GetVariableName if not showing (not the fastest function)
Debug("• Variable Name: " .. GetVariableName(tonumber(DeviceID), VariableID))
end
Debug("• Value: " .. strValue)
DebugDivider("-")
local SwitchedToDeviceID = tonumber(strValue)
if(VariableID == 1000) then
if(SwitchedToDeviceID == C4:GetDeviceID() or SwitchedToDeviceID == C4:GetProxyDevices()) then
Debug("Switched To Android TV Device, DO NOTHING")
else
if (Properties["On Room OFF"] == "Pause") then
Debug("Switched AWAY from Android TV Device, PAUSE")
SendKey(tonumber(Properties["PAUSE_ROOM_OFF Mapping"]))
else
Debug("Switched AWAY from Android TV Device, PAUSE NOT ENABLED, DO NOTHING")
end
end
end
end
function ReceivedFromProxy(BindingID, strCommand, tParams)
DebugHeader("ReceivedFromProxy (" .. BindingID .. "): " .. strCommand)
if (tParams ~= nil) then Debug(tParams) end
if(Properties["Connection"] == "OFFLINE") then
Debug("Connection OFFLINE, Attempting To Connect...")
LUA_ACTION.ConnectToCommand(nil)
end
if (BindingID == 5001) then
if (strCommand == "ON") then
if (Properties["Send WOL on ON"] == "True") then
SendMagicPacket()
end
if (Properties["Power Status"] == "OFF") then
SendKey(tonumber(Properties["POWER Mapping"]))
end
elseif (strCommand == "OFF") then
if (Properties["On Room OFF"] == "Pause") then
SendKey(tonumber(Properties["PAUSE_ROOM_OFF Mapping"]))
elseif (Properties["On Room OFF"] == "Turn OFF") then
if (Properties["Power Status"] == "ON") then
SendKey(tonumber(Properties["POWER Mapping"]))
end
end
else
if (ProcessInputCommand(strCommand, tParams) == false) then
DebugDivider("!")
Debug("COMMAND UNKNOWN: "..strCommand)
DebugDivider("!")
end
end
elseif (BindingID==5002) then -- Mini Driver Commands
--TODO: Implement Mini Drivers
DebugDivider("!")
Debug("COMMAND UNKNOWN (Mini Driver Switch): "..strCommand)
DebugDivider("!")
end
end
function ProcessInputCommand(CMD, tParams)
if (CMD == "GUIDE" or CMD == "START_GUIDE" or CMD == "PULSE_GUIDE" or CMD == "STOP_GUIDE" or CMD == "END_GUIDE") then
return ProcessInputCommandKey(tonumber(Properties["GUIDE Mapping"]), CMD, tParams)
elseif (C == "PAGE_UP" or C == "START_PAGE_UP" or C == "PULSE_PAGE_UP" or C == "STOP_PAGE_UP" or C == "END_PAGE_UP") then
return ProcessInputCommandKey(tonumber(Properties["PAGE UP Mapping"]), CMD, tParams)
elseif (CMD == "PAGE_DOWN" or CMD == "START_PAGE_DOWN" or CMD == "PULSE_PAGE_DOWN" or CMD == "STOP_PAGE_DOWN" or CMD == "END_PAGE_DOWN") then
return ProcessInputCommandKey(tonumber(Properties["PAGE DOWN Mapping"]), CMD, tParams)
elseif (CMD == "UP" or CMD == "START_UP" or CMD == "PULSE_UP" or CMD == "STOP_UP" or CMD == "END_UP") then
return ProcessInputCommandKey(tonumber(Properties["UP Mapping"]), CMD, tParams)
elseif (CMD == "DOWN" or CMD == "START_DOWN" or CMD == "PULSE_DOWN" or CMD == "STOP_DOWN" or CMD == "END_DOWN") then
return ProcessInputCommandKey(tonumber(Properties["DOWN Mapping"]), CMD, tParams)
elseif (CMD == "LEFT" or CMD == "START_LEFT" or CMD == "PULSE_LEFT" or CMD == "STOP_LEFT" or CMD == "END_LEFT") then
return ProcessInputCommandKey(tonumber(Properties["LEFT Mapping"]), CMD, tParams)
elseif (CMD == "RIGHT" or CMD == "START_RIGHT" or CMD == "PULSE_RIGHT" or CMD == "STOP_RIGHT" or CMD == "END_RIGHT") then
return ProcessInputCommandKey(tonumber(Properties["RIGHT Mapping"]), CMD, tParams)
elseif (CMD == "ENTER" or CMD == "START_ENTER" or CMD == "PULSE_ENTER" or CMD == "STOP_ENTER" or CMD == "END_ENTER") then
return ProcessInputCommandKey(tonumber(Properties["ENTER Mapping"]), CMD, tParams)
elseif (CMD == "RECALL" or CMD == "START_RECALL" or CMD == "PULSE_RECALL" or CMD == "STOP_RECALL" or CMD == "END_RECALL") then
return ProcessInputCommandKey(tonumber(Properties["PREVIOUS CHANNEL Mapping"]), CMD, tParams)
elseif (CMD == "CH_UP" or CMD == "START_CH_UP" or CMD == "PULSE_CH_UP" or CMD == "STOP_CH_UP" or CMD == "END_CH_UP") then
return ProcessInputCommandKey(tonumber(Properties["CHANNEL UP Mapping"]), CMD, tParams)
elseif (CMD == "CH_DOWN" or CMD == "START_CH_DOWN" or CMD == "PULSE_CH_DOWN" or CMD == "STOP_CH_DOWN" or CMD == "END_CH_DOWN") then
return ProcessInputCommandKey(tonumber(Properties["CHANNEL DOWN Mapping"]), CMD, tParams)
elseif (CMD == "INFO" or CMD == "START_INFO" or CMD == "PULSE_INFO" or CMD == "STOP_INFO" or CMD == "END_INFO") then
return ProcessInputCommandKey(tonumber(Properties["INFO Mapping"]), CMD, tParams)
elseif (CMD == "MENU" or CMD == "START_MENU" or CMD == "PULSE_MENU" or CMD == "STOP_MENU" or CMD == "END_MENU") then
return ProcessInputCommandKey(tonumber(Properties["MENU Mapping"]), CMD, tParams)
elseif (CMD == "CANCEL" or CMD == "START_CANCEL" or CMD == "PULSE_CANCEL" or CMD == "STOP_CANCEL" or CMD == "END_CANCEL") then
return ProcessInputCommandKey(tonumber(Properties["CANCEL Mapping"]), CMD, tParams)
elseif (CMD == "DVR" or CMD == "START_DVR" or CMD == "PULSE_DVR" or CMD == "STOP_DVR" or CMD == "END_DVR") then
return ProcessInputCommandKey(tonumber(Properties["DVR Mapping"]), CMD, tParams)
elseif (CMD == "SCAN_REV" or CMD == "START_SCAN_REV" or CMD == "PULSE_SCAN_REV" or CMD == "STOP_SCAN_REV" or CMD == "END_SCAN_REV") then
return ProcessInputCommandKey(tonumber(Properties["REWIND Mapping"]), CMD, tParams)
elseif (CMD == "SCAN_FWD" or CMD == "START_SCAN_FWD" or CMD == "PULSE_SCAN_FWD" or CMD == "STOP_SCAN_FWD" or CMD == "END_SCAN_FWD") then
return ProcessInputCommandKey(tonumber(Properties["FAST FORWARD Mapping"]), CMD, tParams)
elseif (CMD == "SKIP_REV" or CMD == "START_SKIP_REV" or CMD == "PULSE_SKIP_REV" or CMD == "STOP_SKIP_REV" or CMD == "END_SKIP_REV") then
return ProcessInputCommandKey(tonumber(Properties["SKIP BACKWARD Mapping"]), CMD, tParams)
elseif (CMD == "SKIP_FWD" or CMD == "START_SKIP_FWD" or CMD == "PULSE_SKIP_FWD" or CMD == "STOP_SKIP_FWD" or CMD == "END_SKIP_FWD") then
return ProcessInputCommandKey(tonumber(Properties["SKIP FORWARD Mapping"]), CMD, tParams)
elseif (CMD == "PLAY" or CMD == "START_PLAY" or CMD == "PULSE_PLAY" or CMD == "STOP_PLAY" or CMD == "END_PLAY") then
return ProcessInputCommandKey(tonumber(Properties["PLAY Mapping"]), CMD, tParams)
elseif (CMD == "PAUSE" or CMD == "START_PAUSE" or CMD == "PULSE_PAUSE" or CMD == "STOP_PAUSE" or CMD == "END_PAUSE") then
return ProcessInputCommandKey(tonumber(Properties["PAUSE Mapping"]), CMD, tParams)
elseif (CMD == "RECORD" or CMD == "START_RECORD" or CMD == "PULSE_RECORD" or CMD == "STOP_RECORD" or CMD == "END_RECORD") then
return ProcessInputCommandKey(tonumber(Properties["RECORD Mapping"]), CMD, tParams)
elseif (CMD == "STOP" or CMD == "START_STOP" or CMD == "PULSE_STOP" or CMD == "STOP_STOP" or CMD == "END_STOP") then
return ProcessInputCommandKey(tonumber(Properties["STOP Mapping"]), CMD, tParams)
elseif (CMD == "PROGRAM_A" or CMD == "START_PROGRAM_A" or CMD == "PULSE_PROGRAM_A" or CMD == "STOP_PROGRAM_A" or CMD == "END_PROGRAM_A") then
return ProcessInputCommandKey(tonumber(Properties["RED Mapping"]), CMD, tParams)
elseif (CMD == "PROGRAM_B" or CMD == "START_PROGRAM_B" or CMD == "PULSE_PROGRAM_B" or CMD == "STOP_PROGRAM_B" or CMD == "END_PROGRAM_B") then
return ProcessInputCommandKey(tonumber(Properties["GREEN Mapping"]), CMD, tParams)
elseif (CMD == "PROGRAM_C" or CMD == "START_PROGRAM_C" or CMD == "PULSE_PROGRAM_C" or CMD == "STOP_PROGRAM_C" or CMD == "END_PROGRAM_C") then
return ProcessInputCommandKey(tonumber(Properties["YELLOW Mapping"]), CMD, tParams)
elseif (CMD == "PROGRAM_D" or CMD == "START_PROGRAM_D" or CMD == "PULSE_PROGRAM_D" or CMD == "STOP_PROGRAM_D" or CMD == "END_PROGRAM_D") then
return ProcessInputCommandKey(tonumber(Properties["BLUE Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_1" or CMD == "START_NUMBER_1" or CMD == "PULSE_NUMBER_1" or CMD == "STOP_NUMBER_1" or CMD == "END_NUMBER_1") then
return ProcessInputCommandKey(tonumber(Properties["1 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_2" or CMD == "START_NUMBER_2" or CMD == "PULSE_NUMBER_2" or CMD == "STOP_NUMBER_2" or CMD == "END_NUMBER_2") then
return ProcessInputCommandKey(tonumber(Properties["2 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_3" or CMD == "START_NUMBER_3" or CMD == "PULSE_NUMBER_3" or CMD == "STOP_NUMBER_3" or CMD == "END_NUMBER_3") then
return ProcessInputCommandKey(tonumber(Properties["3 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_4" or CMD == "START_NUMBER_4" or CMD == "PULSE_NUMBER_4" or CMD == "STOP_NUMBER_4" or CMD == "END_NUMBER_4") then
return ProcessInputCommandKey(tonumber(Properties["4 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_5" or CMD == "START_NUMBER_5" or CMD == "PULSE_NUMBER_5" or CMD == "STOP_NUMBER_5" or CMD == "END_NUMBER_5") then
return ProcessInputCommandKey(tonumber(Properties["5 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_6" or CMD == "START_NUMBER_6" or CMD == "PULSE_NUMBER_6" or CMD == "STOP_NUMBER_6" or CMD == "END_NUMBER_6") then
return ProcessInputCommandKey(tonumber(Properties["6 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_7" or CMD == "START_NUMBER_7" or CMD == "PULSE_NUMBER_7" or CMD == "STOP_NUMBER_7" or CMD == "END_NUMBER_7") then
return ProcessInputCommandKey(tonumber(Properties["7 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_8" or CMD == "START_NUMBER_8" or CMD == "PULSE_NUMBER_8" or CMD == "STOP_NUMBER_8" or CMD == "END_NUMBER_8") then
return ProcessInputCommandKey(tonumber(Properties["8 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_9" or CMD == "START_NUMBER_9" or CMD == "PULSE_NUMBER_9" or CMD == "STOP_NUMBER_9" or CMD == "END_NUMBER_9") then
return ProcessInputCommandKey(tonumber(Properties["9 Mapping"]), CMD, tParams)
elseif (CMD == "NUMBER_0" or CMD == "START_NUMBER_0" or CMD == "PULSE_NUMBER_0" or CMD == "STOP_NUMBER_0" or CMD == "END_NUMBER_0") then
return ProcessInputCommandKey(tonumber(Properties["0 Mapping"]), CMD, tParams)
elseif (CMD == "STAR" or CMD == "START_STAR" or CMD == "PULSE_STAR" or CMD == "STOP_STAR" or CMD == "END_STAR") then
return ProcessInputCommandKey(tonumber(Properties["* Mapping"]), CMD, tParams)
elseif (CMD == "POUND" or CMD == "START_POUND" or CMD == "PULSE_POUND" or CMD == "STOP_POUND" or CMD == "END_POUND") then
return ProcessInputCommandKey(tonumber(Properties["# Mapping"]), CMD, tParams)
elseif (CMD == "TV_VIDEO" or CMD == "START_TV_VIDEO" or CMD == "PULSE_TV_VIDEO" or CMD == "STOP_TV_VIDEO" or CMD == "END_TV_VIDEO") then
return ProcessInputCommandKey(tonumber(Properties["TV_VIDEO Mapping"]), CMD, tParams)
elseif (CMD == "CLOSED_CAPTIONED" or CMD == "START_CLOSED_CAPTIONED" or CMD == "PULSE_CLOSED_CAPTIONED" or CMD == "STOP_CLOSED_CAPTIONED" or CMD == "END_CLOSED_CAPTIONED") then
return ProcessInputCommandKey(tonumber(Properties["CLOSED_CAPTIONED Mapping"]), CMD, tParams)
elseif (CMD == "CUSTOM_1" or CMD == "START_CUSTOM_1" or CMD == "PULSE_CUSTOM_1" or CMD == "STOP_CUSTOM_1" or CMD == "END_CUSTOM_1") then
return ProcessInputCommandKey(tonumber(Properties["CUSTOM_1 Mapping"]), CMD, tParams)
elseif (CMD == "CUSTOM_2" or CMD == "START_CUSTOM_2" or CMD == "PULSE_CUSTOM_2" or CMD == "STOP_CUSTOM_2" or CMD == "END_CUSTOM_2") then
return ProcessInputCommandKey(tonumber(Properties["CUSTOM_2 Mapping"]), CMD, tParams)
elseif (CMD == "CUSTOM_3" or CMD == "START_CUSTOM_3" or CMD == "PULSE_CUSTOM_3" or CMD == "STOP_CUSTOM_3" or CMD == "END_CUSTOM_3") then
return ProcessInputCommandKey(tonumber(Properties["CUSTOM_3 Mapping"]), CMD, tParams)
end
return false
end
function ProcessInputCommandKey(KeyCode, CMD, tParams)
HasBegin = false
Debug("ProcessInputCommandKey(" .. KeyCode .. ", " .. CMD .. ")")
if(tParams ~= nil and tParams.BEGIN ~= nil) then HasBegin = true end
if(KeyCode == 0) then
Debug("NO KEY MAPPING DEFINED")
return
end
if(KeyCode >= 501 and KeyCode <= 520) then
--Launch App
local URL = Properties["Launch App URL " .. (KeyCode - 500)]
if(URL ~= "") then
SendURL(URL)
end
return
elseif(KeyCode >= 601 and KeyCode <= 620) then
--Fire Event
C4:FireEventByID(KeyCode)
return
elseif(KeyCode >= 1000 and KeyCode < 2000) then
--Long Press
local LongPressKeyCode = (KeyCode - 1000)
local LongPressTime = tonumber(Properties["LONG_PRESS_TIME"])
if(HasBegin or CMD:find("START_")) then return end
Debug("LONG PRESS: " .. LongPressKeyCode)
SendKey_PRESS(LongPressKeyCode)
C4:SetTimer(LongPressTime, function(timer)
Debug("LONG PRESS RELEASED: " .. LongPressKeyCode)
SendKey_RELEASE(LongPressKeyCode)
end)
return
end
if(HasBegin) then
Debug("Input Command HAS A Begin Param.")
if(CMD:find("PULSE_")) then
SendKey(KeyCode)
else
SendKey_PRESS(KeyCode)
end
else
Debug("Input Command DOES NOT HAVE Begin Param.")
if(CMD:find("STOP_") or CMD:find("END_")) then
SendKey_RELEASE(KeyCode)
elseif(CMD:find("START_")) then
SendKey_PRESS(KeyCode)
else
SendKey(KeyCode)
end
end
return true
end
function OnConnectionStatusChanged(BindingID, nPort, strStatus)
print("Connection Status Changed: " .. strStatus)
if (strStatus == "ONLINE") then
if(nPort == 6467) then
Pairing_SendRequest()
end
local MAC = C4:GetDeviceMAC(6001)
C4:UpdateProperty("MAC Address", MAC)
end
if(nPort == 6466) then
C4:UpdateProperty("Connection", strStatus)
end
end
local MessageBuffer = ""
function ReceivedFromNetwork(BindingID, nPort, strData)
MessageBuffer = MessageBuffer .. strData
local MessageSize = MessageBuffer:byte(1)
if(#MessageBuffer > MessageSize) then -- Check Buffer For Completed Message
local Message = MessageBuffer:sub(2, MessageSize+1)
ProcessNetworkMessage(Message)
MessageBuffer = MessageBuffer:sub(MessageSize+2)
end
end
PingRequestCount = 0
PingTimerID = 0
function StartPingTimer()
if (PingTimerID ~= 0) then
PingTimerID = C4:KillTimer(PingTimerID)
end
PingTimerID = C4:AddTimer(60, "SECONDS", true)
end
function ExecutePingTimer()
--Debug("Ping Request Count: " .. PingRequestCount)
if(string.len(Properties["Device Public Key Modulus"]) > 0 and PingRequestCount == 0) then
Debug("Communication Failed, attempting to connect...")
LUA_ACTION.DisconnectFromCommand(nil)
LUA_ACTION.ConnectToCommand(nil)
end
PingRequestCount = 0
end
function ProcessNetworkMessage(Message)
local WireMessage = DecodeWire(Message, 1)
local FingerPrint = GetWireMessageFingerPrint(WireMessage)
if(FingerPrint ~= "[F8][F1][WT0][F2][WT0]") then -- DO NOT DEBUG PING / PONG
DebugHeader("Processing Message (" .. #Message .. "):" .. GetHexString(Message))
DebugWireMessage(WireMessage)
Debug("Message FingerPrint: " .. FingerPrint)
end
if(FingerPrint == "[F8][F1][WT0][F2][WT0]") then -- Ping Request
PingRequestCount = PingRequestCount + 1
local PingRequestNumber = WireMessage[8][1].Value
SendWireCommand({[9]={[1]={ID=1, WireType=WireType_VarInt, Value=PingRequestNumber}}})
elseif(FingerPrint == "[F20][F1][F12][WT2]") then
Debug("CURRENT APP")
local CurrentApp = WireMessage[20][1][12].Value
Debug("Current App: " .. CurrentApp)
C4:UpdateProperty("Current App", CurrentApp)
C4:SetVariable("CURRENT_APP", CurrentApp)
C4:FireEventByID(EventID_CurrentAppChanged) -- CurrentAppChanged
elseif(FingerPrint == "[F1][F1][WT0][F2][F1][WT2][F2][WT2][F3][WT0][F4][WT2][F5][WT2][F6][WT2]") then
Debug("DEVICE DESCRIPTOR")
C4:UpdateProperty("Model Name", WireMessage[1][2][1].Value)
C4:UpdateProperty("Vendor Name", WireMessage[1][2][2].Value)
C4:UpdateProperty("Device Version", WireMessage[1][2][4].Value)
C4:UpdateProperty("Device Package Name", WireMessage[1][2][5].Value)
C4:UpdateProperty("Device App Version", WireMessage[1][2][6].Value)
SendDriverDescriptorPayload()
elseif(FingerPrint == "[F1][F1][WT0][F2][F1][WT2][F2][WT2][F3][WT0][F4][WT2][F5][WT2][F6][WT2][F3][WT0]") then
Debug("DEVICE DESCRIPTOR")
C4:UpdateProperty("Model Name", WireMessage[1][2][1].Value)
C4:UpdateProperty("Vendor Name", WireMessage[1][2][2].Value)
C4:UpdateProperty("Device Version", WireMessage[1][2][4].Value)
C4:UpdateProperty("Device Package Name", WireMessage[1][2][5].Value)
C4:UpdateProperty("Device App Version", WireMessage[1][2][6].Value)
SendDriverDescriptorPayload()
elseif(FingerPrint == "[F50][F1][WT0][F2][WT0][F3][WT2][F4][WT0]") then
Debug("PLAYER NAME & VOLUME LEVEL")
elseif(FingerPrint == "[F40][F1][WT0]") then
Debug("DEVICE STATUS")
if(WireMessage[40][1].Value == 1) then
C4:UpdateProperty("Power Status", "ON")
else
C4:UpdateProperty("Power Status", "OFF")
end
elseif(FingerPrint == "[F1][WT0][F2][WT0][F11][WT2]") then
Debug("1st PAIRING RESPONSE - IGNORE FOR NOW")
elseif(FingerPrint == "[F1][WT0][F2][WT0][F20][F2][WT2][F3][WT0]") then
Debug("2nd PAIRING RESPONSE - IGNORE FOR NOW")
elseif(FingerPrint == "[F1][WT0][F2][WT0][F31][WT2]") then
Debug("3rd PAIRING RESPONSE - IGNORE FOR NOW")
elseif(FingerPrint == "[F1][WT0][F2][WT0][F41][F1][F1765][WT2][F15][WT3][F191414][WT4][F188][WT4][F439][WT0][F14][WT0]") then
Debug("FINAL PAIRING RESPONSE - IGNORE FOR NOW")
elseif(FingerPrint == "[F1][WT0][F2][WT0][F41][F1][WT2]") then
Debug("FINAL PAIRING RESPONSE - IGNORE FOR NOW")
elseif(FingerPrint == "[F2][WT2]") then
Debug("OK? SEND SECOND CONFIGURATION MESSAGE")
--Second Configuration Message??
SendWireCommand({[2]={[1]={ID=1, WireType=WireType_VarInt, Value=622}}})
else
DebugDivider("#")
Debug("")
DebugHeader("UNHANDLED FINGERPRINT: " .. FingerPrint)
Debug("")
DebugDivider("#")
end
end
function SendWireCommand(WireMessage)
SendCommandWithSize(EncodeWire(WireMessage))
end
function SendCommandWithSize(Payload)
local WireMessage = DecodeWire(Payload, 1)
local FingerPrint = GetWireMessageFingerPrint(WireMessage)
if(FingerPrint ~= "[F9][F1][WT0]") then
DebugHeader("Sending Command (" .. #Payload .. ") HEX: " .. GetHexString(Payload))
DebugWireMessage(WireMessage)
end
C4:SendToNetwork(6001, 6466, string.char(#Payload));
C4:SendToNetwork(6001, 6466, Payload);
end
function SendKey(KeyCode) SendKeyType(KeyCode, KEY_SEND) end
function SendKey_PRESS(KeyCode) SendKeyType(KeyCode, KEY_PRESS) end
function SendKey_RELEASE(KeyCode) SendKeyType(KeyCode, KEY_RELEASE) end
function SendKey_PRESS_AND_RELEASE(KeyCode) SendKey_PRESS(KeyCode) SendKey_RELEASE(KeyCode) end
function SendKeyType(KeyCode, SendType)
Debug("Sending " .. KeyCode .. " (" .. SendType .. ")")
SendWireCommand({
[10]={
[1]={ID=1, WireType=WireType_VarInt, Value=KeyCode},
[2]={ID=2, WireType=WireType_VarInt, Value=SendType}
}
})
end
function SendURL(URL)
Debug("Sending URL (" .. URL .. ")")
SendWireCommand({
[90]={[1]={ID=1, WireType=WireType_String, Value=URL}}
})
end
function SendDriverDescriptorPayload()
Debug("Sending Driver Descriptor...")
SendWireCommand({
[1] = {
[1] = { ID = 1, WireType = 0, Value = 622},
[2] = {
[3] = { ID = 3, WireType = 0, Value = 1},
[6] = { ID = 6, Value = DriverVersion, WireType = 2},
[4] = { ID = 4, Value = "1", WireType = 2},
[5] = { ID = 5, Value = "androidtv.control4.gmsoftware", WireType = 2},
}
}
})
end
--[[ ACTIONS ]]--
function LUA_ACTION.BeginPair(tQueryParams)
BeginPair(tQueryParams)
end
function LUA_ACTION.FinishPair(tQueryParams)
FinishPair(tQueryParams)
end
function LUA_ACTION.ConnectToCommand(tQueryParams)
Debug("Connecting To Command Interface...")
C4:NetConnect(6001, 6466)
StartPingTimer()
end
function LUA_ACTION.DisconnectFromCommand(tQueryParams)
Debug("Disconnecting From Command Interface...")
C4:NetDisconnect(6001, 6466)
end
function LUA_ACTION.TestURL(tQueryParams)
local CommandURL = tQueryParams.URL;
print("Sending URL (" .. CommandURL .. ")...")
SendURL(CommandURL)
end
function LUA_ACTION.TestKeyCodeSend(tQueryParams)
print("Sending KeyCode (" .. tQueryParams.KeyCode .. ")...")
SendKey(tonumber(tQueryParams.KeyCode))
end
function LUA_ACTION.TestKeyCodePressThenRelease(tQueryParams)
print("Sending KeyCode Press Then Release (" .. tQueryParams.KeyCode .. ")...")
SendKey_PRESS_AND_RELEASE(tonumber(tQueryParams.KeyCode))
end
function LUA_ACTION.BackupConfiguration(tQueryParams)
BackupConfiguration()
end
function LUA_ACTION.SendWOL(tQueryParams)
print("Sending Magic Packet...")
SendMagicPacket()
end
function SendMagicPacket()
print("Sending Magic Packet...")
local MAC = Properties["MAC Address"]
if (MAC ~= "" and MAC ~= "?") then
local RawMAC = string.gsub(MAC, ":", "") -- replace : with "" in mac address
Debug("Sending Magic Packet to " .. RawMAC)
C4:SendWOL(RawMAC)
end
end
function GetPrivateKeyPassword(Binding, Port)
--Open Source, Key is not secret, might look into generating a self signed certificate in driver
return 'password'
end
C4:UpdateProperty("Driver Version", DriverVersion)
Debug("Driver Loaded...")
if(string.len(Properties["Device Public Key Modulus"]) > 0) then
LUA_ACTION.ConnectToCommand(nil)
end