1 /*
2  *  Copyright (c) 2016, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file contains definitions for the CLI interpreter.
32  */
33 
34 #ifndef CLI_DATASET_HPP_
35 #define CLI_DATASET_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <stdarg.h>
40 
41 #include <openthread/dataset.h>
42 
43 #include "cli/cli_utils.hpp"
44 
45 namespace ot {
46 namespace Cli {
47 
48 /**
49  * Implements the Dataset CLI interpreter.
50  *
51  */
52 class Dataset : private Utils
53 {
54 public:
Dataset(otInstance * aInstance,OutputImplementer & aOutputImplementer)55     Dataset(otInstance *aInstance, OutputImplementer &aOutputImplementer)
56         : Utils(aInstance, aOutputImplementer)
57     {
58     }
59 
60     /**
61      * Processes a CLI sub-command.
62      *
63      * @param[in]  aArgs     An array of command line arguments.
64      *
65      * @retval OT_ERROR_NONE              Successfully executed the CLI command.
66      * @retval OT_ERROR_PENDING           The CLI command was successfully started but final result is pending.
67      * @retval OT_ERROR_INVALID_COMMAND   Invalid or unknown CLI command.
68      * @retval OT_ERROR_INVALID_ARGS      Invalid arguments.
69      * @retval ...                        Error during execution of the CLI command.
70      *
71      */
72     otError Process(Arg aArgs[]);
73 
74 private:
75     using Command    = CommandEntry<Dataset>;
76     using Components = otOperationalDatasetComponents;
77 
78     struct ComponentMapper
79     {
Compareot::Cli::Dataset::ComponentMapper80         int Compare(const char *aName) const { return strcmp(aName, mName); }
81 
AreInOrderot::Cli::Dataset::ComponentMapper82         constexpr static bool AreInOrder(const ComponentMapper &aFirst, const ComponentMapper &aSecond)
83         {
84             return AreStringsInOrder(aFirst.mName, aSecond.mName);
85         }
86 
87         const char *mName;
88         bool Components::*mIsPresentPtr;
89         void (Dataset::*mOutput)(const otOperationalDataset &aDataset);
90         otError (Dataset::*mParse)(Arg *&aArgs, otOperationalDataset &aDataset);
91     };
92 
93     const ComponentMapper *LookupMapper(const char *aName) const;
94 
95     void OutputActiveTimestamp(const otOperationalDataset &aDataset);
96     void OutputChannel(const otOperationalDataset &aDataset);
97     void OutputChannelMask(const otOperationalDataset &aDataset);
98     void OutputDelay(const otOperationalDataset &aDataset);
99     void OutputExtendedPanId(const otOperationalDataset &aDataset);
100     void OutputMeshLocalPrefix(const otOperationalDataset &aDataset);
101     void OutputNetworkKey(const otOperationalDataset &aDataset);
102     void OutputNetworkName(const otOperationalDataset &aDataset);
103     void OutputPanId(const otOperationalDataset &aDataset);
104     void OutputPendingTimestamp(const otOperationalDataset &aDataset);
105     void OutputPskc(const otOperationalDataset &aDataset);
106     void OutputSecurityPolicy(const otOperationalDataset &aDataset);
107 
108     otError ParseActiveTimestamp(Arg *&aArgs, otOperationalDataset &aDataset);
109     otError ParseChannel(Arg *&aArgs, otOperationalDataset &aDataset);
110     otError ParseChannelMask(Arg *&aArgs, otOperationalDataset &aDataset);
111     otError ParseDelay(Arg *&aArgs, otOperationalDataset &aDataset);
112     otError ParseExtendedPanId(Arg *&aArgs, otOperationalDataset &aDataset);
113     otError ParseMeshLocalPrefix(Arg *&aArgs, otOperationalDataset &aDataset);
114     otError ParseNetworkKey(Arg *&aArgs, otOperationalDataset &aDataset);
115     otError ParseNetworkName(Arg *&aArgs, otOperationalDataset &aDataset);
116     otError ParsePanId(Arg *&aArgs, otOperationalDataset &aDataset);
117     otError ParsePendingTimestamp(Arg *&aArgs, otOperationalDataset &aDataset);
118     otError ParsePskc(Arg *&aArgs, otOperationalDataset &aDataset);
119     otError ParseSecurityPolicy(Arg *&aArgs, otOperationalDataset &aDataset);
120 
121     otError ParseTlvs(Arg &aArg, otOperationalDatasetTlvs &aDatasetTlvs);
122 
123     otError ProcessCommand(const ComponentMapper &aMapper, Arg aArgs[]);
124 
125     template <CommandId kCommandId> otError Process(Arg aArgs[]);
126 
127     otError Print(otOperationalDatasetTlvs &aDatasetTlvs);
128 
129 #if OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE && OPENTHREAD_FTD
130     otError     ProcessUpdater(Arg aArgs[]);
131     static void HandleDatasetUpdater(otError aError, void *aContext);
132     void        HandleDatasetUpdater(otError aError);
133 #endif
134 
135     void    OutputSecurityPolicy(const otSecurityPolicy &aSecurityPolicy);
136     otError ParseSecurityPolicy(otSecurityPolicy &aSecurityPolicy, Arg *&aArgs);
137 
138     static otOperationalDatasetTlvs sDatasetTlvs;
139 };
140 
141 } // namespace Cli
142 } // namespace ot
143 
144 #endif // CLI_DATASET_HPP_
145