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
Subdevicehas 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
subdevicesmatters. - The user should be able to define a
NetworkConfigurationthat describes the expectedsubdeviceson the bus. - The
NetworkConfigurationshould 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_datais defined in theSubdeviceConfiguration - The user should be able to easily reference / read / write to the
process_datafor a specificsubdevicewhich 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
NetworkConfigurationto beconstbecause 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_datain theNetworkConfiguration. - Perhaps I can take in a
* const NetworkConfigurationand cut it up / distribute it among a number of[]Subdevicewhere eachSubdevicecontains a* const SubDeviceConfigurationin addition toprocess_data. - I could model
process_dataas a[]u8even 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[]u8representation not too horrible. - To manipulate the
process_dataI 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 atypefield to mySubdeviceConfigurationand really go deep into generic code.