Skip to content

Commit

Permalink
Unify code by creating a common get_interface function that returns i…
Browse files Browse the repository at this point in the history
…nterface in configs or the default one.

Signed-off-by: Marko Saukko <marko.saukko@jollamobile.com>
  • Loading branch information
Marko Saukko committed Jul 4, 2013
1 parent fcd767c commit 02a5807
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/usb_moded-network.c
Expand Up @@ -37,9 +37,33 @@
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#endif

const char default_interface[] = "usb0";

#endif
char* get_interface(struct mode_list_elem *data)
{
char *interface = NULL;

if(data)
{
if(data->network_interface)
{
interface = malloc(32*sizeof(char));
strncpy(interface, data->network_interface, 32);
}
}
else
interface = get_network_interface();

if(interface == NULL)
{
interface = malloc(sizeof(default_interface)*sizeof(char));
strncpy(interface, default_interface, sizeof(default_interface));
}

return interface;
}

/**
* Activate the network interface
Expand All @@ -49,7 +73,6 @@ int usb_network_up(struct mode_list_elem *data)
{
char *ip, *interface, *gateway;
char command[128];
const char default_interface[] = "usb0";

#if CONNMAN
DBusConnection *dbus_conn_connman = NULL;
Expand Down Expand Up @@ -78,27 +101,11 @@ int usb_network_up(struct mode_list_elem *data)
return(ret);

#else

if(data)
{
if(data->network_interface)
{
interface = malloc(32*sizeof(char));
strncpy(interface, data->network_interface, 32);
}
}
else
interface = get_network_interface();

interface = get_interface(data);
ip = get_network_ip();
gateway = get_network_gateway();

if(interface == NULL)
{
interface = malloc(sizeof(default_interface)*sizeof(char));
strncpy(interface, default_interface, sizeof(default_interface));
}

if(ip == NULL)
{
sprintf(command,"ifconfig %s 192.168.2.15", interface);
Expand Down Expand Up @@ -143,21 +150,9 @@ int usb_network_down(struct mode_list_elem *data)
const char *interface;
char command[128];

if(data)
{
if(data->network_interface)
{
interface = malloc(32*sizeof(char));
strncpy((char *)interface, data->network_interface, 32);
}
}
else
interface = get_network_interface();
interface = get_interface(data);

if(interface == NULL)
sprintf(command, "ifconfig usb0 down\n");
else
sprintf(command, "ifconfig %s down\n", interface);
sprintf(command, "ifconfig %s down\n", interface);
system(command);

free((char *)interface);
Expand Down

0 comments on commit 02a5807

Please sign in to comment.