kvaser.c 6.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/*******************************************************************************
 * 
 * Kvaser / Altera Logger of CAN FD frames.
 * 
 * Module: Kvaser access functions
 *
 * Copyright (C) 2015-2018 Ondrej Ille <ondrej.ille@gmail.com>
 *  
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

#include <pty.h>
#include <utmp.h>

#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <canlib.h>

#include <time.h>
#include <stdlib.h>

#include "include/kvaser.h"

/* Kvaser global variables */
static canHandle hnd_chn_1;
static canHandle hnd_chn_2;

static int kvalt_dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};


static void check(char* id, canStatus stat)
{
	if (stat != canOK) {
		char buf[50];
		buf[0] = '\0';
		canGetErrorText(stat, buf, sizeof(buf));
		printf("%s: failed, stat=%d (%s)\n", id, (int)stat, buf);
	}
}


int kvaser_init(void)
{
	canStatus stat;

	/* Allow signals to interrupt syscalls */
	siginterrupt(SIGINT, 1);

	canInitializeLibrary();

	/* Open channel, set parameters and go on bus */
	hnd_chn_1 = canOpenChannel(0, canOPEN_CAN_FD | canOPEN_EXCLUSIVE | 
					canOPEN_REQUIRE_EXTENDED);
	hnd_chn_2 = canOpenChannel(1, canOPEN_CAN_FD | canOPEN_EXCLUSIVE | 
					canOPEN_REQUIRE_EXTENDED);
	if (hnd_chn_1 < 0) {
		fprintf(stdout, "canOpenChannel 0");
		return -1;
	}

	if (hnd_chn_2 < 0) {
		fprintf(stdout, "canOpenChannel 1");
		return -1;
	}

	/* Channel 1 */

	/* Set Nominal Bit-rate to 500 K */ 
	stat = canSetBusParams(hnd_chn_1, canFD_BITRATE_500K_80P, 0, 0, 0, 0, 0);
	check("canSetBusParams", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}

	/* Set Data Bit-rate to 2 M */
	stat = canSetBusParamsFd(hnd_chn_1, canFD_BITRATE_2M_80P, 0, 0, 0);
	check("canSetBusParamsFd", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}

	stat = canBusOn(hnd_chn_1);
	check("canBusOn", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}
	
	/* Channel 2 */

	/* Set Nominal Bit-rate to 500 K */ 
	stat = canSetBusParams(hnd_chn_2, canFD_BITRATE_500K_80P, 0, 0, 0, 0, 0);
	check("canSetBusParams", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}


	/* Set Data Bit-rate to 2 M */
	stat = canSetBusParamsFd(hnd_chn_2, canFD_BITRATE_2M_80P, 0, 0, 0);
	check("canSetBusParamsFd", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}

	stat = canBusOn(hnd_chn_2);
	check("canBusOn", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}

	return EXIT_SUCCESS;

ErrorExit:
  	stat = canBusOff(hnd_chn_1);
  	stat = canBusOff(hnd_chn_2);
  	check("canBusOff", stat);
  	stat = canClose(hnd_chn_1);
  	stat = canClose(hnd_chn_2);
  	check("canClose", stat);
  	stat = canUnloadLibrary();
  	check("canUnloadLibrary", stat);
	return 1;
}


void print_can_frame(struct kvalt_can_frame *frame)
{
	fprintf(stdout, "CAN Frame: \n");
	fprintf(stdout, "	Data length: %d\n", frame->data_length);
	fprintf(stdout, "	RTR: 0x%x\n", frame->rtr);
	fprintf(stdout, "	BRS: 0x%x\n", frame->brs);
	fprintf(stdout, "	Frame Type: 0x%x\n", frame->fr_type);
	fprintf(stdout, "	ID Type: 0x%x\n", frame->id_type);
	fprintf(stdout, "	ID: %lu \n", frame->id);
}


int generate_can_frame(struct kvalt_can_frame *frame)
{
	if (frame == NULL){
		fprintf(stderr, "Invalid CAN frame pointer\n");
		return EXIT_FAILURE;
	}

	int rval;

	rval = rand();
	frame->id_type = (uint8_t)(rval % 2);

	rval = rand();
	frame->fr_type = (uint8_t)(rval % 2);

	rval = rand();	
	if (frame->fr_type){
		frame->brs = (uint8_t)(rval % 2);
		frame->rtr = 0;
	} else {
		frame->rtr = (uint8_t)(rval % 2);
		frame->brs = 0;
	}

	rval = rand();
	if (frame->fr_type) {
		if (frame->brs)
			frame->data_length = kvalt_dlc[rval % 16];
		else
			frame->data_length = kvalt_dlc[rval % 11];
	} else {
		if (frame->rtr) {
			frame->data_length = 0;
		} else {
			frame->data_length = kvalt_dlc[rval % 8];
		}
	}

	rval = rand();
	if (frame->id_type) {
		frame->id = (rval % 536870912);
	} else {
		frame->id = (rval % 2048);
	}

	memset(frame->data, 0, sizeof(frame->data));

	for (int i = 0; i < frame->data_length; i++){
		rval = rand();
		frame->data[i] = (char)(rval % 0xFF);
	}

	return EXIT_SUCCESS;
}


int kvaser_send_frame(struct kvalt_can_frame *frame, int channel)
{
	unsigned int flag = 0U;
	canStatus stat;
	canHandle tmp_handle;

	if (frame->rtr)
		flag |= canMSG_RTR;

	if (frame->id_type)
		flag |= canMSG_EXT;
	else
		flag |= canMSG_STD;

	if (frame->fr_type)
		flag |= canFDMSG_FDF;
	
	if (frame->brs)
		flag |= canFDMSG_BRS;

	if (channel == 0) {
		tmp_handle = hnd_chn_1;
	} else if (channel == 1) {
		tmp_handle = hnd_chn_2;
	} else {
		fprintf(stderr, "Invalid Kvaser Channel!\n");
		return EXIT_FAILURE;
	}

	stat = canWrite(tmp_handle, frame->id, &(frame->data),
			frame->data_length, flag);
	check("canWrite", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}

	stat = canWriteSync(tmp_handle, 1000);
	check("canWriteSync", stat);
	if (stat != canOK) {
		goto ErrorExit;
	}
	return EXIT_SUCCESS;

ErrorExit:
	fprintf(stderr, "Failed to send CAN Frame\n");
	return EXIT_FAILURE;
}


int kvaser_read_frame(struct kvalt_can_frame *frame, int channel)
{
	canStatus stat;
	canHandle tmp_handle;
	unsigned int flag;
	unsigned long time;

	if (channel == 0) {
		tmp_handle = hnd_chn_1;
	} else if (channel == 1) {
		tmp_handle = hnd_chn_2;
	} else {
		fprintf(stderr, "Invalid Kvaser Channel!\n");
		return EXIT_FAILURE;
	}
	stat = canReadWait(tmp_handle, &(frame->id), &(frame->data), &(frame->data_length), &flag, &time, 100);

	if (stat == canOK)
		return EXIT_SUCCESS;

	fprintf(stderr, "Error in reading CAN Frame");
	return EXIT_FAILURE;
}