📄 5ess_drv.c
字号:
#include <poll.h>#include <stdio.h>#include <stdlib.h>#include "simdrv.h"#include "simu112.h"#include "simufunc.h"#define MAX_FAIL_TIMES 4#define WAIT_TIME 60000#define RESULT_OTHER -4#define RESULT_NULL -5#define RESULT_BUSY -6#define RESULT_TIMEOUT -7FILE *lp;int result_receive_connect(int sock, char *szBuffer, char *phone_number);int result_receive_op_rslt(int sock, char *szBuffer);int f_5ess_result_receive(int sock, char *szBuffer);int f_5ess_send_wakeup(int port_fd);int f_set_wspos(int port_fd);int f_5ess_test_process(int port_fd, char *phone_number, TESTMSG * testmsg);int f_5ess_analysis(char *receive_str, TESTMSG * testmsg);void f_5ess_get_res(char *result, char *val_str);void f_5ess_get_cap(char *result, char *val_str);void f_5ess_get_vol(char *result, char *val_str);static char receive_str[2048];/*********************************************************Function:int att_5ess_proc(int port_fd, char *phone_number, TESTMSG * testmsg)Narrative: Called in simdrv.c. It is the main flow of 5ess_drv driver.Param: int port_fd - socket id char *phone_number - a pointer to accessed telephoen number TESTMSG * testmsg - struct of test messageReturn: 0 OK or Null TelephoneNumber or Line Busy. AM_NO_RESPONSE AM_PORT_DOWN AM_UNREADY_ERROR AM_TIME_OUT*********************************************************/int att_5ess_proc(int port_fd, char *phone_number, TESTMSG * testmsg){ int ret; int test_stat = 1; int fail_count; struct tm *ctime; time_t lt;#ifdef DEBUG lt = time(NULL); ctime = localtime(<); lp = backup_log("./5ess_drv.log"); /* lp=fopen("./5ess_drv.log","a+"); */ fprintf(lp, "\n>>> Start AT&T5ESS Test at %4d.%02d.%02d %02d:%02d:%02d <<<\n", ctime->tm_year > 90 ? ctime->tm_year + 1900 : ctime->tm_year + 2000, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); fflush(lp);#endif fail_count = 0; while (fail_count < MAX_FAIL_TIMES) { ret = f_5ess_send_wakeup(port_fd); if (!ret) { ret = f_set_wspos(port_fd); if (!ret) { ret = f_5ess_test_process(port_fd, phone_number, testmsg); break; } else ++fail_count; } else ++fail_count;#ifdef DEBUG fprintf(lp, "ret=%d, fail_count=%d\n", ret, fail_count); fflush(lp);#endif }#ifdef DEBUG lt = time(NULL); ctime = localtime(<); fprintf(lp, "\n>>> Stop AT&T5ESS Test at %4d.%02d.%02d %02d:%02d:%02d <<<\n", ctime->tm_year > 90 ? ctime->tm_year + 1900 : ctime->tm_year + 2000, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); fflush(lp); fclose(lp);#endif switch (ret) { case -1: ret = AM_UNREADY_ERROR; break; case -2: ret = AM_NO_RESPONSE; break; case -3: ret = AM_PORT_DOWN; break; case -4: ret = AM_UNREADY_ERROR; break; case -5: ret = 0; strcpy(testmsg->TestResult.TestConclusion, "V08"); break; case -6: ret = 0; strcpy(testmsg->TestResult.TestConclusion, "V6"); break; case -7: ret = AM_TIME_OUT; break; } return (ret);}/* end of att_5ess_drv *//*********************************************************Function:int att_5ess_conn_switch(int port_fd)Narrative: Detect if interface to switch runs well by sending a wakeup signal or a command.Param: int port_fd - socket id Return: 0 OK. -1 No fd ready when timeout. -2 Connect Switch OK, but wake up fail.*********************************************************/int att_5ess_conn_switch(int port_fd){ int ret_val; struct tm *ctime; time_t lt;#ifdef DEBUG lt = time(NULL); ctime = localtime(<); lp = backup_log("./5ess_drv.log"); fprintf(lp, "\n>>> Start AT&T5ESS Test at %4d.%02d.%02d %02d:%02d:%02d <<<\n", ctime->tm_year > 90 ? ctime->tm_year + 1900 : ctime->tm_year + 2000, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); fflush(lp);#endif receive_str[0] = 0; if (!f_5ess_send_wakeup(port_fd)) ret_val = 0; else if (strlen(receive_str) == 0) ret_val = -1; else ret_val = -2;#ifdef DEBUG lt = time(NULL); ctime = localtime(<); fprintf(lp, "\n>>> Stop AT&T5ESS Test at %4d.%02d.%02d %02d:%02d:%02d <<<\n", ctime->tm_year > 90 ? ctime->tm_year + 1900 : ctime->tm_year + 2000, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); fflush(lp); fclose(lp);#endif return ret_val;}/*********************************************************Function:int f_5ess_send_wakeup(int port_fd)Narrative: Active switch port by sending wakeup signal.Param: int port_fd - socket idReturn: 0 OK. -1 No expected string. -2 No fd ready when timeout. -3 An error occur when performing poll or read.*********************************************************/int f_5ess_send_wakeup(int port_fd){ struct pollfd pollfdsp[1]; char ch, str[256]; int p, rc, ret; pollfdsp[0].fd = port_fd; pollfdsp[0].events = POLLIN; pollfdsp[0].revents = 0; /* send Wakeup string to switch port */ /* fprintf(lp,"send: %s\n",str); */ writeport(port_fd, ";"); sleep(1); sprintf(str, "%c", 7); writeport(port_fd, str); p = 0; while (1) { ret = poll(pollfdsp, 1, 1000); if (ret < 0) return (-3); if (ret == 0) return (-2); if (pollfdsp[0].revents & POLLIN) { rc = read(port_fd, &ch, 1); /* fprintf(lp,"ret=%d,rc=%d ch=%c\n",ret,rc,ch); */ if (rc <= 0) return (-3); if (ch <= 0) continue; receive_str[p] = ch; ++p; receive_str[p] = 0; if (ch == '<') {#ifdef DEBUG fprintf(lp, "\nsend Wakeup OK!\n"); fflush(lp);#endif return (0); } } }}/* end of f_5ess_send_wakeup *//*********************************************************Function:int f_set_wspos(int port_fd)Narrative: Send set-wspos string to switch port.Param: int port_fd - socket idReturn: 0 OK. -1 An error of hangup occur when reading. -2 No fd ready when timeout. -3 An error occur when performing poll or read.*********************************************************/int f_set_wspos(int port_fd){ char str[256]; int ret; /* send set-wspos string to switch port */ writeport(port_fd, "set-wspos:tp=1;"); ret = f_5ess_result_receive(port_fd, str); return (ret);}/* end of f_set_wspos *//*********************************************************Function:int f_5ess_test_process(int port_fd, char *phone_number, TESTMSG * testmsg)Narrative: Send test command to switch and get test value.Param: int port_fd - socket id char *phone_number - a pointer to accessed telephoen number TESTMSG * testmsg - struct of test messageReturn: 0 OK. -1 An error of hangup occur when reading. -2 No fd ready when timeout. -3 An error occur when performing poll or read.*********************************************************/int f_5ess_test_process(int port_fd, char *phone_number, TESTMSG * testmsg){ char test_str[30]; /* char receive_str[2048]; */ int ret; int i; f_trim_space(phone_number); sprintf(test_str, "conn-tstdn:dn=%s;", phone_number); /* connect line */ /* send test string to switch port */ writeport(port_fd, test_str); ret = result_receive_connect(port_fd, receive_str, phone_number); if (ret != 0) /* if fail then return */ goto _exit_5ess; sprintf(test_str, "set-tcat:category=ins;"); /* set resistance */ writeport(port_fd, test_str); ret = f_5ess_result_receive(port_fd, receive_str); sprintf(test_str, "set-tcat:category=cap;"); /* set capacitance */ writeport(port_fd, test_str); ret = f_5ess_result_receive(port_fd, receive_str); sprintf(test_str, "set-tcat:category=fvt;"); /* set voltage */ writeport(port_fd, test_str); ret = f_5ess_result_receive(port_fd, receive_str); sprintf(test_str, "exec-onkt:;"); /* start test */ writeport(port_fd, test_str); ret = f_5ess_result_receive(port_fd, receive_str); i = 0; while (i < 10) { sleep(5); sprintf(test_str, "op-rslt;"); /* send test string to switch port */ writeport(port_fd, test_str); ret = result_receive_op_rslt(port_fd, receive_str); if (ret != RESULT_BUSY) break; ++i; } if (!ret) { ret = f_5ess_analysis(receive_str, testmsg); }_exit_5ess: sleep(1); sprintf(test_str, "disc-lut;"); /* release line */ writeport(port_fd, test_str); f_5ess_result_receive(port_fd, receive_str);#ifdef DEBUG fprintf(lp, "\nret=%d!\n", ret); fflush(lp);#endif return (ret);}/* end f_5ess_test_process *//*********************************************************Function:int result_receive_connect(int sock, char *szBuffer, char *phone_number)Narrative: Send test command to switch and get test value.Param: int sock - socket id char *szBuffer - a pointer to received string char *phone_number - a pointer to accessed telephone numberReturn: 0 OK. -1 An error of hangup occur when reading. -2 No fd ready when timeout. -3 An error occur when performing poll or read. RESULT_OTHER Fail for other reason. RESULT_NULL Invalid telephone number. RESULT_BUSY Line busy.*********************************************************/int result_receive_connect(int sock, char *szBuffer, char *phone_number){ int p; int retval, ret, rc; char ch; char finish_str[30] = ""; char error_str[] = "LINE NOT CONNECTED"; char busy_str1[] = "LINE IS BUSY"; char busy_str2[] = "NO SERVICEABLE SLIM UNIT AVAILABLE"; char fail_str1[] = "COULD NOT INTERPRET INPUT"; char fail_str2[] = "CONGESTION OR ROUTE FAIL"; struct pollfd pollfdsp[1]; sprintf(finish_str, "CONN : DN %s", phone_number); pollfdsp[0].fd = sock; pollfdsp[0].events = POLLIN; pollfdsp[0].revents = 0; retval = 0; p = 0; szBuffer[p] = '\0'; while (1) { ret = poll(pollfdsp, 1, WAIT_TIME); if (ret < 0) { retval = -3; break; } else if (ret == 0) { retval = -2; break; } if (pollfdsp[0].revents & POLLIN) { rc = read(sock, &ch, 1); if (rc < 0) { retval = -3; break; } if (rc == 0) { retval = -1; break; } if (ch != 0) { szBuffer[p] = ch; ++p; /* fprintf(lp,"%02x,%c\n",ch,ch); */ } if (ch == 0x0d && p > 17) { szBuffer[p] = '\0'; if (strstr(szBuffer, finish_str)) {#ifdef DEBUG fprintf(lp, "\nFinish\n");#endif retval = 0; break; } else if (strstr(szBuffer, busy_str1) || strstr(szBuffer, busy_str2)) {#ifdef DEBUG fprintf(lp, "\nBusy\n");#endif retval = RESULT_BUSY; /* 忙 */ break; } else if (strstr(szBuffer, error_str)) {#ifdef DEBUG fprintf(lp, "\nError\n");#endif retval = RESULT_NULL; /* 空号 */ break; } else if (strstr(szBuffer, fail_str1) || strstr(szBuffer, fail_str2)) {#ifdef DEBUG fprintf(lp, "\nFail\n");#endif retval = RESULT_OTHER; /* 其它原因 */ break; } } } } szBuffer[p] = '\0';#ifdef DEBUG fprintf(lp, "Server Replied:p=%d\n%s\n", p, szBuffer); fflush(lp);#endif switch (retval) { case 0:#ifdef DEBUG fprintf(lp, "\nReceive OK!\n");#endif break; case -1:#ifdef DEBUG fprintf(lp, "Receive 0, exit\n");#endif break; case -2:#ifdef DEBUG fprintf(lp, "Server no respone!\n");#endif break; case -3:#ifdef DEBUG fprintf(lp, "\nServer port down!\n");#endif break; } return (retval);}/*********************************************************Function:int f_5ess_result_receive(int sock, char *szBuffer)Narrative: Recieve responded string after send command.Param: int sock - socket id char *szBuffer - a pointer to array receive_strReturn: 0 OK. -1 an error of hangup occur when reading. -2 No fd ready when timeout. -3 an error occur when performing poll or read. RESULT_OTHER Fail for other reason.*********************************************************/int f_5ess_result_receive(int sock, char *szBuffer){ int p; int retval, ret, rc; char ch; char finish_str[30] = " OK"; char fail_str[] = "COULD NOT INTERPRET INPUT"; struct pollfd pollfdsp[1]; pollfdsp[0].fd = sock; pollfdsp[0].events = POLLIN; pollfdsp[0].revents = 0; retval = 0; p = 0; szBuffer[p] = '\0'; while (1) { ret = poll(pollfdsp, 1, WAIT_TIME); if (ret < 0) { retval = -3; break; } else if (ret == 0) { retval = -2; break; } if (pollfdsp[0].revents & POLLIN) { rc = read(sock, &ch, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -