Skip to content

Commit 06ce9da

Browse files
committed
lib: tls_credentials: print usage
Print usage hint instead of unhelpful "wrong parameter count" message. Signed-off-by: Maximilian Deubel <[email protected]>
1 parent 7c39469 commit 06ce9da

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

subsys/net/lib/tls_credentials/tls_credentials_shell.c

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ static int tls_cred_cmd_add(const struct shell *sh, size_t argc, char *argv[])
357357
bool keep_copy = false;
358358
int ref_slot = -1;
359359

360+
if (argc < 5) {
361+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred add <sectag> <type> <backend> <format> [data]\n");
362+
shell_fprintf(sh, SHELL_ERROR, " <sectag> : Security tag\n");
363+
shell_fprintf(sh, SHELL_ERROR, " <type> : CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
364+
shell_fprintf(sh, SHELL_ERROR, " <backend> : default\n");
365+
shell_fprintf(sh, SHELL_ERROR, " <format> : str, strt, bin, bint\n");
366+
shell_fprintf(sh, SHELL_ERROR, " [data] : Optional credential data (or use 'cred buf' first)\n");
367+
return -EINVAL;
368+
}
369+
360370
/* Lock credentials so that we can interact with them directly.
361371
* Mainly this is required by credential_get.
362372
*/
@@ -571,6 +581,14 @@ static int tls_cred_cmd_buf_load(const struct shell *sh, size_t argc, char *argv
571581
/* Buffers credential data into the credential buffer. */
572582
static int tls_cred_cmd_buf(const struct shell *sh, size_t argc, char *argv[])
573583
{
584+
if (argc < 2) {
585+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred buf <data>\n");
586+
shell_fprintf(sh, SHELL_ERROR, " <data> : Base64 encoded credential data to buffer\n");
587+
shell_fprintf(sh, SHELL_ERROR, "Or use: cred buf clear - Clear the credential buffer\n");
588+
shell_fprintf(sh, SHELL_ERROR, "Or use: cred buf load - Load credential interactively\n");
589+
return -EINVAL;
590+
}
591+
574592
/* Otherwise, assume provided arg is base64 and attempt to write it into the credential
575593
* buffer.
576594
*/
@@ -586,6 +604,13 @@ static int tls_cred_cmd_del(const struct shell *sh, size_t argc, char *argv[])
586604
struct tls_credential *cred = NULL;
587605
int ref_slot = -1;
588606

607+
if (argc < 3) {
608+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred del <sectag> <type>\n");
609+
shell_fprintf(sh, SHELL_ERROR, " <sectag> : Security tag\n");
610+
shell_fprintf(sh, SHELL_ERROR, " <type> : CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
611+
return -EINVAL;
612+
}
613+
589614
/* Lock credentials so that we can safely use internal access functions. */
590615
credentials_lock();
591616

@@ -655,6 +680,14 @@ static int tls_cred_cmd_get(const struct shell *sh, size_t argc, char *argv[])
655680

656681
size_t line_length;
657682

683+
if (argc < 4) {
684+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred get <sectag> <type> <format>\n");
685+
shell_fprintf(sh, SHELL_ERROR, " <sectag> : Security tag\n");
686+
shell_fprintf(sh, SHELL_ERROR, " <type> : CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
687+
shell_fprintf(sh, SHELL_ERROR, " <format> : str, strt, bin, bint\n");
688+
return -EINVAL;
689+
}
690+
658691
/* Lock credentials so that we can safely use internal access functions. */
659692
credentials_lock();
660693

@@ -778,6 +811,16 @@ static int tls_cred_cmd_list(const struct shell *sh, size_t argc, char *argv[])
778811
sec_tag_t sectag_filter = TLS_SEC_TAG_NONE;
779812
enum tls_credential_type type_filter = TLS_CREDENTIAL_NONE;
780813

814+
/* Show usage on explicit help request */
815+
if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
816+
shell_fprintf(sh, SHELL_NORMAL, "Usage: cred list [sectag] [type]\n");
817+
shell_fprintf(sh, SHELL_NORMAL, " [sectag] : Filter by security tag (or 'any' for all)\n");
818+
shell_fprintf(sh, SHELL_NORMAL, " [type] : Filter by credential type (or 'any' for all)\n");
819+
shell_fprintf(sh, SHELL_NORMAL, " Types: CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
820+
shell_fprintf(sh, SHELL_NORMAL, "\nOutput format: <sectag>,<type>,<digest>,<error>\n");
821+
return 0;
822+
}
823+
781824
/* Lock credentials so that we can safely use internal access functions. */
782825
credentials_lock();
783826

@@ -849,16 +892,16 @@ SHELL_STATIC_SUBCMD_SET_CREATE(tls_cred_buf_cmds,
849892

850893
SHELL_STATIC_SUBCMD_SET_CREATE(tls_cred_cmds,
851894
SHELL_CMD_ARG(buf, &tls_cred_buf_cmds, "Buffer in credential data so it can be added.",
852-
tls_cred_cmd_buf, 2, 0),
895+
tls_cred_cmd_buf, 0, 0),
853896
SHELL_CMD_ARG(add, NULL, "Add a TLS credential.",
854-
tls_cred_cmd_add, 5, 1),
897+
tls_cred_cmd_add, 0, 0),
855898
SHELL_CMD_ARG(del, NULL, "Delete a TLS credential.",
856-
tls_cred_cmd_del, 3, 0),
899+
tls_cred_cmd_del, 0, 0),
857900
SHELL_CMD_ARG(get, NULL, "Retrieve the contents of a TLS credential",
858-
tls_cred_cmd_get, 4, 0),
901+
tls_cred_cmd_get, 0, 0),
859902
SHELL_CMD_ARG(list, NULL, "List stored TLS credentials, optionally filtering by type "
860903
"or sectag.",
861-
tls_cred_cmd_list, 1, 2),
904+
tls_cred_cmd_list, 0, 0),
862905
SHELL_SUBCMD_SET_END
863906
);
864907

0 commit comments

Comments
 (0)