Files
DigiRadio/Software/components/core/src/DspParamJson.cpp
T
micheleandClaude Sonnet 5 7cb25be3f3 Fix audio-profile NVS persistence and whitespace-intolerant JSON parsers
- NvsAudioProfileStore used the key "audio_profile_json" (18 chars),
  exceeding NVS's 15-char key-name limit. Every nvs_set_str call failed
  silently with ESP_ERR_NVS_KEY_TOO_LONG (0x1109): applyProfile() always
  updated live DSP audio correctly, so the bug was invisible except as
  "store_failed" in the HTTP response and settings never surviving a
  reboot. Renamed to "audio_profile" (13 chars); confirmed live, EQ/mixer
  changes now persist across a reset.

- The hand-rolled JSON extractJsonString()/extractJsonBool() helpers
  (duplicated per-file: TunerJson, DspParamJson, StationListJson,
  WebRadioJson, WifiProvisionJson, AudioProfileJson, plus inline mac/name/
  save parsing in BluetoothJson) matched only the exact literal
  `"key":"value"` / `"key":true`, with no tolerance for a space after the
  colon. Standard JSON encoders (e.g. Swift's JSONEncoder in its default,
  non-compact mode) emit `"key": "value"`, which silently failed to parse
  as invalid_json/missing_field. Numeric fields were already fine
  (strtoul/strtof skip leading whitespace per the C standard); fixed only
  the string/bool extractors to skip whitespace after the colon before
  matching the value.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-24 14:55:11 +02:00

110 lines
2.9 KiB
C++

/**
* @file DspParamJson.cpp
* @brief DspParamJson implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* @author Michele Bigi
* @date 2026-08-18
*/
#include "core/DspParamJson.hpp"
#include <cstdlib>
#include <sstream>
namespace core {
namespace {
[[nodiscard]] std::string_view extractJsonString(std::string_view json,
std::string_view key)
{
const std::string needle =
std::string("\"") + std::string(key) + "\":";
const std::size_t needlePos = json.find(needle);
if (needlePos == std::string_view::npos) {
return {};
}
std::size_t start = needlePos + needle.size();
while (start < json.size()
&& (json[start] == ' ' || json[start] == '\t'
|| json[start] == '\r' || json[start] == '\n')) {
++start;
}
if (start >= json.size() || json[start] != '"') {
return {};
}
const std::size_t valueStart = start + 1U;
const std::size_t valueEnd = json.find('"', valueStart);
if (valueEnd == std::string_view::npos) {
return {};
}
return json.substr(valueStart, valueEnd - valueStart);
}
[[nodiscard]] bool extractJsonFloat(std::string_view json,
std::string_view key, float& out)
{
const std::string needle = std::string("\"") + std::string(key) + "\":";
const std::size_t start = json.find(needle);
if (start == std::string_view::npos) {
return false;
}
const std::size_t valueStart = start + needle.size();
char* end = nullptr;
out = std::strtof(json.data() + valueStart, &end);
return end != json.data() + valueStart;
}
} // namespace
std::expected<DspParamWriteRequest, ParseError> parseDspParamWriteJson(
std::string_view json)
{
if (json.find('{') == std::string_view::npos) {
return std::unexpected(ParseError::InvalidJson);
}
const std::string_view name = extractJsonString(json, "name");
if (name.empty()) {
return std::unexpected(ParseError::MissingField);
}
float value = 0.0F;
if (!extractJsonFloat(json, "value", value)) {
return std::unexpected(ParseError::MissingField);
}
DspParamWriteRequest req;
req.name.assign(name.begin(), name.end());
req.value = value;
return req;
}
std::string serializeDspParamListJson(const std::vector<DspParamInfo>& params)
{
std::ostringstream out;
out << "{\"params\":[";
for (std::size_t i = 0; i < params.size(); ++i) {
if (i > 0U) {
out << ',';
}
out << "{\"name\":\"" << params[i].name << "\",\"address\":"
<< params[i].address << "}";
}
out << "]}";
return out.str();
}
std::string serializeDspParamErrorJson(const char* reason)
{
return std::string("{\"status\":\"error\",\"reason\":\"") + reason
+ "\"}";
}
} // namespace core