1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/coloz-blinker-library

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
BlinkerOTA.h 20 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
i3water Отправлено 4 лет назад 081c5c8
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
#ifndef BLINKER_OTA_H
#define BLINKER_OTA_H
#if (defined(ESP8266) || defined(ESP32))
#include "../Blinker/BlinkerConfig.h"
#include "../Blinker/BlinkerDebug.h"
#include <EEPROM.h>
#if defined(ESP8266)
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#ifndef BLINKER_WITHOUT_SSL
extern BearSSL::WiFiClientSecure client_mqtt;
#endif
#elif defined(ESP32)
#include <WiFi.h>
#include <Update.h>
// extern WiFiClient client_s;
extern WiFiClientSecure client_s;
#endif
#include "BlinkerUpdater.h"
// extern WiFiClientSecure client_s;
enum bota_status_t{
BLINKER_UPGRADE_DISABLE,
BLINKER_UPGRADE_BEGIN,
BLINKER_UPGRADE_START,
BLINKER_UPGRADE_FAIL,
BLINKER_UPGRADE_LOAD_FAIL,
BLINKER_UPGRADE_VERI_FAIL,
BLINKER_UPGRADE_SUCCESS
};
// #define VERSIONPARAM "1.0.1"
// if (loadOTACheck()) {
// if (!loadVersion()) {
// saveVersion();
// UPGRADE_STATUS = UPGRADE_SUCCESS;
// }
// else {
// UPGRADE_STATUS = UPGRADE_FAIL;
// }
// }
class BlinkerOTA
{
public :
BlinkerOTA() {}
// void parseData(char *OTAdata);
void host(String _host) { ota_host = _host; }
void url(String _url) { ota_url = _url; }
void setURL(String urlstr);
void config(String _host, String _url, String _fingerPrint, String _md5)
{
// #if defined(ESP8266)
BLINKER_LOG_ALL(_host.indexOf("https"));
if (_host.indexOf("https") != -1)
{
ota_host = _host.substring(8);
}
else
{
ota_host = _host;
}
// #else
// ota_host = _host;
// #endif
ota_url = _url;
ota_fingerPrint = _fingerPrint;
ota_md5 = _md5;
}
bool update();
// bool run();
bota_status_t status() {
return _status;
}
uint8_t loadOTACheck();
void saveOTARun();
void saveOTACheck();
void clearOTACheck();
bool loadVersion();
void saveVersion();
private :
String getHeaderValue(String header, String headerName) {
return header.substring(strlen(headerName.c_str()));
}
protected :
// #if defined(ESP32)
// uint8_t OTACheck;
// #endif
String ota_host;
String ota_url;
String ota_fingerPrint;
String ota_md5;
uint16_t ota_port = 443;
char *otaUrl;
bota_status_t _status;
// int
size_t contentLength = 0;
bool isValidContentType = false;
};
void BlinkerOTA::setURL(String url) {
otaUrl = (char*)malloc(url.length()*sizeof(char));
strcpy(otaUrl,url.c_str());
BLINKER_LOG_ALL(BLINKER_F("ota url: "), otaUrl);
_status = BLINKER_UPGRADE_START;
update();
}
bool BlinkerOTA::update() {
saveOTACheck();
#if defined(ESP8266)
// #ifndef BLINKER_WITHOUT_SSL
BearSSL::WiFiClientSecure client_s;
BLINKER_LOG_FreeHeap();
#ifndef BLINKER_WITHOUT_SSL
client_mqtt.stop();
#endif
::delay(100);
bool mfln = client_s.probeMaxFragmentLength(ota_host, ota_port, 1024);
if (mfln) {
client_s.setBufferSizes(1024, 1024);
}
// client_s.setFingerprint(ota_fingerPrint.c_str());
BLINKER_LOG_FreeHeap();
client_s.setInsecure();
// BLINKER_LOG_ALL(BLINKER_F("Connecting to: "), ota_host);
// t_httpUpdate_return ret = BlinkerhttpUpdate.update(client_s, ota_host + ota_url, ota_md5, "");
// switch (ret) {
// case HTTP_UPDATE_FAILED:
// // USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
// // break;
// BLINKER_LOG_ALL(BLINKER_F("HTTP_UPDATE_FAILD Error : "), BlinkerhttpUpdate.getLastError());
// return false;
// case HTTP_UPDATE_NO_UPDATES:
// // USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES");
// // break;
// BLINKER_LOG_ALL(BLINKER_F("HTTP_UPDATE_NO_UPDATES"));
// return false;
// case HTTP_UPDATE_OK:
// // USE_SERIAL.println("HTTP_UPDATE_OK");
// // break;
// BLINKER_LOG_ALL(BLINKER_F("HTTP_UPDATE_OK"));
// return true;
// }
// #else
// WiFiClient client_s;
// #endif
#elif defined(ESP32)
client_s.stop();
#endif
BlinkerUpdater.setMD5(ota_md5.c_str());
while (1) {
if (!client_s.connect(ota_host.c_str(), ota_port)) {
BLINKER_ERR_LOG(BLINKER_F("server connection failed"));
BLINKER_LOG_FreeHeap();
::delay(1000);
}
else {
BLINKER_LOG_ALL(BLINKER_F("connection succeed"));
BLINKER_LOG_FreeHeap();
BLINKER_LOG_ALL(BLINKER_F("Fetching Bin: "), ota_url);
String _ota_url = BLINKER_F("GET ");
_ota_url += ota_url;
_ota_url += BLINKER_F(" HTTP/1.1\r\nHost: ");
_ota_url += ota_host;
_ota_url += BLINKER_F("\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n");
// client_s.print(String("GET ") + ota_url + " HTTP/1.1\r\n" +
// "Host: " + ota_host + "\r\n" +
// "Cache-Control: no-cache\r\n" +
// "Connection: close\r\n\r\n");
client_s.print(_ota_url);
BLINKER_LOG_ALL(BLINKER_F("_ota_url: "), _ota_url);
unsigned long timeout = millis();
while (client_s.available() == 0) {
if (millis() - timeout > 5000) {
BLINKER_LOG_ALL(BLINKER_F("Client Timeout !"));
client_s.stop();
_status = BLINKER_UPGRADE_LOAD_FAIL;
return false;
}
}
// Once the response is available,
// check stuff
while (client_s.available()) {
// read line till /n
String line = client_s.readStringUntil('\n');
// remove space, to check if the line is end of headers
line.trim();
// if the the line is empty,
// this is end of headers
// break the while and feed the
// remaining `client_s` to the
// Update.writeStream();
if (!line.length()) {
//headers ended
break; // and get the OTA started
}
BLINKER_LOG_ALL(BLINKER_F("line: "), line);
// Check if the HTTP Response is 200
// else break and Exit Update
if (line.startsWith("HTTP/1.1")) {
if (line.indexOf("200") < 0) {
BLINKER_LOG_ALL(BLINKER_F("Got a non 200 status code from server. Exiting OTA Update."));
break;
}
}
// extract headers here
// Start with content length
if (line.startsWith("Content-Length: ")) {
contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str());
BLINKER_LOG_ALL(BLINKER_F("Got "), contentLength, BLINKER_F(" bytes from server"));
}
// Next, the content type
if (line.startsWith("Content-Type: ")) {
String contentType = getHeaderValue(line, "Content-Type: ");
BLINKER_LOG_ALL(BLINKER_F("Got "), contentType, BLINKER_F(" payload."));
if (contentType == "application/octet-stream") {
isValidContentType = true;
}
}
}
// Check what is the contentLength and if content type is `application/octet-stream`
BLINKER_LOG_ALL(BLINKER_F("contentLength : "),
contentLength,
BLINKER_F(", isValidContentType : "),
isValidContentType);
// check contentLength and content type
if (contentLength && isValidContentType)
{
// Check if there is enough to OTA Update
bool canBegin = BlinkerUpdater.begin(contentLength);
// If yes, begin
if (canBegin) {
BLINKER_LOG(BLINKER_F("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!"));
// No activity would appear on the Serial monitor
// So be patient. This may take 2 - 5mins to complete
size_t written = BlinkerUpdater.writeStream(client_s);
if (written == contentLength) {
BLINKER_LOG(BLINKER_F("Written : "), written, BLINKER_F(" successfully"));
}
else {
BLINKER_LOG(BLINKER_F("Written only : "), written,
BLINKER_F("/"), contentLength, BLINKER_F(". Retry?"));
}
if (BlinkerUpdater.end()) {
BLINKER_LOG(BLINKER_F("OTA done!"));
if (BlinkerUpdater.isFinished()) {
BLINKER_LOG(BLINKER_F("Update successfully completed. Rebooting."));
_status = BLINKER_UPGRADE_SUCCESS;
// ESP.restart();
return true;
} else {
BLINKER_LOG(BLINKER_F("Update not finished? Something went wrong!"));
_status = BLINKER_UPGRADE_FAIL;
return false;
}
}
else {
BLINKER_LOG(BLINKER_F("Error Occurred. Error #: "), BlinkerUpdater.getError());
_status = BLINKER_UPGRADE_FAIL;
// return false;
}
}
else {
// not enough space to begin OTA
// Understand the partitions and
// space availability
BLINKER_LOG(BLINKER_F("Not enough space to begin OTA"));
client_s.flush();
_status = BLINKER_UPGRADE_FAIL;
return false;
}
}
else {
BLINKER_LOG(BLINKER_F("There was no content in the response"));
client_s.flush();
_status = BLINKER_UPGRADE_FAIL;
return false;
}
}
}
// #endif
}
// bool BlinkerOTA::run()
// {
// while (1) {
// if (!client_s.connect(ota_host.c_str(), ota_port)) {
// BLINKER_ERR_LOG(BLINKER_F("server connection failed"));
// BLINKER_LOG_FreeHeap();
// ::delay(1000);
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("connection succeed"));
// BLINKER_LOG_FreeHeap();
// BLINKER_LOG_ALL(BLINKER_F("Fetching Bin: "), ota_url);
// client_s.print(String("GET ") + ota_url + " HTTP/1.1\r\n" +
// "Host: " + ota_host + "\r\n" +
// "Cache-Control: no-cache\r\n" +
// "Connection: close\r\n\r\n");
// unsigned long timeout = millis();
// while (client_s.available() == 0) {
// if (millis() - timeout > 5000) {
// BLINKER_LOG_ALL(BLINKER_F("Client Timeout !"));
// client_s.stop();
// _status = BLINKER_UPGRADE_LOAD_FAIL;
// return false;
// }
// }
// // Once the response is available,
// // check stuff
// while (client_s.available()) {
// // read line till /n
// String line = client_s.readStringUntil('\n');
// // remove space, to check if the line is end of headers
// line.trim();
// // if the the line is empty,
// // this is end of headers
// // break the while and feed the
// // remaining `client_s` to the
// // Update.writeStream();
// if (!line.length()) {
// //headers ended
// break; // and get the OTA started
// }
// // Check if the HTTP Response is 200
// // else break and Exit Update
// if (line.startsWith("HTTP/1.1")) {
// if (line.indexOf("200") < 0) {
// BLINKER_LOG_ALL(BLINKER_F("Got a non 200 status code from server. Exiting OTA Update."));
// break;
// }
// }
// // extract headers here
// // Start with content length
// if (line.startsWith("Content-Length: ")) {
// contentLength = atoi((getHeaderValue(line, "Content-Length: ")).c_str());
// BLINKER_LOG_ALL(BLINKER_F("Got "), contentLength, BLINKER_F(" bytes from server"));
// }
// // Next, the content type
// if (line.startsWith("Content-Type: ")) {
// String contentType = getHeaderValue(line, "Content-Type: ");
// BLINKER_LOG_ALL(BLINKER_F("Got "), contentType, BLINKER_F(" payload."));
// if (contentType == "application/octet-stream") {
// isValidContentType = true;
// }
// }
// }
// // Check what is the contentLength and if content type is `application/octet-stream`
// BLINKER_LOG_ALL(BLINKER_F("contentLength : "),
// contentLength,
// BLINKER_F(", isValidContentType : "),
// isValidContentType);
// // check contentLength and content type
// if (contentLength && isValidContentType)
// {
// // Check if there is enough to OTA Update
// bool canBegin = BlinkerUpdater.begin(contentLength);
// // If yes, begin
// if (canBegin) {
// BLINKER_LOG(BLINKER_F("Begin OTA. This may take 2 - 5 mins to complete. Things might be quite for a while.. Patience!"));
// // No activity would appear on the Serial monitor
// // So be patient. This may take 2 - 5mins to complete
// size_t written = BlinkerUpdater.writeStream(client_s);
// if (written == contentLength) {
// BLINKER_LOG(BLINKER_F("Written : "), written, BLINKER_F(" successfully"));
// }
// else {
// BLINKER_LOG(BLINKER_F("Written only : "), written,
// BLINKER_F("/"), contentLength, BLINKER_F(". Retry?"));
// }
// if (BlinkerUpdater.end()) {
// BLINKER_LOG(BLINKER_F("OTA done!"));
// if (BlinkerUpdater.isFinished()) {
// BLINKER_LOG(BLINKER_F("Update successfully completed. Rebooting."));
// _status = BLINKER_UPGRADE_SUCCESS;
// // ESP.restart();
// return true;
// } else {
// BLINKER_LOG(BLINKER_F("Update not finished? Something went wrong!"));
// _status = BLINKER_UPGRADE_FAIL;
// return false;
// }
// }
// else {
// BLINKER_LOG(BLINKER_F("Error Occurred. Error #: "), BlinkerUpdater.getError());
// _status = BLINKER_UPGRADE_FAIL;
// // return false;
// }
// }
// else {
// // not enough space to begin OTA
// // Understand the partitions and
// // space availability
// BLINKER_LOG(BLINKER_F("Not enough space to begin OTA"));
// client_s.flush();
// _status = BLINKER_UPGRADE_FAIL;
// return false;
// }
// }
// else {
// BLINKER_LOG(BLINKER_F("There was no content in the response"));
// client_s.flush();
// _status = BLINKER_UPGRADE_FAIL;
// return false;
// }
// }
// }
// }
uint8_t BlinkerOTA::loadOTACheck() {
// #if defined(ESP8266)
static uint8_t OTACheck;
// #endif
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.get(BLINKER_EEP_ADDR_OTA_CHECK, OTACheck);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA Check: "), OTACheck);
// BLINKER_LOG_ALL(BLINKER_F("BLINKER_EEP_ADDR_OTA_CHECK: "), BLINKER_EEP_ADDR_OTA_CHECK);
return OTACheck;
// if (OTACheck != BLINKER_OTA_START) {
// BLINKER_LOG_ALL(BLINKER_F("OTA NOT START"));
// return false;
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("OTA START"));
// return true;
// }
}
void BlinkerOTA::saveOTARun() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_CHECK, BLINKER_OTA_RUN);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA RUN: "), BLINKER_OTA_RUN);
}
void BlinkerOTA::saveOTACheck() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_CHECK, BLINKER_OTA_START);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA START: "), BLINKER_OTA_START);
}
void BlinkerOTA::clearOTACheck() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_CHECK, BLINKER_OTA_CLEAR);
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("OTA CLEAR: "), BLINKER_OTA_CLEAR);
_status = BLINKER_UPGRADE_DISABLE;
}
bool BlinkerOTA::loadVersion() {
// #if defined(BLINKER_PRO)
// uint32_t versionCheck;
// EEPROM.begin(BLINKER_EEP_SIZE);
// EEPROM.get(BLINKER_EEP_ADDR_OTA_INFO, versionCheck);//+BUNDLINGSIZE+isBundling
// EEPROM.commit();
// EEPROM.end();
// BLINKER_LOG_ALL(BLINKER_F("loadVersion: "), versionCheck);
// if (versionCheck != BLINKER_OTA_VERSION_CODE) {
// BLINKER_LOG_ALL(BLINKER_F("OTA SUCCESS BLINKER_OTA_VERSION_CODE NOT UPGRADE"));
// _status = BLINKER_UPGRADE_SUCCESS;
// return false;
// }
// else {
// BLINKER_LOG_ALL(BLINKER_F("OTA FAIL OR NOT START OTA"));
// _status = BLINKER_UPGRADE_FAIL;
// return true;
// }
// #else
char versionCheck[11];
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.get(BLINKER_EEP_ADDR_OTA_INFO, versionCheck);//+BUNDLINGSIZE+isBundling
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("loadVersion: "), versionCheck);
if (strcmp(versionCheck, BLINKER_OTA_VERSION_CODE)) {
BLINKER_LOG_ALL(BLINKER_F("OTA SUCCESS BLINKER_OTA_VERSION_CODE NOT UPGRADE"));
_status = BLINKER_UPGRADE_SUCCESS;
return false;
}
else {
BLINKER_LOG_ALL(BLINKER_F("OTA FAIL OR NOT START OTA"));
_status = BLINKER_UPGRADE_FAIL;
return true;
}
// #endif
}
void BlinkerOTA::saveVersion() {
EEPROM.begin(BLINKER_EEP_SIZE);
EEPROM.put(BLINKER_EEP_ADDR_OTA_INFO, BLINKER_OTA_VERSION_CODE);//+BUNDLINGSIZE+isBundling
EEPROM.commit();
EEPROM.end();
BLINKER_LOG_ALL(BLINKER_F("SAVE BLINKER_OTA_VERSION_CODE"));
}
#endif
#endif

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/coloz-blinker-library.git
git@gitlife.ru:oschina-mirror/coloz-blinker-library.git
oschina-mirror
coloz-blinker-library
coloz-blinker-library
master