add update version check

This commit is contained in:
sharandac
2020-07-28 10:42:22 +02:00
parent 9766af8197
commit 7873331c59
12 changed files with 199 additions and 24 deletions

View File

@@ -26,6 +26,8 @@
#include <HTTPUpdate.h>
#include "update.h"
#include "update_check_version.h"
#include "gui/mainbar/mainbar.h"
#include "gui/statusbar.h"
#include "hardware/display.h"
@@ -97,7 +99,12 @@ void update_tile_setup( lv_obj_t *tile, lv_style_t *style, lv_coord_t hres, lv_c
lv_obj_add_style( update_status_label, LV_OBJ_PART_MAIN, style );
lv_label_set_text( update_status_label, "" );
lv_obj_align( update_status_label, update_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 5 );
// regster callback
WiFi.onEvent( [](WiFiEvent_t event, WiFiEventInfo_t info) {
update_check_version();
}, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP );
update_event_handle = xEventGroupCreate();
xEventGroupClearBits( update_event_handle, UPDATE_REQUEST );
@@ -131,9 +138,26 @@ static void update_event_handler(lv_obj_t * obj, lv_event_t event) {
}
}
void update_check_version( void ) {
if ( xEventGroupGetBits( update_event_handle ) & UPDATE_GET_VERSION_REQUEST ) {
return;
}
else {
xEventGroupSetBits( update_event_handle, UPDATE_GET_VERSION_REQUEST );
vTaskResume( _update_Task );
}
}
void update_Task( void * pvParameters ) {
while( true ) {
vTaskDelay( 500 );
if ( xEventGroupGetBits( update_event_handle) & UPDATE_GET_VERSION_REQUEST ) {
if ( update_check_new_version() > atol( __FIRMWARE__ ) ) {
lv_label_set_text( update_status_label, "new version available" );
lv_obj_align( update_status_label, update_btn, LV_ALIGN_OUT_BOTTOM_MID, 0, 15 );
}
xEventGroupClearBits( update_event_handle, UPDATE_GET_VERSION_REQUEST );
}
if ( xEventGroupGetBits( update_event_handle) & UPDATE_REQUEST ) {
if( WiFi.status() == WL_CONNECTED ) {

View File

@@ -24,11 +24,13 @@
#include <TTGO.h>
#define UPDATE_REQUEST _BV(0)
#define UPDATE_REQUEST _BV(0)
#define UPDATE_GET_VERSION_REQUEST _BV(1)
#define FIRMWARE_LOCATION "https://github.com/sharandac/My-TTGO-Watch/blob/master/ttgo-t-watch2020_v1.ino.bin"
void update_tile_setup( lv_obj_t *tile, lv_style_t *style, lv_coord_t hres, lv_coord_t vres );
void update_check_version( void );
void update_update_firmware( void );
#endif // _UPDATE_H

View File

@@ -0,0 +1,101 @@
/****************************************************************************
* July 28 00:23:05 2020
* Copyright 2020 Dirk Brosswick
* Email: dirk.brosswick@googlemail.com
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <Arduino.h>
#include <WiFi.h>
#include "ArduinoJson.h"
#include "HTTPClient.h"
#include "update_check_version.h"
uint64_t update_check_new_version( void ) {
WiFiClient check_version_client;
uint64_t retval = -1;
if ( !check_version_client.connect( FIRMWARE_HOST, FIRMWARE_HOST_PORT ) ) {
Serial.printf("connection failed\r\n");
return( -1 );
}
check_version_client.printf( "GET /ttgo-t-watch2020_v1.version.json HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: close\r\n"
"Pragma: no-cache\r\n"
"Cache-Control: no-cache\r\n"
"User-Agent: ESP32\r\n"
"Accept: text/html,application/json\r\n\r\n", FIRMWARE_HOST );
uint64_t startMillis = millis();
while ( check_version_client.available() == 0 ) {
if ( millis() - startMillis > 5000 ) {
Serial.printf("connection timeout\r\n");
check_version_client.stop();
return( retval );
}
}
char *json = (char *)ps_malloc( 100 );
if ( json == NULL ) {
Serial.printf("memory alloc failed\r\n");
check_version_client.stop();
return( retval );
}
char *ptr = json;
bool data_begin = false;
while( check_version_client.available() ) {
if ( data_begin ) {
ptr[ check_version_client.readBytes( ptr, 100 - 1 ) ] = '\0';
}
else if ( check_version_client.read() == '{' ) {
data_begin = true;
*ptr = '{';
ptr++;
}
}
check_version_client.stop();
if ( data_begin == false ) {
free( json );
return( retval );
}
check_version_client.stop();
DynamicJsonDocument doc(200);
DeserializationError error = deserializeJson( doc, json);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
doc.clear();
free( json );
return( retval );
}
retval = atoll( doc["version"] );
doc.clear();
free( json );
return( retval );
}

View File

@@ -0,0 +1,32 @@
/****************************************************************************
* July 28 00:23:05 2020
* Copyright 2020 Dirk Brosswick
* Email: dirk.brosswick@googlemail.com
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _UPDATE_CHECK_VERSION_H
#define _UPDATE_CHECK_VERSION_H
#include <TTGO.h>
#define FIRMWARE_HOST "www.neo-guerillaz.de"
#define FIRMWARE_HOST_PORT 80
uint64_t update_check_new_version();
#endif // _UPDATE_CHECK_VERSION_H