/
usb_moded-udev.c
168 lines (149 loc) · 4.06 KB
/
usb_moded-udev.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
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <poll.h>
#include <libudev.h>
#include <glib.h>
#include "usb_moded-log.h"
#include "usb_moded-config.h"
#include "usb_moded-hw-ab.h"
#include "usb_moded.h"
/* global variables */
static struct udev *udev;
static struct udev_monitor *mon;
static GIOChannel *iochannel;
static guint watch_id;
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);
gboolean hwal_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 FALSE;
}
udev_path = find_udev_path();
if(udev_path)
dev = udev_device_new_from_syspath(udev, udev_path);
else
dev = udev_device_new_from_syspath(udev, "/sys/class/power_supply/usb");
if (!dev)
{
log_err("Unable to find $power_supply device.");
/* communicate failure, mainloop will exit and call appropriate clean-up */
return FALSE;
}
else
{
dev_name = udev_device_get_sysname(dev);
udev_device_unref(dev);
}
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 FALSE;
}
ret = udev_monitor_filter_add_match_subsystem_devtype(mon, "power_supply", NULL);
if(ret != 0)
{
log_err("Udev match failed.\n");
return FALSE;
}
ret = udev_monitor_enable_receiving (mon);
if(ret != 0)
{
log_err("Failed to enable monitor recieving.\n");
return FALSE;
}
/* check if we are already connected */
udev_parse(dev);
iochannel = g_io_channel_unix_new(udev_monitor_get_fd(mon));
watch_id = g_io_add_watch(iochannel, G_IO_IN, monitor_udev, NULL);
/* everything went well */
return TRUE;
}
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 */
if(strcmp(dev_name, udev_device_get_sysname(dev)))
return TRUE;
if(!strcmp(udev_device_get_action(dev), "change"))
{
udev_parse(dev);
}
udev_device_unref(dev);
}
/* if we get something else something bad happened stop watching to avoid busylooping */
else
exit(1);
}
/* keep watching */
return TRUE;
}
void hwal_cleanup(void)
{
g_source_remove(watch_id);
watch_id = 0;
g_io_channel_unref(iochannel);
iochannel = NULL;
udev_monitor_unref(mon);
udev_unref(udev);
}
static void udev_parse(struct udev_device *dev)
{
const char *tmp;
tmp = udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE");
if(!tmp)
{
tmp = udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT");
/* log_warning("Using present property\n"); */
}
if(!tmp)
{
log_err("No usable power supply indicator\n");
exit(1);
}
if(!strcmp(tmp, "1"))
{
log_debug("UDEV:power supply present\n");
/* power supply type might not exist */
tmp = udev_device_get_property_value(dev, "POWER_SUPPLY_TYPE");
if(!tmp)
{
/* power supply type might not exist also :( Send connected event but this will not be able
to discriminate between charger/cable */
log_warning("Fallback since cable detection cannot be accurate. Will connect on any voltage on usb.\n");
set_usb_connected(TRUE);
return;
}
if(!strcmp(tmp, "USB")||!strcmp(tmp, "USB_CDP"))
{
log_debug("UDEV:USB cable connected\n");
set_usb_connected(TRUE);
}
}
else
{
log_debug("UDEV:USB cable disconnected\n");
set_usb_connected(FALSE);
}
}