I’m trying to model the following problem, but I’m not sure how to structure my data.
- I have an industrial fieldbus which may contain a number of
subdevices
. - Each
Subdevice
has a fairly arduous amount ofSubdeviceConfiguration
. - There can be multiple of the same model of
Subdevice
, but each must be given a uniqueStationAddress (u16)
. It would be nice to be able to reuse the configuration if a user has many of the same model of subdevices. - The ordering of the
subdevices
matters. - The user should be able to define a
NetworkConfiguration
that describes the expectedsubdevices
on the bus. - The
NetworkConfiguration
should beconst
. - The subdevices each report a variable bit length of
process_data
(not guaranteed to be divisible by 8), which can be modeled as apacked struct
. The bit length of theprocess_data
is defined in theSubdeviceConfiguration
- The user should be able to easily reference / read / write to the
process_data
for a specificsubdevice
which is kept up to date by regularly transfering ethernet frames between the host and the subdevice.
What i’ve come up with so far:
- I know I want the
NetworkConfiguration
to beconst
because I don’t want some big massive mess of global variable state. There are other libraries that have gone this route and they are very difficult to read / understand because of this. For this same reason I don’t want to put theprocess_data
in theNetworkConfiguration
. - Perhaps I can take in a
* const NetworkConfiguration
and cut it up / distribute it among a number of[]Subdevice
where eachSubdevice
contains a* const SubDeviceConfiguration
in addition toprocess_data
. - I could model
process_data
as a[]u8
even though it could have a bit length anywhere between 1 and 65535. It is typically a multiple of 8 and typically less than 256. Which makes the inefficency of the[]u8
representation not too horrible. - To manipulate the
process_data
I could provide a function to retreive it and write to it using a packed struct using a generic function, and assert that the bitsize of the packed struct provided has the same size as what is configured in theSubdeviceConfiguration
. Or I could add atype
field to mySubdeviceConfiguration
and really go deep into generic code.