diff mbox

[OpenWrt-Devel] comgt: Allow using non-TTY devices

Message ID 1421168424-27146-1-git-send-email-malaakso@elisanet.fi
State Changes Requested
Headers show

Commit Message

Matti Laakso Jan. 13, 2015, 5 p.m. UTC
Some Huawei mobile broadband sticks utilizing the NCM protocol expose
the control channel as a cdc-wdm device node instead of a virtual TTY.
This device node does not support the terminal ioctls. This patch
adds a check whether the provided device is a TTY or not and does not
attempt to use the terminal ioctls if they are not supported.

Signed-off-by: Matti Laakso <malaakso@elisanet.fi>
---

Comments

John Crispin Jan. 17, 2015, 7:11 a.m. UTC | #1
On 13/01/2015 18:00, Matti Laakso wrote:
> Some Huawei mobile broadband sticks utilizing the NCM protocol expose
> the control channel as a cdc-wdm device node instead of a virtual TTY.
> This device node does not support the terminal ioctls. This patch
> adds a check whether the provided device is a TTY or not and does not
> attempt to use the terminal ioctls if they are not supported.
> 
> Signed-off-by: Matti Laakso <malaakso@elisanet.fi>
> ---
> diff --git a/package/network/utils/comgt/patches/004-check_tty.patch b/package/network/utils/comgt/patches/004-check_tty.patch
> new file mode 100644
> index 0000000..d269bce
> --- /dev/null
> +++ b/package/network/utils/comgt/patches/004-check_tty.patch
> @@ -0,0 +1,177 @@
> +--- a/comgt.c
> ++++ b/comgt.c
> +@@ -91,6 +91,7 @@ unsigned long hstart,hset;
> + char NullString[]={ "" };
> + BOOL lastcharnl=1; /* Indicate that last char printed from getonebyte
> +                                was a nl, so no new one is needed */
> ++BOOL tty=1;
> + 
> + 
> + //"open com \"/dev/modem\"\nset com 38400n81\nset senddelay 0.05\nsend \"ATi^m\"\nget 2 \" ^m\" $s\nprint \"Response : \",$s,\"\\n\"\nget 2 \" ^m\" $s\nprint \"Response :\",$s,\"\\n\"\nget 2 \" ^m\" $s\nprint \"Response : \",$s,\"\\n\"\n\n";
> +@@ -918,10 +919,12 @@ BOOL getonoroff(void) {
> + }
> + 
> + void setcom(void) {
> +-  stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
> +-  stbuf.c_cflag |= (speed | bits | CREAD | clocal | parity | stopbits );
> +-  if (ioctl(comfd, TCSETA, &stbuf) < 0) {
> +-    serror("Can't ioctl set device",1);
> ++  if (tty) {
> ++    stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
> ++    stbuf.c_cflag |= (speed | bits | CREAD | clocal | parity | stopbits );
> ++    if (ioctl(comfd, TCSETA, &stbuf) < 0) {
> ++      serror("Can't ioctl set device",1);
> ++    }
> +   }
> + }
> + 
> +@@ -1224,9 +1227,11 @@ void doclose(void) {
> +   if(strcmp(token,"hardcom")==0) {
> +     if(comfd== -1) serror("Com device not open",1);
> +     vmsg("Closing device");
> +-    if (ioctl(comfd, TCSETA, &svbuf) < 0) {
> +-      sprintf(msg,"Can't ioctl set device %s.\n",device);
> +-      serror(msg,1);
> ++    if (tty) {
> ++      if (ioctl(comfd, TCSETA, &svbuf) < 0) {
> ++        sprintf(msg,"Can't ioctl set device %s.\n",device);
> ++        serror(msg,1);
> ++      }
> +     }


if (tty && (ioctl(comfd, TCSETA, &svbuf) < 0)) .....


i think this pattern would drastically reduce the diffstat




> +     close(comfd);
> +     comfd= -1;
> +@@ -1266,26 +1271,32 @@ void opengt(void) {
> +       ext(1);
> +     }
> +   }
> +-  if (ioctl (comfd, TCGETA, &svbuf) < 0) {
> +-    sprintf(msg,"Can't control %s, please try again.\n",device);
> +-    serror(msg,1);
> ++  if (isatty (comfd))
> ++    tty=1;
> ++  else
> ++    tty=0;
> ++  if (tty) {
> ++    if (ioctl (comfd, TCGETA, &svbuf) < 0) {
> ++      sprintf(msg,"Can't control %s, please try again.\n",device);
> ++      serror(msg,1);
> ++    }
> ++    ioctl(comfd, TCGETA, &stbuf);
> ++    speed=stbuf.c_cflag & CBAUD;
> ++    if (high_speed == 0)  strcpy(cspeed,"115200");
> ++    else strcpy(cspeed,"57600");
> ++    bits=stbuf.c_cflag & CSIZE;
> ++    clocal=stbuf.c_cflag & CLOCAL;
> ++    stopbits=stbuf.c_cflag & CSTOPB;
> ++    parity=stbuf.c_cflag & (PARENB | PARODD);
> ++    stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
> ++    stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
> ++    stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
> ++    stbuf.c_lflag &= ~(ECHO | ECHOE);
> ++    stbuf.c_cc[VMIN] = 1;
> ++    stbuf.c_cc[VTIME] = 0;
> ++    stbuf.c_cc[VEOF] = 1;
> +   }
> +   setenv("COMGTDEVICE",device,1);
> +-  ioctl(comfd, TCGETA, &stbuf);
> +-  speed=stbuf.c_cflag & CBAUD;
> +-  if (high_speed == 0)  strcpy(cspeed,"115200");
> +-  else strcpy(cspeed,"57600");
> +-  bits=stbuf.c_cflag & CSIZE;
> +-  clocal=stbuf.c_cflag & CLOCAL;
> +-  stopbits=stbuf.c_cflag & CSTOPB;
> +-  parity=stbuf.c_cflag & (PARENB | PARODD);
> +-  stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
> +-  stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
> +-  stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
> +-  stbuf.c_lflag &= ~(ECHO | ECHOE);
> +-  stbuf.c_cc[VMIN] = 1;
> +-  stbuf.c_cc[VTIME] = 0;
> +-  stbuf.c_cc[VEOF] = 1;
> +   setcom();
> +   dormir(200000); /* Wait a bit (DTR raise) */
> +   sprintf(msg,"Opened %s as FD %d",device,comfd);
> +@@ -1302,45 +1313,50 @@ void opendevice(void) {
> +     }
> +   }
> +   else comfd=0;
> +-
> +-  if (ioctl (comfd, TCGETA, &svbuf) < 0) {
> +-    sprintf(msg,"Can't ioctl get device %s.\n",device);
> +-    serror(msg,1);
> +-  }
> +-  ioctl(comfd, TCGETA, &stbuf);
> +-  speed=stbuf.c_cflag & CBAUD;
> +-  switch(speed) {
> +-    case B0: strcpy(cspeed,"0");break;
> +-    case B50: strcpy(cspeed,"50");break;
> +-    case B75: strcpy(cspeed,"75");break;
> +-    case B110: strcpy(cspeed,"110");break;
> +-    case B300: strcpy(cspeed,"300");break;
> +-    case B600: strcpy(cspeed,"600");break;
> +-    case B1200: strcpy(cspeed,"1200");break;
> +-    case B2400: strcpy(cspeed,"2400");break;
> +-    case B4800: strcpy(cspeed,"4800");break;
> +-    case B9600: strcpy(cspeed,"9600");break;
> +-    case B19200: strcpy(cspeed,"19200");break;
> +-    case B38400: strcpy(cspeed,"38400");break;
> +-    case B115200:
> ++  if (isatty (comfd))
> ++    tty=1;
> ++  else
> ++    tty=0;
> ++  if (tty) {
> ++    if (ioctl (comfd, TCGETA, &svbuf) < 0) {
> ++      sprintf(msg,"Can't ioctl get device %s.\n",device);
> ++      serror(msg,1);
> ++    }
> ++    ioctl(comfd, TCGETA, &stbuf);
> ++    speed=stbuf.c_cflag & CBAUD;
> ++    switch(speed) {
> ++      case B0: strcpy(cspeed,"0");break;
> ++      case B50: strcpy(cspeed,"50");break;
> ++      case B75: strcpy(cspeed,"75");break;
> ++      case B110: strcpy(cspeed,"110");break;
> ++      case B300: strcpy(cspeed,"300");break;
> ++      case B600: strcpy(cspeed,"600");break;
> ++      case B1200: strcpy(cspeed,"1200");break;
> ++      case B2400: strcpy(cspeed,"2400");break;
> ++      case B4800: strcpy(cspeed,"4800");break;
> ++      case B9600: strcpy(cspeed,"9600");break;
> ++      case B19200: strcpy(cspeed,"19200");break;
> ++      case B38400: strcpy(cspeed,"38400");break;
> ++      case B115200:
> +                 {
> +                   if (high_speed == 0) strcpy(cspeed,"115200");
> +                   else strcpy(cspeed,"57600");
> +                   break;
> +                 }
> +-    case B460800: strcpy(cspeed, "460800");break;
> ++      case B460800: strcpy(cspeed, "460800");break;
> ++    }
> ++    bits=stbuf.c_cflag & CSIZE;
> ++    clocal=stbuf.c_cflag & CLOCAL;
> ++    stopbits=stbuf.c_cflag & CSTOPB;
> ++    parity=stbuf.c_cflag & (PARENB | PARODD);
> ++    stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
> ++    stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
> ++    stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
> ++    stbuf.c_lflag &= ~(ECHO | ECHOE);
> ++    stbuf.c_cc[VMIN] = 1;
> ++    stbuf.c_cc[VTIME] = 0;
> ++    stbuf.c_cc[VEOF] = 1;
> +   }
> +-  bits=stbuf.c_cflag & CSIZE;
> +-  clocal=stbuf.c_cflag & CLOCAL;
> +-  stopbits=stbuf.c_cflag & CSTOPB;
> +-  parity=stbuf.c_cflag & (PARENB | PARODD);
> +-  stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
> +-  stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
> +-  stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
> +-  stbuf.c_lflag &= ~(ECHO | ECHOE);
> +-  stbuf.c_cc[VMIN] = 1;
> +-  stbuf.c_cc[VTIME] = 0;
> +-  stbuf.c_cc[VEOF] = 1;
> +   setcom();
> +   dormir(200000); /* Wait a bit (DTR raise) */
> +   sprintf(msg,"Opened %s as FD %d",device,comfd);
>
diff mbox

Patch

diff --git a/package/network/utils/comgt/patches/004-check_tty.patch b/package/network/utils/comgt/patches/004-check_tty.patch
new file mode 100644
index 0000000..d269bce
--- /dev/null
+++ b/package/network/utils/comgt/patches/004-check_tty.patch
@@ -0,0 +1,177 @@ 
+--- a/comgt.c
++++ b/comgt.c
+@@ -91,6 +91,7 @@ unsigned long hstart,hset;
+ char NullString[]={ "" };
+ BOOL lastcharnl=1; /* Indicate that last char printed from getonebyte
+                                was a nl, so no new one is needed */
++BOOL tty=1;
+ 
+ 
+ //"open com \"/dev/modem\"\nset com 38400n81\nset senddelay 0.05\nsend \"ATi^m\"\nget 2 \" ^m\" $s\nprint \"Response : \",$s,\"\\n\"\nget 2 \" ^m\" $s\nprint \"Response :\",$s,\"\\n\"\nget 2 \" ^m\" $s\nprint \"Response : \",$s,\"\\n\"\n\n";
+@@ -918,10 +919,12 @@ BOOL getonoroff(void) {
+ }
+ 
+ void setcom(void) {
+-  stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
+-  stbuf.c_cflag |= (speed | bits | CREAD | clocal | parity | stopbits );
+-  if (ioctl(comfd, TCSETA, &stbuf) < 0) {
+-    serror("Can't ioctl set device",1);
++  if (tty) {
++    stbuf.c_cflag &= ~(CBAUD | CSIZE | CSTOPB | CLOCAL | PARENB);
++    stbuf.c_cflag |= (speed | bits | CREAD | clocal | parity | stopbits );
++    if (ioctl(comfd, TCSETA, &stbuf) < 0) {
++      serror("Can't ioctl set device",1);
++    }
+   }
+ }
+ 
+@@ -1224,9 +1227,11 @@ void doclose(void) {
+   if(strcmp(token,"hardcom")==0) {
+     if(comfd== -1) serror("Com device not open",1);
+     vmsg("Closing device");
+-    if (ioctl(comfd, TCSETA, &svbuf) < 0) {
+-      sprintf(msg,"Can't ioctl set device %s.\n",device);
+-      serror(msg,1);
++    if (tty) {
++      if (ioctl(comfd, TCSETA, &svbuf) < 0) {
++        sprintf(msg,"Can't ioctl set device %s.\n",device);
++        serror(msg,1);
++      }
+     }
+     close(comfd);
+     comfd= -1;
+@@ -1266,26 +1271,32 @@ void opengt(void) {
+       ext(1);
+     }
+   }
+-  if (ioctl (comfd, TCGETA, &svbuf) < 0) {
+-    sprintf(msg,"Can't control %s, please try again.\n",device);
+-    serror(msg,1);
++  if (isatty (comfd))
++    tty=1;
++  else
++    tty=0;
++  if (tty) {
++    if (ioctl (comfd, TCGETA, &svbuf) < 0) {
++      sprintf(msg,"Can't control %s, please try again.\n",device);
++      serror(msg,1);
++    }
++    ioctl(comfd, TCGETA, &stbuf);
++    speed=stbuf.c_cflag & CBAUD;
++    if (high_speed == 0)  strcpy(cspeed,"115200");
++    else strcpy(cspeed,"57600");
++    bits=stbuf.c_cflag & CSIZE;
++    clocal=stbuf.c_cflag & CLOCAL;
++    stopbits=stbuf.c_cflag & CSTOPB;
++    parity=stbuf.c_cflag & (PARENB | PARODD);
++    stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
++    stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
++    stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
++    stbuf.c_lflag &= ~(ECHO | ECHOE);
++    stbuf.c_cc[VMIN] = 1;
++    stbuf.c_cc[VTIME] = 0;
++    stbuf.c_cc[VEOF] = 1;
+   }
+   setenv("COMGTDEVICE",device,1);
+-  ioctl(comfd, TCGETA, &stbuf);
+-  speed=stbuf.c_cflag & CBAUD;
+-  if (high_speed == 0)  strcpy(cspeed,"115200");
+-  else strcpy(cspeed,"57600");
+-  bits=stbuf.c_cflag & CSIZE;
+-  clocal=stbuf.c_cflag & CLOCAL;
+-  stopbits=stbuf.c_cflag & CSTOPB;
+-  parity=stbuf.c_cflag & (PARENB | PARODD);
+-  stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
+-  stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
+-  stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
+-  stbuf.c_lflag &= ~(ECHO | ECHOE);
+-  stbuf.c_cc[VMIN] = 1;
+-  stbuf.c_cc[VTIME] = 0;
+-  stbuf.c_cc[VEOF] = 1;
+   setcom();
+   dormir(200000); /* Wait a bit (DTR raise) */
+   sprintf(msg,"Opened %s as FD %d",device,comfd);
+@@ -1302,45 +1313,50 @@ void opendevice(void) {
+     }
+   }
+   else comfd=0;
+-
+-  if (ioctl (comfd, TCGETA, &svbuf) < 0) {
+-    sprintf(msg,"Can't ioctl get device %s.\n",device);
+-    serror(msg,1);
+-  }
+-  ioctl(comfd, TCGETA, &stbuf);
+-  speed=stbuf.c_cflag & CBAUD;
+-  switch(speed) {
+-    case B0: strcpy(cspeed,"0");break;
+-    case B50: strcpy(cspeed,"50");break;
+-    case B75: strcpy(cspeed,"75");break;
+-    case B110: strcpy(cspeed,"110");break;
+-    case B300: strcpy(cspeed,"300");break;
+-    case B600: strcpy(cspeed,"600");break;
+-    case B1200: strcpy(cspeed,"1200");break;
+-    case B2400: strcpy(cspeed,"2400");break;
+-    case B4800: strcpy(cspeed,"4800");break;
+-    case B9600: strcpy(cspeed,"9600");break;
+-    case B19200: strcpy(cspeed,"19200");break;
+-    case B38400: strcpy(cspeed,"38400");break;
+-    case B115200:
++  if (isatty (comfd))
++    tty=1;
++  else
++    tty=0;
++  if (tty) {
++    if (ioctl (comfd, TCGETA, &svbuf) < 0) {
++      sprintf(msg,"Can't ioctl get device %s.\n",device);
++      serror(msg,1);
++    }
++    ioctl(comfd, TCGETA, &stbuf);
++    speed=stbuf.c_cflag & CBAUD;
++    switch(speed) {
++      case B0: strcpy(cspeed,"0");break;
++      case B50: strcpy(cspeed,"50");break;
++      case B75: strcpy(cspeed,"75");break;
++      case B110: strcpy(cspeed,"110");break;
++      case B300: strcpy(cspeed,"300");break;
++      case B600: strcpy(cspeed,"600");break;
++      case B1200: strcpy(cspeed,"1200");break;
++      case B2400: strcpy(cspeed,"2400");break;
++      case B4800: strcpy(cspeed,"4800");break;
++      case B9600: strcpy(cspeed,"9600");break;
++      case B19200: strcpy(cspeed,"19200");break;
++      case B38400: strcpy(cspeed,"38400");break;
++      case B115200:
+                 {
+                   if (high_speed == 0) strcpy(cspeed,"115200");
+                   else strcpy(cspeed,"57600");
+                   break;
+                 }
+-    case B460800: strcpy(cspeed, "460800");break;
++      case B460800: strcpy(cspeed, "460800");break;
++    }
++    bits=stbuf.c_cflag & CSIZE;
++    clocal=stbuf.c_cflag & CLOCAL;
++    stopbits=stbuf.c_cflag & CSTOPB;
++    parity=stbuf.c_cflag & (PARENB | PARODD);
++    stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
++    stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
++    stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
++    stbuf.c_lflag &= ~(ECHO | ECHOE);
++    stbuf.c_cc[VMIN] = 1;
++    stbuf.c_cc[VTIME] = 0;
++    stbuf.c_cc[VEOF] = 1;
+   }
+-  bits=stbuf.c_cflag & CSIZE;
+-  clocal=stbuf.c_cflag & CLOCAL;
+-  stopbits=stbuf.c_cflag & CSTOPB;
+-  parity=stbuf.c_cflag & (PARENB | PARODD);
+-  stbuf.c_iflag &= ~(IGNCR | ICRNL | IUCLC | INPCK | IXON | IXANY | IGNPAR );
+-  stbuf.c_oflag &= ~(OPOST | OLCUC | OCRNL | ONLCR | ONLRET);
+-  stbuf.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL);
+-  stbuf.c_lflag &= ~(ECHO | ECHOE);
+-  stbuf.c_cc[VMIN] = 1;
+-  stbuf.c_cc[VTIME] = 0;
+-  stbuf.c_cc[VEOF] = 1;
+   setcom();
+   dormir(200000); /* Wait a bit (DTR raise) */
+   sprintf(msg,"Opened %s as FD %d",device,comfd);