/
wlanloader.c
223 lines (176 loc) · 6.49 KB
/
wlanloader.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
@file wlanloader.c
@brief Plugin for resetting kernel WLAN module after wifi hotspot usage
Wlan loader plugin in it's current form simply listens to connman's
property "Tethering" changes, and resets the wlan kernel module by restarting
a systemd service called "wlan-module-load.service" each time "Tethering"
is set to false.
At start up, the plugin detects if "wlan-module-load.service" is present in
systemd and only becomes active if it is found. If your hardware platform
requires WLAN to be reset after wifi hotspot use, please provide a
systemd system service named "wlan-module-load.service", that at ExecStart
modprobes the wlan kernel module, and at ExecStop rmmods the module.
<p>
Copyright (C) 2014-2017 Jolla Ltd.
@author Kalle Jokiniemi <kalle.jokiniemi@jolla.com>
@author Slava Monich <slava.monich@jolla.com>
@author Simo Piiroinen <simo.piiroinen@jollamobile.com>
This file is part of Dsme.
Dsme is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Dsme 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Dsme. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <string.h>
#include "dbusproxy.h"
#include "dsme_dbus.h"
#include "../include/dsme/modules.h"
#include "../include/dsme/logging.h"
#include "../include/dsme/modulebase.h"
#define WLAN_SYSTEMD_UNIT "wlan-module-load.service"
static module_t *this_module = 0;
static void reset_wlan_module(void)
{
DBusError err = DBUS_ERROR_INIT;
DBusConnection *conn = 0;
DBusMessage *req = NULL;
const char *unit = WLAN_SYSTEMD_UNIT;
const char *mode = "ignore-requirements";
dsme_log(LOG_DEBUG, "wlanloader: Resetting WLAN");
if (!(conn = dsme_dbus_get_connection(&err)))
{
dsme_log(LOG_ERR, "wlanloader: system bus connect: %s: %s",
err.name, err.message);
goto cleanup;
}
req = dbus_message_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"RestartUnit");
if (!req)
goto cleanup;
if (!dbus_message_append_args(req,
DBUS_TYPE_STRING, &unit,
DBUS_TYPE_STRING, &mode,
DBUS_TYPE_INVALID)) {
goto cleanup;
}
if (!dbus_connection_send(conn, req, NULL))
goto cleanup;
cleanup:
if (req) dbus_message_unref(req);
if (conn) dbus_connection_unref(conn);
dbus_error_free(&err);
}
static void connman_tethering_changed(const DsmeDbusMessage* sig)
{
bool tethering;
if (strcmp(dsme_dbus_message_get_string(sig), "Tethering") == 0 &&
strcmp(dsme_dbus_message_path(sig),
"/net/connman/technology/wifi") == 0)
{
tethering = dsme_dbus_message_get_variant_bool(sig);
dsme_log(LOG_DEBUG, "wlanloader: Tethering status changed to %d",
tethering);
if (!tethering) {
reset_wlan_module();
}
}
}
static const dsme_dbus_signal_binding_t dbus_signals_array[] = {
{ connman_tethering_changed , "net.connman.Technology", "PropertyChanged" },
{ 0, 0 }
};
static bool dbus_signals_bound = false;
static void loader_needed_cb(DBusPendingCall *pending, void *user_data)
{
(void)user_data;
DBusMessage *rsp = 0;
DBusError err = DBUS_ERROR_INIT;
if (!(rsp = dbus_pending_call_steal_reply(pending)))
goto cleanup;
if (dbus_set_error_from_message(&err, rsp)) {
dsme_log(LOG_DEBUG, "wlanloader: disabled, GetUnit: %s: %s",
err.name, err.message);
} else {
/* We got the reply without error, so the service exists */
const module_t *restore = modulebase_enter_module(this_module);
dsme_dbus_bind_signals(&dbus_signals_bound, dbus_signals_array);
modulebase_enter_module(restore);
dsme_log(LOG_DEBUG, "wlanloader: activated");
}
cleanup:
if (rsp) dbus_message_unref(rsp);
dbus_error_free(&err);
}
/* Helper function to check if this module is needed */
static void check_loader_needed(void)
{
DBusError err = DBUS_ERROR_INIT;
DBusPendingCall *pc = 0;
DBusConnection *conn = 0;
DBusMessage *req = NULL;
const char *unit = WLAN_SYSTEMD_UNIT;
if (!(conn = dsme_dbus_get_connection(&err)))
{
dsme_log(LOG_ERR, "wlanloader: system bus connect: %s: %s",
err.name, err.message);
goto cleanup;
}
req = dbus_message_new_method_call("org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnit");
if (!req)
goto cleanup;
if (!dbus_message_append_args(req,
DBUS_TYPE_STRING, &unit,
DBUS_TYPE_INVALID)) {
goto cleanup;
}
if (!dbus_connection_send_with_reply(conn, req, &pc, -1))
goto cleanup;
if (!pc) {
dsme_log(LOG_WARNING, "wlanloader: null pending call received");
goto cleanup;
}
if (!dbus_pending_call_set_notify(pc, loader_needed_cb, 0, 0))
goto cleanup;
cleanup:
if (pc) dbus_pending_call_unref(pc);
if (req) dbus_message_unref(req);
if (conn) dbus_connection_unref(conn);
dbus_error_free(&err);
}
DSME_HANDLER(DSM_MSGTYPE_DBUS_CONNECTED, client, msg)
{
dsme_log(LOG_DEBUG, "wlanloader: DBUS_CONNECTED");
check_loader_needed();
}
DSME_HANDLER(DSM_MSGTYPE_DBUS_DISCONNECT, client, msg)
{
dsme_log(LOG_DEBUG, "wlanloader: DBUS_DISCONNECT");
}
module_fn_info_t message_handlers[] = {
DSME_HANDLER_BINDING(DSM_MSGTYPE_DBUS_CONNECTED),
DSME_HANDLER_BINDING(DSM_MSGTYPE_DBUS_DISCONNECT),
{ 0 }
};
void
module_init(module_t * handle)
{
this_module = handle;
dsme_log(LOG_DEBUG, "wlanloader.so loaded");
}
void
module_fini(void)
{
dsme_dbus_unbind_signals(&dbus_signals_bound, dbus_signals_array);
dsme_log(LOG_DEBUG, "libwlanloader.so unloaded");
}