source: pacpussensors/trunk/CanGateway/driver/igepCanDriver.cpp@ 64

Last change on this file since 64 was 4, checked in by guyardro, 11 years ago

add igep can driver for CanGateway and add support for igep compilation

File size: 5.9 KB
RevLine 
[4]1/********************************************************************
2// created: 2011/02/21 - 10:46
3// filename: igepCanDriver.cpp
4//
5// author: Sergio Rodriguez
6//
7// version: $Id: igepCanDriver.cpp srodrigu $
8//
9// purpose: Implementation of the igepCanDriver class
10//
11*********************************************************************/
12
13#include <iostream>
14
15#include "igepCanDriver.h"
16#include <stdio.h>
17#include <cstring>
18#include <cstdlib>
19#include <iostream>
20#include <unistd.h>
21#include <string.h>
22
23#include <net/if.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <sys/ioctl.h>
27
28#include <linux/can.h>
29#include <linux/can/raw.h>
30#define DEFAULT_NODE "/dev/pcanusb0"
31#define WAIT_RECEIVING_FRAME_TIMEOUT 1000
32
33/**
34* Constructor which enables to initialize the different attributes of the class with default values.
35*/
36igepCanDriver::igepCanDriver (void)
37{
38 printf("Notice : IGEP CAN Driver used\n");
39 //szDevNode_ = DEFAULT_NODE;
40 mode_ = ReadOnly;
41}
42
43/**
44* Constructor which enables to initialize the different attributes of the class with default values.
45*/
46igepCanDriver::igepCanDriver (int channel)
47{
48 printf("Notice : IGEP CAN Driver used\n");
49 //szDevNode_ = DEFAULT_NODE;
50 mode_ = ReadOnly;
51}
52
53/**
54* Constructor which enables to initialize the different attributes of the class with default values.
55*/
56igepCanDriver::igepCanDriver (int channel, unsigned int bitRate)
57{
58 printf("Notice : IGEP CAN Driver used\n");
59 //szDevNode_ = DEFAULT_NODE;
60 mode_ = ReadOnly;
61}
62
63/**
64* Constructor which enables to initialize the different attributes of the class with default values.
65*/
66igepCanDriver::igepCanDriver(const char* port, const char* mode)
67{
68 szDevNode_ =(char*) malloc(14*sizeof(char));
69 strcpy(szDevNode_,DEFAULT_NODE);
70
71 printf("Notice : IGEP CAN Driver used\n");
72 //strcpy(szDevNode_, port);
73// printf("Driver to be connected at port: %s",szDevNode_);
74
75}
76
77/**
78* Destructor which clean up the different attributs of the class.
79*/
80igepCanDriver::~igepCanDriver (void)
81{
82
83}
84
85/**
86* Member used to initialise the configuration of the CAN Card.
87* @see cleanUpPort (void)
88* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
89*/
90short igepCanDriver::initPort (void)
91{
92 int nbytes;
93 struct sockaddr_can addr;
94 struct ifreq ifr;
95
96 char ifname[] = "can0";
97
98 if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
99 {
100 perror("Error while opening socket");
101 return 1;
102 }
103
104 strcpy(ifr.ifr_name, ifname);
105 ioctl(s, SIOCGIFINDEX, &ifr);
106
107 addr.can_family = AF_CAN;
108 addr.can_ifindex = ifr.ifr_ifindex;
109
110
111 if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
112 {
113 perror("Error in socket bind");
114 return 1;
115 }
116
117 return PSUCCESS;
118}
119
120/**
121* Member used to clean up the configuration of the CAN Card.
122* @see initPort (void)
123* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
124*/
125short igepCanDriver::cleanUpPort (void)
126{
127 if(canDeviceHandle_)
128 {
129 printf("Closing CAN Driver..");
130 close(s);
131 // CAN_Close(canDeviceHandle_);
132 return PSUCCESS;
133 }
134 printf("CAN Driver finished.\n");
135 return 1;
136}
137
138
139/**
140* Member which permit to send a frame on the CAN bus and test if the frame is well acknowledged.
141* @param flags a character which contain the flags of the sent frame.
142* @param dlc a character which defined the number of characters of the sent frame.
143* @param data a table of characters with the data of the sent frame.
144* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
145* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
146*/
147short igepCanDriver::sendFrame (struct CanFrame frame)
148{
149 can_frame message;
150 message.can_dlc = frame.dlc;
151 message.can_id = frame.id;
152 memcpy(message.data,frame.data, frame.dlc);
153
154 write(s, &message, sizeof(struct can_frame));
155 return PSUCCESS;
156}
157
158
159/**
160* Member which permit to receive of a frame on the CAN bus.
161* @param flags a character pointer which contain the flags of the received frame.
162* @param dlc a character pointer which defined the number of characters of the received frame.
163* @param data a pointer of table of characters with the data of the received frame.
164* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
165* @return a Pstatus variable which contain the error code of the function. On success, it return PSUCCESS. On failure, it return Pstatus error code.
166*/
167short igepCanDriver::receiveFrame (struct CanFrame &frame)
168{
169 can_frame message;
170
171 if ((read(s, &message, sizeof(struct can_frame))) < 0)
172 {
173 perror("application: CAN_Read()");
174 std::cout << "ERREUR" << std::endl;
175std::flush(std::cout);
176 return 1;
177 }
178 else
179 {
180// std::cout << "id : " << message.can_id << " dlc : " << message.can_dlc << " message : " << message.data << std::endl;
181std::flush(std::cout);
182 frame.dlc = message.can_dlc; //ok
183 frame.id = message.can_id;
184 if( ( frame.dlc > 8 ) || ( frame.dlc < 0 ))
185 frame.dlc = 8;
186 memcpy(frame.data,message.data, frame.dlc);
187 return PSUCCESS;
188
189 }
190
191
192}
193
194
195/**
196* Member which wait the reception of a frame on the CAN bus.
197* @see sendFrame (unsigned char flags, unsigned char dlc, unsigned char * data)
198* @see receiveFrame (unsigned char * flags, unsigned char * dlc, unsigned char ** data)
199*/
200void igepCanDriver::waitReceivingFrame(void)
201{
202
203}
204// print out the contents of a CAN message
205void igepCanDriver::print_message(TPCANMsg *m)
206{
207 int i;
208
209 // print RTR, 11 or 29, CAN-Id and datalength
210 printf("message: %c %c 0x%08x %1d ",
211 (m->MSGTYPE & MSGTYPE_RTR) ? 'r' : 'm',
212 (m->MSGTYPE & MSGTYPE_EXTENDED) ? 'e' : 's',
213 m->ID,
214 m->LEN);
215
216 // don't print any telegram contents for remote frames
217 if (!(m->MSGTYPE & MSGTYPE_RTR))
218 for (i = 0; i < m->LEN; i++)
219 printf("0x%02x ", m->DATA[i]);
220
221 printf("\n");
222}
Note: See TracBrowser for help on using the repository browser.