Skip to content

Commit

Permalink
move partition version code to GetOVMSPartitionVersion() in ovms_vers…
Browse files Browse the repository at this point in the history
…ion.cpp

list partition versions from ota_status()
  • Loading branch information
leres committed Nov 14, 2018
1 parent 39a32b0 commit c6ea721
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 92 deletions.
15 changes: 13 additions & 2 deletions vehicle/OVMS.V3/components/ovms_ota/src/ovms_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static const char *TAG = "ota";
#include "ovms_buffer.h"
#include "ovms_boot.h"
#include "ovms_netmanager.h"
#include "ovms_version.h"
#include "crypt_md5.h"

OvmsOTA MyOTA __attribute__ ((init_priority (4400)));
Expand Down Expand Up @@ -83,16 +84,26 @@ int buildverscmp(std::string v1, std::string v2)
void ota_status(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
ota_info info;
std::string version;
int len = 0;
bool check_update = (strcmp(cmd->GetName(), "status")==0);
MyOTA.GetStatus(info, check_update);

if (info.version_firmware != "")
len += writer->printf("Firmware: %s\n", info.version_firmware.c_str());
if (info.partition_running != "")
len += writer->printf("Running partition: %s\n", info.partition_running.c_str());
if (info.partition_boot != "")
len += writer->printf("Boot partition: %s\n", info.partition_boot.c_str());
if (info.version_firmware != "")
len += writer->printf("Firmware: %s\n", info.version_firmware.c_str());
version = GetOVMSPartitionVersion(ESP_PARTITION_SUBTYPE_APP_FACTORY);
if (version != "")
len += writer->printf("Factory image: %s\n", version.c_str());
version = GetOVMSPartitionVersion(ESP_PARTITION_SUBTYPE_APP_OTA_0);
if (version != "")
len += writer->printf("OTA_O image: %s\n", version.c_str());
version = GetOVMSPartitionVersion(ESP_PARTITION_SUBTYPE_APP_OTA_1);
if (version != "")
len += writer->printf("OTA_1 image: %s\n", version.c_str());
if (info.version_server != "")
{
len += writer->printf("Server Available: %s%s\n", info.version_server.c_str(),
Expand Down
113 changes: 24 additions & 89 deletions vehicle/OVMS.V3/components/ovms_webserver/src/web_cfg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
#include "vehicle.h"
#include "ovms_housekeeping.h"
#include "ovms_peripherals.h"
#include "esp_image_format.h" /* ESP_IMAGE_HEADER_MAGIC */
#include "ovms_version.h" /* OVMS_VERSION_PREFIX */
#include "ovms_version.h"

#ifdef CONFIG_OVMS_COMP_OTA
#include "ovms_ota.h"
Expand Down Expand Up @@ -1406,87 +1405,6 @@ void OvmsWebServer::HandleCfgAutoInit(PageEntry_t& p, PageContext_t& c)
c.done();
}

#ifdef CONFIG_OVMS_COMP_OTA
/* Add the version detail if possible */
static void partition_detail(esp_partition_subtype_t t, const char *what, char *detail, size_t size)
{
int i, len;
size_t offset;
size_t n, size2;
char *cp, *cp2, *buf2;
const esp_partition_t *p;
esp_image_header_t *pfhdr;
esp_image_segment_header_t *header;
char buf[512 + 1];
const char prefix[] = OVMS_VERSION_PREFIX;
const char postfix[] = OVMS_VERSION_POSTFIX;

/* Set the default detail so we can just return if we fail */
strlcpy(detail, what, size);

/* Find the partition detail */
p = esp_partition_find_first(ESP_PARTITION_TYPE_APP, t, NULL);
if (p == NULL)
return;

/* Read image header */
pfhdr = (esp_image_header_t *)buf;
offset = 0;
if (esp_partition_read(p, offset, pfhdr, sizeof(*pfhdr)) != ESP_OK)
return;
if (pfhdr->magic != ESP_IMAGE_HEADER_MAGIC)
return;
if (pfhdr->segment_count > ESP_IMAGE_MAX_SEGMENTS)
return;

/* Read the first segment header */
header = (esp_image_segment_header_t *)buf;
offset += sizeof(*pfhdr);
if (esp_partition_read(p, offset, header, sizeof(*header)) != ESP_OK)
return;

/* Search for the version string */
offset += sizeof(*header);
len = header->data_len;
size2 = sizeof(buf) - 1;
buf2 = buf;
while (len > 0) {
n = size2;
if (n > len)
n = len;
if (esp_partition_read(p, offset, buf2, n) != ESP_OK)
return;
offset += n;

/* Insure EOS */
buf2[n] = '\0';
len -= n;

n = sizeof(buf) - (sizeof(prefix) + sizeof(postfix) - 2);
for (i = 0, cp = buf; i < n; ++i, ++cp) {
if (*cp != prefix[0])
continue;
if (strncmp(cp, prefix, sizeof(prefix) - 1) != 0)
continue;
cp2 = strstr(cp, postfix);
if (cp2 == NULL)
continue;
*cp2 = '\0';
cp += sizeof(prefix) - 1;
snprintf(detail, size, "%s (%s)", what, cp);
*cp2 = '\0';
len = 0;
return;
}

/* Shift the buffer left to avoid splitting the string */
n = (sizeof(buf) - 1) / 2;
buf2 = buf + n;
memcpy(buf, buf + n, n);
size2 = n;
}
}
#endif

#ifdef CONFIG_OVMS_COMP_OTA
/**
Expand All @@ -1500,6 +1418,8 @@ void OvmsWebServer::HandleCfgFirmware(PageEntry_t& p, PageContext_t& c)
bool auto_enable;
std::string auto_hour, server, tag;
std::string output;
std::string version;
const char *what;
char buf[132];

if (c.method == "POST") {
Expand Down Expand Up @@ -1603,12 +1523,27 @@ void OvmsWebServer::HandleCfgFirmware(PageEntry_t& p, PageContext_t& c)
c.input_info("Running partition", info.partition_running.c_str());
c.printf("<input type=\"hidden\" name=\"boot_old\" value=\"%s\">", _attr(info.partition_boot));
c.input_select_start("Boot from", "boot");
partition_detail(ESP_PARTITION_SUBTYPE_APP_FACTORY, "Factory image", buf, sizeof(buf));
c.input_select_option(buf, "factory", (info.partition_boot == "factory"));
partition_detail(ESP_PARTITION_SUBTYPE_APP_OTA_0, "OTA_0 image", buf, sizeof(buf));
c.input_select_option(buf, "ota_0", (info.partition_boot == "ota_0"));
partition_detail(ESP_PARTITION_SUBTYPE_APP_OTA_1, "OTA_1 image", buf, sizeof(buf));
c.input_select_option(buf, "ota_1", (info.partition_boot == "ota_1"));
what = "Factory image";
version = GetOVMSPartitionVersion(ESP_PARTITION_SUBTYPE_APP_FACTORY);
if (version != "") {
snprintf(buf, sizeof(buf), "%s (%s)", what, version.c_str());
what = buf;
}
c.input_select_option(what, "factory", (info.partition_boot == "factory"));
what = "OTA_0 image";
version = GetOVMSPartitionVersion(ESP_PARTITION_SUBTYPE_APP_OTA_0);
if (version != "") {
snprintf(buf, sizeof(buf), "%s (%s)", what, version.c_str());
what = buf;
}
c.input_select_option(what, "ota_0", (info.partition_boot == "ota_0"));
what = "OTA_1 image";
version = GetOVMSPartitionVersion(ESP_PARTITION_SUBTYPE_APP_OTA_1);
if (version != "") {
snprintf(buf, sizeof(buf), "%s (%s)", what, version.c_str());
what = buf;
}
c.input_select_option(what, "ota_1", (info.partition_boot == "ota_1"));
c.input_select_end();

// Server & auto update:
Expand Down
81 changes: 80 additions & 1 deletion vehicle/OVMS.V3/main/ovms_version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,94 @@
#include "ovms_log.h"
static const char *TAG = "version";

#include <esp_system.h>
#include <esp_image_format.h>
#include <esp_ota_ops.h>
#include <esp_system.h>
#include "ovms.h"
#include "ovms_version.h"
#include "ovms_config.h"
#include "ovms_metrics.h"
#include "metrics_standard.h"
#include "ovms_events.h"


std::string GetOVMSPartitionVersion(esp_partition_subtype_t t)
{
int i, len;
size_t offset;
size_t n, size;
char *cp, *cp2, *buf2;
std::string version;
const esp_partition_t *p;
esp_image_header_t *pfhdr;
esp_image_segment_header_t *header;
char buf[512 + 1];
const char prefix[] = OVMS_VERSION_PREFIX;
const char postfix[] = OVMS_VERSION_POSTFIX;

/* Find the partition detail */
p = esp_partition_find_first(ESP_PARTITION_TYPE_APP, t, NULL);
if (p == NULL)
return "";

/* Read image header */
pfhdr = (esp_image_header_t *)buf;
offset = 0;
if (esp_partition_read(p, offset, pfhdr, sizeof(*pfhdr)) != ESP_OK)
return "";
if (pfhdr->magic != ESP_IMAGE_HEADER_MAGIC)
return "";
if (pfhdr->segment_count > ESP_IMAGE_MAX_SEGMENTS)
return "";

/* Read the first segment header */
header = (esp_image_segment_header_t *)buf;
offset += sizeof(*pfhdr);
if (esp_partition_read(p, offset, header, sizeof(*header)) != ESP_OK)
return "";

/* Search for the version string */
offset += sizeof(*header);
len = header->data_len;
size = sizeof(buf) - 1;
buf2 = buf;
while (len > 0) {
n = size;
if (n > len)
n = len;
if (esp_partition_read(p, offset, buf2, n) != ESP_OK)
return "";
offset += n;

/* Insure EOS */
buf2[n] = '\0';
len -= n;

n = sizeof(buf) - (sizeof(prefix) + sizeof(postfix) - 2);
for (i = 0, cp = buf; i < n; ++i, ++cp) {
if (*cp != prefix[0])
continue;
if (strncmp(cp, prefix, sizeof(prefix) - 1) != 0)
continue;
cp2 = strstr(cp, postfix);
if (cp2 == NULL)
continue;
*cp2 = '\0';
cp += sizeof(prefix) - 1;
*cp2 = '\0';
version.assign(cp);
return version;
}

/* Shift the buffer left to avoid splitting the string */
n = (sizeof(buf) - 1) / 2;
buf2 = buf + n;
memcpy(buf, buf2, n);
size = n;
}
return "";
}

std::string GetOVMSVersion()
{
std::string searchversion(OVMS_VERSION_PREFIX OVMS_VERSION OVMS_VERSION_POSTFIX);
Expand Down
3 changes: 3 additions & 0 deletions vehicle/OVMS.V3/main/ovms_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@

#include <string>

#include <esp_partition.h>

#define OVMS_VERSION_PREFIX "########OVMS_PRE########"
#define OVMS_VERSION_POSTFIX "########OVMSPOST########"

extern std::string GetOVMSVersion();
extern std::string GetOVMSBuild();
extern std::string GetOVMSHardware();
extern std::string GetOVMSPartitionVersion(esp_partition_subtype_t);

#endif //#ifndef __VFS_H__

0 comments on commit c6ea721

Please sign in to comment.