Greetings all,
I’m trying to implement middleware (driver) for an embedded device with generic communication media layer. Not sure what is the best way to do it so I’m seeking an advice from more experienced stackoverflow users:). Basically we’ve got devices around the country communicating with our servers (or a pda/laptop in used in field). Usual form of communication is over TCP/IP, but could be also using usb, RF dongle, IR, etc. The plan is to have object corresponding with each of these devices, handling the proprietary protocol on one side and requests/responses from other internal systems on the other.
The thing is how create something generic in between the media and the handling objects. I had a play around with the TCP dispatcher using boost.asio but trying to create something generic seems like a nightmare :). Anybody tried to do something like that? What is the best way how to do it?
Example: Device connects to our Linux server. New middleware instance is created (on the server) which announces itself to one of the running services (details are not important). The service is responsible for making sure that device’s time is synchronized. So it asks the middleware what is the device’s time, driver translates it to device language (protocol) and sends the message, device responses and driver again translates it for the service. This might seem as a bit overkill for such a simple request but imagine there are more complex requests which the driver must translate, also there are several versions of the device which use different protocol, etc. but would use the same time sync service. The goal is to abstract the devices through the middleware to be able to use the same service to communicate with them.
Another example: we find out that the remote communications with the device are down. So we send somebody out with PDA, he connects to the device using USB cable. Starts up the application which has the same functionality as the timesync service. Again middleware instance is created (on the PDA) to translate communication between application and the device this time only using USB/serial media not TCP/IP as in previous example.
I hope it makes more sense now 🙂
Cheers,
Tom
I haven’t used Boost, but the way I usually handled that kind of problem was to create a Device base class which the server interacts with, and then subclassed it for each device type, and made the subclasses deal with the device oddness. That way, the Device class becomes a definition of your protocol. Also, the Device class would need to be portable, but the subclasses would not.
If you had to get fancier than that, you could use the Factory pattern to create the actual subclassed objects.
As far as actually communicating, I’d see if I could just run one process per Device. If you have to have more than one Device per process, on Linux I’d just use
select()and its friends to manage I/O between the various Device instances. I don’t know how to do that on Windows; itsselectonly works for sockets, not serial ports or other file-like things.Other things that come to mind that might be useful include dbus and the MPI (Message Passing Interface) library, though they aren’t complete solutions for your problem (dbus doesn’t do inter-computer communications, IIRC).
Does this help at all?
EDIT: Needed a formatted response to Tom’s reply…
The subclasses contain the communication specific parts. That’s the whole point of using subclasses here; the generic stuff goes in the base class, and the specifics go in the subclass.
I think that may be a bit complex, and you’re expecting a generic Device to deal with a specific Connection, which can get hard to maintain fast.
What I’d recommend is a Device subclass specifically for handling that type of Connection which takes the Connection from the dispatcher and owns it until the connection closes. Then your manager can talk to the generic Device and the Connection can mess with the specifics.
An example: Say you have a temperature sensor USB thingamajig. You have some dispatcher that catches the “USB thing plugged in” signal. When it sees the USB thing plugged in:
USBTemperatureThingConnection.USBTemperatureDevice, which is a subclass of Device, giving theUSBTemperatureThingConnectionto theUSBTemperatureDeviceas a constructor parameter.USBTemperatureDevice::USBTemperatureDevice(USBTemperatureThingConnection* conn)goes and sets up whatever it needs locally to finish setting up the Connection, then sends a message to the Device Manager saying it has set itself up.Some time later, the Device Manager wants to set the time on all devices. So it iterates through its list of devices and calls the generic (maybe even abstract)
Device::SetTime(const struct timespec_t&)method on each of them.When it gets to your temperature device, it calls
USBTemperatureDevice::SetTime(const struct timespec_t&), sinceUSBTemperatureDeviceoverrode the one inDevice(which was either abstract, i.e.virtual void SetTime(const struct timespec_t&) = 0;or a no-op, i.e.virtual void SetTime(const struct timespec_t&) {}, so you don’t have to override it for devices that can’t set time).USBTemperatureDevice::SetTime(const struct timespec_t&)does whatever USB Temperature sensor-specific things are needed, using theUSBTemperatureThingConnection, to get the time set.Some time later, the device might send back a “Time Set Result” message, saying if it worked or not. That comes in on the
USBTemperatureThingConnection, which wakes up your thread and you need to deal with it. So yourUSBTemperatureDevice::DealWithMessageFromSensor()method (which only exists inUSBTemperatureDevice) dives into the message contents and figures out if the time setting worked or not. It then takes that result, turns it into a value defined inenum Device::ResultCodeand callsDevice::TimeSetComplete(ResultCode result), which records the result, sets a flag (bool Device::timeComplete) saying the result is in, and then hits aSemaphoreorConditionto wake up the Device Manager and get it to check all theDevice‘s, in case it was blocked waiting for all the devices to finish setting time before continuing.I have no idea what that pattern is called. If pressed, I’d say “subclassing”, or “object-oriented design” if I felt grumpy. The “middleware” is the
Deviceclass, the DeviceManager, and all their underlings. The application then just talks to the Device Manager, or at most to the genericDeviceinterface of a specific device.Good to hear.