@@ -19,6 +19,7 @@ extern rebootDelay_t rebootDelay;
1919extern Web webServer;
2020
2121
22+
2223#define MAX_BUFF_SIZE 4096
2324void GitRelease::setReleaseProperty (const char *key, const char *val) {
2425 if (strcmp (key, " id" ) == 0 ) this ->id = atol (val);
@@ -115,16 +116,16 @@ int16_t GitRepo::getReleases(uint8_t num) {
115116 strcpy (main->version .name , " main" );
116117 strcpy (main->name , " Main" );
117118 strcpy (main->hwVersions , " 32,s3" );
118- HTTPClient * https = new HTTPClient () ;
119- https-> setReuse (false );
120- if (https-> begin (sclient, url)) {
121- int httpCode = https-> GET ();
119+ HTTPClient https;
120+ https. setReuse (false );
121+ if (https. begin (sclient, url)) {
122+ int httpCode = https. GET ();
122123 Serial.printf (" [HTTPS] GET... code: %d\n " , httpCode);
123124 if (httpCode > 0 ) {
124- int len = https-> getSize ();
125+ int len = https. getSize ();
125126 Serial.printf (" [HTTPS] GET... code: %d - %d\n " , httpCode, len);
126127 if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
127- WiFiClient *stream = https-> getStreamPtr ();
128+ WiFiClient *stream = https. getStreamPtr ();
128129 uint8_t buff[128 ] = {0 };
129130 char jsonElem[32 ] = " " ;
130131 char jsonValue[128 ] = " " ;
@@ -135,7 +136,7 @@ int16_t GitRepo::getReleases(uint8_t num) {
135136 bool inValue = false ;
136137 bool awaitValue = false ;
137138 bool inAss = false ;
138- while (https-> connected () && (len > 0 || len == -1 ) && ndx < count) {
139+ while (https. connected () && (len > 0 || len == -1 ) && ndx < count) {
139140 size_t size = stream->available ();
140141 if (size) {
141142 int c = stream->readBytes (buff, ((size > sizeof (buff)) ? sizeof (buff) : size));
@@ -226,16 +227,14 @@ int16_t GitRepo::getReleases(uint8_t num) {
226227 }
227228 }
228229 else {
229- https->end ();
230- sclient.stop ();
231- delete https;
230+ https.end ();
231+ // sclient.stop();
232232 return httpCode;
233233 }
234234 }
235- https-> end ();
236- delete https ;
235+ https. end ();
236+ sclient. stop () ;
237237 }
238- sclient.stop ();
239238 settings.printAvailHeap ();
240239 return 0 ;
241240}
@@ -399,15 +398,15 @@ void GitUpdater::emitUpdateCheck(uint8_t num) {
399398int GitUpdater::checkInternet () {
400399 int err = 500 ;
401400 uint32_t t = millis ();
402- WiFiClientSecure client ;
403- client .setInsecure ();
404- client .setHandshakeTimeout (3 );
405- HTTPClient * https = new HTTPClient () ;
406- https-> setReuse (false );
407- if (https-> begin (client , " https://github.com/rstrouse/ESPSomfy-RTS" )) {
408- https-> setFollowRedirects (HTTPC_FORCE_FOLLOW_REDIRECTS);
409- https-> setTimeout (5000 );
410- int httpCode = https-> sendRequest (" HEAD" );
401+ WiFiClientSecure sclient ;
402+ sclient .setInsecure ();
403+ sclient .setHandshakeTimeout (3 );
404+ HTTPClient https;
405+ https. setReuse (false );
406+ if (https. begin (sclient , " https://github.com/rstrouse/ESPSomfy-RTS" )) {
407+ https. setFollowRedirects (HTTPC_FORCE_FOLLOW_REDIRECTS);
408+ https. setTimeout (5000 );
409+ int httpCode = https. sendRequest (" HEAD" );
411410 if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode == HTTP_CODE_FOUND) {
412411 err = 0 ;
413412 Serial.printf (" Internet is Available: %ldms\n " , millis () - t);
@@ -418,10 +417,9 @@ int GitUpdater::checkInternet() {
418417 Serial.printf (" Internet is Unavailable: %d: %ldms\n " , err, millis () - t);
419418 this ->inetAvailable = false ;
420419 }
421- https->end ();
420+ https.end ();
421+ sclient.stop ();
422422 }
423- client.stop ();
424- delete https;
425423 return err;
426424}
427425void GitUpdater::emitDownloadProgress (size_t total, size_t loaded, const char *evt) { this ->emitDownloadProgress (255 , total, loaded, evt); }
@@ -516,119 +514,114 @@ bool GitUpdater::recoverFilesystem() {
516514}
517515bool GitUpdater::endUpdate () { return true ; }
518516int8_t GitUpdater::downloadFile () {
519- WiFiClientSecure *client = new WiFiClientSecure;
520517 Serial.printf (" Begin update %s\n " , this ->currentFile );
521- if (client) {
522- client->setInsecure ();
523- HTTPClient https;
524- char url[196 ];
525- sprintf (url, " %s%s" , this ->baseUrl , this ->currentFile );
526- Serial.println (url);
527- if (https.begin (*client, url)) {
528- https.setFollowRedirects (HTTPC_FORCE_FOLLOW_REDIRECTS);
529- Serial.print (" [HTTPS] GET...\n " );
530- int httpCode = https.GET ();
531- if (httpCode > 0 ) {
532- size_t len = https.getSize ();
533- size_t total = 0 ;
534- uint8_t pct = 0 ;
535- Serial.printf (" [HTTPS] GET... code: %d - %d\n " , httpCode, len);
536- if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode == HTTP_CODE_FOUND) {
537- WiFiClient *stream = https.getStreamPtr ();
538- if (!Update.begin (len, this ->partition )) {
539- Serial.println (" Update Error detected!!!!!" );
540- Update.printError (Serial);
541- https.end ();
542- return -(Update.getError () + UPDATE_ERR_OFFSET);
543- }
544- uint8_t *buff = (uint8_t *)malloc (MAX_BUFF_SIZE);
545- if (buff) {
546- this ->emitDownloadProgress (len, total);
547- int timeouts = 0 ;
548- while (https.connected () && (len > 0 || len == -1 ) && total < len) {
549- size_t size = stream->available ();
550- if (size) {
551- if (this ->cancelled && !this ->lockFS ) {
552- Update.abort ();
553- https.end ();
554- free (buff);
555- return -(Update.getError () + UPDATE_ERR_OFFSET);
556- }
557- int c = stream->readBytes (buff, ((size > MAX_BUFF_SIZE) ? MAX_BUFF_SIZE : size));
558- total += c;
559- // Serial.println(total);
560- if (Update.write (buff, c) != c) {
518+ WiFiClientSecure sclient;
519+ sclient.setInsecure ();
520+ HTTPClient https;
521+ char url[196 ];
522+ sprintf (url, " %s%s" , this ->baseUrl , this ->currentFile );
523+ Serial.println (url);
524+ if (https.begin (sclient, url)) {
525+ https.setFollowRedirects (HTTPC_FORCE_FOLLOW_REDIRECTS);
526+ Serial.print (" [HTTPS] GET...\n " );
527+ int httpCode = https.GET ();
528+ if (httpCode > 0 ) {
529+ size_t len = https.getSize ();
530+ size_t total = 0 ;
531+ uint8_t pct = 0 ;
532+ Serial.printf (" [HTTPS] GET... code: %d - %d\n " , httpCode, len);
533+ if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode == HTTP_CODE_FOUND) {
534+ WiFiClient *stream = https.getStreamPtr ();
535+ if (!Update.begin (len, this ->partition )) {
536+ Serial.println (" Update Error detected!!!!!" );
537+ Update.printError (Serial);
538+ https.end ();
539+ return -(Update.getError () + UPDATE_ERR_OFFSET);
540+ }
541+ uint8_t *buff = (uint8_t *)malloc (MAX_BUFF_SIZE);
542+ if (buff) {
543+ this ->emitDownloadProgress (len, total);
544+ int timeouts = 0 ;
545+ while (https.connected () && (len > 0 || len == -1 ) && total < len) {
546+ size_t size = stream->available ();
547+ if (size) {
548+ if (this ->cancelled && !this ->lockFS ) {
549+ Update.abort ();
550+ free (buff);
551+ https.end ();
552+ return -(Update.getError () + UPDATE_ERR_OFFSET);
553+ }
554+ int c = stream->readBytes (buff, ((size > MAX_BUFF_SIZE) ? MAX_BUFF_SIZE : size));
555+ total += c;
556+ // Serial.println(total);
557+ if (Update.write (buff, c) != c) {
558+ Update.printError (Serial);
559+ Serial.printf (" Upload of %s aborted invalid size %d\n " , url, c);
560+ free (buff);
561+ https.end ();
562+ sclient.stop ();
563+ return -(Update.getError () + UPDATE_ERR_OFFSET);
564+ }
565+ // Calculate the percentage.
566+ uint8_t p = (uint8_t )floor (((float )total / (float )len) * 100 .0f );
567+ if (p != pct) {
568+ pct = p;
569+ Serial.printf (" LEN:%d TOTAL:%d %d%%\n " , len, total, pct);
570+ this ->emitDownloadProgress (len, total);
571+ }
572+ delay (1 );
573+ if (total >= len) {
574+ if (!Update.end (true )) {
575+ Serial.println (" Error downloading update..." );
561576 Update.printError (Serial);
562- Serial.printf (" Upload of %s aborted invalid size %d\n " , url, c);
563- free (buff);
564- https.end ();
565- return -(Update.getError () + UPDATE_ERR_OFFSET);
566- }
567- // Calculate the percentage.
568- uint8_t p = (uint8_t )floor (((float )total / (float )len) * 100 .0f );
569- if (p != pct) {
570- pct = p;
571- Serial.printf (" LEN:%d TOTAL:%d %d%%\n " , len, total, pct);
572- this ->emitDownloadProgress (len, total);
573577 }
574- delay (1 );
575- if (total >= len) {
576- if (!Update.end (true )) {
577- Serial.println (" Error downloading update..." );
578- Update.printError (Serial);
579- }
580- else {
581- Serial.println (" Update.end Called..." );
582- }
583- https.end ();
584- }
585- }
586- else {
587- timeouts++;
588- if (timeouts >= 500 ) {
589- Update.abort ();
590- https.end ();
591- free (buff);
592- Serial.println (" Stream timeout!!!" );
593- return -43 ;
578+ else {
579+ Serial.println (" Update.end Called..." );
594580 }
595- sockEmit.loop ();
596- webServer.loop ();
597- delay (100 );
581+ https.end ();
582+ sclient.stop ();
598583 }
599584 }
600- free (buff);
601- if (len > total) {
602- Update.abort ();
603- somfy.commit ();
604- Serial.println (" Error downloading file!!!" );
605- return -42 ;
606-
585+ else {
586+ timeouts++;
587+ if (timeouts >= 500 ) {
588+ Update.abort ();
589+ https.end ();
590+ free (buff);
591+ Serial.println (" Stream timeout!!!" );
592+ return -43 ;
593+ }
594+ sockEmit.loop ();
595+ webServer.loop ();
596+ delay (100 );
607597 }
608- else
609- Serial.printf (" Update %s complete\n " , this ->currentFile );
610-
611598 }
612- else {
613- // TODO: memory allocation error.
614- Serial.println (" Unable to allocate memory for update!!!" );
599+ free (buff);
600+ if (len > total) {
601+ Update.abort ();
602+ somfy.commit ();
603+ Serial.println (" Error downloading file!!!" );
604+ return -42 ;
615605 }
606+ else
607+ Serial.printf (" Update %s complete\n " , this ->currentFile );
616608 }
617609 else {
618- Serial. printf ( " Invalid HTTP Code... %d " , httpCode);
619- return httpCode ;
610+ // TODO: memory allocation error.
611+ Serial. println ( " Unable to allocate memory for update!!! " ) ;
620612 }
621- }
613+ }
622614 else {
623- Serial.printf (" Invalid HTTP Code: %d\n " , httpCode);
615+ Serial.printf (" Invalid HTTP Code... %d" , httpCode);
616+ return httpCode;
624617 }
625-
626- if (https.connected ()) https.end ();
627- Serial.printf (" End update %s\n " , this ->currentFile );
628-
618+ }
619+ else {
620+ Serial.printf (" Invalid HTTP Code: %d\n " , httpCode);
629621 }
630- client->stop ();
631- delete client;
622+ https.end ();
623+ sclient.stop ();
624+ Serial.printf (" End update %s\n " , this ->currentFile );
632625 }
633626 return 0 ;
634627}
0 commit comments