standby.h/c: duty-cycle ACTIVE/SLEEP, standby_wait() будит в burst сразу, в SLEEP — до следующего burst; notify_network_activity для раннего пробуждения. Интеграция (по флагу UTUN_HAVE_STANDBY, десктоп без изменений): - conn_mgr: bg_ping/candidate_ping в SLEEP defer через standby_wait - route_connectivity: probe_node откладывается (PROBE_STATUS_DEFERRED) - topo_group_connect: пауза между попытками — активный режим 1с, фон standby_wait - jni_bridge/ConfigProvider/NetworkActivityMonitor: set_active + сетевая активность - chat_setting: standby_active_sec/sleep_sec/min_sleep_sec + юнит-тест test_standby (31 проверок)
170 lines
6.1 KiB
C
170 lines
6.1 KiB
C
/*
|
|
* chat_setting.c — chat-related settings: validation, storage, on-the-fly apply
|
|
*
|
|
* Используется и из парсера конфига (headless), и из GUI (через bridge).
|
|
* Никаких зависимостей от g_cc или instance — работает автономно.
|
|
*/
|
|
|
|
#include "chat_setting.h"
|
|
#include "../../lib/debug_config.h"
|
|
|
|
#ifdef DEBUG_CATEGORY_CHAT_SYNC
|
|
#define CS_ID DEBUG_CATEGORY_CHAT_SYNC
|
|
#else
|
|
#define CS_ID DEBUG_CATEGORY_GENERAL
|
|
#endif
|
|
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
/* ─── типы настроек ─── */
|
|
|
|
#define CHAT_SETTING_INT 0
|
|
#define CHAT_SETTING_BOOL 1
|
|
#define CHAT_SETTING_STRING 2
|
|
|
|
struct chat_setting_def {
|
|
const char* name;
|
|
int type;
|
|
int default_val;
|
|
int min_val;
|
|
int max_val;
|
|
};
|
|
|
|
/* хранилище для строковых значений */
|
|
#define CHAT_SETTING_STRING_MAX 16
|
|
static int g_chat_string_idx[CHAT_SETTING_STRING_MAX];
|
|
static char g_chat_string_val[CHAT_SETTING_STRING_MAX][256];
|
|
static int g_chat_string_count = 0;
|
|
|
|
/* ─── реестр всех известных настроек ─── */
|
|
|
|
static const struct chat_setting_def g_setting_defs[] = {
|
|
{"storage_autoload", CHAT_SETTING_BOOL, 1, 0, 1},
|
|
{"opus_codec_preset", CHAT_SETTING_INT, 1, 0, 4},
|
|
{"compressor_enabled", CHAT_SETTING_BOOL, 0, 0, 1},
|
|
{"compressor_max_gain_db", CHAT_SETTING_INT, 25, 1, 100},
|
|
{"compressor_rise_rate", CHAT_SETTING_INT, 10, 1, 100},
|
|
{"media_download_max_peers", CHAT_SETTING_INT, 3, 1, 16},
|
|
{"standby_active_sec", CHAT_SETTING_INT, 2, 1, 3600},
|
|
{"standby_sleep_sec", CHAT_SETTING_INT, 60, 1, 86400},
|
|
{"standby_min_sleep_sec", CHAT_SETTING_INT, 15, 1, 86400},
|
|
{"join_policy", CHAT_SETTING_INT, 1, 0, 2}, /* 0=autojoin, 1=ask, 2=deny */
|
|
{"whisper_enabled", CHAT_SETTING_BOOL, 0, 0, 1},
|
|
{"whisper_model_path", CHAT_SETTING_STRING, 0, 0, 0},
|
|
};
|
|
|
|
#define CHAT_SETTING_COUNT (sizeof(g_setting_defs) / sizeof(g_setting_defs[0]))
|
|
|
|
/* ─── состояние (текущие значения) ─── */
|
|
|
|
static int g_chat_values[CHAT_SETTING_COUNT];
|
|
static int g_chat_initialized = 0;
|
|
|
|
static void chat_setting_reset_defaults(void) {
|
|
for (size_t i = 0; i < CHAT_SETTING_COUNT; i++)
|
|
g_chat_values[i] = g_setting_defs[i].default_val;
|
|
g_chat_initialized = 1;
|
|
}
|
|
|
|
/* ─── поиск ─── */
|
|
|
|
static int chat_setting_find(const char* name) {
|
|
for (size_t i = 0; i < CHAT_SETTING_COUNT; i++)
|
|
if (strcmp(g_setting_defs[i].name, name) == 0) return (int)i;
|
|
return -1;
|
|
}
|
|
|
|
/* ─── apply: вызов специфичного для настройки кода ─── */
|
|
|
|
static void chat_setting_apply(void) {
|
|
/* будет заполняться по мере появления runtime-setter'ов в модулях:
|
|
if (idx == ...) some_module_set_xxx(value); */
|
|
}
|
|
|
|
/* ─── public API ─── */
|
|
|
|
int chat_setting_set(const char* name, const char* value) {
|
|
if (!name || !value || !*name || !*value) return -2;
|
|
|
|
if (!g_chat_initialized) chat_setting_reset_defaults();
|
|
|
|
int idx = chat_setting_find(name);
|
|
if (idx < 0) {
|
|
DEBUG_ERROR(CS_ID, "chat_setting: unknown setting '%s'", name);
|
|
return -1;
|
|
}
|
|
|
|
const struct chat_setting_def* def = &g_setting_defs[idx];
|
|
int v;
|
|
|
|
if (def->type == CHAT_SETTING_STRING) {
|
|
/* сохраняем строку в отдельном хранилище */
|
|
for (int i = 0; i < g_chat_string_count; i++) {
|
|
if (g_chat_string_idx[i] == idx) {
|
|
if (strcmp(g_chat_string_val[i], value) == 0) return 0;
|
|
snprintf(g_chat_string_val[i], sizeof(g_chat_string_val[i]), "%s", value);
|
|
DEBUG_INFO(CS_ID, "chat_setting: %s = %s", name, value);
|
|
chat_setting_apply();
|
|
return 0;
|
|
}
|
|
}
|
|
if (g_chat_string_count >= CHAT_SETTING_STRING_MAX) {
|
|
DEBUG_ERROR(CS_ID, "chat_setting: string storage full for '%s'", name);
|
|
return -2;
|
|
}
|
|
g_chat_string_idx[g_chat_string_count] = idx;
|
|
snprintf(g_chat_string_val[g_chat_string_count], sizeof(g_chat_string_val[0]), "%s", value);
|
|
g_chat_string_count++;
|
|
DEBUG_INFO(CS_ID, "chat_setting: %s = %s", name, value);
|
|
chat_setting_apply();
|
|
return 0;
|
|
}
|
|
|
|
if (def->type == CHAT_SETTING_BOOL) {
|
|
if (strcmp(value, "0") == 0 || strcmp(value, "no") == 0 || strcmp(value, "false") == 0) v = 0;
|
|
else if (strcmp(value, "1") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "true") == 0) v = 1;
|
|
else { DEBUG_ERROR(CS_ID, "chat_setting: '%s' invalid bool '%s' (expect 0/1)", name, value); return -2; }
|
|
} else {
|
|
char* end = NULL;
|
|
long lv = strtol(value, &end, 10);
|
|
if (!end || *end != '\0' || lv < def->min_val || lv > def->max_val) {
|
|
DEBUG_ERROR(CS_ID, "chat_setting: '%s' invalid int '%s' (range %d..%d)", name, value, def->min_val, def->max_val);
|
|
return -2;
|
|
}
|
|
v = (int)lv;
|
|
}
|
|
|
|
if (g_chat_values[idx] == v) return 0;
|
|
|
|
g_chat_values[idx] = v;
|
|
DEBUG_INFO(CS_ID, "chat_setting: %s = %s", name, value);
|
|
chat_setting_apply();
|
|
return 0;
|
|
}
|
|
|
|
const char* chat_setting_get(const char* name) {
|
|
if (!g_chat_initialized) chat_setting_reset_defaults();
|
|
int idx = chat_setting_find(name);
|
|
if (idx < 0) return NULL;
|
|
|
|
const struct chat_setting_def* def = &g_setting_defs[idx];
|
|
if (def->type == CHAT_SETTING_STRING) {
|
|
for (int i = 0; i < g_chat_string_count; i++) {
|
|
if (g_chat_string_idx[i] == idx) return g_chat_string_val[i];
|
|
}
|
|
return "";
|
|
}
|
|
|
|
static char buf[16];
|
|
snprintf(buf, sizeof(buf), "%d", g_chat_values[idx]);
|
|
return buf;
|
|
}
|
|
|
|
int chat_setting_get_int(const char* name, int def) {
|
|
if (!g_chat_initialized) chat_setting_reset_defaults();
|
|
int idx = chat_setting_find(name);
|
|
return idx >= 0 ? g_chat_values[idx] : def;
|
|
}
|