You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
9.4 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * asc2log.c - convert ASC logfile to compact CAN frame 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 <sys/time.h>
  46. #define __USE_XOPEN /* supress warning for strptime */
  47. #include <time.h>
  48. #include <libgen.h>
  49. #include <unistd.h>
  50. #include <stdlib.h>
  51. #include <locale.h>
  52. #include <net/if.h>
  53. #include <linux/can.h>
  54. #include <linux/can/error.h>
  55. #include "lib.h"
  56. extern int optind, opterr, optopt;
  57. void print_usage(char *prg)
  58. {
  59. fprintf(stderr, "Usage: %s\n", prg);
  60. fprintf(stderr, "Options: -I <infile> (default stdin)\n");
  61. fprintf(stderr, " -O <outfile> (default stdout)\n");
  62. }
  63. void prframe(FILE *file, struct timeval *tv, int dev, struct can_frame *cf) {
  64. fprintf(file, "(%ld.%06ld) ", tv->tv_sec, tv->tv_usec);
  65. if (dev > 0)
  66. fprintf(file, "can%d ", dev-1);
  67. else
  68. fprintf(file, "canX ");
  69. /* no CAN FD support so far */
  70. fprint_canframe(file, (struct canfd_frame *)cf, "\n", 0, CAN_MAX_DLEN);
  71. }
  72. void get_can_id(struct can_frame *cf, char *idstring, int base) {
  73. if (idstring[strlen(idstring)-1] == 'x') {
  74. cf->can_id = CAN_EFF_FLAG;
  75. idstring[strlen(idstring)-1] = 0;
  76. } else
  77. cf->can_id = 0;
  78. cf->can_id |= strtoul(idstring, NULL, base);
  79. }
  80. void calc_tv(struct timeval *tv, struct timeval *read_tv,
  81. struct timeval *date_tv, char timestamps, int dplace) {
  82. if (dplace == 4) /* shift values having only 4 decimal places */
  83. read_tv->tv_usec *= 100; /* and need for 6 */
  84. if (dplace == 5) /* shift values having only 5 decimal places */
  85. read_tv->tv_usec *= 10; /* and need for 6 */
  86. if (timestamps == 'a') { /* absolute */
  87. tv->tv_sec = date_tv->tv_sec + read_tv->tv_sec;
  88. tv->tv_usec = date_tv->tv_usec + read_tv->tv_usec;
  89. } else { /* relative */
  90. if (((!tv->tv_sec) && (!tv->tv_usec)) &&
  91. (date_tv->tv_sec || date_tv->tv_usec)) {
  92. tv->tv_sec = date_tv->tv_sec; /* initial date/time */
  93. tv->tv_usec = date_tv->tv_usec;
  94. }
  95. tv->tv_sec += read_tv->tv_sec;
  96. tv->tv_usec += read_tv->tv_usec;
  97. }
  98. if (tv->tv_usec > 1000000) {
  99. tv->tv_usec -= 1000000;
  100. tv->tv_sec++;
  101. }
  102. }
  103. int get_date(struct timeval *tv, char *date) {
  104. char ctmp[10];
  105. int itmp;
  106. struct tm tms;
  107. if (sscanf(date, "%9s %d %9s %9s %d", ctmp, &itmp, ctmp, ctmp, &itmp) == 5) {
  108. /* assume EN/US date due to existing am/pm field */
  109. if (!setlocale(LC_TIME, "en_US")) {
  110. fprintf(stderr, "Setting locale to 'en_US' failed!\n");
  111. return 1;
  112. }
  113. if (!strptime(date, "%B %d %r %Y", &tms))
  114. return 1;
  115. } else {
  116. /* assume DE date due to non existing am/pm field */
  117. if (sscanf(date, "%9s %d %9s %d", ctmp, &itmp, ctmp, &itmp) != 4)
  118. return 1;
  119. if (!setlocale(LC_TIME, "de_DE")) {
  120. fprintf(stderr, "Setting locale to 'de_DE' failed!\n");
  121. return 1;
  122. }
  123. if (!strptime(date, "%B %d %T %Y", &tms))
  124. return 1;
  125. }
  126. //printf("h %d m %d s %d d %d m %d y %d\n",
  127. //tms.tm_hour, tms.tm_min, tms.tm_sec,
  128. //tms.tm_mday, tms.tm_mon+1, tms.tm_year+1900);
  129. tv->tv_sec = mktime(&tms);
  130. if (tv->tv_sec < 0)
  131. return 1;
  132. return 0;
  133. }
  134. int main(int argc, char **argv)
  135. {
  136. char buf[100], tmp1[100], tmp2[100];
  137. FILE *infile = stdin;
  138. FILE *outfile = stdout;
  139. static int verbose;
  140. struct can_frame cf;
  141. static struct timeval tv; /* current frame timestamp */
  142. static struct timeval read_tv; /* frame timestamp from ASC file */
  143. static struct timeval date_tv; /* date of the ASC file */
  144. static int dplace; /* decimal place 4, 5 or 6 or uninitialized */
  145. static char base; /* 'd'ec or 'h'ex */
  146. static char timestamps; /* 'a'bsolute or 'r'elative */
  147. int interface;
  148. char rtr;
  149. int dlc = 0;
  150. int data[8];
  151. int i, found, opt;
  152. while ((opt = getopt(argc, argv, "I:O:v?")) != -1) {
  153. switch (opt) {
  154. case 'I':
  155. infile = fopen(optarg, "r");
  156. if (!infile) {
  157. perror("infile");
  158. return 1;
  159. }
  160. break;
  161. case 'O':
  162. outfile = fopen(optarg, "w");
  163. if (!outfile) {
  164. perror("outfile");
  165. return 1;
  166. }
  167. break;
  168. case 'v':
  169. verbose = 1;
  170. break;
  171. case '?':
  172. print_usage(basename(argv[0]));
  173. return 0;
  174. break;
  175. default:
  176. fprintf(stderr, "Unknown option %c\n", opt);
  177. print_usage(basename(argv[0]));
  178. return 1;
  179. break;
  180. }
  181. }
  182. while (fgets(buf, 99, infile)) {
  183. if (!dplace) { /* the representation of a valid CAN frame not known */
  184. /* check for base and timestamp entries in the header */
  185. if ((!base) &&
  186. (sscanf(buf, "base %s timestamps %s", tmp1, tmp2) == 2)) {
  187. base = tmp1[0];
  188. timestamps = tmp2[0];
  189. if (verbose)
  190. printf("base %c timestamps %c\n", base, timestamps);
  191. if ((base != 'h') && (base != 'd')) {
  192. printf("invalid base %s (must be 'hex' or 'dez')!\n",
  193. tmp1);
  194. return 1;
  195. }
  196. if ((timestamps != 'a') && (timestamps != 'r')) {
  197. printf("invalid timestamps %s (must be 'absolute'"
  198. " or 'relative')!\n", tmp2);
  199. return 1;
  200. }
  201. continue;
  202. }
  203. /* check for the original logging date in the header */
  204. if ((!date_tv.tv_sec) &&
  205. (!strncmp(buf, "date", 4))) {
  206. if (get_date(&date_tv, &buf[9])) { /* skip 'date day ' */
  207. fprintf(stderr, "Not able to determine original log "
  208. "file date. Using current time.\n");
  209. /* use current date as default */
  210. gettimeofday(&date_tv, NULL);
  211. }
  212. if (verbose)
  213. printf("date %ld => %s", date_tv.tv_sec, ctime(&date_tv.tv_sec));
  214. continue;
  215. }
  216. /* check for decimal places length in valid CAN frames */
  217. if (sscanf(buf, "%ld.%s %d ", &tv.tv_sec, tmp2, &i) == 3){
  218. dplace = strlen(tmp2);
  219. if (verbose)
  220. printf("decimal place %d, e.g. '%s'\n", dplace, tmp2);
  221. if (dplace < 4 || dplace > 6) {
  222. printf("invalid dplace %d (must be 4, 5 or 6)!\n", dplace);
  223. return 1;
  224. }
  225. } else
  226. continue; /* dplace remains zero until first found CAN frame */
  227. }
  228. /* the representation of a valid CAN frame is known here */
  229. /* so try to get CAN frames and ErrorFrames and convert them */
  230. /* 0.002367 1 390x Rx d 8 17 00 14 00 C0 00 08 00 */
  231. found = 0; /* found valid CAN frame ? */
  232. if (base == 'h') { /* check for CAN frames with hexadecimal values */
  233. if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %x %x %x %x %x %x %x %x",
  234. &read_tv.tv_sec, &read_tv.tv_usec, &interface,
  235. tmp1, &rtr, &dlc,
  236. &data[0], &data[1], &data[2], &data[3],
  237. &data[4], &data[5], &data[6], &data[7]
  238. ) == dlc + 6 ) {
  239. found = 1;
  240. get_can_id(&cf, tmp1, 16);
  241. }
  242. } else { /* check for CAN frames with decimal values */
  243. if (sscanf(buf, "%ld.%ld %d %s %*s %c %d %d %d %d %d %d %d %d %d",
  244. &read_tv.tv_sec, &read_tv.tv_usec, &interface,
  245. tmp1, &rtr, &dlc,
  246. &data[0], &data[1], &data[2], &data[3],
  247. &data[4], &data[5], &data[6], &data[7]
  248. ) == dlc + 6 ) {
  249. found = 1;
  250. get_can_id(&cf, tmp1, 10);
  251. }
  252. }
  253. if (found) {
  254. if (rtr == 'r')
  255. cf.can_id |= CAN_RTR_FLAG;
  256. cf.can_dlc = dlc & 0x0FU;
  257. for (i=0; i<dlc; i++)
  258. cf.data[i] = data[i] & 0xFFU;
  259. calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
  260. prframe(outfile, &tv, interface, &cf);
  261. fflush(outfile);
  262. continue;
  263. }
  264. /* check for ErrorFrames */
  265. if (sscanf(buf, "%ld.%ld %d %s",
  266. &read_tv.tv_sec, &read_tv.tv_usec,
  267. &interface, tmp1) == 4) {
  268. if (!strncmp(tmp1, "ErrorFrame", strlen("ErrorFrame"))) {
  269. memset(&cf, 0, sizeof(cf));
  270. /* do not know more than 'Error' */
  271. cf.can_id = (CAN_ERR_FLAG | CAN_ERR_BUSERROR);
  272. cf.can_dlc = CAN_ERR_DLC;
  273. calc_tv(&tv, &read_tv, &date_tv, timestamps, dplace);
  274. prframe(outfile, &tv, interface, &cf);
  275. fflush(outfile);
  276. }
  277. }
  278. }
  279. fclose(outfile);
  280. fclose(infile);
  281. return 0;
  282. }