Program: 35953
Project: 35953
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>();
    private Map<IBusCommands,IBusMethodHolder> IBusCommandMap=new HashMap<IBusCommands,IBusMethodHolder>();
    /** 
 * 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 {
      statusLED.write(true);
      if ((Calendar.getInstance().getTimeInMillis() - lastRead) > 30) {
        readBuffer.clear();
      }
      if (!callbackRegistered && mIBusCbListener != null)       initiateHandlers();
      try {
        if (busIn.available() > 0) {
          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();
          }
        }
 else         if (actionQueue.size() > 0) {
          if ((Calendar.getInstance().getTimeInMillis() - lastSend) > 75) {
            Log.d(TAG,String.format("String_Node_Str",actionQueue.get(0).toString()));
            IBusMethodHolder command=IBusCommandMap.get(actionQueue.get(0));
            byte[] outboundMsg=new byte[]{};
            try {
              outboundMsg=(byte[])command.methodReference.invoke(command.classInstance,command.args);
            }
 catch (            IllegalAccessException e) {
              e.printStackTrace();
            }
catch (            IllegalArgumentException e) {
              e.printStackTrace();
            }
catch (            InvocationTargetException 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();
          }
        }
      }
 catch (      IOException e) {
        Log.e(TAG,String.format("String_Node_Str",e.getMessage()));
      }
      statusLED.write(false);
      Thread.sleep(2);
    }
    /** 
 * This class is used to hold a class instance, method instance and any possible optional arguments. These instances will be placed into a map and called when a command execution request comes into the `actionQueue`
 */
class IBusMethodHolder {
      public IBusSystemCommand classInstance=null;
      public Method methodReference=null;
      public String args=null;
      IBusMethodHolder(      IBusSystemCommand cls,      Method mth){
        classInstance=cls;
        methodReference=mth;
      }
    }
    private void initiateHandlers(){
      IBusSysMap.put(DeviceAddress.Radio.toByte(),new RadioSystemCommand());
      IBusSysMap.put(DeviceAddress.InstrumentClusterElectronics.toByte(),new IKESystemCommand());
      IBusSysMap.put(DeviceAddress.NavigationEurope.toByte(),new NavigationSystemCommand());
      IBusSysMap.put(DeviceAddress.MultiFunctionSteeringWheel.toByte(),new SteeringWheelSystemCommand());
      IBusSysMap.put(DeviceAddress.GraphicsNavigationDriver.toByte(),new BoardMonitorSystemCommand());
      for (      Object key : IBusSysMap.keySet())       IBusSysMap.get(key).registerCallbacks(mIBusCbListener);
      IBusSystemCommand BM=IBusSysMap.get(DeviceAddress.GraphicsNavigationDriver.toByte());
      try {
        IBusCommandMap.put(IBusCommands.BMToIKEGetTime,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetDate,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetOutdoorTemp,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetFuel1,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetFuel2,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetRange,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetAvgSpeed,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioGetStatus,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEResetFuel1,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEResetFuel2,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEResetAvgSpeed,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioModePress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioModeRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioVolumeUp,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioVolumeDown,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneFwdPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneFwdRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneRevPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneRevRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioFMPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioFMRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioAMPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioAMRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioInfoPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioInfoRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
      }
 catch (      NoSuchMethodException e) {
        Log.e(TAG,e.getMessage());
      }
      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>();
    private Map<IBusCommands,IBusMethodHolder> IBusCommandMap=new HashMap<IBusCommands,IBusMethodHolder>();
    /** 
 * 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 {
      statusLED.write(true);
      if ((Calendar.getInstance().getTimeInMillis() - lastRead) > 30) {
        readBuffer.clear();
      }
      if (!callbackRegistered && mIBusCbListener != null)       initiateHandlers();
      try {
        if (busIn.available() > 0) {
          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();
          }
        }
 else         if (actionQueue.size() > 0) {
          if ((Calendar.getInstance().getTimeInMillis() - lastSend) > 75) {
            Log.d(TAG,String.format("String_Node_Str",actionQueue.get(0).toString()));
            IBusMethodHolder command=IBusCommandMap.get(actionQueue.get(0));
            byte[] outboundMsg=new byte[]{};
            try {
              outboundMsg=(byte[])command.methodReference.invoke(command.classInstance);
            }
 catch (            IllegalAccessException e) {
              e.printStackTrace();
            }
catch (            IllegalArgumentException e) {
              e.printStackTrace();
            }
catch (            InvocationTargetException 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();
          }
        }
      }
 catch (      IOException e) {
        Log.e(TAG,String.format("String_Node_Str",e.getMessage()));
      }
      statusLED.write(false);
      Thread.sleep(2);
    }
    /** 
 * This class is used to hold a class instance, method instance and any possible optional arguments. These instances will be placed into a map and called when a command execution request comes into the `actionQueue`
 */
class IBusMethodHolder {
      public IBusSystemCommand classInstance=null;
      public Method methodReference=null;
      IBusMethodHolder(      IBusSystemCommand cls,      Method mth){
        classInstance=cls;
        methodReference=mth;
      }
    }
    private void initiateHandlers(){
      IBusSysMap.put(DeviceAddress.Radio.toByte(),new RadioSystemCommand());
      IBusSysMap.put(DeviceAddress.InstrumentClusterElectronics.toByte(),new IKESystemCommand());
      IBusSysMap.put(DeviceAddress.NavigationEurope.toByte(),new NavigationSystemCommand());
      IBusSysMap.put(DeviceAddress.MultiFunctionSteeringWheel.toByte(),new SteeringWheelSystemCommand());
      IBusSysMap.put(DeviceAddress.GraphicsNavigationDriver.toByte(),new BoardMonitorSystemCommand());
      for (      Object key : IBusSysMap.keySet())       IBusSysMap.get(key).registerCallbacks(mIBusCbListener);
      IBusSystemCommand BM=IBusSysMap.get(DeviceAddress.GraphicsNavigationDriver.toByte());
      try {
        IBusCommandMap.put(IBusCommands.BMToIKEGetTime,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetDate,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetOutdoorTemp,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetFuel1,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetFuel2,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetRange,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEGetAvgSpeed,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioGetStatus,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEResetFuel1,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEResetFuel2,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToIKEResetAvgSpeed,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioModePress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioModeRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioVolumeUp,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioVolumeDown,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneFwdPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneFwdRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneRevPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioTuneRevRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioFMPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioFMRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioAMPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioAMRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioInfoPress,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
        IBusCommandMap.put(IBusCommands.BMToRadioInfoRelease,new IBusMethodHolder(BM,BM.getClass().getMethod("String_Node_Str")));
      }
 catch (      NoSuchMethodException e) {
        Log.e(TAG,e.getMessage());
      }
      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) {
      }
    }
  }
;
}