-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL3_FSMmain.cpp
More file actions
239 lines (211 loc) · 7.15 KB
/
L3_FSMmain.cpp
File metadata and controls
239 lines (211 loc) · 7.15 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
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
#include "L3_FSMevent.h"
#include "L3_msg.h"
#include "L3_timer.h"
#include "L3_LLinterface.h"
#include "protocol_parameters.h"
#include "mbed.h"
#include "L3_Quiz.h"
#include "L3_2min_timer.h"
#include "L3_state.h"
L3_State l3_state = IDLE;
static L3_State prev_state = IDLE;
static uint8_t originalWord[1030];
static uint8_t wordLen = 0;
static uint8_t sdu[1030];
static Serial pc(USBTX, USBRX);
static uint8_t myDestId;
static uint8_t myId;
void L3_quiz_showSelectedToUser(Serial &pc); //
void L3service_processInputWord(void)
{
char c = pc.getc();
if (!L3_event_checkEventFlag(L3_event_dataToSend))
{
if (c == '\n' || c == '\r')
{
originalWord[wordLen++] = '\0';
L3_event_setEventFlag(L3_event_dataToSend);
debug_if(DBGMSG_L3, "word is ready! ::: %s\n", originalWord);
}
else
{
originalWord[wordLen++] = c;
if (wordLen >= L3_MAXDATASIZE - 1)
{
originalWord[wordLen++] = '\0';
L3_event_setEventFlag(L3_event_dataToSend);
pc.printf("\n max reached! word forced to be ready :::: %s\n", originalWord);
}
}
}
}
void L3_initFSM(uint8_t thisId, uint8_t destId)
{
myId = thisId;
myDestId = destId;
pc.attach(&L3service_processInputWord, Serial::RxIrq);
// pc.printf("Give a word to send : ");
}
void L3_FSMrun(void)
{
if (prev_state != l3_state)
{
debug_if(DBGMSG_L3, "[L3] State transition from %s to %s\n",
L3_stateToStr(prev_state), L3_stateToStr(l3_state));
prev_state = l3_state;
}
switch (l3_state)
{
case WAIT_ANSWER:
{
L3_quiz_showMenuToHost(pc);
QuizSelectResult result = L3_quiz_select(pc);
if (result == QUIZ_SELECTION_FAILED)
{
pc.printf("[ERROR] Quiz selection failed. Terminating.\n\n");
l3_state = TERMINATE;
break;
}
if (result == QUIZ_JOIN_AS_USER)
{
const char *joinMsg = "join";
L3_LLI_dataReqFunc((uint8_t *)joinMsg, strlen(joinMsg) + 1, myDestId);
pc.printf("[You] join (sent)\n\n");
pc.printf("[Switching to IDLE state.]\n\n");
l3_state = IDLE;
break;
}
// 정상 퀴즈 선택
char quizIndexMsg[8];
sprintf(quizIndexMsg, "quiz%d", selected_quiz_index);
L3_LLI_dataReqFunc((uint8_t *)quizIndexMsg, strlen(quizIndexMsg) + 1, myDestId);
pc.printf("[Quiz index %d sent to user.]\n\n", selected_quiz_index);
l3_state = CHAT_READY;
break;
}
case WAIT_QUIZ:
{
// pc.printf(":: role: USER\n");
L3_quiz_showSelectedToUser(pc);
pc.attach(NULL, Serial::RxIrq); // 시리얼 인터럽트 해제
const int maxAttempts = 3;
Timer quizTimer;
quizTimer.start();
for (int attempt = 1; attempt <= maxAttempts; attempt++)
{
float elapsed = quizTimer.read();
float remaining = 120.0f - elapsed;
if (remaining <= 0.0f)
{
pc.printf("[TIMEOUT] 2 minutes are over. Terminating.\n\n");
l3_state = TERMINATE;
break;
}
pc.printf("\n[Attempt %d/%d (Time left: %d seconds\n\n)]", attempt, maxAttempts, (int)remaining);
pc.printf("Answer: ");
char answerBuffer[MAX_PASSWORD_LEN] = {0};
bool gotInput = getInputWithinTime(pc, answerBuffer, sizeof(answerBuffer), remaining);
if (!gotInput)
{
pc.printf("\n[Incorrect answer. (No input detected)]\n\n");
continue;
}
if (L3_quiz_isAnswerCorrect(answerBuffer))
{
pc.printf("\n[Correct! Moving to CHAT_READY state.]\n\n");
l3_state = CHAT_READY;
break;
}
else
{
pc.printf("\n[Incorrect answer.]\n\n");
}
if (attempt == maxAttempts)
{
pc.printf("[You've used all attempts. Terminating session.]\n\n");
l3_state = TERMINATE;
}
}
pc.attach(&L3service_processInputWord, Serial::RxIrq); // 다시 attach
break;
}
case IDLE:
{
if (L3_event_checkEventFlag(L3_event_msgRcvd))
{
uint8_t *dataPtr = L3_LLI_getMsgPtr();
uint8_t size = L3_LLI_getSize();
char printBuf[1030] = {0};
memcpy(printBuf, dataPtr, size);
printBuf[size] = '\0';
// debug("\n -------------------------------------------------\n");
// debug("RCVD MSG : %s (length:%i)\n", printBuf, size);
// debug(" -------------------------------------------------\n");
if (strncmp((char *)dataPtr, "quiz", 4) == 0)
{
selected_quiz_index = dataPtr[4] - '0';
if (selected_quiz_index >= 0 && selected_quiz_index < QUIZ_TOTAL_COUNT)
{
strncpy(selected_answer, quiz_answers[selected_quiz_index], MAX_PASSWORD_LEN);
pc.printf("[Quiz Received] Index: %d\n\n", selected_quiz_index);
l3_state = WAIT_QUIZ;
}
else
{
pc.printf("[Error] Invalid quiz index received.\n\n");
l3_state = TERMINATE;
}
L3_event_clearEventFlag(L3_event_msgRcvd);
return;
}
if (strncmp((char *)dataPtr, "quit", 4) == 0)
{
pc.printf("['quit' received from peer. Terminating chat...]\n\n");
l3_state = TERMINATE;
L3_event_clearEventFlag(L3_event_msgRcvd);
return;
}
pc.printf("[Peer] %s\n\n", printBuf);
// pc.printf("Give a word to send : ");
L3_event_clearEventFlag(L3_event_msgRcvd);
}
else if (L3_event_checkEventFlag(L3_event_dataToSend))
{
if (strcmp((char *)originalWord, "quit") == 0)
{
l3_state = TERMINATE;
pc.printf("[Chat terminated by user input.]\n\n");
wordLen = 0;
memset(originalWord, 0, sizeof(originalWord));
L3_event_clearEventFlag(L3_event_dataToSend);
break;
}
strcpy((char *)sdu, (char *)originalWord);
// debug("[L3] msg length : %i\n", wordLen);
L3_LLI_dataReqFunc(sdu, wordLen, myDestId);
pc.printf("[You] %s\n\n", sdu);
wordLen = 0;
// pc.printf("Give a word to send : ");
L3_event_clearEventFlag(L3_event_dataToSend);
}
break;
}
case CHAT_READY:
{
pc.printf("[Entering CHAT_READY...]\n\n");
l3_state = IDLE;
break;
}
case TERMINATE:
{
pc.printf("[Entering TERMINATE. Chat session ended.]\n\n");
L3_event_clearAllEventFlag();
pc.attach(NULL);
while (1)
;
break;
}
default:
break;
}
}