Exemplo n.º 1
0
int drv_generic_serial_read(char *string, const int len)
{
    int count, run, ret;

    count = len < 0 ? -len : len;

    for (run = 0; run < 10; run++) {
	ret = drv_generic_serial_poll(string, count);
	if (ret >= 0 || errno != EAGAIN)
	    break;
	info("%s: read(%s): EAGAIN", Driver, Port);
	usleep(1000);
    }

    if (ret > 0 && ret != count && len > 0) {
	error("%s: partial read(%s): len=%d ret=%d", Driver, Port, len, ret);
    }

    return ret;
}
Exemplo n.º 2
0
static int drv_CW_poll(void)
{
    while (1) {
	char buffer[32];
	int num, n;

	num = drv_generic_serial_poll(buffer, sizeof(buffer));
	if (num <= 0)
	    break;		/* no more input */

	/* put result into RingBuffer */
	for (n = 0; n < num; n++) {
	    RingBuffer[RingWPos++] = (unsigned char) buffer[n];
	    if (RingWPos >= sizeof(RingBuffer))
		RingWPos = 0;
	}
    }

    if (RingRPos != RingWPos)
	return 1;
    else
	return 0;
}
Exemplo n.º 3
0
static int drv_LW_ABP_poll(void)
{
    char received[100];

    /* read into RingBuffer */
    while (1) {
	char buffer[32];
	int num, n;
	num = drv_generic_serial_poll(buffer, sizeof(buffer));
	if (num <= 0)
	    break;
	/* put result into RingBuffer */
	for (n = 0; n < num; n++) {
	    RingBuffer[RingWPos++] = (unsigned char) buffer[n];
	    if (RingWPos >= sizeof(RingBuffer))
		RingWPos = 0;
	}
    }

    received[0] = '\0';

    /* process RingBuffer */
    while (1) {
	char command[32];
	int n, num;
	/* packet size */
	num = RingWPos - RingRPos;
	if (num < 0)
	    num += sizeof(RingBuffer);
	/* minimum packet size=3 */
	if (num < 1) {
	    if (strlen(received) > 0) {
		debug("%s: received: %s", Name, received);
	    }
	    return 0;
	}
	if (byte(0) != '[') {
	    goto GARBAGE;
	}
	for (n = 0; (n < num) && ((unsigned) n < (sizeof(command) - 1)); n++) {
	    command[n] = byte(n);
	    if (command[n] == ']') {
		n++;
		break;
	    }
	}
	command[n] = '\0';
	if (command[n - 1] != ']') {
	    if (strlen(command) < 4) {
		if (strlen(received) > 0) {
		    debug("%s: received: %s", Name, received);
		}
		return 0;
	    }
	    goto GARBAGE;
	}
	info("%s: command read from keypad: %s\n", Name, command);
	if (sscanf(command, "[T%d]", &button) == 1) {
	    info("%s: button %d pressed\n", Name, button);
	} else {
	    goto GARBAGE;
	}
	/* increment read pointer */
	RingRPos += strlen(command);
	if (RingRPos >= sizeof(RingBuffer))
	    RingRPos -= sizeof(RingBuffer);
	/* a packet arrived */
	if (strlen(received) > 0) {
	    debug("%s: received: %s", Name, received);
	}
	return 1;
      GARBAGE:
	switch (byte(0)) {
	case '\n':
	case '\r':
	case '>':
	    if (strlen(received) > 0) {
		debug("%s: received: %s", Name, received);
		received[0] = '\0';
	    }
	    break;
	default:
	    if (byte(0) < ' ')
		debug("%s: dropping garbage byte %02x", Name, byte(0));
	    else if ((strlen(received) + 2) > sizeof(received)) {
		debug("%s: received: %s", Name, received);
		received[0] = '\0';
	    }
	    sprintf(received, "%s%c", received, byte(0));
	    break;
	}
	RingRPos++;
	if (RingRPos >= sizeof(RingBuffer))
	    RingRPos = 0;
	continue;
    }

    /* not reached */
    return 0;
}