Repository navigation
Expand file tree
/
Copy patherror.c
More file actions
111 lines (98 loc) · 3.01 KB
/
Copy patherror.c
File metadata and controls
111 lines (98 loc) · 3.01 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
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
#include "error.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
FaultInjection* fault_injection_init(void) {
FaultInjection* fi = (FaultInjection*)malloc(sizeof(FaultInjection));
fi->fault_enabled = 0;
fi->fault_type = 0;
fi->fault_active = 0;
fi->last_alert_time = 0;
return fi;
}
void enable_fault_injection(FaultInjection* fi) {
if (fi->fault_enabled == 0) {
fi->fault_enabled = 1;
fi->fault_active = 1;
fi->fault_type = (fi->fault_type + 1) % 3;
printf("\n[FAULT INJECTION] 🔴 ENABLED - Type: ");
switch(fi->fault_type) {
case 0:
printf("VOLTAGE FAULT (220V → -50V)\n");
break;
case 1:
printf("CURRENT FAULT (24A → 45A OVERLOAD)\n");
break;
case 2:
printf("EXTREME OVERLOAD (5500W → 8500W)\n");
break;
}
} else {
fi->fault_type = (fi->fault_type + 1) % 3;
printf("\n[FAULT INJECTION] 🔄 SWITCHING - Type: ");
switch(fi->fault_type) {
case 0:
printf("VOLTAGE FAULT (220V → -50V)\n");
break;
case 1:
printf("CURRENT FAULT (24A → 45A OVERLOAD)\n");
break;
case 2:
printf("EXTREME OVERLOAD (5500W → 8500W)\n");
break;
}
}
}
void disable_fault_injection(FaultInjection* fi) {
if (fi->fault_enabled) {
fi->fault_enabled = 0;
fi->fault_active = 0;
printf("\n[FAULT INJECTION] 🟢 DISABLED - System restored to normal\n");
}
}
void apply_fault_injection(FaultInjection* fi, SensorData* sensor_data) {
if (!fi->fault_enabled || !fi->fault_active) {
return;
}
switch(fi->fault_type) {
case 0:
sensor_data->voltage = -50.0f;
break;
case 1:
sensor_data->current = 45.0f;
break;
case 2:
sensor_data->voltage = 230.0f;
sensor_data->current = 45.0f;
break;
}
}
int is_fault_injection_active(FaultInjection* fi) {
return fi->fault_enabled;
}
void print_fault_alert(FaultInjection* fi) {
if (!fi->fault_enabled || !fi->fault_active) {
return;
}
time_t now = time(NULL);
// Print alert every 2 seconds
if (now - fi->last_alert_time >= 2) {
printf("\n\n==========================================\n");
printf(" FAULT DETECTED!!! SHUT DOWN SYSTEM\n");
printf("==========================================\n");
printf(" Fault Type: ");
switch(fi->fault_type) {
case 0:
printf("VOLTAGE FAULT\n");
break;
case 1:
printf("CURRENT OVERLOAD\n");
break;
case 2:
printf("EXTREME OVERLOAD\n");
break;
}
printf("\n");
fi->last_alert_time = now;
}
}