-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommandFusion_iViewer_Server v4.5.usp
More file actions
436 lines (387 loc) · 11.8 KB
/
CommandFusion_iViewer_Server v4.5.usp
File metadata and controls
436 lines (387 loc) · 11.8 KB
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
/*
System Name: CommandFusion iViewer Server
Programmer: CommandFusion
Version: 4.5
Comments: www.commandfusion.com
*/
#HELP_BEGIN
Please refer to the 'CommandFusion iViewer Crestron Module' PDF packaged with the iViewer Crestron sample program.
The User Guide PDF can also be obtained from our website - http://www.commandfusion.com/downloads
#HELP_END
#SYMBOL_NAME "CommandFusion iViewer Server v4.5"
#CATEGORY "46" "iPhone / iPod Touch" // Custom
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
#ANALOG_SERIAL_EXPAND InputsWithOutputs
#DIGITAL_EXPAND InputsWithOutputs
#ENABLE_TRACE
#DEFINE_CONSTANT NUMDIGITAL 1000 // Max size inputs/outputs can be expanded to. Use as few as possible to reduce memory usage
#DEFINE_CONSTANT NUMANALOG 99 // Max size inputs/outputs can be expanded to. Use as few as possible to reduce memory usage
#DEFINE_CONSTANT NUMSERIAL 99 // Max size inputs/outputs can be expanded to. Use as few as possible to reduce memory usage
#DEFINE_CONSTANT STRINGLEN 240
#DEFINE_CONSTANT SOCKETLEN 9999
#DEFINE_CONSTANT MAXDIGITALLEN 8 // d####=#\x03
#DEFINE_CONSTANT MAXANALOGLEN 12 // a####=#####\x03
#DEFINE_CONSTANT MAXSTRINGLEN 247 // s####=STRINGLEN\x03
BUFFER_INPUT _SKIP_, _SKIP_, _SKIP_, list_tx$[255];
DIGITAL_INPUT _SKIP_, _SKIP_, _SKIP_, RequestVersion, UpdatingList, SystemBarToggle, SystemBarShow, SystemBarHide, DebugMode, EnableConnections, _SKIP_, fb[NUMDIGITAL, 1];
ANALOG_INPUT _SKIP_, _SKIP_, an_fb[NUMANALOG, 1], _SKIP_;
STRING_INPUT text_o[NUMSERIAL, 1][STRINGLEN];
STRING_OUTPUT iViewerVersion, ConnectedDeviceID, PageName, list_rx$;
DIGITAL_OUTPUT _SKIP_, _SKIP_, _SKIP_, PortraitMode, LandscapeMode, _SKIP_, ReceivedData, ActivityTimeout, _SKIP_, Offline, _SKIP_, press[NUMDIGITAL, 1];
ANALOG_OUTPUT CommandSocketStatus, _SKIP_;
ANALOG_OUTPUT an_act[NUMANALOG, 1],_SKIP_;
STRING_OUTPUT text_i[NUMSERIAL, 1];
STRING_PARAMETER Password$[20];
INTEGER_PARAMETER Port, ActivityTimeoutAmount;
#BEGIN_PARAMETER_PROPERTIES Port
propValidUnits=unitDecimal;
propShortDescription= "Port for handling communication with the iViewer.";
#END_PARAMETER_PROPERTIES
#BEGIN_PARAMETER_PROPERTIES ActivityTimeoutAmount
propValidUnits=unitDecimal;
propShortDescription= "Time in seconds to wait before closing a connection due to no activity.";
#END_PARAMETER_PROPERTIES
#BEGIN_PARAMETER_PROPERTIES Password$
propValidUnits=unitString;
propDefaultValue="na";
propShortDescription="Access password (enter 'na' without quotes for blank password)";
#END_PARAMETER_PROPERTIES
TCP_SERVER CommandServer[SOCKETLEN];
String Debug[999], text_o_store[NUMSERIAL][STRINGLEN];
Integer AccessGranted, Processing;
INTEGER Semaphore;
FUNCTION DebugLogLine(String msg) {
if (DebugMode) {
Print("\n[%s] %s", TIME(), msg);
}
Debug = "";
}
FUNCTION SendFeedback(STRING theMsg, INTEGER containsListData) {
SIGNED_INTEGER status;
// Wait for any list data being sent to finish sending so we dont interrupt it
// (list data can be quite long and require sending a few packets)
while (UpdatingList && !containsListData || Processing) {
ProcessLogic();
}
Processing = 1;
if (CommandServer.SocketStatus = 2) {
status = SocketSend(CommandServer, theMsg);
if (DebugMode) {
Debug = "SEND: " + theMsg;
DebugLogLine(Debug);
if (status < 0) {
Debug = "Error sending to iViewer: " + ITOA(status);
DebugLogLine(Debug);
}
}
} else if (DebugMode) {
Trace("Cannot send when socket status = %d", CommandServer.SocketStatus);
}
Processing = 0;
}
Function InitializeFeedback() {
STRING Response[SOCKETLEN];
INTEGER i, count;
count = 0;
// Digital
for (i=1 to GetNumArrayCols(fb)) {
if (IsSignalDefined(fb[i])) {
if (fb[i]) {
if (count + MAXDIGITALLEN > SOCKETLEN){
SendFeedback(Response, 0);
Response = "";
count = 0;
}
Response = Response + "d" + ITOA(i) + "=" + ITOA(fb[i]) + "\x03";
count = len(Response);
}
}
}
// Analog
for (i=1 to GetNumArrayCols(an_fb)) {
if (IsSignalDefined(an_fb[i])) {
if (an_fb[i]) {
if (count + MAXANALOGLEN > SOCKETLEN) {
SendFeedback(Response, 0);
Response = "";
count = 0;
}
Response = Response + "a" + ITOA(i) + "=" + ITOA(an_fb[i]) + "\x03";
count = len(Response);
}
}
}
// Serial
for (i=1 to GetNumArrayCols(text_o)) {
if (IsSignalDefined(text_o[i])) {
if (text_o_store[i] <> "") {
if (count + MAXSTRINGLEN > SOCKETLEN) {
SendFeedback(Response, 0);
Response = "";
count = 0;
}
Response = Response + "s" + ITOA(i) + "=" + text_o_store[i] + "\x03";
count = len(response);
}
}
}
if (count + 24 > SOCKETLEN) {
SendFeedback(Response, 0);
Response = "";
count = 0;
}
if (SystemBarToggle) {
Response = Response + "d17922=1\x03";
}
if (SystemBarShow) {
Response = Response + "d17931=1\x03";
}
if (SystemBarHide) {
Response = Response + "d17932=0\x03";
}
SendFeedback(Response, 0);
Response = "";
}
FUNCTION ProcessCommands(String CommandRequest) {
String Response[1024], CommandType[1], Join[10], Value[255], Request[1024], Trash[1];
Request = CommandRequest;
// Get command type (d, a, s, etc)
CommandType = RemoveByLength(1, Request);
if (CommandType = "i") {
if (AccessGranted) {
// Initialize request
InitializeFeedback();
// use remaining response as value
Value = RemoveByLength(LEN(Request), Request);
}
} else if (CommandType = "h") {
if (DebugMode) {
Trace("Heartbeat. (Port: %u)",port);
}
// Heartbeat message
// Remove the equals sign
Trash = RemoveByLength(1, Request);
// use remaining response as value
Value = RemoveByLength(LEN(Request), Request);
if (Value = "0") {
Response = "h=1\x03";
SendFeedback(Response, 0);
}
} else if (CommandType = "p") {
// Password received
// Remove the equals sign
Trash = RemoveByLength(1, Request);
// use remaining response as value
AccessGranted = 0;
// Check for empty passwords
if (len(Request))
{
Value = RemoveByLength(LEN(Request), Request);
}
else
{
Value = "";
}
if (Password$ = Value || Password$ = "na") {
Response = "p=ok\x03";
InitializeFeedback();
AccessGranted = 1;
} else {
Response = "p=bad\x03";
}
SendFeedback(Response, 0);
}
else if (CommandType = "m") {
// Orientation change
// Remove the equals sign
Trash = RemoveByLength(1, Request);
// use remaining response as value
Value = RemoveByLength(LEN(Request), Request);
if (Value = "portrait") {
LandscapeMode = 0;
PortraitMode = 1;
} else if (Value = "landscape") {
PortraitMode = 0;
LandscapeMode = 1;
}
} else if (CommandType = "n") {
// Page change
// Remove the equals sign
Trash = RemoveByLength(1, Request);
// use remaining response as value
Value = RemoveByLength(LEN(Request), Request);
PageName = Value;
} else if (CommandType = "l") {
// Received list data
list_rx$ = "l" + Request + "\x03";
} else if (FIND("=", Request) > 1) {
if (AccessGranted)
{
// Get join number
Join = RemoveByLength(FIND("=", Request) - 1, Request);
// Remove the equals sign
Trash = RemoveByLength(1, Request);
// use remaining response as value
Value = Request;
// Remove end of message delimeter
if (RIGHT(Value, 1) = "\x03")
{
Value = Left(Value, LEN(Value) - 1);
}
// Disguard zero joins
if (ATOI(Join) > 0) {
//Print("\nCommandType: %s, Join: %d, Value: %s", CommandType, ATOI(Join), Value);
if (CompareStrings(LEFT(CommandType, 1), "d") = 0) {
if (IsSignalDefined(press[ATOI(Join)]))
{
press[ATOI(Join)] = ATOI(Value);
}
Debug = "Digital " + Join + ": " + Value;
DebugLogLine(Debug);
} else if (CompareStrings(CommandType, "a") = 0) {
if (IsSignalDefined(an_act[ATOI(Join)])) {
an_act[ATOI(Join)] = ATOI(Value);
}
Debug = "Analog " + Join + ": " + Value;
DebugLogLine(Debug);
} else if (CompareStrings(CommandType, "s") = 0) {
if (IsSignalDefined(text_i[ATOI(Join)])) {
text_i[ATOI(Join)] = Value;
}
// Reserved join for iViewer version number on client
if (ATOI(Join) = 10001) {
iViewerVersion = value;
}
// Reserved join for client Device ID
if (ATOI(Join) = 10002) {
ConnectedDeviceID = value;
}
Debug = "Serial " + Join + ": " + Value;
DebugLogLine(Debug);
} else if (DebugMode) {
DebugLogLine("Couldn't match the command");
DebugLogLine(CommandRequest);
}
}
}
}
}
PUSH RequestVersion {
SendFeedback("d10001=1", 0);
SendFeedback("d10001=0", 0);
}
CHANGE fb {
INTEGER index;
STRING Response[255];
index = GetLastModifiedArrayIndex();
Response = "d" + ITOA(index) + "=" + ITOA(fb[index]) + "\x03";
SendFeedback(Response, 0);
}
CHANGE an_fb {
INTEGER index, Value;
STRING Response[255];
index = GetLastModifiedArrayIndex();
Value = an_fb[index];
Response = "a" + ITOA(index) + "=" + ITOA(Value) + "\x03";
SendFeedback(Response, 0);
}
CHANGE text_o {
INTEGER index;
STRING Response[255];
index = GetLastModifiedArrayIndex();
if (text_o[index] = "") {
Response = "s" + ITOA(index) + "= \x03";
text_o_store[index] = " ";
} else {
While (len(text_o[index]) > MAXSTRINGLEN) {
Response = "s" + ITOA(index) + "=" + GatherByLength(80, text_o[index]) + "\x03";
SendFeedback(Response, 0);
}
Response = "s" + ITOA(index) + "=" + text_o[index] + "\x03";
text_o_store[index] = text_o[index];
}
SendFeedback(Response, 0);
}
CHANGE list_tx$ {
STRING command[50];
if (Semaphore = 0) {
Semaphore = 1;
while (find("\x03", list_tx$) <> 0) {
command = Remove("\x03", list_tx$);
SendFeedback(command, 1);
}
Semaphore = 0;
}
}
PUSH EnableConnections {
SIGNED_INTEGER status;
DebugLogLine("iViewer Connections Enabled");
AccessGranted = 0;
//Trace("\nPort: %i", Port);
if (Port > 1) {
// Start server listening
status = SocketServerStartListen(CommandServer, "0.0.0.0", Port);
if (status < 0) {
Print("Error listening to 0.0.0.0 on port %u (status: %d)", port, status);
}
}
}
RELEASE EnableConnections {
DebugLogLine("iViewer Connections Disabled");
// Stop client handler server listening
SocketServerStopListen(CommandServer);
Offline = 1;
}
PUSH SystemBarToggle {
SendFeedback("d17922=1\x03", 0);
}
PUSH SystemBarShow {
SendFeedback("d17931=1\x03", 0);
}
PUSH SystemBarHide {
SendFeedback("d17932=0\x03", 0);
}
// CONNECTIONS
SOCKETCONNECT CommandServer {
DebugLogLine("iViewer connected");
Offline = 0;
WAIT(ActivityTimeoutAmount * 100, ActivityWatcher) {
PULSE(50, ActivityTimeout);
if (Offline = 0) {
Offline = 1;
}
}
}
SOCKETDISCONNECT CommandServer {
SIGNED_INTEGER status;
Integer i;
Offline = 1;
CANCELWAIT(ActivityWatcher);
DebugLogLine("iViewer disconnected");
AccessGranted = 0;
// Force all digital outputs low, safeguard against held buttons (volume ramping etc, after disconnecting)
for (i=1 to GetNumArrayCols(press)) {
if (IsSignalDefined(press[i])) {
press[i] = 0;
}
}
}
SOCKETSTATUS CommandServer {
CommandSocketStatus = SocketGetStatus();
}
Function Main() {
STRING temp[255];
AccessGranted = 0;
Offline = 1;
WHILE (1) {
temp = gather("\x03",CommandServer.SocketRxBuf);
debugLogLine("new command gathered");
RETIMEWAIT(ActivityTimeoutAmount * 100, ActivityWatcher);
temp = left(temp, Len(temp) - 1);
if (len(temp) > 0) {
ProcessCommands(temp);
}
delay(5);
}
}