40
40
#endif
41
41
42
42
bool waitForPDAMessageHighlight (struct aqualinkdata * aq_data , int highlighIndex , int numMessageReceived );
43
- bool waitForPDAMessageType (struct aqualinkdata * aq_data , unsigned char mtype , int numMessageReceived );
44
- bool waitForPDAMessageTypes (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , int numMessageReceived );
45
- bool waitForPDAMessageTypesOrMenu (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , int numMessageReceived , char * text , int line );
43
+ static bool waitForPDAMessageType (struct aqualinkdata * aq_data , unsigned char mtype ,
44
+ unsigned long sec , unsigned long msec );
45
+ bool waitForPDAMessageTypes (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , unsigned long sec , unsigned long msec );
46
+ bool waitForPDAMessageTypesOrMenu (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , unsigned long sec , unsigned long msec , char * text , int line );
46
47
bool goto_pda_menu (struct aqualinkdata * aq_data , pda_menu_type menu );
47
48
bool wait_pda_selected_item (struct aqualinkdata * aq_data );
48
49
bool waitForPDAnextMenu (struct aqualinkdata * aq_data );
@@ -114,8 +115,8 @@ bool wait_pda_selected_item(struct aqualinkdata *aq_data)
114
115
{
115
116
int i = 0 ;
116
117
117
- while (pda_m_hlightindex () == -1 && i < 5 ){
118
- waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,10 );
118
+ while (( pda_m_hlightindex () == -1 ) && ( i < 5 ) ){
119
+ waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,2 , 0 );
119
120
i ++ ;
120
121
}
121
122
@@ -126,8 +127,8 @@ bool wait_pda_selected_item(struct aqualinkdata *aq_data)
126
127
}
127
128
128
129
bool waitForPDAnextMenu (struct aqualinkdata * aq_data ) {
129
- waitForPDAMessageType (aq_data ,CMD_PDA_CLEAR ,10 );
130
- return waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_PDA_HIGHLIGHTCHARS ,15 );
130
+ waitForPDAMessageType (aq_data ,CMD_PDA_CLEAR ,2 , 0 );
131
+ return waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_PDA_HIGHLIGHTCHARS ,3 , 0 );
131
132
}
132
133
133
134
bool loopover_devices (struct aqualinkdata * aq_data ) {
@@ -144,7 +145,7 @@ bool loopover_devices(struct aqualinkdata *aq_data) {
144
145
for (i = 0 ; i < 18 && (index = pda_find_m_index ("ALL OFF" )) == -1 ; i ++ ) {
145
146
send_cmd (KEY_PDA_DOWN );
146
147
//waitForMessage(aq_data, NULL, 1);
147
- waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_MSG_LONG ,8 );
148
+ waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_MSG_LONG ,2 , 0 );
148
149
}
149
150
if (index == -1 ) {
150
151
LOG (PDA_LOG ,LOG_ERR , "loopover_devices :- can't find ALL OFF\n" );
@@ -184,7 +185,7 @@ bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charli
184
185
//delay(500);
185
186
//wait_for_empty_cmd_buffer();
186
187
//waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2);
187
- waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_MSG_LONG ,8 );
188
+ waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_MSG_LONG ,2 , 0 );
188
189
//waitForMessage(aq_data, NULL, 1);
189
190
index = (charlimit == 0 )?pda_find_m_index (menuText ):pda_find_m_index_case (menuText , charlimit );
190
191
if (index >= 0 ) {
@@ -323,7 +324,7 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
323
324
}
324
325
} else if (pda_m_type () == PM_BUILDING_HOME ) {
325
326
LOG (PDA_LOG ,LOG_DEBUG , "goto_pda_menu building home menu\n" );
326
- waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,15 );
327
+ waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,3 , 0 );
327
328
}
328
329
329
330
@@ -429,7 +430,7 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
429
430
LOG (PDA_LOG ,LOG_DEBUG , "PDA in single device mode, \n" );
430
431
ret = select_pda_menu_item (aq_data , "SET TEMP" , false);
431
432
// We could press enter here, but I can't test it, so just wait for message to dissapear.
432
- ret = waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_PDA_HIGHLIGHTCHARS ,25 );
433
+ ret = waitForPDAMessageTypes (aq_data ,CMD_PDA_HIGHLIGHT ,CMD_PDA_HIGHLIGHTCHARS ,5 , 0 );
433
434
//waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10);
434
435
//waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,20,"press ANY key",8);
435
436
}
@@ -517,14 +518,14 @@ void *set_aqualink_PDA_device_on_off( void *ptr )
517
518
} else {
518
519
send_cmd (KEY_PDA_SELECT );
519
520
while (get_aq_cmd_length () > 0 ) { delay (500 ); }
520
- if (!waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,20 )) {
521
+ if (!waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,5 , 0 )) {
521
522
LOG (PDA_LOG ,LOG_ERR , "PDA Device On/Off: %s on - wait for CMD_PDA_HIGHLIGHT\n" ,
522
523
aq_data -> aqbuttons [device ].label );
523
524
}
524
525
}
525
526
} else { // not turning on heater wait for line update
526
527
// worst case spa when pool is running
527
- if (!waitForPDAMessageType (aq_data ,CMD_MSG_LONG ,2 )) {
528
+ if (!waitForPDAMessageType (aq_data ,CMD_MSG_LONG ,0 , 500 )) {
528
529
LOG (PDA_LOG ,LOG_ERR , "PDA Device On/Off: %s on - wait for CMD_MSG_LONG\n" ,
529
530
aq_data -> aqbuttons [device ].label );
530
531
}
@@ -714,74 +715,74 @@ bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex,
714
715
}
715
716
716
717
717
- bool waitForPDAMessageType (struct aqualinkdata * aq_data , unsigned char mtype , int numMessageReceived )
718
+ static bool waitForPDAMessageType (struct aqualinkdata * aq_data , unsigned char mtype ,
719
+ unsigned long sec , unsigned long msec )
718
720
{
719
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageType 0x%02hhx\n" ,mtype );
720
-
721
- int i = 0 ;
722
- pthread_mutex_lock (& aq_data -> active_thread .thread_mutex );
723
-
724
- while ( ++ i <= numMessageReceived )
725
- {
726
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageType 0x%02hhx, last message type was 0x%02hhx (%d of %d)\n" ,mtype ,aq_data -> last_packet_type ,i ,numMessageReceived );
727
-
728
- if (aq_data -> last_packet_type == mtype ) break ;
729
-
730
- pthread_cond_wait (& aq_data -> active_thread .thread_cond , & aq_data -> active_thread .thread_mutex );
731
- }
732
-
733
- pthread_mutex_unlock (& aq_data -> active_thread .thread_mutex );
734
-
735
- if (aq_data -> last_packet_type != mtype ) {
736
- //LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
737
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageType: did not receive 0x%02hhx\n" ,mtype );
738
- return false;
739
- } else
740
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageType: received 0x%02hhx\n" ,mtype );
741
-
742
- return true;
721
+ return waitForPDAMessageTypesOrMenu (aq_data , mtype , 0xFF , sec , msec , NULL , 0 );
743
722
}
744
723
745
724
// Wait for Message, hit return on particular menu.
746
- bool waitForPDAMessageTypesOrMenu (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , int numMessageReceived , char * text , int line )
725
+ bool waitForPDAMessageTypesOrMenu (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , unsigned long sec , unsigned long msec , char * text , int line )
747
726
{
748
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypes 0x%02hhx or 0x%02hhx\n" ,mtype1 ,mtype2 );
727
+ LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,%s,%d,%lu.%03lu sec\n" ,
728
+ mtype1 ,mtype2 ,text ,line ,sec , msec );
749
729
750
730
int i = 0 ;
751
731
bool gotmenu = false;
732
+ struct timespec max_wait ;
733
+ int ret = 0 ;
734
+
735
+ if (msec > 999 )
736
+ {
737
+ LOG (PDA_LOG ,LOG_ERR , "waitForPDAMessageTypesOrMenu INVALID msec value %lu\n" , msec );
738
+ }
739
+ clock_gettime (CLOCK_REALTIME , & max_wait );
740
+ max_wait .tv_sec += sec ;
741
+ max_wait .tv_nsec += msec * 1000000 ;
742
+ if (max_wait .tv_nsec > 999999999L ) {
743
+ max_wait .tv_nsec -= 1000000000L ;
744
+ max_wait .tv_sec ++ ;
745
+ }
752
746
pthread_mutex_lock (& aq_data -> active_thread .thread_mutex );
753
747
754
- while ( ++ i <= numMessageReceived )
755
- {
748
+ while (true) {
749
+ i ++ ;
756
750
if (gotmenu == false && line > 0 && text != NULL ) {
757
751
if (stristr (pda_m_line (line ), text ) != NULL ) {
758
752
send_cmd (KEY_PDA_SELECT );
759
753
gotmenu = true;
760
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypesOrMenu saw '%s' and line %d\n" ,text ,line );
754
+ LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypesOrMenu saw '%s' in line %d\n" ,text ,line );
761
755
}
762
756
}
763
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypes 0x%02hhx | 0x%02hhx, last message type was 0x%02hhx (%d of %d)\n" ,mtype1 ,mtype2 ,aq_data -> last_packet_type ,i ,numMessageReceived );
764
-
765
- if (aq_data -> last_packet_type == mtype1 || aq_data -> last_packet_type == mtype2 ) break ;
766
-
767
- pthread_cond_wait (& aq_data -> active_thread .thread_cond , & aq_data -> active_thread .thread_mutex );
757
+ if (aq_data -> last_packet_type == mtype1 || aq_data -> last_packet_type == mtype2 ) {
758
+ break ;
759
+ }
760
+ LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypesOrMenu last message type 0x%02hhx (%d)\n" , aq_data -> last_packet_type , i );
761
+ if ((ret = pthread_cond_timedwait (& aq_data -> active_thread .thread_cond ,
762
+ & aq_data -> active_thread .thread_mutex , & max_wait ))) {
763
+ LOG (PDA_LOG ,LOG_ERR , "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,%s,%d - %s\n" ,
764
+ mtype1 , mtype2 , text ,line , strerror (ret ));
765
+ break ;
766
+ }
768
767
}
769
768
770
769
pthread_mutex_unlock (& aq_data -> active_thread .thread_mutex );
771
770
772
771
if (aq_data -> last_packet_type != mtype1 && aq_data -> last_packet_type != mtype2 ) {
773
772
//LOG(PDA_LOG,LOG_ERR, "Could not select MENU of Aqualink control panel\n");
774
- LOG (PDA_LOG ,LOG_ERR , "waitForPDAMessageTypes : did not receive 0x%02hhx or 0x%02hhx\n" ,mtype1 ,mtype2 );
773
+ LOG (PDA_LOG ,LOG_ERR , "waitForPDAMessageTypesOrMenu : did not receive 0x%02hhx or 0x%02hhx\n" ,mtype1 ,mtype2 );
775
774
return false;
776
- } else
777
- LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypes : received 0x%02hhx\n" ,aq_data -> last_packet_type );
778
-
775
+ } else {
776
+ LOG (PDA_LOG ,LOG_DEBUG , "waitForPDAMessageTypesOrMenu : received 0x%02hhx\n" ,aq_data -> last_packet_type );
777
+ }
779
778
return true;
780
779
}
781
780
782
- bool waitForPDAMessageTypes (struct aqualinkdata * aq_data , unsigned char mtype1 , unsigned char mtype2 , int numMessageReceived )
781
+ bool waitForPDAMessageTypes (struct aqualinkdata * aq_data , unsigned char mtype1 ,
782
+ unsigned char mtype2 , unsigned long sec ,
783
+ unsigned long msec )
783
784
{
784
- return waitForPDAMessageTypesOrMenu (aq_data , mtype1 , mtype2 , numMessageReceived , NULL , 0 );
785
+ return waitForPDAMessageTypesOrMenu (aq_data , mtype1 , mtype2 , sec , msec , NULL , 0 );
785
786
}
786
787
787
788
bool set_PDA_numeric_field_value (struct aqualinkdata * aq_data , int val , int * cur_val , char * select_label , int step ) {
@@ -798,7 +799,7 @@ bool set_PDA_numeric_field_value(struct aqualinkdata *aq_data, int val, int *cur
798
799
while ( strncasecmp (pda_m_hlight (), select_label , strlen (select_label )) != 0 ) {
799
800
send_cmd (KEY_PDA_DOWN );
800
801
delay (500 ); // Last message probably was CMD_PDA_HIGHLIGHT, so wait before checking.
801
- waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,2 );
802
+ waitForPDAMessageType (aq_data ,CMD_PDA_HIGHLIGHT ,0 , 500 );
802
803
if (i > 10 ) {
803
804
LOG (PDA_LOG ,LOG_ERR , "PDA numeric selector could not find string '%s'\n" , select_label );
804
805
return false;
0 commit comments