0

I'm working on an app which talks to a bluetooth low energy (BLE) device and exchanges customized data with it. Our team has defined the data models and there's a method that will parse the data payload and assign values to the corresponding characteristics. It works all fine until we have introduced a new firmware.

In this new firmware some values in the data payload have been re-defined or the offset of a specific value has been changed. So the parsing method will need to be updated/re-written according to the new payload definition. Then here comes the problem: both two versions of the firmware need to be supported! Of course I could write a lot of if/else in the parsing method right now, but what happend if 3 other firmware updates arrive one after other? I can imagine the code will become hard to read and lose it's simplicity.

I'm wondering if there's an elegant way to manage the co-existence of firmware versions. Maybe a design pattern that can be adopted here?

To make it more specific:

In my model class Temperature I've the following properties:

@property (nonatomic, readonly) float temp_outside;
@property (nonatomic, readonly) float temp_inside;

The library has to support 3 different firmware versions:

  • V1 does only support temp_outside
  • V2 does support temp_outside and temp_inside
  • V3 does support temp_outside and temp_inside, but temp_inside has to be obtained differently compared to V1 & V2

Assuming the firmware provides their information within the manufacturer data of the advertisings as raw data:

  • V1 firmware: 00 42 0a
    • (byte 1: sw id, byte 2: protocol id, byte 3: temp_outside)
  • V2 firmware: 00 43 0a 10
    • (byte 1: sw id, byte 2: protocol id, byte 3: temp_outside, byte 4: temp_inside)
  • V3 firmware: 00 44 10 0a
    • (byte 1: sw id, byte 2: protocol id, byte 3: temp_inside, byte 4: temp_outside)

What is the best way to implement this? There are several things to consider:

  1. The model has values which might not be filled by a specific firmware; should I have separate models per firmware or how can I make sure that only supported properties will be accessed?

  2. The method responsible for parsing the bytes into the model has to support all firmwares. Should there be one method / parser with several conditions based on the protocol id or several parser classes?

  3. The usage of the library within view controllers should be as convenient as possible.

asked Jun 28, 2014 at 12:07

1 Answer 1

1

Your problem isn't so much that you need to support different firmware versions, but rather that all these firmware versions use slightly different protocols to communicate.

If you have any influence on the protocol used by the device's firmware, you should aim for a protocol that is both backwards and forwards compatible, so that a newer sender can communicate with an older receiver and vice versa.

For the protocol versions that you already have, you can use the Strategy pattern, where you have a parser for each protocol version as a strategy and you select the right parser based on the identification fields in each message (or in the first message).

Inside your Model classes (and your Views), you should plan for the possibility that not all properties are guaranteed to be available. The two ways to handle this is to have a secondary boolean property/method for each property to indicate if the value is available, or to use a special, out-of-bounds marker value to indicate the value is not available.

answered Jun 28, 2014 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.