Dawn Framework 1.0
Universal data acquisition framework for embedded systems
pretty.hxx
1// dawn/include/dawn/proto/shell/pretty.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <map>
9#include <string>
10
11#include "dawn/common/thread.hxx"
12#include "dawn/porting/config.hxx"
13#include "dawn/proto/common.hxx"
14
15namespace dawn
16{
17// Forward declaration
18
19class IIOCommon;
20class CIOCommon;
21struct io_ddata_t;
22
31 : public CProtoCommon
32 , protected CThreadedObject
33{
34public:
35 static int inst;
36 static FILE *outstream_;
37
38 enum
39 {
40 PROTO_SHELL_CFG_FIRST = 0,
44 PROTO_SHELL_CFG_LAST = 31
45 } typedef EProtoShellCfg;
46
47 enum
48 {
49 PROTO_SHELL_CMDID_INVAL = 0,
50 PROTO_SHELL_CMDID_HELP,
51 PROTO_SHELL_CMDID_EXIT,
52 PROTO_SHELL_CMDID_INFO,
53 PROTO_SHELL_CMDID_GETIO,
54#ifdef CONFIG_DAWN_IO_NOTIFY
55 PROTO_SHELL_CMDID_GETIONOTIFY,
56#endif
57 PROTO_SHELL_CMDID_GETIOLOOP,
58 PROTO_SHELL_CMDID_SETIO,
59 PROTO_SHELL_CMDID_SETCFG,
60 PROTO_SHELL_CMDID_GETCFG,
61#ifdef CONFIG_DAWN_PROTO_SHELL_INSPECT
62 PROTO_SHELL_CMDID_LIST,
63 PROTO_SHELL_CMDID_INSPECT,
64 PROTO_SHELL_CMDID_TREE,
65 PROTO_SHELL_CMDID_STATS,
66#endif
67 } typedef EProtoShellCmdId;
68
69 static_assert(PROTO_SHELL_CFG_LAST - 1 <= SObjectCfg::ID_MAX);
70
71 struct
72 {
73 SObjectId::UObjectId objid;
74 } typedef SProtoShellIOBind;
75
76 explicit CProtoShellPretty(CDescObject &desc)
77 : CProtoCommon(desc)
78 , instream(nullptr)
79 , outstream(nullptr)
80 {
81 }
82
83 ~CProtoShellPretty() override;
84
85 CProtoShellPretty(const CProtoShellPretty &) = delete;
86 CProtoShellPretty &operator=(const CProtoShellPretty &) = delete;
87
88#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
89 const char *getClassNameStr() const override
90 {
91 return "shell";
92 }
93#endif
94
95 int configure() override;
96 int doStart() override;
97 int doStop() override;
98 bool hasThread() const override;
99
100 constexpr static SObjectId::ObjectId objectId(uint16_t id)
101 {
102 return SObjectId::objectId(
104 }
105
106 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint8_t dtype, uint8_t size, uint8_t id)
107 {
110 }
111
112 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size)
113 {
114 return CProtoShellPretty::cfgId(false, SObjectId::DTYPE_ANY, size, PROTO_SHELL_CFG_IOBIND);
115 }
116
117 constexpr static SObjectCfg::ObjectCfgId cfgIdPath(uint16_t size)
118 {
119 return CProtoShellPretty::cfgId(true, SObjectId::DTYPE_CHAR, size, PROTO_SHELL_CFG_PATH);
120 }
121
122 constexpr static SObjectCfg::ObjectCfgId cfgIdPrompt(uint16_t size)
123 {
124 return CProtoShellPretty::cfgId(true, SObjectId::DTYPE_CHAR, size, PROTO_SHELL_CFG_PROMPT);
125 }
126
127private:
128 const char *promptDefault = "dsh> ";
129 const char *prompt = promptDefault;
130 const char *path = nullptr;
131 FILE *instream;
132 FILE *outstream;
133
134 std::map<std::string, EProtoShellCmdId> cmdmap = {
135 {"help", PROTO_SHELL_CMDID_HELP},
136 {"exit", PROTO_SHELL_CMDID_EXIT},
137 {"info", PROTO_SHELL_CMDID_INFO},
138 {"getio", PROTO_SHELL_CMDID_GETIO},
139 {"setio", PROTO_SHELL_CMDID_SETIO},
140#ifdef CONFIG_DAWN_IO_NOTIFY
141 {"getionotify", PROTO_SHELL_CMDID_GETIONOTIFY},
142#endif
143 {"getioloop", PROTO_SHELL_CMDID_GETIOLOOP},
144 {"setcfg", PROTO_SHELL_CMDID_SETCFG},
145 {"getcfg", PROTO_SHELL_CMDID_GETCFG},
146#ifdef CONFIG_DAWN_PROTO_SHELL_INSPECT
147 {"list", PROTO_SHELL_CMDID_LIST},
148 {"inspect", PROTO_SHELL_CMDID_INSPECT},
149 {"tree", PROTO_SHELL_CMDID_TREE},
150 {"stats", PROTO_SHELL_CMDID_STATS},
151#endif
152 };
153
154#ifdef CONFIG_DAWN_IO_NOTIFY
155 static int notifierCb(void *priv, io_ddata_t *data);
156#endif
157
158 void setPrompt(const char *p);
159 void getAndPrint(CIOCommon *io) const;
160 void getAndPrintSeek(CIOCommon *io) const;
161 void getcfgAndPrint(CObject *io) const;
162
163 void cmdHelp();
164 void cmdExit();
165 void cmdInfo();
166 void cmdGetio(const char *arg);
167 void cmdGetioloop(const char *arg);
168
169#ifdef CONFIG_DAWN_IO_NOTIFY
170 void cmdGetioNotify(const char *arg);
171#endif
172
173 void cmdSetio(const char *arg);
174 void cmdSetcfg(const char *arg);
175 void cmdGetcfg(const char *arg);
176
177#ifdef CONFIG_DAWN_PROTO_SHELL_INSPECT
178 void cmdList(const char *arg);
179 void cmdInspect(const char *arg);
180 void cmdTree(const char *arg);
181 void cmdStats(const char *arg);
182#endif
183
184 void cmdHandle(char *buffer, const char *arg);
185 int configureDesc(const CDescObject &desc);
186 void allocObject(const SObjectId::UObjectId &obj);
187 void thread();
188};
189} // Namespace dawn
Base class for all protocol implementations.
Definition common.hxx:23
CProtoCommon(CDescObject &desc)
Constructor.
Definition common.cxx:12
@ PROTO_CLASS_SHELL_STD
Interactive shell on stdin/stdout.
Definition common.hxx:55
Interactive Command-Line Shell Interface.
Definition pretty.hxx:33
int doStart()
Start implementation hook.
Definition pretty.cxx:1198
@ PROTO_SHELL_CFG_PATH
Shell device path (TTY/serial).
Definition pretty.hxx:42
@ PROTO_SHELL_CFG_PROMPT
Custom prompt string.
Definition pretty.hxx:43
@ PROTO_SHELL_CFG_IOBIND
I/O object binding configuration.
Definition pretty.hxx:41
static int inst
Global instance counter for multiple shell instances.
Definition pretty.hxx:35
int doStop()
Stop implementation hook.
Definition pretty.cxx:1203
static FILE * outstream_
Global output stream for shell messages.
Definition pretty.hxx:36
bool hasThread() const
Check if a background thread is active.
Definition pretty.cxx:1208
int configure()
Configure object from descriptor data.
Definition pretty.cxx:1139
Portable thread owner abstraction for Dawn components.
Definition thread.hxx:32
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
static size_t ID_MAX
Configuration ID field constants and bit shift positions.
Definition objectcfg.hxx:45
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
@ OBJTYPE_PROTO
Protocol object type.
Definition objectid.hxx:193
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
@ DTYPE_CHAR
Character/string type (null-terminated, 4-byte aligned).
Definition objectid.hxx:144
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290