Stavo pensando di utilizzare Boost Asio per leggere i dati da un Socket CAN. Non c'è niente di interessante in linux/can.h, e il dispositivo dovrebbe comportarsi come l'interfaccia di loopback e essere utilizzato con un socket non elaborato.boost :: asio over SocketCAN
Guardando a livello di interfaccia basic_raw_socket
sembra che io possa fare uso di basic_raw_socket::assign da assegnare alla presa nativo creata con
socket(PF_CAN, SOCK_RAW, CAN_RAW);
Questo è quello che ho finora
namespace can {
class CanSocket {
public:
typedef boost::asio::ip::basic_endpoint<CanSocket> endpoint;
typedef boost::asio::ip::basic_resolver_query<CanSocket> resolver_query;
typedef boost::asio::ip::basic_resolver_iterator<CanSocket> resolver_iterator;
typedef boost::asio::basic_raw_socket<CanSocket> socket;
typedef boost::asio::ip::basic_resolver<CanSocket> resolver;
CanSocket()
: _protocol(CAN_RAW)
, _family(PF_CAN)
{
}
static CanSocket v4()
{
return CanSocket();
}
static CanSocket v6();
int type() const;
int protocol() const;
int family() const;
friend bool operator==(const CanSocket& p1, const CanSocket& p2)
{
return p1._protocol != p2._protocol || p1._family != p2._family;
}
friend bool operator!=(const CanSocket& p1, const CanSocket& p2)
{
return p1._protocol == p2._protocol || p1._family == p2._family;
}
private:
int _protocol;
int _family;
};
}
e questo è come lo uso nella mia applicazione
boost::asio::io_service ioserv;
CanSocket::socket s(ioserv);
int sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
s.assign(CanSocket::v4(), sock);
struct ifreq ifr;
strcpy(ifr.ifr_name, "vcan0");
ioctl(sock, SIOCGIFINDEX, &ifr); /* ifr.ifr_ifindex gets filled
* with that device's index */
/* Select that CAN interface, and bind the socket to it. */
/* this should be the endpoint */
struct sockaddr_can addr;
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
/* s.bind (....) */
bind(sock, (struct sockaddr*)&addr, sizeof(addr));
Ciò che non riesco a ottenere è esattamente come faccio a binds
per l'endpoint locale? Non ci sono IP o porte coinvolte.
C'è qualcos'altro che dovrebbe essere implementato oltre all'endpoint per farlo funzionare?
La prego di fornire l'esempio completo per la lettura/scrittura può telai, proprio come ho fatto per Python: http: //libbits.wordpress .com/2012/05/22/socketcan-support-in-python/Lo posizionerei su http://elinux.org/CAN_Bus#SocketCAN_Support_in_Programming_Languages.2FEnvironments – yegorich