-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathcheck_gpio.py
executable file
·261 lines (223 loc) · 7.67 KB
/
check_gpio.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
#!/usr/bin/env python3
# This file is part of Checkbox.
#
# Copyright 2024 Canonical Ltd.
# Written by:
# Rick Wu <rick.wu@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox 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 GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>
import argparse
import os
from contextlib import contextmanager
from checkbox_support.snap_utils.snapd import Snapd
from checkbox_support.snap_utils.system import get_gadget_snap
import requests
from typing import Dict, List
def list_gpio_slots(
snapd: object, gadget_name: str
) -> Dict[str, Dict[str, int]]:
"""
List GPIO slots defined by a gadget snap.
Args:
snapd: A Snapd object for interacting with Snapd.
gadget_name: The name of the gadget snap.
Returns:
A dictionary containing GPIO slot information.
"""
gpio_slot = {}
# Go through whole response of "Snapd.interface()", and parser out
# the interfaces that is GPIO and define by gadget snap.
for slot in snapd.interfaces()["slots"]:
if slot["interface"] == "gpio" and slot["snap"] == gadget_name:
gpio_slot[slot["slot"]] = {"number": slot["attrs"]["number"]}
return gpio_slot
def parse_config(config: str) -> List[int]:
"""
Parse a configuration string containing port numbers or ranges
of port numbers.
Args:
config (str): A comma-separated string containing port numbers
or ranges of port numbers.
Port ranges are specified using the format 'start:end'.
Returns:
list: A list containing all the port numbers parsed from
the configuration string.
Example:
>>> parse_config("1,2,5:7,10")
[1, 2, 5, 6, 7, 10]
"""
expect_port = []
if not config:
raise ValueError("Error: Config is empty!")
for port_list in config.split(","):
if ":" not in port_list:
expect_port.append(int(port_list))
else:
start_port = port_list.split(":")[0]
end_port = port_list.split(":")[1]
try:
start_port = int(start_port)
end_port = int(end_port)
if start_port > end_port:
raise ValueError(
"Invalid port range: {}".format(port_list)
)
for range_port in range(start_port, end_port + 1):
expect_port.append(range_port)
except ValueError:
raise ValueError("Invalid port range: {}".format(port_list))
return expect_port
def check_gpio_list(gpio_list: Dict[int, Dict[str, int]], config: str) -> None:
"""
Check if all expected GPIO numbers are defined in the gadget snap.
Args:
gpio_list: A dictionary containing GPIO slot information.
config: Checkbox config including expected GPIO numbers.
e.g. EXPECTED_GADGET_GPIO=499,500,501:504
Sprate by comma, and also colon to define a range of ports
Raises:
SystemExit: If any expected GPIO slot is not defined in the gadget
snap.
"""
if not gpio_list:
raise SystemExit("Error: No any GPIO slots existed!")
expect_port = parse_config(config)
for gpio in gpio_list.values():
if gpio["number"] in expect_port:
expect_port.remove(gpio["number"])
if expect_port:
for gpio_slot in expect_port:
print(
"Error: Slot of GPIO {} is not defined in gadget snap".format(
gpio_slot
)
)
raise SystemExit(1)
else:
print("All expected GPIO slots have been defined in gadget snap.")
@contextmanager
def interface_test(
gpio_slot: str, gadget_name: str, timeout: int = 60
) -> None:
snap = os.environ["SNAP_NAME"]
timeout = int(os.environ.get("SNAPD_TASK_TIMEOUT", timeout))
try:
connect_interface(gadget_name, gpio_slot, snap, timeout)
yield
finally:
disconnect_interface(gadget_name, gpio_slot, snap, timeout)
def connect_interface(
gadget_name: str, gpio_slot: str, snap: str, timeout: int
) -> None:
"""
Connect GPIO plugs of checkbox to GPIO slots of gadget snap.
Args:
gpio_slot: A GPIO slot information.
gadget_name: The name of the gadget snap.
Raises:
SystemExit: If failed to connect any GPIO.
"""
# Get the snap name of checkbox
print("Attempting connect GPIO to {}:{}".format(gadget_name, gpio_slot))
try:
Snapd(task_timeout=timeout).connect(
gadget_name, gpio_slot, snap, "gpio"
)
print("Success")
except requests.HTTPError:
print("Failed to connect {}".format(gpio_slot))
raise SystemExit(1)
def disconnect_interface(
gadget_name: str, gpio_slot: str, snap: str, timeout: int
) -> None:
"""
Connect GPIO plugs of checkbox to GPIO slots of gadget snap.
Args:
gpio_slot: A GPIO slot information.
gadget_name: The name of the gadget snap.
Raises:
SystemExit: If failed to connect any GPIO.
"""
# Get the snap name of checkbox
print(
"Attempting disconnect GPIO slot {}:{}".format(gadget_name, gpio_slot)
)
try:
Snapd(task_timeout=timeout).disconnect(
gadget_name, gpio_slot, snap, "gpio"
)
print("Success")
except requests.HTTPError:
print("Failed to disconnect {}".format(gpio_slot))
raise SystemExit(1)
def check_node(num: int) -> None:
"""
Check if a GPIO node is exported.
Args:
num: The GPIO number to check.
Raises:
SystemExit: If the GPIO node does not exist.
"""
path = "/sys/class/gpio/gpio{}".format(num)
if os.path.exists(path):
print("GPIO node of {} exist!".format(num))
else:
raise SystemExit("GPIO node of {} not exist!".format(num))
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
dest="action",
help="Action in check-gpio, check-node and dump",
)
check_gpio_subparser = subparsers.add_parser("check-gpio")
check_gpio_subparser.add_argument(
"-c",
"--config",
required=True,
help="Checkbox config include expected GPIO\
e.g. 499:500:501:502",
)
check_node_subparser = subparsers.add_parser("check-node")
check_node_subparser.add_argument(
"-n",
"--num",
type=int,
required=True,
help="GPIO number to check if node exported",
)
check_node_subparser.add_argument(
"-s",
"--slot",
type=str,
required=True,
help="GPIO slot to connect.",
)
subparsers.add_parser("dump", help="Dump GPIO slots from gadget")
args = parser.parse_args()
snapd = Snapd()
gadget_name = get_gadget_snap()
gpio_slots = list_gpio_slots(snapd, gadget_name)
if args.action == "check-gpio":
check_gpio_list(gpio_slots, args.config)
if args.action == "dump":
for x in gpio_slots:
print(
"slot: {}\ngpio_number: {}\n".format(
x, gpio_slots[x]["number"]
)
)
if args.action == "check-node":
with interface_test(args.slot, gadget_name):
check_node(args.num)
if __name__ == "__main__":
main()