Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d4f60d7

Browse files
committedSep 15, 2020
ballle98#21: unpredictable waitForPDAMessageType added 45 second delay
1 parent 7282370 commit d4f60d7

File tree

1 file changed

+56
-55
lines changed

1 file changed

+56
-55
lines changed
 

‎pda_aq_programmer.c

+56-55
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
#endif
4141

4242
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);
4647
bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu);
4748
bool wait_pda_selected_item(struct aqualinkdata *aq_data);
4849
bool waitForPDAnextMenu(struct aqualinkdata *aq_data);
@@ -114,8 +115,8 @@ bool wait_pda_selected_item(struct aqualinkdata *aq_data)
114115
{
115116
int i=0;
116117

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);
119120
i++;
120121
}
121122

@@ -126,8 +127,8 @@ bool wait_pda_selected_item(struct aqualinkdata *aq_data)
126127
}
127128

128129
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);
131132
}
132133

133134
bool loopover_devices(struct aqualinkdata *aq_data) {
@@ -144,7 +145,7 @@ bool loopover_devices(struct aqualinkdata *aq_data) {
144145
for (i=0; i < 18 && (index = pda_find_m_index("ALL OFF")) == -1 ; i++) {
145146
send_cmd(KEY_PDA_DOWN);
146147
//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);
148149
}
149150
if (index == -1) {
150151
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
184185
//delay(500);
185186
//wait_for_empty_cmd_buffer();
186187
//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);
188189
//waitForMessage(aq_data, NULL, 1);
189190
index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit);
190191
if (index >= 0) {
@@ -323,7 +324,7 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
323324
}
324325
} else if (pda_m_type() == PM_BUILDING_HOME) {
325326
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);
327328
}
328329

329330

@@ -429,7 +430,7 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) {
429430
LOG(PDA_LOG,LOG_DEBUG, "PDA in single device mode, \n");
430431
ret = select_pda_menu_item(aq_data, "SET TEMP", false);
431432
// 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);
433434
//waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10);
434435
//waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,20,"press ANY key",8);
435436
}
@@ -517,14 +518,14 @@ void *set_aqualink_PDA_device_on_off( void *ptr )
517518
} else {
518519
send_cmd(KEY_PDA_SELECT);
519520
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)) {
521522
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - wait for CMD_PDA_HIGHLIGHT\n",
522523
aq_data->aqbuttons[device].label);
523524
}
524525
}
525526
} else { // not turning on heater wait for line update
526527
// 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)) {
528529
LOG(PDA_LOG,LOG_ERR, "PDA Device On/Off: %s on - wait for CMD_MSG_LONG\n",
529530
aq_data->aqbuttons[device].label);
530531
}
@@ -714,74 +715,74 @@ bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex,
714715
}
715716

716717

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)
718720
{
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);
743722
}
744723

745724
// 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)
747726
{
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);
749729

750730
int i=0;
751731
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+
}
752746
pthread_mutex_lock(&aq_data->active_thread.thread_mutex);
753747

754-
while( ++i <= numMessageReceived)
755-
{
748+
while (true) {
749+
i++;
756750
if (gotmenu == false && line > 0 && text != NULL) {
757751
if (stristr(pda_m_line(line), text) != NULL) {
758752
send_cmd(KEY_PDA_SELECT);
759753
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);
761755
}
762756
}
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+
}
768767
}
769768

770769
pthread_mutex_unlock(&aq_data->active_thread.thread_mutex);
771770

772771
if (aq_data->last_packet_type != mtype1 && aq_data->last_packet_type != mtype2) {
773772
//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);
775774
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+
}
779778
return true;
780779
}
781780

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)
783784
{
784-
return waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, numMessageReceived, NULL, 0);
785+
return waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, sec, msec, NULL, 0);
785786
}
786787

787788
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
798799
while ( strncasecmp(pda_m_hlight(), select_label, strlen(select_label)) != 0 ) {
799800
send_cmd(KEY_PDA_DOWN);
800801
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);
802803
if (i > 10) {
803804
LOG(PDA_LOG,LOG_ERR, "PDA numeric selector could not find string '%s'\n", select_label);
804805
return false;

0 commit comments

Comments
 (0)
Please sign in to comment.