76 lines
2.2 KiB
C
76 lines
2.2 KiB
C
#include "maze32_wifi_config.h"
|
|
|
|
#include <string.h>
|
|
|
|
static size_t bounded_length(const char *value, size_t capacity)
|
|
{
|
|
size_t length = 0U;
|
|
while (length < capacity && value[length] != '\0') {
|
|
++length;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
static int has_control_bytes(const char *value)
|
|
{
|
|
const unsigned char *cursor = (const unsigned char *)value;
|
|
while (*cursor != '\0') {
|
|
if (*cursor < 0x20U || *cursor == 0x7fU) {
|
|
return 1;
|
|
}
|
|
++cursor;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
maze32_wifi_config_validation_t maze32_wifi_credentials_validate(
|
|
const maze32_wifi_credentials_t *credentials)
|
|
{
|
|
if (credentials == NULL) {
|
|
return MAZE32_WIFI_CONFIG_INVALID_SSID;
|
|
}
|
|
|
|
size_t ssid_length = bounded_length(credentials->ssid, sizeof(credentials->ssid));
|
|
if (ssid_length == 0U || ssid_length > MAZE32_WIFI_SSID_MAX_BYTES ||
|
|
has_control_bytes(credentials->ssid)) {
|
|
return MAZE32_WIFI_CONFIG_INVALID_SSID;
|
|
}
|
|
|
|
size_t password_length = bounded_length(credentials->password, sizeof(credentials->password));
|
|
if (password_length > MAZE32_WIFI_PASSWORD_MAX_BYTES ||
|
|
(password_length > 0U && password_length < 8U) ||
|
|
has_control_bytes(credentials->password)) {
|
|
return MAZE32_WIFI_CONFIG_INVALID_PASSWORD;
|
|
}
|
|
|
|
return MAZE32_WIFI_CONFIG_VALID;
|
|
}
|
|
|
|
int maze32_wifi_config_apply(const maze32_wifi_credentials_t *credentials,
|
|
maze32_reconfiguration_guard_fn guard,
|
|
maze32_credentials_save_fn save,
|
|
void *context)
|
|
{
|
|
if (maze32_wifi_credentials_validate(credentials) != MAZE32_WIFI_CONFIG_VALID ||
|
|
guard == NULL || save == NULL) {
|
|
return -1;
|
|
}
|
|
if (guard(context) != 0) {
|
|
return -2;
|
|
}
|
|
return save(credentials, context) == 0 ? 0 : -3;
|
|
}
|
|
|
|
int maze32_wifi_config_reset(maze32_reconfiguration_guard_fn guard,
|
|
maze32_credentials_clear_fn clear,
|
|
void *context)
|
|
{
|
|
if (guard == NULL || clear == NULL) {
|
|
return -1;
|
|
}
|
|
if (guard(context) != 0) {
|
|
return -2;
|
|
}
|
|
return clear(context) == 0 ? 0 : -3;
|
|
}
|