📄 sms_modem.c
字号:
/* -------------------------------------------------------------------- *//* SMS Client, send messages to mobile phones and pagers *//* *//* sms_modem.c *//* *//* Copyright (C) 1997,1998 Angelo Masci *//* *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Library General Public *//* License as published by the Free Software Foundation; either *//* version 2 of the License, or (at your option) any later version. *//* *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *//* Library General Public License for more details. *//* *//* You should have received a copy of the GNU Library General Public *//* License along with this library; if not, write to the Free *//* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *//* You can contact the author at this e-mail address: *//* *//* angelo@styx.demon.co.uk *//* *//* -------------------------------------------------------------------- *//* $Id: sms_modem.c,v 5.1 1998/02/01 07:10:39 root Exp root $ -------------------------------------------------------------------- */#include <stdio.h>#include <termios.h>#include <stdlib.h>#include <sys/stat.h>#include <time.h>#include <fcntl.h>#include <unistd.h>#include <signal.h>#include <string.h>#include "parserc.h"#include "sms_modem.h"#include "sms_lock.h"#include "logfile.h"#include "expect.h"#include "sms_resource.h"#include "common.h"/* -------------------------------------------------------------------- */#if !defined(MMODEMDIR)#error "MMODEMDIR undefined"#else#define MODEMDIR MMODEMDIR#endif/* -------------------------------------------------------------------- */#define MDM_OK 1#define MDM_CONNECT 2#define MDM_BUSY 3#define MDM_NO_DIALTONE 4#define MDM_NO_ANSWER 5#define MDM_RING 6#define MDM_NO_CARRIER 7#define MDM_ERROR 8#define MDM_UNKNOWN_RESPONSE 9#define MDM_DISCONNECTED 10/* -------------------------------------------------------------------- */struct response_struct { char *response_str; int response_code; };typedef struct response_struct RESPONSE;static RESPONSE response_list[] = { { "OK", MDM_OK }, { "CONNECT", MDM_CONNECT }, { "BUSY", MDM_BUSY }, { "NO DIALTONE", MDM_NO_DIALTONE }, { "NO DIAL TONE", MDM_NO_DIALTONE }, { "NO ANSWER", MDM_NO_ANSWER }, { "RING", MDM_RING }, { "NO CARRIER", MDM_NO_CARRIER }, { "ERROR", MDM_ERROR }, { "UNKNOWN", MDM_UNKNOWN_RESPONSE }, { "CARRIER", MDM_CONNECT }, { NULL, 0 } };/* -------------------------------------------------------------------- */#define MODEM_OPEN 0#define MODEM_CLOSED 1#define MAX_BUFSIZE 1024/* -------------------------------------------------------------------- */static int modem_status = MODEM_CLOSED;/* -------------------------------------------------------------------- */static struct termios t, t_orig;static long MDM_response_timeout, MDM_hangup_timeout, MDM_connect_timeout, MDM_write_timeout, MDM_echo_timeout, MDM_test_echo_timeout, MDM_dtr_sleep, MDM_settle_time, MDM_lock_retry_delay;static int modem_echo = TRUE; /* Is the modem echoing */ /* Characters? Default is YES */static long soft_hangup_retries; /* Number of times I should */ /* continue sending +++ATH */ /* If 0 then don't bother */ /* and rely on lowering DTR */static long guard_time; /* Escape Code Guard Time */ /* in microseconds */static long test_guard_time; /* Echo Test Guard Time */ /* in microseconds */static long esc_time; /* Time between sending each */ /* character of attention */ /* string in microseconds */static char modem_file[512], modem_lockfile[512];static char *device_dir, *device, *command_prefix, *command_suffix, *number_prefix, *dial_command, *hangup_command, *init_command, *attention_command, *response_prefix, *response_suffix, *set_local_echo_off, *lock_action, *flow_control, *lock_dir, *lock_prefix; static RESOURCE resource_list[] = { { RESOURCE_STRING, "MDM_device", 0, 1, NULL, 0, "modem", 0, &device }, { RESOURCE_STRING, "MDM_device_dir", 0, 0, NULL, 0, "/dev/", 0, &device_dir }, { RESOURCE_STRING, "MDM_lock_dir", 0, 0, NULL, 0, "/var/lock", 0, &lock_dir }, { RESOURCE_STRING, "MDM_lock_prefix", 0, 0, NULL, 0, "LCK..", 0, &lock_prefix }, { RESOURCE_STRING, "MDM_command_prefix", 0, 1, NULL, 0, "AT", 0, &command_prefix }, { RESOURCE_STRING, "MDM_flow_control", 0, 1, NULL, 0, "Hardware", 0, &flow_control }, { RESOURCE_STRING, "MDM_command_suffix", 0, 1, NULL, 0, "\r", 0, &command_suffix }, { RESOURCE_STRING, "MDM_number_prefix", 0, 1, NULL, 0, "", 0, &number_prefix }, { RESOURCE_STRING, "MDM_dial_command", 0, 1, NULL, 0, "DT", 0, &dial_command }, { RESOURCE_STRING, "MDM_hangup_command", 0, 1, NULL, 0, "H", 0, &hangup_command }, { RESOURCE_STRING, "MDM_init_command", 0, 1, NULL, 0, "Z", 0, &init_command }, { RESOURCE_STRING, "MDM_set_local_echo_off", 0, 0, NULL, 0, "E0", 0, &set_local_echo_off }, { RESOURCE_STRING, "MDM_response_prefix", 0, 1, NULL, 0, "\r\n", 0, &response_prefix }, { RESOURCE_STRING, "MDM_response_suffix", 0, 1, NULL, 0, "\r\n", 0, &response_suffix }, { RESOURCE_STRING, "MDM_attention_command", 0, 1, NULL, 0, "+++", 0, &attention_command }, { RESOURCE_NUMERIC, "MDM_soft_hangup_retries", 0, 0, NULL, 0, NULL, 3, &soft_hangup_retries }, { RESOURCE_NUMERIC, "MDM_guard_time", 0, 0, NULL, 0, NULL, 1750000, &guard_time }, { RESOURCE_NUMERIC, "MDM_test_guard_time", 0, 0, NULL, 0, NULL, 250000, &test_guard_time }, { RESOURCE_NUMERIC, "MDM_esc_time", 0, 0, NULL, 0, NULL, 125000, &esc_time }, { RESOURCE_STRING, "MDM_lock_action", 0, 1, NULL, 0, "NO_BLOCK", 0, &lock_action }, { RESOURCE_NUMERIC, "MDM_lock_retry_delay", 0, 1, NULL, 0, NULL, 5000000, &MDM_lock_retry_delay }, { RESOURCE_NUMERIC, "MDM_response_timeout", 0, 0, NULL, 0, NULL, 10, &MDM_response_timeout }, { RESOURCE_NUMERIC, "MDM_hangup_timeout", 0, 0, NULL, 0, NULL, 60, &MDM_hangup_timeout }, { RESOURCE_NUMERIC, "MDM_connect_timeout", 0, 0, NULL, 0, NULL, 60, &MDM_connect_timeout }, { RESOURCE_NUMERIC, "MDM_write_timeout", 0, 0, NULL, 0, NULL, 10, &MDM_write_timeout }, { RESOURCE_NUMERIC, "MDM_echo_timeout", 0, 0, NULL, 0, NULL, 10, &MDM_echo_timeout }, { RESOURCE_NUMERIC, "MDM_test_echo_timeout", 0, 0, NULL, 0, NULL, 10, &MDM_test_echo_timeout }, { RESOURCE_NUMERIC, "MDM_dtr_sleep", 0, 0, NULL, 0, NULL, 3, &MDM_dtr_sleep }, { RESOURCE_NUMERIC, "MDM_settle_time", 0, 0, NULL, 0, NULL, 1, &MDM_settle_time }, { RESOURCE_NULL, NULL, 0, 1, NULL, 0, NULL, 0, NULL }, };/* --------------------------------------------------------------------MDM_response_timeout Used as timeout for standard AT command responses ie. Waiting for OKMDM_hangup_timeout Used as timeout for standard ATH response Waiting for OKMDM_connect_timeout Used as timeout for connect string response. Includes time taken to dial, other party to answer and both modems to negotiate ie. CONNECTMDM_write_timeout Used as timeout for writes of commands to modemMDM_echo_timeout Used as timeout for echoing of commands written to modemMDM_test_echo_timeout Used as timeout for testing whether the modem is echoing commands we send itMDM_dtr_sleep How many seconds should we sleep after lowering DTR to ensure disconnectMDM_settle_time How many seconds should we sleep after receiving the lockfile -------------------------------------------------------------------- */int MDM_send(int modem, char *str);int MDM_response(int modem, int timeout);void fcntl_set(int fd, int flags);void fcntl_clear(int fd, int flags);int MDM_init(char *modem_file, char data, char parity, char stop, char flow, long baud);int MDM_dial(char *number, char data, char parity, char stop, long baud);void MDM_release_lock(void);void MDM_obtain_lock(char *device);void MDM_hangup(int modem);int toggle_DTR(int modem, int sleep_int);/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int SMS_dial(char *number, char *params, long baud){ int fd; lprintf(LOG_VERBOSE, "Using SMSmodem Package\n"); fd = MDM_dial(number, params[0], /* Bits */ params[1], /* Parity */ params[2], /* Stop */ baud); /* Baud */ return fd;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void SMS_hangup(int modem){ MDM_hangup(modem);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *get_response(int id){ RESPONSE *ptr; ptr = response_list; while (ptr->response_str != NULL) { if (ptr->response_code == id) { return ptr->response_str; } ptr++; } return "UNKOWN";}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int MDM_send(int modem, char *str){ char buf[1024]; int res; res = twrite(modem, str, sms_strlen(str), MDM_write_timeout); if (res) { return(res); } if (modem_echo) { /* The modem is echoing our commands. So we */ /* should expect to read these back */ res = expnstr(modem, buf, str, sms_strlen(str), MDM_echo_timeout); if (res) { return(res); } } return 0;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int MDM_response(int modem, int timeout){ int i; char buf[MAX_BUFSIZE]; int res; res = expstr(modem, buf, response_prefix, MAX_BUFSIZE, timeout); if (res) { if (res == -2) { return(MDM_DISCONNECTED); } return(res); } res = expstr(modem, buf, response_suffix, MAX_BUFSIZE, timeout); if (res) { if (res == -2) { return(MDM_DISCONNECTED); } return(res); } for(i=0; response_list[i].response_str != NULL; i++) { if (strncmp(buf, response_list[i].response_str, sms_strlen(response_list[i].response_str)) == 0) { return(response_list[i].response_code); } } return(MDM_UNKNOWN_RESPONSE);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void fcntl_set(int fd, int flags){ int val; if ((val = fcntl(fd, F_GETFL, 0)) < 0) { lprintf(LOG_ERROR, "MODEM: fcntl F_GETFL\n"); exit(-1); } val |= flags; if (fcntl(fd, F_SETFL, val) < 0) { lprintf(LOG_ERROR, "MODEM: fcntl F_SETFL\n"); exit(-1); }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void fcntl_clear(int fd, int flags){ int val; if ((val = fcntl(fd, F_GETFL, 0)) < 0) { lprintf(LOG_ERROR, "MODEM: fcntl F_GETFL\n"); exit(-1); } val &= ~flags; if (fcntl(fd, F_SETFL, val) < 0) { lprintf(LOG_ERROR, "MODEM: fcntl F_SETFL\n"); exit(-1); }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void MDM_release_lock(void){ resource_unlock(modem_lockfile);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void MDM_obtain_lock(char *device){ int no_block, notify; resource_check_lockdir(modem_lockfile); /* Exit with message if lockdir */ /* does not exist or we dont */ /* have write permissions */ if (strcmp("NO_BLOCK", lock_action) == 0) { no_block = TRUE; } else if (strcmp("BLOCK", lock_action) == 0) { no_block = FALSE; } else { lprintf(LOG_WARNING, "MDM_lock_action invalid, defaulting to NO_BLOCK\n"); no_block = TRUE; } notify = TRUE; while(resource_lock(modem_lockfile)) { if (no_block) { lprintf(LOG_ERROR, "Could not obtain lock for modem device\n"); lprintf(LOG_STANDARD, "Another program is using the modem.\n"); exit(-1); } if (notify) { lprintf(LOG_VERBOSE, "Blocking on lockfile '%s'\n", modem_lockfile); lprintf(LOG_STANDARD, "Another program is using the modem.\n"); lprintf(LOG_STANDARD, "Waiting...\n"); notify = FALSE; } resource_wait(modem_lockfile, MDM_lock_retry_delay); } if (!notify) { lprintf(LOG_STANDARD, "Modem is now free.\n"); lprintf(LOG_STANDARD, "Continuing...\n"); } atexit(MDM_release_lock); /* Establish EXIT handler */ /* to release the lockfile if */ /* we leave prematurely */ sleep(MDM_settle_time); /* Allow modem to settle down */ /* I might have just obtained */ /* The lock on the device from */ /* another process */}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int MDM_init(char *modem_file, char data, char parity, char stop, char flow, long baud){ int modem, t_baud; modem = open(modem_file, O_RDWR|O_NONBLOCK); if (modem == -1) { lprintf(LOG_WARNING, "MODEM: Failed to open %s\n", modem_file); return -1; } if (tcgetattr(modem, &t) == -1) { lprintf(LOG_WARNING, "MODEM: Failed tcgetattr()\n"); close(modem); return -1; } if (tcgetattr(modem, &t_orig) == -1) { lprintf(LOG_WARNING, "MODEM: Failed tcgetattr()\n"); close(modem); return -1; } /* ---------------------------- */ /* Set up terminal attributes */ /* for the device */ /* ---------------------------- */ t.c_cc[VMIN] = 1; /* 1 Character buffer */ t.c_cc[VTIME] = 0; /* Block indefinitely */ t.c_cflag |= CREAD | /* Enable receiver */ HUPCL; /* Lower modem lines on last close */ /* 1 stop bit (since CSTOPB off) */ t.c_oflag = 0; /* Turn off all output processing */ t.c_iflag = 0; t.c_lflag = 0; /* Everything off in local flags, */ /* disables: */ /* canonical mode */ /* signal generation */ /* echo */ /* ---------------------------- */ /* Set data bits 5 through 8 */ /* ---------------------------- */ t.c_cflag &= ~(CSIZE); /* Mask data size */ switch (data) { case '5': t.c_cflag |= CS5; /* Set 5 bits */ break; case '6': t.c_cflag |= CS6; /* Set 6 bits */ break; case '7': t.c_cflag |= CS7; /* Set 7 bits */ break; case '8': t.c_cflag |= CS8; /* Set 8 bits */ break; default: lprintf(LOG_ERROR, "MODEM: Number of bits must be either 5,6,7 or 8\n"); close(modem); return -1; } /* ---------------------------- */ /* Set parity Even, Odd or None */ /* ---------------------------- */ switch (parity) { case 'E': t.c_cflag |= PARENB; /* Enable parity */ break; case 'O': t.c_cflag |= PARENB; /* Enable parity */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -