2 #include <sys/socket.h>
6 #include <netinet/in.h>
14 #include <linux/can.h>
15 #include <linux/can/bcm.h>
17 #include "lin_config.h"
19 #define SLLIN_LDISC 25
21 struct bcm_msg_head msg_head;
22 struct can_frame frame;
25 struct sllin_connection {
26 int bcm_sock; // FIXME is necessary??
28 char iface[IFNAMSIZ+1];
31 int sllin_bcm_config(struct linc_lin_state *linc_lin_state,
32 struct sllin_connection *sllin_connection)
34 struct sockaddr_can caddr;
41 //printf("tty %s to netdevice %s\n", linc_lin_state->dev, sllin_connection->iface);
43 s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
45 perror("socket(): bcmsocket");
49 strcpy(ifr.ifr_name, sllin_connection->iface);
50 ioctl(s, SIOCGIFINDEX, &ifr);
52 memset(&caddr, 0, sizeof(caddr));
53 caddr.can_family = AF_CAN;
54 caddr.can_ifindex = ifr.ifr_ifindex;
56 // ret = bind(s, (struct sockaddr*)&caddr, sizeof(caddr));
62 // sllin_connection->bcm_sock = s;
64 ret = connect(s, (struct sockaddr *)&caddr, sizeof(caddr));
70 for (i = 0; i < linc_lin_state->scheduler_entries_cnt; i++) {
71 memset(&msg, 0, sizeof(msg));
72 msg.msg_head.nframes = 1;
73 msg.msg_head.opcode = TX_SETUP;
74 msg.msg_head.flags |= SETTIMER | STARTTIMER;
75 //msg.msg_head.ival2.tv_sec = // FIXME
76 msg.msg_head.ival2.tv_usec =
77 linc_lin_state->scheduler_entry[i].interval_ms * 1000;
79 linc_lin_state->scheduler_entry[i].lin_id | CAN_RTR_FLAG;
80 msg.frame.can_dlc = 0;
82 sendto(s, &msg, sizeof(msg), 0,
83 (struct sockaddr*)&caddr, sizeof(caddr));
85 //read_response(s); // FIXME
90 printf("Configuration finished\n");
94 int sllin_config(struct linc_lin_state *linc_lin_state)
97 int ldisc = SLLIN_LDISC;
99 struct sllin_connection sllin_connection;
101 tty = open(linc_lin_state->dev, O_WRONLY | O_NOCTTY);
107 /* Set sllin line discipline on given tty */
108 if (linc_lin_state->flags & SLLIN_ATTACH_fl) {
109 ret = ioctl(tty, TIOCSETD, &ldisc);
111 perror("ioctl TIOCSETD");
115 /* Retrieve the name of the created CAN netdevice */
116 ret = ioctl(tty, SIOCGIFNAME, sllin_connection.iface);
118 perror("ioctl SIOCGIFNAME");
122 printf("Attached tty %s to netdevice %s\n", linc_lin_state->dev, sllin_connection.iface);
125 if (linc_lin_state->flags & SLLIN_DETACH_fl) {
127 ret = ioctl(tty, TIOCSETD, &ldisc);
133 printf("Detached sllin line discipline from %s\n", linc_lin_state->dev);
137 ret = sllin_bcm_config(linc_lin_state, &sllin_connection);
139 printf("Press any key to detach %s ...\n", linc_lin_state->dev);