📄 s1240_drv.c
字号:
#include <poll.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <time.h>#include "simdrv.h"#include "simu112.h"#include "simufunc.h"#define MAX_FAIL_TIMES 13#define WAIT_TIME 5000#define RESULT_OTHER -4#define RESULT_NULL -5#define RESULT_BUSY -6#define RESULT_TIMEOUT -7FILE *lp;static void send_break(int sock);int f_s1240_result_receive(int sock, char *szBuffer, char *end_str1, char *end_str2, int wait_time);int f_s1240_send_wakeup(int port_fd, TESTREQMSG * testReqMsg);int f_s1240_test_process(int port_fd, char *phone_number, TESTMSG * testmsg, int version);int f_s1240_analysis(char *receive_str, TESTMSG * testmsg);void f_s1240_get_res(char *result, char *val_str);void f_s1240_get_cap(char *result, char *val_str);void f_s1240_get_vol(char *result, char *val_str);int f_s1240_makepassword(char *password);int f_s1240_send_ctrlX(int port_fd);int f_s1240_make518_7x_cmd(char *test_str, char *phone_number);int f_s1240_make518_5x_cmd(char *test_str, char *phone_number);static char anstr[] ={2, 'A', 'I', 'V', 'T', '@', '@', ' ', ' ', ' ', 4, 6};static char receive_str[4096];/*********************************************************Function:int s1240_proc(int port_fd, TESTREQMSG * testReqMsg, TESTMSG * testmsg)Narrative: Called in simdrv.c. It is the main flow of s1240_drv driver.Param: int port_fd - socket id TESTREQMSG *testReqMsg - struct of test request message TESTMSG * testmsg - struct of test message int version - the software version of s1240 switchReturn: 0 OK or Null TelephoneNumber or Line Busy. AM_UNREADY_ERROR AM_NO_RESPONSE AM_PORT_DOWN AM_UNREADY_ERROR AM_TIME_OUT*********************************************************/int s1240_proc(int port_fd, TESTREQMSG * testReqMsg, TESTMSG * testmsg, int version){ int ret; int test_stat = 1; int fail_count; struct tm *ctime; time_t lt; char *phone_number; char *password;#ifdef DEBUG lt = time(NULL); ctime = localtime(<); lp = backup_log("./s1240_drv.log"); fprintf(lp, "\n>>> Start s1240 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 password = testReqMsg->PortPassword; phone_number = testReqMsg->TeleNumber; ret = f_s1240_send_wakeup(port_fd, testReqMsg); if (!ret) ret = f_s1240_test_process(port_fd, phone_number, testmsg, version);#ifdef DEBUG fprintf(lp, "ret=%d\n", ret); fflush(lp);#endif#ifdef DEBUG lt = time(NULL); ctime = localtime(<); fprintf(lp, "\n>>> Stop s1240 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 s1240_drv *//*********************************************************Function:int f_s1240_send_wakeup(int port_fd, TESTREQMSG * testReqMsg)Narrative: Active switch port by sending wakeup signal.Param: int port_fd - socket id TESTREQMSG *testReqMsg - struct of test request messageReturn: 0 OK. RESULT_OTHER No successful.*********************************************************/int f_s1240_send_wakeup(int port_fd, TESTREQMSG * testReqMsg){ int ret; int i; char send_str[50]; int stat; char *password; char *userid; userid = testReqMsg->SwitchUserName; password = testReqMsg->PortPassword; f_trim_space(userid); f_s1240_makepassword(userid); f_trim_space(password); f_s1240_makepassword(password); /* send Break string to switch port */ i = 0; stat = -1; while (i < MAX_FAIL_TIMES) {#ifdef DEBUG fprintf(lp, "Send Break..."); fflush(lp);#endif send_break(port_fd); ret = f_s1240_result_receive(port_fd, receive_str, "<", NULL, 2000); if (strstr(receive_str, "USERID <") != NULL) { poll(0,0,1000); stat = 0; break; } else if (strstr(receive_str, "PASSWORD <") != NULL) { poll(0,0,1000); stat = 1; break; } else if (strstr(receive_str, "<") != NULL) { poll(0,0,2000); stat = 2; break; } ++i; } if (stat < 0) {#ifdef DEBUG fprintf(lp, "Send Break Fail!,fail_counter=%d\n", i); fflush(lp);#endif f_s1240_send_ctrlX(port_fd); return (RESULT_OTHER); } /* send LOGIN */ else if (stat == 0) {#ifdef DEBUG /* fprintf(lp, "Send USER ID:%s", userid); */ fprintf(lp, "Send USER ID:%s", "********"); fflush(lp);#endif writeport(port_fd, userid); ret = f_s1240_result_receive(port_fd, receive_str, "<", NULL, 5000); if (strstr(receive_str, "PASSWORD <") == NULL) { f_s1240_send_ctrlX(port_fd); return (RESULT_OTHER); } else { poll(0,0,1000); stat = 1; } } if (stat == 1) {#ifdef DEBUG /* fprintf(lp, "Send PASSWORD:%s", password); */ fprintf(lp, "Send PASSWORD:%s", "********"); fflush(lp);#endif writeport(port_fd, password); ret = f_s1240_result_receive(port_fd, receive_str, "<", NULL, 10000); if (strstr(receive_str, "<") == NULL) { f_s1240_send_ctrlX(port_fd); return (RESULT_OTHER); } else { poll(0,0,2000); stat = 2; } } if (stat == 2) { return (0); } else return (RESULT_OTHER);}/* end of f_s1240_send_wakeup *//*********************************************************Function:int f_s1240_test_process(int port_fd, char *phone_number, TESTMSG * testmsg, int version)Narrative: Send test command to switch and get test value.Param: int port_fd - socket id char *phone_number - char pointer to accessed telephone number string TESTMSG * testmsg - struct of test message int version - the software version of s1240 switchReturn: 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_NOTSUPPORT No supported test type or command.*********************************************************/int f_s1240_test_process(int port_fd, char *phone_number, TESTMSG * testmsg, int version){ char test_str[50]; int ret; int i; f_trim_space(phone_number); /* send access line string to switch port */ if (version == S1240J) f_s1240_make518_7x_cmd(test_str, phone_number); else f_s1240_make518_5x_cmd(test_str, phone_number);#ifdef DEBUG fprintf(lp, "send:%s\n", test_str); fflush(lp);#endif writeport(port_fd, test_str); ret = f_s1240_result_receive(port_fd, receive_str, "LAST REPORT", "ARGUMENT SEMANTIC ERROR", 60000); if (ret != 0) /* if fail then return */ { return ret; } else { if (strstr(receive_str, "ARGUMENT SEMANTIC ERROR") || strstr(receive_str, "LINE NOT EQUIPPED") || strstr(receive_str, "DN NOT IN PLAN")) { ret = -5; /* Null Telphone Number */ return ret; } else if ( strstr(receive_str, "LINE BUSY") || strstr(receive_str, "TEST DEVICE TAU BUSY") || strstr(receive_str, "WRONG LINE STATE") || strstr(receive_str, "TEST TERMINATED ABNORMALLY") ) { ret = -6; /* Line Busy */ return ret; } } ret = f_s1240_analysis(receive_str, testmsg); /* send ^X */ f_s1240_send_ctrlX(port_fd); return (ret);} /* end f_s1240_test_process *//*********************************************************Function:int f_s1240_result_receive(int port_fd, char *szBuffer, char *end_str1, char *end_str2, int wait_time)Narrative: Recieve responded string from switch after sending command.Param: int port_fd - socket id char *szBuffer - a pointer to receive_str char *end_str1 - expected end lable1 of recieved stirng char *end_str2 - expected end lable2 of recieved stirng int wait_time - poll system call waiting timeReturn: 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_s1240_result_receive(int sock, char *szBuffer, char *end_str1, char *end_str2, int wait_time){ int p; int retval, ret, rc; char ch; 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); if (rc < 0) { retval = -3; break; } if (rc == 0) { retval = -1; break; } if (ch != 0) { if (p > 4090) p = 0; szBuffer[p] = ch; ++p; szBuffer[p] = '\0';/* #ifdef DEBUG fprintf(lp,"%02x,%c\n",(unsigned char)ch,ch); fflush(lp); #endif */ } if (ch == 0x03 || ch == '<') {#ifdef DEBUG fprintf(lp, "\n Receive 0x%02X\n", (unsigned char) ch); fflush(lp);#endif if (end_str1 != NULL) { if (strstr(szBuffer, end_str1) != NULL) {#ifdef DEBUG fprintf(lp, "\n Receive end_str1\n"); fflush(lp);#endif retval = 0; break; } } if (end_str2 != NULL) { if (strstr(szBuffer, end_str2) != NULL) {#ifdef DEBUG fprintf(lp, "\n Receive end_str2\n"); fflush(lp);#endif retval = 0; break; } } } if (ch == 0x17) /* ^W */ {#ifdef DEBUG fprintf(lp, "\n Receive ^W"); fflush(lp);#endif } if (ch == 0x1B) /* ^[ */ {#ifdef DEBUG fprintf(lp, "\n Receive ^["); fflush(lp);#endif } if (ch == 0x05) /* ^E */ {#ifdef DEBUG fprintf(lp, "\n Receive ^E\n"); fflush(lp);#endif if (p > 1 && (szBuffer[p - 2] == 0x17 || szBuffer[p - 2] == 0x1B)) {#ifdef DEBUG fprintf(lp, "\n Receive ^W^E or ^[^E, write answer string\n"); fflush(lp);#endif writeport(sock, anstr); poll(0, 0, 500); send_break(sock); } } } }#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"); fflush(lp);#endif break; case -1:#ifdef DEBUG fprintf(lp, "Receive 0, exit\n"); fflush(lp);#endif break; case -2:#ifdef DEBUG fprintf(lp, "Server no respone!\n"); fflush(lp);#endif retval = 0; break; case -3:#ifdef DEBUG fprintf(lp, "\nServer port down!\n"); fflush(lp);#endif break; } return (retval);}/*********************************************************Function:int f_s1240_analysis(char *receive_str, TESTMSG * testmsg)Narrative: Get 12 test values from Received stirng from switch.Param: char *receive_str - an pointer to receive_str TESTMSG * testmsg - struct of test messageReturn: 0 OK. RESULT_OTHER No expected string.*********************************************************/int f_s1240_analysis(char *receive_str, TESTMSG * testmsg){ char temp_str[30]; char *p, *q, *r; p = (char *) strstr(receive_str, "RESULT FOLLOWS"); if (!p) return (RESULT_OTHER); else { q = strstr(receive_str, "POT DC POT AC"); if (q == NULL) return (RESULT_OTHER); /* get AB DCVol and ACVol */ p = strstr(q, "A-B");/* skip this *//* if (p!=NULL) { memset(temp_str,0,30); strncpy(temp_str,p+12,20); f_s1240_get_vol(testmsg->TestResult.TR2_DCvol,temp_str); #ifdef DEBUG fprintf(lp,"TR2_DCvol=%s\n",testmsg->TestResult.TR2_DCvol); fflush(lp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -