diff --git a/Makefile b/Makefile index 5f0e916..545139c 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,16 @@ -CC=gcc -CFLAGS=-Wall -all: cmux +CC?=gcc +CFLAGS?=-Wall +DESTDIR?=/ +PREFIX?=usr -program: cmux.c +cmux: cmux.c + $(CC) $(CFLAGS) cmux.c -o cmux + +install: cmux + install -m 0755 cmux $(DESTDIR)/$(PREFIX)/bin/cmux clean: - rm -f cmux + -@rm cmux -run: cmux - ./cmux \ No newline at end of file +.PHONY: install clean diff --git a/README.md b/README.md index 9f7026f..eae9759 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ Cmux ==== This program enables GSM 0710 multiplexing using linux n_gsm line dicipline. +Supported are also SIM900 and Telit modules. How it works ------- @@ -12,29 +13,40 @@ This program : * Daemonizes and waits for signal * Removes the virtual TTYs -In order to activate the CMUX mode the program sends some AT commands. Theses commands are mainly vendor specific and should be adapted to your own modem. The example in this code works for the Quectel M95 GSM module. +In order to activate the CMUX mode the program sends some AT commands. +Theses commands are mainly vendor specific and should be adapted to your own modem. +The example in this code works for the Quectel M95 GSM module. How to ------ -* In order to run this program you should have `n_gsm` module built for your linux kernel and loaded. Use `modprobe n_gsm` to load it. If it fails you probably have to build the kernel module by yourself. +* In order to run this program you should have `n_gsm` module built for your linux kernel and loaded. + Use `modprobe n_gsm` to load it. If it fails you probably have to build the kernel module by yourself. * Change the defined options in `cmux.c`: -```c -/* serial port of the modem */ -#define SERIAL_PORT "/dev/ttyS1" -/* line speed */ -#define LINE_SPEED B115200 +``` +$ ./cmux -h +Usage: cmux --device /dev/ttyUSB0 --speed 115200 + +--type SIM900, TELIT or default. (Default: default) +--device Serial device name. (Default: /dev/ttyUSB0) +--speed Serial device line speed. (Default: 115200) +--mtu MTU size. (Default: 512) +--debug [1|0] Enable debugging. (Default: 1) +--daemon [1|0] Fork into background. (Default: 1) +--driver Driver to use. (Default: gsmtty) +--base Base name for the nodes. (Default: /dev/ttyGSM) +--nodes [0-4] Number of nodes to create. (Default: 1) ``` * Change the AT commands set to fit your modem in `cmux.c`. -* Make and run the program. +* Call make and run the program. ``` make ./cmux ``` -It will create four devices: +It will create up to four devices: * /dev/ttyGSM1 * /dev/ttyGSM2 * /dev/ttyGSM3 @@ -45,3 +57,7 @@ You should now be able to access your modem with four TTYs and both use `ppp` an Links ----- This program is mainly an actual implementation of [GSM 0710 tty multiplexor HOWTO](http://stuff.mit.edu/afs/sipb/contrib/linux/Documentation/serial/n_gsm.txt) + +License +----- +CC0 diff --git a/cmux.c b/cmux.c index 1af0f50..3fe556e 100644 --- a/cmux.c +++ b/cmux.c @@ -2,7 +2,7 @@ * Cmux * Enables GSM 0710 multiplex using n_gsm * -* Copyright (C) 2013 - Rtone - Nicolas Le Manchet +* Copyright (C) 2013 - Rtone - Nicolas Le Manchet and others * * 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 @@ -22,19 +22,22 @@ #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. +#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" @@ -49,57 +52,50 @@ # define TIOCSETD 0x5423 #endif -/* serial port of the modem */ -#define SERIAL_PORT "/dev/ttyS1" - -/* line speed */ -#define LINE_SPEED B115200 + /* size of the reception buffer which gets data from the serial line */ +#define SIZE_BUF 256 -/* maximum transfert unit (MTU), value in bytes */ -#define MTU 512 - -/** -* whether or not to create virtual TTYs for the multiplex -* 0 : do not create -* 1 : create -*/ -#define CREATE_NODES 1 +char *g_type = "default"; /* number of virtual TTYs to create (most modems can handle up to 4) */ -#define NUM_NODES 4 +int g_nodes = 1; /* name of the virtual TTYs to create */ -#define BASENAME_NODES "/dev/ttyGSM" +char *g_base = "/dev/ttyGSM"; /* name of the driver, used to get the major number */ -#define DRIVER_NAME "gsmtty" +char *g_driver = "gsmtty"; + +/** +* whether or not to detach the program from the terminal +* 0 : do not daemonize +* 1 : daemonize +*/ +int g_daemon = 1; /** * whether or not to print debug messages to stderr * 0 : debug off * 1 : debug on */ -#define DEBUG 1 +int g_debug = 1; -/** -* whether or not to detach the program from the terminal -* 0 : do not daemonize -* 1 : daemonize -*/ -#define DAEMONIZE 1 +/* serial port of the modem */ +char *g_device = "/dev/ttyUSB0"; - /* size of the reception buffer which gets data from the serial line */ -#define SIZE_BUF 256 +/* line speed */ +int g_speed = 115200; +/* maximum transfer unit (MTU), value in bytes */ +int g_mtu = 512; /** * Prints debug messages to stderr if debug is wanted */ static void dbg(char *fmt, ...) { - va_list args; - if (DEBUG) { + if (g_debug) { fflush(NULL); va_start(args, fmt); vfprintf(stderr, fmt, args); @@ -116,14 +112,13 @@ static void dbg(char *fmt, ...) { * -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); - + err(EXIT_FAILURE, "Cannot write to %s", g_device); + /* wait a bit to allow the modem to rest */ sleep(1); @@ -131,8 +126,8 @@ int send_at_command(int serial_fd, char *command) { memset(buf, 0, sizeof(buf)); r = read(serial_fd, buf, sizeof(buf)); if (r == -1) - err(EXIT_FAILURE, "Cannot read %s", SERIAL_PORT); - + err(EXIT_FAILURE, "Cannot read %s", g_device); + /* if there is no result from the modem, return failure */ if (r == 0) { dbg("%s\t: No response", command); @@ -140,24 +135,25 @@ int send_at_command(int serial_fd, char *command) { } /* 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)); + if (g_debug) { + unsigned int i; + char bufp[sizeof(buf)+40]; + memset(bufp, 0, sizeof(bufp)); + snprintf(bufp, sizeof(bufp), "%s => %s", command, buf); + for(i=0; i SIM900, TELIT or default. (Default: %s)\n" + "--device Serial device name. (Default: %s)\n" + "--speed Serial device line speed. (Default: %d)\n" + "--mtu MTU size. (Default: %d)\n" + "--debug [1|0] Enable debugging. (Default: %d)\n" + "--daemon [1|0] Fork into background. (Default: %d)\n" + "--driver Driver to use. (Default: %s)\n" + "--base Base name for the nodes. (Default: %s)\n" + "--nodes [0-4] Number of nodes to create. (Default: %d)\n" + "\n", + g_type, g_device, g_speed, g_mtu, g_debug, + g_daemon, g_driver, g_base, g_nodes + ); +} + +int to_line_speed(int speed) { + switch(speed) { + case 2400: return B2400; + case 4800: return B4800; + case 9600: return B9600; + case 19200: return B19200; + case 38400: return B38400; + case 57600: return B57600; + case 115200: return B115200; + default: + errx(EXIT_FAILURE, "Invalid value for speed: %d", speed); + } +} + +// string lower case +char *to_lower(const char *str) { + int i; + + if (str == NULL) + return NULL; + + char *s = strdup(str); + for (i = 0; i < strlen(s); ++i) { + s[i] = tolower(s[i]); + } + + return s; +} + +int main(int argc, char **argv) { + int serial_fd, major, speed, i; struct termios tio; int ldisc = N_GSM0710; struct gsm_config gsm; char atcommand[40]; + for (i = 1; i < argc; ++i) { + char **args = &argv[i]; + + if (match(args[0], "-h")) { + print_help(); + return 0; + } + + if (handle_string_arg(args, &g_type, "--type") + || handle_string_arg(args, &g_device, "--device") + || handle_number_arg(args, &g_speed, "--speed") + || handle_number_arg(args, &g_mtu, "--mtu") + || handle_number_arg(args, &g_debug, "--debug") + || handle_number_arg(args, &g_daemon, "--daemon") + || handle_number_arg(args, &g_nodes, "--nodes") + || handle_string_arg(args, &g_driver, "--driver") + || handle_string_arg(args, &g_base, "--base")) { + i++; + } else { + errx(EXIT_FAILURE, "Unknown argument: %s", args[0]); + } + }; + + speed = to_line_speed(g_speed); + g_type = to_lower(g_type); + + if (strcmp(g_type, "default") && strcmp(g_type, "sim900") && strcmp(g_type, "telit")) + errx(EXIT_FAILURE, "Invalid value for --type: %s", g_type); + + if (g_daemon != 0 && g_daemon != 1) + errx(EXIT_FAILURE, "Invalid value for --daemon: %d", g_daemon); + + if (g_debug != 0 && g_debug != 1) + errx(EXIT_FAILURE, "Invalid value for --debug: %d", g_debug); + + if (g_nodes > 4) + errx(EXIT_FAILURE, "Invalid value for --nodes: %d", g_nodes); + + if (match(g_type, "sim900")) { + g_mtu = 255; + } else { + g_mtu = 512; + } + /* print global parameters */ - dbg("SERIAL_PORT = %s", SERIAL_PORT); + dbg( + "type: %s\n" + "device: %s\n" + "speed: %d\n" + "mtu: %d\n" + "debug: %d\n" + "daemon: %d\n" + "driver: %s\n" + "base: %s\n" + "nodes: %d\n", + g_type, g_device, g_speed, g_mtu, g_debug, + g_daemon, g_driver, g_nodes ? g_base : "disabled", g_nodes + ); /* open the serial port */ - serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY); + serial_fd = open(g_device, O_RDWR | O_NOCTTY | O_NDELAY); if (serial_fd == -1) - err(EXIT_FAILURE, "Cannot open %s", SERIAL_PORT); - + err(EXIT_FAILURE, "Cannot open %s", g_device); + /* get the current attributes of the serial port */ if (tcgetattr(serial_fd, &tio) == -1) err(EXIT_FAILURE, "Cannot get line attributes"); - - /* set the new attrbiutes */ + + /* set the new attributes */ tio.c_iflag = 0; tio.c_oflag = 0; tio.c_cflag = CS8 | CREAD | CLOCAL; @@ -298,37 +427,72 @@ int main(void) { tio.c_lflag = 0; tio.c_cc[VMIN] = 1; tio.c_cc[VTIME] = 0; - + /* write the speed of the serial line */ - if (cfsetospeed(&tio, LINE_SPEED) < 0 || cfsetispeed(&tio, LINE_SPEED) < 0) + if (cfsetospeed(&tio, speed) < 0 || cfsetispeed(&tio, speed) < 0) err(EXIT_FAILURE, "Cannot set line speed"); - + /* write the attributes */ if (tcsetattr(serial_fd, TCSANOW, &tio) == -1) err(EXIT_FAILURE, "Cannot set line attributes"); /** * Send AT commands to put the modem in CMUX mode. - * This is vendor specific and should be changed + * This is vendor specific and should be changed * to fit your modem needs. * The following matches Quectel M95. */ - if (send_at_command(serial_fd, "AT+IFC=2,2\r") == -1) - errx(EXIT_FAILURE, "AT+IFC=2,2: bad response"); - if (send_at_command(serial_fd, "AT+GMM\r") == -1) - warnx("AT+GMM: bad response"); - if (send_at_command(serial_fd, "AT\r") == -1) - warnx("AT: bad response"); - if (send_at_command(serial_fd, "AT+IPR=115200&w\r") == -1) - errx(EXIT_FAILURE, "AT+IPR=115200&w: bad response"); - sprintf(atcommand, "AT+CMUX=0,0,5,%d,10,3,30,10,2\r", MTU); - if (send_at_command(serial_fd, atcommand) == -1) - errx(EXIT_FAILURE, "Cannot enable modem CMUX"); + + if (match(g_type, "sim900")) { + if (send_at_command(serial_fd, "AAAT\r") == -1) + errx(EXIT_FAILURE, "AAAAT: bad response"); + } + + if (match(g_type, "telit")) { + if (send_at_command(serial_fd, "AT#SELINT=2\r") == -1) + errx(EXIT_FAILURE, "AT#SELINT=2: bad response"); + + if (send_at_command(serial_fd, "ATE0V1&K3&D2\r") == -1) + errx(EXIT_FAILURE, "ATE0V1&K3&D2: bad response"); + + sprintf(atcommand, "AT+IPR=%d\r", g_speed); + if (send_at_command(serial_fd, atcommand) == -1) + errx(EXIT_FAILURE, "AT+IPR=%d: bad response", g_speed); + + if (send_at_command(serial_fd, "AT#CMUXMODE=0\r") == -1) + errx(EXIT_FAILURE, "AT#CMUXMODE=0: bad response"); + + (void)send_at_command(serial_fd, "AT+CMUX=0\r"); + } else { + if (send_at_command(serial_fd, "AT+IFC=2,2\r") == -1) + errx(EXIT_FAILURE, "AT+IFC=2,2: bad response"); + + if (send_at_command(serial_fd, "AT+GMM\r") == -1) + warnx("AT+GMM: bad response"); + + if (send_at_command(serial_fd, "AT\r") == -1) + warnx("AT: bad response"); + + if (!match(g_type, "sim900")) { + sprintf(atcommand, "AT+IPR=%d&w\r", g_speed); + if (send_at_command(serial_fd, atcommand) == -1) + errx(EXIT_FAILURE, "AT+IPR=%d&w: bad response", g_speed); + } + + sprintf(atcommand, "AT+CMUX=0,0,5,%d,10,3,30,10,2\r", g_mtu); + if (send_at_command(serial_fd, atcommand) == -1) + errx(EXIT_FAILURE, "Cannot enable modem CMUX"); + } /* use n_gsm line discipline */ - sleep(2); + if (match(g_type, "sim900")) { + sleep(0.1); + } else { + sleep(2); + } + if (ioctl(serial_fd, TIOCSETD, &ldisc) < 0) - err(EXIT_FAILURE, "Cannot set line dicipline. Is 'n_gsm' module registred?"); + err(EXIT_FAILURE, "Cannot set line discipline. Is 'n_gsm' module registered?"); /* get n_gsm configuration */ if (ioctl(serial_fd, GSMIOC_GETCONF, &gsm) < 0) @@ -337,8 +501,8 @@ int main(void) { /* set and write new attributes */ gsm.initiator = 1; gsm.encapsulation = 0; - gsm.mru = MTU; - gsm.mtu = MTU; + gsm.mru = g_mtu; + gsm.mtu = g_mtu; gsm.t1 = 10; gsm.n2 = 3; gsm.t2 = 30; @@ -347,18 +511,18 @@ int main(void) { if (ioctl(serial_fd, GSMIOC_SETCONF, &gsm) < 0) err(EXIT_FAILURE, "Cannot set GSM multiplex parameters"); dbg("Line dicipline set"); - + /* create the virtual TTYs */ - if (CREATE_NODES) { + if (g_nodes > 0) { int created; - if ((major = get_major(DRIVER_NAME)) < 0) + if ((major = get_major(g_driver)) < 0) errx(EXIT_FAILURE, "Cannot get major number"); - if ((created = make_nodes(major, BASENAME_NODES, NUM_NODES)) < NUM_NODES) - warnx("Cannot create all nodes, only %d/%d have been created.", created, NUM_NODES); + if ((created = make_nodes(major, g_base, g_nodes)) < g_nodes) + warnx("Cannot create all nodes, only %d/%d have been created.", created, g_nodes); } /* detach from the terminal if needed */ - if (DAEMONIZE) { + if (g_daemon) { dbg("Going to background"); if (daemon(0,0) != 0) err(EXIT_FAILURE, "Cannot daemonize"); @@ -368,10 +532,10 @@ int main(void) { signal(SIGINT, signal_callback_handler); signal(SIGTERM, signal_callback_handler); pause(); - + /* remove the created virtual TTYs */ - if (CREATE_NODES) { - remove_nodes(BASENAME_NODES, NUM_NODES); + if (g_nodes > 0) { + remove_nodes(g_base, g_nodes); } /* close the serial line */ diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..e5c6253 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,25 @@ +cmux (1.2) stable; urgency=medium + + * most options are configurable now + * make Makefile cross compiler aware + * add support for TELIT modules + + -- Moritz Warning Sat, 08 Aug 2015 21:49:07 +0300 + +cmux (1.1) stable; urgency=medium + + * fix MTU setting. MTU set to 255 (maximum allowed for SIM900) + + -- Evgeny Boger Thu, 19 Feb 2015 21:03:32 +0300 + +cmux (1.0) stable; urgency=medium + + * Initial release. (https://github.com/contactless/cmux/) + + -- Evgeny Boger Sat, 20 Sep 2014 04:29:51 +0400 + +cmux (0.1) stable; urgency=medium + +* Initial code base. (https://github.com/Rtone/cmux) + + -- Nicolas Le Manchet diff --git a/debian/cmux.dirs b/debian/cmux.dirs new file mode 100644 index 0000000..98d1583 --- /dev/null +++ b/debian/cmux.dirs @@ -0,0 +1,2 @@ +usr/bin +usr/share/man/man1 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..ae7415b --- /dev/null +++ b/debian/control @@ -0,0 +1,11 @@ +Source: cmux +Maintainer: Evgeny Boger +Section: misc +Priority: optional +Standards-Version: 3.9.2 +Build-Depends: debhelper (>= 9) + +Package: cmux +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: Enables GSM 0710 multiplex using n_gsm line dicipline \ No newline at end of file diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..e69de29 diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..cbe925d --- /dev/null +++ b/debian/rules @@ -0,0 +1,3 @@ +#!/usr/bin/make -f +%: + dh $@ diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..9f67427 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (native) \ No newline at end of file