|
1 | 1 | /** |
2 | | - * @file src/ota/backup.h |
3 | | - * @brief Public API of the backup (.bkp) generator — OTA. |
| 2 | + * @file src/ota/backup.h |
| 3 | + * @brief API pública do gerador de backup (.bkp) — Fase 1 do OTA. |
4 | 4 | * |
5 | | - * @details Generation does two passes over LittleFS to avoid buffering in RAM: |
6 | | - * 1) scan: calculates payload_size + payload_crc32 + file_count. |
7 | | - * 2) emit: writes header (with CRCs) + payload streaming via Print. |
| 5 | + * @details A geração faz dois passes sobre a LittleFS para evitar buffer em RAM: |
| 6 | + * 1) scan: calcula payload_size + payload_crc32 + file_count. |
| 7 | + * 2) emit: escreve header (com CRCs) + payload em streaming via Print. |
8 | 8 | * |
9 | | - * The caller is responsible for ensuring LittleFS is mounted and |
10 | | - * that the operation is serialized against concurrent writes (e.g. |
11 | | - * via HeavyTaskGuard in WebManager). |
| 9 | + * O caller é responsável por garantir que o LittleFS está montado e |
| 10 | + * que a operação é serializada contra escritas concorrentes (ex.: |
| 11 | + * via HeavyTaskGuard no WebManager). |
12 | 12 | * |
13 | | - * @project SIMUT — Sistema Integrado de Monitoramento Universal e Telemetria |
14 | | - * SIMUT — Integrated Universal Monitoring and Telemetry System |
15 | | - * @target Raspberry Pi Pico W (RP2040) — Arduino Framework |
16 | | - * @author Ângelo Moisés Alves |
| 13 | + * @project SIMUT |
| 14 | + * @target Raspberry Pi Pico W (RP2040) — Arduino Framework |
| 15 | + * @author Ângelo Moisés Alves |
17 | 16 | * @license MIT License |
18 | 17 | */ |
19 | 18 | #pragma once |
|
26 | 25 | namespace ota { |
27 | 26 |
|
28 | 27 | /** |
29 | | - * @brief Incremental CRC32 (poly 0xEDB88320, init 0xFFFFFFFF, xor-out 0xFFFFFFFF). |
| 28 | + * @brief CRC32 incremental (poly 0xEDB88320, init 0xFFFFFFFF, xor-out 0xFFFFFFFF). |
30 | 29 | * |
31 | | - * Compatible with gzip/zlib CRC32. Reused in other steps (staging |
32 | | - * validation, metadata). |
| 30 | + * Compatível com CRC32 do gzip/zlib. Reutilizado em outras fases (validação |
| 31 | + * de staging, metadata). |
33 | 32 | * |
34 | | - * @param crc Previous state (pass OTA_CRC32_INIT on first chunk). |
35 | | - * @param data Byte buffer. |
36 | | - * @param len Length. |
37 | | - * @return Partial CRC; apply `^ 0xFFFFFFFFu` when done to get the final CRC. |
| 33 | + * @param crc Estado anterior (passar OTA_CRC32_INIT no primeiro chunk). |
| 34 | + * @param data Buffer de bytes. |
| 35 | + * @param len Tamanho. |
| 36 | + * @return CRC parcial; aplicar `^ 0xFFFFFFFFu` ao terminar para obter o CRC final. |
38 | 37 | */ |
39 | 38 | uint32_t crc32_update(uint32_t crc, const uint8_t* data, size_t len); |
40 | 39 |
|
41 | 40 | /** |
42 | | - * @brief Reads the unique RP2040 ID (flash_get_unique_id). |
| 41 | + * @brief Lê o ID único do RP2040 (flash_get_unique_id). |
43 | 42 | * |
44 | | - * Cached after first call (operation costs ~100us and disables IRQs). |
| 43 | + * Cacheado após a primeira chamada (operação custa ~100us e desabilita IRQs). |
45 | 44 | * |
46 | | - * @param out 8-byte buffer; filled with the ID. |
| 45 | + * @param out Buffer de 8 bytes; preenchido com o ID. |
47 | 46 | */ |
48 | 47 | void read_chip_id(uint8_t out[8]); |
49 | 48 |
|
50 | 49 | /** |
51 | | - * @brief Encodes version string "vMAJOR.MINOR.PATCH" into packed uint32. |
| 50 | + * @brief Encoda string de versão "vMAJOR.MINOR.PATCH" em uint32 packed. |
52 | 51 | * |
53 | | - * Format: (major<<16) | (minor<<8) | patch. Each field truncated to 8 bits. |
54 | | - * E.g.: "v3.37.8" → 0x00032508. |
| 52 | + * Formato: (major<<16) | (minor<<8) | patch. Cada campo é truncado para 8 bits. |
| 53 | + * Ex.: "v3.37.8" → 0x00032508. |
55 | 54 | * |
56 | | - * @param version_str String like "v3.37.8" or "3.37.8" (prefix 'v' optional). |
57 | | - * @return uint32 encoded; 0 if string is invalid. |
| 55 | + * @param version_str String tipo "v3.37.8" ou "3.37.8" (prefixo 'v' opcional). |
| 56 | + * @return uint32 encoded; 0 se string for inválida. |
58 | 57 | */ |
59 | 58 | uint32_t encode_version_u32(const char* version_str); |
60 | 59 |
|
61 | 60 | /** |
62 | | - * @brief Result of the LittleFS scan (prerequisite to generate header). |
| 61 | + * @brief Resultado do scan da LittleFS (pré-requisito para gerar header). |
63 | 62 | */ |
64 | 63 | struct BackupScanResult { |
65 | | - uint32_t payload_size; /**< Total payload size in bytes. */ |
66 | | - uint32_t payload_crc32; /**< Final payload CRC32 (already with xor-out applied). */ |
67 | | - uint16_t file_count; /**< Number of files enumerated. */ |
| 64 | + uint32_t payload_size; /**< Tamanho total do payload em bytes. */ |
| 65 | + uint32_t payload_crc32; /**< CRC32 final do payload (já com xor-out aplicado). */ |
| 66 | + uint16_t file_count; /**< Quantidade de arquivos enumerados. */ |
68 | 67 | }; |
69 | 68 |
|
70 | 69 | /** |
71 | | - * @brief Backup validation status codes. |
| 70 | + * @brief Códigos de status da validação de backup (Fase 2). |
72 | 71 | * |
73 | | - * Ordered so lower values correspond to failures detected |
74 | | - * earlier in parsing — useful for hierarchical error messages. |
| 72 | + * Ordenados de forma que valores baixos correspondam a falhas detectadas |
| 73 | + * mais cedo no parse — útil pra mensagens de erro hierárquicas. |
75 | 74 | */ |
76 | 75 | enum class BackupStatus : uint8_t { |
77 | | - OK = 0, |
78 | | - BAD_MAGIC = 1, |
79 | | - UNSUPPORTED_SCHEMA = 2, |
80 | | - HEADER_CRC_MISMATCH = 3, |
81 | | - PAYLOAD_TRUNCATED = 4, |
82 | | - PAYLOAD_CRC_MISMATCH = 5, |
83 | | - CHIP_ID_MISMATCH = 6, |
84 | | - PATH_INVALID = 7, |
85 | | - PATH_TOO_LONG = 8, |
86 | | - IO_ERROR = 9, |
87 | | - INTERNAL_ERROR = 10, |
| 76 | + OK = 0, |
| 77 | + BAD_MAGIC = 1, |
| 78 | + UNSUPPORTED_SCHEMA = 2, |
| 79 | + HEADER_CRC_MISMATCH = 3, |
| 80 | + PAYLOAD_TRUNCATED = 4, |
| 81 | + PAYLOAD_CRC_MISMATCH = 5, |
| 82 | + CHIP_ID_MISMATCH = 6, |
| 83 | + PATH_INVALID = 7, |
| 84 | + PATH_TOO_LONG = 8, |
| 85 | + IO_ERROR = 9, |
| 86 | + INTERNAL_ERROR = 10, |
88 | 87 | }; |
89 | 88 |
|
90 | | -/* Validation of .bkp in production is done by the state machine in |
91 | | - * ota::RestoreSession (VALIDATE mode) — see src/ota/restore.h. The |
92 | | - * Stream-based backup_validate was removed to save flash; the web upload |
93 | | - * pipeline already consumes chunks and maintains state, so the state |
94 | | - * machine covers both scenarios. */ |
| 89 | +/* Validação de .bkp em produção é feita pelo state machine em ota::RestoreSession |
| 90 | + * (modo VALIDATE) — ver src/ota/restore.h. backup_validate Stream-based foi |
| 91 | + * removido para economizar flash; o pipeline de upload web já consome chunks e |
| 92 | + * mantém estado, então a state machine cobre os dois cenários. */ |
95 | 93 |
|
96 | 94 | /** |
97 | | - * @brief Pass 1: scans LittleFS, computes total size and payload CRC32. |
| 95 | + * @brief Pass 1: varre a LittleFS, computa tamanho total e CRC32 do payload. |
98 | 96 | * |
99 | | - * Does NOT write anything; only measures. Returns metrics to build the header |
100 | | - * before pass 2. |
| 97 | + * NÃO escreve nada; apenas mede. Retorna métricas para construir o header |
| 98 | + * antes do pass 2. |
101 | 99 | * |
102 | | - * @param out Result. |
103 | | - * @return true if scan was successful; false on I/O error. |
| 100 | + * @param out Resultado. |
| 101 | + * @return true se o scan foi bem-sucedido; false em I/O error. |
104 | 102 | */ |
105 | 103 | bool backup_scan(BackupScanResult& out); |
106 | 104 |
|
107 | 105 | /** |
108 | | - * @brief Pass 2: writes the full backup (header + payload) in streaming. |
| 106 | + * @brief Pass 2: escreve o backup completo (header + payload) em streaming. |
109 | 107 | * |
110 | | - * Requires a valid BackupScanResult obtained by backup_scan (ideally |
111 | | - * immediately before, under HeavyTaskGuard, to ensure consistency). |
| 108 | + * Requer um `BackupScanResult` válido obtido por `backup_scan` (idealmente |
| 109 | + * imediatamente antes, sob HeavyTaskGuard, para garantir consistência). |
112 | 110 | * |
113 | | - * @param out Output stream (Print&; e.g. WebServer client). |
114 | | - * @param scan Result of pass 1. |
115 | | - * @param firmware_version Encoded version via encode_version_u32. |
116 | | - * @param timestamp Unix epoch UTC; 0 if NTP unavailable. |
117 | | - * @return true if write completed without I/O errors. |
| 111 | + * @param out Stream de saída (Print&; ex.: WebServer client). |
| 112 | + * @param scan Resultado do pass 1. |
| 113 | + * @param firmware_version Versão encoded via encode_version_u32. |
| 114 | + * @param timestamp Unix epoch UTC; 0 se NTP indisponível. |
| 115 | + * @return true se a escrita completou sem erros de I/O. |
118 | 116 | */ |
119 | 117 | bool backup_emit(Print& out, |
120 | | - const BackupScanResult& scan, |
121 | | - uint32_t firmware_version, |
122 | | - uint32_t timestamp); |
| 118 | + const BackupScanResult& scan, |
| 119 | + uint32_t firmware_version, |
| 120 | + uint32_t timestamp); |
123 | 121 |
|
124 | 122 | } /* namespace ota */ |
0 commit comments