drivers/can_serial/can_serial_hub.c
author Christian Taedcke <hacking@taedcke.com>
Thu, 22 Dec 2011 11:42:44 +0100
changeset 673 f511d955ac30
parent 451 236c2e901e2e
permissions -rw-r--r--
FIXED: - the macro SUB_PROG_CFLAGS was overwritten for 64bit machines using gcc. Because of this the xenomai compilation failed.
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): James Steward
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
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
     7
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
     8
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
     9
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
    10
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
    11
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
    12
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    13
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
    14
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
    15
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
    16
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
    17
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    18
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
    19
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
    20
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
    21
*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    22
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    23
/**
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    24
	Pseudo CAN hub application.
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
#define _GNU_SOURCE  //for asprintf()
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
#include <stdio.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    30
#include <unistd.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    31
#include <fcntl.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    32
#include <errno.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    33
#include <string.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    34
#include <termios.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    35
#include <stdlib.h>
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    36
#include <dlfcn.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
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    40
#ifdef DLL_CALL
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    41
#undef DLL_CALL
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    42
#endif
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    43
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    44
#define DLL_CALL(funcname) (* funcname##_driver)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    45
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    46
#include "canfestival.h"
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    47
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    48
int DLL_CALL(canfd)(CAN_HANDLE)FCT_PTR_INIT;
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
#ifdef DLL_CALL
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    51
#undef DLL_CALL
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    52
#endif
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
#define DLL_CALL(funcname) funcname##_driver
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
#define DLSYM(name)\
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    57
        *(void **) (&name##_driver) = dlsym(handle, #name"_driver");\
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    58
        if ((error = dlerror()) != NULL)  {\
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    59
                fprintf (stderr, "%s\n", error);\
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    60
                UnLoadCanDriver(handle);\
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    61
                return NULL;\
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
#define MAX_HUB_PORTS 16
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
typedef struct {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    67
	int fd;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    68
	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
    69
	char *name;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    70
} port;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    71
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    72
port hub_ports[MAX_HUB_PORTS + 1];  //An extra for a CAN driver port
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    73
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    74
s_BOARD bus_if = { "/dev/ttyS0", "125K" };
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    75
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    76
/* Receive a CAN message from a hub (pseudo) port */
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    77
int hub_receive(port *p, Message *m)
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
	int rv, N, n = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    80
	fd_set rfds;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    81
	struct timeval tv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    82
	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    83
	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
    84
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    85
retry:
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    86
	rv = read(p->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
    87
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    88
	if (rv == -1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    89
		fprintf(stderr, "read: %d, %s\n", p->fd, strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    90
		return -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    91
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    92
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    93
	n += rv;
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
	if (n == 4) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    96
		N = (4 + m->len);
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
		if (m->len > 8) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
    99
			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
   100
					m->len);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   101
			
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   102
			//try to resync
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   103
			return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   104
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   105
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
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   108
	if (n < N) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   109
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   110
		FD_ZERO(&rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   111
		FD_SET(p->fd, &rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   112
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   113
		tv.tv_sec = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   114
		tv.tv_usec=100000;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   115
		
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   116
		rv = select(p->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
   117
		if (rv == 0 || rv == -1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   118
			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
   119
			return 0;
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
		goto retry;	
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
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   125
	return 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   126
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   127
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   128
/* send a CAN message to one of the hub ports */
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   129
UNS8 hub_send(port *p, Message *m)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   130
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   131
	int rv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   132
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   133
	rv = write(p->fd, m, 4 + m->len);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   134
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   135
	if (rv != 4 + m->len) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   136
		return 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   137
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   138
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   139
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   140
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   141
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   142
/* Open a hub port */
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   143
int hub_open(port *p)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   144
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   145
	if (p->fd != -1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   146
		fprintf(stderr, "Warning, port %s is already open, fd %d!\n", 
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   147
					p->name, p->fd);	
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
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   150
	p->fd = open(p->name, O_RDWR);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   151
	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   152
	if (p->fd < 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   153
		fprintf(stderr, "open: %s, %s\n", p->name, strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   154
		goto exit_here;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   155
	}
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
	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
   158
		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
   159
					p->name, strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   160
		close(p->fd);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   161
		p->fd = -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   162
		goto exit_here;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   163
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   164
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   165
	memcpy(&p->new_termio, &p->old_termio, sizeof(p->old_termio));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   166
	cfmakeraw(&p->new_termio);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   167
	cfsetispeed(&p->new_termio, B115200);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   168
	cfsetospeed(&p->new_termio, B115200);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   169
	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
   170
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   171
exit_here:
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   172
	return p->fd;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   173
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   174
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   175
/* Close a hub port*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   176
int hub_close(port *p)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   177
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   178
	if (p->fd >= 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   179
		tcsetattr(p->fd, TCSANOW, &p->old_termio);
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
		p->fd = -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   182
	}
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
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   185
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   186
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   187
/** Read from the port index rd_port, and write to all other ports. */
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   188
int read_write(int rd_port, port *p, CAN_HANDLE h, fd_set *wfds, int max_fd)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   189
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   190
	Message m;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   191
	int rv, i;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   192
	fd_set wfds_copy;
451
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   193
	struct timeval tv = {.tv_sec = 0, .tv_usec = 0}; //wait 0 msec
448
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
	if (rd_port == MAX_HUB_PORTS) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   196
		rv = DLL_CALL(canReceive)(h, &m);
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 (rv == 1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   199
			return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   200
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   201
	} else {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   202
		rv = hub_receive(&p[rd_port], &m);
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
		if (rv != 1) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   205
			return rv;
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
	
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   209
	memcpy(&wfds_copy, wfds, sizeof(fd_set));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   210
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   211
	rv = select(max_fd + 1, NULL, &wfds_copy, NULL, &tv);
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
	if (rv <= 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   214
		return 0;
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
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   217
	for (i = 0; i < MAX_HUB_PORTS + 1; i++) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   218
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   219
		if (i == rd_port) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   220
			fprintf(stderr, "[%d] ", i);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   221
			continue;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   222
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   223
451
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   224
		if (p[i].fd < 0 || !FD_ISSET(p[i].fd, &wfds_copy)) {
448
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   225
			fprintf(stderr, "{%d} ", i);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   226
			continue;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   227
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   228
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   229
		fprintf(stderr, "<%d> ", i);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   230
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   231
		if (i == MAX_HUB_PORTS && h) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   232
			DLL_CALL(canSend)(h, &m);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   233
		} else {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   234
			hub_send(&p[i], &m);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   235
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   236
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   237
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   238
	print_message(&m);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   239
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   240
	return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   241
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   242
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   243
void help(void)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   244
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   245
	printf("\n\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   246
	printf("This is a software hub for the CANFestival library, \n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   247
	printf("based on the *nix pseudo tty.  It supports up to 16\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   248
	printf("connections from clients, and a connection to a CANFestival\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   249
	printf("driver, for connection to the outside world.\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   250
	printf("\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   251
	printf("Basic use is simply to run can_hub.  Without arguments, it\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   252
	printf("will use /dev/ptya[0..f] ptys.  You should then run your\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   253
	printf("linux CANFestival app using libcanfestival_can_serial.so\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   254
	printf("with the bus name /dev/ttyaX, where X is 0..f and unused.\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   255
	printf("\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   256
	printf("You can alter the pty base with -p /dev/ptyx .\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   257
	printf("\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   258
	printf("If you want to interface with some other CAN driver, supply\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   259
	printf("the option -l /path/to/libcanfestival_can_foo.so .\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   260
	printf("The default bus name and baud are /dev/ttyS0 and 125k.\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   261
	printf("These can be overridden with -b /dev/{bus name} and -s {baud}.\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   262
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   263
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   264
/*UnLoads the dll*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   265
UNS8 UnLoadCanDriver(LIB_HANDLE handle)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   266
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   267
        if(handle!=NULL)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   268
        {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   269
                dlclose(handle);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   270
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   271
                handle=NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   272
                return 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   273
        }
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   274
        return -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   275
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   276
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   277
/*Loads the dll and get funcs ptr*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   278
LIB_HANDLE LoadCanDriver(char* driver_name)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   279
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   280
        LIB_HANDLE handle = NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   281
        char *error;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   282
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   283
        if(handle==NULL)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   284
        {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   285
                handle = dlopen(driver_name, RTLD_LAZY);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   286
        }
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   287
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   288
        if (!handle) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   289
                fprintf (stderr, "%s\n", dlerror());
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   290
                return NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   291
        }
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   292
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   293
        /*Get function ptr*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   294
        DLSYM(canReceive)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   295
        DLSYM(canSend)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   296
        DLSYM(canOpen)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   297
        DLSYM(canChangeBaudRate)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   298
        DLSYM(canClose)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   299
	DLSYM(canfd)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   300
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   301
        return handle;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   302
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   303
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   304
/**
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   305
*/
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   306
int main(int argc, char **argv)
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   307
{
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   308
	int i, rv, max_fd = 0, ret = 0;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   309
	fd_set rfds, rfds_copy;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   310
	CAN_HANDLE can_h = NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   311
        LIB_HANDLE lib_h = NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   312
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   313
	int c;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   314
	extern char *optarg;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   315
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   316
	char *can_drv = NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   317
	char *pty_base = "/dev/ptya";
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   318
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   319
	while ((c = getopt(argc, argv, "-b:s:l:p:h")) != EOF) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   320
		switch (c) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   321
		case 'b':
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   322
			if (optarg[0] == 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   323
				help();
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   324
				exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   325
			}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   326
			bus_if.busname = optarg;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   327
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   328
		case 's':
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   329
			if (optarg[0] == 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   330
				help();
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   331
				exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   332
			}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   333
			bus_if.baudrate = optarg;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   334
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   335
		case 'l':
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   336
			if (optarg[0] == 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   337
				help();
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   338
				exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   339
			}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   340
			can_drv = optarg;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   341
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   342
		case 'p':
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   343
			if (optarg[0] == 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   344
				help();
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   345
				exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   346
			}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   347
			pty_base = optarg;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   348
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   349
		case 'h':
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   350
			help();
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   351
			exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   352
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   353
		default:
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   354
			help();
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   355
			exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   356
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   357
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   358
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   359
	FD_ZERO(&rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   360
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   361
	hub_ports[MAX_HUB_PORTS].fd = -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   362
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   363
	if (can_drv) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   364
		lib_h = LoadCanDriver(can_drv);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   365
		if (lib_h == NULL) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   366
			printf("Unable to load library: %s\n", can_drv);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   367
			exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   368
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   369
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   370
	        can_h = DLL_CALL(canOpen)(&bus_if);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   371
        	if(!can_h) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   372
        	        fprintf(stderr,"canOpen : failed\n");
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   373
        	        exit(1);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   374
        	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   375
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   376
		hub_ports[MAX_HUB_PORTS].fd = DLL_CALL(canfd)(can_h);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   377
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   378
		FD_SET(hub_ports[MAX_HUB_PORTS].fd, &rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   379
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   380
		if (hub_ports[MAX_HUB_PORTS].fd > max_fd) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   381
			max_fd = hub_ports[MAX_HUB_PORTS].fd;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   382
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   383
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   384
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   385
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   386
	for (i = 0; i < MAX_HUB_PORTS; i++) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   387
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   388
		hub_ports[i].fd = -1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   389
		hub_ports[i].name = NULL;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   390
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   391
		rv = asprintf(&hub_ports[i].name, "%s%x", pty_base, i);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   392
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   393
		if (rv < 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   394
			fprintf(stderr, "asprintf: %s\n", strerror(errno));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   395
			ret = 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   396
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   397
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   398
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   399
		rv = hub_open(&hub_ports[i]);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   400
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   401
		if (rv < 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   402
			ret = 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   403
			break;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   404
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   405
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   406
		FD_SET(rv, &rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   407
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   408
		if (rv > max_fd) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   409
			max_fd = rv;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   410
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   411
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   412
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   413
	if (ret) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   414
		return ret;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   415
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   416
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   417
	while (!ret) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   418
		memcpy(&rfds_copy, &rfds, sizeof(rfds));
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   419
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   420
		rv = select(max_fd + 1, &rfds_copy, NULL, NULL, NULL);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   421
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   422
		if (rv < 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   423
			//select error
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   424
			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
   425
			ret = 1;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   426
			continue;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   427
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   428
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   429
		//as timeout is NULL, must be a rfds set.
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   430
		for (i = 0; i < MAX_HUB_PORTS + 1; i++) {
451
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   431
			if (hub_ports[i].fd >= 0 && 
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   432
					FD_ISSET(hub_ports[i].fd, &rfds_copy)) {
448
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   433
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   434
				rv = read_write(i, hub_ports, can_h, &rfds, max_fd);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   435
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   436
				if (rv < 0 && i < MAX_HUB_PORTS) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   437
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   438
					FD_CLR(hub_ports[i].fd, &rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   439
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   440
					hub_close(&hub_ports[i]);
451
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   441
				}						
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   442
			}
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   443
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   444
			if (hub_ports[i].fd < 0 && i < MAX_HUB_PORTS) {
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   445
				rv = hub_open(&hub_ports[i]);
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   446
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   447
				if (rv >= 0) {
448
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   448
					FD_SET(rv, &rfds);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   449
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   450
					if (rv > max_fd) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   451
						max_fd = rv;
451
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   452
					}
236c2e901e2e Serial Hub fixes... Thanks James.
etisserant
parents: 448
diff changeset
   453
				}
448
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   454
			}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   455
		}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   456
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   457
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   458
	for (i = 0; i < MAX_HUB_PORTS; i++) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   459
		hub_close(&hub_ports[i]);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   460
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   461
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   462
	if (hub_ports[MAX_HUB_PORTS].fd >= 0) {
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   463
		DLL_CALL(canClose)(&bus_if);
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   464
	}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   465
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   466
	return ret;
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   467
}
732c33c2d8a7 CAN over Serial link (TTY) interface, with serial hub software. Thanks to James Steward.
etisserant
parents:
diff changeset
   468