From e2f0f6be1ec979b509e23d76961cac89d417ae25 Mon Sep 17 00:00:00 2001 From: Evgeny Boger Date: Sun, 27 Oct 2013 04:31:27 +0400 Subject: [PATCH 01/17] Make daemon compatible with SIMCOM SIM900 --- cmux.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cmux.c b/cmux.c index 1af0f50..bedc0d7 100644 --- a/cmux.c +++ b/cmux.c @@ -50,7 +50,7 @@ #endif /* serial port of the modem */ -#define SERIAL_PORT "/dev/ttyS1" +#define SERIAL_PORT "/dev/ttyAPP0" /* line speed */ #define LINE_SPEED B115200 @@ -86,7 +86,7 @@ * 0 : do not daemonize * 1 : daemonize */ -#define DAEMONIZE 1 +#define DAEMONIZE 0 /* size of the reception buffer which gets data from the serial line */ #define SIZE_BUF 256 @@ -313,20 +313,24 @@ int main(void) { * to fit your modem needs. * The following matches Quectel M95. */ + if (send_at_command(serial_fd, "AAAT\r") == -1) + errx(EXIT_FAILURE, "AAAAT: bad response"); + 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, "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); + sprintf(atcommand, "AT+CMUX=0\r"); if (send_at_command(serial_fd, atcommand) == -1) errx(EXIT_FAILURE, "Cannot enable modem CMUX"); /* use n_gsm line discipline */ - sleep(2); + sleep(0.1); if (ioctl(serial_fd, TIOCSETD, &ldisc) < 0) err(EXIT_FAILURE, "Cannot set line dicipline. Is 'n_gsm' module registred?"); From c7198d0bd51624711888b93c387b78e726f1ef56 Mon Sep 17 00:00:00 2001 From: Evgeny Boger Date: Sun, 27 Oct 2013 04:51:39 +0400 Subject: [PATCH 02/17] daemonize --- cmux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmux.c b/cmux.c index bedc0d7..ef1fbd8 100644 --- a/cmux.c +++ b/cmux.c @@ -86,7 +86,7 @@ * 0 : do not daemonize * 1 : daemonize */ -#define DAEMONIZE 0 +#define DAEMONIZE 1 /* size of the reception buffer which gets data from the serial line */ #define SIZE_BUF 256 From dae43d986207c928cab23b8004cbf598db97596b Mon Sep 17 00:00:00 2001 From: Evgeny Boger Date: Fri, 13 Jun 2014 22:19:23 +0400 Subject: [PATCH 03/17] fix MTU according to SIM900R manual --- cmux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmux.c b/cmux.c index ef1fbd8..b91cebb 100644 --- a/cmux.c +++ b/cmux.c @@ -56,7 +56,7 @@ #define LINE_SPEED B115200 /* maximum transfert unit (MTU), value in bytes */ -#define MTU 512 +#define MTU 255 /** * whether or not to create virtual TTYs for the multiplex From 89aa15a3623e870879c28790a0e6566352896cc6 Mon Sep 17 00:00:00 2001 From: Evgeny Boger Date: Sat, 20 Sep 2014 13:44:19 +0400 Subject: [PATCH 04/17] Add debianization --- Makefile | 24 +++++++++++++++++------- debian/changelog | 5 +++++ debian/cmux.dirs | 2 ++ debian/compat | 1 + debian/control | 11 +++++++++++ debian/copyright | 0 debian/rules | 3 +++ debian/source/format | 1 + 8 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 debian/changelog create mode 100644 debian/cmux.dirs create mode 100644 debian/compat create mode 100644 debian/control create mode 100644 debian/copyright create mode 100755 debian/rules create mode 100644 debian/source/format diff --git a/Makefile b/Makefile index 5f0e916..6e36bf5 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,22 @@ -CC=gcc -CFLAGS=-Wall +DESTDIR=/ +prefix=usr -all: cmux +ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE)) + CC=gcc +else + CC=$(DEB_HOST_GNU_TYPE)-gcc +endif -program: cmux.c + +cmux: + $(CC) cmux.c -o cmux + +install: cmux + install -m 0755 cmux $(DESTDIR)/$(prefix)/bin/cmux clean: - rm -f cmux + -@rm cmux +.PHONY: install clean + + -run: cmux - ./cmux \ No newline at end of file diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..67feac6 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +cmux (1.0) stable; urgency=medium + + * Initial release. (https://github.com/contactless/cmux/) + + -- Evgeny Boger Sat, 20 Sep 2014 04:29:51 +0400 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 From 2756417bcb1c72a80321d0e8070a719890416f73 Mon Sep 17 00:00:00 2001 From: Evgeny Boger Date: Thu, 19 Feb 2015 21:04:17 +0300 Subject: [PATCH 05/17] fix mtu (http://contactless.ru/forums/topic/cmux/#post-3029 ) --- cmux.c | 50 +++++++++++++++++++++++++----------------------- debian/changelog | 6 ++++++ 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/cmux.c b/cmux.c index b91cebb..0fb6729 100644 --- a/cmux.c +++ b/cmux.c @@ -33,8 +33,8 @@ #include #include #include -/** -* gsmmux.h provides n_gsm line dicipline structures and functions. +/** +* gsmmux.h provides n_gsm line dicipline structures and functions. * It should be kept in sync with your kernel release. */ #include "gsmmux.h" @@ -96,7 +96,7 @@ * Prints debug messages to stderr if debug is wanted */ static void dbg(char *fmt, ...) { - + va_list args; if (DEBUG) { @@ -116,14 +116,14 @@ 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); - + /* wait a bit to allow the modem to rest */ sleep(1); @@ -132,7 +132,7 @@ int send_at_command(int serial_fd, char *command) { 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); @@ -155,8 +155,8 @@ int send_at_command(int serial_fd, char *command) { if (strstr(buf, "OK\r") != NULL) { return 0; } - - return -1; + + return -1; } @@ -180,20 +180,20 @@ int get_major(char *driver) { ssize_t read; char device[20]; int major = -1; - + /* open /proc/devices file */ if ((fp = fopen("/proc/devices", "r")) == NULL) err(EXIT_FAILURE, "Cannot open /proc/devices"); /* read the file line by line */ while ((major == -1) && (read = getline(&line, &len, fp)) != -1) { - + /* if the driver name string is found in the line, try to get the major */ if (strstr(line, driver) != NULL) { if (sscanf(line,"%d %s\n", &major, device) != 2) major = -1; } - + /* free the line before getting a new one */ if (line) { free(line); @@ -226,10 +226,10 @@ int make_nodes(int major, char *basename, int number_nodes) { /* append the minor number to the base name */ sprintf(node_name, "%s%d", basename, minor); - + /* store a device info with major and minor */ device = makedev(major, minor); - + /* create the actual character node */ if (mknod(node_name, S_IFCHR | 0666, device) != 0) { warn("Cannot create %s", node_name); @@ -259,7 +259,7 @@ void remove_nodes(char *basename, int number_nodes) { /* append the minor number to the base name */ sprintf(node_name, "%s%d", basename, node); - + /* unlink the actual character node */ dbg("Removing %s", node_name); if (unlink(node_name) == -1) @@ -285,11 +285,11 @@ int main(void) { serial_fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY); if (serial_fd == -1) err(EXIT_FAILURE, "Cannot open %s", SERIAL_PORT); - + /* 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 */ tio.c_iflag = 0; tio.c_oflag = 0; @@ -298,18 +298,18 @@ 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) 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. */ @@ -317,15 +317,17 @@ int main(void) { errx(EXIT_FAILURE, "AAAAT: bad response"); if (send_at_command(serial_fd, "AT+IFC=2,2\r") == -1) - errx(EXIT_FAILURE, "AT+IFC=2,2: bad response"); + 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); - sprintf(atcommand, "AT+CMUX=0\r"); + + // Set CMUX options: port_speed=115200, frame_size=MTU + 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"); @@ -351,7 +353,7 @@ 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) { int created; @@ -372,7 +374,7 @@ 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); diff --git a/debian/changelog b/debian/changelog index 67feac6..78d11dd 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +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/) From 8a473e2d4144c7c232d779d912c666e4f4ebd566 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Mon, 29 Jun 2015 11:49:55 +0200 Subject: [PATCH 06/17] make options configurable and add support for telit modules --- Makefile | 22 ++-- README.md | 32 ++++-- cmux.c | 293 +++++++++++++++++++++++++++++++++++------------ debian/changelog | 12 ++ 4 files changed, 265 insertions(+), 94 deletions(-) diff --git a/Makefile b/Makefile index 6e36bf5..545139c 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,16 @@ -DESTDIR=/ -prefix=usr -ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE)) - CC=gcc -else - CC=$(DEB_HOST_GNU_TYPE)-gcc -endif +CC?=gcc +CFLAGS?=-Wall +DESTDIR?=/ +PREFIX?=usr - -cmux: - $(CC) cmux.c -o cmux +cmux: cmux.c + $(CC) $(CFLAGS) cmux.c -o cmux install: cmux - install -m 0755 cmux $(DESTDIR)/$(prefix)/bin/cmux + install -m 0755 cmux $(DESTDIR)/$(PREFIX)/bin/cmux clean: -@rm cmux -.PHONY: install clean - - +.PHONY: install clean diff --git a/README.md b/README.md index 9f7026f..3517d2e 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,42 @@ 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 + +--gsm 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 + +Usage: cmux --device /dev/ttyUSB0 --speed 115200 ``` * 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 diff --git a/cmux.c b/cmux.c index 0fb6729..d7bfbed 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 @@ -33,6 +33,7 @@ #include #include #include +#include /** * gsmmux.h provides n_gsm line dicipline structures and functions. * It should be kept in sync with your kernel release. @@ -49,57 +50,50 @@ # define TIOCSETD 0x5423 #endif -/* serial port of the modem */ -#define SERIAL_PORT "/dev/ttyAPP0" - -/* line speed */ -#define LINE_SPEED B115200 - -/* maximum transfert unit (MTU), value in bytes */ -#define MTU 255 + /* size of the reception buffer which gets data from the serial line */ +#define SIZE_BUF 256 -/** -* whether or not to create virtual TTYs for the multiplex -* 0 : do not create -* 1 : create -*/ -#define CREATE_NODES 1 +char *g_gsm = "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 transfert 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,13 +110,12 @@ 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,7 +124,7 @@ 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) { @@ -140,7 +133,7 @@ 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) { + if (g_debug) { int i; char bufp[SIZE_BUF]; memcpy(bufp, buf, sizeof(buf)); @@ -157,7 +150,6 @@ int send_at_command(int serial_fd, char *command) { } return -1; - } /** @@ -173,7 +165,6 @@ void signal_callback_handler(int signum) { * -1 on failure */ int get_major(char *driver) { - FILE *fp; char *line = NULL; size_t len = 0; @@ -199,7 +190,6 @@ int get_major(char *driver) { free(line); line = NULL; } - } /* close /proc/devices file */ @@ -213,7 +203,6 @@ int get_major(char *driver) { * Returns the number of nodes created */ int make_nodes(int major, char *basename, int number_nodes) { - int minor, created = 0; dev_t device; char node_name[15]; @@ -237,7 +226,6 @@ int make_nodes(int major, char *basename, int number_nodes) { created++; dbg("Created %s", node_name); } - } /* revert the mask to the old one */ @@ -251,9 +239,8 @@ int make_nodes(int major, char *basename, int number_nodes) { * Returns nothing, it doesn't really matter if it fails */ void remove_nodes(char *basename, int number_nodes) { - - int node; char node_name[15]; + int node; for (node=1; node 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_gsm, 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_gsm, "--gsm") + || 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]); + } + }; + + 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); + + speed = to_line_speed(g_speed); + g_gsm = to_lower(g_gsm); + + if (match(g_gsm, "sim900")) { + g_mtu = 255; + } else { + g_mtu = 512; + } + /* print global parameters */ - dbg("SERIAL_PORT = %s", SERIAL_PORT); + dbg( + "gsm: %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_gsm, 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) @@ -300,7 +422,7 @@ int main(void) { 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 */ @@ -313,26 +435,55 @@ int main(void) { * to fit your modem needs. * The following matches Quectel M95. */ - if (send_at_command(serial_fd, "AAAT\r") == -1) - errx(EXIT_FAILURE, "AAAAT: bad response"); - 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"); + if (match(g_gsm, "sim900")) { + if (send_at_command(serial_fd, "AAAT\r") == -1) + errx(EXIT_FAILURE, "AAAAT: bad response"); + } + + if (match(g_gsm, "telit")) { + if (send_at_command(serial_fd, "AT#SELINT=2\r") == -1) + errx(EXIT_FAILURE, "AT#SELINT=2: bad response"); - // Set CMUX options: port_speed=115200, frame_size=MTU - sprintf(atcommand, "AT+CMUX=0,0,5,%d,10,3,30,10,2\r", MTU); + if (send_at_command(serial_fd, "ATE0V1&K3&D2\r") == -1) + errx(EXIT_FAILURE, "ATE0V1&K3&D2: bad response"); - if (send_at_command(serial_fd, atcommand) == -1) - errx(EXIT_FAILURE, "Cannot enable modem CMUX"); + 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_gsm, "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(0.1); + if (match(g_gsm, "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?"); @@ -343,8 +494,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; @@ -355,16 +506,16 @@ int main(void) { 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"); @@ -376,8 +527,8 @@ int main(void) { 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 index 78d11dd..0eeaa97 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +cmux (1.2) stable; urgency=medium + + * most options are configurable now + * make Makefile cross compiler aware + * add support for TELIT modules + cmux (1.1) stable; urgency=medium * fix MTU setting. MTU set to 255 (maximum allowed for SIM900) @@ -9,3 +15,9 @@ 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 From 9caa8710eb310ec01e9a7172925e9a9936c9640e Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Mon, 29 Jun 2015 15:42:09 +0200 Subject: [PATCH 07/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3517d2e..8965f26 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Usage: cmux --device /dev/ttyUSB0 --speed 115200 --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 +--nodes [0-4] Number of nodes to create. (Default: 1) Usage: cmux --device /dev/ttyUSB0 --speed 115200 ``` From 6a464401960279c615f6cd6e8c9cca15c0920202 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Tue, 7 Jul 2015 20:34:10 +0200 Subject: [PATCH 08/17] fix whitespace --- cmux.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmux.c b/cmux.c index d7bfbed..f5ba588 100644 --- a/cmux.c +++ b/cmux.c @@ -327,13 +327,13 @@ int to_line_speed(int speed) { // string lower case char *to_lower(const char *str) { - int i; + int i; if(str == NULL) return NULL; char *s = strdup(str); - for (i = 0; i < strlen(s); ++i) { + for (i = 0; i < strlen(s); ++i) { s[i] = tolower(s[i]); } From 6e7ff48cc3bd70bcf89118f7f5b8bdf9553f7e6b Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Tue, 7 Jul 2015 20:34:24 +0200 Subject: [PATCH 09/17] fix readme --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 8965f26..44fbb9c 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,6 @@ Usage: cmux --device /dev/ttyUSB0 --speed 115200 --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) - -Usage: cmux --device /dev/ttyUSB0 --speed 115200 ``` * Change the AT commands set to fit your modem in `cmux.c`. From a0912b11ca83a970ec73a5b6921e30a3b2ae94ca Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Tue, 7 Jul 2015 20:39:38 +0200 Subject: [PATCH 10/17] validate --gsm argument --- cmux.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmux.c b/cmux.c index f5ba588..df07c02 100644 --- a/cmux.c +++ b/cmux.c @@ -370,6 +370,12 @@ int main(int argc, char **argv) { } }; + speed = to_line_speed(g_speed); + g_gsm = to_lower(g_gsm); + + if(strcmp(g_gsm, "default") && strcmp(g_gsm, "sim900") && strcmp(g_gsm, "telit")) + errx(EXIT_FAILURE, "Invalid value for --gsm: %s", g_gsm); + if(g_daemon != 0 && g_daemon != 1) errx(EXIT_FAILURE, "Invalid value for --daemon: %d", g_daemon); @@ -379,9 +385,6 @@ int main(int argc, char **argv) { if(g_nodes > 4) errx(EXIT_FAILURE, "Invalid value for --nodes: %d", g_nodes); - speed = to_line_speed(g_speed); - g_gsm = to_lower(g_gsm); - if (match(g_gsm, "sim900")) { g_mtu = 255; } else { From 6277433913d1c70a181afda96cd51636180a4d51 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Tue, 7 Jul 2015 20:40:39 +0200 Subject: [PATCH 11/17] code style fixes --- cmux.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmux.c b/cmux.c index df07c02..489f721 100644 --- a/cmux.c +++ b/cmux.c @@ -137,7 +137,7 @@ int send_at_command(int serial_fd, char *command) { int i; char bufp[SIZE_BUF]; memcpy(bufp, buf, sizeof(buf)); - for(i=0; i 4) + if (g_nodes > 4) errx(EXIT_FAILURE, "Invalid value for --nodes: %d", g_nodes); if (match(g_gsm, "sim900")) { From 75bbfe676cfca9e974eca600028f5f014e7fff86 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Tue, 7 Jul 2015 20:44:28 +0200 Subject: [PATCH 12/17] add license --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 44fbb9c..f657310 100644 --- a/README.md +++ b/README.md @@ -57,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 From 34ae3726a0d6db01e56e655869f3465df663b546 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Mon, 13 Jul 2015 17:25:34 +0200 Subject: [PATCH 13/17] cleaner debug output --- cmux.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cmux.c b/cmux.c index 489f721..104e3c3 100644 --- a/cmux.c +++ b/cmux.c @@ -134,14 +134,16 @@ 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 (g_debug) { - int i; - char bufp[SIZE_BUF]; - memcpy(bufp, buf, sizeof(buf)); - for (i=0; i %s", command, buf); + + for(i=0; i Date: Mon, 13 Jul 2015 17:27:53 +0200 Subject: [PATCH 14/17] minor cleanup --- cmux.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cmux.c b/cmux.c index 104e3c3..be43df3 100644 --- a/cmux.c +++ b/cmux.c @@ -22,18 +22,19 @@ #include #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. @@ -417,7 +418,7 @@ int main(int argc, char **argv) { 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; @@ -490,7 +491,7 @@ int main(int argc, char **argv) { } 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) From e8abe8db42c1b8f736b2e06423d135ecd2d31c66 Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Thu, 16 Jul 2015 09:22:36 +0200 Subject: [PATCH 15/17] --gsm => --type --- README.md | 2 +- cmux.c | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f657310..eae9759 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ How to $ ./cmux -h Usage: cmux --device /dev/ttyUSB0 --speed 115200 ---gsm SIM900, TELIT or default. (Default: default) +--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) diff --git a/cmux.c b/cmux.c index be43df3..6f6a58b 100644 --- a/cmux.c +++ b/cmux.c @@ -54,7 +54,7 @@ /* size of the reception buffer which gets data from the serial line */ #define SIZE_BUF 256 -char *g_gsm = "default"; +char *g_type = "default"; /* number of virtual TTYs to create (most modems can handle up to 4) */ int g_nodes = 1; @@ -299,7 +299,7 @@ int handle_number_arg(char **args, int *val, const char *opt) { void print_help() { printf( "Usage: cmux --device /dev/ttyUSB0 --speed 115200\n\n" - "--gsm SIM900, TELIT or default. (Default: %s)\n" + "--type 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" @@ -309,7 +309,7 @@ void print_help() { "--base Base name for the nodes. (Default: %s)\n" "--nodes [0-4] Number of nodes to create. (Default: %d)\n" "\n", - g_gsm, g_device, g_speed, g_mtu, g_debug, + g_type, g_device, g_speed, g_mtu, g_debug, g_daemon, g_driver, g_base, g_nodes ); } @@ -358,7 +358,7 @@ int main(int argc, char **argv) { return 0; } - if (handle_string_arg(args, &g_gsm, "--gsm") + 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") @@ -374,10 +374,10 @@ int main(int argc, char **argv) { }; speed = to_line_speed(g_speed); - g_gsm = to_lower(g_gsm); + g_type = to_lower(g_type); - if (strcmp(g_gsm, "default") && strcmp(g_gsm, "sim900") && strcmp(g_gsm, "telit")) - errx(EXIT_FAILURE, "Invalid value for --gsm: %s", g_gsm); + 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); @@ -388,7 +388,7 @@ int main(int argc, char **argv) { if (g_nodes > 4) errx(EXIT_FAILURE, "Invalid value for --nodes: %d", g_nodes); - if (match(g_gsm, "sim900")) { + if (match(g_type, "sim900")) { g_mtu = 255; } else { g_mtu = 512; @@ -396,7 +396,7 @@ int main(int argc, char **argv) { /* print global parameters */ dbg( - "gsm: %s\n" + "type: %s\n" "device: %s\n" "speed: %d\n" "mtu: %d\n" @@ -405,7 +405,7 @@ int main(int argc, char **argv) { "driver: %s\n" "base: %s\n" "nodes: %d\n", - g_gsm, g_device, g_speed, g_mtu, g_debug, + g_type, g_device, g_speed, g_mtu, g_debug, g_daemon, g_driver, g_nodes ? g_base : "disabled", g_nodes ); @@ -442,12 +442,12 @@ int main(int argc, char **argv) { * The following matches Quectel M95. */ - if (match(g_gsm, "sim900")) { + if (match(g_type, "sim900")) { if (send_at_command(serial_fd, "AAAT\r") == -1) errx(EXIT_FAILURE, "AAAAT: bad response"); } - if (match(g_gsm, "telit")) { + if (match(g_type, "telit")) { if (send_at_command(serial_fd, "AT#SELINT=2\r") == -1) errx(EXIT_FAILURE, "AT#SELINT=2: bad response"); @@ -472,7 +472,7 @@ int main(int argc, char **argv) { if (send_at_command(serial_fd, "AT\r") == -1) warnx("AT: bad response"); - if (!match(g_gsm, "sim900")) { + 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); @@ -484,7 +484,7 @@ int main(int argc, char **argv) { } /* use n_gsm line discipline */ - if (match(g_gsm, "sim900")) { + if (match(g_type, "sim900")) { sleep(0.1); } else { sleep(2); From bf3b74f06e753d24c6efc514bba07c34cae4cfc9 Mon Sep 17 00:00:00 2001 From: Evgeny Boger Date: Sat, 8 Aug 2015 22:02:35 +0300 Subject: [PATCH 16/17] fix changelog entry --- debian/changelog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/debian/changelog b/debian/changelog index 0eeaa97..e5c6253 100644 --- a/debian/changelog +++ b/debian/changelog @@ -4,6 +4,8 @@ cmux (1.2) stable; urgency=medium * 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) From e8e8f2c9b1ccba77663b51c02a2eff06c2281bde Mon Sep 17 00:00:00 2001 From: Moritz Warning Date: Sun, 25 Jun 2023 15:00:50 +0200 Subject: [PATCH 17/17] minor fixes --- cmux.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmux.c b/cmux.c index 6f6a58b..3fe556e 100644 --- a/cmux.c +++ b/cmux.c @@ -34,6 +34,7 @@ #include #include #include +#include /** * gsmmux.h provides n_gsm line dicipline structures and functions. @@ -85,7 +86,7 @@ char *g_device = "/dev/ttyUSB0"; /* line speed */ int g_speed = 115200; -/* maximum transfert unit (MTU), value in bytes */ +/* maximum transfer unit (MTU), value in bytes */ int g_mtu = 512; /**