📄 neax_rsu_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};static char str_release1[] ={0x40, 0x41, 0x0d, 0x00};FILE *lp;int do_wakeup1_flag;char *f_nec_rsu_make_access_str(char *phone_number);int f_nec_rsu_send_wakeup1(int port_fd, int *stat);int f_nec_rsu_send_wakeup2(int port_fd, int *stat, int *char_cnt);int f_nec_rsu_access(int port_fd, char *phone_number, int *stat);int f_nec_rsu_access_state(int port_fd, int *stat);int f_nec_rsu_test_process(int port_fd, int *stat, TESTMSG * testmsg);float f_nec_rsu_get_result(float A, char *XXX, int C, int D);int f_nec_rsu_split_data(char *str, float *A, char *XXX, int *C, int *D);float f_nec_rsu_mypower(int C);int f_nec_rsu_get_response(int port_fd, char *expect_str, int len, int wait_time);/*********************************************************Function:int f_nec_rsu_init_ltc(int port_fd)Narrative: Initialize neax_rsu ltc port.Param: int port_fd - socket id Return: 0 OK. -1 Otherwise fail.*********************************************************/int f_nec_rsu_init_ltc(int port_fd){ int ret; int state; int i; char ISAC_before_init_info[] = {0x03, 0x70, 0x0D, 0x00}; char ISAC_mainmenu_info[] = {0x03, 0x5C, 0x02, 0x00}; char ISAC_ltc_info[] = {0x03, 0x5A, 0x1E, 0x31, 0x41, 0x61, 0x0D, 0x00}; char ISAC_init[] = {0x03, 0x40, 0x0D, 0x00}; char ISAC_init_resp[] = {0x03, 0x5C, 0x02, 0x00}; char ISAC_enter_ltc[] = {0x03, 0x23, 0x0D, 0x4B, 0x48, 0x0D, 0x00}; char ISAC_enter_ltc_resp[] = {0x03, 0x53, 0x40, 0x0D, 0x00}; char ISAC_back_to_mainmenu[] = {0x4B, 0x47, 0x0D, 0x03, 0x27, 0x0D, 0x03, 0x46, 0x0D, 0x00}; state = -1; for (i = 0; i < 7; ++i) { ret = f_nec_rsu_get_response(port_fd, ISAC_before_init_info, strlen(ISAC_before_init_info), 2000); if (ret == 0) { state = 0;#ifdef DEBUG fprintf(lp, "receive ISAC_before_init_info state=%d i=%d\n", state, i); fflush(lp);#endif break; } }#ifdef DEBUG fprintf(lp, "111 state=%d i=%d\n", state, i); fflush(lp);#endif if (ret == 0) state = 0; /* Port type is ISAC and port is not initialized */ else { for (i = 0; i < 3; ++i) { if ((ret = f_nec_rsu_get_response(port_fd, ISAC_mainmenu_info, strlen(ISAC_mainmenu_info), 2000)) == 0) break; } if (ret == 0) { state = 2; /* we are into main menu */#ifdef DEBUG fprintf(lp, "receive ISAC_mainmenu_info state=%d i=%d\n", state, i); fflush(lp);#endif } else {#ifdef DEBUG fprintf(lp, "Do ISAC_back_to_mainmenu (F1) state=%d\n", state); fflush(lp);#endif ret = f_nec_rsu_do_command(port_fd, ISAC_back_to_mainmenu, ISAC_mainmenu_info, strlen(ISAC_mainmenu_info), 3000, 1); { if (ret == 0) state = 2; /* we are into main menu */ else return -1; /* otherwise fail! */ } } }#ifdef DEBUG fprintf(lp, "222 state=%d\n", state); fflush(lp);#endif if (state == 0) {#ifdef DEBUG fprintf(lp, "Do ISAC_init state=%d\n", state); fflush(lp);#endif ret = f_nec_rsu_do_command(port_fd, ISAC_init, ISAC_init_resp, strlen(ISAC_init_resp), 2000, 5); if (ret == 0) state = 2; /* we are into main menu */ else return -1; } if (state == 2) {#ifdef DEBUG fprintf(lp, "Do ISAC_enter_ltc state=%d\n", state); fflush(lp);#endif ret = f_nec_rsu_do_command(port_fd, ISAC_enter_ltc, ISAC_enter_ltc_resp, strlen(ISAC_enter_ltc_resp), 3000, 3); if (ret == 0) { state = 3; /* we are now into LTC */ } }#ifdef DEBUG fprintf(lp, "333 state=%d\n", state); fflush(lp);#endif if (state == 3) return 0; else return -1;}/*******************************************************************Function:int f_nec_rsu_do_command(int port_fd,char *expect_str,int len,int wait_time,int loop)Narrative: Send neax_rsu command to switch.Parameters: port_fd - socket id command - the command we want to send expect_str - expecting for received string len - length wait_time - wait time (miliseconds) loop - loop timesReturn: 0 receive OK <0 receive Fail*******************************************************************/int f_nec_rsu_do_command(int port_fd, char *command, char *expect_str, int len, int wait_time, int loop){ int i, ret; ret = 0; for (i = 0; i < loop; ++i) { writeport(port_fd, command); ret = f_nec_rsu_get_response(port_fd, expect_str, len, wait_time); if (ret == 0) break; } return ret;}/*******************************************************************Function:nt f_nec_rsu_get_response(int port_fd,char *expect_str,int len,int wait_time)Narrative: Recieve responded string from switch after sending command.Parameters: port_fd - socket id expect_str - expecting for received string len - length wait_time - wait time (miliseconds)Return: 0 receive OK <0 receive Fail*******************************************************************/int f_nec_rsu_get_response(int port_fd, char *expect_str, int len, int wait_time){ struct pollfd pollfdsp[1]; char ch; int p, rc, ret; pollfdsp[0].fd = port_fd; pollfdsp[0].events = POLLIN; pollfdsp[0].revents = 0; p = 0; while (1) { ret = poll(pollfdsp, 1, wait_time); 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, expect_str[p]=%02X,p=%d,len=%d\n",(unsigned char)ch,expect_str[p],p,len); fflush(lp); #endif */ if (ch == expect_str[p]) { ++p; if (p == len) {#ifdef DEBUG fprintf(lp, "\nReceive OK!\n"); fflush(lp);#endif return (0); } } else p = 0; } if (rc <= 0) return (-3); } }}/*********************************************************Function:int neax_rsu_proc(int port_fd, char *phone_number, TESTMSG * testmsg)Narrative: Called in simdrv.c. It is the main flow of neax_rsu_drv driver.Param: int port_fd - socket id char *phone_number - char pointer to accessed telephone number string 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_rsu_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; int i; /* stat=STAT_START; */#ifdef DEBUG lt = time(NULL); ctime = localtime(<); lp = backup_log("./neax_rsu_drv.log"); fprintf(lp, "\n>>> Start Neax-61 (RSU) Test at %4d.%02d.%02d %02d:%02d:%02d <<<\n", ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour, ctime->tm_min, ctime->tm_sec); fflush(lp);#endif for (i = 0; i < 2; ++i) { ret = f_nec_rsu_init_ltc(port_fd); if (ret == 0) break; } if (ret != 0) { ret = -1; goto _exit_neax1; } fail_count = 0; do_wakeup1_flag = 0; stat = STAT_WAKEUP2; while (stat < STAT_ACCESS && fail_count < 8) { ret = f_nec_rsu_send_wakeup2(port_fd, &stat, &char_cnt); ++fail_count;#ifdef DEBUG fprintf(lp, "stat=%d, ret=%d, fail_count=%d\n", stat, ret, fail_count); fflush(lp);#endif } /* ret=f_nec_rsu_send_wakeup1(port_fd,&stat); */ /* while (stat<STAT_ACCESS && fail_count<4) { switch (stat) { case STAT_WAKEUP1: ret=f_nec_rsu_send_wakeup1(port_fd,&stat); break; case STAT_WAKEUP2: ret=f_nec_rsu_send_wakeup2(port_fd,&stat,&char_cnt); if (ret) { do_wakeup1_flag=1; stat=STAT_WAKEUP1; } break; } ++ fail_count; #ifdef DEBUG fprintf(lp,"stat=%d, ret=%d, fail_count=%d\n",stat,ret,fail_count); fflush(lp); #endif } */ if (stat < STAT_ACCESS) goto _exit_neax; if (stat == STAT_ACCESS) { ret = f_nec_rsu_access(port_fd, phone_number, &stat); if (ret == 1 || ret == 3) { ret = -6; /* æ */ goto _exit_neax; } } if (stat == STAT_ACCESS_STATE) { ret = f_nec_rsu_access_state(port_fd, &stat); stat = STAT_DCV_AB; } if (stat < STAT_DCV_AB) goto _exit_neax; while (stat < STAT_FINISH) { switch (stat) { 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_nec_rsu_test_process(port_fd, &stat, testmsg); break; } }_exit_neax: writeport(port_fd, str_release); /* release */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -