Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Subclass QTcpSocket or QIODevice for Hardware API



  • Hey,

    I'm not sure, what´s the best approach to solve my problem:
    I'm having an USB network interface device which driver won't show up as a network interface in the windows stack. But there is an API that allows to send and receive single packets, where i can set the payload and parts of the header (MAC address, ether type etc. (Layer 2 of ISO/OSI model) ). In my application i need a TCP based connection to a server using the extern USB network device.
    I guess i have to subclass a QTcpSocket or QAbstractSocket or directly a QIODevice class and overload the "sendlayer2packet" and "receivelayer2packet" methods. Is this the correct approach?

    Informations about my system: x86 Windows 7, QT 5.5.1


  • Moderators

    @MariusW
    who is establishing the TCP connection? Is this already done by the API you are using?
    If so you should subclass QIODevice and simply forward the read/write access.



  • Thanks for the fast reply. The API for the network device isn't aware of any networking protocols besides the layer 2 ethernet protocol. So i can set the MAC sender and receiver addresses and the ethertype. The API is also handling the preamble and the checksum calculation.

    So the TCP connection has to be established by a QT class.


  • Moderators

    @MariusW
    so you need to pass a socket descriptor?



  • Do i? Correct me if i'm wrong: The socket descriptor contains Information about the IP adress and port number, the protocol is listening on or connecting to. The socket descriptor is not defining/handling how the data is actually send?
    Because what i need is "get" the data of each packet the QT TCP stack wants to send and of course inject the received packets to the TCP stack.


  • Moderators

    @MariusW
    i was asking you!
    When your API doesn't establish a connection - as you said - you need to pass it somehow. Thats why i asked if it expects a socket. Becasue then you could use QTcpSocket.
    If there is some special handling needed you anyway need to fall back to QIODevice as a base class.

    Edit:
    Nevermind... i think i simply misunderstood you. I think you should use QIODevice as a base class.



  • Ok, so i create a subclass of QIODevice and overload the read and write data and the read ready signal etc. methods to use the API and the usb device.
    Now i have 2 questions:
    How do i tell the QTCPSocket class to use my version of QIODevice?
    What is the data my write function receives from the QTCPSocket? Is it an ethernet packet, starting with the receiver MAC address?

    I know this is somewhat driver/operating system specific programming :/


  • Moderators

    @MariusW said:

    How do i tell the QTCPSocket class to use my version of QIODevice?

    why you need to do that?!



  • I'm not used to subclass parts of a framework to change the way something deep between framework, operating system and hardware is handled.
    So if i have a subclass of QIODevice that uses my hardware API, how do i use that class in combination with a QTcpSocket class?


  • Moderators

    @MariusW said:

    So if i have a subclass of QIODevice that uses my hardware API, how do i use that class in combination with a QTcpSocket class?

    again...why?



  • Ok... i don't get it. What is the approach i could start with? You suggested i should use QIODevice as a base class, but i don't know how to use the created subclass...


  • Moderators

    @MariusW
    i re-read your first post.
    I understood it this way that you just want to send data over this device.
    But you actually want to alter the data before it's sent over the device?

    This is actually the responsibility of the official API of your device?!



  • Alright, i'll explain my problem a litte bit in more detail, maybe i'm missing some point and it's impossible :/

    The API for the device allows me to send raw ethernet packets over the extern device. The official windows driver is bad, so the device won't be registered as a networking device in windows. That means the normal QTcpSocket or similiar network functions can't use my device, because they don't know it is existent.

    At some point the QTcpSocket segments the data stream into packets and adds the TCP header. After this point i want to pick the packets and use my API to send them with the external device.

    Now i see the QTcpSocket inherits from QAbstraktSocket which inherits from QIODevice, wich i understand as the QIODevice communicates somehow with an windows network API.

    Is there an way to subclass and override some read/write or send/receive functions so that i can use the Qt TCP stack with my external hardware?


  • Qt Champions 2017

    @MariusW
    Hello,

    That means the normal QTcpSocket or similiar network functions can't use my device, because they don't know it is existent.

    Nope, it means the OS can't use your device, because you're trying to circumvent it.

    At some point the QTcpSocket segments the data stream into packets and adds the TCP header. After this point i want to pick the packets and use my API to send them with the external device.

    It most certainly does not. This is deep inside the OS's implementation of the TCP/IP stack and is ordinarily not even exposed through an API. You, as a programmer confined by the OS's API, are in practice oblivious of how the TCP packets are (un)wrapped around the lower OSI levels' layers. What I mean is that you're asking how to reverse engineer a part of the OS by deriving a high-level class that Qt provides ... you just can't do it.

    Is there an way to subclass and override some read/write or send/receive functions so that i can use the Qt TCP stack with my external hardware?

    Qt doesn't go as deep as the TCP stack implementation. Qt interfaces with the OS's API which manages the stack internally.

    Kind regards.



  • Hey,
    thank you for the detailed explanation. That means i have to find a TCP/IP stack which provides access to the lower levels or has an interface to the segmented packets...


  • Qt Champions 2017

    @MariusW said:

    That means i have to find a TCP/IP stack which provides access to the lower levels or has an interface to the segmented packets...

    Actually, I think @raven-worx has the correct idea. You should rather wrap around the API you have by subclassing the QIODevice class and implementing the abstract methods.

    Kind regards.


Log in to reply