fix linux build failure
[mfgtools.git] / libuuu / cmd.h
blob1f0d1c93c9cdf8dedfed113349911f2bb7e18353
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #pragma once
34 #include <map>
35 #include <memory>
36 #include <string>
37 #include <vector>
39 class ConfigItem;
41 std::string get_next_param(const std::string &cmd, size_t &pos, char sperate = ' ');
43 class CmdCtx
45 public:
46 CmdCtx() = default;
47 CmdCtx(const CmdCtx&) = delete;
48 CmdCtx& operator=(const CmdCtx&) = delete;
49 virtual ~CmdCtx();
51 ConfigItem *m_config_item = nullptr;
52 void *m_dev = nullptr;
53 short m_current_bcd;
56 class CmdUsbCtx : public CmdCtx
58 public:
59 ~CmdUsbCtx() override;
60 int look_for_match_device(const char * procotol);
63 struct Param
65 enum class Type
67 e_uint32,
68 e_bool,
69 e_string,
70 e_null,
71 e_string_filename,
74 const char * const key;
75 const char * const Error;
76 void *pData;
77 const Type type;
78 const bool ignore_case;
79 Param(const char *ky, void *pD, Type tp, bool ignore = true, const char *error = nullptr) :
80 key{ky}, Error{error}, pData{pD}, type{tp}, ignore_case{ignore}
85 class CmdBase
87 public:
88 CmdBase() = default;
89 CmdBase(char *p) { if (p) m_cmd = p; }
90 virtual ~CmdBase();
92 virtual int dump();
93 const std::string& get_cmd() const noexcept { return m_cmd; }
94 bool get_lastcmd() const noexcept { return m_lastcmd; }
95 void insert_param_info(const char *key, void *pD, Param::Type tp, bool ignore_case = true, const char* err = nullptr)
97 m_param.emplace_back(Param{key, pD, tp, ignore_case, err});
99 virtual int parser_protocal(char *p, size_t &pos);
100 virtual int parser(char *p = nullptr);
101 virtual int run(CmdCtx *p) = 0;
103 protected:
104 bool m_bCheckTotalParam = false;
105 std::string m_cmd;
106 bool m_lastcmd = false;
107 bool m_NoKeyParam = false;
108 uint64_t m_timeout = 2000;
110 private:
111 std::vector<Param> m_param;
114 using CreateCmdObj = std::shared_ptr<CmdBase> (*) (char *);
116 class CmdObjCreateMap:public std::map<std::string, CreateCmdObj>
118 public:
119 CmdObjCreateMap();
122 class CmdDone :public CmdBase
124 public:
125 CmdDone(char *p) :CmdBase(p) { m_lastcmd = true; }
127 int run(CmdCtx *p) override;
130 class CmdDelay :public CmdBase
132 public:
133 CmdDelay(char *p) :CmdBase(p) {}
135 int parser(char *p = nullptr) override;
136 int run(CmdCtx *p) override;
138 private:
139 int m_ms = 0;
142 class CmdError : public CmdBase
144 public:
145 CmdError(char *p) :CmdBase(p) {}
146 int parser(char *p = nullptr) override;
147 int run(CmdCtx *p) override;
149 private:
150 std::string m_error;
153 class CmdShell : public CmdBase
155 public:
156 CmdShell(char *p) : CmdBase(p) {}
158 int parser(char *p = nullptr) override;
159 int run(CmdCtx *p) override;
161 private:
162 bool m_dyn = false;
163 std::string m_protocal;
164 std::string m_shellcmd;
167 class CmdIf : public CmdBase
169 public:
170 CmdIf(char *p) : CmdBase(p) {}
172 int parser(char *p = nullptr) override;
173 int run(CmdCtx *p) override;
175 private:
176 std::string m_condtion;
177 std::string m_protocal;
178 std::string m_true_cmd;
179 std::map <std::string, std::string> m_key_map;
180 void build_map(CmdCtx *p);
183 class CmdEnv : public CmdBase
185 public:
186 using CmdBase::CmdBase;
188 int parser(char *p = nullptr) override;
189 int run(CmdCtx *p) override;
191 private:
192 std::string m_unfold_cmd;
195 class CmdList : public std::vector<std::shared_ptr<CmdBase>>
197 public:
198 int run_all(CmdCtx *p, bool dry_run = false);
201 class CmdMap : public std::map<std::string, std::shared_ptr<CmdList>>
203 public:
204 int run_all(const std::string &protocal, CmdCtx *p, bool dry_run = false);
207 class CfgCmd :public CmdBase
209 public:
210 CfgCmd(char *cmd) :CmdBase(cmd) {}
212 int parser(char * /*p*/) override { return 0; }
213 int run(CmdCtx *p) override;
216 int run_cmds(const char *procotal, CmdCtx *p);
217 int run_cmd(CmdCtx *pCtx, const char * cmd, int dry);