]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - test/tst-packet.c
The two options "CAN bit-timing calculation" and
[socketcan-devel.git] / test / tst-packet.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * tst-packet.c - send and receive CAN frames via PF_PACKET sockets
7  *
8  * Remark: The sending of CAN frames via PF_PACKET does not work for
9  * virtual CAN devices (vcan) as the packet type is not set to
10  * PACKET_LOOPBACK by the packet socket, so you'll never get something
11  * back (btw the outgoing vcan packet counters are updated correctly).
12  *
13  * Copyright (c) 2002-2009 Volkswagen Group Electronic Research
14  * All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of Volkswagen nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * Alternatively, provided that this notice is retained in full, this
29  * software may be distributed under the terms of the GNU General
30  * Public License ("GPL") version 2, in which case the provisions of the
31  * GPL apply INSTEAD OF those given above.
32  *
33  * The provided data structures and external interfaces from this code
34  * are not restricted to be used by modules with a GPL compatible license.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
47  * DAMAGE.
48  *
49  * Send feedback to <socketcan-users@lists.berlios.de>
50  *
51  */
52
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <unistd.h>
56 #include <string.h>
57
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <sys/ioctl.h>
61 #include <net/if.h>
62 #include <netinet/in.h>         /* for htons() */
63
64 #include <linux/if_packet.h>
65 #include <linux/if_ether.h>     /* for ETH_P_CAN */
66 #include <linux/can.h>          /* for struct can_frame */
67
68 int main(int argc, char **argv)
69 {
70         int s;
71         struct can_frame frame;
72         int nbytes, i;
73         static struct ifreq ifr;
74         static struct sockaddr_ll sll;
75         char *ifname = "vcan2";
76         int ifindex;
77         int opt;
78         int send_one_frame = 0;
79
80         while ((opt = getopt(argc, argv, "i:s")) != -1) {
81                 switch (opt) {
82
83                 case 'i':
84                         ifname = optarg;
85                         break;
86
87                 case 's':
88                         send_one_frame = 1;
89                         break;
90
91                 default:
92                         fprintf(stderr, "Unknown option %c\n", opt);
93                         break;
94                 }
95         }
96
97         s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_CAN));
98         if (s < 0) {
99                 perror("socket");
100                 return 1;
101         }
102
103         if (strcmp(ifname, "any") == 0)
104                 ifindex = 0;
105         else {
106                 strcpy(ifr.ifr_name, ifname);
107                 ioctl(s, SIOCGIFINDEX, &ifr);
108                 ifindex = ifr.ifr_ifindex;
109         }
110
111         sll.sll_family   = AF_PACKET;
112         sll.sll_ifindex  = ifindex;
113         sll.sll_protocol = htons(ETH_P_CAN);
114
115         if (bind(s, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
116                 perror("bind");
117                 return 1;
118         }
119
120         if(send_one_frame) {
121
122                 frame.can_id  = 0x123;
123                 frame.can_dlc = 2;
124                 frame.data[0] = 0x11;
125                 frame.data[1] = 0x22;
126
127                 nbytes = write(s, &frame, sizeof(struct can_frame));
128                 if (nbytes < 0) {
129                         perror("write");
130                         return 1;
131                 }
132                 if (nbytes != sizeof(struct can_frame)) {
133                         perror("write_len");
134                         return 1;
135                 }
136         }
137
138         while (1) {
139
140                 if ((nbytes = read(s, &frame, sizeof(struct can_frame))) < 0) {
141                         perror("read");
142                         return 1;
143                 } else if (nbytes < sizeof(struct can_frame)) {
144                         fprintf(stderr, "read: incomplete CAN frame\n");
145                         return 1;
146                 } else {
147                         if (frame.can_id & CAN_EFF_FLAG)
148                                 printf("%8X  ", frame.can_id & CAN_EFF_MASK);
149                         else
150                                 printf("%3X  ", frame.can_id & CAN_SFF_MASK);
151             
152                         printf("[%d] ", frame.can_dlc);
153             
154                         for (i = 0; i < frame.can_dlc; i++) {
155                                 printf("%02X ", frame.data[i]);
156                         }
157                         if (frame.can_id & CAN_RTR_FLAG)
158                                 printf("remote request");
159                         printf("\n");
160                         fflush(stdout);
161                 }
162         }
163
164         close(s);
165
166         return 0;
167 }