⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diag_l2_raw.c

📁 Freediag contains various drivers (ISO9141, ISO14230, SAEJ1850-VPW, SAEJ1850-PWM) for different adap
💻 C
字号:
/*
 *	freediag - Vehicle Diagnostic Utility
 *
 *
 * Copyright (C) 2001 Richard Almeida & Ibex Ltd (rpa@ibex.co.uk)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *************************************************************************
 *
 * Diag
 *
 * L2 driver for "raw" interface (just sends and receives data without
 * modifying it
 *
 */

#include "diag_os.h" /* operating specific includes */

#include <stdlib.h>
#include <stdio.h>

#include <fcntl.h>
#include <string.h>
#include <errno.h>

#include <time.h>

#include "diag.h"
#include "diag_l1.h"
#include "diag_l2.h"
#include "diag_err.h"
#include "diag_general.h"

#include "diag_l2_raw.h" /* prototypes for this file */

static char *cvsid = "$Id: diag_l2_raw.c,v 1.3 2002/04/03 04:56:40 bjorn_helgaas Exp $";

extern int diag_l2_debug;
#define DIAG_MODULE "diag_l2_raw"

/*
*/
int
diag_l2_proto_raw_startcomms( diag_l2_conn_t	*d_l2_conn, u_int16_t flags,
	int bitrate, u_int8_t target, u_int8_t source)
{
	/* Set the speed as shown */
	diag_l1_setspeed( d_l2_conn->diag_link->diag_l2_fd,
		bitrate, 8, 1, DIAG_L1_PAR_N);

	return(0);
}

/*
*/
int
diag_l2_proto_raw_stopcomms(diag_l2_conn_t* pX)
{
	return (0);
}

/*
 * Just send the data, with no processing etc
 */
int
diag_l2_proto_raw_send(diag_l2_conn_t *d_l2_conn, diag_msg_t *msg)
{
	int rv;

	if (diag_l2_debug & DIAG_DEBUG_WRITE)
		fprintf(stderr,
			"%s: diag_l2_send 0x%x 0x%x len %d called\n",
				DIAG_MODULE, d_l2_conn, msg, msg->len);

	rv = diag_l1_send (d_l2_conn->diag_link->diag_l2_fd, 0,
		msg->data, msg->len, d_l2_conn->diag_l2_p4min);

	if (diag_l2_debug & DIAG_DEBUG_WRITE)
		fprintf(stderr, "%s: about to return %d\n",
				DIAG_MODULE, rv);

	return(rv);
}

/*
*/
int
diag_l2_proto_raw_recv(diag_l2_conn_t *d_l2_conn, int timeout,
	void (*callback)(void *handle, diag_msg_t *msg), void *handle)
{
	u_int8_t rxbuf[1024];
	diag_msg_t msg;
	int rv;

	/*
 	 * Read data from fd
	 */
	rv = diag_l1_recv (d_l2_conn->diag_link->diag_l2_fd, 0,
		rxbuf, sizeof(rxbuf), timeout);

	if (rv <= 0)		/* Failure, or 0 bytes (which cant happen) */
		return(rv);


	msg.len = rv;
	msg.data = rxbuf;
	/* This is raw, unframed data */
	msg.fmt = 0;
	msg.next = 0;
	(void)gettimeofday(&msg.rxtime, NULL);

	if (diag_l2_debug & DIAG_DEBUG_READ)
	{
		printf("%s: rcv callback calling %x(%x)\n", DIAG_MODULE,
				callback, handle);
	}

	/*
	 * Call user callback routine
	 */
	if (callback)
		callback(handle, &msg);


	return(0);
}

/*
*/
diag_msg_t *
diag_l2_proto_raw_request(diag_l2_conn_t *d_l2_conn, diag_msg_t *msg,
		int *errval)
{
	int rv;
	diag_msg_t *rmsg = NULL;
	u_int8_t rxbuf[1024];

	rv = diag_l2_send(d_l2_conn, msg);
	if (rv < 0)
	{
		*errval = rv;
		return(NULL);
	}

	/* And wait for response */
	rv = diag_l1_recv (d_l2_conn->diag_link->diag_l2_fd,
		0, rxbuf, sizeof(rxbuf), 1000);

	if (rv <= 0)
	{
		*errval = rv;
	}
	else
	{
		/*
		 * Ok, alloc a message
		 */
		rmsg = diag_allocmsg(rv);
		memcpy(&rmsg->data, rxbuf, rv);	/* Data */
		rmsg->len = rv;
		rmsg->fmt = 0;
		(void)gettimeofday(&rmsg->rxtime, NULL);
	}
	return(rmsg);
}


/*
 * No need to do anything with ATP in raw mode
 */
int
diag_l2_proto_raw_atp(diag_l2_conn_t* d_l2_conn, int p2min, int p2max, int p3min, int p3max, int p4min)
{
	return (0);
}

/*
 * No need to do anything with Timer in raw mode
 */
void
diag_l2_proto_raw_timer(int iX)
{
}

/*
 * No need to do anything with Timeouts in raw mode
 */
void
diag_l2_proto_raw_timeout(diag_l2_conn_t * dlctX)
{

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -