Add esp32s2 support

This commit is contained in:
2026-04-12 21:25:01 +03:00
parent 98cc0db107
commit 818a84bf4f
10 changed files with 176 additions and 91 deletions

View File

@@ -1,52 +1,123 @@
#include "protocol.h"
#include "cJSON.h"
#include <stdio.h>
#include <stdarg.h>
cJSON* protocol_message_to_json(const ProtocolMessage* msg) {
cJSON* root = cJSON_CreateObject();
#define PROTOCOL_VERSION 1
cJSON_AddNumberToObject(root, "v", msg->v);
cJSON_AddStringToObject(root, "id", msg->id.c_str());
cJSON_AddStringToObject(root, "type", msg->type.c_str());
cJSON_AddNumberToObject(root, "ts", msg->ts);
cJSON_AddStringToObject(root, "deviceId", msg->deviceId.c_str());
static inline int append(char* buf, size_t size, int pos, const char* fmt, ...) {
if ((size_t)pos >= size) return -1;
cJSON* payload_obj = nullptr;
if (msg->payload) {
payload_obj = cJSON_Duplicate(msg->payload, 1); // deep copy
} else {
payload_obj = cJSON_CreateObject();
va_list args;
va_start(args, fmt);
int written = vsnprintf(buf + pos, size - pos, fmt, args);
va_end(args);
if (written < 0 || (size_t)(pos + written) >= size) {
return -1;
}
cJSON_AddItemToObject(root, "payload", payload_obj);
return root;
return pos + written;
}
ProtocolMessage protocol_message_from_json(const cJSON* root) {
ProtocolMessage msg;
// --- TELEMETRY (single measurement) ---
int build_telemetry_single(
char* buf,
size_t buf_size,
const char* id,
const char* device_id,
int64_t ts_ms,
const char* metric,
double value,
const char* unit,
const char* source,
int64_t m_ts_ms
) {
int pos = 0;
const cJSON* item = nullptr;
// header
pos = append(buf, buf_size, pos,
"{\"v\":%d,\"id\":\"%s\",\"type\":\"telemetry\",\"ts\":%lld,"
"\"deviceId\":\"%s\",\"payload\":{\"measurements\":[",
PROTOCOL_VERSION,
id,
(long long)ts_ms,
device_id
);
if (pos < 0) return -1;
item = cJSON_GetObjectItem(root, "v");
if (item && cJSON_IsNumber(item)) msg.v = item->valueint;
// measurement start
pos = append(buf, buf_size, pos,
"{\"metric\":\"%s\",\"value\":%.3f",
metric,
value
);
if (pos < 0) return -1;
item = cJSON_GetObjectItem(root, "id");
if (item && cJSON_IsString(item)) msg.id = item->valuestring;
item = cJSON_GetObjectItem(root, "type");
if (item && cJSON_IsString(item)) msg.type = item->valuestring;
item = cJSON_GetObjectItem(root, "ts");
if (item && cJSON_IsNumber(item)) msg.ts = item->valuedouble; // uint64_t
item = cJSON_GetObjectItem(root, "deviceId");
if (item && cJSON_IsString(item)) msg.deviceId = item->valuestring;
item = cJSON_GetObjectItem(root, "payload");
if (item && cJSON_IsObject(item)) {
msg.payload = cJSON_Duplicate(item, 1); // deep copy
} else {
msg.payload = cJSON_CreateObject();
// optional
if (unit) {
pos = append(buf, buf_size, pos, ",\"unit\":\"%s\"", unit);
if (pos < 0) return -1;
}
return msg;
if (source) {
pos = append(buf, buf_size, pos, ",\"source\":\"%s\"", source);
if (pos < 0) return -1;
}
if (m_ts_ms > 0) {
pos = append(buf, buf_size, pos, ",\"ts\":%lld", (long long)m_ts_ms);
if (pos < 0) return -1;
}
// close measurement + payload
pos = append(buf, buf_size, pos, "}]}}");
if (pos < 0) return -1;
return pos;
}
// --- EVENT ---
int build_event(
char* buf,
size_t buf_size,
const char* id,
const char* device_id,
int64_t ts_ms,
const char* name,
const char* severity,
const char* message
) {
int pos = 0;
// header
pos = append(buf, buf_size, pos,
"{\"v\":%d,\"id\":\"%s\",\"type\":\"event\",\"ts\":%lld,"
"\"deviceId\":\"%s\",\"payload\":{",
PROTOCOL_VERSION,
id,
(long long)ts_ms,
device_id
);
if (pos < 0) return -1;
// required
pos = append(buf, buf_size, pos, "\"name\":\"%s\"", name);
if (pos < 0) return -1;
// optional
if (severity) {
pos = append(buf, buf_size, pos, ",\"severity\":\"%s\"", severity);
if (pos < 0) return -1;
}
if (message) {
pos = append(buf, buf_size, pos, ",\"message\":\"%s\"", message);
if (pos < 0) return -1;
}
// close
pos = append(buf, buf_size, pos, "}}");
if (pos < 0) return -1;
return pos;
}