-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_manager.py
More file actions
83 lines (68 loc) · 2.31 KB
/
Copy pathsim_manager.py
File metadata and controls
83 lines (68 loc) · 2.31 KB
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
import subprocess
import time
import os
import signal
import sys
ECU_TYPES = ["engine", "brakes", "doors", "lights"]
PROCESSES = []
def start_simulator():
print("🚀 Starting Ford FNV Network Simulator...")
gw_proc = subprocess.Popen(
["./build/gateway"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
PROCESSES.append(gw_proc)
time.sleep(1)
for ecu in ECU_TYPES:
print(f"🔌 Launching {ecu.upper()} ECU...")
p = subprocess.Popen(["./build/ecu_node", ecu], stdout=subprocess.DEVNULL)
PROCESSES.append(p)
return gw_proc
def reset_ecu(ecu_type):
print(f"🔄 Manager triggering RESET for {ecu_type}...")
for p in PROCESSES:
if len(p.args) > 1 and p.args[1] == ecu_type:
p.kill()
p.wait()
PROCESSES.remove(p)
break
new_p = subprocess.Popen(["./build/ecu_node", ecu_type], stdout=subprocess.DEVNULL)
PROCESSES.append(new_p)
print(f"✅ {ecu_type.upper()} ECU has been restarted.")
def cleanup(sig, frame):
print("\n🛑 Shutting down vehicle network...")
for p in PROCESSES:
p.terminate()
exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, cleanup)
if not os.path.exists("./build/gateway") or not os.path.exists("./build/ecu_node"):
print("❌ Error: Run 'make' in build folder first.")
sys.exit(1)
# 1. Start the system
gw = start_simulator()
# 2. Test Failure: Kill engine after 10 seconds
print("\n⏳ System running. Waiting 10s before injecting fault...")
time.sleep(10)
print("\n🔥 FAULT INJECTION: Killing Engine ECU process...")
for p in PROCESSES:
if len(p.args) > 1 and p.args[1] == "engine":
p.kill()
p.wait()
break
# 3. Monitor Loop: Read Gateway output for ALERTS
try:
while True:
line = gw.stdout.readline()
if line:
print(line.strip()) # Show the gateway output
if "ALERT: ECU [1]" in line:
reset_ecu("engine")
# Check if gateway process died
if gw.poll() is not None:
break
except KeyboardInterrupt:
cleanup(None, None)