-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_event.cpp
122 lines (98 loc) · 2.56 KB
/
serial_event.cpp
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
#include "serial_event.h"
#define CREDENTIALS_CHANGE_COMPLETED() (login_state == credentials_change_completed)
Nvm_Manager eep;
Serial_Event::Serial_Event()
{
Serial.begin(SERIAL_BAUDRATE);
login_state = credentials_change_completed;
/* Reserve bytes for inputString */
inputString = "";
new_ssid = "";
new_password = "";
inputString.reserve(EEPROM_SIZE_BYTE);
new_ssid.reserve(EEPROM_SIZE_BYTE);
new_password.reserve(EEPROM_SIZE_BYTE);
}
/*
* Perform an action according to inputString given in
* the Serial_RxEvent
*/
void Serial_Event::Serial_ParseString(String s)
{
switch(login_state)
{
case credentials_change_request:
{
new_ssid = s;
login_state = credentials_ssid_stored;
Serial.println("LOGIN -> Enter PASSWORD");
break;
}
case credentials_ssid_stored:
{
new_password = s;
login_state = credentials_change_completed;
/* Write new credentials to the NvM */
eep.Nvm_CredentialsWrite(new_ssid.c_str(), new_password.c_str(), strlen(new_ssid.c_str()), strlen(new_password.c_str()));
Serial.println("LOGIN -> Credentials CHANGED");
/* Start reconnect timer */
Start_reconnect_tmr(TMR_RECONNECT_TIMEOUT_MS);
break;
}
case credentials_change_completed:
{
break;
}
default:
{
Serial.println("LOGIN -> Unexpected error");
break;
}
}
if((String("reboot") == s) && CREDENTIALS_CHANGE_COMPLETED())
{
Serial.println("REBOOT -> Reboot in progress");
WiFi.disconnect();
ESP.restart();
/* Wait until the reset occurs */
while(1);
}
else if((String("login") == s) && CREDENTIALS_CHANGE_COMPLETED())
{
/* Stop reconnect timer */
Stop_reconnect_tmr();
Serial.println("LOGIN -> Enter SSID");
login_state = credentials_change_request;
}
else
{
if(CREDENTIALS_CHANGE_COMPLETED())
{
Serial.println("OK");
}
}
/* Clear the string */
inputString = "";
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void Serial_Event::Serial_RxEvent()
{
while(Serial.available())
{
char inChar = (char)Serial.read();
/* if the incoming character is a newline - set a flag */
if(inChar == '\n')
{
Serial_ParseString(inputString);
}
else
{
/* Add new char to the inputString */
inputString += inChar;
}
}
}