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

📄 diag_general.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.
 *
 *************************************************************************
 *
 *
 * General routines
 *
 */
#include <stdlib.h>
#include <stdio.h>

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

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

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

static char *cvsid = "$Id: diag_general.c,v 1.3 2002/04/15 09:04:43 rpalmeida Exp $";

int diag_errno;		/* Global errno */

int diag_init(void)
{
	int rv;

	if ((rv = diag_l1_init()) < 0)
		return(rv);
	if ((rv = diag_l2_init()) < 0)
		return(rv);
	if ((rv = diag_os_init()) < 0)
		return(rv);

	(void)diag_dtc_init();

	return(0);
}


/*
 * Message handling
 */
diag_msg_t *
diag_allocmsg(int datalen)
{
	diag_msg_t *newmsg;

	newmsg = calloc(1, sizeof(diag_msg_t));
	newmsg->iflags |= DIAG_MSG_IFLAG_MALLOC;

	if (datalen)
		newmsg->data = calloc(1, datalen);
	else
		newmsg->data = NULL;
	newmsg->idata = newmsg->data;	/* Keep tab as users change newmsg->data */

	return(newmsg);
}

/* Duplicate a message, and its contents */
diag_msg_t *
diag_dupmsg(diag_msg_t *msg)
{
	diag_msg_t	*newmsg = NULL, *tmsg, *cmsg;

	/*
	 * Dup first msg
	 * -- we dont copy "iflags" as that is about how this message
	 * was created, and not about the message we are duplicating
	 */

	newmsg = diag_allocmsg(msg->len);
	newmsg->fmt = msg->fmt;
	newmsg->type = msg->type;
	newmsg->dest = msg->dest;
	newmsg->src = msg->src;
	newmsg->len = msg->len;
	newmsg->rxtime = msg->rxtime;
	
	/* Dup data */
	memcpy(newmsg->data, msg->data, msg->len);

	/* And any on chain */
	cmsg = newmsg;
	msg = msg->next;
	while (msg)
	{
		tmsg = diag_allocmsg(msg->len);
		if (tmsg == NULL)
			return(NULL);	/* Out of memory */

		tmsg->fmt = msg->fmt;
		tmsg->type = msg->type;
		tmsg->dest = msg->dest;
		tmsg->src = msg->src;
		tmsg->len = msg->len;
		tmsg->rxtime = msg->rxtime;
		tmsg->next = NULL;	/* Except next message pointer */
		/* Dup data */
		memcpy(tmsg->data, msg->data, msg->len);

		/* Attach tmsg, and update cmsg */
		cmsg->next = tmsg;
		cmsg = cmsg->next;

		/* And process next in list */
		msg = msg->next;
	}

	return(newmsg);
}

/* Duplicate a single message, dont follow the chain */
diag_msg_t *
diag_dupsinglemsg(diag_msg_t *msg)
{
	diag_msg_t	*newmsg = NULL;

	/* Dup first msg */

	newmsg = diag_allocmsg(msg->len);
	newmsg->fmt = msg->fmt;
	newmsg->type = msg->type;
	newmsg->dest = msg->dest;
	newmsg->src = msg->src;
	newmsg->len = msg->len;
	newmsg->rxtime = msg->rxtime;
	newmsg->iflags = msg->iflags;
	/* Dup data */
	memcpy(newmsg->data, msg->data, msg->len);

	return(newmsg);
}


/* Free a msg that we dup'ed */
void
diag_freemsg(diag_msg_t *msg)
{
	diag_msg_t *nextmsg;

	if ( (msg->iflags & DIAG_MSG_IFLAG_MALLOC) == 0 )
	{
		printf("ERROR: diag_freemsg called for non diag_allocmsg()'ed message 0x%x\n", msg);
		return;
	}

	while (msg)
	{
		nextmsg = msg->next;

		if (msg->idata)
			free(msg->idata);
		free(msg);
		msg = nextmsg;
	}
	return;
}

⌨️ 快捷键说明

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