/
usb_moded-trigger.c
266 lines (219 loc) · 7.6 KB
/
usb_moded-trigger.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @file usb_moded-trigger.c
*
* Copyright (C) 2011 Nokia Corporation. All rights reserved.
* Copyright (C) 2014-2018 Jolla Ltd.
*
* @author: Philippe De Swert <philippedeswert@gmail.com>
* @author: Philippe De Swert <phdeswer@lumi.maa>
* @author: Philippe De Swert <philippe.de-swert@nokia.com>
* @author: Philippe De Swert <philippe.deswert@jollamobile.com>
* @author: Simo Piiroinen <simo.piiroinen@jollamobile.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 <locale.h>
#include <unistd.h>
#include <poll.h>
#include <libudev.h>
#include <glib.h>
#include "usb_moded.h"
#include "usb_moded-log.h"
#include "usb_moded-config-private.h"
#include "usb_moded-udev.h"
#include "usb_moded-modesetting.h"
#include "usb_moded-trigger.h"
#if defined MEEGOLOCK
# include "usb_moded-devicelock.h"
#endif /* MEEGOLOCK */
/* ========================================================================= *
* Prototypes
* ========================================================================= */
/* -- trigger -- */
static void trigger_udev_error_cb (gpointer data);
static gboolean trigger_udev_input_cb (GIOChannel *iochannel, GIOCondition cond, gpointer data);
static void trigger_parse_udev_properties(struct udev_device *dev);
bool trigger_init (void);
void trigger_stop (void);
/* ========================================================================= *
* Data
* ========================================================================= */
static struct udev *trigger_udev_handle = 0;
static struct udev_monitor *trigger_udev_monitor = 0;
static guint trigger_udev_watch_id = 0;
static gchar *trigger_udev_sysname = 0;
/* ========================================================================= *
* Functions
* ========================================================================= */
static void trigger_udev_error_cb (gpointer data)
{
(void)data;
log_debug("trigger watch destroyed\n!");
/* clean up & restart trigger */
trigger_stop();
trigger_init();
}
bool trigger_init(void)
{
bool ack = false;
gchar *devpath = 0;
gchar *subsystem = 0;
struct udev_device *dev = 0;
GIOChannel *chn = 0;
int ret;
if( !(devpath = config_check_trigger()) ) {
log_err("No trigger path. Not starting trigger.\n");
goto EXIT;
}
if( !(subsystem = config_get_trigger_subsystem()) ) {
log_err("No trigger subsystem. Not starting trigger.\n");
goto EXIT;
}
/* Create the udev object */
if( !(trigger_udev_handle = udev_new()) ) {
log_err("Can't create udev\n");
}
dev = udev_device_new_from_syspath(trigger_udev_handle, devpath);
if( !dev ) {
log_err("Unable to find the trigger device.");
goto EXIT;
}
trigger_udev_sysname = g_strdup(udev_device_get_sysname(dev));
log_debug("device name = %s\n", trigger_udev_sysname);
trigger_udev_monitor = udev_monitor_new_from_netlink(trigger_udev_handle,
"udev");
if( !trigger_udev_monitor ) {
log_err("Unable to monitor the netlink\n");
goto EXIT;
}
ret = udev_monitor_filter_add_match_subsystem_devtype(trigger_udev_monitor,
subsystem,
NULL);
if (ret != 0 ) {
log_err("Udev match failed.\n");
goto EXIT;
}
ret = udev_monitor_enable_receiving(trigger_udev_monitor);
if( ret != 0 ) {
log_err("Failed to enable monitor recieving.\n");
goto EXIT;
}
/* check if we are already connected */
trigger_parse_udev_properties(dev);
chn = g_io_channel_unix_new(udev_monitor_get_fd(trigger_udev_monitor));
if( !chn )
goto EXIT;
trigger_udev_watch_id = g_io_add_watch_full(chn,
0,
G_IO_IN,
trigger_udev_input_cb,
NULL,
trigger_udev_error_cb);
/* everything went well */
log_debug("Trigger enabled!\n");
ack = true;
EXIT:
if(chn)
g_io_channel_unref(chn);
if( dev )
udev_device_unref(dev);
g_free(subsystem);
g_free(devpath);
/* All or nothing */
if( !ack )
trigger_stop();
return ack;
}
static gboolean trigger_udev_input_cb(GIOChannel *iochannel G_GNUC_UNUSED, GIOCondition cond,
gpointer data G_GNUC_UNUSED)
{
struct udev_device *dev;
if(cond & G_IO_IN)
{
/* This normally blocks but G_IO_IN indicates that we can read */
dev = udev_monitor_receive_device (trigger_udev_monitor);
if (dev)
{
/* check if it is the actual device we want to check */
if(strcmp(trigger_udev_sysname, udev_device_get_sysname(dev))) {
log_crit("name does not match, disabling udev trigger io-watch");
trigger_udev_watch_id = 0;
return FALSE;
}
if(!strcmp(udev_device_get_action(dev), "change"))
{
log_debug("Trigger event recieved.\n");
trigger_parse_udev_properties(dev);
}
udev_device_unref(dev);
}
/* if we get something else something bad happened stop watching to avoid busylooping */
else
{
log_debug("Bad trigger data. Stopping\n");
trigger_udev_watch_id = 0;
trigger_stop();
return FALSE;
}
}
/* keep watching */
return TRUE;
}
void trigger_stop(void)
{
if(trigger_udev_watch_id)
{
g_source_remove(trigger_udev_watch_id);
trigger_udev_watch_id = 0;
}
if(trigger_udev_monitor)
{
udev_monitor_unref(trigger_udev_monitor);
trigger_udev_monitor = 0;
}
if(trigger_udev_handle)
{
udev_unref(trigger_udev_handle);
trigger_udev_handle = 0;
}
g_free(trigger_udev_sysname),
trigger_udev_sysname = 0;
}
static void trigger_parse_udev_properties(struct udev_device *dev)
{
char *trigger_property = 0;
char *trigger_value = 0;
char *trigger_mode = 0;
const char *value = 0;
if( !usbmoded_can_export() )
goto EXIT;
if( !(trigger_mode = config_get_trigger_mode()) )
goto EXIT;
if( !(trigger_property = config_get_trigger_property()) )
goto EXIT;
if( !(value = udev_device_get_property_value(dev, trigger_property)) )
goto EXIT;
if( (trigger_value = config_get_trigger_value()) ) {
if( strcmp(trigger_value, value) )
goto EXIT;
}
usbmoded_set_usb_mode(trigger_mode);
EXIT:
free(trigger_value);
free(trigger_property);
free(trigger_mode);
}