123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QFileDialog>
- #include <QMessageBox>
- #include <iostream>
- #include <libgen.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <unistd.h>
- #include <linux/can.h>
- #include <net/if.h>
- #include <sys/time.h>
- extern "C"
- {
- #include "lib.h"
- }
- MainWindow::MainWindow(QWidget *parent)
- : QMainWindow(parent)
- , ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- setWindowTitle("Log To ASC");
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::on_pushButton_convert_clicked()
- {
- QString strinfilename = QFileDialog::getOpenFileName(this,"Open log File",".","*.log");
- if(strinfilename.isEmpty())
- {
- std::cout<<" not select input file."<<std::endl;
- return;
- }
- int indexsplash = strinfilename.lastIndexOf('/');
- QString lastPath = strinfilename.left(indexsplash);
- std::cout<<" input file name: "<<strinfilename.toStdString()<<std::endl;
- QString stroutfilename = QFileDialog::getSaveFileName(this,"Save asc File",lastPath,"*.asc");
- if(stroutfilename.isEmpty())
- {
- std::cout<<" not select output file."<<std::endl;
- return;
- }
- if(stroutfilename.right(4) != ".asc")
- {
- std::cout<<" append .asc"<<std::endl;
- stroutfilename.append(".asc");
- }
- std::cout<<" out file name: "<<stroutfilename.toStdString()<<std::endl;
- int nrtn = log2asc(strinfilename,stroutfilename);
- if(nrtn == 1){
- QMessageBox::information(this,"Success","Convert Successfully.",QMessageBox::YesAll);
- }
- }
- #define DEVSZ 22
- #define EXTRASZ 20
- #define TIMESZ sizeof("(1345212884.318850) ")
- #define BUFSZ (DEVSZ + AFRSZ + EXTRASZ + TIMESZ)
- /* adapt sscanf() functions below on error */
- #if (AFRSZ != 6300)
- #error "AFRSZ value does not fit sscanf restrictions!"
- #endif
- #if (DEVSZ != 22)
- #error "DEVSZ value does not fit sscanf restrictions!"
- #endif
- #if (EXTRASZ != 20)
- #error "EXTRASZ value does not fit sscanf restrictions!"
- #endif
- static void can_asc(struct canfd_frame *cfd, int devno, int nortrdlc,
- char *extra_info, FILE *outfile)
- {
- int i;
- char id[10];
- char *dir = "Rx";
- int dlc;
- struct can_frame *cf = (struct can_frame *)cfd; /* for len8_dlc */
- fprintf(outfile, "%-2d ", devno); /* channel number left aligned */
- if (cf->can_id & CAN_ERR_FLAG)
- fprintf(outfile, "ErrorFrame");
- else {
- sprintf(id, "%X%c", cf->can_id & CAN_EFF_MASK,
- (cf->can_id & CAN_EFF_FLAG)?'x':' ');
- /* check for extra info */
- if (strlen(extra_info) > 0) {
- /* only the first char is defined so far */
- if (extra_info[0] == 'T')
- dir = "Tx";
- }
- fprintf(outfile, "%-15s %s ", id, dir);
- if (cf->len == CAN_MAX_DLC &&
- cf->len8_dlc > CAN_MAX_DLC &&
- cf->len8_dlc <= CAN_MAX_RAW_DLC)
- dlc = cf->len8_dlc;
- else
- dlc = cf->len;
- if (cf->can_id & CAN_RTR_FLAG) {
- if (nortrdlc)
- fprintf(outfile, "r"); /* RTR frame */
- else
- fprintf(outfile, "r %X", dlc); /* RTR frame */
- } else {
- fprintf(outfile, "d %X", dlc); /* data frame */
- for (i = 0; i < cf->len; i++) {
- fprintf(outfile, " %02X", cf->data[i]);
- }
- }
- }
- }
- static void canfd_asc(struct canfd_frame *cf, int devno, int mtu,
- char *extra_info, FILE *outfile)
- {
- int i;
- char id[10];
- char *dir = "Rx";
- unsigned int flags = 0;
- unsigned int dlen = cf->len;
- unsigned int dlc = can_fd_len2dlc(dlen);
- /* relevant flags in Flags field */
- #define ASC_F_RTR 0x00000010
- #define ASC_F_FDF 0x00001000
- #define ASC_F_BRS 0x00002000
- #define ASC_F_ESI 0x00004000
- /* check for extra info */
- if (strlen(extra_info) > 0) {
- /* only the first char is defined so far */
- if (extra_info[0] == 'T')
- dir = "Tx";
- }
- fprintf(outfile, "CANFD %3d %s ", devno, dir); /* 3 column channel number right aligned */
- sprintf(id, "%X%c", cf->can_id & CAN_EFF_MASK,
- (cf->can_id & CAN_EFF_FLAG)?'x':' ');
- fprintf(outfile, "%11s ", id);
- fprintf(outfile, "%c ", (cf->flags & CANFD_BRS)?'1':'0');
- fprintf(outfile, "%c ", (cf->flags & CANFD_ESI)?'1':'0');
- /* check for extra DLC when having a Classic CAN with 8 bytes payload */
- if ((mtu == CAN_MTU) && (dlen == CAN_MAX_DLEN)) {
- struct can_frame *ccf = (struct can_frame *)cf;
- if ((ccf->len8_dlc > CAN_MAX_DLEN) && (ccf->len8_dlc <= CAN_MAX_RAW_DLC))
- dlc = ccf->len8_dlc;
- }
- fprintf(outfile, "%x ", dlc);
- if (mtu == CAN_MTU) {
- if (cf->can_id & CAN_RTR_FLAG) {
- /* no data length but dlc for RTR frames */
- dlen = 0;
- flags = ASC_F_RTR;
- }
- } else {
- flags = ASC_F_FDF;
- if (cf->flags & CANFD_BRS)
- flags |= ASC_F_BRS;
- if (cf->flags & CANFD_ESI)
- flags |= ASC_F_ESI;
- }
- fprintf(outfile, "%2d", dlen);
- for (i = 0; i < (int)dlen; i++) {
- fprintf(outfile, " %02X", cf->data[i]);
- }
- fprintf(outfile, " %8d %4d %8X 0 0 0 0 0", 130000, 130, flags);
- }
- int MainWindow::log2asc(QString strinfile, QString stroutfile)
- {
- static char buf[BUFSZ], device[DEVSZ], afrbuf[AFRSZ], extra_info[EXTRASZ];
- static cu_t cu;
- static struct timeval tv, start_tv;
- FILE *infile = stdin;
- FILE *outfile = stdout;
- static int maxdev, devno, i, crlf, fdfmt, nortrdlc, d4, opt, mtu;
- (void)maxdev; (void)i; (void)opt;
- int print_banner = 1;
- unsigned long long sec, usec;
- infile = fopen(strinfile.toStdString().data(), "r");
- outfile = fopen(stroutfile.toStdString().data(), "w");
- //printf("Found %d CAN devices!\n", maxdev);
- while (fgets(buf, BUFSZ-1, infile)) {
- if (strlen(buf) >= BUFSZ-2) {
- fprintf(stderr, "line too long for input buffer\n");
- return 1;
- }
- /* check for a comment line */
- if (buf[0] != '(')
- continue;
- if (sscanf(buf, "(%llu.%llu) %21s %6299s %19s", &sec, &usec,
- device, afrbuf, extra_info) != 5) {
- /* do not evaluate the extra info */
- extra_info[0] = 0;
- if (sscanf(buf, "(%llu.%llu) %21s %6299s", &sec, &usec,
- device, afrbuf) != 4) {
- fprintf(stderr, "incorrect line format in logfile\n");
- return 1;
- }
- }
- tv.tv_sec = sec;
- tv.tv_usec = usec;
- if (print_banner) { /* print banner */
- print_banner = 0;
- start_tv = tv;
- fprintf(outfile, "date %s", ctime(&start_tv.tv_sec));
- fprintf(outfile, "base hex timestamps absolute%s",
- (crlf)?"\r\n":"\n");
- fprintf(outfile, "no internal events logged%s",
- (crlf)?"\r\n":"\n");
- }
- // for (i = 0, devno = 0; i < maxdev; i++) {
- // if (!strcmp(device, argv[optind+i])) {
- // devno = i + 1; /* start with channel '1' */
- // break;
- // }
- // }
- devno = 1;
- if (devno) { /* only convert for selected CAN devices */
- mtu = parse_canframe(afrbuf, &cu);
- /* convert only CAN CC and CAN FD frames */
- if ((mtu != CAN_MTU) && (mtu != CANFD_MTU)) {
- printf("no valid CAN CC/FD frame\n");
- return 1;
- }
- /* we don't support error message frames in CAN FD */
- if ((mtu == CANFD_MTU) && (cu.cc.can_id & CAN_ERR_FLAG))
- continue;
- tv.tv_sec = tv.tv_sec - start_tv.tv_sec;
- tv.tv_usec = tv.tv_usec - start_tv.tv_usec;
- if (tv.tv_usec < 0)
- tv.tv_sec--, tv.tv_usec += 1000000;
- if (tv.tv_sec < 0)
- tv.tv_sec = tv.tv_usec = 0;
- if (d4)
- fprintf(outfile, "%4llu.%04llu ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec/100);
- else
- fprintf(outfile, "%4llu.%06llu ", (unsigned long long)tv.tv_sec, (unsigned long long)tv.tv_usec);
- if ((mtu == CAN_MTU) && (fdfmt == 0))
- can_asc(&cu.fd, devno, nortrdlc, extra_info, outfile);
- else
- canfd_asc(&cu.fd, devno, mtu, extra_info, outfile);
- if (crlf)
- fprintf(outfile, "\r");
- fprintf(outfile, "\n");
- }
- }
- fflush(outfile);
- fclose(outfile);
- fclose(infile);
- return 1;
- }
|