6 * lib.c - library for command line tools
8 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, the following disclaimer and
16 * the referenced file 'COPYING'.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of Volkswagen nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * Alternatively, provided that this notice is retained in full, this
25 * software may be distributed under the terms of the GNU General
26 * Public License ("GPL") version 2 as distributed in the 'COPYING'
27 * file from the main directory of the linux kernel source.
29 * The provided data structures and external interfaces from this code
30 * are not restricted to be used by modules with a GPL compatible license.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
45 * Send feedback to <socketcan-users@lists.berlios.de>
52 #include <sys/socket.h> /* for sa_family_t */
53 #include <linux/can.h>
57 #define CANID_DELIM '#'
58 #define DATA_SEPERATOR '.'
60 #define MAX_CANFRAME "12345678#01.23.45.67.89.AB.CD.EF"
61 #define MAX_LONG_CANFRAME "12345678 [8] 01 23 45 67 89 AB CD EF '........'"
63 static int asc2nibble(char c) {
65 if ((c >= '0') && (c <= '9'))
68 if ((c >= 'A') && (c <= 'F'))
71 if ((c >= 'a') && (c <= 'f'))
74 return 16; /* error */
77 int parse_canframe(char *cs, struct can_frame *cf) {
78 /* documentation see lib.h */
80 int i, idx, dlc, len, tmp;
83 //printf("'%s' len %d\n", cs, len);
85 memset(cf, 0, sizeof(*cf)); /* init CAN frame, e.g. DLC = 0 */
90 if (!((cs[3] == CANID_DELIM) || (cs[8] == CANID_DELIM)))
93 if (cs[8] == CANID_DELIM) { /* 8 digits */
97 if ((tmp = asc2nibble(cs[i])) > 0x0F)
99 cf->can_id |= (tmp << (7-i)*4);
101 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe? */
102 cf->can_id |= CAN_EFF_FLAG; /* then it is an extended frame */
104 } else { /* 3 digits */
108 if ((tmp = asc2nibble(cs[i])) > 0x0F)
110 cf->can_id |= (tmp << (2-i)*4);
114 if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
115 cf->can_id |= CAN_RTR_FLAG;
119 for (i=0, dlc=0; i<8; i++){
121 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
124 if(idx >= len) /* end of string => end of data */
127 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
129 cf->data[i] = (tmp << 4);
130 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
141 void fprint_canframe(FILE *stream , struct can_frame *cf, char *eol, int sep) {
142 /* documentation see lib.h */
144 char buf[sizeof(MAX_CANFRAME)+1]; /* max length */
146 sprint_canframe(buf, cf, sep);
147 fprintf(stream, "%s", buf);
149 fprintf(stream, "%s", eol);
152 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
153 /* documentation see lib.h */
157 if (cf->can_id & CAN_ERR_FLAG) {
158 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
160 } else if (cf->can_id & CAN_EFF_FLAG) {
161 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
164 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
168 if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
169 sprintf(buf+offset, "R");
171 for (i = 0; i < cf->can_dlc; i++) {
172 sprintf(buf+offset, "%02X", cf->data[i]);
174 if (sep && (i+1 < cf->can_dlc))
175 sprintf(buf+offset++, ".");
181 void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int ascii) {
182 /* documentation see lib.h */
184 char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */
186 sprint_long_canframe(buf, cf, ascii);
187 fprintf(stream, "%s", buf);
189 fprintf(stream, "%s", eol);
192 void sprint_long_canframe(char *buf , struct can_frame *cf, int ascii) {
193 /* documentation see lib.h */
197 if (cf->can_id & CAN_ERR_FLAG) {
198 sprintf(buf, "%8X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
200 } else if (cf->can_id & CAN_EFF_FLAG) {
201 sprintf(buf, "%8X ", cf->can_id & CAN_EFF_MASK);
204 sprintf(buf, "%3X ", cf->can_id & CAN_SFF_MASK);
208 sprintf(buf+offset, "[%d]", cf->can_dlc);
211 if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
212 sprintf(buf+offset, " remote request");
214 for (i = 0; i < cf->can_dlc; i++) {
215 sprintf(buf+offset, " %02X", cf->data[i]);
218 if (cf->can_id & CAN_ERR_FLAG)
219 sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+13, "ERRORFRAME");
221 sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+4, "'");
222 offset += 3*(8-cf->can_dlc)+4;
224 for (i = 0; i < cf->can_dlc; i++)
225 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
226 buf[offset++] = cf->data[i];
229 sprintf(buf+offset, "'");