]> git.ipfire.org Git - oddments/fireinfo.git/blame - src/fireinfo/device.py
virt: Fix off-by-one error when detecting hypervisor
[oddments/fireinfo.git] / src / fireinfo / device.py
CommitLineData
3b5ed4e1
MT
1#!/usr/bin/python
2###############################################################################
3# #
4# Fireinfo #
5# Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
e8221cde 22import os.path
b45f0e98
MT
23
24class Device(object):
e8221cde
MT
25 """
26 This is an abstract class that represents all devices in the system.
27 Every single device has its own instance of this class.
28 """
29
b45f0e98 30 def __init__(self, path):
e8221cde
MT
31 """
32 Collect all information about the device by reading the
33 "uevent" file and parsing it.
34 """
35
36 # Save the path in /sys to the device.
37 self.path = path
38
39 # Datastructure to store information we read.
40 self._uevent = {}
41
42 # Open the uevent file and parse all lines.
b45f0e98
MT
43 with open(os.path.join(path, "uevent")) as f:
44 for line in f.readlines():
45 key, val = line.split("=")
e8221cde
MT
46 self._uevent[key] = val.rstrip().lower()
47
b45f0e98
MT
48 @property
49 def driver(self):
e8221cde
MT
50 """
51 Get the driver/kernel module that device is driven by or return None.
52 """
b45f0e98
MT
53 return self._uevent.get("DRIVER", None)
54
55
56class PCIDevice(Device):
e8221cde
MT
57 """
58 A class that represents all PCI (and PCIe) devices in a system.
59 """
60
b45f0e98 61 subsystem = "pci"
e8221cde 62
b45f0e98
MT
63 @property
64 def model(self):
e8221cde
MT
65 """
66 Return the PCI model id of this device.
67 """
b45f0e98
MT
68 return self._uevent['PCI_ID'].split(":")[1]
69
70 @property
71 def vendor(self):
e8221cde
MT
72 """
73 Return the PCI vendor id of this device.
74 """
b45f0e98
MT
75 return self._uevent['PCI_ID'].split(":")[0]
76
77 @property
78 def deviceclass(self):
e8221cde
MT
79 """
80 Return the PCI device class of this device.
81 """
b45f0e98 82 return self._uevent['PCI_CLASS']
97679775
MT
83
84 @property
85 def sub_vendor(self):
e8221cde
MT
86 """
87 Return the PCI vendor sub id of this device.
88 """
97679775
MT
89 return self._uevent["PCI_SUBSYS_ID"].split(":")[0]
90
91 @property
92 def sub_model(self):
e8221cde
MT
93 """
94 Return the PCI model sub id of this device.
95 """
97679775
MT
96 return self._uevent["PCI_SUBSYS_ID"].split(":")[1]
97
98
b45f0e98 99class USBDevice(Device):
e8221cde
MT
100 """
101 A class that represents all USB devices in a system.
102 """
103
b45f0e98
MT
104 subsystem = "usb"
105
106 def pad(self, s):
e8221cde
MT
107 """
108 A function to pad ids that have no leading zeroes.
109 """
b45f0e98
MT
110 while len(s) < 4:
111 s = "0"+s
112 return s
e8221cde 113
b45f0e98
MT
114 @property
115 def vendor(self):
e8221cde
MT
116 """
117 Return the USB vendor id of this device.
118 """
b45f0e98
MT
119 return self.pad(self._uevent['PRODUCT'].split("/")[0])
120
121 @property
122 def model(self):
e8221cde
MT
123 """
124 Return the USB model id of this device.
125 """
b45f0e98
MT
126 return self.pad(self._uevent['PRODUCT'].split("/")[1])
127
128 @property
129 def deviceclass(self):
e8221cde
MT
130 """
131 Return the USB device class of this device.
132 """
b45f0e98 133 return self._uevent.get("INTERFACE", None)