Skip to content

Latest commit

 

History

History
243 lines (215 loc) · 5.46 KB

usb_moded-trigger.c

File metadata and controls

243 lines (215 loc) · 5.46 KB
 
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
/**
@file usb_moded-trigger.c
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 <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.h"
#include "usb_moded-hw-ab.h"
Aug 17, 2011
Aug 17, 2011
39
#include "usb_moded-modesetting.h"
40
#include "usb_moded-trigger.h"
Mar 1, 2012
Mar 1, 2012
41
42
43
#if defined MEEGOLOCK
#include "usb_moded-lock.h"
#endif /* MEEGOLOCK */
44
45
/* global variables */
Jul 7, 2016
Jul 7, 2016
46
47
48
49
static struct udev *udev = 0;
static struct udev_monitor *mon = 0;
static GIOChannel *iochannel = 0;
static guint watch_id = 0;
50
51
52
53
54
55
56
static const char *dev_name;
/* static function definitions */
static gboolean monitor_udev(GIOChannel *iochannel G_GNUC_UNUSED, GIOCondition cond,
gpointer data G_GNUC_UNUSED);
static void udev_parse(struct udev_device *dev);
Aug 15, 2011
Aug 15, 2011
57
58
59
static void notify_issue (gpointer data)
{
log_debug("trigger watch destroyed\n!");
Aug 17, 2011
Aug 17, 2011
60
61
/* clean up & restart trigger */
trigger_stop();
Aug 15, 2011
Aug 15, 2011
62
63
64
65
trigger_init();
}
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
gboolean trigger_init(void)
{
const gchar *udev_path = NULL;
struct udev_device *dev;
int ret = 0;
/* Create the udev object */
udev = udev_new();
if (!udev)
{
log_err("Can't create udev\n");
return 1;
}
udev_path = check_trigger();
if(udev_path)
dev = udev_device_new_from_syspath(udev, udev_path);
else
{
log_err("No trigger path. Not starting trigger.\n");
return 1;
}
if (!dev)
{
log_err("Unable to find the trigger device.");
return 1;
}
else
{
dev_name = udev_device_get_sysname(dev);
log_debug("device name = %s\n", dev_name);
}
mon = udev_monitor_new_from_netlink (udev, "udev");
if (!mon)
{
log_err("Unable to monitor the netlink\n");
/* communicate failure, mainloop will exit and call appropriate clean-up */
return 1;
}
ret = udev_monitor_filter_add_match_subsystem_devtype(mon, get_trigger_subsystem(), NULL);
if(ret != 0)
{
log_err("Udev match failed.\n");
return 1;
}
ret = udev_monitor_enable_receiving (mon);
if(ret != 0)
{
log_err("Failed to enable monitor recieving.\n");
return 1;
}
/* check if we are already connected */
udev_parse(dev);
iochannel = g_io_channel_unix_new(udev_monitor_get_fd(mon));
Aug 15, 2011
Aug 15, 2011
122
watch_id = g_io_add_watch_full(iochannel, 0, G_IO_IN, monitor_udev, NULL, notify_issue);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* everything went well */
log_debug("Trigger enabled!\n");
return 0;
}
static gboolean monitor_udev(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 (mon);
if (dev)
{
/* check if it is the actual device we want to check */
Jul 7, 2016
Jul 7, 2016
141
142
143
144
145
if(strcmp(dev_name, udev_device_get_sysname(dev))) {
log_crit("name does not match, disabling udev trigger io-watch");
watch_id = 0;
return FALSE;
}
146
147
148
149
150
151
152
153
154
155
156
157
if(!strcmp(udev_device_get_action(dev), "change"))
{
log_debug("Trigger event recieved.\n");
udev_parse(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");
Jul 7, 2016
Jul 7, 2016
158
watch_id = 0;
159
trigger_stop();
Jul 7, 2016
Jul 7, 2016
160
return FALSE;
161
162
163
164
165
166
167
168
169
}
}
/* keep watching */
return TRUE;
}
void trigger_stop(void)
{
Jul 7, 2016
Jul 7, 2016
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
if(watch_id)
{
g_source_remove(watch_id);
watch_id = 0;
}
if(iochannel) {
g_io_channel_unref(iochannel);
iochannel = NULL;
}
if(mon)
{
udev_monitor_unref(mon);
mon = 0;
}
if(udev)
{
udev_unref(udev);
udev = 0;
}
dev_name = 0;
190
191
192
193
}
static void udev_parse(struct udev_device *dev)
{
Nov 11, 2014
Nov 11, 2014
194
195
const char *tmp = 0;
char *trigger = 0;
Mar 1, 2012
Mar 1, 2012
196
197
198
trigger = get_trigger_property();
tmp = udev_device_get_property_value(dev, trigger);
199
200
201
if(!tmp)
{
/* do nothing and return */
Nov 11, 2014
Nov 11, 2014
202
free(trigger);
203
204
205
206
return;
}
else
{
Nov 11, 2014
Nov 11, 2014
207
free(trigger);
Mar 1, 2012
Mar 1, 2012
208
209
trigger = get_trigger_value();
if(trigger)
Mar 1, 2012
Mar 1, 2012
211
if(!strcmp(tmp, trigger))
May 17, 2011
May 17, 2011
212
{
Mar 1, 2012
Mar 1, 2012
213
#if defined MEEGOLOCK
Feb 14, 2017
Feb 14, 2017
214
if(usb_moded_get_export_permission())
Mar 1, 2012
Mar 1, 2012
215
#endif /* MEEGOLOCK */
Jun 27, 2011
Jun 27, 2011
216
if(strcmp(get_trigger_mode(), get_usb_mode()) != 0)
Aug 17, 2011
Aug 17, 2011
217
218
{
usb_moded_mode_cleanup(get_usb_module());
Jun 27, 2011
Jun 27, 2011
219
set_usb_mode(get_trigger_mode());
Aug 17, 2011
Aug 17, 2011
220
}
Nov 11, 2014
Nov 11, 2014
221
free(trigger);
May 17, 2011
May 17, 2011
222
}
223
else
Mar 1, 2012
Mar 1, 2012
224
{
Nov 11, 2014
Nov 11, 2014
225
free(trigger);
226
return;
Mar 1, 2012
Mar 1, 2012
227
}
Aug 18, 2011
Aug 18, 2011
229
230
else
/* for triggers without trigger value */
May 17, 2011
May 17, 2011
231
{
Mar 1, 2012
Mar 1, 2012
232
#if defined MEEGOLOCK
Feb 14, 2017
Feb 14, 2017
233
if(usb_moded_get_export_permission())
Mar 1, 2012
Mar 1, 2012
234
#endif /* MEEGOLOCK */
Jun 27, 2011
Jun 27, 2011
235
if(strcmp(get_trigger_mode(), get_usb_mode()) != 0)
Aug 18, 2011
Aug 18, 2011
236
237
{
usb_moded_mode_cleanup(get_usb_module());
Jun 27, 2011
Jun 27, 2011
238
set_usb_mode(get_trigger_mode());
Aug 18, 2011
Aug 18, 2011
239
}
May 17, 2011
May 17, 2011
240
}
241
242
243
return;
}
}