#author("2021-12-05T16:40:45+09:00","default:Real2Virtual202111","Real2Virtual202111")
#author("2024-07-01T11:46:59+09:00","default:TESLA202407","TESLA202407")
[[Real2Virtual202111]]

* CommandBuffer.cpp [#f82114a2]

#code(c){{

//#include "common.h"
#include "CommandBuffer.h"
#include "OutBuffer.h"
#include "InBuffer.h"
#include "StringQueue.h"
#include "mbed.h"
#include "Initialize.h" 


void setPortFunctions();
unsigned char getRegisterValue( int fid);
//CommandBuffer *commandBuffer;


void CommandBuffer::shift( int i){
    int j;
    if(i>p) return;
    for(j=0; j<p-i; j++){
        commandFaceID[j]=commandFaceID[j+i];
        buff[j]=buff[j+i];
    }
    p=p-i;
}

void CommandBuffer::putCommand( int fid, char *s){
   unsigned char i;
   /*
   char a[20];
   sprintf(a,"%d",fid);
   sputs("fid=");
   sputs(a);
   sputs("\n\r");
   */
   initBuffer();
   i=0;
   while(s[i]!=0){
      buff[p]=s[i];
      commandFaceID[p]=fid;
      p++;
      i++;
   }
   buff[p]=0;
}
void CommandBuffer::appendString( int fid, char *s){
   unsigned char i;
   /*
   char a[20];
   sprintf(a,"%d",fid);
   sputs("fid=");
   sputs(a);
   sputs("\n\r");
   */

   i=0;
   while(s[i]!=0){
      buff[(*this).p]=s[i];
      commandFaceID[(*this).p]=fid;
      p++;
      i++;
   }
   buff[p]=0;
//   sendCommand(fid,buff);
}

void CommandBuffer::putChar( int fid, unsigned char c){
     p=0;
   buff[p]=c;
   commandFaceID[p]=fid;
   p++;
   buff[p]=0;
    sendCommand(fid,buff);
}

void CommandBuffer::putInt(int fid, unsigned char i){
    char s[6];
    unsigned char p;
    p=5;
    s[p]=0;
    p--;
    s[p]='0';
    while(i!=0){
//        s[p]='0'+(i%10);
//        i=i/10;
        s[p]='0'+xmod(i,10);
        i=xdiv(i,10);
        if(p==0) break;
        p--;
    }
//    (*this).putCommand(this,fid, &(s[p]));
//    appendString(fid,s);
        sendCommand(fid,s);
}
/*
void strcpy(char *x, char *y){
    int i;
    for(i=0;i<CMax-1;i++){
        x[i]=y[i];
        if(y[i]==0) return;
    }
    x[CMax-1]=0;
}
*/
void CommandBuffer::setName( char *n){
    char *x, *y;
    x=myname;
    y=n;
    xstrcpy(x,y);
//  strcpy(x,y);
}

void CommandBuffer::initBuffer(void){
    p=0;
    buff[p]=0;
}

void CommandBuffer::setLock( unsigned char trueOrFalse){
    lock=trueOrFalse;
}
unsigned char CommandBuffer::isLocked(void){
    return lock;
}

void CommandBuffer::setUpFaceID( int i){
    UpFaceID=i;
}

int CommandBuffer::isBufferNull(void){
    return (p)<=0;
}

int CommandBuffer::startsWith( char *s){
    int p;
    p=0;
    while(s[p]!=0){
       if(s[p]!=buff[p]) return 0;
       p++;
    }
    return 1;
}

void CommandBuffer::startRecursiveReset(int UpFaceID){ // srr
        char rbuffer[32];
        unsigned char i;
        if(!resetFlag) return;
        resetFlag=0;
        for(i=0;i<FaceMax;i++){   // 各面について解析
            if(i!=UpFaceID){      // この面が上位ノードでなければ以下を実行
                if( (((char)0x80) &  faceID[i] ) == 0x00 ){ // 面 i が接続している場合
                    rbuffer[0]='r';
                    rbuffer[1]='e';
                    rbuffer[2]='s';
                    rbuffer[3]='e';
                    rbuffer[4]='t';
                    rbuffer[5]='0'+i;
                    rbuffer[6]=0;
//                  (*this).sendCommand(UpFaceID,rbuffer);
//                  (*this).sendCommand(i,"srr."); // 面 i に接続した dice で再び リセット。
                    sendCommand(UpFaceID,rbuffer);
                    sendCommand((int)i,"srr.");
                }
            }
        }
        resetBuffer(); // このdice を reset.
}


/*
parseUSB

*/


void CommandBuffer::put_error(int fid){
    sendCommand(fid,"error.\n\r");
}
/*
int strlen(char *x){
    int i;
    for(i=0;i<CMax;i++){
        if(x[i]==0) return i;
    }
    return 0;
}
*/
int isNumberLetter(unsigned char c){
    if(c<'0') return 0;
    if('9'<c) return 0;
    return 1;
}

int isA2FLetter(unsigned char c){
    if('A'<=c && c<='F') return 1;
    return 0;
}

int isa2fLetter(unsigned char c){
    if('a'<=c && c<='f')
        return 1;
    return 0;
}
int CommandBuffer::startsWithPInt( int *x){
    unsigned char c;
    int i,n;
    skipSpace();
    c=buff[0];
    if(!isNumberLetter(c)) return 0;
    n=0;
    for(i=0;i<CMax;i++){
        c=buff[i];
        if(isNumberLetter(c)){
            n=n*10+c-'0';
        }
        else
            break;
    }
    *x=n;
    return i;
}

int isHexLetter(unsigned char c){
    if(isA2FLetter(c)) return 1;
    if(isa2fLetter(c)) return 1;
    if(isNumberLetter(c)) return 1;
    return 0;
}

int CommandBuffer::startsWithHexDec( int *x){
    unsigned char c;
    int i,n;
    skipSpace();
    c=buff[0];
    if(!isHexLetter(c)) return 0;
    n=0;
    for(i=0;i<CMax;i++){
        c=buff[i];
        if(isNumberLetter(c)){
            n=n*16+c-'0';
        }
        else
        if(isa2fLetter(c)){
            n=n*16+c-'a'+10;
        }
        else
        if(isA2FLetter(c)){
            n=n*16+c-'A'+10;
        }
        else{
            break;
        }
    }
    *x=n;
    return i;
}

int CommandBuffer::startsWithStringConst( char *x){
    unsigned char c;
    int i,j,k;
    skipSpace();
    c=buff[0];
    if(!(c=='"')) return 0;
    j=0;
    k=1;
    for(i=0;i<CMax;i++){
        c=buff[k];
        if(c==0){
            return 0;
        }
        if(c=='"'){
            x[j]=0;
            return k+1;
        }
        if(c=='\\'){
            k++;
            c=buff[k];
            x[j]=c;
            j++;
        }
        else {
            x[j]=c;
            j++;
        }
        k++;

    }
    return k;
}


void CommandBuffer::skipSpace(void){
    while(startsWith(" ")){
        shift(1);
    }
}

void CommandBuffer::sendHelpMessage( int fid){
//  help message for sh7125
        sendCommand(fid,"\n\r");
        sendCommand(fid,"set port <p> <n> <value>.\n\r");
        sendCommand(fid,"...<p>=PE|PME|PB|PMB <n>=0|1|...|F.\n\r");
        sendCommand(fid,"...<value> is 0 or 1.\n\r");
        sendCommand(fid,".... if <p>=PME|PMB, 0 means output, \n\r");
        sendCommand(fid,"....                 1 means input.\n\r");
        sendCommand(fid,"set mem <n> 1  or set mem <n> 0.\n\r");
        sendCommand(fid,"  ...          <n>=(0|1|...|F)+.\n\r");
        sendCommand(fid,"ask port <p> <n>.\n\r");
        sendCommand(fid,"...<p>=PE|PME|PB|PMB <n>. <n>=0|1|...|F.\n\r");
        sendCommand(fid,"get port <p> <n>.\n\r");
        sendCommand(fid,"...<p>=PE|PME|PB|PMB <n>. <n>=0|1|...|F.\n\r");
        sendCommand(fid,"ask mem <n>.\n\r");
        sendCommand(fid,"  ...          <n>=(0|1|...|F)+.\n\r");
        sendCommand(fid,"get mem <n>.\n\r");
        sendCommand(fid,"  ...          <n>=(0|1|...|F)+.\n\r");
        sendCommand(fid,"set outface <n>.\n\r");
        sendCommand(fid,"set face <n> allLED <value>.\n\r");
        sendCommand(fid,"set face <n> led|dir <value>.\n\r");
        sendCommand(fid,"send face current <strconst>.\n\r");
        sendCommand(fid,"send face <n> <strconst>.\n\r");
        sendCommand(fid,"send root <n> <strconst>.\n\r");
        sendCommand(fid,"bcast face <n> <strconst>.\n\r");
        sendCommand(fid,"cq face <n> dir <value>.\n\r");
        sendCommand(fid,"ack1 face <n> dir <value> uDir <value>.\n\r");
        sendCommand(fid,"ack2 face <n> dir <value> uDir <value>.\n\r");
        sendCommand(fid,"h. or help. or ?\n\r");
        sendCommand(fid,"srs.  ... start recursive search.\n\r");
        sendCommand(fid,"srr.  ... start recursive reset.\n\r");
}

// int CommandBuffer::response2cq( int fid, char *n, int nextDoor, int nextDir){
 int CommandBuffer::response2cq( int fid, int nextDoor, int nextDir){
    int i;
    OutBuffer *ob;
    InBuffer *ib;
//  xstrcpy(myname,n);
    char s[CMax];
    char sn[10];
//  char w[100];
//  sprintf(w,"response2cq,fid=%d, nextDoor=%d nextDir=%d\n",fid,nextDoor,nextDir);
//  UART_puts(w);
    xputs("response2cq,fid=");xputd(fid);
//  xputs(",upName=");xputs(myname);
    xputs(", nextDoor=");xputd(nextDoor);
    xputs(" nextDir=");xputd(nextDir);xputs(".\n\r");
    if(fid<0) return 0;
    if(fid>=FaceMax) return 0;
    if(nextDoor<0) return 0;
    if(nextDoor>=FaceMax) return 0;
    if(nextDir<0) return 0;
    if(nextDir>=DirMax) return 0;
    faceID[fid]=nextDoor;
    dirID[fid]=nextDir;
  ib=faceIn[fid];
    (*ib).setConnected((unsigned char)1);
    
//  sprintf(w,"parameter check passed.\n");
//  UART_puts(w);
    xputs("parameter check passed.\n\r");
    ob=faceOut[fid];
//  (*ob).clear();
//    currentOutFace= ob;
//  for(i=0;i<DirMax;i++){
  i=nextDir;
        (*ob).waitUntilOutputDone();
        s[0]=0;
//      printf("dir=%d ",i);
        (*ob).setLedNumber((unsigned char)i);
        (*ob).setAllOrOne((unsigned char)0);
        strcat(s,"ack1 face ");
        sn[0]='0'+fid; sn[1]=0;
        strcat(s,sn);
        strcat(s," dir ");
        sn[0]='0'+(unsigned char)i; sn[1]=0;
        strcat(s,sn);
        strcat(s," uDir ");
        sn[0]='0'+nextDir; sn[1]=0;
        strcat(s,sn);
        strcat(s,".");
//      printf(" ..s=%s\n",s);
//      xputs(" ..s=");xputs(s);xputs("\n\r");
//      (*ob).putString(ob,s);
//      (*ob).putStringX(s);
    sendCommand(fid,s);
//  (*ob).waitUntilOutputDone();
//  }
    return 1;
}

int CommandBuffer::response2ack1( int fid, int nextDoor, int dir, int uDir){
//  int i;
  char rbuffer[MaxInterFaceStrLen];
    char rbuffer2[5];
    char cmd2next[60];  
    OutBuffer *ob;
    InBuffer *ib;

    xputs("this face(");xputd(fid);xputs("):dir(");xputd(uDir);
    xputs(")<-> next face(");xputd(nextDoor);xputs("):dir(");xputd(dir);xputs(")\n\r");
    if(fid<0) return 0;
    if(fid>=FaceMax) return 0;
    if(nextDoor<0) return 0;
    if(nextDoor>=FaceMax) return 0;
    if(dir<0) return 0;
    if(dir>=DirMax) return 0;
    if(uDir<0) return 0;
    if(uDir>=DirMax) return 0;
//  s[0]=0;
    faceID[fid]=nextDoor;
    dirID[fid]=dir;
    myDirID[fid]=uDir;
    ob=faceOut[fid];
    (*ob).clear();
//    currentOutFace= ob;
//    (*ob).waitUntilOutputDone();
    (*ob).setLedNumber((unsigned char)uDir);
    (*ob).setAllOrOne((unsigned char)0);
    ib=faceIn[fid];
    (*ib).setConnected((unsigned char)1);

    rbuffer[0]='\0';     // send up (this "<myame>" f<k>d<l> next "<downname>" f<k>d<l>)  を上位diceに伝える
    strcpy(rbuffer,"send up \"str (this ");
    strcat(rbuffer,myname);
    strcat(rbuffer," f");
    rbuffer2[0]=fid+'0';
    rbuffer2[1]='d';
    rbuffer2[2]=dirID[fid]+'0';
    rbuffer2[3]=' ';
    rbuffer2[4]=0;
    strcat(rbuffer,rbuffer2);
    strcat(rbuffer," next ");
    strcat(rbuffer,myname);
    rbuffer2[0]=faceID[fid]+'0';
    rbuffer2[1]=0;  
    strcat(rbuffer, rbuffer2);
    strcat(rbuffer," f");   
    strcat(rbuffer,rbuffer2);
    strcat(rbuffer,"d");
    rbuffer2[0]=dirID[fid]+'0';
    rbuffer2[1]=0;
    strcat(rbuffer,rbuffer2);
    strcat(rbuffer,")\".");
    printf("...%s\n\r",rbuffer);
    strcpy(cmd2next,"srs \"");
    strcat(cmd2next,myname);
    strcat(cmd2next,"\".");
     printf("...%s\n\r",cmd2next);
     (*ob).setPortXOff();
     sendCommand(UpFaceID,rbuffer);  
     (*ob).setAllOrOne((unsigned char)0);
     (*ob).setPortXOn();
     sendCommand(fid,cmd2next); // 面 i に接続した dice で再び 構造解析を開始する。     
     return 1;
}

int CommandBuffer::response2ack2( int fid, int nextDoor, int dir, int uDir){
//  int i;
//  struct outBuffer *ob;
//  char s[CMax];
//  char sn[10];
//  char w[100];
//    sprintf(w,"this face(%d):dir(%d)<-> next face(%d):dir(%d)\n",
//            fid,uDir, nextDoor,dir);
//    UART_puts(w);
    xputs("this face(");xputd(fid);xputs("):dir(");xputd(uDir);
    xputs(")<-> next face(");xputd(nextDoor);xputs("):dir(");xputd(dir);xputs(")\n");
    if(fid<0) return 0;
    if(fid>=FaceMax) return 0;
    if(nextDoor<0) return 0;
    if(nextDoor>=FaceMax) return 0;
    if(dir<0) return 0;
    if(dir>=DirMax) return 0;
    if(uDir<0) return 0;
    if(uDir>=DirMax) return 0;
//  s[0]=0;
//  (*this).faceID[fid]=nextDoor;
    dirID[fid]=dir;
    myDirID[fid]=uDir;
    return 1;
}

void CommandBuffer::interpretCommand( int fid){
    char s[CMax];
    int n,l,x;
    OutBuffer *cout;
    //asm("FSET I");                                        //【2】割り込み許可
    //if(cpld()<0) return;                                   // poling... cpld との情報交換処理。
//  sputs((*this).buff);
    if(isBufferNull()) return;
//  fid=(*this).commandFaceID[0];
    if(startsWith( "?")){
        sendHelpMessage(fid);
        return;
    }
    if(startsWith( "h.")){
        sendHelpMessage(fid);
        return;
    }
    if(startsWith( "help.")){
//      sputs("1.");
//      sputs("2.");
        sendHelpMessage(fid);
//      sputs("3.");
        return;
    }
    if(startsWith( "error.")){
//      sputs("1.");
//      sputs("2.");
//        printf("error.\n");
        xputs("error.\n");
//      sputs("3.");
        return;
    }
    if(startsWith( "srs.")){ //再帰的構造解析開始 (start recursive search)
    if(startsWith( "srs.")){ //(start recursive search)
        startRecursiveStructureSearch( fid,"s");
        return;
    }
    if(startsWith( "srs ")){ //再帰的構造解析開始 (start recursive search)
    if(startsWith( "srs ")){ //(start recursive search)
            shift(strlen("srs "));
            skipSpace();
            l=startsWithStringConst(s);
            if(l>0){
            startRecursiveStructureSearch( fid,s);
                }
                else{
                     put_error(fid);
                }
            return;
    }
    
    if(startsWith("srr.")){ //再帰的リセット (start recursive reset)
    if(startsWith("srr.")){ //(start recursive reset)
//      if(!FirstTime) return;
        UpFaceID=fid;
//      FirstTime=0;
        startRecursiveReset( fid);
        return;
    }
    if(startsWith("ask ")){
        shift(strlen("ask "));
        skipSpace();
        if(!getRegisterValue(fid)){
            put_error(fid);
        }
        return;
    }
    if(startsWith("get ")){
        shift(strlen("get "));
        skipSpace();
        if(!getRegisterValue(fid)){
            put_error(fid);
        }
        return;
    }
    if(startsWith("set ")){
        shift(strlen("set "));
        if(startsWith("mem ")){
                l=startsWithHexDec(&n);
                shift(l);
                if(!(startsWith(" "))){
                    put_error(fid);
                    return;
                }
                l=startsWithHexDec(&x);
                shift(l);
                if(0<=n && n<16){
//                  (*x)=n;
                    sendCommand(fid,"\n\r");
                }
                else{
                    put_error(fid);
                    return;
                }

        }
        if(startsWith("port ")){
            shift(strlen("port "));
            skipSpace();
            if(startsWith("PE ")){
                shift(strlen("PE "));
                l=startsWithHexDec(&n);
                shift(l);
                if(!(startsWith(" "))){
                    put_error(fid);
                    return;
                }
                l=startsWithHexDec(&x);
                shift(l);
                if(0<=n && n<16){
//                  printf("set port PE %d %d.\n\r",n,x);
                    xputs("set port PE ");xputd(n);xputs(" ");xputd(x);xputs(".\n\r");
//                  (*setPEX[n])((unsigned char)x);
                    setPEX_i(n,(unsigned char)x);
                    return;
                }
                else{
                    sendCommand(fid,"range error\n\r");
                    return;
                }
            }
            else
            if(startsWith("PB ")){
                shift(strlen("PB "));
                l=startsWithHexDec(&n);
                shift(l);
                if(!(startsWith(" "))){
                    put_error(fid);
                    return;
                }
                l=startsWithHexDec(&x);
                shift(l);
                if(0<=n && n<6){
//                  char w[50];
//                  sprintf(w,"set port PB %d %d.\n\r",n,x);
//                    UART_puts(w);
                    xputs("set port PB ");xputd(n);xputs(" ");xputd(x);xputs(".\n\r");
//                  (*setPBX[n])((unsigned char)x);
                    setPBX_i(n,(unsigned char)x);
                    return;
                }
                else{
                    sendCommand(fid,"range error\n\r");
                    return;
                }
            }
            else
            if(startsWith("PME ")){
                shift(strlen("PME "));
                l=startsWithHexDec(&n);
                shift(l);
                if(!(startsWith(" "))){
                    put_error(fid);
                    return;
                }
                l=startsWithHexDec(&x);
                shift(l);
                if(0<=n && n<16){
//                  char w[50];
//                    sprintf(w,"set port PME %d %d.\n\r",n,x);
//                   UART_puts(w);
                    xputs("set port PME ");xputd(n);xputs(" ");xputd(x);xputs(".\n\r");
//                  (*setPMEX[n])((unsigned char)x);
                      setPMEX_i(n,(unsigned char)x);
                    sendCommand(fid,"\n\r");
                    return;
                }
                else{
                    sendCommand(fid,"range error\n\r");
                    return;
                }
            }
            else
            if(startsWith("PMB ")){
                shift(strlen("PMB "));
                l=startsWithHexDec(&n);
                shift(l);
                if(!(startsWith(" "))){
                    put_error(fid);
                    return;
                }
                l=startsWithHexDec(&x);
                shift(l);
                if(0<=n && n<6){
//                  char w[50];
//                    sprintf(w,"set port PMB %d %d.\n\r",n,x);
//                    UART_puts(w);
                    xputs("set port PMB ");xputd(n);xputs(" ");xputd(x);xputs(".\n\r");
//                  (*setPMBX[n])((unsigned char)x);
                      setPMBX_i(n,(unsigned char)x);
                    sendCommand(fid,"\n\r");
                    return;
                }
                else{
                    sendCommand(fid,"range error\n\r");
                    return;
                }
            }
            else{
                put_error(fid);
                return;
            }
        }
        if(startsWith("outface ")){
                shift(strlen("outface "));
                l=startsWithHexDec(&n);
                shift(l);
                if(0<=n && n<6){
                    currentOutFace= faceOut[n];
                    sendCommand(fid,"\n\r");
                    return;
                }
                else{
                    sendCommand(fid,"range error\n\r");
                    return;
                }
        }
        if(startsWith("face ")){
            shift(strlen("face "));
            l=startsWithHexDec(&n);
            shift(l);
            skipSpace();
            if(startsWith("allLED ")||startsWith("allled ")){
                    shift(strlen("allLED "));
                    l=startsWithHexDec(&x);
                    shift(l);
                    skipSpace();
                    cout=faceOut[n];
                    (*cout).setAllOrOne((unsigned char )x);
                    sendCommand(fid,"\n\r");
                    return;
            }
            if(startsWith("led ")||startsWith("dir ")){
                    shift(strlen("led "));
                    l=startsWithHexDec(&x);
                    shift(l);
                    skipSpace();
                    cout=faceOut[n];
                    (*cout).setLedNumber((unsigned char )x);
                    sendCommand(fid,"\n\r");
                    return;
            }
        }
    }
    if( parseSend(fid)){
        return;
    }
    if( parseCq(fid)){ // cq face <f> dir <d>
        return;
    }
    if(parseAck1(fid)){ // ack1 face <n> dir <d> uDir <d>
        return;
    }
    if(parseAck2(fid)){
         return;
    }
  put_error(fid);
}
int CommandBuffer::parseSend(int fid){
    char s[CMax];
    int n,l;
//  OutBuffer *cout;
    if(startsWith("send ")){
        shift(strlen("send "));
        if(startsWith("face ")){
                shift(strlen("face "));
                if(startsWith("current ")){
                    shift(strlen("current "));
                    l=startsWithStringConst(s);
//                  (*((*this).currentOutFace)).putString((*this).currentOutFace,s);
//                  putStringX(currentOutFace,s);
                    (*currentOutFace).putStringX(s);
//                  sendCommand(fid,"\n\r");
                    return 1;
                }
                l=startsWithHexDec(&n);
                if(l>0){
                    shift(l);
                    if(0<=n && n<6){
                        currentOutFace= faceOut[n];
                        l=startsWithStringConst(s);
//                      (*((*this).currentOutFace)).putString((*this).currentOutFace,s);
                        (*currentOutFace).putStringX(s);
//                      sendCommand(fid,"\n\r");
                        return 1;
                    }
                    else{
                        put_error(fid);
                        return 0;
                    }
                }
        }
        if(startsWith("up ")){
                shift(strlen("up "));
                l=startsWithStringConst(s);
              if(l>0){
                      if(UpFaceID<0){
                              xputs(s); xputs("\n\r");
                        }
                        else{
                            char cmd[MaxInterFaceStrLen];
                            cmd[0]='\0';
                            strcpy(cmd,"send up \"");
                            strcat(cmd,s);
                            strcat(cmd,"\".");
//                          OutBuffer *obuf=faceOut[UpFaceID];
//                          setCurrentOutFace(obuf);
//                  (*((*this).currentOutFace)).putString((*this).currentOutFace,s);
//                  putStringX(currentOutFace,s);
//                    (*currentOutFace).putStringX(s);
//                  sendCommand(fid,"\n\r");
                            sendCommand(UpFaceID,cmd);
                        }
                      return 1;
                }
        }       
        /*
        if(startsWith("bcast ")){
                shift(strlen("bcast "));
              if(startsWith("from ")){
                      shift(strlen("from "));
                      l=startsWithStringConst(s);
                      if(l>0){
                              if(
                        }
                        
                }
              if(l>0){
                      if(UpFaceID<0){
                              xputs(s); xputs("\n\r");
                        }
                        else{
                            char cmd[80];
                            strcpy(cmd,"send up \"");
                            strcat(cmd,s);
                            strcat(cmd,"\".");
//                  (*((*this).currentOutFace)).putString((*this).currentOutFace,s);
//                  putStringX(currentOutFace,s);
                    (*currentOutFace).putStringX(s);
//                  sendCommand(fid,"\n\r");
                        }
                      return 1;
                }
        }   
*/      
        return 0;
    }   
    return 0;
}
int CommandBuffer::parseCq(int fid){
    int n,d,l;  
    if(startsWith("cq ")){ // cq face <f> dir <d>
        shift(strlen("cq "));
//      if(!startsWith("name ")){
//          put_error(fid);
//          return;         
//      }
//      shift(strlen("name "));
//      l=startsWithStringConst(s);
//    if(l<0){
//          put_error(fid);
//          return;
//      }
//      shift(l);
        if(!startsWith("face ")){
            put_error(fid);
            return 0;
        }
        shift(strlen("face "));
        l=startsWithHexDec(&n);
        if(l<=0){
            put_error(fid);
            return 0;
        }
        shift(l);
        if(!startsWith(" dir ")){
            put_error(fid);
            return 0;
        }
        shift(strlen(" dir "));
        l=startsWithHexDec(&d);
        if(l<=0){
            put_error(fid);
            return 0;
        }
        shift(l);
//      if(!response2cq(fid,s,n,d)){
        if(!response2cq(fid,n,d)){
            put_error(fid);
              return 0;
        }
        return 1;
    }   
    return 0;
}
int CommandBuffer::parseAck1(int fid){
    int n,d,e,l;    
    if(startsWith("ack1 ")){ // ack1 face <n> dir <d> uDir <d>
        shift(strlen("ack1 "));
        if(!startsWith("face ")){
            put_error(fid);
            return 0;
        }
        shift(strlen("face "));
        l=startsWithHexDec(&n);
        if(l<=0){
            put_error(fid);
            return 0;
        }
        shift(l);
        if(!startsWith(" dir ")){
            put_error(fid);
            return 0;
        }
        shift(strlen(" dir "));
        l=startsWithHexDec(&d);
        if(l<=0){
            put_error(fid);
            return 0;
        }
        shift(l);
        if(!startsWith(" uDir ")){
            put_error(fid);
            return 0;
        }
        shift(strlen(" uDir "));
        l=startsWithHexDec(&e);
        if(l<=0){
            put_error(fid);
            return 0;
        }
        shift(l);
        if(!response2ack1(fid,n,d,e)){
            put_error(fid);
                      return 0;
        }
        return 1;
    }   
    return 0;
}
int CommandBuffer::parseAck2(int fid){
    int n,d,e,l;    
    if(startsWith("ack2 ")){
        shift(strlen("ack2 "));
        if(!startsWith("face ")){
            put_error(fid);
            return 0;
        }
        shift(strlen("face "));
        l=startsWithHexDec(&n);
        if(l<=0){
            put_error(fid);
            return 0;
        }
        shift(l);
        if(!startsWith(" dir ")){
            put_error(fid);
            return 0;
        }
        shift(strlen(" dir "));
        l=startsWithHexDec(&d);
        if(l<=0){
             put_error(fid);
             return 0;
        }
        shift(l);
        if(!startsWith(" uDir ")){
            put_error(fid);
            return 0 ;
        }
        shift(strlen(" udir "));
        l=startsWithHexDec(&e);
        if(l<=0){
             put_error(fid);
             return 0;
        }
        shift(l);
        if(!response2ack2(fid,n,d,e)){
            put_error(fid);
                      return 0;
        }
        return 1;
    }
    return 0;   
}

unsigned char CommandBuffer::getRegisterValue( int fid){
    char s[50];
    int n,l;
    char cx;

        if(startsWith("mem ")){
            shift(strlen("mem "));
            l=startsWithHexDec(&n);
            if(l>0){
                shift(l);
//              x=(unsigned int)(*n);
                initBuffer();
                return 1;
            }
            else{
                put_error(fid);
                initBuffer();
                return 0;
            }
        }
        if(startsWith("port ")){
            shift(strlen("port "));
            skipSpace();
            if(startsWith("PE ")){
                shift(strlen("PE "));
                l=startsWithHexDec(&n);
                shift(l);
//              cx=(*getPEX[n])();
                cx=getPEX_i(n);
                hex2str((unsigned char )cx,(unsigned char *)s);
                sendCommand(fid,"\n\r");
                sendCommand(fid,s);
                sendCommand(fid,"\n\r");
                initBuffer();
                return 1;
            }
            if(startsWith("PB ")){
                shift(strlen("PB "));
                l=startsWithHexDec(&n);
                shift(l);
                if(n<0||5<n){
                    put_error(fid);
                    initBuffer();
                    return 0;
                }
                shift(l);
//              if(getPBX[n]==0){
                  if(getPBX_i(n)==0){
                    put_error(fid);
                    initBuffer();
                    return 0;
                }
//              cx=(*getPBX[n])();
                cx=getPBX_i(n);
                hex2str((unsigned char)cx,(unsigned char *)s);
                sendCommand(fid,"\n\r");
                sendCommand(fid,s);
                sendCommand(fid,"\n\r");
                initBuffer();
                return 1;
            }
            else
            if(startsWith("PME ")){
                shift(strlen("PME "));
                l=startsWithHexDec(&n);
                shift(l);
//              cx=(*getPMEX[n])();
                cx=getPMEX_i(n);
                hex2str((unsigned char )cx,(unsigned char *)s);
                sendCommand(fid,"\n\r");
                sendCommand(fid,s);
                sendCommand(fid,"\n\r");
                initBuffer();
                return 1;
            }
            else
            if(startsWith("PMB ")){
                shift(strlen("PMB "));
                l=startsWithHexDec(&n);
                shift(l);
                if(n<0||5<n){
                    put_error(fid);
                    initBuffer();
                    return 0;
                }
                shift(l);
//              if(getPBX[n]==0){
                if(getPBX_i(n)==0){
                    put_error(fid);
                    initBuffer();
                    return 0;
                }
//              cx=(*getPMBX[n])();
                cx=getPMBX_i(n);
                hex2str((unsigned char)cx,(unsigned char *)s);
                sendCommand(fid,"\n\r");
                sendCommand(fid,s);
                sendCommand(fid,"\n\r");
                initBuffer();
                return 1;
            }
            else{
                put_error(fid);
                initBuffer();
                return 0;
            }
        }
        return 0;
}

void CommandBuffer::sendCommandToConnectedFace( int faceID, char *s){
    OutBuffer *x;
    InBuffer *y;
    int dir;
    if((0x80&faceID)!=0x00){
        xputs(s);
        return;
    }
    x=faceOut[faceID];
    y=faceIn[faceID];
    if(!(*y).getConnected()){
        printf(" ..send .. face %d is not connected.\n\r",faceID);
        return;
    }
    dir=dirID[faceID];  
    printf("..send..f%d d%d... %s\n\r",faceID,dir,s);   
    (*x).setAllOrOne((unsigned char)0);
  (*x).setLedNumber((unsigned char )dir);
  sendCommand(faceID,s);
}

void CommandBuffer::sendCommand( int faceID, char *s){
    OutBuffer *x;
//  InBuffer *y;
//  int dir;
    if((0x80&faceID)!=0x00){
        xputs(s);
        return;
    }
    x=faceOut[faceID];
    setCurrentOutFace(x);
//  y=faceIn[faceID];
//  printf("..send..%d ... %s\n\r",faceID,s);    // for debug
    (*x).setPortXOff();     
    (*x).putStringX(s);
     (*x).waitUntilOutputDone();    
    (*x).setPortXOn();
}

void CommandBuffer::startRecursiveStructureSearch( int ufid, char *uName){ // srs
    char rbuffer2[5];
//    char cmd2next[50];
        char i;
        char ufc='0';
//      char udc='0';
      UpFaceID=ufid;
      printf("srs ...uf=%d, uname=%s\n\r",UpFaceID,uName);
        if((0x80&UpFaceID)!=0x00){
           ufc='*';
//         udc='*';
        }
        else
        {
           ufc=UpFaceID+'0';
//         udc=myDirID[UpFaceID]+'0';
        }
        if(!FirstTime) return;
        myname[0]=0;
        strcpy(myname,uName);
        upname[0]=0;
        strcpy(upname,uName);
        rbuffer2[0]=ufc;
        rbuffer2[1]=0;
        strcat(myname,rbuffer2);
        FirstTime=0;
        resetFlag=1;
        for(i=0;i<FaceMax;i++){   // 各面について解析
            if(i!=UpFaceID){      // この面が上位ノードでなければ以下を実行
                sendCq(i);
            }
        }
}
/*
void wait(int i){
    int j,k;
    for(j=0;j<i;j++){
        for(k=0;k<10000;k++){}
    }
}
*/
void CommandBuffer::resetBuffer(void){ // reset
    int i;
    FirstTime=1;
    for(i=0;i<FaceMax;i++){
        faceID[i]=(char)(-1);   
        (*(faceIn[i])).setConnected((unsigned char)0);
    }
    initBuffer();
    thisIsMonitoring=0;
    thisIsProbe=0;
    resetFlag=1;
    echoBackUART=1;
}

void CommandBuffer::sendCq( char fID){
    OutBuffer *x;
    InBuffer *y;
    int i;
    char str[20];
    char buf[20];
    char cmd[40];
//  int f;
    str[0]=0;
    // faceID<0
    if( (((char)0x80) & fID)!=0x00  ) return ;
  x=faceOut[fID];
    y=faceIn[fID];
    if((*y).getConnected()) return;
    setCurrentOutFace(x);   
    strcpy(str,"cq face ");
    buf[0]='0'+fID;
    buf[1]=' ';
    buf[2]='d'; buf[3]='i'; buf[4]='r'; buf[5]=' ';buf[6]='x'; buf[7]='.';buf[8]=0;
//  (*x).putString(x,str);
    (*x).setAllOrOne((unsigned char)0);
    for(i=0;i<DirMax;i++){
        strcpy(cmd,str);
        buf[6]='0'+i;
        strcat(cmd,buf);
        (*x).setLedNo((unsigned char)i);
        sendCommand(fID,cmd);
 //   wait(0.1f);       
//   (*x).waitUntilOutputDone();
//   if((*y).getConnected()) return 1;
    }
    (*x).setPortXOn();
}


unsigned char CommandBuffer::faceIsConnected( char fID){
    InBuffer *y;
    // faceID<0
    if( (((char)0x80) & fID)!=0x00  ) return 1;
    y=faceIn[fID];
    return (*y).getConnected();
}

int CommandBuffer::putQueue( int fid, char *x, int l){
//    printf(" putQueueForComBuffer-%s received.\n",x);
    StringQueue *q;
    q=squeue;
    return (*q).putSQueue(fid,x, l);
}


//InBuffer nextDoorIn[1], faceIn[6];
//OutBuffer nextDoorOut[1], faceOut[6];
StringQueue squeuex;

void CommandBuffer::setCurrentOutFace( OutBuffer *x){

   (*(faceOut[0])).clear();
   (*(faceOut[1])).clear();
   (*(faceOut[2])).clear();
   (*(faceOut[3])).clear();
   (*(faceOut[4])).clear();
   (*(faceOut[5])).clear();

   currentOutFace=x;
}


//void intsrv_tim(void)
//#pragma interrupt ISRProcess
void CommandBuffer::interruptProcess(void)
{

    OutBuffer *outFace;
    outFace=currentOutFace;

//  sputs(".");
//  printf(".");

//    ((nextDoorIn[0]).intrProcess)(&nextDoorIn[0]);
    (*faceIn[0]).intrProcess();
    (*faceIn[1]).intrProcess();
    (*faceIn[2]).intrProcess();
    (*faceIn[3]).intrProcess();
    (*faceIn[4]).intrProcess();
    (*faceIn[5]).intrProcess();

 //   if(command.currentOutFace!=NULL){
     if(outFace!=NULL){
       (*outFace).intrProcess();
    }

    // debug
//  a=getPBX[1]();
//  setPBX[1](!a);

    // タイマー割り込みフラグをクリア
//  MK0H.6 = 0;     // TM000割り込み許可
//  IF0H.6 = 0;     // タイマー割り込みフラグをクリア

}
/*
void CommandBuffer::setFaceIn( unsigned char i, InBuffer *fin){
    faceIn[i]=fin;
}
*/


void CommandBuffer::ackReturned( int faceID){
    xputs("ack returned\n\r");
    OutBuffer *out=faceOut[faceID];
        (*out).setStringOutputStatus(2);
}

char i2h1(int x){
    if(x>16) return (char)(-1);
    if(x<10) return '0'+x;
    else return 'a'+x-10;
}
int h2i1(char x){
    int rtn=(int)x-(int)'0';
    if(x>=(int)'a'){
        rtn=(int)x-(int)'a'+10;
    }
    return rtn;
}
CommandBuffer::CommandBuffer( unsigned char idx){
    char i;
    p=0;
    cqp1=0;
    id=idx;
    setPortFunctions();
    FirstTime=1;

    for(i=0;i<FaceMax;i++){
        faceID[i]=(char)(-1);
    }
    // H8SX specific initialization
//    PH.ICR.BIT.B0=1; PH.ICR.B1=1; PH.ICR.B2=1; PH.ICR.B3=1; //H->B
//    PH.ICR.BIT.B4=1; PH.ICR.B5=1; PH.ICR.B6=1; PH.ICR.B7=1;
    setPMEF((unsigned char)0); setPMEE((unsigned char)0);
    setPMED((unsigned char)0); setPMEC((unsigned char)0);
    setPMEB((unsigned char)0); setPMEA((unsigned char)0);
}

void CommandBuffer::setBuffers(void){
    squeue=new StringQueue();
//  (*squeue).initSQueue();

    /* */
    faceIn[0]=new InBuffer(0,this);
       (*(faceIn[0])).setPortConf(getPEF,setPMEF);
//     (*this).setFaceIn( 0, faceIn[0]);
    faceIn[1]=new InBuffer(1,this);
       (*(faceIn[1])).setPortConf(getPEE,setPMEE);
//     (*this).setFaceIn( 1, (faceIn[1]));
    faceIn[2]=new InBuffer(2,this);
       (*(faceIn[2])).setPortConf(getPED,setPMED);
//     (*this).setFaceIn(2, (faceIn[2]));
    faceIn[3]=new InBuffer(3,this);
       (*(faceIn[3])).setPortConf(getPEC,setPMEC);
//     (*this).setFaceIn(3, (faceIn[3]));
    faceIn[4]=new InBuffer(4,this);
       (*(faceIn[4])).setPortConf(getPEB,setPMEB);
//     (*this).setFaceIn(4, (faceIn[4]));
    faceIn[5]=new InBuffer(5,this);
       (*(faceIn[5])).setPortConf(getPEA,setPMEA);
//     (*this).setFaceIn(5, (faceIn[5]));

    faceOut[0]=new OutBuffer((unsigned char)0);
       (*(faceOut[0])).setPortConf( (unsigned char)0, setPE0, getPE0, setPME0);
       (*(faceOut[0])).setPortConf( (unsigned char)1, setPE1, getPE1, setPME1);
       (*(faceOut[0])).setPortConf( (unsigned char)2, setPE2, getPE2, setPME2);
       (*(faceOut[0])).setPortConf( (unsigned char)3, setPE3, getPE3, setPME3);
       (*(faceOut[0])).setPortXConf(   setPE4, setPME4);
    faceOut[1]=new OutBuffer((unsigned char)1);
       (*(faceOut[1])).setPortConf( (unsigned char)0, setPE0, getPE0, setPME0);
       (*(faceOut[1])).setPortConf( (unsigned char)1, setPE1, getPE1, setPME1);
       (*(faceOut[1])).setPortConf( (unsigned char)2, setPE2, getPE2, setPME2);
       (*(faceOut[1])).setPortConf( (unsigned char)3, setPE3, getPE3, setPME3);
       (*(faceOut[1])).setPortXConf(   setPE5, setPME5);
    faceOut[2]=new OutBuffer((unsigned char)2);
       (*(faceOut[2])).setPortConf( (unsigned char)0, setPE0, getPE0, setPME0);
       (*(faceOut[2])).setPortConf( (unsigned char)1, setPE1, getPE1, setPME1);
       (*(faceOut[2])).setPortConf( (unsigned char)2, setPE2, getPE2, setPME2);
       (*(faceOut[2])).setPortConf( (unsigned char)3, setPE3, getPE3, setPME3);
       (*(faceOut[2])).setPortXConf(   setPE6, setPME6);
    faceOut[3]=new OutBuffer((unsigned char)3);
       (*(faceOut[3])).setPortConf( (unsigned char)0, setPE0, getPE0, setPME0);
       (*(faceOut[3])).setPortConf( (unsigned char)1, setPE1, getPE1, setPME1);
       (*(faceOut[3])).setPortConf( (unsigned char)2, setPE2, getPE2, setPME2);
       (*(faceOut[3])).setPortConf( (unsigned char)3, setPE3, getPE3, setPME3);
       (*(faceOut[3])).setPortXConf(   setPE7, setPME7);
    faceOut[4]=new OutBuffer((unsigned char)4);
       (*(faceOut[4])).setPortConf( (unsigned char)0, setPE0, getPE0, setPME0);
       (*(faceOut[4])).setPortConf( (unsigned char)1, setPE1, getPE1, setPME1);
       (*(faceOut[4])).setPortConf( (unsigned char)2, setPE2, getPE2, setPME2);
       (*(faceOut[4])).setPortConf( (unsigned char)3, setPE3, getPE3, setPME3);
       (*(faceOut[4])).setPortXConf(   setPE8, setPME8);
    faceOut[5]=new OutBuffer((unsigned char)5);
       (*(faceOut[5])).setPortConf( (unsigned char)0, setPE0, getPE0, setPME0);
       (*(faceOut[5])).setPortConf( (unsigned char)1, setPE1, getPE1, setPME1);
       (*(faceOut[5])).setPortConf( (unsigned char)2, setPE2, getPE2, setPME2);
       (*(faceOut[5])).setPortConf( (unsigned char)3, setPE3, getPE3, setPME3);
       (*(faceOut[5])).setPortXConf(   setPE9, setPME9);

    currentOutFace= (faceOut[0]);

//    (*setPMBX[1])((unsigned char)1);
}

StringQueue* CommandBuffer::getStringQueue(){
    return squeue;
}

}}
----
#counter

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS