Move functions from sdp.h to sdp.cpp
[mfgtools.git] / libuuu / cmd.h
blob1b39c70437b02886c3cff08c71a1fafda47ab8e5
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 <string>
35 #include <vector>
36 #include <map>
37 #include <memory>
39 #include "liberror.h"
40 #include "libcomm.h"
41 #include "config.h"
43 using namespace std;
45 string get_next_param(const string &cmd, size_t &pos, char sperate = ' ');
46 string remove_square_brackets(const string &cmd);
47 int get_string_in_square_brackets(const string &cmd, string &context);
49 class CmdCtx
51 public:
52 virtual ~CmdCtx();
53 ConfigItem *m_config_item = nullptr;
54 void *m_dev = nullptr;
57 class CmdUsbCtx : public CmdCtx
59 public:
60 ~CmdUsbCtx() override;
61 int look_for_match_device(const char * procotol);
64 struct Param
66 enum class Type
68 e_uint32,
69 e_bool,
70 e_string,
71 e_null,
72 e_string_filename,
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}
86 class CmdBase
88 public:
89 vector<Param> m_param;
90 uint64_t m_timeout = 2000;
91 bool m_lastcmd = false;
92 std::string m_cmd;
93 bool m_NoKeyParam = false;
94 bool m_bCheckTotalParam = false;
96 CmdBase() = default;
97 CmdBase(char *p) { if (p) m_cmd = p; }
98 virtual ~CmdBase();
100 void insert_param_info(const char *key, void *pD, Param::Type tp, bool ignore_case = true, const char* err = nullptr)
102 m_param.emplace_back(Param{key, pD, tp, ignore_case, err});
105 virtual int parser_protocal(char *p, size_t &pos);
106 virtual int parser(char *p = nullptr);
107 virtual int run(CmdCtx *p) = 0;
108 virtual int dump();
111 using CreateCmdObj = shared_ptr<CmdBase> (*) (char *);
113 class CmdObjCreateMap:public map<string, CreateCmdObj>
115 public:
116 CmdObjCreateMap();
119 class CmdDone :public CmdBase
121 public:
122 CmdDone(char *p) :CmdBase(p) { m_lastcmd = true; }
123 int run(CmdCtx *p) override;
126 class CmdDelay :public CmdBase
128 public:
129 int m_ms = 0;
130 CmdDelay(char *p) :CmdBase(p) {}
131 int parser(char *p = nullptr) override;
132 int run(CmdCtx *p) override;
135 class CmdShell : public CmdBase
137 public:
138 string m_shellcmd;
139 string m_protocal;
140 bool m_dyn = false;
142 CmdShell(char *p) : CmdBase(p) {}
143 int parser(char *p = nullptr) override;
144 int run(CmdCtx *p) override;
147 class CmdList : public std::vector<shared_ptr<CmdBase>>
149 public:
150 int run_all(CmdCtx *p, bool dry_run = false);
153 class CmdMap : public std::map<std::string, shared_ptr<CmdList>>
155 public:
156 int run_all(const std::string &protocal, CmdCtx *p, bool dry_run = false);
159 class CfgCmd :public CmdBase
161 public:
162 CfgCmd(char *cmd) :CmdBase(cmd) {}
163 int parser(char * /*p*/) override { return 0; }
164 int run(CmdCtx *p) override;
167 int run_cmds(const char *procotal, CmdCtx *p);