예제 #1
0
파일: CLS1.c 프로젝트: FlorianOechslin/FRDM
/*
** ===================================================================
**     Method      :  CLS1_ReadAndParseWithCommandTable (component Shell)
**     Description :
**         Reads characters from the default input channel and appends
**         it to the buffer. Once a new line has been detected, the
**         line will be parsed using the handlers in the table.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * cmdBuf          - Pointer to buffer provided by the
**                           caller where to store the command to read
**                           in. Characters will be appended, so make
**                           sure string buffer is initialized with a
**                           zero byte at the beginning.
**         cmdBufSize      - Size of buffer
**       * io              - Pointer to I/O channels to be used
**       * parseCallback   - Pointer to callback
**                           table provided by the user application to
**                           parse commands. The table has a NULL
**                           sentinel.
**     Returns     :
**         ---             - Error code
** ===================================================================
*/
uint8_t CLS1_ReadAndParseWithCommandTable(uint8_t *cmdBuf, size_t cmdBufSize, CLS1_ConstStdIOType *io, CLS1_ConstParseCommandCallback *parseCallback)
{
  uint8_t res = ERR_OK;
  size_t len;

  /* IMPORTANT NOTE: this function *appends* to the buffer, so the buffer needs to be initialized first! */
  len = UTIL1_strlen((const char*)cmdBuf);
  if (CLS1_ReadLine(cmdBuf, cmdBuf+len, cmdBufSize-len, io)) {
    len = UTIL1_strlen((const char*)cmdBuf); /* length of buffer string */
    if (len==0) { /* error case */
      return ERR_FAILED;
    } else if (len==1 && (cmdBuf[0]=='\n' || cmdBuf[0]=='\r')) { /* eat preceding newline characters */
      cmdBuf[0] = '\0';
    }
    if (len>=cmdBufSize-1) {           /* buffer overflow? Parse what we have, will be likely return an error */
      (void)CLS1_ParseWithCommandTable(cmdBuf, io, parseCallback);
      cmdBuf[0] = '\0'; /* start again */
      res = ERR_OVERFLOW;
    } else if (cmdBuf[len-1]=='\n' || cmdBuf[len-1]=='\r') { /* line end: parse command */
      cmdBuf[len-1] = '\0';            /* remove line end character for parser */
      res = CLS1_ParseWithCommandTable(cmdBuf, io, parseCallback);
      cmdBuf[0] = '\0';                /* start again */
    } else {
      /* continue to append to buffer */
    }
  }
  return res;
}
예제 #2
0
void I2C_StoreCmd(void) {
#if PL_HAS_UI
    unsigned char buf[32];
    uint8_t strSize;

#if PL_HAS_RUNNER
    buf[0] = '@';
    buf[1] = '\0';
    RUNNER_GetCmdString(buf, sizeof(buf));
#elif PL_HAS_SLIDER
    SLIDER_GetCmdString(buf, sizeof(buf));
#else
    buf[0] = '\0';
#endif
    if (buf[0]!='\0') {
      uint8_t cnt=3;

      while(cnt>0 && memDevice.u.data.cmdLength!=0) { /* poll cmdLength: this will be set to zero by the master if it is ok to place the string */
        FRTOS1_vTaskDelay(50/portTICK_RATE_MS); /* give master some time to clear the flasg */
        cnt--;
      }
      if (cnt==0) { /* timeout. Will loose that command. Not ideal, but simple :-) */
        return; /* get out here */
      }
      strSize = (uint8_t)(UTIL1_strlen(buf)+1); /* size of string including zero byte */
      if (strSize>sizeof(memDevice.u.data.cmd)) {
        strSize = sizeof(memDevice.u.data.cmd);
      }
      EnterCritical();
      memDevice.u.data.cmdLength = strSize;
      UTIL1_strcpy(memDevice.u.data.cmd, sizeof(memDevice.u.data.cmd), buf);
      ExitCritical();
    }
#endif /* PL_HAS_UI */
}
예제 #3
0
/*!
 \brief A simple state machine iterating through different transceiver states
 */
static void RADIO_HandleState(void) {
  tTxPacket ackTxPacket;  /* SMAC structure for TX packets   */
  static uint8_t ackTxDataBuffer[SMAC1_RADIO_BUF_SIZE];    /*Data buffer to hold TX data */

  switch (RADIO_AppStatus) {
    case RADIO_INITIAL_STATE:
        RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON;
        break;

    case RADIO_RECEIVER_ALWAYS_ON:
        RADIO_AppStatus = RADIO_READY_FOR_TX_RX_DATA;
        (void)SMAC1_MLMERXEnableRequest(0); /* Zero means wait forever with RX ON. */
        break;

    case RADIO_READY_FOR_TX_RX_DATA: /* we are ready to receive/send data data */
        break;

    case RADIO_TRANSMIT_DATA:
        if (SMAC1_MLMERXDisableRequest() != SMAC1_SUCCESS) { /* Turn off the RX forever mode. */
          RADIO_AppStatus = RADIO_TRANSMIT_DATA; /* retry */
          break;
        }
        LED1_Neg();
        if ((SMAC1_MCPSDataRequest(&RADIO_TxPacket) == SMAC1_SUCCESS)) { /* transmit data */
          RADIO_AppStatus = RADIO_WAITING_FOR_ACK;
          (void)SMAC1_MLMERXEnableRequest(RADIO_TIMEOUT_COUNT);
        } else {
          RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON; /* what should we otherwise do? */
        }
        break;

    case RADIO_TRANSMIT_ACK:
        /*Initialize the packet.*/
        /*! \todo RADIO: Below we send back the acknowledge message:
         *        check that your RADIO_ACK_STR is what you want and need.
         *        Notice the implicit string concatenation for efficiency and reduced code size. */
        UTIL1_strcpy(ackTxDataBuffer, sizeof(ackTxDataBuffer), (unsigned char*)RADIO_PREFIX_STR RADIO_ACK_STR);
        ackTxPacket.pu8Data = &ackTxDataBuffer[0]; /* Load the address of our txbuffer into the tx structure*/
        ackTxPacket.u8DataLength = (byte)(UTIL1_strlen((char*)ackTxDataBuffer)+1); /* set the size of the packet */
        (void)SMAC1_MCPSDataRequest(&ackTxPacket); /* transmit data */
        RADIO_AppStatus = RADIO_RECEIVER_ALWAYS_ON;
        break;

    case RADIO_RESET_STATE:
        /* MC13192 Reset, reinitialize and return to default state.   */
        SMAC1_RadioInit();
        RADIO_AppStatus = RADIO_INITIAL_STATE;
        break;

    case RADIO_WAITING_FOR_ACK:
        /* At this point only two things happen, 1-we receive the ack packet or 2-timeout.
         * Either way the TX will leave this state and continue. Low power mode could be placed here
         * because both 1 and 2 are interrupt driven, in this case we keep it simple
         */
        break;

    default:
        break;
  }
}
예제 #4
0
uint8_t ESP_SendATCommand(uint8_t *cmd, uint8_t *rxBuf, size_t rxBufSize, uint8_t *expectedTailStr)
{
  uint16_t snt;

  if (AS2_SendBlock(cmd, (uint16_t)UTIL1_strlen((char*)cmd), &snt) != ERR_OK) {
    return ERR_FAILED;
  }
  return RxResponse(rxBuf, rxBufSize, ESP_TIMOUT_MS, expectedTailStr);
}
예제 #5
0
uint8_t RNETA_ParseCommand(const unsigned char *cmd, bool *handled, const CLS1_StdIOType *io) {
    uint8_t res = ERR_OK;
    const uint8_t *p;
    uint16_t val16;

    if (UTIL1_strcmp((char*)cmd, (char*)CLS1_CMD_HELP)==0 || UTIL1_strcmp((char*)cmd, (char*)"app help")==0) {
        PrintHelp(io);
        *handled = TRUE;
    } else if (UTIL1_strcmp((char*)cmd, (char*)CLS1_CMD_STATUS)==0 || UTIL1_strcmp((char*)cmd, (char*)"app status")==0) {
        *handled = TRUE;
        return PrintStatus(io);
    } else if (UTIL1_strncmp((char*)cmd, (char*)"app saddr", sizeof("app saddr")-1)==0) {
        p = cmd + sizeof("app saddr")-1;
        *handled = TRUE;
        if (UTIL1_ScanHex16uNumber(&p, &val16)==ERR_OK) {
            (void)RNWK_SetThisNodeAddr((RNWK_ShortAddrType)val16);
        } else {
            CLS1_SendStr((unsigned char*)"ERR: wrong address\r\n", io->stdErr);
            return ERR_FAILED;
        }
    } else if (UTIL1_strncmp((char*)cmd, (char*)"app daddr", sizeof("app daddr")-1)==0) {
        p = cmd + sizeof("app daddr")-1;
        *handled = TRUE;
        if (UTIL1_ScanHex16uNumber(&p, &val16)==ERR_OK) {
            APP_dstAddr = val16;
        } else {
            CLS1_SendStr((unsigned char*)"ERR: wrong address\r\n", io->stdErr);
            return ERR_FAILED;
        }
#if PL_HAS_RSTDIO
    } else if (UTIL1_strncmp((char*)cmd, (char*)"app send", sizeof("app send")-1)==0) {
        unsigned char buf[32];
        RSTDIO_QueueType queue;

        if (UTIL1_strncmp((char*)cmd, (char*)"app send in", sizeof("app send in")-1)==0) {
            queue = RSTDIO_QUEUE_TX_IN;
            cmd += sizeof("app send in");
        } else if (UTIL1_strncmp((char*)cmd, (char*)"app send out", sizeof("app send out")-1)==0) {
            queue = RSTDIO_QUEUE_TX_OUT;
            cmd += sizeof("app send out");
        } else if (UTIL1_strncmp((char*)cmd, (char*)"app send err", sizeof("app send err")-1)==0) {
            queue = RSTDIO_QUEUE_TX_ERR;
            cmd += sizeof("app send err");
        } else {
            return ERR_OK; /* not handled */
        }
        UTIL1_strcpy(buf, sizeof(buf), cmd);
        UTIL1_chcat(buf, sizeof(buf), '\n');
        buf[sizeof(buf)-2] = '\n'; /* have a '\n' in any case */
        if (RSTDIO_SendToTxStdio(queue, buf, UTIL1_strlen((char*)buf))!=ERR_OK) {
            CLS1_SendStr((unsigned char*)"failed!\r\n", io->stdErr);
        }
        *handled = TRUE;
#endif
    }
    return res;
}
예제 #6
0
int strindex(char *s, char *t) {
  uint16_t i,n;

  n=UTIL1_strlen(t);
  for(i=0;*(s+i); i++) {
    if (UTIL1_strncmp(s+i,t,n) == 0)
      return i;
  }
  return -1;
}
예제 #7
0
static uint8_t Test(CLS1_ConstStdIOTypePtr io) {
  static FIL fp;
  UINT bw;
  uint8_t read_buf[16];
  uint8_t write_buf[10];
  uint8_t i;

  if (FAT1_isWriteProtected()) {
    CLS1_SendStr((unsigned char*)"disk is write protected!\r\n", io->stdErr);
    return ERR_FAILED;
  }
  /* write file */
  CLS1_SendStr((const unsigned char*)"Creating test.txt...\r\n", io->stdOut);
  if (FAT1_open(&fp, "./test.txt", FA_CREATE_ALWAYS|FA_WRITE)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed creating file!\r\n", io->stdErr);
    return ERR_FAILED;
  }
  /* write text */
  if (FAT1_write(&fp, "Hello world ", sizeof("Hello world ")-1, &bw)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr);
    (void)FAT1_close(&fp);
    return ERR_FAILED;
  }
  write_buf[0] = '\0';
  for(i=0;i<4;i++) {
    UTIL1_strcatNum8u(write_buf, sizeof(write_buf), i);
    UTIL1_chcat(write_buf, sizeof(write_buf), ' ');
  }
  if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed writing string!\r\n", io->stdErr);
    (void)FAT1_close(&fp);
    return ERR_FAILED;
  }
  /* closing file */
  (void)FAT1_close(&fp);

  /* read from file */
  CLS1_SendStr((const unsigned char*)"Read from file...\r\n", io->stdOut);
  if (FAT1_open(&fp, "./test.txt", FA_READ)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed opening file!\r\n", io->stdErr);
    return ERR_FAILED;
  }
  if (FAT1_read(&fp, &read_buf[0], sizeof(read_buf)-2, &bw)!=FR_OK) {
    CLS1_SendStr((const unsigned char*)"*** Failed reading file!\r\n", io->stdErr);
    (void)FAT1_close(&fp);
    return ERR_FAILED;
  }
  read_buf[sizeof(read_buf)-2] = '\0'; /* terminate string */
  UTIL1_strcat(read_buf, sizeof(read_buf), (unsigned char*)"\r\n");
  CLS1_SendStr(read_buf, io->stdOut);
  CLS1_SendStr((const unsigned char*)"\r\n", io->stdOut);
  /* close file */
  (void)FAT1_close(&fp);
  return ERR_OK;
}
예제 #8
0
void LogToFile(DataBuffer* myBuf,char annotation, char* fileName) {
	int16_t currentBuf = myBuf->currentReadBuffer;
	//uint8_t write_buf[48];
	UINT bw;

	/* open file */
	if (FAT1_open(&fp, fileName, FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) {
	Err(/*OPEN_FILE_ERROR*/);
	}
	/* move to the end of the file */
	if (FAT1_lseek(&fp, fp.fsize) != FR_OK || fp.fptr != fp.fsize) {
	Err(/*END_OF_FILE_ERROR*/);
	}

	/* write data */
	write_buf[0] = '\0';
	//time_t run_time;
	for(int cur_buf_index=0;cur_buf_index<BUFFER_SIZE;++cur_buf_index)
	{
		/** Time Components */
		UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.hour);
		UTIL1_chcat(write_buf, sizeof(write_buf), ':');
		UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.minute);
		UTIL1_chcat(write_buf, sizeof(write_buf), ':');
		UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.second);
		UTIL1_chcat(write_buf, sizeof(write_buf), ':');
		UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.milliBig);
		UTIL1_strcatNum8u(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].time.milliSmall);
		UTIL1_chcat(write_buf, sizeof(write_buf), ',');
;
		/** Accelerometer Components */
		UTIL1_strcatNum16s(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].x);
		UTIL1_chcat(write_buf, sizeof(write_buf), ',');
		UTIL1_strcatNum16s(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].y);
		UTIL1_chcat(write_buf, sizeof(write_buf), ',');
		UTIL1_strcatNum16s(write_buf, sizeof(write_buf), myBuf->dataBuffer[currentBuf][cur_buf_index].z);
		UTIL1_chcat(write_buf, sizeof(write_buf), ',');

		/** Annotation mode **/
		UTIL1_chcat(write_buf, sizeof(write_buf), annotation);
		UTIL1_strcat(write_buf, sizeof(write_buf), (unsigned char*)"\n");

		/** Write to file **/
		if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) {
		  (void)FAT1_close(&fp);
		  Err(/*WRITE_ERROR*/);
		  }
		memset(write_buf,'\0',48);
	}
	if(myBuf->currentReadBuffer == currentBuf)
		myBuf->currentReadBuffer=-1;
	/* closing file */
	(void)FAT1_close(&fp);
}
예제 #9
0
void RADIO_SendString(const char *data) {
  if (!RADIO_isOn) {
    return;
  }
  while (RADIO_AppStatus != RADIO_READY_FOR_TX_RX_DATA) { /* we are not ready yet! */
    RADIO_HandleState(); /* advance state machine */
  }
  UTIL1_strcpy((char*)RADIO_TxDataBuffer, sizeof(RADIO_TxDataBuffer), RADIO_PREFIX_STR);
  UTIL1_strcat((char*)RADIO_TxDataBuffer, sizeof(RADIO_TxDataBuffer), data);
  RADIO_TxPacket.pu8Data = &RADIO_TxDataBuffer[0];            /* Load the address of our txbuffer into tx structure.*/
  RADIO_TxPacket.u8DataLength = (byte)(UTIL1_strlen((char*)RADIO_TxDataBuffer)+1); /* Set the data length of the packet */
  RADIO_AppStatus = RADIO_TRANSMIT_DATA;
  RADIO_HandleState(); /* advance state machine */
}
예제 #10
0
void SQUEUE_SendString(const unsigned char *str) {
  /*! \todo Implement function */
#if PL_CONFIG_HAS_SQUEUE_SINGLE_CHAR
  while(*str!='\0') {
    if (FRTOS1_xQueueSendToBack(SQUEUE_Queue, str, 100/portTICK_RATE_MS)!=pdPASS) {
      /*for(;;){}*/ /* ups? */ /* loosing character */
    }
    str++;
  }
#else
  unsigned char *ptr;
  size_t bufSize;

  bufSize = UTIL1_strlen(str)+1;
  ptr = FRTOS1_pvPortMalloc(bufSize);
  UTIL1_strcpy(ptr, bufSize, str);
  if (FRTOS1_xQueueSendToBack(SQUEUE_Queue, &ptr, portMAX_DELAY)!=pdPASS) {
    for(;;){} /* ups? */
  }
#endif
}
예제 #11
0
static void LogToFile(int16_t x, int16_t y, int16_t z) {
  uint8_t write_buf[48];
  UINT bw;
  TIMEREC time;

  /* open file */
  if (FAT1_open(&fp, "./log.txt", FA_OPEN_ALWAYS|FA_WRITE)!=FR_OK) {
    Err();
  }
  /* move to the end of the file */
  if (FAT1_lseek(&fp, fp.fsize) != FR_OK || fp.fptr != fp.fsize) {
    Err();
  }
  /* get time */
  if (TmDt1_GetTime(&time)!=ERR_OK) {
    Err();
  }
  /* write data */
  write_buf[0] = '\0';
  UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Hour);
  UTIL1_chcat(write_buf, sizeof(write_buf), ':');
  UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Min);
  UTIL1_chcat(write_buf, sizeof(write_buf), ':');
  UTIL1_strcatNum8u(write_buf, sizeof(write_buf), time.Sec);
  UTIL1_chcat(write_buf, sizeof(write_buf), '\t');

  UTIL1_strcatNum16s(write_buf, sizeof(write_buf), x);
  UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
  UTIL1_strcatNum16s(write_buf, sizeof(write_buf), y);
  UTIL1_chcat(write_buf, sizeof(write_buf), '\t');
  UTIL1_strcatNum16s(write_buf, sizeof(write_buf), z);
  UTIL1_strcat(write_buf, sizeof(write_buf), (unsigned char*)"\r\n");
  if (FAT1_write(&fp, write_buf, UTIL1_strlen((char*)write_buf), &bw)!=FR_OK) {
    (void)FAT1_close(&fp);
    Err();
  }
  /* closing file */
  (void)FAT1_close(&fp);
}
예제 #12
0
uint8_t ESP_SendATCommand(uint8_t *cmd, uint8_t *rxBuf, size_t rxBufSize, uint8_t *expectedTailStr, uint16_t msTimeout, const CLS1_StdIOType *io) {
  uint16_t snt;
  uint8_t res;

  if (rxBuf!=NULL) {
    rxBuf[0] = '\0';
  }
  if (io!=NULL) {
    CLS1_SendStr("sending>>:\r\n", io->stdOut);
    CLS1_SendStr(cmd, io->stdOut);
  }
  if (AS2_SendBlock(cmd, (uint16_t)UTIL1_strlen((char*)cmd), &snt) != ERR_OK) {
    return ERR_FAILED;
  }
  if (rxBuf!=NULL) {
    res = RxResponse(rxBuf, rxBufSize, expectedTailStr, msTimeout);
    if (io!=NULL) {
      CLS1_SendStr("received<<:\r\n", io->stdOut);
      CLS1_SendStr(rxBuf, io->stdOut);
    }
  }
  return res;
}
예제 #13
0
파일: CLS1.c 프로젝트: FlorianOechslin/FRDM
/*
** ===================================================================
**     Method      :  CLS1_ReadLine (component Shell)
**     Description :
**         Reads a line from stdIn and returns TRUE if we have a line,
**         FALSE otherwise.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * bufStart        - Pointer to start of buffer
**       * buf             - Pointer to buffer where to read in the
**                           information
**         bufSize         - size of buffer
**       * io              - Pointer to I/O callbacks
**     Returns     :
**         ---             - TRUE if something has been read, FALSE
**                           otherwise
** ===================================================================
*/
bool CLS1_ReadLine(uint8_t *bufStart, uint8_t *buf, size_t bufSize, CLS1_ConstStdIOType *io)
{
  uint8_t c;
  bool isBackwardHistory;

  if (io->keyPressed()) {
    for(;;) {                          /* while not '\r' or '\n' */
      c = '\0';                        /* initialize character */
      io->stdIn(&c);                   /* read character */
      if (c=='\0') { /* nothing in rx buffer? Something is wrong... */
        break; /* get out of loop */
      }
      if (c=='\b' || c=='\177') {      /* check for backspace */
        if (buf > bufStart) {          /* Avoid buffer underflow */
#if CLS1_ECHO_ENABLED
           io->stdOut('\b');           /* delete character on terminal */
           io->stdOut(' ');
           io->stdOut('\b');
#endif
           buf--;                      /* delete last character in buffer */
           *buf = '\0';
           bufSize++;
        }
      } else if (CLS1_IsHistoryCharacter(c, bufStart, buf-bufStart, &isBackwardHistory)) {
#if CLS1_HISTORY_ENABLED
        uint8_t cBuf[3]={'\0','\0','\0'}, cBufIdx = 0;
        bool prevInHistory;
#endif

        while (c!='\0') {              /* empty the rx buffer (escape sequence) */
#if CLS1_HISTORY_ENABLED
           cBuf[cBufIdx] = c;
           cBufIdx++;
           if (cBufIdx==sizeof(cBuf)) {
             cBufIdx = 0; /* ring buffer */
           }
#endif
           c = '\0';                   /* initialize character */
           io->stdIn(&c);              /* read character */
        }
#if CLS1_HISTORY_ENABLED
        /* if not an alphanumeric switch to history  */
        prevInHistory = cBufIdx==0 && cBuf[0]==0x1b && cBuf[1]==0x5b && (cBuf[2]==0x41 /*up*/ || cBuf[2]==0x44 /*left*/);
        /* up:    0x27 0x5b 0x41
         * down:  0x27 0x5b 0x42
         * right: 0x27 0x5b 0x43
         * left:  0x27 0x5b 0x44
         */
        if (prevInHistory) {
          UTIL1_strcpy(bufStart, CLS1_HIST_LEN, CLS1_history[CLS1_history_index]);
          CLS1_history_index++;        /* update the index */
          if (CLS1_history_index==CLS1_NOF_HISTORY) {
            CLS1_history_index = 0;
          }
        } else {
          if (CLS1_history_index==0) {
            CLS1_history_index = (CLS1_NOF_HISTORY-1);
          } else {
            CLS1_history_index--;
          }
          UTIL1_strcpy(bufStart, CLS1_HIST_LEN, CLS1_history[CLS1_history_index]);
        }
        bufSize = bufSize + buf - bufStart - UTIL1_strlen(bufStart); /* update the buffer */
        buf = bufStart + UTIL1_strlen(bufStart);
#endif
#if CLS1_ECHO_ENABLED
        CLS1_SendStr((unsigned char*)"\r\n", io->stdOut);
        CLS1_PrintPrompt(io);
        CLS1_SendStr(bufStart, io->stdOut);
#endif
      } else {
#if CLS1_ECHO_ENABLED
        io->stdOut(c);                 /* echo character */
#endif
        *buf = (uint8_t)c;             /* append character to the string */
        buf++;
        bufSize--;
        if ((c=='\r') || (c=='\n')) {
#if CLS1_ECHO_ENABLED
          CLS1_SendStr((unsigned char*)"\n", io->stdOut);
#endif
#if CLS1_HISTORY_ENABLED
          if ((bufStart[0] != '\0') && (bufStart[0] != '\r') && (bufStart[0] != '\n')) {
            int i;

            for(i=CLS1_NOF_HISTORY-1; i>0;i--) {
              UTIL1_strcpy(CLS1_history[i], CLS1_HIST_LEN, CLS1_history[i-1]); /* move previous commands */
            }
            CLS1_history_index = 0;    /* update the history with the current command */
            UTIL1_strcpy(CLS1_history[0], CLS1_HIST_LEN, bufStart); /* add the current command to the history */
            if (buf-bufStart <= CLS1_HIST_LEN) { /* size check */
              CLS1_history[0][buf-bufStart-1] = '\0';
            } else {
              CLS1_history[0][CLS1_HIST_LEN-1] = '\0';
            }
          }
#endif
          break;
        }
        if (bufSize <= 1) {            /* buffer full */
          break;
        }
      }
    } /* for */
    *buf = '\0';                       /* zero terminate string */
    return TRUE;
  } else {
    return FALSE;
  }
}
예제 #14
0
uint8_t RNETA_ParseCommand(const unsigned char *cmd, bool *handled, const CLS1_StdIOType *io) {
  uint8_t res = ERR_OK;
  const uint8_t *p;
  uint16_t val16;
  uint8_t val8;

  if (UTIL1_strcmp((char*)cmd, (char*)CLS1_CMD_HELP)==0 || UTIL1_strcmp((char*)cmd, (char*)"rapp help")==0) {
    PrintHelp(io);
    *handled = TRUE;
  } else if (UTIL1_strcmp((char*)cmd, (char*)CLS1_CMD_STATUS)==0 || UTIL1_strcmp((char*)cmd, (char*)"rapp status")==0) {
    *handled = TRUE;
    return PrintStatus(io);
  } else if (UTIL1_strncmp((char*)cmd, (char*)"rapp saddr", sizeof("rapp saddr")-1)==0) {
    p = cmd + sizeof("rapp saddr")-1;
    *handled = TRUE;
    if (UTIL1_ScanHex16uNumber(&p, &val16)==ERR_OK) {
      (void)RNWK_SetThisNodeAddr((RNWK_ShortAddrType)val16);
    } else {
      CLS1_SendStr((unsigned char*)"ERR: wrong address\r\n", io->stdErr);
      return ERR_FAILED;
    }
  } else if (UTIL1_strncmp((char*)cmd, (char*)"rapp send val", sizeof("rapp send val")-1)==0) {
    p = cmd + sizeof("rapp send val")-1;
    *handled = TRUE;
    if (UTIL1_ScanDecimal8uNumber(&p, &val8)==ERR_OK) {
      (void)RAPP_SendPayloadDataBlock(&val8, sizeof(val8), (uint8_t)RAPP_MSG_TYPE_DATA, APP_dstAddr, RPHY_PACKET_FLAGS_NONE); /* only send low byte */
    } else {
      CLS1_SendStr((unsigned char*)"ERR: wrong number format\r\n", io->stdErr);
      return ERR_FAILED;
    }
  } else if (UTIL1_strncmp((char*)cmd, (char*)"rapp daddr", sizeof("rapp daddr")-1)==0) {
    p = cmd + sizeof("rapp daddr")-1;
    *handled = TRUE;
    if (UTIL1_ScanHex16uNumber(&p, &val16)==ERR_OK) {
      APP_dstAddr = val16;
    } else {
      CLS1_SendStr((unsigned char*)"ERR: wrong address\r\n", io->stdErr);
      return ERR_FAILED;
    }
#if RNET_CONFIG_REMOTE_STDIO
  } else if (UTIL1_strncmp((char*)cmd, (char*)"rapp send", sizeof("rapp send")-1)==0) {
    unsigned char buf[32];
    RSTDIO_QueueType queue;
    
    if (UTIL1_strncmp((char*)cmd, (char*)"rapp send in", sizeof("rapp send in")-1)==0) {
      queue = RSTDIO_QUEUE_TX_IN;
      cmd += sizeof("rapp send in");
    } else if (UTIL1_strncmp((char*)cmd, (char*)"rapp send out", sizeof("rapp send out")-1)==0) {
      queue = RSTDIO_QUEUE_TX_OUT;      
      cmd += sizeof("rapp send out");
    } else if (UTIL1_strncmp((char*)cmd, (char*)"rapp send err", sizeof("rapp send err")-1)==0) {
      queue = RSTDIO_QUEUE_TX_ERR;      
      cmd += sizeof("rapp send err");
    } else {
      return ERR_OK; /* not handled */
    }
    UTIL1_strcpy(buf, sizeof(buf), cmd);
    UTIL1_chcat(buf, sizeof(buf), '\n');
    buf[sizeof(buf)-2] = '\n'; /* have a '\n' in any case */
    if (RSTDIO_SendToTxStdio(queue, buf, UTIL1_strlen((char*)buf))!=ERR_OK) {
      CLS1_SendStr((unsigned char*)"failed!\r\n", io->stdErr);
    }
    *handled = TRUE;
#endif
  }
  return res;
}