From d1ca60b0daa17eddbb485e7af1ac6c3d227cecf4 Mon Sep 17 00:00:00 2001 From: Lee Ballard Date: Fri, 14 Jun 2019 14:47:20 -0500 Subject: [PATCH] ballle98/AqualinkD#21: unpredictable waitForPDAMessageType added 45 second delay --- pda_aq_programmer.c | 116 ++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/pda_aq_programmer.c b/pda_aq_programmer.c index dd96cd93..9f4a5b3f 100644 --- a/pda_aq_programmer.c +++ b/pda_aq_programmer.c @@ -40,9 +40,10 @@ #endif bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, int numMessageReceived); -bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, int numMessageReceived); -bool waitForPDAMessageTypes(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived); -bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived, char *text, int line); +static bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, + unsigned long sec, unsigned long msec); +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 long sec, unsigned long msec, char *text, int line); 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); @@ -112,11 +113,8 @@ void pda_programming_thread_check(struct aqualinkdata *aq_data) 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,10); - i++; + while (pda_m_hlightindex() == -1){ + waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2,0); } if (pda_m_hlightindex() == -1) @@ -126,8 +124,8 @@ bool wait_pda_selected_item(struct aqualinkdata *aq_data) } bool waitForPDAnextMenu(struct aqualinkdata *aq_data) { - waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10); - return waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,15); + waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,2,0); + return waitForPDAMessageTypes(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,3,0); } bool loopover_devices(struct aqualinkdata *aq_data) { @@ -167,7 +165,7 @@ bool find_pda_menu_item(struct aqualinkdata *aq_data, char *menuText, int charli send_cmd(KEY_PDA_DOWN); //delay(500); //wait_for_empty_cmd_buffer(); - waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2); + waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,0,500); //waitForMessage(aq_data, NULL, 1); index = (charlimit == 0)?pda_find_m_index(menuText):pda_find_m_index_case(menuText, charlimit); if (index >= 0) { @@ -238,7 +236,7 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) { //logMessage(LOG_DEBUG, "******************PDA Device programmer selected back button\n",menu); waitForPDAnextMenu(aq_data); } else { - waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,15); + waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,3,0); } if (i > 4 ) { @@ -296,8 +294,8 @@ bool goto_pda_menu(struct aqualinkdata *aq_data, pda_menu_type menu) { select_pda_menu_item(aq_data, "MENU", true); // Depending on control panel config, may get an extra menu asking to press any key. select_pda_menu_item(aq_data, "SET TEMP", false); - waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,10); - waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,20,"press ANY key",8); + waitForPDAMessageType(aq_data,CMD_PDA_CLEAR,2,0); + waitForPDAMessageTypesOrMenu(aq_data,CMD_PDA_HIGHLIGHT,CMD_PDA_HIGHLIGHTCHARS,5,0,"press ANY key",8); break; case PM_SET_TIME: select_pda_menu_item(aq_data, "MENU", true); @@ -547,78 +545,80 @@ bool waitForPDAMessageHighlight(struct aqualinkdata *aq_data, int highlighIndex, } -bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, int numMessageReceived) +static bool waitForPDAMessageType(struct aqualinkdata *aq_data, unsigned char mtype, + unsigned long sec, unsigned long msec) { - logMessage(LOG_DEBUG, "waitForPDAMessageType 0x%02hhx\n",mtype); - - int i=0; - pthread_mutex_init(&aq_data->active_thread.thread_mutex, NULL); - pthread_mutex_lock(&aq_data->active_thread.thread_mutex); - - while( ++i <= numMessageReceived) - { - logMessage(LOG_DEBUG, "waitForPDAMessageType 0x%02hhx, last message type was 0x%02hhx (%d of %d)\n",mtype,aq_data->last_packet_type,i,numMessageReceived); - - if (aq_data->last_packet_type == mtype) break; - - pthread_cond_init(&aq_data->active_thread.thread_cond, NULL); - 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 (aq_data->last_packet_type != mtype) { - //logMessage(LOG_ERR, "Could not select MENU of Aqualink control panel\n"); - logMessage(LOG_DEBUG, "waitForPDAMessageType: did not receive 0x%02hhx\n",mtype); - return false; - } else - logMessage(LOG_DEBUG, "waitForPDAMessageType: received 0x%02hhx\n",mtype); - - return true; + return waitForPDAMessageTypesOrMenu(aq_data, mtype, 0xFF, sec, msec, NULL, 0); } // Wait for Message, hit return on particular menu. -bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, int numMessageReceived, char *text, int line) +bool waitForPDAMessageTypesOrMenu(struct aqualinkdata *aq_data, unsigned char mtype1, unsigned char mtype2, unsigned long sec, unsigned long msec, char *text, int line) { - logMessage(LOG_DEBUG, "waitForPDAMessageTypes 0x%02hhx or 0x%02hhx\n",mtype1,mtype2); + logMessage(LOG_DEBUG, "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,%s,%d,%lu.%03lu sec\n", + mtype1,mtype2,text,line,sec, msec); int i=0; bool gotmenu = false; + struct timespec max_wait; + int ret = 0; + + if (msec > 999) + { + logMessage(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_init(&aq_data->active_thread.thread_mutex, NULL); pthread_mutex_lock(&aq_data->active_thread.thread_mutex); - while( ++i <= numMessageReceived) - { + while (true) { + i++; + pthread_cond_init(&aq_data->active_thread.thread_cond, NULL); + if ((ret = pthread_cond_timedwait(&aq_data->active_thread.thread_cond, + &aq_data->active_thread.thread_mutex, &max_wait))) + { + logMessage(LOG_ERR, "waitForPDAMessageTypesOrMenu 0x%02hhx,0x%02hhx,%s,%d - %s\n", + mtype1, mtype2, 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; - logMessage(LOG_DEBUG, "waitForPDAMessageTypesOrMenu saw '%s' and line %d\n",text,line); + logMessage(LOG_DEBUG, "waitForPDAMessageTypesOrMenu saw '%s' in line %d\n",text,line); } } - logMessage(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); - - if (aq_data->last_packet_type == mtype1 || aq_data->last_packet_type == mtype2) break; + if (aq_data->last_packet_type == mtype1 || aq_data->last_packet_type == mtype2) + { + break; + } + logMessage(LOG_DEBUG, "waitForPDAMessageTypesOrMenu last message type 0x%02hhx (%d)\n", aq_data->last_packet_type, i); - pthread_cond_init(&aq_data->active_thread.thread_cond, NULL); - 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 (aq_data->last_packet_type != mtype1 && aq_data->last_packet_type != mtype2) { //logMessage(LOG_ERR, "Could not select MENU of Aqualink control panel\n"); - logMessage(LOG_ERR, "waitForPDAMessageTypes: did not receive 0x%02hhx or 0x%02hhx\n",mtype1,mtype2); + logMessage(LOG_ERR, "waitForPDAMessageTypesOrMenu: did not receive 0x%02hhx or 0x%02hhx\n",mtype1,mtype2); return false; - } else - logMessage(LOG_DEBUG, "waitForPDAMessageTypes: received 0x%02hhx\n",aq_data->last_packet_type); - + } else { + logMessage(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, int numMessageReceived) +bool waitForPDAMessageTypes(struct aqualinkdata *aq_data, unsigned char mtype1, + unsigned char mtype2, unsigned long sec, + unsigned long msec) { - return waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, numMessageReceived, NULL, 0); + return waitForPDAMessageTypesOrMenu(aq_data, mtype1, mtype2, sec, msec, NULL, 0); } bool set_PDA_numeric_field_value(struct aqualinkdata *aq_data, int val, int *cur_val, char *select_label, int step) { @@ -629,7 +629,7 @@ bool set_PDA_numeric_field_value(struct aqualinkdata *aq_data, int val, int *cur while ( strncasecmp(pda_m_hlight(), select_label, 8) != 0 ) { send_cmd(KEY_PDA_DOWN); delay(500); // Last message probably was CMD_PDA_HIGHLIGHT, so wait before checking. - waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,2); + waitForPDAMessageType(aq_data,CMD_PDA_HIGHLIGHT,0,500); if (i > 10) { logMessage(LOG_ERR, "PDA numeric selector could not find string '%s'\n", select_label); return false;