]> rtime.felk.cvut.cz Git - fpga/rpi-motor-control-pxmc.git/blob - src/libs4c/cmdproc/cmd_proc.h
Raspberry Pi 3-phase motor control educational system based on PXMC.
[fpga/rpi-motor-control-pxmc.git] / src / libs4c / cmdproc / cmd_proc.h
1 /*******************************************************************
2   Components for embedded applications builded for
3   laboratory and medical instruments firmware  
4  
5   cmd_proc.h - text line command processor
6                designed for instruments control and setup
7                over RS-232 line
8  
9   Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
10             (C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
11             (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
12
13   This file can be used and copied according to next
14   license alternatives
15    - MPL - Mozilla Public License
16    - GPL - GNU Public License
17    - other license provided by project originators
18  *******************************************************************/
19
20 #ifndef _CMD_PROC_H_
21 #define _CMD_PROC_H_
22
23 #define CMD_PROC_WITH_FILE
24
25 #ifdef CMD_PROC_WITH_FILE
26 #include <stdio.h>
27 #endif
28
29 #define FL_ELB_ECHO   0x10      /* Read characters are echoed back */
30 #define FL_ELB_INSEND 0x20      /* The line is currently being sent */
31 #define FL_ELB_NOCRLF 0x40      /* CR and/or LF will not start the new line */
32 #define CMD_DES_CONTINUE_AT_ID ((cmd_des_t*)1)
33 #define CMD_DES_INCLUDE_SUBLIST_ID ((cmd_des_t*)2)
34 #define CMD_DES_CONTINUE_AT(list)     CMD_DES_CONTINUE_AT_ID,((cmd_des_t*)list)
35 #define CMD_DES_INCLUDE_SUBLIST(list) CMD_DES_INCLUDE_SUBLIST_ID,((cmd_des_t*)list)
36
37 /* generic cmd_io structure */
38
39 /* buffer for in/out line collection */
40 typedef struct{
41   int  flg;                     /* FL_ELB_xxx */
42   int  inbuf;                   /* Index to store new characters */
43   int  alloc;                   /* Size of the buffer pointed by buf */
44   int  maxlen;
45   int  lastch;                  /* Last characted added to the buffer.
46                                  * If FL_ELB_INSEND is set, lastch is
47                                  * index of last sent char. */
48   char *buf;
49 } ed_line_buf_t;
50
51 /* Structure for character input output. It is used either for direct
52  * access to IO device or for buffered IO. In the later case,
53  * priv.edline is used to store buffer information. */
54 typedef struct cmd_io{
55   int (*putc)(struct cmd_io *cmd_io,int ch);
56   int (*getc)(struct cmd_io *cmd_io);
57   int (*write)(struct cmd_io *cmd_io,const void *buf,int count);
58   int (*read)(struct cmd_io *cmd_io,void *buf,int count);
59   union {
60     struct {
61       ed_line_buf_t *in;
62       ed_line_buf_t *out;
63       struct cmd_io *io_stack;
64     } ed_line;
65     struct {
66       long pos;
67       void *ptr;
68     } device;
69 #ifdef CMD_PROC_WITH_FILE
70     struct {
71       FILE *in;
72       FILE *out;
73     } file;
74 #endif /*CMD_PROC_WITH_FILE*/
75     struct {
76       int uartch;
77     } uart;
78   } priv;
79 } cmd_io_t;
80
81 static inline int cmd_io_putc(struct cmd_io *cmd_io,int ch)
82 { if(!cmd_io->putc) return -1;
83   return (*cmd_io->putc)(cmd_io,ch);
84 }
85
86 static inline int cmd_io_getc(struct cmd_io *cmd_io)
87 { if(!cmd_io->getc) return -1;
88   return (*cmd_io->getc)(cmd_io);
89 }
90
91 static inline int cmd_io_write(struct cmd_io *cmd_io,const void *buf,int count)
92 { if(!cmd_io->write) return -1;
93   return (*cmd_io->write)(cmd_io,buf,count);
94 }
95
96 static inline int cmd_io_read(struct cmd_io *cmd_io,void *buf,int count)
97 { if(!cmd_io->read) return -1;
98   return (*cmd_io->read)(cmd_io,buf,count);
99 }
100
101 int cmd_io_puts(cmd_io_t *cmd_io, const char *str);
102
103 int cmd_io_line_putc(cmd_io_t *cmd_io,int ch);
104
105 int cmd_io_write_bychar(cmd_io_t *cmd_io,const void *buf,int count);
106
107 int cmd_io_read_bychar(cmd_io_t *cmd_io,void *buf,int count);
108
109 /* command descriptions */
110
111 #define CDESM_OPCHR     0x10    /* Command uses operation character */
112 #define CDESM_RD        0x01    /* Value read is possible */
113 #define CDESM_WR        0x02    /* Value write is possible */
114 #define CDESM_RW        (CDESM_RD|CDESM_WR) /* Both */
115
116 typedef struct cmd_des{
117   int code;
118   int mode;
119   char *name;
120   char *help;
121   int (*fnc)(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
122   char *info[];
123 } cmd_des_t;
124
125 #define CMDERR_BADCMD 2
126 #define CMDERR_OPCHAR 10
127 #define CMDERR_WRPERM 11
128 #define CMDERR_RDPERM 12
129 #define CMDERR_GARBAG 13
130 #define CMDERR_BADSEP 14
131 #define CMDERR_BADCFG 15
132 #define CMDERR_NOMEM  16
133 #define CMDERR_BADSUF 17
134 #define CMDERR_BADPAR 20
135 #define CMDERR_VALOOR 21
136 #define CMDERR_BADREG 40
137 #define CMDERR_BSYREG 41
138 #define CMDERR_BADDIO 50
139 #define CMDERR_NODEV  60
140 #define CMDERR_TIMEOUT 61
141 #define CMDERR_EIO    62
142
143 int cmd_opchar_check(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
144
145 int cmd_num_suffix(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[], unsigned *val);
146
147 int proc_cmd_line(cmd_io_t *cmd_io, cmd_des_t const **des_arr, char *line);
148
149 int i2str(char *s,long val,int len,int form);
150
151 int cmd_do_stamp(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
152
153 int cmd_do_help(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
154
155 int cmd_do_rw_short(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
156
157 int cmd_do_rw_int(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
158
159 int cmd_do_rw_long(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
160
161 int cmd_do_rw_bitflag(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]);
162
163 int cmd_opchar_replong(cmd_io_t *cmd_io, char *param[], long val,int len,int form);
164
165 int cmd_processor_run(cmd_io_t *cmd_io, cmd_des_t const **commands);
166
167 #endif /* _CMD_PROC_H_ */
168
169 /* Local Variables: */
170 /* c-basic-offset: 2 */
171 /* End */