Files
DigiRadio/Software/components/core/src/WifiProvisionJson.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

103 lines
3.1 KiB
C++

/**
* @file WifiProvisionJson.cpp
* @brief Wi-Fi provisioning JSON parse/serialise implementation.
*
* DigiRadio firmware — https://github.com/manvalan/DigiRadio
*
* Copyright 2026 Michele Bigi
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* @author Michele Bigi
* @date 2026-07-06
*/
#include "core/WifiProvisionJson.hpp"
namespace core {
namespace {
/**
* @brief extractJsonString — read a quoted string value for a key.
*
* @dname extractJsonString
* @param json Full JSON object text.
* @param key Field name without quotes (e.g. ssid).
* @return Decoded string view into json, or empty on failure.
* @pubstate none
*
* @author Michele Bigi
* @date 2026-07-06
*/
[[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);
}
} // namespace
std::expected<WifiCredentials, ParseError>
parseWifiProvisionJson(std::string_view json)
{
if (json.find('{') == std::string_view::npos) {
return std::unexpected(ParseError::InvalidJson);
}
const std::string_view ssidRaw = extractJsonString(json, "ssid");
if (ssidRaw.empty() && json.find("\"ssid\"") == std::string_view::npos) {
return std::unexpected(ParseError::MissingField);
}
if (!WifiSsid::isValid(ssidRaw)) {
return std::unexpected(ParseError::InvalidSsid);
}
std::string_view passwordRaw;
if (json.find("\"password\"") != std::string_view::npos) {
passwordRaw = extractJsonString(json, "password");
}
if (!WifiCredentials::isPasswordValid(passwordRaw)) {
return std::unexpected(ParseError::InvalidPassword);
}
return WifiCredentials(WifiSsid(ssidRaw), Secret(std::string(passwordRaw)));
}
std::string serializeWifiProvisionSavedJson(unsigned rebootInSec)
{
return std::string("{\"status\":\"saved\",\"reboot_in_sec\":")
+ std::to_string(rebootInSec) + "}";
}
std::string serializeWifiProvisionErrorJson(std::string_view reason)
{
return std::string("{\"status\":\"error\",\"reason\":\"") + std::string(reason)
+ "\"}";
}
} // namespace core