📄 neax_drv.c
字号:
#include <poll.h>#include <stdio.h>#include <stdlib.h>#include "simdrv.h"#include "simu112.h"#include "simufunc.h"#define STAT_START 0#define STAT_WAKEUP1 1#define STAT_WAKEUP2 2#define STAT_ACCESS 3#define STAT_ACCESS_STATE 4#define STAT_DCV_AB 5#define STAT_DCV_BG 6#define STAT_DCV_AG 7#define STAT_ACV_AB 8#define STAT_ACV_BG 9#define STAT_ACV_AG 10#define STAT_BAT_AB 11#define STAT_CUR_BG 12#define STAT_CUR_AG 13#define STAT_CAP_AB 14#define STAT_CAP_BG 15#define STAT_CAP_AG 16#define STAT_RES_AB 17#define STAT_RES_BG 18#define STAT_RES_AG 19#define STAT_FINISH 20static char str_wakeup1[] ={0x4a, 0x40, 0x0d, 0x00};static char str_wakeup1_resp[] ={0x40, 0x4f, 0x0d, 0x40, 0x41, 0x0d, 0x00};static char str_wakeup2[] ={0x40, 0x40, 0x0d, 0x00};static char str_wakeup2_resp[] ={0x42, 0x41, 0x0d, 0x42, 0x41, 0x0d, 0x00};static char str_access_state[] ={0x4b, 0x40, 0x0d, 0x4d, 0x6f, 0x0d, 0x4d, 0x6e, 0x0d, 0x00};static char str_DCV_AB[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x6b, 0x0d, 0x00};static char str_DCV_BG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x69, 0x0d, 0x00};static char str_DCV_AG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x6a, 0x0d, 0x00};static char str_ACV_AB[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x63, 0x0d, 0x00};static char str_ACV_BG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x61, 0x0d, 0x00};static char str_ACV_AG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x62, 0x0d, 0x00};static char str_BAT_AB[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x43, 0x6f, 0x0d, 0x00};static char str_CUR_BG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x42, 0x61, 0x0d, 0x00};static char str_CUR_AG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x42, 0x62, 0x0d, 0x00};static char str_CAP_AB[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x41, 0x63, 0x0d, 0x00};static char str_CAP_BG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x41, 0x61, 0x0d, 0x00};static char str_CAP_AG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x41, 0x62, 0x0d, 0x00};/*static char str_RES_AB[]={0x46,0x40,0x0d,0x4e,0x60,0x0d,0x40,0x63,0x0d,0x00}; */static char str_RES_AB[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x40, 0x60, 0x0d, 0x00};static char str_RES_BG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x40, 0x61, 0x0d, 0x00};static char str_RES_AG[] ={0x46, 0x40, 0x0d, 0x4e, 0x60, 0x0d, 0x40, 0x62, 0x0d, 0x00};static char str_end_test[] ={0x46, 0x40, 0x0D, 0x4E, 0x60, 0x0D, 0x4B, 0x60, 0x0D, 0x46, 0x48, 0x0D, 0x46, 0x40, 0x0D, 0x4E, 0x60, 0x0D, 0x00};static char str_release[] ={0x4e, 0x60, 0x0d, 0x4a, 0x62, 0x0d, 0x40, 0x41, 0x0d, 0x00};FILE *lp;int do_wakeup1_flag;char *f_make_access_str(char *phone_number);int f_send_wakeup1(int port_fd, int *stat);int f_send_wakeup2(int port_fd, int *stat, int *char_cnt);int f_access(int port_fd, char *phone_number, int *stat);int f_access_state(int port_fd, int *stat);int f_test_process(int port_fd, int *stat, TESTMSG * testmsg);float f_get_result(float A, char *XXX, int C, int D);int f_split_data(char *str, float *A, char *XXX, int *C, int *D);float f_mypower(int C);/*********************************************************Function:int neax_proc(int port_fd, char *phone_number, TESTMSG * testmsg)Narrative: Called in simdrv.c. It is the main flow of neax_drv driver.Param: int port_fd - socket id char *phone_number - Telephone number of accessed line. 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 neax_proc(int port_fd, char *phone_number, TESTMSG * testmsg){ int ret; int char_cnt; int stat; int fail_count; struct tm *ctime; time_t lt; /* stat=STAT_START; */#ifdef DEBUG lt = time(NULL); ctime = localtime(<); lp = backup_log("./neax_drv.log"); /* lp=fopen("./neax_drv.log","a+"); */ fprintf(lp, "\n>>> Start Neax-61 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; do_wakeup1_flag = 0; stat = STAT_WAKEUP1; while (stat < STAT_FINISH && fail_count < 4) { switch (stat) { case STAT_WAKEUP1: ret = f_send_wakeup1(port_fd, &stat); break; case STAT_WAKEUP2: ret = f_send_wakeup2(port_fd, &stat, &char_cnt); if (ret) { do_wakeup1_flag = 1; stat = STAT_WAKEUP1; } break; case STAT_ACCESS: ret = f_access(port_fd, phone_number, &stat); if (ret == 1) { ret = -6; /* æ */ goto _exit_neax; } break; case STAT_ACCESS_STATE: ret = f_access_state(port_fd, &stat); break; case STAT_DCV_AB: case STAT_DCV_BG: case STAT_DCV_AG: case STAT_ACV_AB: case STAT_ACV_BG: case STAT_ACV_AG: case STAT_BAT_AB: case STAT_CUR_BG: case STAT_CUR_AG: case STAT_CAP_AB: case STAT_CAP_BG: case STAT_CAP_AG: case STAT_RES_AB: case STAT_RES_BG: case STAT_RES_AG: ret = f_test_process(port_fd, &stat, testmsg); if (!ret && fail_count > 2) ++stat; break; } if (!ret) fail_count = 0; else ++fail_count;#ifdef DEBUG fprintf(lp, "stat=%d, ret=%d, fail_count=%d\n", stat, ret, fail_count); fflush(lp);#endif }_exit_neax: writeport(port_fd, str_release); /* release */#ifdef DEBUG lt = time(NULL); ctime = localtime(<); fprintf(lp, "Now stat: %d\n", stat); fprintf(lp, "\n>>> Stop Neax-61 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 switch (ret) { case -1: ret = AM_UNREADY_ERROR; break; case -2: ret = AM_UNREADY_ERROR; 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; } if (stat == STAT_FINISH) { /* f_show_msg(testmsg); */ return (0); /* finish */ } else { return (ret); }}/* end of neax_proc *//*********************************************************Function:char *f_make_access_str(char *phone_number)Narrative: Generate accessing string of telephone number to switch.Param: char *phone_number - Telephone number of accessed line.Return: String of telephone number *********************************************************/char *f_make_access_str(char *phone_number){ char *p, *q; static char str_return[50]; p = phone_number; q = str_return; for (; *p != '\0'; ++p) { if (*p == '0') *p += 0x0a; *q = '\x40'; *(q + 1) = '\x20' + *p; *(q + 2) = '\x0d'; q += 3; } *q = '\0'; return (str_return);}/*********************************************************Function:int f_send_wakeup1(int port_fd, int *stat)Narrative: Active switch port by sending wakeup1 signal.Param: int port_fd - socket id int *stat - a pointer to test stateReturn: 0 OK. -1 No expected string. -2 No fd ready when timeout. -3 An error occur when performing poll or read.*********************************************************/int f_send_wakeup1(int port_fd, int *stat){ 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 Wakeup1 string to switch port */ if (do_wakeup1_flag) {#ifdef DEBUG fprintf(lp, "Send WakeUp1\n"); fflush(lp);#endif writeport(port_fd, str_wakeup1); do_wakeup1_flag = 0; poll(0, 0, 10); } *stat = STAT_WAKEUP2; return (0);/* p=0; while (1){ ret=poll(pollfdsp,1,10000); if (ret<0) return(-3); if (ret==0) return(-2); if (pollfdsp[0].revents & POLLIN){ rc=read(port_fd,&ch,1); if (rc<=0) return(-3); if (ch=='\0') continue; #ifdef DEBUG fprintf(lp,"%02X,%c\n",ch,ch); #endif if (ch==str_wakeup1_resp[p]) { ++p; if (p==strlen(str_wakeup1_resp)) { poll(0,0,10); *stat=STAT_WAKEUP2; #ifdef DEBUG fprintf(lp,"\nsend Wakeup1 string OK!\n"); fflush(lp); #endif return (0); } } else p=0; } } */}/* end of f_send_wakeup1 *//*********************************************************Function:int f_send_wakeup2(int port_fd, int *stat, int *char_cnt)Narrative: Active switch port by sending wakeup2 signal.Param: int port_fd - socket id int *stat - a pointer to test state int *char_cnt - a pointer to character number recievedReturn: 0 OK. -1 No expected string. -2 No fd ready when timeout. -3 An error occur when performing poll or read.*********************************************************/int f_send_wakeup2(int port_fd, int *stat, int *char_cnt){ struct pollfd pollfdsp[1]; char ch, str[256]; char str_release1[] = {0x40, 0x41, 0x0d, 0x00}; int p, rc, ret; pollfdsp[0].fd = port_fd; pollfdsp[0].events = POLLIN; pollfdsp[0].revents = 0; *char_cnt = 0; /* send Wakeup2 string to switch port */ writeport(port_fd, str_release1); poll(0, 0, 50); writeport(port_fd, str_wakeup2); p = 0; while (1) { ret = poll(pollfdsp, 1, 8000); if (ret < 0) return (-3); if (ret == 0) return (-2); if (pollfdsp[0].revents & POLLIN) { rc = read(port_fd, &ch, 1); if (rc <= 0) return (-3); ++*char_cnt; /* count received char number */ if (ch == '\0') continue;#ifdef DEBUG fprintf(lp, "%02X,%c\n", (unsigned char) ch, ch);#endif if (ch == str_wakeup2_resp[p]) { ++p; if (p == strlen(str_wakeup2_resp)) { poll(0, 0, 10); *stat = STAT_ACCESS;#ifdef DEBUG fprintf(lp, "\nsend Wakeup2 string OK!\n"); fflush(lp);#endif return (0); } } else p = 0; } }}/* end of f_send_wakeup2 *//*********************************************************Function:int f_access(int port_fd, char *phone_number, int *stat)Narrative: Access line.Param: int port_fd - socket id char *phone_number - Telephone number of accessed line int *stat - a pointer to test stateReturn: 0 OK. 1 Line busy. 2 Line busy. 3 Line busy. -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_access(int port_fd, char *phone_number, int *stat){ struct pollfd pollfdsp[1]; char ch, str[2048]; int p, q, r, rc, ret; int i; int result; /* 0 - Access OK 1 - Line Busy 2 - Lock Out 3 - Channel Busy */ char str_begin[] = {0x43, 0x40, 0x0d, 0x00}; char str_end[] = {0x40, 0x5f, 0x0d, 0x00}; char str_channel_busy[] = {0x42, 0x43, 0x0d, 0x00}; char str_access_ok[] = {0x40, 0x51, 0x0d, 0x40, 0x55, 0x0d, 0x40, 0x53, 0x0d, 0x00}; char str_access_not_ok[] = {0x40, 0x51, 0x0d, 0x40, 0x56, 0x0d, 0x40, 0x53, 0x0d, 0x00}; char str_access_line_busy[] = {0x40, 0x58, 0x0d, 0x40, 0x55, 0x0d, 0x40, 0x59, 0x0d, 0x40, 0x51, 0x0d, 0x40, 0x5f, 0x0d, 0x00}; char str_access_lock_out[] = {0x40, 0x58, 0x0d, 0x40, 0x54, 0x0d, 0x40, 0x59, 0x0d, 0x40, 0x51, 0x0d, 0x40, 0x5f, 0x0d, 0x00}; pollfdsp[0].fd = port_fd; pollfdsp[0].events = POLLIN; pollfdsp[0].revents = 0;#ifdef DEBUG fprintf(lp, "\nphone number: %s\n", phone_number); fflush(lp);#endif /* send Access string to switch port */ writeport(port_fd, f_make_access_str(phone_number)); result = 0; p = 0; q = 0; r = 0; while (1) { ret = poll(pollfdsp, 1, 8000); if (ret < 0) return (-3); if (ret == 0) return (-2); if (pollfdsp[0].revents & POLLIN) { rc = read(port_fd, &ch, 1); if (rc <= 0) return (-3); if (ch == '\0') continue;#ifdef DEBUG fprintf(lp, "%02X,%c\n", (unsigned char) ch, ch);#endif str[r] = ch; ++r; if (r >= 2048) r = 0; if (ch == str_channel_busy[p]) { ++p; if (p == strlen(str_channel_busy)) { poll(0, 0, 10); /* result=3; *//* result=3 Channel Busy */ result = 0; /* Test anyway, Channel Busy */ break; } } else p = 0; if (ch == str_end[q]) { ++q; if (q == strlen(str_end)) { poll(0, 0, 10); str[r] = '\0';#ifdef DEBUG fprintf(lp, "\naccess respone string: "); for (i = 0; i < strlen(str); ++i) fprintf(lp, "%02X,", (unsigned char) str[i]); fprintf(lp, "\n"); fflush(lp);#endif result = -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -