📄 connect_modem.c
字号:
#include <stdio.h>#include <poll.h>#include <errno.h>extern int errno;extern int logfp;extern void writelog(int logfp, char *str);char *wait_cmd_ret(int fd, int timeval);extern void write_msg(char *head, unsigned char *msg, int msg_len);/*********************************************************Function:int connect_modem(int fd, const char *phone_no)Narrative: Check local modem and connect to remote modem.Param: int fd - socket id const char *phone_no - a pointer to telephone number of remote modemReturn: 0 OK. -1 Local modem fail. -2 Connect to remote modem fail.*********************************************************/int connect_modem(int fd, const char *phone_no){ char modem_cmd_str[128]; char *cmd_ret_str; int ret_val; char *test_str = "UUU"; /* read previous infomation from port */ do { cmd_ret_str = wait_cmd_ret(fd, 100); } while (cmd_ret_str[0] != 0); /* check local modem connection */ write(fd, "\r\n+++", 5); cmd_ret_str = wait_cmd_ret(fd, 2000); write(fd, "at\r\n", 4); cmd_ret_str = wait_cmd_ret(fd, 2000); if (strstr(cmd_ret_str, "at") == NULL) { if (strstr(cmd_ret_str, "OK")) { write(fd, "ato\r\n", 5); cmd_ret_str = wait_cmd_ret(fd, 10000); return 0; } else return -1; } /*hang up the modem */ write(fd, "ath\r\n", 5); cmd_ret_str = wait_cmd_ret(fd, 2000); if (cmd_ret_str[0] == 0) return -1; /*set modem to check BUSY */ write(fd, "atx4\r\n", 6); cmd_ret_str = wait_cmd_ret(fd, 2000); /*reconnect to switcher port */ sprintf(modem_cmd_str, "atdt%s\r\n", phone_no); write(fd, modem_cmd_str, strlen(modem_cmd_str)); cmd_ret_str = wait_cmd_ret(fd, 35000); if (cmd_ret_str[0] == 0) return -2; if (strstr(cmd_ret_str, "CONNECT")) { writelog(logfp, cmd_ret_str);/* cmd_ret_str=wait_cmd_ret(fd,10000); */ return 0; } else { write(fd, "ath\r\n", 5); cmd_ret_str = wait_cmd_ret(fd, 2000); return -2; }}/*********************************************************Function:char *wait_cmd_ret(int fd, int timeval)Narrative: Recieve return stirng from modem.Param: int fd - socket id int timeval - poll system call waiting timeReturn: cmd_ret_str recieved string.*********************************************************/char *wait_cmd_ret(int fd, int timeval){ static char cmd_ret_str[512]; struct pollfd com_pollfd; int ret_len, rc; int poll_rval; char ch; /*intial the struct com_pollfd */ com_pollfd.fd = fd; com_pollfd.events = POLLIN; com_pollfd.revents = 0; cmd_ret_str[0] = 0; ret_len = 0; for (;;) { poll_rval = poll(&com_pollfd, 1, timeval); /* System call poll() error */ if (poll_rval < 0 && errno != EINTR) { writelog(logfp, "serial port poll() failed"); return (cmd_ret_str); } /* Timeout */ if (poll_rval == 0) { if (timeval == 10000) { write_msg("Modem!\n", (unsigned char *) cmd_ret_str, ret_len);/*rc=write(fd,"+++ATH\r\n+++ATH\r\n+++ATH\r\n",24); */ } return (cmd_ret_str); } /* normal data arriving at this fd */ if (com_pollfd.revents & POLLIN) { rc = read(fd, cmd_ret_str + ret_len, 1); if (rc == 1) { ch = cmd_ret_str[ret_len]; ret_len += rc; cmd_ret_str[ret_len] = 0; if (ret_len > 500) return (cmd_ret_str); if (ch == '\n') { if (strstr(cmd_ret_str, "+++")) return (cmd_ret_str); if (strstr(cmd_ret_str, "OK")) return (cmd_ret_str); if (strstr(cmd_ret_str, "CONNECT")) { return (cmd_ret_str); } if (strstr(cmd_ret_str, "NO CARRIER")) return (cmd_ret_str); if (strstr(cmd_ret_str, "BUSY")) return (cmd_ret_str); if (strstr(cmd_ret_str, "NO ANSWER")) return (cmd_ret_str); if (strstr(cmd_ret_str, "NO DIAL TONE")) return (cmd_ret_str); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -