6 * lib.c - library for command line tools
8 * Copyright (c) 2002-2007 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 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.
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.
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.
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
44 * Send feedback to <socketcan-users@lists.berlios.de>
51 #include <sys/socket.h> /* for sa_family_t */
52 #include <linux/can.h>
56 #define CANID_DELIM '#'
57 #define DATA_SEPERATOR '.'
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 '........'"
62 unsigned char asc2nibble(char c) {
64 if ((c >= '0') && (c <= '9'))
67 if ((c >= 'A') && (c <= 'F'))
70 if ((c >= 'a') && (c <= 'f'))
73 return 16; /* error */
76 int hexstring2candata(char *arg, struct can_frame *cf) {
78 int len = strlen(arg);
82 if (!len || len%2 || len > 16)
85 for (i=0; i < len/2; i++) {
87 tmp = asc2nibble(*(arg+(2*i)));
91 cf->data[i] = (tmp << 4);
93 tmp = asc2nibble(*(arg+(2*i)+1));
103 int parse_canframe(char *cs, struct can_frame *cf) {
104 /* documentation see lib.h */
106 int i, idx, dlc, len;
110 //printf("'%s' len %d\n", cs, len);
112 memset(cf, 0, sizeof(*cf)); /* init CAN frame, e.g. DLC = 0 */
117 if (!((cs[3] == CANID_DELIM) || (cs[8] == CANID_DELIM)))
120 if (cs[8] == CANID_DELIM) { /* 8 digits */
124 if ((tmp = asc2nibble(cs[i])) > 0x0F)
126 cf->can_id |= (tmp << (7-i)*4);
128 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe? */
129 cf->can_id |= CAN_EFF_FLAG; /* then it is an extended frame */
131 } else { /* 3 digits */
135 if ((tmp = asc2nibble(cs[i])) > 0x0F)
137 cf->can_id |= (tmp << (2-i)*4);
141 if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
142 cf->can_id |= CAN_RTR_FLAG;
146 for (i=0, dlc=0; i<8; i++){
148 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
151 if(idx >= len) /* end of string => end of data */
154 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
156 cf->data[i] = (tmp << 4);
157 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
168 void fprint_canframe(FILE *stream , struct can_frame *cf, char *eol, int sep) {
169 /* documentation see lib.h */
171 char buf[sizeof(MAX_CANFRAME)+1]; /* max length */
173 sprint_canframe(buf, cf, sep);
174 fprintf(stream, "%s", buf);
176 fprintf(stream, "%s", eol);
179 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
180 /* documentation see lib.h */
183 int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
185 if (cf->can_id & CAN_ERR_FLAG) {
186 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
188 } else if (cf->can_id & CAN_EFF_FLAG) {
189 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
192 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
196 if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
197 sprintf(buf+offset, "R");
199 for (i = 0; i < dlc; i++) {
200 sprintf(buf+offset, "%02X", cf->data[i]);
202 if (sep && (i+1 < dlc))
203 sprintf(buf+offset++, ".");
209 void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int view) {
210 /* documentation see lib.h */
212 char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */
214 sprint_long_canframe(buf, cf, view);
215 fprintf(stream, "%s", buf);
217 fprintf(stream, "%s", eol);
220 void sprint_long_canframe(char *buf , struct can_frame *cf, int view) {
221 /* documentation see lib.h */
223 int i, j, dlen, offset;
224 int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
226 if (cf->can_id & CAN_ERR_FLAG) {
227 sprintf(buf, "%8X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
229 } else if (cf->can_id & CAN_EFF_FLAG) {
230 sprintf(buf, "%8X ", cf->can_id & CAN_EFF_MASK);
233 sprintf(buf, "%3X ", cf->can_id & CAN_SFF_MASK);
237 sprintf(buf+offset, "[%d]", dlc);
240 if (cf->can_id & CAN_RTR_FLAG) { /* there are no ERR frames with RTR */
241 sprintf(buf+offset, " remote request");
245 if (view & CANLIB_VIEW_BINARY) {
246 dlen = 9; /* _10101010 */
247 for (i = 0; i < dlc; i++) {
249 for (j = 7; j >= 0; j--)
250 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
252 buf[offset] = 0; /* terminate string */
255 for (i = 0; i < dlc; i++) {
256 sprintf(buf+offset, " %02X", cf->data[i]);
261 if (cf->can_id & CAN_ERR_FLAG)
262 sprintf(buf+offset, "%*s", dlen*(8-dlc)+13, "ERRORFRAME");
263 else if (view & CANLIB_VIEW_ASCII) {
265 sprintf(buf+offset, "%*s", j, "'");
268 for (i = 0; i < dlc; i++)
269 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
270 buf[offset++] = cf->data[i];
274 sprintf(buf+offset, "'");