ngc 1.5.0
 
Loading...
Searching...
No Matches
ngcbCMD.hpp
Go to the documentation of this file.
1
8#ifndef ngcbCMD_H
9#define ngcbCMD_H
10
11
12#ifndef __cplusplus
13#error This is a C++ include file and cannot be used from plain C
14#endif
15
16#include <ngc/core/ngcb.h>
17
21#define ngcbCMD_LIST_SIZE 256
22
23/*
24 * Global command flags
25 */
26#define ngcbCMD_UPDATE 0x00000001 //< command requires system update
27#define ngcbCMD_PROTECTED 0x00000002 //< protected command
28#define ngcbCMD_PROTECT 0x00000004 //< enforce command protection
29#define ngcbCMD_INTERNAL 0x00000008 //< internal use only
30#define ngcbCMD_DISCARDED 0x00000010 //< command is declared dsicarded
31
32/*
33 * Special component identifiers
34 */
35#define ngcbCMD_ID_IVLD -1 //< id invalid
36#define ngcbCMD_ID_NOT_SET -2 //< id not set
37
42{
43public:
45 int flags;
46
48 void *udata;
49
51 int done;
52
54 int fd;
55
58 param_[0] = '\0';
59 filter_[0] = '\0';
60 flags = 0x0;
61 udata = NULL;
62 done = 0;
63 fd = -1; // not set
64 id_ = ngcbCMD_ID_NOT_SET; // not set
65 }
66
68 virtual ~ngcb_cmd_handle_t() {}
69
71 int Id() {return (id_);}
72
74 int Id(const char *filter) {
75 if ((id_ != ngcbCMD_ID_NOT_SET) && (strcmp(filter, filter_) == 0))
76 {
77 /*
78 * The id is already stored - even if it was invalid
79 */
80 return (id_);
81 }
82 else if ((strlen(param_) > 0) && (strlen(filter) < sizeof(filter_)))
83 {
84 char s[1024];
85 char format[64];
86 int n;
87 strcpy(format, " ");
88 strcat(format, filter);
89 strcat(format, " %d %1023[^\n]");
90 s[0] = '\0';
91 n = sscanf(param_, format, &id_, s);
92 if (n > 0)
93 {
94 if (n < 2) param_[0] = '\0'; // there are no further parameters
95 else strcpy(param_, s); // copy back with filter discarded
96 if (id_ < 0) id_ = ngcbCMD_ID_IVLD; // id is invalid but present
97 }
98 else
99 {
100 id_ = ngcbCMD_ID_NOT_SET;
101 }
102 }
103 else
104 {
105 /*
106 * Parameter string is empty or filter is too large
107 */
108 id_ = ngcbCMD_ID_NOT_SET;
109 }
110 return (id_);
111 }
112
114 int Param(const char *p, const char *filter=NULL) {
115 if (p != NULL)
116 {
117 if (strlen(p) > 0)
118 {
119 if (filter == NULL)
120 {
121 id_ = ngcbCMD_ID_NOT_SET;
122 str2str(param_, p, sizeof(param_) - 1);
123 }
124 else if (strlen(filter) < sizeof(filter_))
125 {
126 int n;
127 char format[64];
128 strcpy(format, " ");
129 strcat(format, filter);
130 strcat(format, " %d %1023[^\n]");
131 strcpy(filter_, filter);
132 n = sscanf(p, format, &id_, param_);
133 if (n > 0)
134 {
135 if (n < 2) param_[0] = '\0'; // no further parameters
136 if (id_ < 0)
137 {
138 /*
139 * Invalid component id. If an id has been specified then
140 * it must be valid!
141 */
142 id_ = ngcbCMD_ID_IVLD; return (-1);
143 }
144 }
145 else
146 {
147 /*
148 * No id has been specified. We forward the parameter buffer
149 * as it is.
150 */
151 id_ = ngcbCMD_ID_NOT_SET;
152 str2str(param_, p, sizeof(param_) - 1);
153 }
154 }
155 else
156 {
157 id_ = ngcbCMD_ID_NOT_SET;
158 str2str(param_, p, sizeof(param_) - 1);
159 }
160 }
161 else
162 {
163 /*
164 * Empty parameter buffer
165 */
166 id_ = ngcbCMD_ID_NOT_SET; param_[0] = '\0';
167 }
168 }
169 else
170 {
171 /*
172 * No parameter buffer
173 */
174 id_ = ngcbCMD_ID_NOT_SET; param_[0] = '\0';
175 }
176
177 return (0);
178 }
179
181 char *Param() {return (param_);}
182
184 int Get(const char *filter, char *s) {
185 return (ngcbF2S(param_, filter, s));
186 }
187
189 int Get(const char *filter, double *v) {
190 return (ngcbF2Double(param_, filter, v));
191 }
192
194 int Get(const char *filter) {
195 return (ngcbF2Int(param_, filter));
196 }
197
199 unsigned int GetX(const char *filter) {
200 return (ngcbF2UInt(param_, filter));
201 }
202
203protected:
204private:
206 char param_[1024];
207
209 int id_;
210
212 char filter_[32];
213};
214
219{
220public:
222 ngcbCMD_SRV();
223
225 virtual ~ngcbCMD_SRV();
226
228 void AssignPassword(const char *fileName);
229
231 const char *PW();
232
234 int LogIn(const char *s);
235
237 void LogOut();
238
240 int LoggedIn();
241
243 void Lock();
244
246 void Unlock();
247
249 int Locked();
250
252 void Encrypt(char *s, const char *key = NULL);
253
254protected:
256 int KeyVal(const char *key, float *value);
257
259 int KeyVal(const char *key, double *value);
260
262 int KeyVal(const char *key, int *value);
263
265 int KeyVal(const char *key, unsigned int *value);
266
268 int KeyValHex(const char *key, int *value);
269
271 int KeyValHex(const char *key, unsigned int *value);
272
274 int KeyValFmt(const char *key, int *value);
275
277 int KeyValFmt(const char *key, unsigned int *value);
278
280 int KeyValFrac(const char *key, unsigned int n, uint64_t *v1, uint64_t *v2);
281
283 int KeyIdx(char *key, const char *mask);
284
286 void DiscardDetIdx(char *key);
287
289 void StringReset(char *s, size_t size = 256);
290
291private:
293 int login_;
294
296 int locked_;
297
299 char pw_[64];
300
302 void LogIn_();
303};
304
309 char *reply);
310
315public:
318
320 char syntax[256];
321
323 char help[1024];
324
326 int flags;
327
329 void *udata;
330
332 bool operator<(const ngcb_cmd_t &other) const {
333 return (strcmp(Cmd(), other.Cmd()) < 0);
334 }
335
337 explicit ngcb_cmd_t(const char *name) {
338 str2str(cmd_, name, sizeof(cmd_) - 1);
339 function = NULL;
340 syntax[0] = '\0';
341 help[0] = '\0';
342 flags = 0x0;
343 udata = NULL;
344 }
345
347 virtual ~ngcb_cmd_t() {}
348
350 static bool Compare(const ngcb_cmd_t *a, const ngcb_cmd_t *b) {
351 return (*a < *b);
352 }
353
355 const char *Cmd() const {return (cmd_);}
356
358 void Syntax(const char *s) {
359 if (s == NULL) sprintf(syntax, "%s (...)", Cmd());
360 else if (strlen(s) == 0) sprintf(syntax, "%s", Cmd());
361 else str2str(syntax, s, sizeof(syntax) - 1);
362 }
363
365 void Help(const char *s) {
366 if (s == NULL) strcpy(help, "No help available!");
367 else str2str(help, s, sizeof(help) - 1);
368 }
369
370protected:
371private:
373 char cmd_[64];
374};
375
380{
381public:
383 explicit ngcbCMD_LIST(ngcbCMD_SRV *server);
384
386 virtual ~ngcbCMD_LIST();
387
389 int Add(const char *cmd, ngcb_function_t function, int flags,
390 const char *syntax, const char *help, void *udata, char *erms);
391
393 void Delete(const char *cmd);
394
396 void Sort();
397
399 int Execute(const char *cmd, ngcb_cmd_handle_t *handle, char *reply);
400
402 int Help(const char *args, char *reply);
403
405 int Defined(const char *cmd);
406
408 void Close();
409
411 void Open();
412
414 int Status();
415
416protected:
417
418private:
420 ngcbCMD_SRV *server_;
421
424
426 size_t size_;
427
429 int status_;
430};
431
432
433#endif
int Help(const char *args, char *reply)
Get help-text.
Definition ngcbCMD_LIST.cpp:243
int Execute(const char *cmd, ngcb_cmd_handle_t *handle, char *reply)
Execute command.
Definition ngcbCMD_LIST.cpp:161
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:84
void Sort()
Sort list alphabetically.
Definition ngcbCMD_LIST.cpp:154
int Status()
Return Status (0=open, 1=complete)
Definition ngcbCMD_LIST.cpp:72
int Defined(const char *cmd)
Check if command is defined.
Definition ngcbCMD_LIST.cpp:74
ngcbCMD_LIST(ngcbCMD_SRV *server)
Constructor.
Definition ngcbCMD_LIST.cpp:38
void Close()
Close command factory - no further commands can be added.
Definition ngcbCMD_LIST.cpp:68
void Delete(const char *cmd)
Delete command from the command list.
Definition ngcbCMD_LIST.cpp:138
virtual ~ngcbCMD_LIST()
Destructor.
Definition ngcbCMD_LIST.cpp:58
void Open()
Open command factory - further commands can be added.
Definition ngcbCMD_LIST.cpp:70
Definition ngcbCMD.hpp:219
int Locked()
Return locked status.
Definition ngcbCMD_SRV.cpp:181
void DiscardDetIdx(char *key)
Discard DETi index.
Definition ngcbCMD_SRV.cpp:310
int KeyValFmt(const char *key, int *value)
Get 32-bit signed integer value from string.
Definition ngcbCMD_SRV.cpp:294
void Unlock()
Unlock command system.
Definition ngcbCMD_SRV.cpp:177
virtual ~ngcbCMD_SRV()
Destructor.
Definition ngcbCMD_SRV.cpp:39
int KeyValFrac(const char *key, unsigned int n, uint64_t *v1, uint64_t *v2)
Fraction after n digits.
Definition ngcbCMD_SRV.cpp:304
int LogIn(const char *s)
Login to trusted mode with a key in the format "name:password".
Definition ngcbCMD_SRV.cpp:188
int LoggedIn()
Return login status.
Definition ngcbCMD_SRV.cpp:256
void StringReset(char *s, size_t size=256)
Fill string with zeros.
Definition ngcbCMD_SRV.cpp:320
ngcbCMD_SRV()
Constructor.
Definition ngcbCMD_SRV.cpp:29
int KeyIdx(char *key, const char *mask)
Get the index after the first occurrence of mask in the keyword.
Definition ngcbCMD_SRV.cpp:315
void AssignPassword(const char *fileName)
Assign password from file.
Definition ngcbCMD_SRV.cpp:52
int KeyValHex(const char *key, int *value)
Get 32-bit signed integer value from hexadecimal keyword string.
Definition ngcbCMD_SRV.cpp:284
const char * PW()
Return password.
Definition ngcbCMD_SRV.cpp:164
int KeyVal(const char *key, float *value)
Get single precision floating point value from keyword string.
Definition ngcbCMD_SRV.cpp:264
void Lock()
Lock command system.
Definition ngcbCMD_SRV.cpp:173
void LogOut()
Log out.
Definition ngcbCMD_SRV.cpp:248
void Encrypt(char *s, const char *key=NULL)
Encrypt string with key.
Definition ngcbCMD_SRV.cpp:118
Definition ngcbCMD.hpp:42
virtual ~ngcb_cmd_handle_t()
Destructor.
Definition ngcbCMD.hpp:68
int Param(const char *p, const char *filter=NULL)
Assign parameter string.
Definition ngcbCMD.hpp:114
int Get(const char *filter, double *v)
Get filtered double precision parameter.
Definition ngcbCMD.hpp:189
int Get(const char *filter, char *s)
Get filtered string parameter.
Definition ngcbCMD.hpp:184
unsigned int GetX(const char *filter)
Get filtered 32-bit unsigned integer parameter.
Definition ngcbCMD.hpp:199
int done
Done flag.
Definition ngcbCMD.hpp:51
void * udata
Forwarded user data.
Definition ngcbCMD.hpp:48
int fd
File descriptor of command source.
Definition ngcbCMD.hpp:54
int Id(const char *filter)
Format parameter string and return filtered component id.
Definition ngcbCMD.hpp:74
int Get(const char *filter)
Get filtered 32-bit signed integer parameter.
Definition ngcbCMD.hpp:194
int Id()
Return component id.
Definition ngcbCMD.hpp:71
char * Param()
Return parameter string.
Definition ngcbCMD.hpp:181
ngcb_cmd_handle_t()
Constructor.
Definition ngcbCMD.hpp:57
int flags
Forwarded flags (ngcbCMD_UPDATE, ngcbCMD_PROTECTED)
Definition ngcbCMD.hpp:45
Definition ngcbCMD.hpp:314
int flags
Flags (ngcbCMD_UPDATE, ngcbCMD_PROTECTED)
Definition ngcbCMD.hpp:326
ngcb_function_t function
Command handling function.
Definition ngcbCMD.hpp:317
ngcb_cmd_t(const char *name)
Constructor.
Definition ngcbCMD.hpp:337
char syntax[256]
Syntax string (for help)
Definition ngcbCMD.hpp:320
void * udata
User data to be forwarded.
Definition ngcbCMD.hpp:329
char help[1024]
Help text.
Definition ngcbCMD.hpp:323
void Syntax(const char *s)
Assign syntax.
Definition ngcbCMD.hpp:358
void Help(const char *s)
Assign help text.
Definition ngcbCMD.hpp:365
virtual ~ngcb_cmd_t()
Destructor.
Definition ngcbCMD.hpp:347
static bool Compare(const ngcb_cmd_t *a, const ngcb_cmd_t *b)
Comparison function.
Definition ngcbCMD.hpp:350
bool operator<(const ngcb_cmd_t &other) const
Comparison operator.
Definition ngcbCMD.hpp:332
const char * Cmd() const
Return command name.
Definition ngcbCMD.hpp:355
#define ngcbCMD_ID_NOT_SET
Definition ngcbCMD.hpp:36
#define ngcbCMD_ID_IVLD
Definition ngcbCMD.hpp:35
int(ngcbCMD_SRV::* ngcb_function_t)(ngcb_cmd_handle_t *handle, char *reply)
Definition ngcbCMD.hpp:308
#define ngcbCMD_LIST_SIZE
Definition ngcbCMD.hpp:21
int ngcbF2S(const char *s, const char *filter, char *v)
Definition ngcbString.c:356
int ngcbF2Int(const char *s, const char *filter)
Definition ngcbString.c:413
int ngcbF2Double(const char *s, const char *filter, double *v)
Definition ngcbString.c:527
#define str2str(s1, s2, n)
Definition ngcb.h:74
unsigned int ngcbF2UInt(const char *s, const char *filter)
Definition ngcbString.c:475