/
usb_moded-network.c
109 lines (89 loc) · 2.67 KB
/
usb_moded-network.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
@file usb-moded_network.c : (De)activates network depending on the network setting system.
Copyright (C) 2011 Nokia Corporation. All rights reserved.
Author: Philippe De Swert <philippe.de-swert@nokia.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the Lesser GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*============================================================================= */
#include <stdio.h>
#include <stdlib.h>
#include "usb_moded-network.h"
#include "usb_moded-config.h"
#if CONNMAN
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#endif
/**
* Activate the network interface
*
*/
int usb_network_up(void)
{
const char *ip, *interface, *gateway;
char command[128];
#if CONNMAN
DBusConnection *dbus_conn_connman = NULL;
DBusMessage *msg = NULL, *reply = NULL;
DBusError error;
dbus_error_init(&error);
if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
{
log_err("Could not connect to dbus for connman\n");
}
if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Service", connect)) != NULL)
{
if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
{
dbus_message_get_args(reply, NULL, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
dbus_message_unref(reply);
}
dbus_message_unref(msg);
}
dbus_connection_unref(dbus_conn_connman);
log_debug("connman state = %d\n", ret);
return(ret);
#else
ip = get_network_ip();
interface = get_network_interface();
gateway = get_network_gateway();
if(ip == NULL)
{
system("ifconfig usb0 192.168.2.15");
return(0);
}
if(interface == NULL)
sprintf(command, "ifconfig usb0 %s\n", ip);
else
sprintf(command, "ifconfig %s %s\n", interface, ip);
system(command);
if(gateway)
{
sprintf(command, "route add default gw %s\n", gateway);
system(command);
}
return(0);
#endif /* CONNMAN */
}
/**
* Deactivate the network interface
*
*/
int usb_network_down(void)
{
#if CONNMAN
#else
system("ifconfig usb0 down");
return(0);
#endif /* CONNMAN */
}