uuu
uuu (Universal Update Utility), mfgtools 3.0
Loading...
Searching...
No Matches
cmd.h
Go to the documentation of this file.
1/*
2* Copyright 2018 NXP.
3*
4* Redistribution and use in source and binary forms, with or without modification,
5* are permitted provided that the following conditions are met:
6*
7* Redistributions of source code must retain the above copyright notice, this
8* list of conditions and the following disclaimer.
9*
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.
13*
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.
17*
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.
29*
30*/
31
32#pragma once
33
34#include <map>
35#include <memory>
36#include <string>
37#include <vector>
38
39class ConfigItem;
40
41std::string get_next_param(const std::string &cmd, size_t &pos, char separate = ' ');
42
43class CmdCtx
44{
45public:
46 CmdCtx() = default;
47 CmdCtx(const CmdCtx&) = delete;
48 CmdCtx& operator=(const CmdCtx&) = delete;
49 virtual ~CmdCtx();
50
52 void *m_dev = nullptr;
54};
55
56class CmdUsbCtx : public CmdCtx
57{
58public:
59 ~CmdUsbCtx() override;
60 int look_for_match_device(const char * protocol);
61};
62
63struct Param
64{
74
75 const char * const key;
76 const char * const Error;
77 void *pData;
78 const Type type;
79 const bool ignore_case;
80 Param(const char *ky, void *pD, Type tp, bool ignore = true, const char *error = nullptr) :
81 key{ky}, Error{error}, pData{pD}, type{tp}, ignore_case{ignore}
82 {
83 }
84};
85
87{
88public:
89 CmdBase() = default;
90 CmdBase(char *p) { if (p) m_cmd = p; }
91 virtual ~CmdBase();
92
93 virtual int dump();
94 const std::string& get_cmd() const noexcept { return m_cmd; }
95 bool get_lastcmd() const noexcept { return m_lastcmd; }
96 void insert_param_info(const char *key, void *pD, Param::Type tp, bool ignore_case = true, const char* err = nullptr)
97 {
98 m_param.emplace_back(Param{key, pD, tp, ignore_case, err});
99 }
100 virtual int parser_protocol(char *p, size_t &pos);
101 virtual int parser(char *p = nullptr);
102 virtual int run(CmdCtx *p) = 0;
103
104protected:
105 bool m_bCheckTotalParam = false;
106 std::string m_cmd;
107 bool m_lastcmd = false;
108 bool m_NoKeyParam = false;
109 int m_timeout = 10000;
110
111private:
112 std::vector<Param> m_param;
113};
114
115using CreateCmdObj = std::shared_ptr<CmdBase> (*) (char *);
116
117class CmdObjCreateMap:public std::map<std::string, CreateCmdObj>
118{
119public:
121};
122
123class CmdDone :public CmdBase
124{
125public:
126 CmdDone(char *p) :CmdBase(p) { m_lastcmd = true; }
127
128 int run(CmdCtx *p) override;
129};
130
131class CmdDelay :public CmdBase
132{
133public:
134 CmdDelay(char *p) :CmdBase(p) {}
135
136 int parser(char *p = nullptr) override;
137 int run(CmdCtx *p) override;
138
139private:
140 int m_ms = 0;
141};
142
143class CmdError : public CmdBase
144{
145public:
146 CmdError(char *p) :CmdBase(p) {}
147 int parser(char *p = nullptr) override;
148 int run(CmdCtx *p) override;
149
150private:
151 std::string m_error;
152};
153
154class CmdShell : public CmdBase
155{
156public:
157 CmdShell(char *p) : CmdBase(p) {}
158
159 int parser(char *p = nullptr) override;
160 int run(CmdCtx *p) override;
161
162private:
163 bool m_dyn = false;
164 std::string m_protocol;
165 std::string m_shellcmd;
166};
167
168class CmdIf : public CmdBase
169{
170public:
171 CmdIf(char *p) : CmdBase(p) {}
172
173 int parser(char *p = nullptr) override;
174 int run(CmdCtx *p) override;
175
176private:
177 std::string m_condition;
178 std::string m_protocol;
179 std::string m_true_cmd;
180 void build_map(CmdCtx *p);
181};
182
183class CmdEnv : public CmdBase
184{
185public:
186 using CmdBase::CmdBase;
187
188 int parser(char *p = nullptr) override;
189 int run(CmdCtx *p) override;
190
191private:
192 std::string m_unfold_cmd;
193};
194
195class CmdList : public std::vector<std::shared_ptr<CmdBase>>
196{
197public:
198 int run_all(CmdCtx *p, bool dry_run = false);
199};
200
201class CmdMap : public std::map<std::string, std::shared_ptr<CmdList>>
202{
203public:
204 int run_all(const std::string &protocol, CmdCtx *p, bool dry_run = false);
205};
206
207class CfgCmd :public CmdBase
208{
209public:
210 CfgCmd(char *cmd) :CmdBase(cmd) {}
211
212 int parser(char * /*p*/) override { return 0; }
213 int run(CmdCtx *p) override;
214};
215
216int run_cmds(const char *protocol, CmdCtx *p);
217int run_cmd(CmdCtx *pCtx, const char * cmd, int dry);
218
219int insert_env_variable(std::string key, std::string value);
220std::string get_env_variable(std::string key);
221int clear_env();
222bool is_evn_exist(std::string key);
int run(CmdCtx *p) override
Definition config.cpp:124
int parser(char *) override
Definition cmd.h:212
CfgCmd(char *cmd)
Definition cmd.h:210
bool m_bCheckTotalParam
Definition cmd.h:105
std::vector< Param > m_param
Definition cmd.h:112
bool m_NoKeyParam
Definition cmd.h:108
virtual int parser(char *p=nullptr)
Definition cmd.cpp:99
int m_timeout
Definition cmd.h:109
virtual ~CmdBase()
Definition cmd.cpp:95
bool m_lastcmd
Definition cmd.h:107
const std::string & get_cmd() const noexcept
Definition cmd.h:94
virtual int run(CmdCtx *p)=0
CmdBase(char *p)
Definition cmd.h:90
std::string m_cmd
Definition cmd.h:106
virtual int parser_protocol(char *p, size_t &pos)
Definition cmd.cpp:205
virtual int dump()
Definition cmd.cpp:240
CmdBase()=default
void insert_param_info(const char *key, void *pD, Param::Type tp, bool ignore_case=true, const char *err=nullptr)
Definition cmd.h:96
bool get_lastcmd() const noexcept
Definition cmd.h:95
Definition cmd.h:44
CmdCtx()=default
virtual ~CmdCtx()
Definition cmd.cpp:91
void * m_dev
Definition cmd.h:52
CmdCtx(const CmdCtx &)=delete
ConfigItem * m_config_item
Definition cmd.h:51
CmdCtx & operator=(const CmdCtx &)=delete
short m_current_bcd
Definition cmd.h:53
int m_ms
Definition cmd.h:140
CmdDelay(char *p)
Definition cmd.h:134
int parser(char *p=nullptr) override
Definition cmd.cpp:608
int run(CmdCtx *p) override
Definition cmd.cpp:629
CmdDone(char *p)
Definition cmd.h:126
int run(CmdCtx *p) override
Definition cmd.cpp:600
Definition cmd.h:184
std::string m_unfold_cmd
Definition cmd.h:192
int parser(char *p=nullptr) override
Definition cmd.cpp:743
int run(CmdCtx *p) override
Definition cmd.cpp:915
CmdBase()=default
int parser(char *p=nullptr) override
Definition cmd.cpp:635
std::string m_error
Definition cmd.h:151
CmdError(char *p)
Definition cmd.h:146
int run(CmdCtx *p) override
Definition cmd.cpp:653
void build_map(CmdCtx *p)
Definition cmd.cpp:850
std::string m_condition
Definition cmd.h:177
CmdIf(char *p)
Definition cmd.h:171
std::string m_protocol
Definition cmd.h:178
int parser(char *p=nullptr) override
Definition cmd.cpp:809
std::string m_true_cmd
Definition cmd.h:179
int run(CmdCtx *p) override
Definition cmd.cpp:867
Definition cmd.h:196
int run_all(CmdCtx *p, bool dry_run=false)
Definition cmd.cpp:253
Definition cmd.h:202
int run_all(const std::string &protocol, CmdCtx *p, bool dry_run=false)
Definition cmd.cpp:294
CmdObjCreateMap()
Definition cmd.cpp:420
std::string m_protocol
Definition cmd.h:164
bool m_dyn
Definition cmd.h:163
int run(CmdCtx *p) override
Definition cmd.cpp:682
CmdShell(char *p)
Definition cmd.h:157
std::string m_shellcmd
Definition cmd.h:165
int parser(char *p=nullptr) override
Definition cmd.cpp:659
Definition cmd.h:57
~CmdUsbCtx() override
Definition usbhotplug.cpp:510
int look_for_match_device(const char *protocol)
Definition usbhotplug.cpp:519
Definition config.h:39
int run_cmds(const char *protocol, CmdCtx *p)
Definition cmd.cpp:920
int clear_env()
Definition cmd.cpp:70
int insert_env_variable(std::string key, std::string value)
Definition cmd.cpp:59
bool is_evn_exist(std::string key)
std::shared_ptr< CmdBase >(*)(char *) CreateCmdObj
Definition cmd.h:115
std::string get_env_variable(std::string key)
Definition cmd.cpp:65
std::string get_next_param(const std::string &cmd, size_t &pos, char separate=' ')
Definition cmd.cpp:308
int run_cmd(CmdCtx *pCtx, const char *cmd, int dry)
Definition cmd.cpp:538
Definition cmd.h:64
void * pData
Definition cmd.h:77
Param(const char *ky, void *pD, Type tp, bool ignore=true, const char *error=nullptr)
Definition cmd.h:80
const char *const key
Definition cmd.h:75
const Type type
Definition cmd.h:78
const char *const Error
Definition cmd.h:76
Type
Definition cmd.h:66
@ e_string_filename
Definition cmd.h:72
@ e_uint32
Definition cmd.h:67
@ e_null
Definition cmd.h:71
@ e_string
Definition cmd.h:70
@ e_bool
Definition cmd.h:69
@ e_uint64
Definition cmd.h:68
const bool ignore_case
Definition cmd.h:79
auto pos
Definition usbhotplug.cpp:159