]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/lib.c
cfc7669cb43918a786a753962197c2c79e3976f7
[socketcan-devel.git] / can-utils / lib.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * lib.c - library for command line tools
7  *
8  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <string.h>
50
51 #include <sys/socket.h> /* for sa_family_t */
52 #include <linux/can.h>
53
54 #include "lib.h"
55
56 #define CANID_DELIM '#'
57 #define DATA_SEPERATOR '.'
58
59 #define MAX_CANFRAME      "12345678#01.23.45.67.89.AB.CD.EF"
60 #define MAX_LONG_CANFRAME "12345678  [8] 10101010 10101010 10101010 10101010 10101010 10101010 10101010 10101010   '........'"
61
62 static int asc2nibble(char c) {
63
64         if ((c >= '0') && (c <= '9'))
65                 return c - '0';
66
67         if ((c >= 'A') && (c <= 'F'))
68                 return c - 'A' + 10;
69
70         if ((c >= 'a') && (c <= 'f'))
71                 return c - 'a' + 10;
72
73         return 16; /* error */
74 }
75
76 int parse_canframe(char *cs, struct can_frame *cf) {
77         /* documentation see lib.h */
78
79         int i, idx, dlc, len, tmp;
80
81         len = strlen(cs);
82         //printf("'%s' len %d\n", cs, len);
83
84         memset(cf, 0, sizeof(*cf)); /* init CAN frame, e.g. DLC = 0 */
85
86         if (len < 4)
87                 return 1;
88
89         if (!((cs[3] == CANID_DELIM) || (cs[8] == CANID_DELIM)))
90                 return 1;
91
92         if (cs[8] == CANID_DELIM) { /* 8 digits */
93
94                 idx = 9;
95                 for (i=0; i<8; i++){
96                         if ((tmp = asc2nibble(cs[i])) > 0x0F)
97                                 return 1;
98                         cf->can_id |= (tmp << (7-i)*4);
99                 }
100                 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe?  */
101                         cf->can_id |= CAN_EFF_FLAG;   /* then it is an extended frame */
102
103         } else { /* 3 digits */
104
105                 idx = 4;
106                 for (i=0; i<3; i++){
107                         if ((tmp = asc2nibble(cs[i])) > 0x0F)
108                                 return 1;
109                         cf->can_id |= (tmp << (2-i)*4);
110                 }
111         }
112
113         if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
114                 cf->can_id |= CAN_RTR_FLAG;
115                 return 0;
116         }
117
118         for (i=0, dlc=0; i<8; i++){
119
120                 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
121                         idx++;
122
123                 if(idx >= len) /* end of string => end of data */
124                         break;
125
126                 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
127                         return 1;
128                 cf->data[i] = (tmp << 4);
129                 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
130                         return 1;
131                 cf->data[i] |= tmp;
132                 dlc++;
133         }
134
135         cf->can_dlc = dlc;
136
137         return 0;
138 }
139
140 void fprint_canframe(FILE *stream , struct can_frame *cf, char *eol, int sep) {
141         /* documentation see lib.h */
142
143         char buf[sizeof(MAX_CANFRAME)+1]; /* max length */
144
145         sprint_canframe(buf, cf, sep);
146         fprintf(stream, "%s", buf);
147         if (eol)
148                 fprintf(stream, "%s", eol);
149 }
150
151 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
152         /* documentation see lib.h */
153
154         int i,offset;
155
156         if (cf->can_id & CAN_ERR_FLAG) {
157                 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
158                 offset = 9;
159         } else if (cf->can_id & CAN_EFF_FLAG) {
160                 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
161                 offset = 9;
162         } else {
163                 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
164                 offset = 4;
165         }
166
167         if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
168                 sprintf(buf+offset, "R");
169         else
170                 for (i = 0; i < cf->can_dlc; i++) {
171                         sprintf(buf+offset, "%02X", cf->data[i]);
172                         offset += 2;
173                         if (sep && (i+1 < cf->can_dlc))
174                                 sprintf(buf+offset++, ".");
175                 }
176
177
178 }
179
180 void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int view) {
181         /* documentation see lib.h */
182
183         char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */
184
185         sprint_long_canframe(buf, cf, view);
186         fprintf(stream, "%s", buf);
187         if (eol)
188                 fprintf(stream, "%s", eol);
189 }
190
191 void sprint_long_canframe(char *buf , struct can_frame *cf, int view) {
192         /* documentation see lib.h */
193
194         int i, j, dlen, offset;
195
196         if (cf->can_id & CAN_ERR_FLAG) {
197                 sprintf(buf, "%8X  ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
198                 offset = 10;
199         } else if (cf->can_id & CAN_EFF_FLAG) {
200                 sprintf(buf, "%8X  ", cf->can_id & CAN_EFF_MASK);
201                 offset = 10;
202         } else {
203                 sprintf(buf, "%3X  ", cf->can_id & CAN_SFF_MASK);
204                 offset = 5;
205         }
206
207         sprintf(buf+offset, "[%d]", cf->can_dlc);
208         offset += 3;
209
210         if (cf->can_id & CAN_RTR_FLAG) { /* there are no ERR frames with RTR */
211                 sprintf(buf+offset, " remote request");
212                 return;
213         }
214
215         if (view & CANLIB_VIEW_BINARY) {
216                 dlen = 9; /* _10101010 */
217                 for (i = 0; i < cf->can_dlc; i++) {
218                         buf[offset++] = ' ';
219                         for (j = 7; j >= 0; j--)
220                                 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
221                 }
222                 buf[offset] = 0; /* terminate string */
223         } else {
224                 dlen = 3; /* _AA */
225                 for (i = 0; i < cf->can_dlc; i++) {
226                         sprintf(buf+offset, " %02X", cf->data[i]);
227                         offset += dlen;
228                 }
229         }
230
231         if (cf->can_id & CAN_ERR_FLAG)
232                 sprintf(buf+offset, "%*s", dlen*(8-cf->can_dlc)+13, "ERRORFRAME");
233         else if (view & CANLIB_VIEW_ASCII) {
234                 j = dlen*(8-cf->can_dlc)+4;
235                 sprintf(buf+offset, "%*s", j, "'");
236                 offset += j;
237
238                 for (i = 0; i < cf->can_dlc; i++)
239                         if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
240                                 buf[offset++] = cf->data[i];
241                         else
242                                 buf[offset++] = '.';
243
244                 sprintf(buf+offset, "'");
245         } 
246 }
247