Skip to content

Commit

Permalink
net: wifi_cred: Decrease flash usage for error print strings
Browse files Browse the repository at this point in the history
As the error print strings are very similar, construct the final
output at runtime to save some flash space.

Signed-off-by: Jukka Rissanen <[email protected]>
(cherry picked from commit 3c59bd4)
  • Loading branch information
jukkar authored and dkalowsk committed Dec 9, 2024
1 parent e60954d commit d6ec225
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions subsys/net/lib/wifi_credentials/wifi_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,27 @@ int wifi_credentials_get_by_ssid_personal_struct(const char *ssid, size_t ssid_l
int ret;

if (ssid == NULL || ssid_len > WIFI_SSID_MAX_LEN || ssid_len == 0) {
LOG_ERR("Cannot retrieve WiFi credentials, SSID has invalid format");
LOG_ERR("Cannot %s WiFi credentials, %s", "retrieve", "SSID has invalid format");
return -EINVAL;
}

if (buf == NULL) {
LOG_ERR("Cannot retrieve WiFi credentials, "
"destination struct pointer cannot be NULL");
LOG_ERR("Cannot %s WiFi credentials, %s", "retrieve", "destination is NULL");
return -EINVAL;
}

k_mutex_lock(&wifi_credentials_mutex, K_FOREVER);

idx = lookup_idx(ssid, ssid_len);
if (idx == -1) {
LOG_DBG("Cannot retrieve WiFi credentials, no entry found for the provided SSID");
LOG_DBG("Cannot %s WiFi credentials, %s", "retrieve", "SSID not found");
ret = -ENOENT;
goto exit;
}

ret = wifi_credentials_load_entry(idx, buf, sizeof(struct wifi_credentials_personal));
if (ret) {
LOG_ERR("Failed to load WiFi credentials at index %d, err: %d", idx, ret);
LOG_ERR("Failed to %s WiFi credentials at index %d, err: %d", "load", idx, ret);
goto exit;
}

Expand All @@ -155,12 +154,12 @@ int wifi_credentials_set_personal_struct(const struct wifi_credentials_personal
int ret;

if (creds == NULL) {
LOG_ERR("Cannot set WiFi credentials, provided struct pointer cannot be NULL");
LOG_ERR("Cannot %s WiFi credentials, %s", "set", "credential not set");
return -EINVAL;
}

if (creds->header.ssid_len > WIFI_SSID_MAX_LEN || creds->header.ssid_len == 0) {
LOG_ERR("Cannot set WiFi credentials, SSID has invalid format");
LOG_ERR("Cannot %s WiFi credentials, %s", "set", "SSID has invalid format");
return -EINVAL;
}

Expand All @@ -170,15 +169,15 @@ int wifi_credentials_set_personal_struct(const struct wifi_credentials_personal
if (idx == -1) {
idx = lookup_unused_idx();
if (idx == -1) {
LOG_ERR("Cannot store WiFi credentials, no space left");
LOG_ERR("Cannot %s WiFi credentials, %s", "store", "no space left");
ret = -ENOBUFS;
goto exit;
}
}

ret = wifi_credentials_store_entry(idx, creds, sizeof(struct wifi_credentials_personal));
if (ret) {
LOG_ERR("Failed to store WiFi credentials at index %d, err: %d", idx, ret);
LOG_ERR("Failed to %s WiFi credentials at index %d, err: %d", "store", idx, ret);
goto exit;
}

Expand All @@ -200,20 +199,21 @@ int wifi_credentials_set_personal(const char *ssid, size_t ssid_len, enum wifi_s
struct wifi_credentials_header *header;

if (ssid == NULL || ssid_len > WIFI_SSID_MAX_LEN || ssid_len == 0) {
LOG_ERR("Cannot set WiFi credentials, SSID has invalid format");
LOG_ERR("Cannot %s WiFi credentials, %s", "set", "SSID has invalid format");
return -EINVAL;
}

if (flags & WIFI_CREDENTIALS_FLAG_BSSID &&
(bssid_len != WIFI_MAC_ADDR_LEN || bssid == NULL)) {
LOG_ERR("Cannot set WiFi credentials, "
"provided flags indicated BSSID, but no BSSID provided");
LOG_ERR("Cannot %s WiFi credentials, %s", "set",
"provided flags indicated BSSID but no BSSID provided");
return -EINVAL;
}

if ((type != WIFI_SECURITY_TYPE_NONE && (password_len == 0 || password == NULL)) ||
(password_len > WIFI_CREDENTIALS_MAX_PASSWORD_LEN)) {
LOG_ERR("Cannot set WiFi credentials, password not provided or invalid");
LOG_ERR("Cannot %s WiFi credentials, %s", "set",
"password not provided or invalid");
return -EINVAL;
}

Expand Down Expand Up @@ -246,9 +246,8 @@ int wifi_credentials_set_personal(const char *ssid, size_t ssid_len, enum wifi_s
break;
}
default:
LOG_ERR("Cannot set WiFi credentials, "
"provided security type %d is unsupported",
type);
LOG_ERR("Cannot %s WiFi credentials, provided security type %d is unsupported",
"set", type);
return -ENOTSUP;
}

Expand All @@ -269,18 +268,18 @@ int wifi_credentials_get_by_ssid_personal(const char *ssid, size_t ssid_len,
struct wifi_credentials_header *header;

if (ssid == NULL || ssid_len > WIFI_SSID_MAX_LEN || ssid_len == 0) {
LOG_ERR("Cannot retrieve WiFi credentials, SSID has invalid format");
LOG_ERR("Cannot %s WiFi credentials, %s", "retrieve", "SSID has invalid format");
return -EINVAL;
}

if (bssid_buf_len != WIFI_MAC_ADDR_LEN || bssid_buf == NULL) {
LOG_ERR("BSSID buffer needs to be provided");
LOG_ERR("%s buffer needs to be provided", "BSSID");
return -EINVAL;
}

if (password_buf == NULL || password_buf_len > WIFI_CREDENTIALS_MAX_PASSWORD_LEN ||
password_buf_len == 0) {
LOG_ERR("WiFi password buffer needs to be provided");
LOG_ERR("%s buffer needs to be provided", "WiFi password");
return -EINVAL;
}

Expand Down Expand Up @@ -318,7 +317,7 @@ int wifi_credentials_get_by_ssid_personal(const char *ssid, size_t ssid_len,
break;
}
default:
LOG_ERR("Cannot get WiFi credentials, "
LOG_ERR("Cannot %s WiFi credentials, %s", "get",
"the requested credentials have invalid WIFI_SECURITY_TYPE");
ret = -EPROTO;
}
Expand All @@ -331,7 +330,7 @@ int wifi_credentials_delete_by_ssid(const char *ssid, size_t ssid_len)
int ret = 0;

if (ssid == NULL || ssid_len > WIFI_SSID_MAX_LEN || ssid_len == 0) {
LOG_ERR("Cannot delete WiFi credentials, SSID has invalid format");
LOG_ERR("Cannot %s WiFi credentials, %s", "delete", "SSID has invalid format");
return -EINVAL;
}

Expand All @@ -345,7 +344,7 @@ int wifi_credentials_delete_by_ssid(const char *ssid, size_t ssid_len)

ret = wifi_credentials_delete_entry(idx);
if (ret) {
LOG_ERR("Failed to delete WiFi credentials index %d, err: %d", idx, ret);
LOG_ERR("Failed to %s WiFi credentials index %d, err: %d", "delete", idx, ret);
goto exit;
}

Expand Down Expand Up @@ -393,8 +392,8 @@ int wifi_credentials_delete_all(void)
if (is_entry_used(i)) {
ret = wifi_credentials_delete_entry(i);
if (ret) {
LOG_ERR("Failed to delete WiFi credentials index %d, err: %d", i,
ret);
LOG_ERR("Failed to %s WiFi credentials index %d, err: %d",
"delete", i, ret);
break;
}

Expand Down

0 comments on commit d6ec225

Please sign in to comment.