drivers/can_serial/can_serial.c
author Edouard Tisserant
Fri, 03 Feb 2012 15:43:44 +0100
changeset 693 e21822be8189
parent 631 08b6b903f84a
permissions -rw-r--r--
Applyed changes from discussion '[Canfestival-devel] some minor CanFestival build suggestions', https://sourceforge.net/mailarchive/message.php?msg_id=28397766
448
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     1
/*
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     3
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     5
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     6
Ruthlessly butchered by James Steward to produce a serial (tty) port
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     7
driver.
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     8
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     9
See COPYING file for copyrights details.
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    10
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    11
This library is free software; you can redistribute it and/or
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    12
modify it under the terms of the GNU Lesser General Public
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    13
License as published by the Free Software Foundation; either
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    14
version 2.1 of the License, or (at your option) any later version.
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    15
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    16
This library is distributed in the hope that it will be useful,
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    17
but WITHOUT ANY WARRANTY; without even the implied warranty of
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    19
Lesser General Public License for more details.
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    20
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    21
You should have received a copy of the GNU Lesser General Public
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    22
License along with this library; if not, write to the Free Software
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    23
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    24
*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    25
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    26
/*
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    27
	Single serial port CAN driver.
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    28
*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    29
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    30
#include <stdio.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    31
#include <unistd.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    32
#include <fcntl.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    33
#include <errno.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    34
#include <string.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    35
#include <termios.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    36
#include <stdlib.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    37
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    38
#define NEED_PRINT_MESSAGE
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    39
#include "can_driver.h"
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    40
#include "def.h"
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    41
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    42
typedef struct {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    43
	int fd;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    44
	struct termios old_termio, new_termio;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    45
} CANPort;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    46
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    47
/*********functions which permit to communicate with the board****************/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    48
UNS8 canReceive_driver(CAN_HANDLE fd0, Message * m)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    49
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    50
	int rv, N, n = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    51
	fd_set rfds;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    52
	struct timeval tv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    53
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    54
	N = 4; //initially try to read 4 bytes, including the length byte
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    55
	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    56
retry:
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    57
	rv = read(((CANPort *) fd0)->fd, &((char *)m)[n], N - n);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    58
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    59
	if (rv == -1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    60
		fprintf(stderr, "read: %s\n", strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    61
		return 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    62
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    63
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    64
	n += rv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    65
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    66
	if (n == 4) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    67
		N = (4 + m->len);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    68
		
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    69
		if (m->len > 8) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    70
			fprintf(stderr, "Warning: invalid message length %d\n", 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    71
					m->len);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    72
			
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    73
			//try to resync
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    74
			n = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    75
			N = 4;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    76
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    77
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    78
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    79
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    80
	if (n < N) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    81
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    82
		FD_ZERO(&rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    83
		FD_SET(((CANPort *) fd0)->fd, &rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    84
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    85
		tv.tv_sec = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    86
		tv.tv_usec=100000;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    87
		
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    88
		rv = select(((CANPort *) fd0)->fd + 1, &rfds, NULL, NULL, &tv);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    89
		if (rv == -1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    90
			fprintf(stderr, "select: %s\n", strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    91
			return 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    92
		} else if (rv == 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    93
			n = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    94
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    95
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    96
		goto retry;	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    97
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    98
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    99
	print_message(m);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   100
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   101
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   102
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   103
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   104
/***************************************************************************/
631
08b6b903f84a Piotr Trojanek (ptroja) cleanup patche. Thanks.
edouard
parents: 448
diff changeset
   105
UNS8 canSend_driver(CAN_HANDLE fd0, Message const * m)
448
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   106
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   107
	int rv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   108
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   109
	print_message(m);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   110
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   111
	// Send to serial port 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   112
	rv = write(((CANPort *) fd0)->fd, m, 4 + m->len);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   113
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   114
	if (rv != 4 + m->len) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   115
		return 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   116
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   117
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   118
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   119
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   120
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   121
/***************************************************************************/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   122
int TranslateBaudRate(char *optarg)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   123
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   124
	if (!strcmp(optarg, "1M"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   125
		return (int) 1000;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   126
	if (!strcmp(optarg, "500K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   127
		return (int) 500;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   128
	if (!strcmp(optarg, "250K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   129
		return (int) 250;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   130
	if (!strcmp(optarg, "125K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   131
		return (int) 125;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   132
	if (!strcmp(optarg, "100K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   133
		return (int) 100;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   134
	if (!strcmp(optarg, "50K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   135
		return (int) 50;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   136
	if (!strcmp(optarg, "20K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   137
		return (int) 20;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   138
	if (!strcmp(optarg, "10K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   139
		return (int) 10;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   140
	if (!strcmp(optarg, "5K"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   141
		return (int) 5;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   142
	if (!strcmp(optarg, "none"))
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   143
		return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   144
	return 0x0000;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   145
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   146
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   147
UNS8 canChangeBaudRate_driver(CAN_HANDLE fd0, char *baud)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   148
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   149
	printf("Faked changing to baud rate %s[%d]\n", 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   150
			baud, TranslateBaudRate(baud));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   151
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   152
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   153
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   154
/***************************************************************************/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   155
CAN_HANDLE canOpen_driver(s_BOARD * board)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   156
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   157
	int rv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   158
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   159
	CANPort *p;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   160
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   161
	p = (CANPort *)calloc(1, sizeof(CANPort));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   162
	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   163
	if (p == NULL) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   164
		fprintf(stderr, "calloc: %s\n", strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   165
		return (CAN_HANDLE) NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   166
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   167
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   168
	p->fd = open(board->busname, O_RDWR);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   169
	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   170
	if (p->fd < 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   171
		fprintf(stderr, "open: %s, %s\n", 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   172
				board->busname, strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   173
		free(p);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   174
		return (CAN_HANDLE) NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   175
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   176
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   177
	if (tcgetattr(p->fd, &p->old_termio) != 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   178
		fprintf(stderr, "tcgetattr: %s, %s\n", 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   179
				board->busname, strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   180
		close(p->fd);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   181
		free(p);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   182
		return (CAN_HANDLE) NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   183
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   184
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   185
	memcpy(&p->new_termio, &p->old_termio, 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   186
					sizeof(p->old_termio));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   187
	cfmakeraw(&p->new_termio);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   188
	cfsetispeed(&p->new_termio, B115200);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   189
	cfsetospeed(&p->new_termio, B115200);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   190
	tcsetattr(p->fd, TCSANOW, &p->new_termio);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   191
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   192
	return (CAN_HANDLE) p;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   193
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   194
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   195
/***************************************************************************/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   196
int canClose_driver(CAN_HANDLE fd0)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   197
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   198
	if ((CANPort *) fd0 && ((CANPort *) fd0)->fd >= 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   199
		tcsetattr(((CANPort *) fd0)->fd, TCSANOW, 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   200
					&((CANPort *) fd0)->old_termio);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   201
		close(((CANPort *) fd0)->fd);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   202
		free((CANPort *) fd0);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   203
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   204
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   205
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   206
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   207
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   208
int canfd_driver(CAN_HANDLE fd0)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   209
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   210
	if ((CANPort *) fd0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   211
		return ((CANPort *) fd0)->fd;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   212
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   213
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   214
	return -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   215
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   216