forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pda_aq_programmer.c
1441 lines (1280 loc) · 49.4 KB
/
pda_aq_programmer.c
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 Shaun Feakes - All rights reserved
*
* You may use redistribute and/or modify this code under the terms of
* the GNU General Public License version 2 as published by the
* Free Software Foundation. For the terms of this license,
* see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* https://github.com/sfeakes/aqualinkd
*/
#define _GNU_SOURCE 1 // for strcasestr & strptime
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include "aqualink.h"
#include "utils.h"
#include "aq_programmer.h"
#include "aq_serial.h"
#include "pda.h"
#include "pda_menu.h"
#include "pda_aq_programmer.h"
#include "config.h"
#include "aq_panel.h"
#include "rs_msg_utils.h"
#ifdef AQ_DEBUG
#include "timespec_subtract.h"
#endif
bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, int numMessageReceived);
static bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
unsigned long sec, unsigned long msec);
static bool _waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
unsigned long sec, unsigned long msec, bool forceNext);
bool waitForPDAMessageTypes(struct aqualinkdata *aq_data, unsigned char mtype1,
unsigned char mtype2, unsigned long sec,
unsigned long msec);
bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data,
unsigned char mtype1, unsigned char mtype2,
unsigned char mtype3, unsigned long sec,
unsigned long msec, char *text, int line);
static bool _waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data,
unsigned char mtype1, unsigned char mtype2,
unsigned char mtype3, unsigned long sec,
unsigned long msec, char *text, int line,
bool forceNext);
bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu);
bool wait_pda_selected_item(struct aqualinkdata *aq_data);
bool waitForPDAnextMenu(struct aqualinkdata *aq_data);
bool loopover_devices(struct aqualinkdata *aq_data);
bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charlimit);
bool select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu);
static pda_type _PDA_Type;
/*
// Each RS message / call to this function is around 0.2 seconds apart
//#define MAX_ACK_FOR_THREAD 200 // ~40 seconds (Init takes 30)
#define MAX_ACK_FOR_THREAD 60 // ~12 seconds (testing, will stop every thread)
// *** DELETE THIS WHEN PDA IS OUT OF BETA ****
void pda_programming_thread_check(struct aqualinkdata *aq_data)
{
static pthread_t thread_id = 0;
static int ack_count = 0;
#ifdef AQ_DEBUG
static struct timespec start;
static struct timespec now;
struct timespec elapsed;
#endif
// Check for long lasting threads
if (aq_data->active_thread.thread_id != 0) {
if (thread_id != *aq_data->active_thread.thread_id) {
printf ("**************** LAST POINTER SET %ld , %p ****************************\n",thread_id,&thread_id);
thread_id = *aq_data->active_thread.thread_id;
#ifdef AQ_DEBUG
clock_gettime(CLOCK_REALTIME, &start);
#endif
printf ("**************** NEW POINTER SET %d, %ld %ld , %p %p ****************************\n",aq_data->active_thread.ptype,thread_id,*aq_data->active_thread.thread_id,&thread_id,aq_data->active_thread.thread_id);
ack_count = 0;
} else if (ack_count > MAX_ACK_FOR_THREAD) {
#ifdef AQ_DEBUG
clock_gettime(CLOCK_REALTIME, &now);
timespec_subtract(&elapsed, &now, &start);
LOG(PDA_LOG,LOG_ERR, "Thread %d,%p FAILED to finished in reasonable time, %d.%03ld sec, killing it.\n",
aq_data->active_thread.ptype,
aq_data->active_thread.thread_id,
elapsed.tv_sec, elapsed.tv_nsec / 1000000L);
#else
LOG(PDA_LOG,LOG_ERR, "Thread %d,%p FAILED to finished in reasonable time, killing it!\n", aq_data->active_thread.ptype, aq_data->active_thread.thread_id)
#endif
if (pthread_cancel(*aq_data->active_thread.thread_id) != 0)
LOG(PDA_LOG,LOG_ERR, "Thread kill failed\n");
else {
}
aq_data->active_thread.thread_id = 0;
aq_data->active_thread.ptype = AQP_NULL;
ack_count = 0;
thread_id = 0;
} else {
ack_count++;
}
} else {
ack_count = 0;
thread_id = 0;
}
}
*/
bool wait_pda_selected_item(struct aqualinkdata *aq_data)
{
int i=0;
while ((pda_m_hlightindex() == -1) && (i < 5)){
waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2,0);
i++;
}
if (pda_m_hlightindex() == -1) {
return false;
} else {
return true;
}
}
bool waitForPDAnextMenu(struct aqualinkdata *aq_data) {
pda_menu_type menu;
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAnextMenu\n");
if (!waitForPDAMessageTypes(aq_data,CMD_PDA_CLEAR,CMD_STATUS,2,0)) {
LOG(PDA_LOG,LOG_ERR, "waitForPDAnextMenu - no CLEAR or STATUS\n");
return false;
} else if (aq_data->last_packet_type == CMD_STATUS) {
// if a STATUS is received something is probably off, either the last key
// sent was lost or we missed the menu change
LOG(PDA_LOG,LOG_WARNING, "waitForPDAnextMenu - received STATUS instead of CLEAR\n");
return true;
} else if (! waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,
CMD_PDA_HIGHLIGHTCHARS,CMD_STATUS,3,
0,NULL,0)) {
LOG(PDA_LOG,LOG_ERR, "waitForPDAnextMenu - no HIGHLIGHT or STATUS\n");
return false;
} else if (aq_data->last_packet_type == CMD_STATUS) {
// The FW version and status menus do not have highlight
LOG(PDA_LOG,LOG_NOTICE, "waitForPDAnextMenu - received STATUS instead of HIGHLIGHT\n");
} else if ((aq_data->last_packet_type == CMD_PDA_HIGHLIGHTCHARS) &&
(((menu = pda_m_type()) == PM_EQUIPTMENT_CONTROL) ||
(menu == PM_HOME) || (menu == PM_BUILDING_HOME))) {
// Flashing state for filter pump and spa mode is done with HIGHLIGHTCHARS
if (! waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_STATUS,2,0)) {
LOG(PDA_LOG,LOG_ERR, "waitForPDAnextMenu - EQUIPTMENT_CONTROL no HIGHLIGHT or STATUS\n");
return false;
}
}
return true;
}
bool loopover_devices(struct aqualinkdata *aq_data) {
int i;
int index = -1;
if (! goto_pda_menu(aq_data, PM_EQUIPTMENT_CONTROL)) {
LOG(PDA_LOG,LOG_ERR, "loopover_devices :- can't goto PM_EQUIPTMENT_CONTROL menu\n");
//cleanAndTerminateThread(threadCtrl);
return false;
}
// Should look for message "ALL OFF", that's end of device list.
for (i=0; i < 18 && (index = pda_find_m_index("ALL OFF")) == -1 ; i++) {
send_cmd(KEY_PDA_DOWN);
//waitForMessage(aq_data, NULL, 1);
waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_MSG_LONG,2,0);
}
if (index == -1) {
LOG(PDA_LOG,LOG_ERR, "loopover_devices :- can't find ALL OFF\n");
return false;
}
return true;
}
/*
if charlimit is set, use case insensitive match and limit chars.
if charlimit is -1, use VERY loose matching.
*/
bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charlimit) {
int i=pda_m_hlightindex();
int min_index = -1;
int max_index = -1;
int index = -1;
int cnt = 0;
bool bLookingForBoost = false;
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer looking for menu text '%s' (limit=%d)\n",menuText,charlimit);
if (charlimit == 0)
index = pda_find_m_index(menuText);
else if (charlimit > 0)
index = pda_find_m_index_case(menuText, charlimit);
else if (charlimit == -1)
index = pda_find_m_index_loose(menuText);
//int index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit);
if (index < 0) { // No menu, is there a page down. "PDA Line 9 = ^^ MORE __"
if (strncasecmp(pda_m_line(9)," ^^ MORE", 10) == 0) {
int j;
for(j=0; j < 20; j++) {
send_cmd(KEY_PDA_DOWN);
//delay(500);
//wait_for_empty_cmd_buffer();
//waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2);
waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_MSG_LONG,2,0);
//waitForMessage(aq_data, NULL, 1);
index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit);
if (index >= 0) {
i=pda_m_hlightindex();
break;
}
}
if (index < 0) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer couldn't find menu item on any page '%s'\n",menuText);
return false;
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer couldn't find menu item '%s' in menu %d index %d\n", menuText, pda_m_type(), index);
return false;
}
}
if (strncasecmp(pda_m_line(9)," ^^ MORE", 10) != 0) {
if (pda_m_type() == PM_HOME) {
min_index = 4;
max_index = 9;
} else if (pda_m_type() == PM_EQUIPTMENT_CONTROL) {
min_index = 1;
max_index = 9;
} else if (pda_m_type() == PM_MAIN) {
// Line 0 = MAIN MENU
// Line 1 =
// Line 2 = HELP >
// Line 3 = PROGRAM >
// Line 4 = SET TEMP >
// Line 5 = SET TIME >
// Line 6 = PDA OPTIONS >
// Line 7 = SYSTEM SETUP >
// Line 8 =
// Line 9 =
// Line 0 = MAIN MENU
// Line 1 = HELP >
// Line 2 = PROGRAM >
// Line 3 = SET TEMP >
// Line 4 = SET TIME >
// Line 5 = SET AquaPure >
// Line 6 = PDA OPTIONS >
// Line 7 = SYSTEM SETUP >
// Line 8 =
// Line 9 = BOOST
// "SET AquaPure" and "BOOST" are only present when filter pump is running
if (strncasecmp(pda_m_line(9)," BOOST ", 16) == 0) {
min_index = 1;
max_index = 8; // to account for 8 missing
if (index == 9) { // looking for boost
bLookingForBoost = true;
index = 8;
}
} else {
min_index = 2;
max_index = 7;
}
} else if (pda_m_type() == PM_BOOST) {
// PDA Line 0 = BOOST
// PDA Line 1 =
// PDA Line 2 = Operate the
// PDA Line 3 = AquaPure
// PDA Line 4 = chlorinator
// PDA Line 5 = at 100%
// PDA Line 6 = for 24 hrs.
// PDA Line 7 =
// PDA Line 8 = START
// PDA Line 9 = GO BACK
// PDA Line 0 = BOOST
// PDA Line 1 =
// PDA Line 2 =
// PDA Line 3 = TIME REMAINING
// PDA Line 4 = 23:59
// PDA Line 5 =
// PDA Line 6 =
// PDA Line 7 = PAUSE
// PDA Line 8 = RESTART
// PDA Line 9 = STOP
if (strncasecmp(pda_m_line(9)," STOP", 10) == 0) {
min_index = 7;
max_index = 9;
} else {
min_index = 8;
max_index = 9;
}
}
}
LOG(PDA_LOG,LOG_DEBUG, "find_pda_menu_item i=%d idx=%d min=%d max=%d boost=%d\n",
i, index, min_index, max_index, bLookingForBoost?1:0);
if (i < index) {
if ((min_index != -1) && ((index - i) > (i - min_index + max_index - index + 1))) {
cnt = i - min_index + max_index - index + 1;
for (i=0; i < cnt; i++) {
waitfor_queue2empty();
send_cmd(KEY_PDA_UP);
}
} else {
for (i=pda_m_hlightindex(); i < index; i++) {
waitfor_queue2empty();
send_cmd(KEY_PDA_DOWN);
}
}
} else if (i > index) {
if ((min_index != -1) && ((i - index) > (index - min_index + max_index - i + 1))) {
cnt = i - min_index + max_index - index + 1;
for (i=0; i < cnt; i++) {
waitfor_queue2empty();
send_cmd(KEY_PDA_UP);
}
} else {
for (i=pda_m_hlightindex(); i > index; i--) {
waitfor_queue2empty();
send_cmd(KEY_PDA_UP);
}
}
}
return waitForPDAMessageHighlight(aq_data, bLookingForBoost?9:index, 10);
}
bool _select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu, bool loose);
bool select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu){
return _select_pda_menu_item(aq_data, menuText, waitForNextMenu, false);
}
bool select_pda_menu_item_loose(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu){
return _select_pda_menu_item(aq_data, menuText, waitForNextMenu, true);
}
bool _select_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, bool waitForNextMenu, bool loose) {
//int matchType = loose?-1:0; // NSF release 2.1.0 was this and it worked. Need to re-check why I did this.
//int matchType = loose?-1:1;
int matchType = loose?-1:strlen(menuText); // NSF Not way to check this. (release 2.2.0 introduced this with the line above)
if ( find_pda_menu_item(aq_data, menuText, matchType) ) {
send_cmd(KEY_PDA_SELECT);
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer selected menu item '%s'\n",menuText);
if (waitForNextMenu)
return waitForPDAnextMenu(aq_data);
return true;
}
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer couldn't select menu item '%s' menu %d\n",menuText, pda_m_type());
return false;
}
// for reference see H0572300 - AquaLink PDA I/O Manual
// https://www.jandy.com/-/media/zodiac/global/downloads/h/h0572300.pdf
// and H0574200 - AquaPalm Wireless Handheld Remote Installation and Operation Manual
// https://www.jandy.com/-/media/zodiac/global/downloads/h/h0574200.pdf
// and 6594 - AquaLink RS Control Panel Installation Manual
// https://www.jandy.com/-/media/zodiac/global/downloads/0748-91071/6594.pdf
bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
bool ret = true;
int cnt = 0;
pda_menu_type prev_menu = PM_UNKNOWN;
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer request for menu %d, current %d\n",
menu, pda_m_type());
if (pda_m_type() == PM_FW_VERSION) {
LOG(PDA_LOG,LOG_DEBUG, "goto_pda_menu at FW version menu\n");
send_cmd(KEY_PDA_BACK);
if (! waitForPDAnextMenu(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer wait for next menu failed\n");
} else if ((pda_m_type() != PM_BUILDING_HOME) && (pda_m_type() != PM_HOME)) {
LOG(PDA_LOG,LOG_NOTICE, "goto_pda_menu went from FW_VERSION to %d\n", pda_m_type());
}
}
while (ret && (pda_m_type() != menu) && (cnt <= 5)) {
if (pda_m_type() == PM_BUILDING_HOME) {
LOG(PDA_LOG,LOG_DEBUG, "goto_pda_menu building home menu\n");
if (! (ret=waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,3,0))) {
LOG(PDA_LOG,LOG_ERR, "goto_pda_menu building home wait for highlight failed\n");
break;
}
if (menu == pda_m_type()) {
break;
}
}
prev_menu = pda_m_type();
switch (menu) {
case PM_HOME:
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
break;
case PM_EQUIPTMENT_CONTROL:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "EQUIPMENT ON/OFF", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_PALM_OPTIONS:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "PALM OPTIONS", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_AUX_LABEL:
if ( _PDA_Type == PDA) {
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SYSTEM SETUP", true);
} else if (pda_m_type() == PM_SYSTEM_SETUP) {
ret = select_pda_menu_item(aq_data, "LABEL AUX", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA in AquaPlalm mode, there is no SYSTEM SETUP / LABEL AUX menu\n");
}
break;
case PM_SYSTEM_SETUP:
if ( _PDA_Type == PDA) {
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SYSTEM SETUP", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA in AquaPlalm mode, there is no SYSTEM SETUP menu\n");
}
break;
case PM_FREEZE_PROTECT:
if ( _PDA_Type == PDA) {
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SYSTEM SETUP", true);
} else if (pda_m_type() == PM_SYSTEM_SETUP) {
ret = select_pda_menu_item(aq_data, "FREEZE PROTECT", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA in AquaPlalm mode, there is no SYSTEM SETUP / FREEZE PROTECT menu\n");
}
break;
case PM_AQUAPURE:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SET AquaPure", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_BOOST:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item_loose(aq_data, "BOOST", true);
//ret = select_pda_menu_item(aq_data, "BOOST", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
//printf("****MENU SELECT RETURN %d*****\n",ret);
break;
case PM_SET_TEMP:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
if (isCOMBO_PANEL) {
ret = select_pda_menu_item(aq_data, "SET TEMP", true);
} else {
// Depending on control panel config, may get an extra menu asking to press any key
LOG(PDA_LOG,LOG_DEBUG, "PDA in single device mode, \n");
ret = select_pda_menu_item(aq_data, "SET TEMP", false);
// We could press enter here, but I can't test it, so just wait for message to dissapear.
ret = waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,5,0);
//waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10);
//waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,20,"press ANY key",8);
}
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
case PM_SET_TIME:
if (pda_m_type() == PM_HOME) {
ret = select_pda_menu_item(aq_data, "MENU", true);
} else if (pda_m_type() == PM_MAIN) {
ret = select_pda_menu_item(aq_data, "SET TIME", true);
} else {
send_cmd(KEY_PDA_BACK);
ret = waitForPDAnextMenu(aq_data);
}
break;
default:
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer didn't understand requested menu\n");
return false;
break;
}
if (prev_menu == pda_m_type()) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer request for menu %d, stuck on %d\n",
menu, pda_m_type());
break;
}
LOG(PDA_LOG,LOG_DEBUG, "PDA Device programmer request for menu %d, current %d\n", menu, pda_m_type());
cnt++;
}
if (pda_m_type() != menu) {
LOG(PDA_LOG,LOG_ERR, "PDA Device programmer didn't find a requested menu %d, current %d\n", menu, pda_m_type());
return false;
}
return true;
}
void *set_aqualink_PDA_device_on_off( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i=0;
//int found;
char device_name[15];
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_DEVICE_ON_OFF);
char *buf = (char*)threadCtrl->thread_args;
unsigned int device = atoi(&buf[0]);
unsigned int state = atoi(&buf[5]);
if (device > aq_data->total_buttons) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off :- bad device number '%d'\n",device);
cleanAndTerminateThread(threadCtrl);
return ptr;
}
LOG(PDA_LOG,LOG_INFO, "PDA Device On/Off, device '%s', state %d\n",aq_data->aqbuttons[device].label,state);
if (! goto_pda_menu(aq_data, PM_EQUIPTMENT_CONTROL)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off :- can't find EQUIPTMENT CONTROL menu\n");
cleanAndTerminateThread(threadCtrl);
return ptr;
}
// If single config (Spa OR pool) rather than (Spa AND pool) heater is TEMP1 and TEMP2
if (isSINGLE_DEV_PANEL && device == aq_data->pool_heater_index) { // rename Heater and Spa
sprintf(device_name,"%-13s\n","TEMP1");
} else if (isSINGLE_DEV_PANEL && device == aq_data->spa_heater_index) {// rename Heater and Spa
sprintf(device_name,"%-13s\n","TEMP2");
} else {
//Pad name with spaces so something like "SPA" doesn't match "SPA BLOWER"
sprintf(device_name,"%-13s\n",aq_data->aqbuttons[device].label);
}
// NSF Added this since DEBUG hitting wrong command
//waitfor_queue2empty();
if ( find_pda_menu_item(aq_data, device_name, 13) ) {
if (aq_data->aqbuttons[device].led->state != state) {
//printf("*** Select State ***\n");
LOG(PDA_LOG,LOG_INFO, "PDA Device On/Off, found device '%s', changing state\n",aq_data->aqbuttons[device].label,state);
force_queue_delete(); // NSF This is a bad thing to do. Need to fix this
send_cmd(KEY_PDA_SELECT);
while (get_aq_cmd_length() > 0) { delay(500); }
// If you are turning on a heater there will be a sub menu to set temp
if ((state == ON) && ((device == aq_data->pool_heater_index) || (device == aq_data->spa_heater_index))) {
if (! waitForPDAnextMenu(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - waitForPDAnextMenu\n",
aq_data->aqbuttons[device].label);
} else {
send_cmd(KEY_PDA_SELECT);
waitForPDAnextMenu(aq_data);
}
} else { // not turning on heater wait for line update
// worst case spa when pool is running
if (!waitForPDAMessageType(aq_data,CMD_STATUS,3,0)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - wait for CMD_STATUS\n",
aq_data->aqbuttons[device].label);
}
// check for delay status
if (pda_m_type() == PM_TURN_ON_AFTER_DELAY) {
send_cmd(KEY_PDA_BACK);
waitForPDAnextMenu(aq_data);
}
}
} else {
LOG(PDA_LOG,LOG_INFO, "PDA Device On/Off, found device '%s', not changing state, is same\n",aq_data->aqbuttons[device].label,state);
}
} else {
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off, device '%s' not found\n",aq_data->aqbuttons[device].label);
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *get_aqualink_PDA_device_status( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i;
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_DEVICE_STATUS);
goto_pda_menu(aq_data, PM_HOME);
if (! loopover_devices(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Device Status :- failed\n");
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_PDA_init( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i=0;
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_INIT);
//int val = atoi((char*)threadCtrl->thread_args);
//LOG(PDA_LOG,LOG_DEBUG, "PDA Init\n", val);
LOG(PDA_LOG,LOG_DEBUG, "PDA Init\n");
if (pda_m_type() == PM_FW_VERSION) {
// check pda_m_line(1) to "AquaPalm"
if (strstr(pda_m_line(1), "AquaPalm") != NULL) {
_PDA_Type = AQUAPALM;
} else {
_PDA_Type = PDA;
}
char *ptr1 = pda_m_line(1);
char *ptr2 = pda_m_line(5);
ptr1[AQ_MSGLEN+1] = '\0';
ptr2[AQ_MSGLEN+1] = '\0';
//strcpy(aq_data->version, stripwhitespace(ptr));
snprintf(aq_data->version, (AQ_MSGLEN*2)-1, "%s %s",stripwhitespace(ptr1),stripwhitespace(ptr2));
//printf("****** Version '%s' ********\n",aq_data->version);
LOG(PDA_LOG,LOG_DEBUG, "PDA type=%d, version=%s\n", _PDA_Type, aq_data->version);
// don't wait for version menu to time out press back to get to home menu faster
send_cmd(KEY_PDA_BACK);
}
else {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- should be called when on FW VERSION menu.\n");
}
// Get status of all devices
if (! loopover_devices(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- can't find menu\n");
}
// Get heater setpoints
if (! get_PDA_aqualink_pool_spa_heater_temps(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- Error getting heater setpoints\n");
}
// Get freeze protect setpoint, AquaPalm doesn't have freeze protect in menu.
if (_PDA_Type != AQUAPALM && ! get_PDA_freeze_protect_temp(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Init :- Error getting freeze setpoints\n");
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
void *set_aqualink_PDA_wakeinit( void *ptr )
{
struct programmingThreadCtrl *threadCtrl;
threadCtrl = (struct programmingThreadCtrl *) ptr;
struct aqualinkdata *aq_data = threadCtrl->aq_data;
//int i=0;
// At this point, we should probably just exit if there is a thread already going as
// it means the wake was called due to changing a device.
waitForSingleThreadOrTerminate(threadCtrl, AQ_PDA_WAKE_INIT);
LOG(PDA_LOG,LOG_DEBUG, "PDA Wake Init\n");
// Get status of all devices
if (! loopover_devices(aq_data)) {
LOG(PDA_LOG,LOG_ERR, "PDA Wake Init :- can't find menu\n");
}
cleanAndTerminateThread(threadCtrl);
// just stop compiler error, ptr is not valid as it's just been freed
return ptr;
}
bool get_PDA_freeze_protect_temp(struct aqualinkdata *aq_data) {
if ( _PDA_Type == PDA) {
if (! goto_pda_menu(aq_data, PM_FREEZE_PROTECT)) {
return false;
}
/* select the freeze protect temp to see which devices are enabled by freeze
protect */
send_cmd(KEY_PDA_SELECT);
return waitForPDAnextMenu(aq_data);
} else {
LOG(PDA_LOG,LOG_INFO, "In PDA AquaPalm mode, freezepoints not supported\n");
return false;
}
}
bool get_PDA_aqualink_pool_spa_heater_temps(struct aqualinkdata *aq_data) {
// Get heater setpoints
if (! goto_pda_menu(aq_data, PM_SET_TEMP)) {
LOG(PDA_LOG,LOG_ERR, "Could not get heater setpoints, trying again!\n");
// Going to try this twice.
if (! goto_pda_menu(aq_data, PM_SET_TEMP)) {
return false;
}
}
return true;
}
bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, int numMessageReceived)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight index %d\n",highlighIndex);
if(pda_m_hlightindex() == highlighIndex) return true;
int i=0;
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
while( ++i <= numMessageReceived)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight last = 0x%02hhx : index %d : (%d of %d)\n",aq_data->last_packet_type,pda_m_hlightindex(),i,numMessageReceived);
if (aq_data->last_packet_type == CMD_PDA_HIGHLIGHT && pda_m_hlightindex() == highlighIndex) break;
pthread_cond_wait(&aq_data->active_thread.thread_cond, &aq_data->active_thread.thread_mutex);
}
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if (pda_m_hlightindex() != highlighIndex) {
//LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight: did not receive index '%d'\n",highlighIndex);
return false;
} else
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageHighlight: received index '%d'\n",highlighIndex);
return true;
}
static bool _waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
unsigned long sec, unsigned long msec, bool forceNext)
{
return _waitForPDAMessageTypesOrMenu(aq_data, mtype, 0xFF, 0xFF, sec, msec,
NULL, 0, forceNext);
}
static bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
unsigned long sec, unsigned long msec) {
return _waitForPDAMessageType(aq_data, mtype, sec, msec, false);
}
bool waitForPDANextMessageType(struct aqualinkdata *aq_data, unsigned char mtype,
unsigned long sec, unsigned long msec) {
return _waitForPDAMessageType(aq_data, mtype, sec, msec, true);
}
// Wait for Message, hit return on particular menu.
bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data,
unsigned char mtype1, unsigned char mtype2,
unsigned char mtype3, unsigned long sec,
unsigned long msec, char *text, int line)
{
return _waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, mtype3, sec,
msec, text, line, false);
}
static bool _waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data,
unsigned char mtype1, unsigned char mtype2,
unsigned char mtype3, unsigned long sec,
unsigned long msec, char *text, int line,
bool forceNext)
{
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,0x%02hhx,%s,%d,%lu.%03lu sec, fn %d\n",
mtype1,mtype2,mtype3,text,line,sec, msec, forceNext);
int i=0;
bool gotmenu = false;
struct timespec max_wait;
int ret = 0;
if (msec > 999)
{
LOG(PDA_LOG,LOG_ERR, "waitForPDAMessageTypesOrMenu INVALID msec value %lu\n", msec);
}
clock_gettime(CLOCK_REALTIME, &max_wait);
max_wait.tv_sec += sec;
max_wait.tv_nsec += msec*1000000;
if (max_wait.tv_nsec > 999999999L) {
max_wait.tv_nsec -= 1000000000L;
max_wait.tv_sec++;
}
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
if (forceNext) { // Ignore current message type, and wait for next
if ((ret = pthread_cond_timedwait(&aq_data->active_thread.thread_cond,
&aq_data->active_thread.thread_mutex, &max_wait))) {
LOG(PDA_LOG,LOG_ERR, "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,%s,%d - %s\n",
mtype1, mtype2, text,line, strerror(ret));
}
}
while (true) {
i++;
if ((ret = pthread_cond_timedwait(&aq_data->active_thread.thread_cond,
&aq_data->active_thread.thread_mutex, &max_wait))) {
LOG(PDA_LOG,LOG_ERR, "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,0x%02hhx,%s,%d - %s\n",
mtype1,mtype2,mtype3,text,line,strerror(ret));
break;
}
if (gotmenu == false && line > 0 && text != NULL) {
if (stristr(pda_m_line(line), text) != NULL) {
send_cmd(KEY_PDA_SELECT);
gotmenu = true;
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypesOrMenu saw '%s' in line %d\n",text,line);
}
}
if (aq_data->last_packet_type == mtype1 || aq_data->last_packet_type == mtype2 ||
aq_data->last_packet_type == mtype3) {
break;
}
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypesOrMenu last message type 0x%02hhx (%d)\n", aq_data->last_packet_type, i);
}
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
if (aq_data->last_packet_type != mtype1 &&
aq_data->last_packet_type != mtype2 &&
aq_data->last_packet_type != mtype3) {
//LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
LOG(PDA_LOG,LOG_ERR, "waitForPDAMessageTypesOrMenu: did not receive 0x%02hhx, 0x%02hhx or 0x%02hhx\n",
mtype1,mtype2,mtype3);
return false;
} else {
LOG(PDA_LOG,LOG_DEBUG, "waitForPDAMessageTypesOrMenu: received 0x%02hhx\n",aq_data->last_packet_type);
}
return true;
}
bool waitForPDAMessageTypes(struct aqualinkdata *aq_data, unsigned char mtype1,
unsigned char mtype2, unsigned long sec,
unsigned long msec)
{
return waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, 0xFF, sec, msec, NULL, 0);
}
/*
Use -1 for cur_val if you want this to find the current value and change it.
Use number for cur_val to increase / decrease from known start point
*/
bool set_PDA_numeric_field_value(struct aqualinkdata *aq_data, int val, int cur_val, char *select_label, int step) {
int i=0;
LOG(PDA_LOG,LOG_DEBUG, "set_PDA_numeric_field_value %s from %d to %d step %d\n", select_label, cur_val, val, step);
if (select_label != NULL) {
if ( ! select_pda_menu_item(aq_data, select_label, false) ) {
return false;
}
}
if (cur_val == -1) {
char *hghlight_chars = NULL;
int hlight_length = 0;
int i=0;
hghlight_chars = pda_m_hlightchars(&hlight_length); // NSF May need to take this out and there for the LOG entry after while
while (hlight_length >= 15 || hlight_length <= 0) {
//delay(500);
waitForPDANextMessageType(aq_data,CMD_PDA_HIGHLIGHTCHARS, 1, 0);
hghlight_chars = pda_m_hlightchars(&hlight_length);
LOG(PDA_LOG,LOG_DEBUG, "Numeric selector, highlight chars '%.*s'\n", hlight_length, hghlight_chars);
if (++i >= 20) {
LOG(PDA_LOG,LOG_ERR, "Numeric selector, didn't find highlight chars, current selection is '%.*s'\n", hlight_length, hghlight_chars);
return false;
}
}
cur_val = atoi(hghlight_chars);
LOG(PDA_LOG,LOG_DEBUG, "Numeric selector, highlight chars '%.*s', numeric value using %d\n", hlight_length, hghlight_chars, cur_val);
}
if (val < cur_val) {
LOG(PDA_LOG,LOG_DEBUG, "Numeric selector %s value : lower from %d to %d\n", select_label, cur_val, val);
for (i = cur_val; i > val; i=i-step) {
send_cmd(KEY_PDA_DOWN);
}
} else if (val > cur_val) {
LOG(PDA_LOG,LOG_DEBUG, "Numeric selector %s value : raise from %d to %d\n", select_label, cur_val, val);
for (i = cur_val; i < val; i=i+step) {
send_cmd(KEY_PDA_UP);
}
} else {
LOG(PDA_LOG,LOG_DEBUG, "Numeric selector %s value : already at %d\n", select_label, val);
}
send_cmd(KEY_PDA_SELECT);
LOG(PDA_LOG,LOG_DEBUG, "Numeric selector %s value : set to %d\n", select_label, val);
return true;
}
bool set_PDA_aqualink_SWG_setpoint(struct aqualinkdata *aq_data, int val) {
if (! goto_pda_menu(aq_data, PM_AQUAPURE)) {
LOG(PDA_LOG,LOG_ERR, "Error finding SWG setpoints menu\n");
return false;
}
// wait for menu to display to capture current value with process_pda_packet_msg_long_SWG
waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS, 2, 0);
if (pda_find_m_index("SET POOL") < 0) {
// Single Setpoint Screen
return set_PDA_numeric_field_value(aq_data, val, -1, NULL, 5);
} else if (aq_data->aqbuttons[SPA_INDEX].led->state != OFF) {
// Dual Setpoint Screen with SPA mode enabled
// :TODO: aq_data should have 2 swg_precent values and GUI should be updated to
// display and modify both values.
return set_PDA_numeric_field_value(aq_data, val, -1, "SET SPA", 5);
} else {
// Dual Setpoint Screen with SPA mode disabled
return set_PDA_numeric_field_value(aq_data, val, -1, "SET POOL", 5);
}
}
bool set_PDA_aqualink_boost(struct aqualinkdata *aq_data, bool val)
{
if (! goto_pda_menu(aq_data, PM_BOOST)) {