/** * Cmux * Enables GSM 0710 multiplex using n_gsm * * Copyright (C) 2013 - Rtone - Nicolas Le Manchet * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /** * gsmmux.h provides n_gsm line dicipline structures and functions. * It should be kept in sync with your kernel release. */ #include "gsmmux.h" /* n_gsm ioctl */ #ifndef N_GSM0710 # define N_GSM0710 21 #endif /* attach a line discipline ioctl */ #ifndef TIOCSETD # define TIOCSETD 0x5423 #endif /* serial port of the modem */ #define SERIAL_PORT "/dev/ttyUSB0" /* line speed */ #define LINE_SPEED B115200 /* maximum transfert unit (MTU), value in bytes */ #define MTU 64 /** * whether or not to create virtual TTYs for the multiplex * 0 : do not create * 1 : create */ #define CREATE_NODES 1 /* number of virtual TTYs to create (most modems can handle up to 4) */ #define NUM_NODES 4 /* name of the virtual TTYs to create */ #define BASENAME_NODES "/dev/ttyGSM" /* name of the driver, used to get the major number */ #define DRIVER_NAME "gsmtty" /** * whether or not to print debug messages to stderr * 0 : debug off * 1 : debug on */ #define DEBUG 1 /** * whether or not to detach the program from the terminal * 0 : do not daemonize * 1 : daemonize */ #define DAEMONIZE 1 /* size of the reception buffer which gets data from the serial line */ #define SIZE_BUF 256 /** * Prints debug messages to stderr if debug is wanted */ static void dbg(char *fmt, ...) { va_list args; if (DEBUG) { fflush(NULL); va_start(args, fmt); vfprintf(stderr, fmt, args); va_end(args); fprintf(stderr, "\n"); fflush(NULL); } return; } /** * Sends an AT command to the specified line and gets its result * Returns 0 on success * -1 on failure */ int send_at_command(int serial_fd, char *command) { char buf[SIZE_BUF]; int r; /* write the AT command to the serial line */ if (write(serial_fd, command, strlen(command)) <= 0) err(EXIT_FAILURE, "Cannot write to %s", SERIAL_PORT); /* wait a bit to allow the modem to rest */ sleep(1); /* read the result of the command from the modem */ memset(buf, 0, sizeof(buf)); r = read(serial_fd, buf, sizeof(buf)); if (r == -1) err(EXIT_FAILURE, "Cannot read %s", SERIAL_PORT); /* if there is no result from the modem, return failure */ if (r == 0) { dbg("%s\t: No response", command); return -1; } /* if we have a result and want debug info, strip CR & LF out from the output */ if (DEBUG) { int i; char bufp[SIZE_BUF]; memcpy(bufp, buf, sizeof(buf)); for(i=0; i