drivers/can_anagate_linux/can_anagate_linux.c
author Robert Lehmann <robert.lehmann@sitec-systems.de>
Tue, 28 Jul 2015 16:36:55 +0200
changeset 793 72e9e1064432
parent 631 08b6b903f84a
permissions -rw-r--r--
timers_unix: Fix termination problem of WaitReceiveTaskEnd

The function pthread_kill sends the Signal thread and to the own process.
If you use this construct than the application which calls uses the
canfestival api will terminate at the call of canClose. To avoid that
use pthread_cancel instead of pthread_kill. To use the pthread_cancel call
you need to set the cancel ability in the thread function. That means
you need to call pthread_setcancelstate and pthread_setcanceltype.
For the termination of the thread at any time it is important to set the
cancel type to PTHREAD_CANCEL_ASYNCHRONOUS.
494
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     1
/*
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack. 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     3
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     4
Copyright (C): Edouard TISSERANT and Francis DUPIN
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     5
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     6
See COPYING file for copyrights details.
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     7
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    12
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    16
Lesser General Public License for more details.
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    17
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    21
*/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    22
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    23
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    24
#include <stdio.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    25
#include <string.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    26
#include <errno.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    27
#include <fcntl.h>
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    28
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    29
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    30
#include "can_driver.h"
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    31
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    32
struct SAnaGatePort
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    33
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    34
   int      hHandle;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    35
   Message  sMsgBuffer;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    36
   struct   SAnaGatePort *pNext;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    37
   struct   SAnaGatePort *pPrev;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    38
   int      bBufferFull;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    39
};
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    40
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    41
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    42
struct SAnaGatePort *pFistAnaGatePort = NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    43
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    44
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    45
/********* AnaGate API CAN receive callback Funciton  ****************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    46
void AnaGateReceiveCallBack (int nIdentifier, const char* pcBuffer, int nBufferLen, int nFlags, int hHandle)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    47
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    48
   int i;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    49
   struct SAnaGatePort *pAnaGatePort = pFistAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    50
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    51
   while (pAnaGatePort->hHandle != hHandle )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    52
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    53
      pAnaGatePort = pAnaGatePort->pNext;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    54
      if (pAnaGatePort == pFistAnaGatePort )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    55
      {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    56
         pAnaGatePort = NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    57
         printf("AnaGateReceiveCallBack (AnaGate_Linux): ERROR: Can't find AnaGatePort-Objekt to the Received Handle %d\n",hHandle);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    58
         return;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    59
      }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    60
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    61
   while (pAnaGatePort->bBufferFull)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    62
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    63
      usleep(5000);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    64
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    65
   pAnaGatePort->sMsgBuffer.cob_id =   nIdentifier;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    66
   pAnaGatePort->sMsgBuffer.len= nBufferLen;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    67
   if (nFlags == 2)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    68
    pAnaGatePort->sMsgBuffer.rtr = 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    69
   else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    70
    pAnaGatePort->sMsgBuffer.rtr = 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    71
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    72
   for (i = 0 ; i < nBufferLen; i++)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    73
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    74
      pAnaGatePort->sMsgBuffer.data[i] = pcBuffer[i];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    75
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    76
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    77
   pAnaGatePort->bBufferFull = 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    78
 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    79
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    80
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    81
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    82
/*********functions which permit to communicate with the board****************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    83
UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    84
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    85
   int i;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    86
   struct SAnaGatePort*  pAnaGatePort = (struct SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    87
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    88
   while (pAnaGatePort->bBufferFull == 0)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    89
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    90
      usleep (5000); 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    91
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    92
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    93
   m->cob_id = pAnaGatePort->sMsgBuffer.cob_id;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    94
   m->len     = pAnaGatePort->sMsgBuffer.len;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    95
   m->rtr     = pAnaGatePort->sMsgBuffer.rtr;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    96
   for (i = 0 ; i < pAnaGatePort->sMsgBuffer.len; i++)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    97
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    98
     m->data[i] = pAnaGatePort->sMsgBuffer.data[i];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
    99
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   100
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   101
   pAnaGatePort->bBufferFull = 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   102
 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   103
   return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   104
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   105
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   106
/***************************************************************************/
631
08b6b903f84a Piotr Trojanek (ptroja) cleanup patche. Thanks.
edouard
parents: 494
diff changeset
   107
UNS8 canSend_driver(CAN_HANDLE fd0, Message const *m)
494
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   108
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   109
   struct SAnaGatePort*  pAnaCanPort = (struct SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   110
   char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   111
   int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   112
   int nMsgTyp;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   113
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   114
   if (m->rtr == 0)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   115
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   116
     nMsgTyp = 0; //Normal;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   117
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   118
   else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   119
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   120
     nMsgTyp = 2; //Remote frame;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   121
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   122
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   123
   if ( (nRetCode = CANWrite(pAnaCanPort->hHandle , m->cob_id,(const char*) m->data, m->len, nMsgTyp) ) )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   124
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   125
     CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   126
     fprintf(stderr,"canSend_driver (AnaGate_Linux) %s \n",nRetCode);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   127
     //printf("canSend_driver (AnaGate_Linux) %s \n",nRetCode);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   128
     return 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   129
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   130
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   131
   return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   132
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   133
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   134
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   135
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   136
int TranslateBaudeRate(char* optarg){
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   137
   if(!strcmp( optarg, "1M"))   return 1000000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   138
   if(!strcmp( optarg, "800K")) return  800000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   139
   if(!strcmp( optarg, "500K")) return  500000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   140
   if(!strcmp( optarg, "250K")) return  250000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   141
   if(!strcmp( optarg, "125K")) return  125000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   142
   if(!strcmp( optarg, "100K")) return  100000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   143
   if(!strcmp( optarg, "50K"))  return   50000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   144
   if(!strcmp( optarg, "20K"))  return   20000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   145
   if(!strcmp( optarg, "10K"))  return   10000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   146
 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   147
   return 0x0000;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   148
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   149
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   150
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   151
UNS8 canChangeBaudRate_driver( CAN_HANDLE fd0, char* baud)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   152
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   153
   int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   154
   char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   155
   struct SAnaGatePort*  pAnaGatePort = (struct SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   156
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   157
   if (nRetCode = CANSetGlobals (pAnaGatePort->hHandle, TranslateBaudeRate(baud), 0, 0, 1) ) 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   158
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   159
      CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   160
      fprintf(stderr, "canChangeBaudRate_drive (AnaGate_Linux): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   161
      //printf("canChangeBaudRate_drive (AnaGate_Linux): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   162
      return 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   163
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   164
   return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   165
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   166
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   167
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   168
/* To open a connection to AnaGate CAN the s_BOARD board->busname must be 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   169
 the AnaGate IP-Adresse followed from the CAN-Port (A or B) you want to use 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   170
 For example "192.168.1.254:A"
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   171
*/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   172
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   173
CAN_HANDLE canOpen_driver(s_BOARD *board)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   174
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   175
   int nPortNr;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   176
   char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   177
   int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   178
   char sIPAddress[16];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   179
   struct SAnaGatePort *pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   180
   unsigned int nBusnameLen;   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   181
   char bBusnameValid = 1;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   182
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   183
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   184
   ///////////////////////////////////////////
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   185
   // Do some checkings concerning the busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   186
   // format should be IP-Adress:Port
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   187
   // e.g. 192.168.1.254:A
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   188
   ///////////////////////////////////////////
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   189
   nBusnameLen = strlen(board->busname);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   190
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   191
   if ( nBusnameLen < strlen( "1.2.3.4:A" ) )         bBusnameValid = 0;  // check minimum length of busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   192
   if ( nBusnameLen > strlen( "123.234.345.456:A" ) ) bBusnameValid = 0;  // check maximum length of busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   193
   if ( bBusnameValid )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   194
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   195
      switch (board->busname[nBusnameLen-1])                                 // check Portname of busname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   196
      {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   197
         case ('A'): nPortNr = 0; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   198
         case ('B'): nPortNr = 1; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   199
         case ('C'): nPortNr = 2; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   200
         case ('D'): nPortNr = 3; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   201
         default :   bBusnameValid = 0; break;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   202
      }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   203
      if (board->busname[nBusnameLen-2] != ':' )    bBusnameValid = 0;   // check Colon before Portname
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   204
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   205
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   206
   if ( ! bBusnameValid )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   207
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   208
      fprintf(stderr, "canOpen_driver (AnaGate_Win32): busname (\"%s\") has a wrong format. Use IPAddr:CANPort for example \"192.168.1.254:A\"\n",board->busname);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   209
      return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   210
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   211
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   212
   board->busname[nBusnameLen-2] = 0; // NULL Terminator for IP Address string
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   213
   strcpy (sIPAddress, board->busname);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   214
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   215
   pNewAnaGatePort = (struct SAnaGatePort*) malloc(sizeof (struct SAnaGatePort));
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   216
   if (pFistAnaGatePort == NULL)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   217
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   218
      pFistAnaGatePort = pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   219
      pNewAnaGatePort->pNext =  pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   220
      pNewAnaGatePort->pPrev =  pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   221
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   222
   else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   223
   {   pNewAnaGatePort->pNext =  pFistAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   224
      pNewAnaGatePort->pPrev =  pFistAnaGatePort->pPrev;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   225
      pFistAnaGatePort->pPrev->pNext = pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   226
      pFistAnaGatePort->pPrev = pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   227
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   228
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   229
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   230
   // Connect to AnaGate
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   231
   if ( nRetCode = CANOpenDevice (&pNewAnaGatePort->hHandle, 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   232
                        0,      /*confimation off*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   233
                        1,      /*Monitor on*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   234
                        nPortNr,
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   235
                        sIPAddress,
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   236
                        1000   /*TimeOut*/ )  )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   237
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   238
      CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   239
      fprintf(stderr, "canOpen_driver (AnaGate_Linux): %s @ %s  Port:%d\n", cErrorMsg,sIPAddress,nPortNr);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   240
      //printf( "canOpen_driver (AnaGate_Linux): %s @ %s Port:%d\n", cErrorMsg,sIPAddress,nPortNr);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   241
      return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   242
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   243
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   244
   // Inizial Baudrate
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   245
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   246
   if (nRetCode = CANSetGlobals (pNewAnaGatePort->hHandle, 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   247
                        TranslateBaudeRate(board->baudrate), 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   248
                        0,/*OperatingMode = normal*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   249
                        0,/*CAN-Termination = off*/ 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   250
                        1 /*HighSpeedMode = on*/) ) 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   251
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   252
      CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   253
      fprintf(stderr, "canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   254
      //printf("canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   255
      return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   256
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   257
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   258
   // Creat receive and receive-acknoledge event 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   259
   /*pNewAnaGatePort->hAnaRecEvent = CreateEvent(   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   260
                                    NULL,  // default security attributes
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   261
                                    FALSE, // manual-reset event
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   262
                                    FALSE, // initial state is nonsignaled
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   263
                                    NULL  // object name
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   264
                                    ); 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   265
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   266
   pNewAnaGatePort->hFesticalRecAcknowledge = CreateEvent( 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   267
                                    NULL,  // default security attributes
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   268
                                    FALSE, // manual-reset event
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   269
                                    FALSE, // initial state is nonsignaled
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   270
                                    NULL   // object name
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   271
                                    ); 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   272
         */
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   273
 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   274
   pNewAnaGatePort->bBufferFull = 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   275
 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   276
   // Install receive callback funktion
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   277
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   278
   if (nRetCode = CANSetCallback(pNewAnaGatePort->hHandle,  AnaGateReceiveCallBack) ) 
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   279
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   280
      canClose_driver (pNewAnaGatePort);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   281
      CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   282
      fprintf(stderr, "canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   283
      //printf("canOpen_driver (AnaGate_Linux): %s @ %s\n", cErrorMsg,sIPAddress);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   284
      return (CAN_HANDLE) NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   285
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   286
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   287
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   288
  return (CAN_HANDLE)pNewAnaGatePort;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   289
}
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   290
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   291
/***************************************************************************/
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   292
int canClose_driver(CAN_HANDLE fd0)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   293
{
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   294
   struct SAnaGatePort*  pAnaGatePort = (struct SAnaGatePort*)fd0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   295
   char cErrorMsg[100];
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   296
   int nRetCode;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   297
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   298
   pAnaGatePort->bBufferFull = 1;  
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   299
   
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   300
   if ( nRetCode = CANCloseDevice(pAnaGatePort->hHandle) )
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   301
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   302
     CANErrorMessage( nRetCode, cErrorMsg ,100 ); // Convert returncode to error messge
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   303
     fprintf(stderr, "canClose_driver (AnaGate_Linux): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   304
     printf("canClose_driver (AnaGate_Linux): %s\n", cErrorMsg);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   305
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   306
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   307
   if (pAnaGatePort->pNext == pAnaGatePort)
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   308
   {
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   309
       free (pAnaGatePort);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   310
       pFistAnaGatePort=NULL;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   311
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   312
   else
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   313
   {  
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   314
      pAnaGatePort->pNext->pPrev = pAnaGatePort->pPrev;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   315
      pAnaGatePort->pPrev->pNext = pAnaGatePort->pNext;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   316
      free (pAnaGatePort);
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   317
   }
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   318
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   319
   return 0;
aa36efce6e78 add anagate device support in CanFestival
greg
parents:
diff changeset
   320
}