-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhardware.py
383 lines (311 loc) · 12 KB
/
hardware.py
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import re
import os
import dbus
import psutil
from typing import Union
from urllib.parse import urlparse
from hm_pyhelper.logger import get_logger
from hm_pyhelper.miner_param import get_public_keys_rust
from hm_pyhelper.hardware_definitions import variant_definitions, get_variant_attribute
from hw_diag.utilities.shell import config_search_param
from retry import retry
logging = get_logger(__name__)
CPUINFO_SERIAL_KEY = "serial"
DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
DBUS_OBJECTMANAGER = 'org.freedesktop.DBus.ObjectManager'
# BlueZ
DBUS_BLUEZ_SERVICE_NAME = 'org.bluez'
DBUS_ADAPTER_IFACE = 'org.bluez.Adapter1'
# NetworkManager
DBUS_NM_SERVICE_NAME = 'org.freedesktop.NetworkManager'
DBUS_NM_OBJECT_PATH = '/org/freedesktop/NetworkManager'
DBUS_NM_IFACE = 'org.freedesktop.NetworkManager'
DBUS_NM_DEVICE_IFACE = 'org.freedesktop.NetworkManager.Device'
NM_DEVICE_TYPES = {1: "Ethernet",
2: "Wi-Fi",
5: "Bluetooth",
6: "OLPC",
7: "WiMAX",
8: "Modem",
9: "InfiniBand",
10: "Bond",
11: "VLAN",
12: "ADSL"}
NM_DEVICE_STATES = {0: "Unknown",
10: "Unmanaged",
20: "Unavailable",
30: "Disconnected",
40: "Prepare",
50: "Config",
60: "Need Auth",
70: "IP Config",
80: "IP Check",
90: "Secondaries",
100: "Activated",
110: "Deactivating",
120: "Failed"}
# ModemManager
DBUS_MM1_SERVICE = 'org.freedesktop.ModemManager1'
DBUS_MM1_PATH = '/org/freedesktop/ModemManager1'
DBUS_MM1_IF = 'org.freedesktop.ModemManager1'
DBUS_MM1_IF_MODEM = 'org.freedesktop.ModemManager1.Modem'
DBUS_MM1_IF_MODEM_SIMPLE = 'org.freedesktop.ModemManager1.Modem.Simple'
DBUS_MM1_IF_MODEM_3GPP = 'org.freedesktop.ModemManager1.Modem.Modem3gpp'
DBUS_MM1_IF_MODEM_CDMA = 'org.freedesktop.ModemManager1.Modem.ModemCdma'
MM_MODEM_CAPABILITY = {
'NONE': 0,
'POTS': 1 << 0,
'CDMA_EVDO': 1 << 1,
'GSM_UMTS': 1 << 2,
'LTE': 1 << 3,
'ADVANCED': 1 << 4,
'IRIDIUM': 1 << 5,
'ANY': 0xFFFFFFFF}
def should_display_lte(diagnostics):
variant = diagnostics.get('VA')
variant_data = variant_definitions.get(variant)
if not variant_data:
return False
return variant_data.get('CELLULAR')
def get_ble_devices():
logging.info("Retrieving list of BLE device(s)")
ble_devices = []
try:
bus = dbus.SystemBus()
proxy_object = bus.get_object(DBUS_BLUEZ_SERVICE_NAME, "/")
dbus_obj_mgr = dbus.Interface(proxy_object, DBUS_OBJECTMANAGER)
dbus_objs = dbus_obj_mgr.GetManagedObjects()
for path, interfaces in dbus_objs.items():
adapter = interfaces.get(DBUS_ADAPTER_IFACE)
if adapter:
ble_devices.append({
"Address": str(adapter.get("Address")),
"Name": str(adapter.get("Name")),
"Powered": str(adapter.get("Powered")),
"Discoverable": str(adapter.get("Discoverable")),
"Pairable": str(adapter.get("Pairable")),
"Discovering": str(adapter.get("Discovering")),
})
logging.info(f"Found the following BLE Devices: {ble_devices}")
except dbus.exceptions.DBusException as e:
logging.error(e.get_dbus_message())
except Exception as e:
logging.error(f"Error while retrieving list of BLE devices: {e}")
return ble_devices
def get_wifi_devices():
logging.info("Retrieving list of WiFi device(s)")
wifi_devices = []
try:
bus = dbus.SystemBus()
proxy = bus.get_object(DBUS_NM_SERVICE_NAME, DBUS_NM_OBJECT_PATH)
manager = dbus.Interface(proxy, DBUS_NM_IFACE)
devices = manager.GetDevices()
for device in devices:
device_proxy = bus.get_object(DBUS_NM_SERVICE_NAME, device)
props_iface = dbus.Interface(device_proxy, DBUS_PROPERTIES)
props = props_iface.GetAll(DBUS_NM_DEVICE_IFACE)
device_type = NM_DEVICE_TYPES.get(props.get("DeviceType"),
"Unknown")
device_state = NM_DEVICE_STATES.get(props.get("State"), "Unknown")
if device_type == "Wi-Fi":
wifi_devices.append({
"Interface": str(props.get("Interface")),
"Type": device_type,
"Driver": str(props.get("Driver")),
"State": device_state,
})
logging.info(f"Found the following WiFi Devices: {wifi_devices}")
except dbus.exceptions.DBusException as e:
logging.error(e.get_dbus_message())
except Exception as e:
logging.error(f"Error while retrieving list of WiFi devices: {e}")
return wifi_devices
def get_lte_devices():
logging.info("Retrieving list of LTE device(s)")
lte_devices = []
try:
bus = dbus.SystemBus()
proxy = bus.get_object(DBUS_MM1_SERVICE, DBUS_MM1_PATH)
manager = dbus.Interface(proxy, DBUS_OBJECTMANAGER)
modems = manager.GetManagedObjects()
for modem in modems:
modem_proxy = bus.get_object(DBUS_MM1_SERVICE, modem)
props_iface = dbus.Interface(modem_proxy, DBUS_PROPERTIES)
props = props_iface.GetAll(DBUS_MM1_IF_MODEM)
model = props.get("Model")
manufacturer = props.get("Manufacturer")
capabilities = props.get("CurrentCapabilities")
equipment_id = props.get("EquipmentIdentifier")
if capabilities & MM_MODEM_CAPABILITY['LTE'] or \
capabilities & MM_MODEM_CAPABILITY['LTE_ADVANCED']:
lte_devices.append({
"Model": str(model),
"Manufacturer": str(manufacturer),
"EquipmentIdentifier": str(equipment_id),
})
logging.info(f"Found the following LTE Devices: {lte_devices}")
except dbus.exceptions.DBusException as e:
logging.error(e.get_dbus_message())
except Exception as e:
logging.error(f"Error while retrieving list of LTE devices: {e}")
return lte_devices
def set_diagnostics_bt_lte(diagnostics):
diagnostics['BT'] = any(get_ble_devices())
diagnostics['LTE'] = any(get_lte_devices())
return diagnostics
def parse_i2c_bus(address):
"""
Takes i2c bus as input parameter, extracts the bus number and returns it.
"""
i2c_bus_pattern = r'i2c-(\d+)'
return re.search(i2c_bus_pattern, address).group(1)
def parse_i2c_address(port):
"""
Takes i2c address in decimal as input parameter, extracts the hex version and returns it.
"""
return f'{port:x}'
def detect_ecc(diagnostics):
# The order of the values in the lists is important!
# It determines which value will be available for which key
variant = diagnostics.get('VA')
i2c_bus = ''
try:
# Example SWARM_KEY_URI: "ecc://i2c-1:96?slot=0"
if os.getenv('SWARM_KEY_URI_OVERRIDE'):
swarm_key_uri = os.getenv('SWARM_KEY_URI_OVERRIDE')
else:
swarm_key_uri = get_variant_attribute(variant, 'SWARM_KEY_URI')
parse_result = urlparse(swarm_key_uri)
i2c_bus = parse_i2c_bus(parse_result.hostname)
i2c_address = parse_i2c_address(parse_result.port)
except Exception as e:
logging.warn("Unable to lookup SWARM_KEY_URI from hardware definitions, "
"Exception message: {}".format(e))
if not i2c_bus or not i2c_bus.isnumeric():
logging.warn("Unable to lookup storage bus from hardware definitions, "
"falling back to the default.")
i2c_bus = '1'
i2c_address = '60'
commands = [
f'i2cdetect -y {i2c_bus}'
]
parameters = [f'{i2c_address} --']
keys = ["ECC"]
for (command, param, key) in zip(commands, parameters, keys):
try:
diagnostics[key] = config_search_param(command, param)
except Exception as e:
logging.error(e)
def fetch_serial_number() -> Union[str, None]:
diag = {}
get_serial_number(diag)
return diag.get("serial_number")
def get_serial_number(diagnostics):
"""
input:
diagnostics - dict
Possible exceptions:
TypeError - if the path is not str.
FileNotFoundError - "/proc/device-tree/serial-number" not found
PermissionError - No file permissions
Writes the received value to the dictionary
"""
try:
cpuinfo = load_cpu_info()
serial_number = ""
if has_valid_serial(cpuinfo):
serial_number = cpuinfo[CPUINFO_SERIAL_KEY]
else:
serial_number = open("/proc/device-tree/serial-number").readline() \
.rstrip('\x00')
except FileNotFoundError as e:
raise e
except PermissionError as e:
raise e
diagnostics["serial_number"] = serial_number
def has_valid_serial(cpuinfo: dict) -> bool:
if CPUINFO_SERIAL_KEY not in cpuinfo:
return False
# most systems that don't use /proc/cpuinfo
# end up serving all zeros for serial
serial_number = cpuinfo[CPUINFO_SERIAL_KEY]
if not int(serial_number):
return False
# probably more checks will go in here later
# untill we have a hal.
return True
def load_cpu_info() -> dict:
'''
returns /proc/cpuinfo as dict, keys are case-insensitive
'''
cpuinfo = {}
try:
with open("/proc/cpuinfo", "r") as f:
lines = f.readlines()
for line in lines:
pair = line.split(":")
if len(pair) == 2:
cpuinfo[pair[0].strip().lower()] = pair[1].strip()
except Exception as e:
logging.warning(f"failed to load /proc/cpuinfo: {e}")
return cpuinfo
@retry(Exception, tries=5, delay=5, max_delay=15, backoff=2, logger=logging)
def lora_module_test():
"""
Checks the status of the lore module.
Returns true or false.
"""
result = None
pkt_fwd_diag_file = "/var/pktfwd/diagnostics"
while result is None:
try:
# The Pktfwder container creates this file
# to pass over the status.
with open(pkt_fwd_diag_file) as data:
lora_status = data.read()
if lora_status == "true":
result = True
else:
result = False
except FileNotFoundError as e:
# Packet forwarder container hasn't started
logging.error(f"File {pkt_fwd_diag_file} doesn't exit yet. "
f"Most likely pktfwd container hasn't started yet")
raise e
return result
def get_public_keys_and_ignore_errors():
error_msg = None
try:
public_keys = get_public_keys_rust()
if not public_keys:
public_keys = {
'name': error_msg,
'key': error_msg
}
except Exception as e:
logging.error(e)
public_keys = {
'name': error_msg,
'key': error_msg
}
return public_keys
def is_button_present(diagnostics):
variant = diagnostics.get('VA')
button = get_variant_attribute(variant, 'BUTTON')
return bool(button)
def get_device_metrics():
try:
temperature = psutil.sensors_temperatures()['cpu_thermal'][0].current
except Exception:
temperature = 0
return {
'cpu': psutil.cpu_percent(),
'memory_total': psutil.virtual_memory().total,
'memory_used': psutil.virtual_memory().used,
'disk_total': psutil.disk_usage('/').total,
'disk_used': psutil.disk_usage('/').used,
'temperature': temperature
}
if __name__ == '__main__':
logging.info('get_wifi_devices(): %s' % get_wifi_devices())
logging.info('set_diagnostics_bt_lte(): %s' % set_diagnostics_bt_lte({}))