ngc 1.4.0
 
Loading...
Searching...
No Matches
ngcbCMD.hpp
Go to the documentation of this file.
1
7#ifndef ngcbCMD_H
8#define ngcbCMD_H
9
10
11#ifndef __cplusplus
12#error This is a C++ include file and cannot be used from plain C
13#endif
14
15#include <ngc/core/ngcb.h>
16
20#define ngcbCMD_LIST_SIZE 256
21
22/*
23 * Global command flags
24 */
25#define ngcbCMD_UPDATE 0x00000001 //< command requires system update
26#define ngcbCMD_PROTECTED 0x00000002 //< protected command
27#define ngcbCMD_PROTECT 0x00000004 //< enforce command protection
28#define ngcbCMD_INTERNAL 0x00000008 //< internal use only
29#define ngcbCMD_DISCARDED 0x00000010 //< command is declared dsicarded
30
31/*
32 * Special component identifiers
33 */
34#define ngcbCMD_ID_IVLD -1 //< id invalid
35#define ngcbCMD_ID_NOT_SET -2 //< id not set
36
41{
42public:
44 int flags;
45
47 void *udata;
48
50 int done;
51
53 int fd;
54
57 param_[0] = '\0';
58 filter_[0] = '\0';
59 flags = 0x0;
60 udata = NULL;
61 done = 0;
62 fd = -1; // not set
63 id_ = ngcbCMD_ID_NOT_SET; // not set
64 }
65
67 virtual ~ngcb_cmd_handle_t() {}
68
70 int Id() {return (id_);}
71
73 int Id(const char *filter) {
74 if ((id_ != ngcbCMD_ID_NOT_SET) && (strcmp(filter, filter_) == 0))
75 {
76 /*
77 * The id is already stored - even if it was invalid
78 */
79 return (id_);
80 }
81 else if ((strlen(param_) > 0) && (strlen(filter) < sizeof(filter_)))
82 {
83 char s[1024];
84 char format[64];
85 int n;
86 strcpy(format, " ");
87 strcat(format, filter);
88 strcat(format, " %d %1023[^\n]");
89 s[0] = '\0';
90 n = sscanf(param_, format, &id_, s);
91 if (n > 0)
92 {
93 if (n < 2) param_[0] = '\0'; // there are no further parameters
94 else strcpy(param_, s); // copy back with filter discarded
95 if (id_ < 0) id_ = ngcbCMD_ID_IVLD; // id is invalid but present
96 }
97 else
98 {
100 }
101 }
102 else
103 {
104 /*
105 * Parameter string is empty or filter is too large
106 */
107 id_ = ngcbCMD_ID_NOT_SET;
108 }
109 return (id_);
110 }
111
113 int Param(const char *p, const char *filter=NULL) {
114 if (p != NULL)
115 {
116 if (strlen(p) > 0)
117 {
118 if (filter == NULL)
119 {
120 id_ = ngcbCMD_ID_NOT_SET;
121 str2str(param_, p, sizeof(param_) - 1);
122 }
123 else if (strlen(filter) < sizeof(filter_))
124 {
125 int n;
126 char format[64];
127 strcpy(format, " ");
128 strcat(format, filter);
129 strcat(format, " %d %1023[^\n]");
130 strcpy(filter_, filter);
131 n = sscanf(p, format, &id_, param_);
132 if (n > 0)
133 {
134 if (n < 2) param_[0] = '\0'; // no further parameters
135 if (id_ < 0)
136 {
137 /*
138 * Invalid component id. If an id has been specified then
139 * it must be valid!
140 */
141 id_ = ngcbCMD_ID_IVLD; return (-1);
142 }
143 }
144 else
145 {
146 /*
147 * No id has been specified. We forward the parameter buffer
148 * as it is.
149 */
150 id_ = ngcbCMD_ID_NOT_SET;
151 str2str(param_, p, sizeof(param_) - 1);
152 }
153 }
154 else
155 {
156 id_ = ngcbCMD_ID_NOT_SET;
157 str2str(param_, p, sizeof(param_) - 1);
158 }
159 }
160 else
161 {
162 /*
163 * Empty parameter buffer
164 */
165 id_ = ngcbCMD_ID_NOT_SET; param_[0] = '\0';
166 }
167 }
168 else
169 {
170 /*
171 * No parameter buffer
172 */
173 id_ = ngcbCMD_ID_NOT_SET; param_[0] = '\0';
174 }
175
176 return (0);
177 }
178
180 char *Param() {return (param_);}
181
183 int Get(const char *filter, char *s) {
184 return (ngcbF2S(param_, filter, s));
185 }
186
188 int Get(const char *filter, double *v) {
189 return (ngcbF2Double(param_, filter, v));
190 }
191
193 int Get(const char *filter) {
194 return (ngcbF2Int(param_, filter));
195 }
196
198 unsigned int GetX(const char *filter) {
199 return (ngcbF2UInt(param_, filter));
200 }
201
202protected:
203private:
205 char param_[1024];
206
208 int id_;
209
211 char filter_[32];
212};
213
218{
219public:
221 ngcbCMD_SRV();
222
224 virtual ~ngcbCMD_SRV();
225
227 void AssignPassword(const char *fileName);
228
230 const char *PW();
231
233 int LogIn(const char *s);
234
236 void LogOut();
237
239 int LoggedIn();
240
242 void Lock();
243
245 void Unlock();
246
248 int Locked();
249
251 void Encrypt(char *s, const char *key = NULL);
252
253protected:
255 int KeyVal(const char *key, float *value);
256
258 int KeyVal(const char *key, double *value);
259
261 int KeyVal(const char *key, int *value);
262
264 int KeyVal(const char *key, unsigned int *value);
265
267 int KeyValHex(const char *key, int *value);
268
270 int KeyValHex(const char *key, unsigned int *value);
271
273 int KeyValFmt(const char *key, int *value);
274
276 int KeyValFmt(const char *key, unsigned int *value);
277
279 int KeyValFrac(const char *key, unsigned int n, uint64_t *v1, uint64_t *v2);
280
282 int KeyIdx(char *key, const char *mask);
283
285 void DiscardDetIdx(char *key);
286
288 void StringReset(char *s, size_t size = 256);
289
290private:
292 int login_;
293
295 int locked_;
296
298 char pw_[64];
299
301 void LogIn_();
302};
303
308 char *reply);
309
314public:
317
319 char syntax[256];
320
322 char help[1024];
323
325 int flags;
326
328 void *udata;
329
331 bool operator<(const ngcb_cmd_t &other) const {
332 return (strcmp(Cmd(), other.Cmd()) < 0);
333 }
334
336 explicit ngcb_cmd_t(const char *name) {
337 str2str(cmd_, name, sizeof(cmd_) - 1);
338 function = NULL;
339 syntax[0] = '\0';
340 help[0] = '\0';
341 flags = 0x0;
342 udata = NULL;
343 }
344
346 virtual ~ngcb_cmd_t() {}
347
349 static bool Compare(const ngcb_cmd_t *a, const ngcb_cmd_t *b) {
350 return (*a < *b);
351 }
352
354 const char *Cmd() const {return (cmd_);}
355
357 void Syntax(const char *s) {
358 if (s == NULL) sprintf(syntax, "%s (...)", Cmd());
359 else if (strlen(s) == 0) sprintf(syntax, "%s", Cmd());
360 else str2str(syntax, s, sizeof(syntax) - 1);
361 }
362
364 void Help(const char *s) {
365 if (s == NULL) strcpy(help, "No help available!");
366 else str2str(help, s, sizeof(help) - 1);
367 }
368
369protected:
370private:
372 char cmd_[64];
373};
374
379{
380public:
382 explicit ngcbCMD_LIST(ngcbCMD_SRV *server);
383
385 virtual ~ngcbCMD_LIST();
386
388 int Add(const char *cmd, ngcb_function_t function, int flags,
389 const char *syntax, const char *help, void *udata, char *erms);
390
392 void Delete(const char *cmd);
393
395 void Sort();
396
398 int Execute(const char *cmd, ngcb_cmd_handle_t *handle, char *reply);
399
401 int Help(const char *args, char *reply);
402
404 int Defined(const char *cmd);
405
407 void Close();
408
410 void Open();
411
413 int Status();
414
415protected:
416
417private:
419 ngcbCMD_SRV *server_;
420
423
425 size_t size_;
426
428 int status_;
429};
430
431
432#endif
int Help(const char *args, char *reply)
Get help-text.
Definition ngcbCMD_LIST.cpp:242
int Execute(const char *cmd, ngcb_cmd_handle_t *handle, char *reply)
Execute command.
Definition ngcbCMD_LIST.cpp:160
int Add(const char *cmd, ngcb_function_t function, int flags, const char *syntax, const char *help, void *udata, char *erms)
Create command handler and add it to the command list.
Definition ngcbCMD_LIST.cpp:83
void Sort()
Sort list alphabetically.
Definition ngcbCMD_LIST.cpp:153
int Status()
Return Status (0=open, 1=complete)
Definition ngcbCMD_LIST.cpp:71
int Defined(const char *cmd)
Check if command is defined.
Definition ngcbCMD_LIST.cpp:73
ngcbCMD_LIST(ngcbCMD_SRV *server)
Constructor.
Definition ngcbCMD_LIST.cpp:37
void Close()
Close command factory - no further commands can be added.
Definition ngcbCMD_LIST.cpp:67
void Delete(const char *cmd)
Delete command from the command list.
Definition ngcbCMD_LIST.cpp:137
virtual ~ngcbCMD_LIST()
Destructor.
Definition ngcbCMD_LIST.cpp:57
void Open()
Open command factory - further commands can be added.
Definition ngcbCMD_LIST.cpp:69
Definition ngcbCMD.hpp:218
int Locked()
Return locked status.
Definition ngcbCMD_SRV.cpp:180
void DiscardDetIdx(char *key)
Discard DETi index.
Definition ngcbCMD_SRV.cpp:309
int KeyValFmt(const char *key, int *value)
Get 32-bit signed integer value from string.
Definition ngcbCMD_SRV.cpp:293
void Unlock()
Unlock command system.
Definition ngcbCMD_SRV.cpp:176
virtual ~ngcbCMD_SRV()
Destructor.
Definition ngcbCMD_SRV.cpp:38
int KeyValFrac(const char *key, unsigned int n, uint64_t *v1, uint64_t *v2)
Fraction after n digits.
Definition ngcbCMD_SRV.cpp:303
int LogIn(const char *s)
Login to trusted mode with a key in the format "name:password".
Definition ngcbCMD_SRV.cpp:187
int LoggedIn()
Return login status.
Definition ngcbCMD_SRV.cpp:255
void StringReset(char *s, size_t size=256)
Fill string with zeros.
Definition ngcbCMD_SRV.cpp:319
ngcbCMD_SRV()
Constructor.
Definition ngcbCMD_SRV.cpp:28
int KeyIdx(char *key, const char *mask)
Get the index after the first occurrence of mask in the keyword.
Definition ngcbCMD_SRV.cpp:314
void AssignPassword(const char *fileName)
Assign password from file.
Definition ngcbCMD_SRV.cpp:51
int KeyValHex(const char *key, int *value)
Get 32-bit signed integer value from hexadecimal keyword string.
Definition ngcbCMD_SRV.cpp:283
const char * PW()
Return password.
Definition ngcbCMD_SRV.cpp:163
int KeyVal(const char *key, float *value)
Get single precision floating point value from keyword string.
Definition ngcbCMD_SRV.cpp:263
void Lock()
Lock command system.
Definition ngcbCMD_SRV.cpp:172
void LogOut()
Log out.
Definition ngcbCMD_SRV.cpp:247
void Encrypt(char *s, const char *key=NULL)
Encrypt string with key.
Definition ngcbCMD_SRV.cpp:117
Definition ngcbCMD.hpp:41
virtual ~ngcb_cmd_handle_t()
Destructor.
Definition ngcbCMD.hpp:67
int Param(const char *p, const char *filter=NULL)
Assign parameter string.
Definition ngcbCMD.hpp:113
int Get(const char *filter, double *v)
Get filtered double precision parameter.
Definition ngcbCMD.hpp:188
int Get(const char *filter, char *s)
Get filtered string parameter.
Definition ngcbCMD.hpp:183
unsigned int GetX(const char *filter)
Get filtered 32-bit unsigned integer parameter.
Definition ngcbCMD.hpp:198
int done
Done flag.
Definition ngcbCMD.hpp:50
void * udata
Forwarded user data.
Definition ngcbCMD.hpp:47
int fd
File descriptor of command source.
Definition ngcbCMD.hpp:53
int Id(const char *filter)
Format parameter string and return filtered component id.
Definition ngcbCMD.hpp:73
int Get(const char *filter)
Get filtered 32-bit signed integer parameter.
Definition ngcbCMD.hpp:193
int Id()
Return component id.
Definition ngcbCMD.hpp:70
char * Param()
Return parameter string.
Definition ngcbCMD.hpp:180
ngcb_cmd_handle_t()
Constructor.
Definition ngcbCMD.hpp:56
int flags
Forwarded flags (ngcbCMD_UPDATE, ngcbCMD_PROTECTED)
Definition ngcbCMD.hpp:44
Definition ngcbCMD.hpp:313
int flags
Flags (ngcbCMD_UPDATE, ngcbCMD_PROTECTED)
Definition ngcbCMD.hpp:325
ngcb_function_t function
Command handling function.
Definition ngcbCMD.hpp:316
ngcb_cmd_t(const char *name)
Constructor.
Definition ngcbCMD.hpp:336
char syntax[256]
Syntax string (for help)
Definition ngcbCMD.hpp:319
void * udata
User data to be forwarded.
Definition ngcbCMD.hpp:328
char help[1024]
Help text.
Definition ngcbCMD.hpp:322
void Syntax(const char *s)
Assign syntax.
Definition ngcbCMD.hpp:357
void Help(const char *s)
Assign help text.
Definition ngcbCMD.hpp:364
virtual ~ngcb_cmd_t()
Destructor.
Definition ngcbCMD.hpp:346
static bool Compare(const ngcb_cmd_t *a, const ngcb_cmd_t *b)
Comparison function.
Definition ngcbCMD.hpp:349
bool operator<(const ngcb_cmd_t &other) const
Comparison operator.
Definition ngcbCMD.hpp:331
const char * Cmd() const
Return command name.
Definition ngcbCMD.hpp:354
#define ngcbCMD_ID_NOT_SET
Definition ngcbCMD.hpp:35
#define ngcbCMD_ID_IVLD
Definition ngcbCMD.hpp:34
int(ngcbCMD_SRV::* ngcb_function_t)(ngcb_cmd_handle_t *handle, char *reply)
Definition ngcbCMD.hpp:307
#define ngcbCMD_LIST_SIZE
Definition ngcbCMD.hpp:20
int ngcbF2S(const char *s, const char *filter, char *v)
Definition ngcbString.c:355
int ngcbF2Int(const char *s, const char *filter)
Definition ngcbString.c:412
int ngcbF2Double(const char *s, const char *filter, double *v)
Definition ngcbString.c:526
#define str2str(s1, s2, n)
Definition ngcb.h:73
unsigned int ngcbF2UInt(const char *s, const char *filter)
Definition ngcbString.c:474