GTA04: Retry multiple times to find and open modem nodes, with delay
authorPaul Kocialkowski <contact@paulk.fr>
Thu, 30 Aug 2012 16:19:02 +0000 (18:19 +0200)
committerPaul Kocialkowski <contact@paulk.fr>
Thu, 30 Aug 2012 16:19:02 +0000 (18:19 +0200)
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
device/gta04/gta04.c

index 64a9aae..0d4c0ba 100644 (file)
@@ -146,6 +146,7 @@ int gta04_transport_find_node(char **tty_node, char *name)
        char *tty_sysfs_node = NULL;
        char *buf = NULL;
        int name_length;
+       int retries = 10;
        int fd = -1;
        int i;
 
@@ -155,25 +156,30 @@ int gta04_transport_find_node(char **tty_node, char *name)
        name_length = strlen(name);
        buf = calloc(1, name_length);
 
-       for(i=0 ; i < TTY_NODE_MAX ; i++) {
-               asprintf(&tty_sysfs_node, "%s/%s%d/%s",
-                       TTY_SYSFS_BASE, TTY_NODE_BASE, i, TTY_HSOTYPE);
+       while(retries) {
+               for(i=0 ; i < TTY_NODE_MAX ; i++) {
+                       asprintf(&tty_sysfs_node, "%s/%s%d/%s",
+                               TTY_SYSFS_BASE, TTY_NODE_BASE, i, TTY_HSOTYPE);
 
-               fd = open(tty_sysfs_node, O_RDONLY);
-               if(fd < 0) {
-                       free(tty_sysfs_node);
-                       continue;
-               }
+                       fd = open(tty_sysfs_node, O_RDONLY);
+                       if(fd < 0) {
+                               free(tty_sysfs_node);
+                               continue;
+                       }
 
-               read(fd, buf, name_length);
-               if(strncmp(name, buf, name_length) == 0) {
-                       asprintf(tty_node, "%s/%s%d", TTY_DEV_BASE, TTY_NODE_BASE, i);
+                       read(fd, buf, name_length);
+                       if(strncmp(name, buf, name_length) == 0) {
+                               asprintf(tty_node, "%s/%s%d", TTY_DEV_BASE, TTY_NODE_BASE, i);
 
-                       free(tty_sysfs_node);
-                       return 0;
-               } else {
-                       free(tty_sysfs_node);
+                               free(tty_sysfs_node);
+                               return 0;
+                       } else {
+                               free(tty_sysfs_node);
+                       }
                }
+
+               retries--;
+               usleep(750000);
        }
 
        *tty_node = NULL;
@@ -181,10 +187,43 @@ int gta04_transport_find_node(char **tty_node, char *name)
        return -1;
 }
 
+int gta04_transport_open_node(char *dev_node)
+{
+       struct termios term;
+       int retries = 10;
+       int fd = -1;
+       int rc = -1;
+
+       while(retries) {
+               fd = open(dev_node, O_RDWR | O_NOCTTY | O_NDELAY);
+               if(fd < 0)
+                       goto failure;
+
+               rc = tcgetattr(fd, &term);
+               if(rc < 0)
+                       goto failure;
+
+               term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
+               cfsetispeed(&term, B115200);
+               cfsetospeed(&term, B115200);
+
+               rc = tcsetattr(fd, TCSANOW, &term);
+               if(rc < 0)
+                       goto failure;
+
+               return fd;
+
+failure:
+               retries--;
+               usleep(750000);
+       }
+
+       return -1;
+}
+
 int gta04_transport_open(void *sdata)
 {
        struct gta04_transport_data *transport_data = NULL;
-       struct termios term;
        char *dev_node = NULL;
        int fd = -1;
        int rc = -1;
@@ -197,33 +236,17 @@ int gta04_transport_open(void *sdata)
        // Open Modem node
        if(transport_data->modem_fd <= 0) {
                rc = gta04_transport_find_node(&dev_node, "Modem");
-               if(rc < 0) {
+               if(rc < 0 || dev_node == NULL) {
                        LOGE("Unable to find Modem node, aborting!");
                        goto failure;
                }
 
-               fd = open(dev_node, O_RDWR | O_NOCTTY | O_NDELAY);
+               fd = gta04_transport_open_node(dev_node);
                if(fd < 0) {
                        LOGE("Unable to open Modem node, aborting!");
                        goto failure;
                }
 
-               rc = tcgetattr(fd, &term);
-               if(rc < 0) {
-                       LOGE("Unable to get Modem note attrs, aborting!");
-                       goto failure;
-               }
-
-               term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
-               cfsetispeed(&term, B115200);
-               cfsetospeed(&term, B115200);
-
-               rc = tcsetattr(fd, TCSANOW, &term);
-               if(rc < 0) {
-                       LOGE("Unable to set Modem note attrs, aborting!");
-                       goto failure;
-               }
-
                LOGD("Opened Modem node");
                transport_data->modem_fd = fd;
        }
@@ -236,28 +259,12 @@ int gta04_transport_open(void *sdata)
                        goto failure;
                }
 
-               fd = open(dev_node, O_RDWR | O_NOCTTY | O_NDELAY);
+               fd = gta04_transport_open_node(dev_node);
                if(fd < 0) {
                        LOGE("Unable to open Application node, aborting!");
                        goto failure;
                }
 
-               rc = tcgetattr(fd, &term);
-               if(rc < 0) {
-                       LOGE("Unable to get Application note attrs, aborting!");
-                       goto failure;
-               }
-
-               term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
-               cfsetispeed(&term, B115200);
-               cfsetospeed(&term, B115200);
-
-               rc = tcsetattr(fd, TCSANOW, &term);
-               if(rc < 0) {
-                       LOGE("Unable to set Application note attrs, aborting!");
-                       goto failure;
-               }
-
                LOGD("Opened Application node");
                transport_data->application_fd = fd;
        }
@@ -282,9 +289,11 @@ int gta04_transport_close(void *sdata)
 
        if(transport_data->modem_fd > 0)
                close(transport_data->modem_fd);
+       transport_data->modem_fd = -1;
 
        if(transport_data->application_fd > 0)
                close(transport_data->application_fd);
+       transport_data->application_fd = -1;
 
        return 0;
 }