forked from SmingHub/Sming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.cpp
218 lines (196 loc) · 6.02 KB
/
application.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
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
#include <user_config.h>
#include <SmingCore.h>
// download urls, set appropriately
#define ROM_0_URL "http://192.168.7.5:80/rom0.bin"
#define ROM_1_URL "http://192.168.7.5:80/rom1.bin"
#define SPIFFS_URL "http://192.168.7.5:80/spiff_rom.bin"
// If you want, you can define WiFi settings globally in Eclipse Environment Variables
#ifndef WIFI_SSID
#define WIFI_SSID "PleaseEnterSSID" // Put you SSID and Password here
#define WIFI_PWD "PleaseEnterPass"
#endif
rBootHttpUpdate* otaUpdater = 0;
void OtaUpdate_CallBack(rBootHttpUpdate& client, bool result)
{
Serial.println("In callback...");
if(result == true) {
// success
uint8 slot;
slot = rboot_get_current_rom();
if(slot == 0)
slot = 1;
else
slot = 0;
// set to boot new rom and then reboot
Serial.printf("Firmware updated, rebooting to rom %d...\r\n", slot);
rboot_set_current_rom(slot);
System.restart();
} else {
// fail
Serial.println("Firmware update failed!");
}
}
void OtaUpdate()
{
uint8 slot;
rboot_config bootconf;
Serial.println("Updating...");
// need a clean object, otherwise if run before and failed will not run again
if(otaUpdater)
delete otaUpdater;
otaUpdater = new rBootHttpUpdate();
// select rom slot to flash
bootconf = rboot_get_config();
slot = bootconf.current_rom;
if(slot == 0)
slot = 1;
else
slot = 0;
#ifndef RBOOT_TWO_ROMS
// flash rom to position indicated in the rBoot config rom table
otaUpdater->addItem(bootconf.roms[slot], ROM_0_URL);
#else
// flash appropriate rom
if(slot == 0) {
otaUpdater->addItem(bootconf.roms[slot], ROM_0_URL);
} else {
otaUpdater->addItem(bootconf.roms[slot], ROM_1_URL);
}
#endif
#ifndef DISABLE_SPIFFS
// use user supplied values (defaults for 4mb flash in makefile)
if(slot == 0) {
otaUpdater->addItem(RBOOT_SPIFFS_0, SPIFFS_URL);
} else {
otaUpdater->addItem(RBOOT_SPIFFS_1, SPIFFS_URL);
}
#endif
// request switch and reboot on success
//otaUpdater->switchToRom(slot);
// and/or set a callback (called on failure or success without switching requested)
otaUpdater->setCallback(OtaUpdate_CallBack);
// start update
otaUpdater->start();
}
void Switch()
{
uint8 before, after;
before = rboot_get_current_rom();
if(before == 0)
after = 1;
else
after = 0;
Serial.printf("Swapping from rom %d to rom %d.\r\n", before, after);
rboot_set_current_rom(after);
Serial.println("Restarting...\r\n");
System.restart();
}
void ShowInfo()
{
Serial.printf("\r\nSDK: v%s\r\n", system_get_sdk_version());
Serial.printf("Free Heap: %d\r\n", system_get_free_heap_size());
Serial.printf("CPU Frequency: %d MHz\r\n", system_get_cpu_freq());
Serial.printf("System Chip ID: %x\r\n", system_get_chip_id());
Serial.printf("SPI Flash ID: %x\r\n", spi_flash_get_id());
//Serial.printf("SPI Flash Size: %d\r\n", (1 << ((spi_flash_get_id() >> 16) & 0xff)));
rboot_config conf;
conf = rboot_get_config();
debugf("Count: %d", conf.count);
debugf("ROM 0: %d", conf.roms[0]);
debugf("ROM 1: %d", conf.roms[1]);
debugf("ROM 2: %d", conf.roms[2]);
debugf("GPIO ROM: %d", conf.gpio_rom);
}
void serialCallBack(Stream& stream, char arrivedChar, unsigned short availableCharsCount)
{
int pos = stream.indexOf('\n');
if(pos > -1) {
char str[pos + 1];
for(int i = 0; i < pos + 1; i++) {
str[i] = stream.read();
if(str[i] == '\r' || str[i] == '\n') {
str[i] = '\0';
}
}
if(!strcmp(str, "connect")) {
// connect to wifi
WifiStation.config(WIFI_SSID, WIFI_PWD);
WifiStation.enable(true);
WifiStation.connect();
} else if(!strcmp(str, "ip")) {
Serial.printf("ip: %s mac: %s\r\n", WifiStation.getIP().toString().c_str(), WifiStation.getMAC().c_str());
} else if(!strcmp(str, "ota")) {
OtaUpdate();
} else if(!strcmp(str, "switch")) {
Switch();
} else if(!strcmp(str, "restart")) {
System.restart();
} else if(!strcmp(str, "ls")) {
Vector<String> files = fileList();
Serial.printf("filecount %d\r\n", files.count());
for(unsigned int i = 0; i < files.count(); i++) {
Serial.println(files[i]);
}
} else if(!strcmp(str, "cat")) {
Vector<String> files = fileList();
if(files.count() > 0) {
Serial.printf("dumping file %s:\r\n", files[0].c_str());
Serial.println(fileGetContent(files[0]));
} else {
Serial.println("Empty spiffs!");
}
} else if(!strcmp(str, "info")) {
ShowInfo();
} else if(!strcmp(str, "help")) {
Serial.println();
Serial.println("available commands:");
Serial.println(" help - display this message");
Serial.println(" ip - show current ip address");
Serial.println(" connect - connect to wifi");
Serial.println(" restart - restart the esp8266");
Serial.println(" switch - switch to the other rom and reboot");
Serial.println(" ota - perform ota update, switch rom and reboot");
Serial.println(" info - show esp8266 info");
#ifndef DISABLE_SPIFFS
Serial.println(" ls - list files in spiffs");
Serial.println(" cat - show first file in spiffs");
#endif
Serial.println();
} else {
Serial.println("unknown command");
}
}
}
void init()
{
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default
Serial.systemDebugOutput(true); // Debug output to serial
// mount spiffs
int slot = rboot_get_current_rom();
#ifndef DISABLE_SPIFFS
if(slot == 0) {
#ifdef RBOOT_SPIFFS_0
debugf("trying to mount spiffs at 0x%08x, length %d", RBOOT_SPIFFS_0, SPIFF_SIZE);
spiffs_mount_manual(RBOOT_SPIFFS_0, SPIFF_SIZE);
#else
debugf("trying to mount spiffs at 0x%08x, length %d", 0x100000, SPIFF_SIZE);
spiffs_mount_manual(0x100000, SPIFF_SIZE);
#endif
} else {
#ifdef RBOOT_SPIFFS_1
debugf("trying to mount spiffs at 0x%08x, length %d", RBOOT_SPIFFS_1, SPIFF_SIZE);
spiffs_mount_manual(RBOOT_SPIFFS_1, SPIFF_SIZE);
#else
debugf("trying to mount spiffs at 0x%08x, length %d", 0x300000, SPIFF_SIZE);
spiffs_mount_manual(0x300000, SPIFF_SIZE);
#endif
}
#else
debugf("spiffs disabled");
#endif
WifiAccessPoint.enable(false);
Serial.printf("\r\nCurrently running rom %d.\r\n", slot);
Serial.println("Type 'help' and press enter for instructions.");
Serial.println();
Serial.setCallback(serialCallBack);
}