asc2log.c 9.4 KB

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