lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I'm looking for a little design inspiration from others.  I have a
basic approach (which I'll describe, below), but chances are good that
someone here has already implemented something like this or can see a
better solution.

I have a UDP-based communications protocol for controlling a device.
The protocol is implemented in C and specified in a tedious Microsoft
Word document.  The base protocol is simple and extensible and isn't
interesting.  What is interesting is there are a large number of
binary messages, each with a unique structure.  I'm bothered that when
I change the C code to add a new message, I also have to update the
Word document.  The two are separate things.

I'd like to write a tool that could parse these messages and display
them in human readable form.  And I'd like another tool that does the
reverse-- specify a message in a human-readable form, and get back out
a message.  And I'd like to be able to write a Wireshark dissector for
these messages.  And I'd like... well, more stuff.

The solution would seem to be to express the protocol in some
abstract, high-level representation (like... a Lua table).  Once in
this form, I can write a tool that will dump out a description of the
protocol in HTML.  I could also have it generate optimized C code to
implement the protocol parser (such as converting to a finite state
machine).  The table would also be at the heart of the Wireshark
dissector and the tools to display and create packets.  And so on.

Now whenever I make changes to the protocol, I edit the Lua table that
describes the messages, and I'm done.  Everything flows from that.

So how about something like this:

    OPT_MUTE = {
        option = 0x02,
        read = true,
        write = true,
        message = {
                name = "type",
                type = OutputInput,
                desc = "Channel Type"
                name = "number"
                type = ChannelNumber,
                desc = "Channel Number"
                name = "mute"
                type = OffOn,
                desc = "Mute Status"

So I have a message named OPT_MUTE that has a binary value of 0x02,
it's both readable and writable, and has three items in the payload.
The three items are named type, number, and mute and are of specific
types (OutputInput, ChannelNumber, and OffOn).  Each of these types is
itself a table that describes them further:

ChannelNumber = {
    bytes = 1,
    low = 0,
    high = 23,

There are (potentially) a variety of other fields to describe messages
(which devices implement them, numeric ranges), and the payload
section can have repetitions of a item or a larger structure, or
optional data at the end of a message.

What I'm looking for is if anyone has done this kind of thing before
and what kind of representation you came up with.  The biggest
stumbling block I have is the representation of repeated and optional
data in the payload.