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.
45 string
get_next_param(string
&cmd
, size_t &pos
, char sperate
= ' ');
46 string
remove_square_brackets(string
&cmd
);
47 int get_string_in_square_brackets(string
&cmd
, string
&context
);
48 uint32_t str_to_uint(string
&str
);
53 CmdCtx() { m_config_item
= NULL
; m_dev
= NULL
; };
55 ConfigItem
*m_config_item
;
59 class CmdUsbCtx
: public CmdCtx
62 CmdUsbCtx() :CmdCtx() {};
64 int look_for_match_device(const char * procotol
);
82 Param(const char *ky
, void *pD
, Param_Type tp
, bool ignore
=true)
84 key
= ky
; pData
= pD
; type
= tp
; ignore_case
= ignore
;
91 vector
<Param
> m_param
;
96 void CmdBaseInit() { m_timeout
= 2000; m_lastcmd
= false;}
97 CmdBase() { CmdBaseInit(); };
98 CmdBase(char *p
) { CmdBaseInit(); if (p
) m_cmd
= p
; }
100 void insert_param_info(const char *key
, void *pD
, Param::Param_Type tp
, bool ignore_case
= true)
102 m_param
.push_back(Param(key
, pD
, tp
, ignore_case
));
105 virtual int parser_protocal(char *p
, size_t &pos
)
110 string prot
= get_next_param(m_cmd
, pos
, ':');
112 if (get_string_in_square_brackets(prot
, param
))
117 size_t param_pos
= 0;
118 string s
= get_next_param(param
, param_pos
);
123 timeout
= get_next_param(param
, param_pos
);
124 m_timeout
= str_to_uint(timeout
);
129 err
= "Unknown option: ";
131 err
+= " for protocol: ";
132 err
+= remove_square_brackets(prot
);
133 set_last_err_string(err
);
139 virtual int parser(char *p
= NULL
);
140 virtual int run(CmdCtx
*p
)=0;
144 typedef shared_ptr
<CmdBase
> (*CreateCmdObj
) (char *);
146 class CmdObjCreateMap
:public map
<string
, CreateCmdObj
>
152 class CmdDone
:public CmdBase
155 CmdDone(char *p
) :CmdBase(p
) { m_lastcmd
= true; };
159 class CmdDelay
:public CmdBase
163 virtual int parser(char *p
= NULL
);
164 CmdDelay(char *p
) :CmdBase(p
) { m_ms
= 0; };
168 class CmdShell
: public CmdBase
175 CmdShell(char *p
) : CmdBase(p
) { m_dyn
= false; };
176 virtual int parser(char *p
= NULL
);
180 class CmdList
: public std::vector
<shared_ptr
<CmdBase
>>
183 int run_all(CmdCtx
*p
, bool dry_run
= false);
186 class CmdMap
: public std::map
<std::string
, shared_ptr
<CmdList
>>
189 int run_all(std::string protocal
, CmdCtx
*p
, bool dry_run
= false)
191 if (find(protocal
) == end())
195 err
.append("Unknown Protocal:");
196 err
.append(protocal
);
197 set_last_err_string(err
);
200 return at(protocal
)->run_all(p
, dry_run
);
204 class CfgCmd
:public CmdBase
207 int parser(char * /*p*/) { return 0; };
208 CfgCmd(char *cmd
) :CmdBase(cmd
) {};
212 int run_cmds(const char *procotal
, CmdCtx
*p
);