Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / shell / shell_common / cmd_base64.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <lib/shell/shell.h>
19
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>
25
26 #include <inttypes.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <ChipShellCollection.h>
33
34 using namespace chip;
35 using namespace chip::Shell;
36 using namespace chip::Logging;
37 using namespace chip::ArgParser;
38
39 chip::Shell::Shell theShellBase64;
40
41 int cmd_base64_help_iterator(shell_command_t * command, void * arg)
42 {
43     streamer_printf(streamer_get(), "  %-15s %s\n\r", command->cmd_name, command->cmd_help);
44     return 0;
45 }
46
47 int cmd_base64_help(int argc, char ** argv)
48 {
49     theShellBase64.ForEachCommand(cmd_base64_help_iterator, nullptr);
50     return 0;
51 }
52
53 int cmd_base64_decode(int argc, char ** argv)
54 {
55     CHIP_ERROR error  = CHIP_NO_ERROR;
56     streamer_t * sout = streamer_get();
57     uint32_t binarySize;
58     uint8_t binary[256];
59
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");
64
65 exit:
66     return error;
67 }
68
69 int cmd_base64_encode(int argc, char ** argv)
70 {
71     CHIP_ERROR error  = CHIP_NO_ERROR;
72     streamer_t * sout = streamer_get();
73     char base64[256];
74     uint8_t binary[256];
75     uint32_t binarySize, base64Size;
76
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);
81
82 exit:
83     return error;
84 }
85
86 int cmd_base64_dispatch(int argc, char ** argv)
87 {
88     CHIP_ERROR error = CHIP_NO_ERROR;
89
90     VerifyOrExit(argc > 0, error = CHIP_ERROR_INVALID_ARGUMENT);
91
92     error = theShellBase64.ExecCommand(argc, argv);
93
94 exit:
95     return error;
96 }
97
98 static const shell_command_t cmds_base64_root = { &cmd_base64_dispatch, "base64", "Base64 encode / decode utilities" };
99
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>" },
105 };
106
107 void cmd_base64_init()
108 {
109     // Register `base64` subcommands with the local shell dispatcher.
110     theShellBase64.RegisterCommands(cmds_base64, ArraySize(cmds_base64));
111
112     // Register the root `base64` command with the top-level shell.
113     shell_register(&cmds_base64_root, 1);
114 }