Netlink socket
-
Hi,
I'm currently doing some experiments with a netlink socket to read a 1-wire iButton key. This works well when using native socket functions. However, it would be nice to have signal/slot functionality. Therefore, I would like to 'wrap' my native socket in a Qt object. Because it is a netlink socket, I think I have to use QAbstractSocket for that. First, I open a native socket and do a bind. When successful, I instantiate a QAbstractSocket :
@ s = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);if (s == -1)
{
qDebug ("Socket problem");
return;
}l_local.nl_family = AF_NETLINK;
l_local.nl_groups = 23;
l_local.nl_pid = getpid();if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1)
{
qDebug ("Bind problem");
::close(s);
}QAbstractSocket *as = new QAbstractSocket(QAbstractSocket::UnknownSocketType, this);
if (!as->setSocketDescriptor(s, QAbstractSocket::ConnectedState))
{
qDebug ("Failed to assign native socket to QAbstractSocket");
}connect (as, SIGNAL(readyRead()), this, SLOT(axisDataAvailable()));@
When I apply the key, the slot is never called. I also tested to read data from the QAbstractSocket, but with no success.
Do I miss something ? Can I do it like this or are there better approaches to do this ?Filip
-
Problem solved.
With QTcpSocket instead of QAbstractSocket, it looks like it's working fine. -
As I'm very busy on my job (you know...deadlines...), I just post a snippet of my code :
@
int m_sock;
struct sockaddr_nl l_local;
QTcpSocket m_mySocket;
/ open netlink socket */
m_sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);if (m_sock == -1) { qDebug ("Socket problem"); return; } l_local.nl_family = AF_NETLINK; l_local.nl_groups = 23; l_local.nl_pid = getpid(); if (bind(m_sock, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) { qDebug ("Bind problem"); close(m_sock); m_sock = 0; return; } m_mySocket = new QTcpSocket(); if (!m_mySocket->setSocketDescriptor(m_sock)) { qDebug ("Failed to assign native socket to QTcpSocket"); close(m_sock); m_sock = 0; return; } connect (m_mySocket, SIGNAL(readyRead()), this, SLOT(netlinkDataAvailable()));@
Hope this will help you...
(Implemented this already some time ago, unfortunately not remembering all the details anymore right now.)