Approach for implementing Device and Protocol layers in C++?


Loading ....
 

Software https://softwareengineering.stackexchange.com/questions/388469/approach-for-implementing-device-and-protocol-layers-in-c

I am writing an app that will interface with an external apparatus. It will support a lot of devices that will use different communication interfaces like USB, sequential, etc..

This is what I have so far:

An abstract Apparatus course with a few generic methods and a few pure virtual methods:

Class Device

private:
    bool initialised = false;

people:
    bool isInitialised() const 
        return this->initialised;
    

    emptiness setInitialised(bool initialised) 
        this->initialised = initialised;
    

    virtual void open() = 0;

    Digital void close() = 0;

    // These will be overridden by derived classes that implement a Particular communication port (USB, serial, etc)
    virtual void read(unsigned char* buffer, int length, unsigned int timeout) = 0;

    virtual void write(unsigned char* buffer, int length) = 0;
;

And many classes that extend the Device class and execute a particular communication port. Here’s a simplified version of the USBDevice class:

Class USBDevice: public Device

private:
    libusb_device* libUsbDevice = nullptr;
    libusb_device_handle* libUsbDeviceHandle = nullptr;
    uint16_t vendorId;
    uint16_t productId;

public:
    // Attempts to obtain a device handle and interface via libusb.
     Void open() override;

    void close() override;

    // Communication through libusb
    void read(unsigned char* buffer, int length, unsigned int timeout) override;

    // Communication through libusb
    void write(unsigned char* buffer, int length) override;
;

A few of the apparatus use their particular communication protocol, and also many others (say, in the particular manufacturer) use the same protocol.

Thus, so as to avoid code duplication, I’d like to present a protocol layer, in which I will define the protocol and use it on several devices.

Course Protocol1

;

class Protocol2

;

The Issue is, I Want to introduce my apparatus to my routine class, so that I can communicate with the unit through the protocol, like this:

// Send data to the device
device->compose (blah blah);

And I am not sure what the ideal method to do this would be.

I can get the Protocol classes inherit from the device class:

Course Protocol1: public Device

;

course Protocol2: general Device

;

And then, for my Real device:

Course SomeDevice: people USBDevice: people Protocol1


;

And for devices that support several protocols:

Class SomeOtherDevice: public USBDevice: public Protocol1: people Protocol2


;

The trouble with this method is that a) it results in a”Diamond of Dread” instance, and b) it seems wrong in the sense that a protocol is not a type of apparatus, therefore it shouldn’t actually inherit from your device type.

The other way I thought of would be to possess the device write of the protocol layer, because after all, a device does contain one or more protocols:

Course SomeOtherDevice: people USBDevice
And with this approach, I'd expose the apparatus to the protocol by just simply injecting it at point of init:

    this->device = dev;

But with this approach, it merely feels hacky. I can’t think of a rationale for why it feels like that, but it just sounds wrong. I don’t think that it’s as wrong because the initial approach, where I would effectively be abusing multiple inheritance, however I’m convinced there is a much better way that I haven’t thought about.

Are some of the strategies I’ve clarified okay? How would you get it?

 


Loading ....
 

Originally posted 2019-03-12 12:46:11. Republished by Blog Post Promoter

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.