|
@@ -21,6 +21,7 @@
|
|
|
#include "esp_chip_info.h"
|
|
#include "esp_chip_info.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <stdio.h>
|
|
|
|
|
+#include <cstring>
|
|
|
|
|
|
|
|
#include "Helper.h"
|
|
#include "Helper.h"
|
|
|
|
|
|
|
@@ -29,47 +30,209 @@ std::string starttime = "";
|
|
|
|
|
|
|
|
static const char *TAG = "MAIN SERVER";
|
|
static const char *TAG = "MAIN SERVER";
|
|
|
|
|
|
|
|
-static void appendJsonStringField(std::string &json, const char *key,
|
|
|
|
|
- const std::string &value, bool &firstField)
|
|
|
|
|
|
|
+// Writes JSON in small chunks so a complete response buffer is never allocated.
|
|
|
|
|
+class JsonChunkWriter
|
|
|
{
|
|
{
|
|
|
- if (!firstField)
|
|
|
|
|
|
|
+public:
|
|
|
|
|
+ explicit JsonChunkWriter(httpd_req_t *request)
|
|
|
|
|
+ : req(request), used(0), error(ESP_OK)
|
|
|
{
|
|
{
|
|
|
- json.push_back(',');
|
|
|
|
|
}
|
|
}
|
|
|
- firstField = false;
|
|
|
|
|
|
|
|
|
|
- json.push_back('"');
|
|
|
|
|
- json.append(key);
|
|
|
|
|
- json.append("\":\"");
|
|
|
|
|
|
|
+ void appendLiteral(const char *text)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (error != ESP_OK || text == nullptr)
|
|
|
|
|
+ {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- for (unsigned char ch : value)
|
|
|
|
|
|
|
+ appendBytes(text, strlen(text));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void appendStringField(const char *key, const char *value, bool &firstField)
|
|
|
{
|
|
{
|
|
|
- switch (ch)
|
|
|
|
|
|
|
+ appendFieldPrefix(key, firstField);
|
|
|
|
|
+ appendLiteral("\"");
|
|
|
|
|
+ appendEscapedString(value);
|
|
|
|
|
+ appendLiteral("\"");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void appendNumberField(const char *key, unsigned long long value, bool &firstField)
|
|
|
|
|
+ {
|
|
|
|
|
+ appendFieldPrefix(key, firstField);
|
|
|
|
|
+
|
|
|
|
|
+ char number[24];
|
|
|
|
|
+ const int length = snprintf(number, sizeof(number), "%llu", value);
|
|
|
|
|
+ if (length > 0 && static_cast<size_t>(length) < sizeof(number))
|
|
|
{
|
|
{
|
|
|
- case '"': json.append("\\\""); break;
|
|
|
|
|
- case '\\': json.append("\\\\"); break;
|
|
|
|
|
- case '\b': json.append("\\b"); break;
|
|
|
|
|
- case '\f': json.append("\\f"); break;
|
|
|
|
|
- case '\n': json.append("\\n"); break;
|
|
|
|
|
- case '\r': json.append("\\r"); break;
|
|
|
|
|
- case '\t': json.append("\\t"); break;
|
|
|
|
|
- default:
|
|
|
|
|
- if (ch < 0x20)
|
|
|
|
|
|
|
+ appendBytes(number, static_cast<size_t>(length));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void appendNullField(const char *key, bool &firstField)
|
|
|
|
|
+ {
|
|
|
|
|
+ appendFieldPrefix(key, firstField);
|
|
|
|
|
+ appendLiteral("null");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ esp_err_t finish()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (error != ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ return error;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ flush();
|
|
|
|
|
+ if (error != ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ return error;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return httpd_resp_send_chunk(req, nullptr, 0);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+private:
|
|
|
|
|
+ static constexpr size_t BUFFER_SIZE = 128;
|
|
|
|
|
+
|
|
|
|
|
+ httpd_req_t *req;
|
|
|
|
|
+ char buffer[BUFFER_SIZE];
|
|
|
|
|
+ size_t used;
|
|
|
|
|
+ esp_err_t error;
|
|
|
|
|
+
|
|
|
|
|
+ void flush()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (error != ESP_OK || used == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ error = httpd_resp_send_chunk(req, buffer, used);
|
|
|
|
|
+ used = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void appendBytes(const char *data, size_t length)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (error != ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ while (length > 0 && error == ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ const size_t available = BUFFER_SIZE - used;
|
|
|
|
|
+ const size_t copyLength = (length < available) ? length : available;
|
|
|
|
|
+
|
|
|
|
|
+ memcpy(buffer + used, data, copyLength);
|
|
|
|
|
+ used += copyLength;
|
|
|
|
|
+ data += copyLength;
|
|
|
|
|
+ length -= copyLength;
|
|
|
|
|
+
|
|
|
|
|
+ if (used == BUFFER_SIZE)
|
|
|
{
|
|
{
|
|
|
- char escaped[7];
|
|
|
|
|
- snprintf(escaped, sizeof(escaped), "\\u%04x", ch);
|
|
|
|
|
- json.append(escaped);
|
|
|
|
|
|
|
+ flush();
|
|
|
}
|
|
}
|
|
|
- else
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void appendEscapedString(const char *value)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (error != ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (value == nullptr)
|
|
|
|
|
+ {
|
|
|
|
|
+ value = "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (const unsigned char *cursor =
|
|
|
|
|
+ reinterpret_cast<const unsigned char *>(value);
|
|
|
|
|
+ *cursor != '\0' && error == ESP_OK;
|
|
|
|
|
+ ++cursor)
|
|
|
|
|
+ {
|
|
|
|
|
+ char escaped[6];
|
|
|
|
|
+ size_t escapedLength = 1;
|
|
|
|
|
+
|
|
|
|
|
+ switch (*cursor)
|
|
|
{
|
|
{
|
|
|
- json.push_back(static_cast<char>(ch));
|
|
|
|
|
|
|
+ case '"':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = '"';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '\\':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = '\\';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '\b':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = 'b';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '\f':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = 'f';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '\n':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = 'n';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '\r':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = 'r';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ case '\t':
|
|
|
|
|
+ escaped[0] = '\\';
|
|
|
|
|
+ escaped[1] = 't';
|
|
|
|
|
+ escapedLength = 2;
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ if (*cursor < 0x20)
|
|
|
|
|
+ {
|
|
|
|
|
+ const int length = snprintf(
|
|
|
|
|
+ escaped, sizeof(escaped), "\\u%04x", *cursor);
|
|
|
|
|
+ if (length != 6)
|
|
|
|
|
+ {
|
|
|
|
|
+ error = ESP_FAIL;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ escapedLength = 6;
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ escaped[0] = static_cast<char>(*cursor);
|
|
|
|
|
+ }
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
- break;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ appendBytes(escaped, escapedLength);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- json.push_back('"');
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ void appendFieldPrefix(const char *key, bool &firstField)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (error != ESP_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!firstField)
|
|
|
|
|
+ {
|
|
|
|
|
+ appendLiteral(",");
|
|
|
|
|
+ }
|
|
|
|
|
+ firstField = false;
|
|
|
|
|
+
|
|
|
|
|
+ appendLiteral("\"");
|
|
|
|
|
+ appendEscapedString(key);
|
|
|
|
|
+ appendLiteral("\":");
|
|
|
|
|
+ }
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
/* An HTTP GET handler */
|
|
/* An HTTP GET handler */
|
|
@@ -419,8 +582,7 @@ esp_err_t img_tmp_virtual_handler(httpd_req_t *req)
|
|
|
|
|
|
|
|
esp_err_t sysinfo_handler(httpd_req_t *req)
|
|
esp_err_t sysinfo_handler(httpd_req_t *req)
|
|
|
{
|
|
{
|
|
|
- // Collect each group once. In particular, SD partition statistics share one f_getfree()
|
|
|
|
|
- // call instead of performing three separate FATFS queries.
|
|
|
|
|
|
|
+ // Take one snapshot of resource and card information for this response.
|
|
|
const ESPHeapStats heap = getESPHeapStats();
|
|
const ESPHeapStats heap = getESPHeapStats();
|
|
|
const SDCardDetails sdCard = getSDCardDetails();
|
|
const SDCardDetails sdCard = getSDCardDetails();
|
|
|
|
|
|
|
@@ -434,56 +596,83 @@ esp_err_t sysinfo_handler(httpd_req_t *req)
|
|
|
const std::string hostname = hostnameValue ? *hostnameValue : std::string();
|
|
const std::string hostname = hostnameValue ? *hostnameValue : std::string();
|
|
|
const std::string ipAddress = ipAddressValue ? *ipAddressValue : std::string();
|
|
const std::string ipAddress = ipAddressValue ? *ipAddressValue : std::string();
|
|
|
const std::string ssid = ssidValue ? *ssidValue : std::string();
|
|
const std::string ssid = ssidValue ? *ssidValue : std::string();
|
|
|
|
|
+ const std::string uptime = getFormatedUptime(false);
|
|
|
|
|
|
|
|
- std::string json;
|
|
|
|
|
- json.reserve(1400);
|
|
|
|
|
- json.append("[{");
|
|
|
|
|
- bool firstField = true;
|
|
|
|
|
|
|
+ char cpuTemperature[16];
|
|
|
|
|
+ snprintf(cpuTemperature, sizeof(cpuTemperature), "%d", (int)temperatureRead());
|
|
|
|
|
|
|
|
- // Keep the original /sysinfo keys and array/object envelope for existing clients.
|
|
|
|
|
- appendJsonStringField(json, "firmware", libfive_git_version(), firstField);
|
|
|
|
|
- appendJsonStringField(json, "buildtime", build_time(), firstField);
|
|
|
|
|
- appendJsonStringField(json, "gitbranch", libfive_git_branch(), firstField);
|
|
|
|
|
- appendJsonStringField(json, "gittag", libfive_git_version(), firstField);
|
|
|
|
|
- appendJsonStringField(json, "gitrevision", libfive_git_revision(), firstField);
|
|
|
|
|
- appendJsonStringField(json, "html", getHTMLversion(), firstField);
|
|
|
|
|
- appendJsonStringField(json, "cputemp", std::to_string((int)temperatureRead()), firstField);
|
|
|
|
|
- appendJsonStringField(json, "hostname", hostname, firstField);
|
|
|
|
|
- appendJsonStringField(json, "IPv4", ipAddress, firstField);
|
|
|
|
|
- appendJsonStringField(json, "freeHeapMem", std::to_string((unsigned long long)heap.totalFree), firstField);
|
|
|
|
|
-
|
|
|
|
|
- // Additional fields used by info.html.
|
|
|
|
|
- appendJsonStringField(json, "starttime", starttime, firstField);
|
|
|
|
|
- appendJsonStringField(json, "uptime", getFormatedUptime(false), firstField);
|
|
|
|
|
- appendJsonStringField(json, "SSID", ssid, firstField);
|
|
|
|
|
-
|
|
|
|
|
- appendJsonStringField(json, "chipCores", std::to_string(chipInfo.cores), firstField);
|
|
|
|
|
- appendJsonStringField(json, "chipRevision", std::to_string(chipInfo.revision), firstField);
|
|
|
|
|
- appendJsonStringField(json, "chipFeatures", std::to_string(chipInfo.features), firstField);
|
|
|
|
|
-
|
|
|
|
|
- appendJsonStringField(json, "sdCardManufacturer", sdCard.manufacturer, firstField);
|
|
|
|
|
- appendJsonStringField(json, "sdCardName", sdCard.name, firstField);
|
|
|
|
|
- appendJsonStringField(json, "sdCardCapacity", sdCard.capacityMB, firstField);
|
|
|
|
|
- appendJsonStringField(json, "sdCardSectorSize", sdCard.sectorSizeBytes, firstField);
|
|
|
|
|
- appendJsonStringField(json, "sdCardPartitionSize", sdCard.partitionSizeMB, firstField);
|
|
|
|
|
- appendJsonStringField(json, "sdCardFreePartitionSpace", sdCard.partitionFreeMB, firstField);
|
|
|
|
|
- appendJsonStringField(json, "sdCardPartitionAllocationSize", sdCard.partitionAllocationSizeBytes, firstField);
|
|
|
|
|
-
|
|
|
|
|
- appendJsonStringField(json, "RAMTotalFree", std::to_string((unsigned long long)heap.totalFree), firstField);
|
|
|
|
|
- appendJsonStringField(json, "ExtRAMFree", std::to_string((unsigned long long)heap.externalFree), firstField);
|
|
|
|
|
- appendJsonStringField(json, "ExtRAMLargestFree", std::to_string((unsigned long long)heap.externalLargestFreeBlock), firstField);
|
|
|
|
|
- appendJsonStringField(json, "ExtRAMMinFree", std::to_string((unsigned long long)heap.externalMinFree), firstField);
|
|
|
|
|
- appendJsonStringField(json, "IntRAMFree", std::to_string((unsigned long long)heap.internalFree), firstField);
|
|
|
|
|
- appendJsonStringField(json, "IntRAMLargestFree", std::to_string((unsigned long long)heap.internalLargestFreeBlock), firstField);
|
|
|
|
|
- appendJsonStringField(json, "IntRAMMinFree", std::to_string((unsigned long long)heap.internalMinFree), firstField);
|
|
|
|
|
-
|
|
|
|
|
- json.append("}]");
|
|
|
|
|
|
|
+ char freeHeap[24];
|
|
|
|
|
+ snprintf(freeHeap, sizeof(freeHeap), "%llu",
|
|
|
|
|
+ static_cast<unsigned long long>(heap.totalFree));
|
|
|
|
|
|
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
|
|
httpd_resp_set_type(req, "application/json");
|
|
httpd_resp_set_type(req, "application/json");
|
|
|
- return httpd_resp_send(req, json.c_str(), json.length());
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
|
|
+ JsonChunkWriter json(req);
|
|
|
|
|
+ json.appendLiteral("[{");
|
|
|
|
|
+ bool firstField = true;
|
|
|
|
|
+
|
|
|
|
|
+ // Keep the original /sysinfo fields and their string types for existing clients.
|
|
|
|
|
+ json.appendStringField("firmware", libfive_git_version(), firstField);
|
|
|
|
|
+ json.appendStringField("buildtime", build_time(), firstField);
|
|
|
|
|
+ json.appendStringField("gitbranch", libfive_git_branch(), firstField);
|
|
|
|
|
+ json.appendStringField("gittag", libfive_git_version(), firstField);
|
|
|
|
|
+ json.appendStringField("gitrevision", libfive_git_revision(), firstField);
|
|
|
|
|
+ json.appendStringField("html", getHTMLversion().c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("cputemp", cpuTemperature, firstField);
|
|
|
|
|
+ json.appendStringField("hostname", hostname.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("IPv4", ipAddress.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("freeHeapMem", freeHeap, firstField);
|
|
|
|
|
+
|
|
|
|
|
+ // Additional fields consumed by info.html.
|
|
|
|
|
+ json.appendStringField("starttime", starttime.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("uptime", uptime.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("SSID", ssid.c_str(), firstField);
|
|
|
|
|
+
|
|
|
|
|
+ json.appendNumberField("chipCores", chipInfo.cores, firstField);
|
|
|
|
|
+ json.appendNumberField("chipRevision", chipInfo.revision, firstField);
|
|
|
|
|
+ json.appendNumberField("chipFeatures", chipInfo.features, firstField);
|
|
|
|
|
+
|
|
|
|
|
+ if (sdCard.cardAvailable)
|
|
|
|
|
+ {
|
|
|
|
|
+ json.appendStringField("sdCardManufacturer", sdCard.manufacturer.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("sdCardName", sdCard.name.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("sdCardCapacity", sdCard.capacityMB.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("sdCardSectorSize", sdCard.sectorSizeBytes.c_str(), firstField);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ json.appendNullField("sdCardManufacturer", firstField);
|
|
|
|
|
+ json.appendNullField("sdCardName", firstField);
|
|
|
|
|
+ json.appendNullField("sdCardCapacity", firstField);
|
|
|
|
|
+ json.appendNullField("sdCardSectorSize", firstField);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (sdCard.partitionAvailable)
|
|
|
|
|
+ {
|
|
|
|
|
+ json.appendStringField("sdCardPartitionSize", sdCard.partitionSizeMB.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("sdCardFreePartitionSpace", sdCard.partitionFreeMB.c_str(), firstField);
|
|
|
|
|
+ json.appendStringField("sdCardPartitionAllocationSize",
|
|
|
|
|
+ sdCard.partitionAllocationSizeBytes.c_str(), firstField);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ json.appendNullField("sdCardPartitionSize", firstField);
|
|
|
|
|
+ json.appendNullField("sdCardFreePartitionSpace", firstField);
|
|
|
|
|
+ json.appendNullField("sdCardPartitionAllocationSize", firstField);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ json.appendNumberField("RAMTotalFree", heap.totalFree, firstField);
|
|
|
|
|
+ json.appendNumberField("ExtRAMFree", heap.externalFree, firstField);
|
|
|
|
|
+ json.appendNumberField("ExtRAMLargestFree", heap.externalLargestFreeBlock, firstField);
|
|
|
|
|
+ json.appendNumberField("ExtRAMMinFree", heap.externalMinFree, firstField);
|
|
|
|
|
+ json.appendNumberField("IntRAMFree", heap.internalFree, firstField);
|
|
|
|
|
+ json.appendNumberField("IntRAMLargestFree", heap.internalLargestFreeBlock, firstField);
|
|
|
|
|
+ json.appendNumberField("IntRAMMinFree", heap.internalMinFree, firstField);
|
|
|
|
|
+
|
|
|
|
|
+ json.appendLiteral("}]");
|
|
|
|
|
+ return json.finish();
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
void register_server_main_uri(httpd_handle_t server, const char *base_path)
|
|
void register_server_main_uri(httpd_handle_t server, const char *base_path)
|
|
|
{
|
|
{
|