log2asc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * log2asc.c - convert compact CAN frame logfile to ASC logfile
  3. *
  4. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Volkswagen nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * Alternatively, provided that this notice is retained in full, this
  20. * software may be distributed under the terms of the GNU General
  21. * Public License ("GPL") version 2, in which case the provisions of the
  22. * GPL apply INSTEAD OF those given above.
  23. *
  24. * The provided data structures and external interfaces from this code
  25. * are not restricted to be used by modules with a GPL compatible license.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38. * DAMAGE.
  39. *
  40. * Send feedback to <linux-can@vger.kernel.org>
  41. *
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <time.h>
  46. #include <libgen.h>
  47. #include <unistd.h>
  48. #include <net/if.h>
  49. #include <sys/time.h>
  50. #include <linux/can.h>
  51. #include "lib.h"
  52. #define BUFSZ 400 /* for one line in the logfile */
  53. extern int optind, opterr, optopt;
  54. void print_usage(char *prg)
  55. {
  56. fprintf(stderr, "Usage: %s [can-interfaces]\n", prg);
  57. fprintf(stderr, "Options: -I <infile> (default stdin)\n");
  58. fprintf(stderr, " -O <outfile> (default stdout)\n");
  59. fprintf(stderr, " -4 (reduce decimal place to 4 digits)\n");
  60. fprintf(stderr, " -n (set newline to cr/lf - default lf)\n");
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. static char buf[BUFSZ], device[BUFSZ], ascframe[BUFSZ], id[10];
  65. struct canfd_frame cf;
  66. static struct timeval tv, start_tv;
  67. FILE *infile = stdin;
  68. FILE *outfile = stdout;
  69. static int maxdev, devno, i, crlf, d4, opt;
  70. while ((opt = getopt(argc, argv, "I:O:4n?")) != -1) {
  71. switch (opt) {
  72. case 'I':
  73. infile = fopen(optarg, "r");
  74. if (!infile) {
  75. perror("infile");
  76. return 1;
  77. }
  78. break;
  79. case 'O':
  80. outfile = fopen(optarg, "w");
  81. if (!outfile) {
  82. perror("outfile");
  83. return 1;
  84. }
  85. break;
  86. case 'n':
  87. crlf = 1;
  88. break;
  89. case '4':
  90. d4 = 1;
  91. break;
  92. case '?':
  93. print_usage(basename(argv[0]));
  94. return 0;
  95. break;
  96. default:
  97. fprintf(stderr, "Unknown option %c\n", opt);
  98. print_usage(basename(argv[0]));
  99. return 1;
  100. break;
  101. }
  102. }
  103. maxdev = argc - optind; /* find real number of CAN devices */
  104. if (!maxdev) {
  105. fprintf(stderr, "no CAN interfaces defined!\n");
  106. print_usage(basename(argv[0]));
  107. return 1;
  108. }
  109. //printf("Found %d CAN devices!\n", maxdev);
  110. while (fgets(buf, BUFSZ-1, infile)) {
  111. if (strlen(buf) >= BUFSZ-2) {
  112. fprintf(stderr, "line too long for input buffer\n");
  113. return 1;
  114. }
  115. /* check for a comment line */
  116. if (buf[0] != '(')
  117. continue;
  118. if (sscanf(buf, "(%ld.%ld) %s %s", &tv.tv_sec, &tv.tv_usec,
  119. device, ascframe) != 4) {
  120. fprintf(stderr, "incorrect line format in logfile\n");
  121. return 1;
  122. }
  123. if (!start_tv.tv_sec) { /* print banner */
  124. start_tv = tv;
  125. fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
  126. fprintf(outfile, "base hex timestamps absolute%s",
  127. (crlf)?"\r\n":"\n");
  128. fprintf(outfile, "no internal events logged%s",
  129. (crlf)?"\r\n":"\n");
  130. }
  131. for (i=0, devno=0; i<maxdev; i++) {
  132. if (!strcmp(device, argv[optind+i])) {
  133. devno = i+1; /* start with channel '1' */
  134. break;
  135. }
  136. }
  137. if (devno) { /* only convert for selected CAN devices */
  138. if (parse_canframe(ascframe, &cf) != CAN_MTU) /* no CAN FD support so far */
  139. return 1;
  140. tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
  141. tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
  142. if (tv.tv_usec < 0)
  143. tv.tv_sec--, tv.tv_usec += 1000000;
  144. if (tv.tv_sec < 0)
  145. tv.tv_sec = tv.tv_usec = 0;
  146. if (d4)
  147. fprintf(outfile, "%4ld.%04ld ", tv.tv_sec, tv.tv_usec/100);
  148. else
  149. fprintf(outfile, "%4ld.%06ld ", tv.tv_sec, tv.tv_usec);
  150. fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
  151. if (cf.can_id & CAN_ERR_FLAG)
  152. fprintf(outfile, "ErrorFrame");
  153. else {
  154. sprintf(id, "%X%c", cf.can_id & CAN_EFF_MASK,
  155. (cf.can_id & CAN_EFF_FLAG)?'x':' ');
  156. fprintf(outfile, "%-15s Rx ", id);
  157. if (cf.can_id & CAN_RTR_FLAG)
  158. fprintf(outfile, "r"); /* RTR frame */
  159. else {
  160. fprintf(outfile, "d %d", cf.len); /* data frame */
  161. for (i = 0; i < cf.len; i++) {
  162. fprintf(outfile, " %02X", cf.data[i]);
  163. }
  164. }
  165. }
  166. if (crlf)
  167. fprintf(outfile, "\r");
  168. fprintf(outfile, "\n");
  169. }
  170. }
  171. fflush(outfile);
  172. fclose(outfile);
  173. fclose(infile);
  174. return 0;
  175. }