src/plugins/serial/serial_terminal.c
static int
recv_response(struct ipmi_intf * intf, unsigned char *data, int len)
{
	char hex_rs[IPMI_SERIAL_MAX_RESPONSE * 3];
	int i, j, resp_len = 0;
	long rv;
	char *p, *pp;
	char ch, str_hex[3];

	p = hex_rs;
	while (1) {
		if ((rv = serial_read_line(intf, p, sizeof(hex_rs) - resp_len)) < 0) {
			/* error */
			return -1;
		}
		p += rv;
		resp_len += rv;
		if (resp_len >= 2 && *(p - 2) == ']'
			&& (*(p - 1) == '\n' || *(p - 1) == '\r')) {
			*(p - 1) = 0; /* overwrite EOL */
			break;
		}
	}

	p = strrchr(hex_rs, '[');
	if (!p) {
		lprintf(LOG_ERR, "Serial response is invalid");
		return -1;
	}

	p++;
	pp = strchr(p, ']');
	if (!pp) {
		lprintf(LOG_ERR, "Serial response is invalid");
		return -1;
	}
	*pp = 0;

	/* was it an error? */
	if (!strcmp(p, "ERR ")) {
		serial_write_line(intf, "\r\r\r\r");
		sleep(1);
		serial_flush(intf);
		errno = 0;
		rv = strtoul(p + 4, &p, 16);
		if ((rv && rv < 0x100 && *p == '\0')
				|| (rv == 0 && !errno)) {
			/* The message didn't get it through. The upper
			   level will have to re-send */
			return 0;
		} else {
			lprintf(LOG_ERR, "Serial response is invalid");
			return -1;
		}
	}

	/* this is needed for correct string to long conversion */
	str_hex[2] = 0;

	/* parse the response */
	i = 0;
	j = 0;
	while (*p) {
		if (i >= len) {
			lprintf(LOG_ERR, "Serial response is too long(%d, %d)", i, len);
			return -1;
		}
		ch = *(p++);
		if (isxdigit(ch)) {
			str_hex[j++] = ch;
		} else {
			if (j == 1 || !isspace(ch)) {
				lprintf(LOG_ERR, "Serial response is invalid");
				return -1;
			}
		}
		if (j == 2) {
			unsigned long tmp;
			errno = 0;
			/* parse the hex number */
			tmp = strtoul(str_hex, NULL, 16);
			if ( tmp > 0xFF || ( !tmp && errno ) ) {
				lprintf(LOG_ERR, "Serial response is invalid");
				return -1;
			}
			data[i++] = tmp;
			j = 0;
		}
	}

	return i;
}
/*
 *	Receive IPMI response from the device
 *	Len: buffer size
 *	Returns: -1 or response length on success
 */
Receive IPMI response from the device
Len: buffer size
Returns: -1 or response length on success
Receive IPMI response from the device Len : buffer size Returns : - 1 or response length on success
static int
recv_response(struct ipmi_intf * intf, unsigned char *data, int len)
{
	char hex_rs[IPMI_SERIAL_MAX_RESPONSE * 3];
	int i, j, resp_len = 0;
	long rv;
	char *p, *pp;
	char ch, str_hex[3];
	p = hex_rs;
	while (1) {
		if ((rv = serial_read_line(intf, p, sizeof(hex_rs) - resp_len)) < 0) {
			return -1;
		}
		p += rv;
		resp_len += rv;
		if (resp_len >= 2 && *(p - 2) == ']'
			&& (*(p - 1) == '\n' || *(p - 1) == '\r')) {
			*(p - 1) = 0; 
			break;
		}
	}
	p = strrchr(hex_rs, '[');
	if (!p) {
		lprintf(LOG_ERR, "Serial response is invalid");
		return -1;
	}
	p++;
	pp = strchr(p, ']');
	if (!pp) {
		lprintf(LOG_ERR, "Serial response is invalid");
		return -1;
	}
	*pp = 0;
	if (!strcmp(p, "ERR ")) {
		serial_write_line(intf, "\r\r\r\r");
		sleep(1);
		serial_flush(intf);
		errno = 0;
		rv = strtoul(p + 4, &p, 16);
		if ((rv && rv < 0x100 && *p == '\0')
				|| (rv == 0 && !errno)) {
			return 0;
		} else {
			lprintf(LOG_ERR, "Serial response is invalid");
			return -1;
		}
	}
	str_hex[2] = 0;
	i = 0;
	j = 0;
	while (*p) {
		if (i >= len) {
			lprintf(LOG_ERR, "Serial response is too long(%d, %d)", i, len);
			return -1;
		}
		ch = *(p++);
		if (isxdigit(ch)) {
			str_hex[j++] = ch;
		} else {
			if (j == 1 || !isspace(ch)) {
				lprintf(LOG_ERR, "Serial response is invalid");
				return -1;
			}
		}
		if (j == 2) {
			unsigned long tmp;
			errno = 0;
			tmp = strtoul(str_hex, NULL, 16);
			if ( tmp > 0xFF || ( !tmp && errno ) ) {
				lprintf(LOG_ERR, "Serial response is invalid");
				return -1;
			}
			data[i++] = tmp;
			j = 0;
		}
	}
	return i;
}
static int recv_response ( struct ipmi_intf * intf , unsigned char * data , int len ) { char hex_rs [ IPMI_SERIAL_MAX_RESPONSE * 3 ] ; int i , j , resp_len = 0 ; long rv ; char * p , * pp ; char ch , str_hex [ 3 ] ; p = hex_rs ; while ( 1 ) { if ( ( rv = serial_read_line ( intf , p , sizeof ( hex_rs ) - resp_len ) ) < 0 ) { return -1 ; } p += rv ; resp_len += rv ; if ( resp_len >= 2 && * ( p - 2 ) == ' ' && ( * ( p - 1 ) == ' \n ' || * ( p - 1 ) == ' \r ' ) ) { * ( p - 1 ) = 0 ; break ; } } p = strrchr ( hex_rs , ' ' ) ; if ( ! p ) { lprintf ( LOG_ERR , " " ) ; return -1 ; } p ++ ; pp = strchr ( p , ' ' ) ; if ( ! pp ) { lprintf ( LOG_ERR , " " ) ; return -1 ; } * pp = 0 ; if ( ! strcmp ( p , " " ) ) { serial_write_line ( intf , " \r \r \r \r " ) ; sleep ( 1 ) ; serial_flush ( intf ) ; errno = 0 ; rv = strtoul ( p + 4 , & p , 16 ) ; if ( ( rv && rv < 0x100 && * p == ' \0 ' ) || ( rv == 0 && ! errno ) ) { return 0 ; } else { lprintf ( LOG_ERR , " " ) ; return -1 ; } } str_hex [ 2 ] = 0 ; i = 0 ; j = 0 ; while ( * p ) { if ( i >= len ) { lprintf ( LOG_ERR , " " , i , len ) ; return -1 ; } ch = * ( p ++ ) ; if ( isxdigit ( ch ) ) { str_hex [ j ++ ] = ch ; } else { if ( j == 1 || ! isspace ( ch ) ) { lprintf ( LOG_ERR , " " ) ; return -1 ; } } if ( j == 2 ) { unsigned long tmp ; errno = 0 ; tmp = strtoul ( str_hex , NULL , 16 ) ; if ( tmp > 0xFF || ( ! tmp && errno ) ) { lprintf ( LOG_ERR , " " ) ; return -1 ; } data [ i ++ ] = tmp ; j = 0 ; } } return i ; }
Receive IPMI response from the device
      Len: buffer size
      Returns: -1 or response length on success
Receive IPMI response from the device Len : buffer size Returns : - 1 or response length on success
/* error */ /* overwrite EOL */ /* was it an error? */ /* The message didn't get it through. The upper
			   level will have to re-send */ /* this is needed for correct string to long conversion */ /* parse the response */ /* parse the hex number */