Skip to content

Latest commit

 

History

History
141 lines (123 loc) · 3.92 KB

udev-search.c

File metadata and controls

141 lines (123 loc) · 3.92 KB
 
Aug 24, 2018
Aug 24, 2018
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
* @file udev-search.c
*
* This is a basic utility to check for potential
* viable paths to use for usb_moded.
*
* This is in case usb_moded can not figure it out for itself.
*
* compile with gcc -o udev-search udev-search.c -ludev
*
* Copyright (C) 2014-2018 Jolla. All rights reserved.
*
* @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
*/
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
Dec 17, 2014
Dec 17, 2014
35
#include <string.h>
36
37
38
39
40
#include <poll.h>
#include <libudev.h>
Aug 24, 2018
Aug 24, 2018
41
42
43
44
45
46
47
48
49
50
51
52
/* ========================================================================= *
* Protorypes
* ========================================================================= */
/* -- check -- */
static int check_device_is_usb_power_supply(const char *syspath);
/* ========================================================================= *
* Functions
* ========================================================================= */
Dec 17, 2014
Dec 17, 2014
53
54
static int check_device_is_usb_power_supply(const char *syspath)
{
Aug 24, 2018
Aug 24, 2018
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
struct udev *udev;
struct udev_device *dev = 0;
const char *udev_name;
int score = 0;
udev = udev_new();
dev = udev_device_new_from_syspath(udev, syspath);
if(!dev)
return 0;
udev_name = udev_device_get_sysname(dev);
printf("device name = %s\n", udev_name);
/* check it is no battery */
if(strstr(udev_name, "battery") ||
strstr(udev_name, "BAT"))
return 0;
if(strstr(udev_name, "usb"))
score = score + 10;
if(strstr(udev_name, "charger"))
score = score + 5;
if(udev_device_get_property_value(dev, "POWER_SUPPLY_PRESENT"))
{
score = score + 5;
printf("present property found\n");
}
if(udev_device_get_property_value(dev, "POWER_SUPPLY_ONLINE"))
{
score = score + 10;
printf("online property found\n");
}
if(udev_device_get_property_value(dev, "POWER_SUPPLY_TYPE"))
{
score = score + 10;
printf("type property found\n");
}
return(score);
Dec 17, 2014
Dec 17, 2014
91
}
Aug 24, 2018
Aug 24, 2018
93
int main (int argc, char **argv)
Aug 24, 2018
Aug 24, 2018
95
96
97
98
99
100
101
102
103
104
105
(void)argc;
(void)argv;
struct udev *udev;
struct udev_enumerate *list;
struct udev_list_entry *list_entry, *first_entry;
const char *udev_name;
int ret = 0, score = 0;
typedef struct power_device {
const char *syspath;
Dec 17, 2014
Dec 17, 2014
106
int score;
Aug 24, 2018
Aug 24, 2018
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
} power_device;
struct power_device power_dev;
power_dev.score = 0;
udev = udev_new();
list = udev_enumerate_new(udev);
ret = udev_enumerate_add_match_subsystem(list, "power_supply");
ret = udev_enumerate_scan_devices(list);
if(ret < 0)
{
printf("List empty\n");
exit(1);
}
first_entry = udev_enumerate_get_list_entry(list);
udev_list_entry_foreach(list_entry, first_entry)
Dec 17, 2014
Dec 17, 2014
124
{
Aug 24, 2018
Aug 24, 2018
125
126
127
128
129
130
131
132
133
134
udev_name = udev_list_entry_get_name(list_entry);
score = check_device_is_usb_power_supply(udev_name);
printf("power_supply device name = %s score = %d\n", udev_name, score);
if(score)
{
if(score > power_dev.score)
{
power_dev.score = score;
power_dev.syspath = udev_name;
}
Dec 17, 2014
Dec 17, 2014
135
136
137
}
}
Aug 24, 2018
Aug 24, 2018
138
printf("most likely power supply device = %s\n", power_dev.syspath);
Dec 17, 2014
Dec 17, 2014
139
Aug 24, 2018
Aug 24, 2018
140
exit(0);