Added support of Multiple same SSIDs including band steering.
[platform/upstream/connman.git] / plugins / tist.c
old mode 100644 (file)
new mode 100755 (executable)
index 28498b6..ad5ef79
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2011  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2012  Intel Corporation. All rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2 as
@@ -23,7 +23,9 @@
 #include <config.h>
 #endif
 
+#define _GNU_SOURCE
 #include <stdio.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -55,7 +57,7 @@ static unsigned long baud_rate = 0;
 static guint install_watch = 0;
 static guint uart_watch = 0;
 
-static gint install_count = 0;
+static int install_count = 0;
 
 #define NCCS2 19
 struct termios2 {
@@ -104,7 +106,7 @@ static int read_baud_rate(unsigned long *baud)
        DBG("");
 
        f = fopen(TIST_SYSFS_BAUD, "r");
-       if (f == NULL)
+       if (!f)
                return -EIO;
 
        err = fscanf(f, "%lu", baud);
@@ -115,17 +117,17 @@ static int read_baud_rate(unsigned long *baud)
        return err;
 }
 
-static int read_uart_name(char uart_name[])
+static int read_uart_name(char uart_name[], size_t uart_name_len)
 {
        int err;
        FILE *f;
 
        DBG("");
 
-       memset(uart_name, 0, sizeof(uart_name));
+       memset(uart_name, 0, uart_name_len);
 
        f = fopen(TIST_SYSFS_UART, "r");
-       if (f == NULL)
+       if (!f)
                return -EIO;
 
         err = fscanf(f, "%s", uart_name);
@@ -326,19 +328,22 @@ static gboolean uart_event(GIOChannel *channel,
        if (ioctl(uart_fd, TIOCSETD, &ldisc) < 0)
                goto err;
 
-       g_atomic_int_set(&install_count, 0);
+       install_count = 1;
+       __sync_synchronize();
 
        return FALSE;
 
 err:
-       g_atomic_int_set(&install_count, 0);
+       install_count = 0;
+       __sync_synchronize();
+
        g_io_channel_shutdown(channel, TRUE, NULL);
        g_io_channel_unref(channel);
 
        return FALSE;
 }
 
-static int install_ldisc(GIOChannel *channel, gboolean install)
+static int install_ldisc(GIOChannel *channel, bool install)
 {
        int uart_fd, err;
        struct speed_change_cmd cmd;
@@ -346,10 +351,11 @@ static int install_ldisc(GIOChannel *channel, gboolean install)
 
        DBG("%d %p", install, uart_channel);
 
-       if (install == FALSE) {
-               g_atomic_int_set(&install_count, 0);
+       if (!install) {
+               install_count = 0;
+               __sync_synchronize();
 
-               if (uart_channel == NULL) {
+               if (!uart_channel) {
                        DBG("UART channel is NULL");
                        return 0;
                }
@@ -362,19 +368,19 @@ static int install_ldisc(GIOChannel *channel, gboolean install)
                return 0;
        }
 
-       if (uart_channel != NULL) {
+       if (uart_channel) {
                g_io_channel_shutdown(uart_channel, TRUE, NULL);
                g_io_channel_unref(uart_channel);
                uart_channel = NULL;
        }
 
        DBG("opening %s custom baud %lu", uart_dev_name, baud_rate);
-       
-       uart_fd = open(uart_dev_name, O_RDWR);
+
+       uart_fd = open(uart_dev_name, O_RDWR | O_CLOEXEC);
        if (uart_fd < 0)
                return -EIO;
 
-       uart_channel = g_io_channel_unix_new(uart_fd);  
+       uart_channel = g_io_channel_unix_new(uart_fd);
        g_io_channel_set_close_on_unref(uart_channel, TRUE);
 
        g_io_channel_set_encoding(uart_channel, NULL, NULL);
@@ -403,7 +409,8 @@ static int install_ldisc(GIOChannel *channel, gboolean install)
                        uart_channel = NULL;
                }
 
-               g_atomic_int_set(&install_count, 0);
+               install_count = 0;
+               __sync_synchronize();
 
                return 0;
        }
@@ -435,7 +442,7 @@ static gboolean install_event(GIOChannel *channel,
 {
        GIOStatus status = G_IO_STATUS_NORMAL;
        unsigned int install_state;
-       gboolean install;
+       bool install;
        char buf[8];
        gsize len;
 
@@ -446,7 +453,8 @@ static gboolean install_event(GIOChannel *channel,
                return FALSE;
        }
 
-       if (g_atomic_int_get(&install_count) != 0) {
+       __sync_synchronize();
+       if (install_count != 0) {
                status = g_io_channel_seek_position(channel, 0, G_SEEK_SET, NULL);
                if (status != G_IO_STATUS_NORMAL) {
                        g_io_channel_shutdown(channel, TRUE, NULL);
@@ -455,7 +463,8 @@ static gboolean install_event(GIOChannel *channel,
                }
 
                /* Read the install value */
-               status = g_io_channel_read_chars(channel, (gchar *) buf, 8, &len, NULL);
+               status = g_io_channel_read_chars(channel, (gchar *) buf,
+                                               8, &len, NULL);
                if (status != G_IO_STATUS_NORMAL) {
                        g_io_channel_shutdown(channel, TRUE, NULL);
                        g_io_channel_unref(channel);
@@ -467,7 +476,8 @@ static gboolean install_event(GIOChannel *channel,
 
                return TRUE;
        } else {
-               g_atomic_int_set(&install_count, 1);
+               install_count = 1;
+               __sync_synchronize();
        }
 
        status = g_io_channel_seek_position(channel, 0, G_SEEK_SET, NULL);
@@ -493,7 +503,8 @@ static gboolean install_event(GIOChannel *channel,
 
        if (install_ldisc(channel, install) < 0) {
                connman_error("ldisc installation failed");
-               g_atomic_int_set(&install_count, 0);
+               install_count = 0;
+               __sync_synchronize();
                return TRUE;
        }
 
@@ -510,7 +521,7 @@ static int tist_init(void)
        int fd, err;
        gsize len;
 
-       err = read_uart_name(uart_dev_name);
+       err = read_uart_name(uart_dev_name, sizeof(uart_dev_name));
        if (err < 0) {
                connman_error("Could not read the UART name");
                return err;
@@ -522,7 +533,7 @@ static int tist_init(void)
                return err;
        }
 
-       fd = open(TIST_SYSFS_INSTALL, O_RDONLY);
+       fd = open(TIST_SYSFS_INSTALL, O_RDONLY | O_CLOEXEC);
        if (fd < 0) {
                connman_error("Failed to open TI ST sysfs install file");
                return -EIO;
@@ -563,8 +574,10 @@ static int tist_init(void)
                                            install_event, NULL, NULL);
 
        if (install_state) {
-               g_atomic_int_set(&install_count, 1);
-               err = install_ldisc(install_channel, TRUE);
+               install_count = 1;
+               __sync_synchronize();
+
+               err = install_ldisc(install_channel, true);
                if (err < 0) {
                        connman_error("ldisc installtion failed");
                        return err;
@@ -586,7 +599,7 @@ static void tist_exit(void)
        g_io_channel_shutdown(install_channel, TRUE, NULL);
        g_io_channel_unref(install_channel);
 
-       if (uart_channel != NULL) {
+       if (uart_channel) {
                g_io_channel_shutdown(uart_channel, TRUE, NULL);
                g_io_channel_unref(uart_channel);
                uart_channel = NULL;