-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathfactory.c
More file actions
191 lines (174 loc) · 7.12 KB
/
Copy pathfactory.c
File metadata and controls
191 lines (174 loc) · 7.12 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
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
/**
* SSAS - Simple Smart Automotive Software
* Copyright (C) 2021 Parai Wang <parai@foxmail.com>
*
*/
/* ================================ [ INCLUDES ] ============================================== */
#include "factory.h"
#include "Std_Debug.h"
/* ================================ [ MACROS ] ============================================== */
#define AS_LOG_FACTORY 0
/* ================================ [ TYPES ] ============================================== */
/* ================================ [ DECLARES ] ============================================== */
/* ================================ [ DATAS ] ============================================== */
/* ================================ [ LOCALS ] ============================================== */
static Std_ReturnType factory_health_check(const factory_t *factory) {
Std_ReturnType ret = E_NOT_OK;
uint8_t machineId;
uint8_t nodeId;
machineId = factory->context->machineId;
if (machineId < factory->numOfMachines) {
nodeId = factory->context->nodeId;
if (nodeId < factory->machines[machineId].numOfNodes) {
ret = E_OK;
} else {
factory_init(factory);
ASLOG(ERROR, ("%s: Invalid NodeId %d for %s\n", factory->name, nodeId,
factory->machines[machineId].name));
}
} else {
factory_init(factory);
ASLOG(ERROR, ("%s: Invalid machineId %d\n", factory->name, machineId));
}
return ret;
}
Std_ReturnType factory_post(const factory_t *factory, uint8_t machineId, uint8_t nodeId,
Std_ReturnType ercd) {
Std_ReturnType ret = ercd;
if (E_OK == ercd) {
/* pass */
} else if (FACTORY_E_RETRY == ercd) {
factory->context->state = FACTORY_RUNNING;
ret = E_OK;
} else if (FACTORY_E_EVENT == ercd) {
factory->context->state = FACTORY_WAITING;
ret = E_OK;
} else if (FACTORY_E_STOP == ercd) {
ASLOG(FACTORY, ("%s: %s stopped\n", factory->name, factory->machines[machineId].name));
factory_init(factory);
factory->stateNotification(machineId, MACHINE_STOP);
ret = E_OK;
} else if ((ercd >= FACTORY_E_SWITCH_TO) &&
(ercd < (FACTORY_E_SWITCH_TO + FACTORY_MAX_MACHINES))) {
factory->stateNotification(machineId, MACHINE_STOP);
machineId = ercd - FACTORY_E_SWITCH_TO;
if (machineId < factory->numOfMachines) {
factory->context->machineId = machineId;
factory->context->nodeId = 0;
ASLOG(FACTORY, ("%s: switch to %s\n", factory->name, factory->machines[machineId].name));
ret = FACTORY_E_SWITCH_TO;
} else {
ASLOG(ERROR, ("%s: Invalid machineId %d\n", factory->name, machineId));
factory_init(factory);
ret = E_NOT_OK;
}
} else if ((ercd >= FACTORY_E_GOTO) && (ercd < (FACTORY_E_GOTO + FACTORY_MAX_NODES))) {
nodeId = ercd - FACTORY_E_GOTO;
if (nodeId < factory->machines[machineId].numOfNodes) {
factory->context->nodeId = nodeId;
ASLOG(FACTORY, ("%s: goto %s:%s\n", factory->name, factory->machines[machineId].name,
factory->machines[machineId].nodes[nodeId].name));
ret = FACTORY_E_GOTO;
} else {
ASLOG(ERROR, ("%s: Invalid NodeId %d for %s\n", factory->name, nodeId,
factory->machines[machineId].name));
factory_init(factory);
ret = E_NOT_OK;
}
} else {
ASLOG(ERROR,
("%s: %s %s failed with ercd %d\n", factory->name, factory->machines[machineId].name,
factory->machines[machineId].nodes[nodeId].name, ercd));
factory_init(factory);
factory->stateNotification(machineId, MACHINE_FAIL);
}
return ret;
}
/* ================================ [ FUNCTIONS ] ============================================== */
void factory_init(const factory_t *factory) {
factory->context->state = FACTORY_IDLE;
factory->context->machineId = 0;
factory->context->nodeId = 0;
}
void factory_cancel(const factory_t *factory) {
Std_ReturnType ret;
uint8_t machineId;
ret = factory_health_check(factory);
if (E_OK == ret) {
machineId = factory->context->machineId;
factory_init(factory);
factory->stateNotification(machineId, MACHINE_FAIL);
}
}
Std_ReturnType factory_main(const factory_t *factory) {
Std_ReturnType ret = E_OK;
uint8_t machineId;
uint8_t nodeId;
if (FACTORY_RUNNING == factory->context->state) {
ret = factory_health_check(factory);
if (E_OK == ret) {
machineId = factory->context->machineId;
nodeId = factory->context->nodeId;
ASLOG(FACTORY, ("%s: %s %s main\n", factory->name, factory->machines[machineId].name,
factory->machines[machineId].nodes[nodeId].name));
ret = factory->machines[machineId].nodes[nodeId].main();
ret = factory_post(factory, machineId, nodeId, ret);
}
}
return ret;
}
Std_ReturnType factory_on_event(const factory_t *factory, uint8_t eventId) {
Std_ReturnType ret = E_NOT_OK;
uint8_t machineId;
uint8_t nodeId;
if (FACTORY_WAITING == factory->context->state) {
ret = factory_health_check(factory);
if (E_OK == ret) {
machineId = factory->context->machineId;
nodeId = factory->context->nodeId;
if (eventId < factory->machines[machineId].nodes[nodeId].numOfEvents) {
ASLOG(FACTORY, ("%s: %s %s on event %d\n", factory->name, factory->machines[machineId].name,
factory->machines[machineId].nodes[nodeId].name, eventId));
ret = factory->machines[machineId].nodes[nodeId].events[eventId]();
ret = factory_post(factory, machineId, nodeId, ret);
if ((FACTORY_E_SWITCH_TO == ret) || (FACTORY_E_GOTO == ret)) {
factory->context->state = FACTORY_RUNNING;
ret = factory_main(factory);
} else {
/* do nothing as pass or fail */
}
}
} else {
ASLOG(ERROR,
("%s: %s %s on invalid event %d\n", factory->name,
factory->machines[factory->context->machineId].name,
factory->machines[factory->context->machineId].nodes[factory->context->nodeId].name,
eventId));
factory_init(factory);
ret = E_NOT_OK;
}
}
return ret;
}
Std_ReturnType factory_start_machine(const factory_t *factory, uint8_t machineId) {
Std_ReturnType ret = E_NOT_OK;
if (machineId < factory->numOfMachines) {
if (FACTORY_IDLE == factory->context->state) {
factory->context->state = FACTORY_RUNNING;
factory->context->machineId = machineId;
factory->context->nodeId = 0;
ret = E_OK;
ASLOG(FACTORY, ("%s: start %s\n", factory->name, factory->machines[machineId].name));
} else {
ASLOG(ERROR, ("%s: %s start failed, current machine %d node %d state %d\n", factory->name,
factory->machines[machineId].name, factory->context->machineId,
factory->context->nodeId, factory->context->state));
}
} else {
ASLOG(ERROR, ("%s: invalid machineId %d\n", factory->name, machineId));
}
return ret;
}
uint8_t factory_get_state(const factory_t *factory) {
return factory->context->state;
}