+SUBDIRS=cmdproc cmdprocio
+
lib_LIBRARIES = misc
#shared_LIBRARIES =
#include_HEADERS = usb.h usb_spec.h usbdebug.h usb_stdreq.h
-include_HEADERS = utils.h cmd_proc.h cmd_bth.h
-
-ifeq ($(CONFIG_BLUETOOTH),y)
-cmd_bth.c = cmd_bth.c
-endif
-
-misc_SOURCES = utils.c cmd_proc.c cmd_rs232.c $(cmd_bth.c)
-
-#lib_LOADLIBES =
-#bin_PROGRAMS =
+include_HEADERS = utils.h
-default_CONFIG = CONFIG_NO_STDIO=n
+misc_SOURCES = utils.c
-LOCAL_CONFIG_H = cmd_proc_config.h
+++ /dev/null
-/*******************************************************************
- Components for embedded applications builded for
- laboratory and medical instruments firmware
-
- cmd_bth.c - interconnection of text command processor
- with RS-232 line
-
- Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
- (C) 2002 by PiKRON Ltd. http://www.pikron.com
-
- *******************************************************************/
-
-#include <types.h>
-#include <ctype.h>
-#include <string.h>
-//#include <utils.h>
-#include <cmd_proc.h>
-#include <bth_inface.h>
-
-#define ED_LINE_CHARS 80
-
-cmd_io_t cmd_io_bth_dev;
-
-char ed_line_chars_bth_in[ED_LINE_CHARS+1];
-char ed_line_chars_bth_out[ED_LINE_CHARS+1];
-
-ed_line_buf_t ed_line_buf_bth_in={
- flg:FL_ELB_ECHO,
- inbuf:0,
- alloc:sizeof(ed_line_chars_bth_in),
- maxlen:0,
- lastch:0,
- buf:ed_line_chars_bth_in
-};
-
-ed_line_buf_t ed_line_buf_bth_out={
- flg:FL_ELB_NOCRLF,
- inbuf:0,
- alloc:sizeof(ed_line_chars_bth_out),
- maxlen:0,
- lastch:0,
- buf:ed_line_chars_bth_out
-};
-
-cmd_io_t cmd_io_bth={
- putc:cmd_io_putc_ed_line,
- getc:NULL,
- write:cmd_io_write_bychar,
- read:NULL,
- priv:{
- ed_line:{
- in: &ed_line_buf_bth_in,
- out:&ed_line_buf_bth_out,
- io_stack:&cmd_io_bth_dev
- }
- }
-};
-
-/* process pending output */
-int cmd_bth_line_out(cmd_io_t *cmd_io)
-{
- cmd_io_t* io_stack;
- ed_line_buf_t* ed_line=cmd_io->priv.ed_line.out;
-
- if(!ed_line->inbuf) return 0;
- if(!(ed_line->flg&FL_ELB_INSEND)){
- ed_line->flg|=FL_ELB_INSEND;
- ed_line->lastch=0;
- }
- if(!(io_stack=cmd_io->priv.ed_line.io_stack))
- return -1;
-
- while(cmd_io_putc(io_stack,(__u8)ed_line_buf_bth_out.
- buf[ed_line_buf_bth_out.lastch])>=0){
- if(++ed_line_buf_bth_out.lastch>=ed_line_buf_bth_out.inbuf){
- ed_line->lastch=0;
- ed_line->inbuf=0;
- ed_line->flg&=~FL_ELB_INSEND;
- return 0;
- }
- }
- return 1;
-}
-
-/* process input */
-int cmd_bth_line_in(cmd_io_t *cmd_io)
-{
- int ch;
- cmd_io_t* io_stack;
-
- if(!(io_stack=cmd_io->priv.ed_line.io_stack))
- return -1;
-
- while((ch=cmd_io_getc(io_stack))>=0){
- if(ed_line_buf_bth_in.flg&FL_ELB_ECHO)
- while(cmd_io_putc(io_stack,ch)<0);
-
- if(cmd_ed_line_buf(&ed_line_buf_bth_in,ch)){
- if(ed_line_buf_bth_in.flg&FL_ELB_ECHO){
- while(cmd_io_putc(io_stack,'\r')<0);
- while(cmd_io_putc(io_stack,'\n')<0);
- }
- return 1;
- }
- }
-
- return 0;
-}
-
-char *cmd_bth_rdline(cmd_io_t *cmd_io, int mode)
-{
- int ret;
- while((ret=cmd_bth_line_in(cmd_io))==0)
- if(!mode) break;
- if(ret<=0) return NULL;
- return cmd_io->priv.ed_line.in->buf;
-}
-
-
-//#include <periph/l2cap_bth.h>
-
-int cmd_io_putc_bth(struct cmd_io *cmd_io,int ch)
-{
- return bth_inface_sendch(ch,0); //=0; //sci_bth_sendch(ch,IMPLIC_NUM_SERIAL_PORT);;
-}
-
-int cmd_io_getc_bth(struct cmd_io *cmd_io)
-{
- return bth_inface_recch(0);//=-1; //sci_bth_recch(IMPLIC_NUM_SERIAL_PORT);
-}
-
-cmd_io_t cmd_io_bth_dev={
- putc:cmd_io_putc_bth,
- getc:cmd_io_getc_bth,
- write:cmd_io_write_bychar,
- read:cmd_io_read_bychar
-};
+++ /dev/null
-/*******************************************************************
- Components for embedded applications builded for
- laboratory and medical instruments firmware
-
- cmd_rs232.c - interconnection of text command processor
- with RS-232 line
-
- Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
- (C) 2002 by PiKRON Ltd. http://www.pikron.com
-
- This file can be used and copied according to next
- license alternatives
- - MPL - Mozilla Public License
- - GPL - GNU Public License
- - other license provided by project originators
-
- *******************************************************************/
-
-#include <types.h>
-#include <ctype.h>
-#include <string.h>
-#include <utils.h>
-#include <cmd_proc.h>
-
-#define ED_LINE_CHARS 80
-
-cmd_io_t cmd_io_rs232_dev;
-
-char ed_line_chars_rs232_in[ED_LINE_CHARS+1];
-char ed_line_chars_rs232_out[ED_LINE_CHARS+1];
-
-ed_line_buf_t ed_line_buf_rs232_in={
- flg:FL_ELB_ECHO,
- inbuf:0,
- alloc:sizeof(ed_line_chars_rs232_in),
- maxlen:0,
- lastch:0,
- buf:ed_line_chars_rs232_in
-};
-
-ed_line_buf_t ed_line_buf_rs232_out={
- flg:FL_ELB_NOCRLF,
- inbuf:0,
- alloc:sizeof(ed_line_chars_rs232_out),
- maxlen:0,
- lastch:0,
- buf:ed_line_chars_rs232_out
-};
-
-cmd_io_t cmd_io_rs232={
- putc:cmd_io_putc_ed_line,
- getc:NULL,
- write:cmd_io_write_bychar,
- read:NULL,
- priv:{
- ed_line:{
- in: &ed_line_buf_rs232_in,
- out:&ed_line_buf_rs232_out,
- io_stack:&cmd_io_rs232_dev
- }
- }
-};
-
-/* process pending output */
-int cmd_rs232_line_out(cmd_io_t *cmd_io)
-{
- cmd_io_t* io_stack;
- ed_line_buf_t* ed_line=cmd_io->priv.ed_line.out;
-
- if(!ed_line->inbuf) return 0;
- if(!(ed_line->flg&FL_ELB_INSEND)){
- ed_line->flg|=FL_ELB_INSEND;
- ed_line->lastch=0;
- }
- if(!(io_stack=cmd_io->priv.ed_line.io_stack))
- return -1;
-
- while(cmd_io_putc(io_stack,(__u8)ed_line_buf_rs232_out.
- buf[ed_line_buf_rs232_out.lastch])>=0){
- if(++ed_line_buf_rs232_out.lastch>=ed_line_buf_rs232_out.inbuf){
- ed_line->lastch=0;
- ed_line->inbuf=0;
- ed_line->flg&=~FL_ELB_INSEND;
- return 0;
- }
- }
- return 1;
-}
-
-/* process input */
-int cmd_rs232_line_in(cmd_io_t *cmd_io)
-{
- int ch;
- cmd_io_t* io_stack;
-
- if(!(io_stack=cmd_io->priv.ed_line.io_stack))
- return -1;
-
- while((ch=cmd_io_getc(io_stack))>=0){
- if(ed_line_buf_rs232_in.flg&FL_ELB_ECHO)
- while(cmd_io_putc(io_stack,ch)<0);
-
- if(cmd_ed_line_buf(&ed_line_buf_rs232_in,ch)){
- if(ed_line_buf_rs232_in.flg&FL_ELB_ECHO){
- while(cmd_io_putc(io_stack,'\r')<0);
- while(cmd_io_putc(io_stack,'\n')<0);
- }
- return 1;
- }
- }
-
- return 0;
-}
-
-char *cmd_rs232_rdline(cmd_io_t *cmd_io, int mode)
-{
- int ret;
- while((ret=cmd_rs232_line_in(cmd_io))==0)
- if(!mode) break;
- if(ret<=0) return NULL;
- return cmd_io->priv.ed_line.in->buf;
-}
-
-
-#include <periph/sci_rs232.h>
-
-int cmd_io_putc_rs232(struct cmd_io *cmd_io,int ch)
-{
- return sci_rs232_sendch(ch, sci_rs232_chan_default);
-}
-
-int cmd_io_getc_rs232(struct cmd_io *cmd_io)
-{
- return sci_rs232_recch(sci_rs232_chan_default);
-}
-
-cmd_io_t cmd_io_rs232_dev={
- putc:cmd_io_putc_rs232,
- getc:cmd_io_getc_rs232,
- write:cmd_io_write_bychar,
- read:cmd_io_read_bychar
-};
-
--- /dev/null
+# Generic directory or leaf node makefile for OCERA make framework
+
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" == `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) )
+endif
+
+ifeq ($(MAKERULES_DIR),)
+all : default
+.DEFAULT::
+ @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n"
+else
+include $(MAKERULES_DIR)/Makefile.rules
+endif
+
--- /dev/null
+lib_LIBRARIES = cmdproc
+cmdproc_SOURCES = cmd_proc.c cmd_io.c cmd_io_line.c cmdio_std.c cmdio_std_line.c
+
+include_HEADERS = cmd_proc.h
+
+bin_PROGRAMS = cmdproc_test
+cmdproc_test_SOURCES = cmdproc_test.c
+cmdproc_test_LIBS = cmdproc
--- /dev/null
+Platform independent files for command processor.
+
+Besides the main library, you can find here also the definition of
+cmdio which uses standard input/output and an example application,
+which shows almost all features of the command processor.
+
+Platform dependent cmdios should be placed in cmdprocio library.
--- /dev/null
+#include <cmd_proc.h>
+#include <string.h>
+#include <types.h>
+
+int cmd_io_puts(cmd_io_t *cmd_io, const char *str)
+{
+ int ret;
+ if (!str) return 0;
+ ret = cmd_io_write(cmd_io, str, strlen(str));
+ return ret;
+}
+
+int cmd_io_write_bychar(cmd_io_t *cmd_io,const void *buf,int count)
+{
+ int cn=0;
+ __u8* p=(__u8*)buf;
+ while(count--&&(*cmd_io->putc)(cmd_io,*p++)>=0){
+ cn++;
+ }
+ return cn;
+}
+
+int cmd_io_read_bychar(cmd_io_t *cmd_io,void *buf,int count)
+{
+ int cn=0;
+ int ch;
+ __u8* p=(__u8*)buf;
+ while(count--&&(ch=(*cmd_io->getc)(cmd_io))>=0){
+ *p++=ch;
+ cn++;
+ }
+ return cn;
+}
+
--- /dev/null
+/*******************************************************************
+ Components for embedded applications builded for
+ laboratory and medical instruments firmware
+
+ cmd_proc.h - text line command processor
+ designed for instruments control and setup
+ over RS-232 line
+
+ Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
+ (C) 2002 by PiKRON Ltd. http://www.pikron.com
+ (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
+
+ This file can be used and copied according to next
+ license alternatives
+ - MPL - Mozilla Public License
+ - GPL - GNU Public License
+ - other license provided by project originators
+ *******************************************************************/
+
+#include <cmd_proc.h>
+#include "cmd_proc_priv.h"
+#include <string.h>
+
+/* cmd_io line editor */
+
+/**
+ * Adds new characters to an edit line buffer.
+ *
+ * @param elb Edit line buffer.
+ * @param ch character to add.
+ *
+ * @return 1 in case of end of line, 0 otherwise.
+ */
+int cmd_ed_line_buf(ed_line_buf_t *elb, int ch)
+{
+ int lastch=elb->lastch;
+ if (elb->flg&FL_ELB_INSEND)
+ return -1;
+ elb->lastch=ch;
+ if(!lastch){
+ elb->inbuf=0; /* Start new line */
+ }
+ if((!(elb->flg&FL_ELB_NOCRLF))&&((ch=='\n')||(ch=='\r'))){
+ if((lastch=='\n')&&(ch=='\r')) /* Empty line, ignore it. */
+ return 0;
+ elb->lastch=0; /* End the string */
+ elb->buf[elb->inbuf]=0;
+ return 1;
+ }
+ if(elb->inbuf>=elb->alloc-1){
+ /* try to reallocate buffer len not implemented */
+ return 0;
+ }
+ elb->buf[elb->inbuf++]=ch;
+ return 0;
+}
+
+int cmd_io_line_putc(cmd_io_t *cmd_io,int ch)
+{
+ return cmd_ed_line_buf(cmd_io->priv.ed_line.out,ch);
+}
+
+/* Process pending output */
+int cmd_io_line_out(cmd_io_t *cmd_io)
+{
+ cmd_io_t* io_stack=cmd_io->priv.ed_line.io_stack;
+ ed_line_buf_t* ed_line_out=cmd_io->priv.ed_line.out;
+
+ if(!ed_line_out->inbuf) return 0;
+ if(!io_stack)
+ return -1;
+
+ if(!(ed_line_out->flg&FL_ELB_INSEND)){
+ ed_line_out->flg|=FL_ELB_INSEND;
+ ed_line_out->lastch=0;
+ }
+ while(cmd_io_putc(io_stack, ed_line_out->buf[ed_line_out->lastch])>=0){
+ if(++ed_line_out->lastch >= ed_line_out->inbuf){
+ ed_line_out->lastch=0;
+ ed_line_out->inbuf=0;
+ ed_line_out->flg&=~FL_ELB_INSEND;
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/* process input */
+int cmd_io_line_in(cmd_io_t *cmd_io)
+{
+ int ch;
+ cmd_io_t* io_stack = cmd_io->priv.ed_line.io_stack;
+ ed_line_buf_t *ed_line_in = cmd_io->priv.ed_line.in;
+
+ if(!io_stack)
+ return -1;
+
+ while((ch=cmd_io_getc(io_stack))>=0){
+// DPRINT("Added %c (%d)\n", ch, ch);
+ int eol = cmd_ed_line_buf(ed_line_in,ch);
+ if(eol){
+ if(ed_line_in->flg&FL_ELB_ECHO){
+ while(cmd_io_putc(io_stack,'\r')<0);
+ while(cmd_io_putc(io_stack,'\n')<0);
+ }
+ return 1;
+ }
+ else
+ if(ed_line_in->flg&FL_ELB_ECHO) {
+ while(cmd_io_putc(io_stack,ch)<0);
+ }
+ }
+ return 0;
+}
+
+/* char *cmd_io_line_rdline(cmd_io_t *cmd_io, int mode) */
+/* { */
+/* int ret; */
+/* while((ret=cmd_rs232_line_in(cmd_io))==0) */
+/* if(!mode) break; */
+/* if(ret<=0) return NULL; */
+/* return cmd_io->priv.ed_line.in->buf; */
+/* } */
+
+/* Local Variables: */
+/* c-basic-offset: 2 */
+/* End */
Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002 by PiKRON Ltd. http://www.pikron.com
+ (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
This file can be used and copied according to next
license alternatives
- other license provided by project originators
*******************************************************************/
-#include "cmd_proc_config.h"
#include <types.h>
#include <ctype.h>
#include <string.h>
-#ifndef CONFIG_NO_STDIO
-# include <utils.h>
-# include <stdio.h>
-#endif
#include <stdlib.h>
#include <cmd_proc.h>
-
-/* generic cmd_io tructure */
-
-#if 0 /* declared inline */
-
-inline int cmd_io_putc(struct cmd_io *cmd_io,int ch)
-{ if(!cmd_io->putc) return -1;
- return (*cmd_io->putc)(cmd_io,ch);
-}
-
-inline int cmd_io_getc(struct cmd_io *cmd_io)
-{ if(!cmd_io->getc) return -1;
- return (*cmd_io->getc)(cmd_io);
-}
-
-inline int cmd_io_write(struct cmd_io *cmd_io,const void *buf,int count)
-{ if(!cmd_io->write) return -1;
- return (*cmd_io->write)(cmd_io,buf,count);
-}
-
-inline int cmd_io_read(struct cmd_io *cmd_io,void *buf,int count)
-{ if(!cmd_io->read) return -1;
- return (*cmd_io->read)(cmd_io,buf,count);
-}
-
-
-#endif /* declared inline */
-
-int cmd_io_send_string(cmd_io_t *cmd_io, char *str)
-{
- for (; *str; str++) {
- cmd_io_putc(cmd_io, *str);
- }
- return 0;
-};
-
-/* cmd_io line editor */
-
-/**
- * Adds new characters to edit line buffers.
- *
- * @param elb
- * @param ch character to add.
- *
- * @return 1 in case of end of line, 0 otherwise.
- */
-
-int cmd_ed_line_buf(ed_line_buf_t *elb, int ch)
-{
- int lastch=elb->lastch;
- elb->lastch=ch;
- if(!lastch){
- elb->inbuf=0; /* Start the new line */
- }
- if((!(elb->flg&FL_ELB_NOCRLF))&&((ch=='\n')||(ch=='\r'))){
- if((lastch=='\n')&&(ch=='\r')) /* Empty line, ignore it. */
- return 0;
- elb->lastch=0; /* End the string */
- elb->buf[elb->inbuf]=0;
- return 1;
- }
- if(elb->inbuf>=elb->alloc-1){
- /* try to reallocate buffer len not implemented */
- return 0;
- }
- elb->buf[elb->inbuf++]=ch;
- return 0;
-}
-
-int cmd_io_putc_ed_line(cmd_io_t *cmd_io,int ch)
-{
- return cmd_ed_line_buf(cmd_io->priv.ed_line.out,ch);
-}
-
-int cmd_io_write_bychar(cmd_io_t *cmd_io,const void *buf,int count)
-{
- int cn=0;
- __u8* p=(__u8*)buf;
- while(count--&&(*cmd_io->putc)(cmd_io,*p++)>=0){
- cn++;
- }
- return cn;
-}
-
-int cmd_io_read_bychar(cmd_io_t *cmd_io,void *buf,int count)
-{
- int cn=0;
- int ch;
- __u8* p=(__u8*)buf;
- while(count--&&(ch=(*cmd_io->getc)(cmd_io))>=0){
- *p++=ch;
- cn++;
- }
- return cn;
-}
+#include "cmd_proc_priv.h"
/* cmd_line processing */
return p;
}
-#define CMD_ARR_STACK_SIZE 4
-
+/**
+ *
+ * @return Zero if no command was given in line, -CMDERR_BADCMD if
+ * command is not known or has no function assigned to it. If a
+ * command is executed, then the return value of the command function
+ * is returned.
+ */
int proc_cmd_line(cmd_io_t *cmd_io, cmd_des_t const **des_arr, char *line)
{
char *p=line, *r, *var;
int res, i, parcnt;
param[0]=p=skip_white(p);
if(!*p) return 0;
+ /* Determine the name of the command */
if(!isalnum((__u8)*p)){
cmd_len=1;
p++;
}
param[1]=param[2]=skip_white(p);
+ /* Find the command in des_arr */
while(1){
des=*(des_arr++);
if(!des){
des_arr=arr_stack[--arr_stack_sp];
continue;
}
- if(des==CMD_DES_CONTINUE_AT){
+ if(des==CMD_DES_CONTINUE_AT_ID){
/* list continues at new address */
des_arr=(const cmd_des_t **)*des_arr;continue;
}
- if(des==CMD_DES_INCLUDE_SUBLIST){
+ if(des==CMD_DES_INCLUDE_SUBLIST_ID){
/* list includes commands from sublists */
if(arr_stack_sp>=CMD_ARR_STACK_SIZE){
des_arr++;
var=NULL;
while(*r){
while((*p==*r)&&i){i--;r++;p++;};
- if((i==0)&&!*r) break;
+ if((i==0)&&!*r) break; /* We've found the command */
if((*r=='?')&&i){
if(!var) var=p;
p++; r++; i--;
i=1;
break;
}
- if(i!=0) continue;
+ if(i!=0) continue; /* Try next command */
if(des->mode&CDESM_OPCHR){
if(!param[2])continue;
if(!*param[2])continue;
return -CMDERR_BADCMD;
}
-
+/**
+ * Checks whether the the command allows the operation specified by
+ * opchar.
+ *
+ * @return opchar if perimssions allow this operations, -CMDERR_WRPERM
+ * or -CMDERR_RDPERM if the operation is not allows, -CMDERR_OPCHAR,
+ * if the opchar is not ':' or '?'.
+ */
int cmd_opchar_check(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
{
int opchar=*param[2];
return 0;
}
-int cmd_do_help(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
-{
- char *help;
- char *filt=param[1];
- const cmd_des_t **des_arr=*(const cmd_des_t ***)des->info[0];
- cmd_des_t const **arr_stack[CMD_ARR_STACK_SIZE];
- int arr_stack_sp=0;
-
- if(filt) {
- filt=skip_white(filt);
- if(!*filt) filt=NULL;
- }
-#ifndef CONFIG_NO_STDIO
- printf("Help for commands\n");
-#else
- cmd_io_send_string(cmd_io, "Help for commands\n");
-#endif
- while(1){
- des=*(des_arr++);
- if(!des){
- if(!arr_stack_sp) break;
- des_arr=arr_stack[--arr_stack_sp];
- continue;
- }
- if(des==CMD_DES_CONTINUE_AT){
- /* list continues at new address */
- des_arr=(const cmd_des_t **)*des_arr;
- continue;
- }
- if(des==CMD_DES_INCLUDE_SUBLIST){
- /* list includes commands from sublists */
- if(arr_stack_sp>=CMD_ARR_STACK_SIZE){
- des_arr++;
- }else{
- arr_stack[arr_stack_sp++]=des_arr+1;
- des_arr=(const cmd_des_t **)*des_arr;
- continue;
- }
- }
- if(des->name){
- if(!filt || !strncmp(des->name,filt,strlen(filt))) {
- help=des->help;
- if(!help) help="?";
-#ifdef CONFIG_NO_STDIO
- cmd_io_send_string(cmd_io,des->name);
- cmd_io_send_string(cmd_io," - ");
- cmd_io_send_string(cmd_io,help);
- cmd_io_send_string(cmd_io, "\n");
-#else
- printf(" %s - %s\n",des->name,help);
-#endif
- }
- }
- }
- return 0;
-}
-
+/**
+ * Converts integer to string.
+ * @param s Buffer to store the result.
+ * @param val Value to convert.
+ * @param len Minimal width of the converted strign (padded by ' ').
+ * @param form Unused.
+ * @return 0
+ */
int i2str(char *s,long val,int len,int form)
{
int sig;
return 0;
}
-
+/**
+ * Implementation of a command that reads or writes short pointed by des->info[0].
+ */
int cmd_do_rw_short(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
{
short val;
return 0;
}
-
+/**
+ * Implementation of a command that reads or writes int pointed by des->info[0].
+ */
int cmd_do_rw_int(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
{
int val;
}
+/**
+ * Implementation of a command that reads or writes long pointed by des->info[0].
+ */
int cmd_do_rw_long(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
{
long val;
return 0;
}
+/**
+ * Prints name of the command followed by '=' and the value of val.
+ */
int cmd_opchar_replong(cmd_io_t *cmd_io, char *param[], long val,int len,int form)
{
char str[20];
}
#endif
+
+/**
+ * Implementation of help command
+ */
+int cmd_do_help(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ char *help;
+ char *filt=param[1];
+ const cmd_des_t **des_arr=*(const cmd_des_t ***)des->info[0];
+ cmd_des_t const **arr_stack[CMD_ARR_STACK_SIZE];
+ int arr_stack_sp=0;
+
+ if (cmd_io->priv.ed_line.io_stack)
+ cmd_io = cmd_io->priv.ed_line.io_stack;
+
+ if(filt) {
+ filt=skip_white(filt);
+ if(!*filt) filt=NULL;
+ }
+ cmd_io_puts(cmd_io,"Help for commands\n");
+ while(1){
+ des=*(des_arr++);
+ if(!des){
+ if(!arr_stack_sp) break;
+ des_arr=arr_stack[--arr_stack_sp];
+ continue;
+ }
+ if(des==CMD_DES_CONTINUE_AT_ID){
+ /* list continues at new address */
+ des_arr=(const cmd_des_t **)*des_arr;
+ continue;
+ }
+ if(des==CMD_DES_INCLUDE_SUBLIST_ID){
+ /* list includes commands from sublists */
+ if(arr_stack_sp>=CMD_ARR_STACK_SIZE){
+ des_arr++;
+ }else{
+ arr_stack[arr_stack_sp++]=des_arr+1;
+ des_arr=(const cmd_des_t **)*des_arr;
+ continue;
+ }
+ }
+ if(des->name){
+ if(!filt || !strncmp(des->name,filt,strlen(filt))) {
+ help=des->help;
+ if(!help) help="?";
+ cmd_io_puts(cmd_io,des->name);
+ cmd_io_puts(cmd_io," - ");
+ cmd_io_puts(cmd_io,help);
+ cmd_io_puts(cmd_io, "\n");
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Executes command processor. This function is usually called from
+ * application's main loop.
+ */
+int cmd_processor_run(cmd_io_t *cmd_io, cmd_des_t const **commands)
+{
+ int val;
+
+ if(cmd_io_line_out(cmd_io))
+ return 1; /* Not all the output has been sent. */
+
+ if(cmd_io_line_in(cmd_io)<=0)
+ return 0; /* Input line not finished or error. */
+
+ if(commands){
+ val=proc_cmd_line(cmd_io, commands, cmd_io->priv.ed_line.in->buf);
+ }else{
+ val=-CMDERR_BADCMD;
+ }
+
+ if(cmd_io->priv.ed_line.out->inbuf){
+ cmd_io_putc(cmd_io,'\r');
+ cmd_io_putc(cmd_io,'\n');
+ }else if(val<0){
+ char s[20];
+ cmd_io_puts(cmd_io,"ERROR ");
+ i2str(s,-val,0,0);
+ cmd_io_puts(cmd_io,s);
+ cmd_io_putc(cmd_io,'\r');
+ cmd_io_putc(cmd_io,'\n');
+ }
+ return 1; /* Command line processed */
+}
+
+
+/* Local Variables: */
+/* c-basic-offset: 2 */
+/* End */
Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
(C) 2002 by PiKRON Ltd. http://www.pikron.com
+ (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
This file can be used and copied according to next
license alternatives
#define FL_ELB_ECHO 0x10 /* Read characters are echoed back */
#define FL_ELB_INSEND 0x20 /* The line is currently being sent */
#define FL_ELB_NOCRLF 0x40 /* CR and/or LF will not start the new line */
-
-#define CMD_DES_CONTINUE_AT ((cmd_des_t*)1)
-#define CMD_DES_INCLUDE_SUBLIST ((cmd_des_t*)2)
+#define CMD_DES_CONTINUE_AT_ID ((cmd_des_t*)1)
+#define CMD_DES_INCLUDE_SUBLIST_ID ((cmd_des_t*)2)
+#define CMD_DES_CONTINUE_AT(list) CMD_DES_CONTINUE_AT_ID,((cmd_des_t*)list)
+#define CMD_DES_INCLUDE_SUBLIST(list) CMD_DES_INCLUDE_SUBLIST_ID,((cmd_des_t*)list)
/* generic cmd_io tructure */
/* buffer for in/out line collection */
typedef struct{
int flg; /* FL_ELB_xxx */
- int inbuf;
- int alloc;
+ int inbuf; /* Index to store new characters */
+ int alloc; /* Size of the buffer pointed by buf */
int maxlen;
- int lastch;
+ int lastch; /* Last characted added to the buffer.
+ * If FL_ELB_INSEND is set, lastch is
+ * index of last sent char. */
char *buf;
} ed_line_buf_t;
-/* Structure for character input output */
+/* Structure for character input output. It is used either for direct
+ * access to IO device or for buffered IO. In the later case,
+ * priv.edline is used to store buffer information. */
typedef struct cmd_io{
int (*putc)(struct cmd_io *cmd_io,int ch);
int (*getc)(struct cmd_io *cmd_io);
return (*cmd_io->read)(cmd_io,buf,count);
}
-int cmd_ed_line_buf(ed_line_buf_t *elb, int ch);
+int cmd_io_puts(cmd_io_t *cmd_io, const char *str);
-int cmd_io_putc_ed_line(cmd_io_t *cmd_io,int ch);
+int cmd_io_line_putc(cmd_io_t *cmd_io,int ch);
int cmd_io_write_bychar(cmd_io_t *cmd_io,const void *buf,int count);
#define CDESM_OPCHR 0x10 /* Command uses operation character */
#define CDESM_RD 0x01 /* Value read is possible */
#define CDESM_WR 0x02 /* Value write is possible */
-#define CDESM_RW CDESM_RD|CDESM_WR /* Both */
+#define CDESM_RW (CDESM_RD|CDESM_WR) /* Both */
typedef struct cmd_des{
int code;
int cmd_opchar_replong(cmd_io_t *cmd_io, char *param[], long val,int len,int form);
-int cmd_send_string(cmd_io_t *cmd_io, char *q);
-
-/* RS232 specific */
-
-cmd_des_t const **cmd_rs232;
-
-cmd_io_t cmd_io_rs232;
-
-int cmd_rs232_line_out(cmd_io_t *cmd_io);
-
-int cmd_rs232_line_in(cmd_io_t *cmd_io);
-
-char *cmd_rs232_rdline(cmd_io_t *cmd_io, int mode);
+int cmd_processor_run(cmd_io_t *cmd_io, cmd_des_t const **commands);
#endif /* _CMD_PROC_H_ */
+
+/* Local Variables: */
+/* c-basic-offset: 2 */
+/* End */
--- /dev/null
+#ifndef CMD_PROC_PRIV_H
+#define CMD_PROC_PRIV_H
+
+/* The maximal depth of command arrays nesting (see CMD_DES_INCLUDE_SUBLIST). */
+#define CMD_ARR_STACK_SIZE 4
+
+char *skip_white(char *p);
+int cmd_io_line_out(cmd_io_t *cmd_io);
+int cmd_io_line_in(cmd_io_t *cmd_io);
+
+#include <stdio.h>
+#define DPRINT(...) fprintf(stderr, __VA_ARGS__)
+
+#endif
--- /dev/null
+/* Definitions of IOs */
+#include <unistd.h>
+#include <cmd_proc.h>
+#include <stdio.h>
+
+static int std_putc(cmd_io_t *cmd_io, int ch) {
+ int r=putchar(ch);
+ fflush(stdout);
+ return r;
+}
+
+//int _getkey_nb(void);
+static int std_getc(cmd_io_t *cmd_io) {
+ return getchar(); /* On UNIX, we don't use non-blocking
+ * variant. */
+ //return _getkey_nb();
+}
+static int std_write(cmd_io_t *cmd_io, const void *buf, int count) {
+ return write(1, buf, count);
+}
+static int std_read(cmd_io_t *cmd_io, void *buf, int count) {
+ return read(0, buf, count);
+}
+
+cmd_io_t cmd_io_std={
+ putc:std_putc,
+ getc:std_getc,
+ write:std_write,
+ read:std_read
+};
+
--- /dev/null
+#include <cmd_proc.h>
+#include <stdio.h>
+
+int cmd_do_testopchar(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ int opchar;
+
+ opchar=cmd_opchar_check(cmd_io,des,param);
+ if(opchar<0) return opchar;
+
+ printf(
+ "cmd_do_testopchar called\n"
+ "param[0]=%s\n"
+ "param[1]=%s\n"
+ "param[2]=%s\n"
+ "param[3]=%s\n"
+ "opchar=%c\n",
+ param[0], param[1], param[2], param[3], opchar);
+
+ return 0;
+}
+
+int cmd_do_testparam(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ printf(
+ "cmd_do_testparam called\n"
+ "param[0]=%s\n"
+ "param[1]=%s\n"
+ "param[2]=%s\n",
+ param[0], param[1], param[2]);
+
+ return 0;
+}
+
+int cmd_do_testerror(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ return -1234;
+}
+
+int cmd_do_test(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ printf("This is the simplest command\n");
+ return 0;
+}
+
+int cmd_do_testcmdio(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+ cmd_io_puts(cmd_io, "The first line of text\n");
+ cmd_io_puts(cmd_io, "The second line of text\n");
+ /* Only ED_LINE_CHARS character can be sent. */
+ return 0;
+}
+
+cmd_des_t const **cmd_list;
+
+cmd_des_t const cmd_des_help={
+ 0, 0,
+ "HELP","prints help for commands",
+ cmd_do_help,{(char*)&cmd_list}};
+
+int val;
+cmd_des_t const cmd_des_val={
+ 0, CDESM_OPCHR|CDESM_RW,
+ "VAL","use ':' or '?' to store/read value of an integer variable",
+ cmd_do_rw_int, {(char*)&val}};
+
+cmd_des_t const cmd_des_valro={
+ 0, CDESM_OPCHR|CDESM_RD,
+ "VALRO","read only access to an integer variable",
+ cmd_do_rw_int, {(char*)&val}};
+
+cmd_des_t const cmd_des_valwo={
+ 0, CDESM_OPCHR|CDESM_WR,
+ "VALWO","write only access to an integer variable",
+ cmd_do_rw_int, {(char*)&val}};
+
+cmd_des_t const cmd_des_opchar_test={
+ 0, CDESM_OPCHR|CDESM_RW,
+ "OPCHAR","opchar test (use ':' or '?' as suffix)",
+ cmd_do_testopchar};
+
+cmd_des_t const cmd_des_opchar_testro={
+ 0, CDESM_OPCHR|CDESM_RD,
+ "OPCHARRO","opchar test (only '?' is allowed)",
+ cmd_do_testopchar};
+
+cmd_des_t const cmd_des_test={
+ 0, 0,
+ "TEST","the simplest command",
+ cmd_do_test};
+
+cmd_des_t const cmd_des_testio={
+ 0, 0,
+ "TESTIO","test of cmd_io inside functions (universal way to print results)",
+ cmd_do_testcmdio};
+
+cmd_des_t const cmd_des_param={
+ 0, 0,
+ "PARAM","test of parameters",
+ cmd_do_testparam};
+
+cmd_des_t const cmd_des_prefix={
+ 0, 0,
+ "PREFIX*","suffix of the command is supplied as a parametr",
+ cmd_do_testparam};
+
+cmd_des_t const cmd_des_num={
+ 0, 0,
+ "NUM##","suffix of the command (two digits) is supplied as a parametr",
+ cmd_do_testparam};
+
+cmd_des_t const cmd_des_char={
+ 0, 0,
+ "CHAR?","suffix of the command (one character) is supplied as a parametr",
+ cmd_do_testparam};
+
+cmd_des_t const cmd_des_charmid={
+ 0, 0,
+ "CHAR?MID","middle character of the command is supplied as a parametr",
+ cmd_do_testparam};
+
+cmd_des_t const cmd_des_hiddedn={
+ 0, 0,
+ "HIDDEN","should not be available",
+ cmd_do_test};
+
+cmd_des_t const cmd_des_error={
+ 0, 0,
+ "TESTERROR","should produce an error",
+ cmd_do_testerror};
+
+/* Command lists */
+
+cmd_des_t const *cmd_list_1[]={
+ &cmd_des_val,
+ &cmd_des_valro,
+ &cmd_des_valwo,
+ &cmd_des_opchar_test,
+ &cmd_des_opchar_testro,
+ NULL
+ };
+
+cmd_des_t const *cmd_list_2[]={
+ &cmd_des_test,
+ &cmd_des_testio,
+ &cmd_des_param,
+ &cmd_des_prefix,
+ &cmd_des_num,
+ &cmd_des_char,
+ &cmd_des_charmid,
+ NULL
+ };
+
+cmd_des_t const *cmd_list_main[]={
+ &cmd_des_help,
+ &cmd_des_error,
+ CMD_DES_INCLUDE_SUBLIST(cmd_list_1),
+ CMD_DES_CONTINUE_AT(cmd_list_2),
+ &cmd_des_hiddedn,
+ NULL
+};
+
+cmd_des_t const **cmd_list = cmd_list_main;
+
+cmd_io_t cmd_io_std_line;
+
+int main()
+{
+ while (1) {
+ cmd_processor_run(&cmd_io_std_line, cmd_list_main);
+ }
+}
--- /dev/null
+# Generic directory or leaf node makefile for OCERA make framework
+
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" == `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) )
+endif
+
+ifeq ($(MAKERULES_DIR),)
+all : default
+.DEFAULT::
+ @echo -e "\nThe Makefile.rules has not been found in this or partent directory\n"
+else
+include $(MAKERULES_DIR)/Makefile.rules
+endif
+
--- /dev/null
+lib_LIBRARIES = cmprocio
+
+cmprocio_SOURCES += cmdio_rs232.c cmdio_rs232_line.c
+
+ifeq ($(CONFIG_BLUETOOTH),y)
+cmprocio_SOURCES += cmdio_bth.c cmdio_bth_line.c
+endif
--- /dev/null
+/*******************************************************************
+ Components for embedded applications builded for
+ laboratory and medical instruments firmware
+
+ cmd_bth.c - interconnection of text command processor
+ with RS-232 line
+
+ Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
+ (C) 2002 by PiKRON Ltd. http://www.pikron.com
+
+ *******************************************************************/
+
+#include <cmd_proc.h>
+#include <bth_inface.h>
+
+#define ED_LINE_CHARS 80
+
+/* cmd_io_t cmd_io_bth_dev; */
+
+/* char ed_line_chars_bth_in[ED_LINE_CHARS+1]; */
+/* char ed_line_chars_bth_out[ED_LINE_CHARS+1]; */
+
+/* ed_line_buf_t ed_line_buf_bth_in={ */
+/* flg:FL_ELB_ECHO, */
+/* inbuf:0, */
+/* alloc:sizeof(ed_line_chars_bth_in), */
+/* maxlen:0, */
+/* lastch:0, */
+/* buf:ed_line_chars_bth_in */
+/* }; */
+
+/* ed_line_buf_t ed_line_buf_bth_out={ */
+/* flg:FL_ELB_NOCRLF, */
+/* inbuf:0, */
+/* alloc:sizeof(ed_line_chars_bth_out), */
+/* maxlen:0, */
+/* lastch:0, */
+/* buf:ed_line_chars_bth_out */
+/* }; */
+
+/* cmd_io_t cmd_io_bth={ */
+/* putc:cmd_io_putc_ed_line, */
+/* getc:NULL, */
+/* write:cmd_io_write_bychar, */
+/* read:NULL, */
+/* priv:{ */
+/* ed_line:{ */
+/* in: &ed_line_buf_bth_in, */
+/* out:&ed_line_buf_bth_out, */
+/* io_stack:&cmd_io_bth_dev */
+/* } */
+/* } */
+/* }; */
+
+int cmd_io_putc_bth(struct cmd_io *cmd_io,int ch)
+{
+ return bth_inface_sendch(ch,0); //=0; //sci_bth_sendch(ch,IMPLIC_NUM_SERIAL_PORT);;
+}
+
+int cmd_io_getc_bth(struct cmd_io *cmd_io)
+{
+ return bth_inface_recch(0);//=-1; //sci_bth_recch(IMPLIC_NUM_SERIAL_PORT);
+}
+
+cmd_io_t cmd_io_bth={
+ putc:cmd_io_putc_bth,
+ getc:cmd_io_getc_bth,
+ write:cmd_io_write_bychar,
+ read:cmd_io_read_bychar
+};
--- /dev/null
+#include <cmd_proc.h>
+#include <string.h>
+
+#define ED_LINE_CHARS 80
+
+char ed_line_in_bth[ED_LINE_CHARS+1];
+char ed_line_out_bth[ED_LINE_CHARS+1];
+
+
+ed_line_buf_t ed_line_buf_in_bth={
+ flg:FL_ELB_ECHO,
+ inbuf:0,
+ alloc:sizeof(ed_line_in_bth),
+ maxlen:0,
+ lastch:0,
+ buf:ed_line_in_bth
+};
+
+ed_line_buf_t ed_line_buf_out_bth={
+ flg:FL_ELB_NOCRLF,
+ inbuf:0,
+ alloc:sizeof(ed_line_out_bth),
+ maxlen:0,
+ lastch:0,
+ buf:ed_line_out_bth
+};
+
+cmd_io_t cmd_io_bth;
+const cmd_io_t cmd_io_bth_line={
+ putc:cmd_io_line_putc,
+ getc:NULL,
+ write:cmd_io_write_bychar,
+ read:NULL,
+ priv:{
+ ed_line:{
+ in: &ed_line_buf_in_bth,
+ out:&ed_line_buf_out_bth,
+ io_stack:&cmd_io_bth
+ }
+ }
+};
--- /dev/null
+/*******************************************************************
+ Components for embedded applications builded for
+ laboratory and medical instruments firmware
+
+ cmd_rs232.c - interconnection of text command processor
+ with RS-232 line
+
+ Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
+ (C) 2002 by PiKRON Ltd. http://www.pikron.com
+
+ This file can be used and copied according to next
+ license alternatives
+ - MPL - Mozilla Public License
+ - GPL - GNU Public License
+ - other license provided by project originators
+
+ *******************************************************************/
+
+#include <cmd_proc.h>
+#include <periph/sci_rs232.h>
+
+static int cmd_io_putc_rs232(struct cmd_io *cmd_io,int ch)
+{
+ return sci_rs232_sendch(ch, sci_rs232_chan_default);
+}
+
+static int cmd_io_getc_rs232(struct cmd_io *cmd_io)
+{
+ return sci_rs232_recch(sci_rs232_chan_default);
+}
+
+cmd_io_t cmd_io_rs232_dev={
+ putc:cmd_io_putc_rs232,
+ getc:cmd_io_getc_rs232,
+ write:cmd_io_write_bychar,
+ read:cmd_io_read_bychar
+};
+
--- /dev/null
+#include <cmd_proc.h>
+#include <string.h>
+
+#define ED_LINE_CHARS 80
+
+char ed_line_in_rs232[ED_LINE_CHARS+1];
+char ed_line_out_rs232[ED_LINE_CHARS+1];
+
+
+ed_line_buf_t ed_line_buf_in_rs232={
+ flg:FL_ELB_ECHO,
+ inbuf:0,
+ alloc:sizeof(ed_line_in_rs232),
+ maxlen:0,
+ lastch:0,
+ buf:ed_line_in_rs232
+};
+
+ed_line_buf_t ed_line_buf_out_rs232={
+ flg:FL_ELB_NOCRLF,
+ inbuf:0,
+ alloc:sizeof(ed_line_out_rs232),
+ maxlen:0,
+ lastch:0,
+ buf:ed_line_out_rs232
+};
+
+cmd_io_t cmd_io_rs232;
+const cmd_io_t cmd_io_rs232_line={
+ putc:cmd_io_line_putc,
+ getc:NULL,
+ write:cmd_io_write_bychar,
+ read:NULL,
+ priv:{
+ ed_line:{
+ in: &ed_line_buf_in_rs232,
+ out:&ed_line_buf_out_rs232,
+ io_stack:&cmd_io_rs232
+ }
+ }
+};