Jump to content

I have been making some improvements to idea/project that first started a while back. Prototype version was crappy C# console application, which turned into java console app, and as of today I have a somewhat working version of android service app. What the app does? Basically a service that is reading (and sending) messages from (to) vehicle's LIN bus network. For my vehicle, bus is simply single wire 9600 8E1 serial.

 

First challenge, which turned out to be the easiest, was to parse individual messages from the bus and find the start. Messages have no beginning or end identifier, and once serial connection is open there is no guarantee that first bytes received are necessarily beginning of a message. On positive side each message had a specific structure.

 

1 byte: Source

1 byte: Length (excluding first two bytes)

1 byte: Destination

x bytes: Payload

1 byte: xor checksum

 

Solution is fairly simply, but brutal. Brute-force.

    private void process()    {      Log.d(TAG, "Starting processing");      boolean working = true;      while ( working )      {        if ( size() >= MSG_MIN_SIZE ) // At least five bytes in buffer (minimum message length)        {          int assumedLength = ( peek(1) & 0xFF ) + 2;          if ( size() >= assumedLength )          {            ByteBuffer byteBuffer = ByteBuffer.allocate(assumedLength);            for ( int i = 0; i < assumedLength; i++ )            {              byteBuffer.put(peek(i));            }            BusMessage busMessage = BusMessage.build(byteBuffer.array());            if ( busMessage != null )            {              setSync(true);              truncate(assumedLength);              fireNewMessage(busMessage);            }            else            {              setSync(false);              truncate();            }          }          else            working = false;        }        else          working = false;      }      Log.d(TAG, "Finished processing");    }

Although there might be a lag before first valid message is found or after corrupt message is received, the process would catch up quickly. Once in "sync" there is almost no delay.

 

The reason for reading messages from LIN is to react to some things that happen in a car. For example a steering wheel button press send a message on said LIN. Say the button pressed is volume up, service would receive such message and increase volume. Might seem silly, but there is more than just volume controls.

 

That is nice and all, but I needed to come up with a polymorphic structure to avoid ridiculously huge amount of switch cases. So here is what I have been thinking. 

When raw message is received, it is parsed into Basic message which includes source device, destination device, length, and payload byte array. Xor checksum is discarded once message was verified.

At this point service would look at list of subscribers that are waiting for messages from source device and to destination device, and dispatch basic message to that subscriber. Subscribers would essentially be a classes that register with service as mentioned.

 

It seems that this method would work and not make a mess. Additionally making plugins would be easier as well.

Would this be a good approach? Perhaps there are issues that I haven't considered, and just general feedback is welcomed.

 

https://github.com/P1X3/BMWinterface

 

Edit: Forgot to add github link in case someone wants to check it out. Doesn't have latest commits, yet, but a somewhat working code is there.

 

 

Link to comment
https://linustechtips.com/topic/198733-looking-for-project-feedback/
Share on other sites

Link to post
Share on other sites

Your project sounds pretty awesome. 

 

I like your approach for sending messages to the subscriber.

As for any issues, I can't really tell if it would be a problem, but what if a subscriber wanted to receive a message from multiple sources, for example, a window being controlled by two different switches?

Also, if you are just relaying the message to the service, could a corrupt message get through that might have some negative impact?

Link to post
Share on other sites

As for any issues, I can't really tell if it would be a problem, but what if a subscriber wanted to receive a message from multiple sources, for example, a window being controlled by two different switches?

Also, if you are just relaying the message to the service, could a corrupt message get through that might have some negative impact?

 

Well the method I had in my head works, but not as well as I expected. When passing message to a subscriber, I must pass context otherwise subscriber wont be able to interact with android. Possible problem is that context might get destroyed before it reaches the subscriber.

Second issue is that this method of "modularity" works only within same apk, which is not what I want. I want to have core application and plugin applications.

 

Any good articles on android modular applications? I will be googling it, but open to suggestions and links to personal favors.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×