Program: 35948
Project: 35948
Buggy source:
/** 
 * This is the thread on which all the IOIO activity happens. It will be run every time the application is resumed and aborted when it is paused. The method setup() will be called right after a connection with the IOIO has been established (which might happen several times!). Then, loop() will be called repetitively until the IOIO gets disconnected.
 * @see ioio.lib.util.android.IOIOService#createIOIOLooper()
 */
@Override protected IOIOLooper createIOIOLooper(){
  return new BaseIOIOLooper(){
    private boolean callbackRegistered=false;
    private Uart IBusConn;
    private InputStream busIn;
    private OutputStream busOut;
    private DigitalOutput statusLED;
    private DigitalOutput faultPin;
    private DigitalOutput chipSelectPin;
    private int IBusRXPinId=10;
    private int IBusTXPinId=13;
    private int chipSelectPinId=11;
    private int faultPinId=12;
    private int msgLength;
    private Calendar time;
    private long lastRead;
    private long lastSend;
    private ArrayList<Byte> readBuffer;
    private Map<Byte,IBusSystemCommand> IBusSysMap=new HashMap<Byte,IBusSystemCommand>();
    /** 
 * Called every time a connection with IOIO has been established. Setup the connection to the IBus and bring up the CS/Fault Pins on the MCP2004
 * @throws ConnectionLostException When IOIO connection is lost.
 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
 */
    @Override protected void setup() throws ConnectionLostException, InterruptedException {
      Log.d(TAG,"String_Node_Str");
      IBusConn=ioio_.openUart(IBusRXPinId,IBusTXPinId,9600,Uart.Parity.EVEN,Uart.StopBits.ONE);
      statusLED=ioio_.openDigitalOutput(IOIO.LED_PIN,true);
      chipSelectPin=ioio_.openDigitalOutput(chipSelectPinId,true);
      chipSelectPin.write(true);
      faultPin=ioio_.openDigitalOutput(faultPinId,true);
      faultPin.write(true);
      busIn=IBusConn.getInputStream();
      busOut=IBusConn.getOutputStream();
      readBuffer=new ArrayList<Byte>();
      msgLength=0;
      time=Calendar.getInstance();
      lastRead=time.getTimeInMillis();
      lastSend=time.getTimeInMillis();
    }
    /** 
 * Called repetitively while the IOIO is connected. Reads and writes to the IBus
 * @throws ConnectionLostException When IOIO connection is lost.
 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
 */
    @Override public void loop() throws ConnectionLostException, InterruptedException {
      if ((Calendar.getInstance().getTimeInMillis() - lastRead) > 30) {
        readBuffer.clear();
      }
      if (!callbackRegistered && mIBusCbListener != null)       initiateHandlers();
      try {
        if (busIn.available() > 0) {
          statusLED.write(true);
          lastRead=Calendar.getInstance().getTimeInMillis();
          readBuffer.add((byte)busIn.read());
          if (readBuffer.size() == 1) {
            msgLength=256;
          }
 else           if (readBuffer.size() == 2) {
            msgLength=(int)readBuffer.get(1);
          }
          if (readBuffer.size() == msgLength + 2) {
            if (checksumMessage(readBuffer)) {
              String data="String_Node_Str";
              for (int i=0; i < readBuffer.size(); i++)               data=String.format("String_Node_Str",data,readBuffer.get(i));
              Log.d(TAG,String.format("String_Node_Str",DeviceLookup.get(readBuffer.get(0)),DeviceLookup.get(readBuffer.get(2)),data));
              handleMessage(readBuffer);
            }
            readBuffer.clear();
          }
          statusLED.write(false);
        }
 else         if (actionQueue.size() > 0) {
          statusLED.write(true);
          if ((Calendar.getInstance().getTimeInMillis() - lastSend) > 75) {
            Log.d(TAG,String.format("String_Node_Str",actionQueue.get(0).toString()));
            IBusCommandsEnum command=actionQueue.get(0).commandType;
            IBusSystemCommand clsInstance=IBusSysMap.get(command.getSystem().toByte());
            Object cmdArgs=actionQueue.get(0).commandArgs;
            byte[] outboundMsg=new byte[]{};
            try {
              Method requestedMethod=clsInstance.getClass().getMethod(command.getMethodName());
              if (cmdArgs == null)               outboundMsg=(byte[])requestedMethod.invoke(clsInstance);
 else               outboundMsg=(byte[])requestedMethod.invoke(clsInstance,cmdArgs);
            }
 catch (            IllegalAccessException e) {
              e.printStackTrace();
            }
catch (            IllegalArgumentException e) {
              e.printStackTrace();
            }
catch (            InvocationTargetException e) {
              e.printStackTrace();
            }
catch (            NoSuchMethodException e) {
              e.printStackTrace();
            }
            actionQueue.remove(0);
            String out="String_Node_Str";
            for (int i=0; i < outboundMsg.length; i++) {
              out=String.format("String_Node_Str",out,outboundMsg[i]);
              busOut.write(outboundMsg[i]);
            }
            Log.d(TAG,out);
            lastSend=Calendar.getInstance().getTimeInMillis();
            statusLED.write(false);
          }
        }
      }
 catch (      IOException e) {
        Log.e(TAG,String.format("String_Node_Str",e.getMessage()));
      }
      Thread.sleep(2);
    }
    private void initiateHandlers(){
      IBusSysMap.put(DeviceAddressEnum.Radio.toByte(),new RadioSystemCommand());
      IBusSysMap.put(DeviceAddressEnum.InstrumentClusterElectronics.toByte(),new IKESystemCommand());
      IBusSysMap.put(DeviceAddressEnum.NavigationEurope.toByte(),new NavigationSystemCommand());
      IBusSysMap.put(DeviceAddressEnum.MultiFunctionSteeringWheel.toByte(),new SteeringWheelSystemCommand());
      IBusSysMap.put(DeviceAddressEnum.GraphicsNavigationDriver.toByte(),new BoardMonitorSystemCommand());
      for (      Object key : IBusSysMap.keySet())       IBusSysMap.get(key).registerCallbacks(mIBusCbListener);
      callbackRegistered=true;
    }
    /** 
 * Verify that the IBus Message is legitimate  by XORing all bytes if correct, the product  should be 0x00
 * @param ArrayList<byte> msgBuffer	The buffer containing all bytes in the Message
 * @return boolean	 true if the message isn't corrupt, otherwise false
 */
    private boolean checksumMessage(    ArrayList<Byte> msgBuffer){
      byte cksum=0x00;
      for (      byte msg : msgBuffer) {
        cksum=(byte)(cksum ^ msg);
      }
      return (cksum == 0x00) ? true : false;
    }
    /** 
 * Send the inbound message to the correct Handler 
 * @param msg
 */
    private void handleMessage(    ArrayList<Byte> msg){
      try {
        IBusSysMap.get(msg.get(0)).mapReceived(msg);
      }
 catch (      NullPointerException npe) {
      }
    }
  }
;
}

Fixed version (ground truth):
/** 
 * This is the thread on which all the IOIO activity happens. It will be run every time the application is resumed and aborted when it is paused. The method setup() will be called right after a connection with the IOIO has been established (which might happen several times!). Then, loop() will be called repetitively until the IOIO gets disconnected.
 * @see ioio.lib.util.android.IOIOService#createIOIOLooper()
 */
@Override protected IOIOLooper createIOIOLooper(){
  return new BaseIOIOLooper(){
    private boolean callbackRegistered=false;
    private Uart IBusConn;
    private InputStream busIn;
    private OutputStream busOut;
    private DigitalOutput statusLED;
    private DigitalOutput faultPin;
    private DigitalOutput chipSelectPin;
    private int IBusRXPinId=10;
    private int IBusTXPinId=13;
    private int chipSelectPinId=11;
    private int faultPinId=12;
    private int msgLength;
    private Calendar time;
    private long lastRead;
    private long lastSend;
    private ArrayList<Byte> readBuffer;
    private Map<Byte,IBusSystemCommand> IBusSysMap=new HashMap<Byte,IBusSystemCommand>();
    /** 
 * Called every time a connection with IOIO has been established. Setup the connection to the IBus and bring up the CS/Fault Pins on the MCP2004
 * @throws ConnectionLostException When IOIO connection is lost.
 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
 */
    @Override protected void setup() throws ConnectionLostException, InterruptedException {
      Log.d(TAG,"String_Node_Str");
      IBusConn=ioio_.openUart(IBusRXPinId,IBusTXPinId,9600,Uart.Parity.EVEN,Uart.StopBits.ONE);
      statusLED=ioio_.openDigitalOutput(IOIO.LED_PIN,true);
      chipSelectPin=ioio_.openDigitalOutput(chipSelectPinId,true);
      chipSelectPin.write(true);
      faultPin=ioio_.openDigitalOutput(faultPinId,true);
      faultPin.write(true);
      busIn=IBusConn.getInputStream();
      busOut=IBusConn.getOutputStream();
      readBuffer=new ArrayList<Byte>();
      msgLength=0;
      time=Calendar.getInstance();
      lastRead=time.getTimeInMillis();
      lastSend=time.getTimeInMillis();
    }
    /** 
 * Called repetitively while the IOIO is connected. Reads and writes to the IBus
 * @throws ConnectionLostException When IOIO connection is lost.
 * @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
 */
    @Override public void loop() throws ConnectionLostException, InterruptedException {
      if ((Calendar.getInstance().getTimeInMillis() - lastRead) > 30) {
        readBuffer.clear();
      }
      if (!callbackRegistered && mIBusCbListener != null)       initiateHandlers();
      try {
        if (busIn.available() > 0) {
          statusLED.write(true);
          lastRead=Calendar.getInstance().getTimeInMillis();
          readBuffer.add((byte)busIn.read());
          if (readBuffer.size() == 1) {
            msgLength=256;
          }
 else           if (readBuffer.size() == 2) {
            msgLength=(int)readBuffer.get(1);
          }
          if (readBuffer.size() == msgLength + 2) {
            if (checksumMessage(readBuffer)) {
              String data="String_Node_Str";
              for (int i=0; i < readBuffer.size(); i++)               data=String.format("String_Node_Str",data,readBuffer.get(i));
              Log.d(TAG,String.format("String_Node_Str",DeviceLookup.get(readBuffer.get(0)),DeviceLookup.get(readBuffer.get(2)),data));
              handleMessage(readBuffer);
            }
            readBuffer.clear();
          }
          statusLED.write(false);
        }
 else         if (actionQueue.size() > 0) {
          statusLED.write(true);
          if ((Calendar.getInstance().getTimeInMillis() - lastSend) > 75) {
            Log.d(TAG,String.format("String_Node_Str",actionQueue.get(0).toString()));
            IBusCommandsEnum command=actionQueue.get(0).commandType;
            IBusSystemCommand clsInstance=IBusSysMap.get(command.getSystem().toByte());
            Object cmdArgs=actionQueue.get(0).commandArgs;
            byte[] outboundMsg=new byte[]{};
            try {
              Method requestedMethod=clsInstance.getClass().getMethod(command.getMethodName());
              if (cmdArgs == null)               outboundMsg=(byte[])requestedMethod.invoke(clsInstance);
 else               outboundMsg=(byte[])requestedMethod.invoke(clsInstance,cmdArgs);
            }
 catch (            IllegalAccessException e) {
              e.printStackTrace();
            }
catch (            IllegalArgumentException e) {
              e.printStackTrace();
            }
catch (            InvocationTargetException e) {
              e.printStackTrace();
            }
catch (            NoSuchMethodException e) {
              e.printStackTrace();
            }
            actionQueue.remove(0);
            String out="String_Node_Str";
            for (int i=0; i < outboundMsg.length; i++) {
              out=String.format("String_Node_Str",out,outboundMsg[i]);
              busOut.write(outboundMsg[i]);
            }
            Log.d(TAG,out);
            lastSend=Calendar.getInstance().getTimeInMillis();
          }
          statusLED.write(false);
        }
      }
 catch (      IOException e) {
        Log.e(TAG,String.format("String_Node_Str",e.getMessage()));
      }
      Thread.sleep(2);
    }
    private void initiateHandlers(){
      IBusSysMap.put(DeviceAddressEnum.Radio.toByte(),new RadioSystemCommand());
      IBusSysMap.put(DeviceAddressEnum.InstrumentClusterElectronics.toByte(),new IKESystemCommand());
      IBusSysMap.put(DeviceAddressEnum.NavigationEurope.toByte(),new NavigationSystemCommand());
      IBusSysMap.put(DeviceAddressEnum.MultiFunctionSteeringWheel.toByte(),new SteeringWheelSystemCommand());
      IBusSysMap.put(DeviceAddressEnum.OnBoardMonitor.toByte(),new BoardMonitorSystemCommand());
      for (      Object key : IBusSysMap.keySet())       IBusSysMap.get(key).registerCallbacks(mIBusCbListener);
      callbackRegistered=true;
    }
    /** 
 * Verify that the IBus Message is legitimate  by XORing all bytes if correct, the product  should be 0x00
 * @param ArrayList<byte> msgBuffer	The buffer containing all bytes in the Message
 * @return boolean	 true if the message isn't corrupt, otherwise false
 */
    private boolean checksumMessage(    ArrayList<Byte> msgBuffer){
      byte cksum=0x00;
      for (      byte msg : msgBuffer) {
        cksum=(byte)(cksum ^ msg);
      }
      return (cksum == 0x00) ? true : false;
    }
    /** 
 * Send the inbound message to the correct Handler 
 * @param msg
 */
    private void handleMessage(    ArrayList<Byte> msg){
      try {
        IBusSysMap.get(msg.get(0)).mapReceived(msg);
      }
 catch (      NullPointerException npe) {
      }
    }
  }
;
}