112 lines
3.0 KiB
C
112 lines
3.0 KiB
C
#include <jl_modbus/jl_config.h>
|
|
#include <jlv135_cli/jl_block_wifi.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#define JLV_WIFI_SSID_BYTES 32
|
|
|
|
typedef struct _jlv_wifi_t
|
|
{
|
|
char ssid[JLV_WIFI_SSID_BYTES];
|
|
char password[64];
|
|
bool enable : 1;
|
|
} jlv_wifi_t;
|
|
|
|
// Метод get: отображает все параметры блока wifi
|
|
int
|
|
jl_block_wifi_get(void)
|
|
{
|
|
jlv_wifi_t conf = {};
|
|
jl_config_load("wifi", &conf, sizeof(jlv_wifi_t));
|
|
|
|
printf("WiFi Configuration:\n");
|
|
printf(" SSID: %s\n", conf.ssid);
|
|
printf(" Password: %s\n", conf.password[0] ? "********" : "(empty)");
|
|
printf(" Enabled: %s\n", conf.enable ? "Yes" : "No");
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Метод set: изменяет параметры блока wifi
|
|
int
|
|
jl_block_wifi_set(int argc, const char *argv[])
|
|
{
|
|
if (argc < 1)
|
|
{
|
|
fprintf(stderr, "Error: insufficient arguments for 'set wifi'\n");
|
|
fprintf(stderr, "Usage:\n");
|
|
fprintf(stderr, " jlv135_cli set wifi ssid <network_name>\n");
|
|
fprintf(stderr, " jlv135_cli set wifi password <password>\n");
|
|
fprintf(stderr, " jlv135_cli set wifi enable <on|off>\n");
|
|
return 1;
|
|
}
|
|
|
|
jlv_wifi_t conf = {};
|
|
jl_config_load("wifi", &conf, sizeof(jlv_wifi_t));
|
|
|
|
// SSID
|
|
if (strcmp(argv[0], "ssid") == 0)
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
fprintf(stderr, "Error: SSID value required\n");
|
|
return 1;
|
|
}
|
|
|
|
strncpy(conf.ssid, argv[1], JLV_WIFI_SSID_BYTES - 1);
|
|
conf.ssid[JLV_WIFI_SSID_BYTES - 1] = '\0';
|
|
jl_config_save("wifi", &conf, sizeof(jlv_wifi_t));
|
|
printf("WiFi SSID set to: %s\n", conf.ssid);
|
|
return 0;
|
|
}
|
|
// Password
|
|
else if (strcmp(argv[0], "password") == 0)
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
fprintf(stderr, "Error: password value required\n");
|
|
return 1;
|
|
}
|
|
|
|
strncpy(conf.password, argv[1], sizeof(conf.password) - 1);
|
|
conf.password[sizeof(conf.password) - 1] = '\0';
|
|
jl_config_save("wifi", &conf, sizeof(jlv_wifi_t));
|
|
printf("WiFi password set\n");
|
|
return 0;
|
|
}
|
|
// Enable/Disable
|
|
else if (strcmp(argv[0], "enable") == 0)
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
fprintf(stderr, "Error: enable value required (on|off)\n");
|
|
return 1;
|
|
}
|
|
|
|
if (strcmp(argv[1], "on") == 0 || strcmp(argv[1], "1") == 0)
|
|
{
|
|
conf.enable = true;
|
|
}
|
|
else if (strcmp(argv[1], "off") == 0 || strcmp(argv[1], "0") == 0)
|
|
{
|
|
conf.enable = false;
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Error: invalid enable value '%s' (use on|off)\n", argv[1]);
|
|
return 1;
|
|
}
|
|
|
|
jl_config_save("wifi", &conf, sizeof(jlv_wifi_t));
|
|
printf("WiFi %s\n", conf.enable ? "enabled" : "disabled");
|
|
return 0;
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr, "Error: unknown parameter '%s'\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|