]> rtime.felk.cvut.cz Git - linux-lin.git/blob - misc/tty_lin_slave/main.c
Userspace LIN is compiled with USE_TERMIOS2 by default now.
[linux-lin.git] / misc / tty_lin_slave / main.c
1 /*
2  * PCAN-LIN, RS-232 to CAN/LIN converter control application
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <termios.h>
11 #include <stdint.h>
12
13 #define LIN_HDR_SIZE            2
14 #define LIN_PKT_MAX_SIZE        16 /* FIXME */
15 struct termios term_attr;
16 /* ------------------------------------------------------------------------ */
17
18 int read_header(int tty)
19 {
20         int p0, p1; /* Parity bits */
21         int par_rec; /* Parity received as a part of a packet */
22         int par_calc; /* Calculated parity */
23         int received = 0;
24         uint8_t buff[LIN_HDR_SIZE];
25         memset(buff, '\0', sizeof(buff));
26
27         while (1) {
28                 received = read(tty, &buff[0], 1);
29                 if (received == -1)
30                         perror("read()");
31                 
32                 if (buff[0] != 0x55) /* Sync byte field */
33                         continue;
34
35                 received = read(tty, &buff[1], 1);
36                 if (received == -1)
37                         perror("read()");
38                 else
39                         break;
40         }
41
42         p0 = (buff[1] ^ (buff[1] >> 1) ^ (buff[1] >> 2) ^ (buff[1] >> 4)) & 0x1;
43         p1 = ~(((buff[1] >> 1) ^ (buff[1] >> 3) ^ (buff[1] >> 4) ^ (buff[1] >> 5))) & 0x1;
44
45         printf("%02X ", buff[0]);
46         printf("%02X ", buff[1]);
47
48         par_rec = (buff[1] & 0xc0) >> 6;
49         par_calc = p0 | (p1 << 1);
50         printf("| LIN id: %02X ", buff[1] & 0x3f);
51         //printf("| par_rec: %X; par_calc: %X ", par_rec, par_calc);
52         if (par_rec == par_calc)
53                 printf("| parity OK");
54         
55         printf("\n");
56
57         return 0;
58 }
59
60 static void reset_input_mode(int tty)
61 {
62         tcsetattr(tty, TCSANOW, &term_attr);
63 }
64
65 static void set_input_mode(int tty)
66 {
67         int status;
68         struct termios tattr;
69
70         /* Flush input and output queues. */
71         if (tcflush(tty, TCIOFLUSH) != 0) {
72                 perror("tcflush");
73                 exit(EXIT_FAILURE);
74         }
75
76         /* Fetch the current terminal parameters. */
77         if(!isatty(tty)) {
78                 fprintf(stderr, "Not a terminal.\n");
79                 exit(EXIT_FAILURE);
80         }
81
82         /* Save settings for later restoring */
83         tcgetattr(tty, &term_attr);
84
85         /* RAW mode */
86         tcgetattr(tty, &tattr);
87         tattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
88                                 | INLCR | IGNCR | ICRNL | IXON);
89         tattr.c_oflag &= ~OPOST;
90         tattr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
91         tattr.c_cflag &= ~(CSIZE | PARENB);
92         tattr.c_cflag |= CS8;
93
94         tattr.c_cc[VMIN] = 1;
95         tattr.c_cc[VTIME] = 0;
96
97         /* Set TX, RX speed */
98         cfsetispeed(&tattr, B9600);
99         cfsetospeed(&tattr, B9600);
100
101         status = tcsetattr(tty, TCSANOW, &tattr);
102         if (status == -1)
103                 perror("tcsetattr()");
104
105 }
106
107 int main(int argc, char* argv[])
108 {
109         char dev[32];
110         int tty;
111
112         if (argc < 2) {
113                 fprintf(stderr, "Device is missing\n");
114                 fprintf(stderr, "Usage: %s DEVICE\n", argv[0]);
115                 return -3;
116         }
117
118         strncpy((char*)&dev, argv[1], 32);
119         tty = open(dev, O_RDWR);
120         if (tty < 0) {
121                 perror("open()");
122                 return -4;
123         }
124
125         /* Configure UART */
126         set_input_mode(tty);
127
128         while(1) {
129                 read_header(tty);
130         }       
131
132         reset_input_mode(tty);
133         close(tty);
134
135         return EXIT_SUCCESS;
136 }