drivers/can_virtual/can_virtual.c
author leonid
Wed, 08 Aug 2007 18:40:47 +0200
changeset 257 4f3364846782
parent 193 0487270d441c
child 301 f4b64aa848e1
permissions -rw-r--r--
*** empty log message ***
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
	Virtual CAN driver.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    27
#include <stdio.h>
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    28
#include <unistd.h>
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    30
#include "can_driver.h"
157
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    31
#include "def.h"
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    33
#define MAX_NB_CAN_PIPES 16
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
typedef struct {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
  char used;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
  int pipe[2];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
} CANPipe;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    40
CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},};
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
/*********functions which permit to communicate with the board****************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    43
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
{
193
0487270d441c Fixed problems in can_virtual, loop when closing pipes.
etisserant
parents: 157
diff changeset
    45
	if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) != (ssize_t)sizeof(Message))
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
		return 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
	return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
157
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    52
#define MyCase(fc) case fc: printf(#fc);break;
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    53
void print_message(Message *m)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    54
{
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    55
	int i;
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    56
	switch(m->cob_id.w >> 7)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    57
	{
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    58
		MyCase(SYNC)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    59
		MyCase(TIME_STAMP)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    60
		MyCase(PDO1tx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    61
		MyCase(PDO1rx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    62
		MyCase(PDO2tx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    63
		MyCase(PDO2rx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    64
		MyCase(PDO3tx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    65
		MyCase(PDO3rx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    66
		MyCase(PDO4tx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    67
		MyCase(PDO4rx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    68
		MyCase(SDOtx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    69
		MyCase(SDOrx)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    70
		MyCase(NODE_GUARD)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    71
		MyCase(NMT)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    72
	}
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    73
	printf(" rtr:%d", m->rtr);
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    74
	printf(" len:%d", m->len);
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    75
	for (i = 0 ; i < m->len ; i++)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    76
		printf(" %02x", m->data[i]);
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    77
	printf("\n");
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    78
}
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    79
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
    81
UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
  int i;
157
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    84
  
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    85
  printf("%x->[ ", (CANPipe*)fd0 - &canpipes[0]); 
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    86
  for(i=0; i < MAX_NB_CAN_PIPES; i++)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    87
  {
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    88
  	if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0)
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    89
  	{
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    90
		printf("%x ",i);	
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    91
  	}
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    92
  }
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    93
  printf(" ]");	
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    94
  print_message(m);
8b45ff4202c3 Added cosmetics with Can_Virtual
etisserant
parents: 145
diff changeset
    95
  
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
  // Send to all readers, except myself
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
  for(i=0; i < MAX_NB_CAN_PIPES; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
  	if(canpipes[i].used && &canpipes[i] != (CANPipe*)fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
  	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
		write(canpipes[i].pipe[1], m, sizeof(Message));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
  	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
  return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
}
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   106
/*
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   107
int TranslateBaudeRate(char* optarg){
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   108
	if(!strcmp( optarg, "1M")) return 1000;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   109
	if(!strcmp( optarg, "500K")) return 500;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   110
	if(!strcmp( optarg, "250K")) return 250;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   111
	if(!strcmp( optarg, "125K")) return 125;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   112
	if(!strcmp( optarg, "100K")) return 100;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   113
	if(!strcmp( optarg, "50K")) return 50;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   114
	if(!strcmp( optarg, "20K")) return 20;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   115
	if(!strcmp( optarg, "10K")) return 10;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   116
	if(!strcmp( optarg, "5K")) return 5;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   117
	if(!strcmp( optarg, "none")) return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   118
	return 0;
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   119
}*/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   121
CAN_HANDLE canOpen_driver(s_BOARD *board)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
  int i;  
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
  for(i=0; i < MAX_NB_CAN_PIPES; i++)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
  {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
  	if(!canpipes[i].used)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
	  	break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
  }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
  /* Create the pipe.  */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
  if (i==MAX_NB_CAN_PIPES || pipe(canpipes[i].pipe))
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
      fprintf (stderr, "Open failed.\n");
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
      return (CAN_HANDLE)NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
   canpipes[i].used = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
   return (CAN_HANDLE) &canpipes[i];
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
/***************************************************************************/
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   142
int canClose_driver(CAN_HANDLE fd0)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
  close(((CANPipe*)fd0)->pipe[0]);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
  close(((CANPipe*)fd0)->pipe[1]);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
  ((CANPipe*)fd0)->used = 0;
145
e747d2e26af0 Win32 Native support and dynamicaly loaded CAN drivers for Linux, Cygwin and Win32.
etisserant
parents: 0
diff changeset
   147
  return 0;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150