mainwindow.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <iostream>
  6. #include <libgen.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <unistd.h>
  11. #include <linux/can.h>
  12. #include <net/if.h>
  13. #include <sys/time.h>
  14. extern "C"
  15. {
  16. #include "lib.h"
  17. }
  18. MainWindow::MainWindow(QWidget *parent)
  19. : QMainWindow(parent)
  20. , ui(new Ui::MainWindow)
  21. {
  22. ui->setupUi(this);
  23. setWindowTitle("Log To ASC");
  24. }
  25. MainWindow::~MainWindow()
  26. {
  27. delete ui;
  28. }
  29. void MainWindow::on_pushButton_convert_clicked()
  30. {
  31. QString strinfilename = QFileDialog::getOpenFileName(this,"Open log File",".","*.log");
  32. if(strinfilename.isEmpty())
  33. {
  34. std::cout<<" not select input file."<<std::endl;
  35. return;
  36. }
  37. int indexsplash = strinfilename.lastIndexOf('/');
  38. QString lastPath = strinfilename.left(indexsplash);
  39. std::cout<<" input file name: "<<strinfilename.toStdString()<<std::endl;
  40. QString stroutfilename = QFileDialog::getSaveFileName(this,"Save asc File",lastPath,"*.asc");
  41. if(stroutfilename.isEmpty())
  42. {
  43. std::cout<<" not select output file."<<std::endl;
  44. return;
  45. }
  46. if(stroutfilename.right(4) != ".asc")
  47. {
  48. std::cout<<" append .asc"<<std::endl;
  49. stroutfilename.append(".asc");
  50. }
  51. std::cout<<" out file name: "<<stroutfilename.toStdString()<<std::endl;
  52. int nrtn = log2asc(strinfilename,stroutfilename);
  53. if(nrtn == 1){
  54. QMessageBox::information(this,"Success","Convert Successfully.",QMessageBox::YesAll);
  55. }
  56. }
  57. #define DEVSZ 22
  58. #define EXTRASZ 20
  59. #define TIMESZ sizeof("(1345212884.318850) ")
  60. #define BUFSZ (DEVSZ + AFRSZ + EXTRASZ + TIMESZ)
  61. /* adapt sscanf() functions below on error */
  62. #if (AFRSZ != 6300)
  63. #error "AFRSZ value does not fit sscanf restrictions!"
  64. #endif
  65. #if (DEVSZ != 22)
  66. #error "DEVSZ value does not fit sscanf restrictions!"
  67. #endif
  68. #if (EXTRASZ != 20)
  69. #error "EXTRASZ value does not fit sscanf restrictions!"
  70. #endif
  71. static void can_asc(struct canfd_frame *cfd, int devno, int nortrdlc,
  72. char *extra_info, FILE *outfile)
  73. {
  74. int i;
  75. char id[10];
  76. char *dir = "Rx";
  77. int dlc;
  78. struct can_frame *cf = (struct can_frame *)cfd; /* for len8_dlc */
  79. fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
  80. if (cf->can_id & CAN_ERR_FLAG)
  81. fprintf(outfile, "ErrorFrame");
  82. else {
  83. sprintf(id, "%X%c", cf->can_id & CAN_EFF_MASK,
  84. (cf->can_id & CAN_EFF_FLAG)?'x':' ');
  85. /* check for extra info */
  86. if (strlen(extra_info) > 0) {
  87. /* only the first char is defined so far */
  88. if (extra_info[0] == 'T')
  89. dir = "Tx";
  90. }
  91. fprintf(outfile, "%-15s %s ", id, dir);
  92. if (cf->len == CAN_MAX_DLC &&
  93. cf->len8_dlc > CAN_MAX_DLC &&
  94. cf->len8_dlc <= CAN_MAX_RAW_DLC)
  95. dlc = cf->len8_dlc;
  96. else
  97. dlc = cf->len;
  98. if (cf->can_id & CAN_RTR_FLAG) {
  99. if (nortrdlc)
  100. fprintf(outfile, "r"); /* RTR frame */
  101. else
  102. fprintf(outfile, "r %X", dlc); /* RTR frame */
  103. } else {
  104. fprintf(outfile, "d %X", dlc); /* data frame */
  105. for (i = 0; i < cf->len; i++) {
  106. fprintf(outfile, " %02X", cf->data[i]);
  107. }
  108. }
  109. }
  110. }
  111. static void canfd_asc(struct canfd_frame *cf, int devno, int mtu,
  112. char *extra_info, FILE *outfile)
  113. {
  114. int i;
  115. char id[10];
  116. char *dir = "Rx";
  117. unsigned int flags = 0;
  118. unsigned int dlen = cf->len;
  119. unsigned int dlc = can_fd_len2dlc(dlen);
  120. /* relevant flags in Flags field */
  121. #define ASC_F_RTR 0x00000010
  122. #define ASC_F_FDF 0x00001000
  123. #define ASC_F_BRS 0x00002000
  124. #define ASC_F_ESI 0x00004000
  125. /* check for extra info */
  126. if (strlen(extra_info) > 0) {
  127. /* only the first char is defined so far */
  128. if (extra_info[0] == 'T')
  129. dir = "Tx";
  130. }
  131. fprintf(outfile, "CANFD %3d %s ", devno, dir); /* 3 column channel number right aligned */
  132. sprintf(id, "%X%c", cf->can_id & CAN_EFF_MASK,
  133. (cf->can_id & CAN_EFF_FLAG)?'x':' ');
  134. fprintf(outfile, "%11s ", id);
  135. fprintf(outfile, "%c ", (cf->flags & CANFD_BRS)?'1':'0');
  136. fprintf(outfile, "%c ", (cf->flags & CANFD_ESI)?'1':'0');
  137. /* check for extra DLC when having a Classic CAN with 8 bytes payload */
  138. if ((mtu == CAN_MTU) && (dlen == CAN_MAX_DLEN)) {
  139. struct can_frame *ccf = (struct can_frame *)cf;
  140. if ((ccf->len8_dlc > CAN_MAX_DLEN) && (ccf->len8_dlc <= CAN_MAX_RAW_DLC))
  141. dlc = ccf->len8_dlc;
  142. }
  143. fprintf(outfile, "%x ", dlc);
  144. if (mtu == CAN_MTU) {
  145. if (cf->can_id & CAN_RTR_FLAG) {
  146. /* no data length but dlc for RTR frames */
  147. dlen = 0;
  148. flags = ASC_F_RTR;
  149. }
  150. } else {
  151. flags = ASC_F_FDF;
  152. if (cf->flags & CANFD_BRS)
  153. flags |= ASC_F_BRS;
  154. if (cf->flags & CANFD_ESI)
  155. flags |= ASC_F_ESI;
  156. }
  157. fprintf(outfile, "%2d", dlen);
  158. for (i = 0; i < (int)dlen; i++) {
  159. fprintf(outfile, " %02X", cf->data[i]);
  160. }
  161. fprintf(outfile, " %8d %4d %8X 0 0 0 0 0", 130000, 130, flags);
  162. }
  163. int MainWindow::log2asc(QString strinfile, QString stroutfile)
  164. {
  165. static char buf[BUFSZ], device[DEVSZ], afrbuf[AFRSZ], extra_info[EXTRASZ];
  166. static cu_t cu;
  167. static struct timeval tv, start_tv;
  168. FILE *infile = stdin;
  169. FILE *outfile = stdout;
  170. static int maxdev, devno, i, crlf, fdfmt, nortrdlc, d4, opt, mtu;
  171. (void)maxdev; (void)i; (void)opt;
  172. int print_banner = 1;
  173. unsigned long long sec, usec;
  174. infile = fopen(strinfile.toStdString().data(), "r");
  175. outfile = fopen(stroutfile.toStdString().data(), "w");
  176. //printf("Found %d CAN devices!\n", maxdev);
  177. while (fgets(buf, BUFSZ-1, infile)) {
  178. if (strlen(buf) >= BUFSZ-2) {
  179. fprintf(stderr, "line too long for input buffer\n");
  180. return 1;
  181. }
  182. /* check for a comment line */
  183. if (buf[0] != '(')
  184. continue;
  185. if (sscanf(buf, "(%llu.%llu) %21s %6299s %19s", &sec, &usec,
  186. device, afrbuf, extra_info) != 5) {
  187. /* do not evaluate the extra info */
  188. extra_info[0] = 0;
  189. if (sscanf(buf, "(%llu.%llu) %21s %6299s", &sec, &usec,
  190. device, afrbuf) != 4) {
  191. fprintf(stderr, "incorrect line format in logfile\n");
  192. return 1;
  193. }
  194. }
  195. tv.tv_sec = sec;
  196. tv.tv_usec = usec;
  197. if (print_banner) { /* print banner */
  198. print_banner = 0;
  199. start_tv = tv;
  200. fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
  201. fprintf(outfile, "base hex timestamps absolute%s",
  202. (crlf)?"\r\n":"\n");
  203. fprintf(outfile, "no internal events logged%s",
  204. (crlf)?"\r\n":"\n");
  205. }
  206. // for (i = 0, devno = 0; i < maxdev; i++) {
  207. // if (!strcmp(device, argv[optind+i])) {
  208. // devno = i + 1; /* start with channel '1' */
  209. // break;
  210. // }
  211. // }
  212. devno = 1;
  213. if (devno) { /* only convert for selected CAN devices */
  214. mtu = parse_canframe(afrbuf, &cu);
  215. /* convert only CAN CC and CAN FD frames */
  216. if ((mtu != CAN_MTU) && (mtu != CANFD_MTU)) {
  217. printf("no valid CAN CC/FD frame\n");
  218. return 1;
  219. }
  220. /* we don't support error message frames in CAN FD */
  221. if ((mtu == CANFD_MTU) && (cu.cc.can_id & CAN_ERR_FLAG))
  222. continue;
  223. tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
  224. tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
  225. if (tv.tv_usec < 0)
  226. tv.tv_sec--, tv.tv_usec += 1000000;
  227. if (tv.tv_sec < 0)
  228. tv.tv_sec = tv.tv_usec = 0;
  229. if (d4)
  230. fprintf(outfile, "%4llu.%04llu ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec/100);
  231. else
  232. fprintf(outfile, "%4llu.%06llu ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec);
  233. if ((mtu == CAN_MTU) && (fdfmt == 0))
  234. can_asc(&cu.fd, devno, nortrdlc, extra_info, outfile);
  235. else
  236. canfd_asc(&cu.fd, devno, mtu, extra_info, outfile);
  237. if (crlf)
  238. fprintf(outfile, "\r");
  239. fprintf(outfile, "\n");
  240. }
  241. }
  242. fflush(outfile);
  243. fclose(outfile);
  244. fclose(infile);
  245. return 1;
  246. }