示例#1
0
void
debug_process_uart (void)
{
#if defined(ECMD_PARSER_SUPPORT) && !defined(SOFT_UART_SUPPORT)
#define LEN 60
#define OUTPUTLEN 40

    static char buf[LEN+1];
    static char *ptr = buf;

    if (usart(UCSR,A) & _BV(usart(RXC))) {
        char data = usart(UDR);

        if (data == '\n' || data == '\r') {
            char *output = malloc(OUTPUTLEN);

            if (output == NULL)
                debug_printf("malloc() failed!\n");

            *ptr = '\0';
            printf_P(PSTR("\n"));

#ifdef DEBUG_ECMD
            debug_printf("parsing command '%s'\n", buf);
#endif
            int l;

            do {
                l = ecmd_parse_command(buf, output, LEN);
                if (is_ECMD_FINAL(l) || is_ECMD_AGAIN(l)) {
                    output[is_ECMD_AGAIN(l) ? ECMD_AGAIN(l) : l] = 0;
                    printf_P(PSTR("%s\n"), output);
                }
            } while (is_ECMD_AGAIN(l));
            free(output);
            ptr = buf;
        } else {
            debug_uart_put(data, stdout);
            if (data == '\b') {if (ptr > &buf[0]) ptr--;}
            else
            {
                if (ptr < &buf[LEN-1])
                    *ptr++ = data;
                else
                    debug_printf("not enough space for storing '%c'\n", data);
            }
        }
    }
#endif  /* ECMD_PARSER_SUPPORT && !SOFT_UART_SUPPORT*/
}
示例#2
0
void ecmd_lufa_periodic(void) {

  if (!must_parse)
    ecmd_lufa_rx();
  
  if (must_parse && write_len == 0) {

    must_parse = 0;

    if (recv_len <= 1) {
      recv_len = 0;
      return;
    }

    write_len = ecmd_parse_command(recv_buffer, write_buffer, sizeof(write_buffer));
    if (is_ECMD_AGAIN(write_len)) {
      /* convert ECMD_AGAIN back to ECMD_FINAL */
      write_len = ECMD_AGAIN(write_len);
      must_parse = 1;

    } else if (is_ECMD_ERR(write_len))
      return;
    else {
      recv_len = 0;
    }

    write_buffer[write_len++] = '\r';
    write_buffer[write_len++] = '\n';

  }

  if (write_len)
    ecmd_lufa_tx();

}
示例#3
0
uint8_t pdu_parser(uint8_t *pdu) 
{
    uint8_t i = 3;
    uint8_t ret_val = 1;
    int16_t write_len = 0;
    uint16_t reply_len = 0;

    SMS_DEBUG("pdu_parser called: %d\n", pdu[i]);
    if (pdu[i++] == '+') {
        SMS_DEBUG("pdu_parser called: %d\n", pdu[i]);
        if (!strncmp((char *) pdu + i, "CMGL", 4)) {    
            SMS_DEBUG("messages will be received\r\n");
            uint8_t smsc_len, sender_len;
            if (reply_len == 0) {
                while (pdu[++i] != '\n');
                i ++;
                /* get length of smsc number */
                sscanf((char *) pdu + i, "%02hhX", &smsc_len);
                /* we ignore some flags "0000" */
                i += (smsc_len * 2) + 4;
                /* get data length */
                sscanf((char *) pdu + i, "%02hhX", &sender_len);
                /* get number of message sender */
                strncpy(nummer, (char *) pdu + i, sender_len + 5);
                nummer[sender_len + 5] = '\0';
                i = i + sender_len + 5 + 18;
                read_sms((uint8_t *) pdu + i);
            }
            
            SMS_DEBUG("ecmd: %s\n", text);
            do {
                if (reply_len < (int16_t)sizeof(write_buffer)) {
                    write_len = ecmd_parse_command((char *) text, 
                                              write_buffer + reply_len, 
                                              sizeof(write_buffer) - reply_len - 1);
                    SMS_DEBUG("ret_len ecmd parser: %d\r\n", write_len);
                    if (is_ECMD_AGAIN(write_len)) {
                        reply_len += -(10 + write_len);
                        *(write_buffer + reply_len) = ' ';
                        reply_len++;
                    } 
                    else if (write_len > 0) {
                        SMS_DEBUG("finished: \"%s\"\n", write_buffer);
                        ret_val = 0;
                        reply_len += write_len;
                        break;
                    }
                    SMS_DEBUG("%s\n", write_buffer);
                }
                else {
                    break;
                }
            } while (write_len <= 0);
           
            write_buffer[reply_len] = '\0';
            sms_send((uint8_t *) nummer, (uint8_t *) write_buffer, NULL, 1);
        }
    }
    return ret_val;
}
示例#4
0
文件: jabber.c 项目: AnDann/ethersex
static void
jabber_parse_ecmd(char *message)
{
  int16_t remain = sizeof(STATE->outbuf) - 1;
  int16_t written = 0;

  while (remain > 0)
  {
    int16_t len = ecmd_parse_command(message, STATE->outbuf + written, remain);
    if (is_ECMD_AGAIN(len))
    {
      len = ECMD_AGAIN(len);
      written += len;
      remain -= len;
      if (remain)
      {
        STATE->outbuf[written++] = '\n';
        remain--;
      }
      continue;
    }
    else if (is_ECMD_ERR(len))
    {
      strncpy_P(STATE->outbuf, PSTR("parse error"), sizeof(STATE->outbuf));
      len = 11;
    }
    written = len;
    break;
  }

  STATE->outbuf[written] = 0;
}
示例#5
0
void
cron_execute(struct cron_event_linkedlist *exec)
{
  if (exec->event.cmd == CRON_JUMP)
  {
#ifdef DEBUG_CRON
    debug_printf("cron: match (JUMP %p)\n", &(exec->event.handler));
#endif
#ifndef DEBUG_CRON_DRYRUN
    exec->event.handler(&(exec->event.extradata));
#endif
  }
  else if (exec->event.cmd == CRON_ECMD)
  {
    // ECMD PARSER
#ifdef DEBUG_CRON
    debug_printf("cron: match (%s)\n", (char *) &(exec->event.ecmddata));
#endif
#ifndef DEBUG_CRON_DRYRUN
    char output[ECMD_INPUTBUF_LENGTH];
#ifdef DEBUG_CRON
    int16_t l =
#endif
      ecmd_parse_command((char *) &(exec->event.ecmddata), output,
                         sizeof(output) - 1);
#ifdef DEBUG_CRON
    if (is_ECMD_AGAIN(l))
      l = ECMD_AGAIN(l);
    if (is_ECMD_FINAL(l))
    {
      output[l] = 0;
      debug_printf("cron output %s\n", output);
    }
    else
    {
      debug_printf("cron output error %d\n", l);
    }
#endif
#endif
  }

  /* Execute job endless if repeat value is equal to zero otherwise
   * decrement the value and check if is equal to zero.
   * If that is the case, it is time to kick out this cronjob. */
  if (exec->event.repeat > 0 && !(--exec->event.repeat))
    cron_jobrm(exec);
}
示例#6
0
static void
irc_handle_ecmd (void)
{
    int16_t len = ecmd_parse_command(STATE->inbuf, STATE->outbuf,
				     ECMD_OUTPUTBUF_LENGTH - 1);

    if ((STATE->reparse = is_ECMD_AGAIN(len)) != 0) {
	/* convert ECMD_AGAIN back to ECMD_FINAL */
	len = ECMD_AGAIN(len);
    }

    if (is_ECMD_ERR(len))
	strcpy_P(STATE->outbuf, PSTR("parse error"));
    else
	STATE->outbuf[len] = 0;

    return;
}
示例#7
0
void newdata(void)
{
    struct ecmd_connection_state_t *state = &uip_conn->appstate.ecmd;


    uint16_t diff = ECMD_INPUTBUF_LENGTH - state->in_len;
    if (diff > 0) {
        int cplen;

        if (uip_datalen() <= diff)
            cplen = uip_datalen();
        else
            cplen = diff;

        memcpy(state->inbuf + state->in_len, uip_appdata, cplen);
        state->in_len += cplen;

#ifdef DEBUG_ECMD_NET
        debug_printf("copied %d bytes\n", cplen);
#endif
    } else {
#ifdef DEBUG_ECMD_NET
        debug_printf("buffer full\n");
#endif
    }

    char *lf = memchr(state->inbuf, '\n', state->in_len);

    if (lf != NULL ||
        memchr(uip_appdata, '\n', uip_datalen()) != NULL) {

#ifdef DEBUG_ECMD_NET
        debug_printf("calling parser\n");
#endif

        if (lf)
            *lf = '\0';
        else
            state->inbuf[ECMD_INPUTBUF_LENGTH-1] = '\0';

        /* kill \r */
        int l;
        for (l = 0; l < ECMD_INPUTBUF_LENGTH; l++)
          if (state->inbuf[l] == '\r')
            state->inbuf[l] = '\0';

        /* if the first character is ! close the connection after the last
         * byte is sent
         */
        uint8_t skip = 0;
        if (state->inbuf[0] == '!') {
          skip = 1;
          state->close_requested = 1;
        }
      
#ifdef ECMD_PAM_SUPPORT
        if (state->pam_state == PAM_UNKOWN) {
          if (strncmp_P(state->inbuf + skip, PSTR("auth "), 5) != 0) { 
            /* No authentification request */
auth_required:
            state->out_len = sprintf_P(state->outbuf, 
                                       PSTR("authentification required\n"));
            memset(state->inbuf, 0, ECMD_INPUTBUF_LENGTH);
            state->in_len = 0;
            return;          
          } else {
            char *user = state->inbuf + skip + 5; /* "auth " */
            char *pass = strchr(user + 1,' ');
            if (! pass) goto auth_required;
            *pass = 0;
            do { pass++; } while (*pass == ' ');
            char *p = strchr(pass, ' ');
            if (p) 
              *p = 0;
            /* Do the Pam request, the pam request will cache username and
             * passwort if its necessary. */
            pam_auth(user, pass, &state->pam_state);

            if (p && p[1] != 0) { /* There ist something after the PAM request */
              memmove(state->inbuf, p+1, strlen(p+1) + 1);
              skip = 0;
              state->in_len = strlen(p+1);
              if (state->pam_state == PAM_PENDING)
                state->parse_again = 1;
            } else {
              state->in_len = 0;
              return;
            }
          }
        }
        if (state->pam_state == PAM_PENDING || state->pam_state == PAM_DENIED)
          return; /* Pam Subsystem promisses to change this state */
#endif


        /* parse command and write output to state->outbuf, reserving at least
         * one byte for the terminating \n */
        l = ecmd_parse_command(state->inbuf + skip,
                                    state->outbuf,
                                    ECMD_OUTPUTBUF_LENGTH-1);

#ifdef DEBUG_ECMD_NET
        debug_printf("parser returned %d\n", l);
#endif

        /* check if the parse has to be called again */
        if (is_ECMD_AGAIN(l)) {
#ifdef DEBUG_ECMD_NET
            debug_printf("parser needs to be called again\n");
#endif

            state->parse_again = 1;
            l = ECMD_AGAIN(l);
        }

#ifdef DEBUG_ECMD_NET
        debug_printf("parser really returned %d\n", l);
#endif

        if (l > 0) {
            if (state->outbuf[l] != ECMD_NO_NEWLINE) state->outbuf[l++] = '\n';
            state->out_len = l;
        }

        if (!state->parse_again) {
#ifdef DEBUG_ECMD_NET
            debug_printf("clearing buffer\n");
#endif
            memset(state->inbuf, 0, ECMD_INPUTBUF_LENGTH);
            state->in_len = 0;
        }
    }
}
示例#8
0
void ecmd_net_main(void)
{
    struct ecmd_connection_state_t *state = &uip_conn->appstate.ecmd;

    if (!uip_poll()) {
#ifdef DEBUG_ECMD_NET
        debug_printf("ecmd_net_main()\n");
#endif
    }

    if(uip_connected()) {
#ifdef DEBUG_ECMD_NET
        debug_printf("new connection\n");
#endif
        state->in_len = 0;
        state->out_len = 0;
        state->parse_again = 0;
        state->parse_again = 0;
        state->close_requested = 0;
#ifdef ECMD_PAM_SUPPORT
        state->pam_state = PAM_UNKOWN;
#endif
        memset(state->inbuf, 0, ECMD_INPUTBUF_LENGTH);
    }

#ifdef ECMD_PAM_SUPPORT
        if (state->pam_state == PAM_DENIED) {
          state->out_len = sprintf_P(state->outbuf, 
                                     PSTR("authentification failed\n"));
          state->close_requested = 1;
        }
#endif


    if(uip_acked()
#ifdef ECMD_PAM_SUPPORT
        || (state->pam_state == PAM_SUCCESS && state->in_len) 
#endif
       
       ) {
        state->out_len = 0;

        if (state->parse_again) {
#ifdef DEBUG_ECMD_NET
            debug_printf("transmission done, calling parser again\n");
#endif
            /* if the first character is ! close the connection after the last
             * byte is sent
             */
            uint8_t skip = 0;
            if (state->inbuf[0] == '!') {
              skip = 1;
              state->close_requested = 1;
            }

            /* parse command and write output to state->outbuf, reserving at least
             * one byte for the terminating \n */
            int l = ecmd_parse_command(state->inbuf + skip,
                    state->outbuf,
                    ECMD_OUTPUTBUF_LENGTH-1);

            /* check if the parse has to be called again */
            if (is_ECMD_AGAIN(l)) {
                state->parse_again = 1;
                l = ECMD_AGAIN(l);
            } else {
                state->parse_again = 0;
                /* We have to clear the input buffer */
                state->in_len = 0;
            }

            if (l > 0) {
                state->outbuf[l++] = '\n';
                state->out_len = l;
            }
        }
    }

    if(uip_newdata()) {
        newdata();
    }

    if(uip_rexmit() ||
            uip_newdata() ||
            uip_acked() ||
            uip_connected() ||
            uip_poll()) {
        if (state->out_len > 0) {
#ifdef DEBUG_ECMD_NET
            debug_printf("sending %d bytes\n", state->out_len);
#endif
            uip_send(state->outbuf, state->out_len);
        } else if (state->close_requested)
          uip_close();
    }
}