#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
-#include <net/if.h>
+//#include <net/if.h>
+#include <linux/if.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
+#include <netlink/netlink.h>
+#include <netlink/cache.h>
+#include <netlink/route/link.h>
+#include <netlink/socket.h>
+
#include <linux/can.h>
#include <linux/can/bcm.h>
tv->tv_usec = (ms % 1000) * 1000;
}
+int sllin_interface_up(struct linc_lin_state *linc_lin_state,
+ struct sllin_connection *sllin_connection)
+{
+ struct nl_sock *s;
+ struct rtnl_link *request;
+ struct nl_cache *cache;
+ struct rtnl_link *link;
+ int ret;
+
+ // Allocate and initialize a new netlink socket
+ s = nl_socket_alloc();
+
+ // Bind and connect the socket to a protocol, NETLINK_ROUTE in this example.
+ nl_connect(s, NETLINK_ROUTE);
+
+ // The first step is to retrieve a list of all available interfaces within
+ // the kernel and put them into a cache.
+ ret = rtnl_link_alloc_cache(s, AF_UNSPEC, &cache);
+ // FIXME errorhandling
+
+ // In a second step, a specific link may be looked up by either interface
+ // index or interface name.
+ link = rtnl_link_get_by_name(cache, sllin_connection->iface);
+
+ // In order to change any attributes of an existing link, we must allocate
+ // a new link to hold the change requests:
+ request = rtnl_link_alloc();
+
+ // We can also shut an interface down administratively
+ //rtnl_link_unset_flags(request, rtnl_link_str2flags("up"));
+ rtnl_link_set_flags(request, rtnl_link_str2flags("up"));
+
+ // Two ways exist to commit this change request, the first one is to
+ // build the required netlink message and send it out in one single
+ // step:
+ rtnl_link_change(s, link, request, 0);
+
+ // An alternative way is to build the netlink message and send it
+ // out yourself using nl_send_auto_complete()
+ // struct nl_msg *msg = rtnl_link_build_change_request(old, request);
+ // nl_send_auto_complete(nl_handle, nlmsg_hdr(msg));
+ // nlmsg_free(msg);
+
+ // After successful usage, the object must be given back to the cache
+ rtnl_link_put(link);
+ nl_socket_free(s);
+
+ return 0;
+}
+
int sllin_cache_config(struct linc_lin_state *linc_lin_state,
struct sllin_connection *sllin_connection)
{
}
for (i = 0; i < 0x3F; i++) {
+ frame.can_dlc = linc_lin_state->frame_entry[i].data_len;
+ frame.can_id = i; /* LIN ID */
+ frame.data[0] = linc_lin_state->frame_entry[i].data[0]; /* Data */
+ frame.data[1] = linc_lin_state->frame_entry[i].data[1]; /* Data */
+ frame.data[2] = linc_lin_state->frame_entry[i].data[2]; /* Data */
+ frame.data[3] = linc_lin_state->frame_entry[i].data[3]; /* Data */
+ frame.data[4] = linc_lin_state->frame_entry[i].data[4]; /* Data */
+ frame.data[5] = linc_lin_state->frame_entry[i].data[5]; /* Data */
+ frame.data[6] = linc_lin_state->frame_entry[i].data[6]; /* Data */
+ frame.data[7] = linc_lin_state->frame_entry[i].data[7]; /* Data */
+ frame.can_id |= LIN_CTRL_FRAME;
if (linc_lin_state->frame_entry[i].status == 1) { /* Is active */
- frame.can_dlc = linc_lin_state->frame_entry[i].data_len;
- frame.can_id = i; /* LIN ID */
- frame.data[0] = linc_lin_state->frame_entry[i].data[0]; /* Data */
- frame.data[1] = linc_lin_state->frame_entry[i].data[1]; /* Data */
- frame.data[2] = linc_lin_state->frame_entry[i].data[2]; /* Data */
- frame.data[3] = linc_lin_state->frame_entry[i].data[3]; /* Data */
- frame.data[4] = linc_lin_state->frame_entry[i].data[4]; /* Data */
- frame.data[5] = linc_lin_state->frame_entry[i].data[5]; /* Data */
- frame.data[6] = linc_lin_state->frame_entry[i].data[6]; /* Data */
- frame.data[7] = linc_lin_state->frame_entry[i].data[7]; /* Data */
-
- frame.can_id |= LIN_CTRL_FRAME | LIN_CACHE_RESPONSE;
- ret = write(s, &frame, sizeof(frame));
- printf("configuring frame cache; ret = %d\n", ret);
- //if (ret ...)
- //read_response(tty);
+ frame.can_id |= LIN_CACHE_RESPONSE;
}
+ ret = write(s, &frame, sizeof(frame));
+ printf("configuring frame cache; ret = %d\n", ret);
+ //if (ret ...)
+ //read_response(tty);
}
close(s);
if (ret < 0)
return ret;
+ ret = sllin_interface_up(linc_lin_state, &sllin_connection);
+ if (ret < 0)
+ return ret;
+
ret = sllin_cache_config(linc_lin_state, &sllin_connection);
/* !!! Do not close "tty" to enable newly