?? dms100_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 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_dms100_result_receive(int sock, char *szBuffer, char *end_str1, char *end_str2, int wait_time);int f_dms100_send_wakeup(int port_fd, char *username, char *password);int f_dms100_test_process(int port_fd, char *phone_number, TESTMSG * testmsg);int f_dms100_analysis(char *receive_str, TESTMSG * testmsg);void f_dms100_get_res(char *result, char *val_str);void f_dms100_get_cap(char *result, char *val_str);void f_dms100_get_vol(char *result, char *val_str);static int f_dms100_is_lock(char *buffer);static char receive_str[2048];/*********************************************************Function:int dms100_proc(int port_fd, char *phone_number, char *username, char *password, TESTMSG * testmsg)Narrative: Called in simdrv.c. It is the main flow of dms100_drv driver.Param: int port_fd - socket id char *phone_number - char pointer to accessed telephone number string char *username - char pointer to login name of switch port char *password - char pointer to switch port password TESTMSG * testmsg - struct of test messageReturn: 0 OK or Null TelephoneNumber or Line Busy. AM_UNREADY_ERROR AM_NO_RESPONSE AM_PORT_DOWN AM_UNREADY_ERROR AM_TIME_OUT*********************************************************/int dms100_proc(int port_fd, char *phone_number, char *username, char *password, 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("./dms100_drv.log"); /* lp=fopen("./dms100_drv.log","a+"); */ fprintf(lp, "\n>>> Start DMS100 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; ret = 1; while (fail_count < MAX_FAIL_TIMES && ret) { ret = f_dms100_send_wakeup(port_fd, username, password); ++fail_count; }#ifdef DEBUG fprintf(lp, "ret=%d, fail_count=%d\n", ret, fail_count); fflush(lp);#endif if (!ret) { ret = f_dms100_test_process(port_fd, phone_number, testmsg); }#ifdef DEBUG lt = time(NULL); ctime = localtime(<); fprintf(lp, "\n>>> Stop DMS100 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 dms100_drv *//* Function dms100_conn_switch() Call by TST-QUERY Test Return: 0 OK -1 Can not connect Switch, or Switch no response -2 Connect Switch OK, but wake up fail *//*********************************************************Function:int dms100_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 idReturn: 0 OK. -1 No fd ready when timeout. -2 an error occur.*********************************************************/int dms100_conn_switch(int port_fd){ int ret, ret_val; struct tm *ctime; time_t lt;#ifdef DEBUG lt = time(NULL); ctime = localtime(<); lp = backup_log("./dms100_drv.log"); fprintf(lp, "\n>>> Start DMS100 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 ret_val = 0; receive_str[0] = 0; send_break(port_fd); ret = f_dms100_result_receive(port_fd, receive_str, "\r?", NULL, 5000); if (ret == 0) 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 DMS100 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_dms100_send_wakeup(int port_fd, char *username, char *password)Narrative: Active switch port by sending wakeup signal and then enter test path.Param: int port_fd - socket id char *username - login name char *password - login passwordReturn: 0 OK. -1 No expected string. -2 No fd ready when timeout. -3 An error occur when performing poll or read. AM_UNREADY_ERROR Switch is not ready. AM_INVALID_PASSWORD Invalid password.*********************************************************/int f_dms100_send_wakeup(int port_fd, char *username, char *password){ int ret; char send_str[50]; /* char username[]="FSE"; */ f_trim_space(password); /* send Break string to switch port */ send_break(port_fd); ret = f_dms100_result_receive(port_fd, receive_str, "\r?", NULL, 5000); /* send LOGIN */ writeport(port_fd, "LOGIN\r"); ret = f_dms100_result_receive(port_fd, receive_str, "Enter User Name\n\r>", "User already logged in on", 5000); if (strstr(receive_str, "Enter User Name")) { /* send User Name */ sprintf(send_str, "%s\r", username); writeport(port_fd, send_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 5000); if (!strstr(receive_str, "Enter Password")) return (AM_UNREADY_ERROR); /* fail! */ /* send Password */ sprintf(send_str, "%s\r", password); writeport(port_fd, send_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 5000); sprintf(send_str, "%s Logged in on", username); if (!strstr(receive_str, send_str)) return (AM_INVALID_PASSWORD); /* fail! */ } else if (strstr(receive_str, "User already logged in") || strstr(receive_str, "User already logging in")) { } else { return (AM_UNREADY_ERROR); } writeport(port_fd, "MAPCI NODISP;MTC;LNS;LTP;LTPLTA;\r"); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 5000); /* if (!strstr(receive_str,"LTPLTA:")) return (AM_UNREADY_ERROR); */ /* send entrance system string */ return (0);}/* end of f_dms100_send_wakeup *//*********************************************************Function:int f_dms100_test_process(int port_fd, char *phone_number, TESTMSG * testmsg)Narrative: Test 12 specialities of line.Param: int port_fd - socket id char *phone_number - accessed telephone number TESTMSG * testmsg - struct of test messageReturn: 0 OK. -1 Error occur when writing port. -2 No fd ready when timeout. -3 An error occur. -5 Invalid telephone number. -6 Line busy.********************************************************/int f_dms100_test_process(int port_fd, char *phone_number, TESTMSG * testmsg){ char test_str[50]; int ret; int i; int lock_flag = 0; f_trim_space(phone_number); /* send access line string to switch port */ sprintf(test_str, "POST D %s PRINT\r", phone_number); writeport(port_fd, test_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 10000); if (ret != 0) /* if fail then return */ { goto _exit_dms100; } else { if (f_dms100_is_lock(receive_str)) { lock_flag = 1; /* send FRLS release Lock */ sprintf(test_str, "FRLS\r"); writeport(port_fd, test_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 2000); } else if (strstr(receive_str, "Invalid Directory Number")) { ret = -5; /* Null Telphone Number */ goto _exit_dms100; } else if (strstr(receive_str, "Invalid Symble")) { ret = -6; /* Line Busy */ goto _exit_dms100; } } /* send VDC */ sprintf(test_str, "VDC\r"); writeport(port_fd, test_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 5000); if (ret != 0) /* if fail then return */ { goto _exit_dms100; } else { if (strstr(receive_str, "Line state invalid") || strstr(receive_str, "Could not Seize Line")) { ret = -6; /* Line Busy */ goto _exit_dms100; } } /* start test */ sprintf(test_str, "LNTST\r"); writeport(port_fd, test_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 10000); if (ret != 0) /* if fail then return */ goto _exit_dms100; else { ret = f_dms100_analysis(receive_str, testmsg); }_exit_dms100: if (lock_flag) { /* send RTS, Lock Line */ sprintf(test_str, "RTS\r"); writeport(port_fd, test_str); ret = f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 5000); } /* send release line string */ sprintf(test_str, "LTA RLS\r"); writeport(port_fd, test_str); f_dms100_result_receive(port_fd, receive_str, "\r>", NULL, 5000);#ifdef DEBUG fprintf(lp, "\nret=%d!\n", ret); fflush(lp);#endif return (ret);} /* end f_dms100_test_process *//*********************************************************Function:int f_dms100_result_receive(int sock, char *szBuffer, char *end_str1, char *end_str2, int wait_time)Narrative: Recieve responded string from switch after sending command.Param: int sock - socket id char *szBuffer - a pointer to receive_str char *end_str1 - end lable1 of recieved stirng char *end_str2 - 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_dms100_result_receive(int sock, char *szBuffer, char *end_str1, char *end_str2, int wait_time){ int p; int retval, ret, rc; char ch; char abnormal_str[] = "OPTION>]\n\r>"; 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)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -