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 void sllin_ms_to_timeval(int ms, struct timeval *tv)
33 tv->tv_sec = (int) ms/1000;
34 tv->tv_usec = (ms % 1000) * 1000;
37 int sllin_cache_config(struct linc_lin_state *linc_lin_state,
38 struct sllin_connection *sllin_connection)
44 int sllin_bcm_config(struct linc_lin_state *linc_lin_state,
45 struct sllin_connection *sllin_connection)
47 struct sockaddr_can caddr;
54 s = socket(PF_CAN, SOCK_DGRAM, CAN_BCM);
56 perror("socket(): bcmsocket");
60 strcpy(ifr.ifr_name, sllin_connection->iface);
61 ioctl(s, SIOCGIFINDEX, &ifr);
63 memset(&caddr, 0, sizeof(caddr));
64 caddr.can_family = AF_CAN;
65 caddr.can_ifindex = ifr.ifr_ifindex;
67 // ret = bind(s, (struct sockaddr*)&caddr, sizeof(caddr));
73 // sllin_connection->bcm_sock = s;
75 ret = connect(s, (struct sockaddr *)&caddr, sizeof(caddr));
81 for (i = 0; i < linc_lin_state->scheduler_entries_cnt; i++) {
83 memset(&msg, 0, sizeof(msg));
85 msg.msg_head.nframes = 1;
86 msg.msg_head.opcode = TX_SETUP;
87 msg.msg_head.flags |= SETTIMER | STARTTIMER;
89 linc_lin_state->scheduler_entry[i].interval_ms, &time);
90 msg.msg_head.ival2.tv_sec = time.tv_sec;
91 msg.msg_head.ival2.tv_usec = time.tv_usec;
92 msg.msg_head.can_id = (
93 linc_lin_state->scheduler_entry[i].lin_id | CAN_RTR_FLAG);
94 msg.frame.can_dlc = 0;
95 msg.frame.can_id = msg.msg_head.can_id;
97 //printf("tv_sec = %i, tv_usec = %i\n", time.tv_sec, time.tv_usec);
99 sendto(s, &msg, sizeof(msg), 0,
100 (struct sockaddr*)&caddr, sizeof(caddr));
101 //read_response(s); // FIXME
104 /* Do not close "s" to make BCM configuration running */
106 printf("Configuration finished\n");
110 int sllin_config(struct linc_lin_state *linc_lin_state)
113 int ldisc = SLLIN_LDISC;
115 struct sllin_connection sllin_connection;
117 tty = open(linc_lin_state->dev, O_WRONLY | O_NOCTTY);
123 /* Set sllin line discipline on given tty */
124 if (linc_lin_state->flags & SLLIN_ATTACH_fl) {
125 ret = ioctl(tty, TIOCSETD, &ldisc);
127 perror("ioctl TIOCSETD");
131 /* Retrieve the name of the created CAN netdevice */
132 ret = ioctl(tty, SIOCGIFNAME, sllin_connection.iface);
134 perror("ioctl SIOCGIFNAME");
138 printf("Attached tty %s to netdevice %s\n",
139 linc_lin_state->dev, sllin_connection.iface);
142 if (linc_lin_state->flags & SLLIN_DETACH_fl) {
144 ret = ioctl(tty, TIOCSETD, &ldisc);
150 printf("Detached sllin line discipline from %s\n",
151 linc_lin_state->dev);
157 ret = sllin_bcm_config(linc_lin_state, &sllin_connection);
161 ret = sllin_cache_config(linc_lin_state, &sllin_connection);
163 /* !!! Do not close "tty" to enable newly
164 configured tty line discipline */