refactor: extract duplicate isHex/parseHex into shared serial_utils.h
All checks were successful
CI / build (push) Successful in 1m7s

Fixes #19
This commit is contained in:
eddy 2026-08-05 08:23:34 +02:00
commit daa2600bde
3 changed files with 20 additions and 28 deletions

View file

@ -10,6 +10,7 @@
#include <esp_log.h>
#include "version.h"
#include "ecdh_helper.h"
#include "serial_utils.h"
static const char *TAG = "client";
@ -126,20 +127,6 @@ class MyClientCallbacks : public NimBLEClientCallbacks {
static MyClientCallbacks clientCallbacks;
static bool isHex(const String &hex) {
for (int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))) return false;
}
return true;
}
static void parseHex(const String &hex, uint8_t *out, int len) {
for (int i = 0; i < len; i++) {
out[i] = (uint8_t)strtoul(hex.substring(i * 2, i * 2 + 2).c_str(), nullptr, 16);
}
}
static void buildAesPacket(uint8_t cmd, uint8_t *packet) {
uint32_t s = ++seq;
packet[0] = (s >> 24) & 0xFF;

View file

@ -13,6 +13,7 @@
#include <esp_log.h>
#include "version.h"
#include "ecdh_helper.h"
#include "serial_utils.h"
static const char *TAG = "server";
@ -211,20 +212,6 @@ static bool decryptAesGcm(const uint8_t *packet, int idx, uint8_t *outCmd) {
return true;
}
static bool isHex(const String &hex) {
for (int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))) return false;
}
return true;
}
static void parseHex(const String &hex, uint8_t *out, int len) {
for (int i = 0; i < len; i++) {
out[i] = (uint8_t)strtoul(hex.substring(i * 2, i * 2 + 2).c_str(), nullptr, 16);
}
}
static void otaReset() {
otaInProgress = false;
otaHandle = 0;

18
src/serial_utils.h Normal file
View file

@ -0,0 +1,18 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <Arduino.h>
static inline bool isHex(const String &hex) {
for (int i = 0; i < hex.length(); i++) {
char c = hex.charAt(i);
if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F'))) return false;
}
return true;
}
static inline void parseHex(const String &hex, uint8_t *out, int len) {
for (int i = 0; i < len; i++) {
out[i] = (uint8_t)strtoul(hex.substring(i * 2, i * 2 + 2).c_str(), nullptr, 16);
}
}