Skip to content

Commit 9c53822

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 9c53822

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

subsys/net/lib/tls_credentials/tls_credentials_shell.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,19 @@ 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,
362+
"Usage: cred add <sectag> <type> <backend> <format> [data]\n");
363+
shell_fprintf(sh, SHELL_ERROR, " <sectag> : Security tag\n");
364+
shell_fprintf(sh, SHELL_ERROR,
365+
" <type> : CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
366+
shell_fprintf(sh, SHELL_ERROR, " <backend> : default\n");
367+
shell_fprintf(sh, SHELL_ERROR, " <format> : str, strt, bin, bint\n");
368+
shell_fprintf(sh, SHELL_ERROR,
369+
" [data] : Optional credential data (or use 'cred buf' first)\n");
370+
return -EINVAL;
371+
}
372+
360373
/* Lock credentials so that we can interact with them directly.
361374
* Mainly this is required by credential_get.
362375
*/
@@ -571,6 +584,17 @@ static int tls_cred_cmd_buf_load(const struct shell *sh, size_t argc, char *argv
571584
/* Buffers credential data into the credential buffer. */
572585
static int tls_cred_cmd_buf(const struct shell *sh, size_t argc, char *argv[])
573586
{
587+
if (argc < 2) {
588+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred buf <data>\n");
589+
shell_fprintf(sh, SHELL_ERROR,
590+
" <data> : Base64 encoded credential data to buffer\n");
591+
shell_fprintf(sh, SHELL_ERROR,
592+
"Or use: cred buf clear - Clear the credential buffer\n");
593+
shell_fprintf(sh, SHELL_ERROR,
594+
"Or use: cred buf load - Load credential interactively\n");
595+
return -EINVAL;
596+
}
597+
574598
/* Otherwise, assume provided arg is base64 and attempt to write it into the credential
575599
* buffer.
576600
*/
@@ -586,6 +610,14 @@ static int tls_cred_cmd_del(const struct shell *sh, size_t argc, char *argv[])
586610
struct tls_credential *cred = NULL;
587611
int ref_slot = -1;
588612

613+
if (argc < 3) {
614+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred del <sectag> <type>\n");
615+
shell_fprintf(sh, SHELL_ERROR, " <sectag> : Security tag\n");
616+
shell_fprintf(sh, SHELL_ERROR,
617+
" <type> : CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
618+
return -EINVAL;
619+
}
620+
589621
/* Lock credentials so that we can safely use internal access functions. */
590622
credentials_lock();
591623

@@ -655,6 +687,15 @@ static int tls_cred_cmd_get(const struct shell *sh, size_t argc, char *argv[])
655687

656688
size_t line_length;
657689

690+
if (argc < 4) {
691+
shell_fprintf(sh, SHELL_ERROR, "Usage: cred get <sectag> <type> <format>\n");
692+
shell_fprintf(sh, SHELL_ERROR, " <sectag> : Security tag\n");
693+
shell_fprintf(sh, SHELL_ERROR,
694+
" <type> : CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
695+
shell_fprintf(sh, SHELL_ERROR, " <format> : str, strt, bin, bint\n");
696+
return -EINVAL;
697+
}
698+
658699
/* Lock credentials so that we can safely use internal access functions. */
659700
credentials_lock();
660701

@@ -778,6 +819,21 @@ static int tls_cred_cmd_list(const struct shell *sh, size_t argc, char *argv[])
778819
sec_tag_t sectag_filter = TLS_SEC_TAG_NONE;
779820
enum tls_credential_type type_filter = TLS_CREDENTIAL_NONE;
780821

822+
/* Show usage on explicit help request */
823+
if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) {
824+
shell_fprintf(sh, SHELL_NORMAL, "Usage: cred list [sectag] [type]\n");
825+
shell_fprintf(sh, SHELL_NORMAL,
826+
" [sectag] : Filter by security tag (or 'any' for all)\n");
827+
shell_fprintf(sh, SHELL_NORMAL,
828+
" [type] : Filter by credential type (or 'any' for all)\n");
829+
shell_fprintf(
830+
sh, SHELL_NORMAL,
831+
" Types: CA_CERT, SERVER_CERT, PRIVATE_KEY, PSK, PSK_ID\n");
832+
shell_fprintf(sh, SHELL_NORMAL,
833+
"\nOutput format: <sectag>,<type>,<digest>,<error>\n");
834+
return 0;
835+
}
836+
781837
/* Lock credentials so that we can safely use internal access functions. */
782838
credentials_lock();
783839

@@ -849,16 +905,16 @@ SHELL_STATIC_SUBCMD_SET_CREATE(tls_cred_buf_cmds,
849905

850906
SHELL_STATIC_SUBCMD_SET_CREATE(tls_cred_cmds,
851907
SHELL_CMD_ARG(buf, &tls_cred_buf_cmds, "Buffer in credential data so it can be added.",
852-
tls_cred_cmd_buf, 2, 0),
908+
tls_cred_cmd_buf, 0, 0),
853909
SHELL_CMD_ARG(add, NULL, "Add a TLS credential.",
854-
tls_cred_cmd_add, 5, 1),
910+
tls_cred_cmd_add, 0, 0),
855911
SHELL_CMD_ARG(del, NULL, "Delete a TLS credential.",
856-
tls_cred_cmd_del, 3, 0),
912+
tls_cred_cmd_del, 0, 0),
857913
SHELL_CMD_ARG(get, NULL, "Retrieve the contents of a TLS credential",
858-
tls_cred_cmd_get, 4, 0),
914+
tls_cred_cmd_get, 0, 0),
859915
SHELL_CMD_ARG(list, NULL, "List stored TLS credentials, optionally filtering by type "
860916
"or sectag.",
861-
tls_cred_cmd_list, 1, 2),
917+
tls_cred_cmd_list, 0, 0),
862918
SHELL_SUBCMD_SET_END
863919
);
864920

0 commit comments

Comments
 (0)