3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <lib/shell/shell.h>
20 #include <lib/core/CHIPCore.h>
21 #include <lib/support/Base64.h>
22 #include <lib/support/CHIPArgParser.hpp>
23 #include <lib/support/CodeUtils.h>
24 #include <lib/support/RandUtils.h>
32 #include <ChipShellCollection.h>
35 using namespace chip::Shell;
36 using namespace chip::Logging;
37 using namespace chip::ArgParser;
39 chip::Shell::Shell theShellBase64;
41 int cmd_base64_help_iterator(shell_command_t * command, void * arg)
43 streamer_printf(streamer_get(), " %-15s %s\n\r", command->cmd_name, command->cmd_help);
47 int cmd_base64_help(int argc, char ** argv)
49 theShellBase64.ForEachCommand(cmd_base64_help_iterator, nullptr);
53 int cmd_base64_decode(int argc, char ** argv)
55 CHIP_ERROR error = CHIP_NO_ERROR;
56 streamer_t * sout = streamer_get();
60 VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT);
61 binarySize = Base64Decode(argv[0], strlen(argv[0]), binary);
62 streamer_print_hex(sout, binary, binarySize);
63 streamer_printf(sout, "\n\r");
69 int cmd_base64_encode(int argc, char ** argv)
71 CHIP_ERROR error = CHIP_NO_ERROR;
72 streamer_t * sout = streamer_get();
75 uint32_t binarySize, base64Size;
77 VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT);
78 ParseHexString(argv[0], strlen(argv[0]), binary, sizeof(binary), binarySize);
79 base64Size = Base64Encode(binary, binarySize, base64);
80 streamer_printf(sout, "%.*s\n\r", base64Size, base64);
86 int cmd_base64_dispatch(int argc, char ** argv)
88 CHIP_ERROR error = CHIP_NO_ERROR;
90 VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT);
92 error = theShellBase64.ExecCommand(argc, argv);
98 static const shell_command_t cmds_base64_root = { &cmd_base64_dispatch, "base64", "Base64 encode / decode utilities" };
100 /// Subcommands for root command: `base64 <subcommand>`
101 static const shell_command_t cmds_base64[] = {
102 { &cmd_base64_help, "help", "Usage: base64 <subcommand>" },
103 { &cmd_base64_encode, "encode", "Encode a hex sting as base64. Usage: base64 encode <hex_string>" },
104 { &cmd_base64_decode, "decode", "Decode a base64 sting as hex. Usage: base64 decode <base64_string>" },
107 void cmd_base64_init()
109 // Register `base64` subcommands with the local shell dispatcher.
110 theShellBase64.RegisterCommands(cmds_base64, ArraySize(cmds_base64));
112 // Register the root `base64` command with the top-level shell.
113 shell_register(&cmds_base64_root, 1);