Package tcg.scada.iec

Examples of tcg.scada.iec.IecApdu


    logger_.info("Change notification for " + datapoint.getName()
            + ". Address: " + datapoint.getAddress());
    logger_.info("Building spontaneous notification message and sending it to all connected clients.");
   
    //build a spontaneous APDU. ensure COT=3 (Spontaneous)
    IecApdu apdu = new IecApdu();
    apdu.format = EIecApduFormat.I_FORMAT;
    apdu.asdu.commonAddress = commonAddress_;
    apdu.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
    apdu.asdu.causeOfTransmission = EIecCauseOfTransmission.SPONTANEOUS; 
   
View Full Code Here


        }
       
        //build a response message, in case no other message will be transmitted.
        //clock synchronization CON (COT=7)
        logger_.info("Sending clock synchronization confirm (C_CS_NA_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);
      }
      else if (request.asdu.typeId == EIecTypeId.C_IC_NA_1)
      {
        //get interrogation group
        int  interroGroup = 0;     
        try
        {
          interroGroup = request.asdu.infoObjects.get(0).infoElements[0].getIntValue();
        }
        catch(Exception ex)
        {
          //ignore
        }
       
        //interrogation command
        logger_.info("Received interrogation command (C_IC_NA_1). Interro Group: " + interroGroup);
       
        //reply to data request only if the data transfer has been enabled
        if (!isDataEnabled_)
        {
          logger_.warn("Data transfer is not enabled. It will be ignored.");
          return false;
        }
       
        //ensure the COT=6
        if (request.asdu.causeOfTransmission.value() != EIecCauseOfTransmission.ACTIVATION.value())
        {
          logger_.warn("Cause of Transmission is not ACTIVATION (6). Maybe we should ignore this message?");
          logger_.warn("For now, we will just process it as if the correct COT is set.");
        }

        //interrogation command CON (COT=7)
        logger_.info("Sending interrogation command confirm (C_IC_NA_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);
     
        Iterator<DataPoint>   it = null;
        DataPoint        dp = null;
        Collection<DataPoint>   dpList = null;
       
        //cause of transmission depend of the interrogation group
        //default: station -> all datapoints
        EIecCauseOfTransmission cot = EIecCauseOfTransmission.INTERROGATED_STATION;
        if (interroGroup == 1)
        {
          //DI
          cot = EIecCauseOfTransmission.INTERROGATED_GROUP1;
        }
        else if (interroGroup == 2)
        {
          //DDI
          cot = EIecCauseOfTransmission.INTERROGATED_GROUP2;
        }
        else if (interroGroup == 3)
        {
          //AI including TDI and UTC
          cot = EIecCauseOfTransmission.INTERROGATED_GROUP3;
        }
        else if (interroGroup == 4)
        {
          //MI
          cot = EIecCauseOfTransmission.INTERROGATED_GROUP4;
        }
       
        //return all di (COT=20)
        if (interroGroup == 0 || interroGroup == 1)
        {
          dpList = parent_.data_.getAllDIPoints();
          logger_.info("Sending all DI datapoints (as single value). Number of datapoints: " + dpList.size());
 
          it = dpList.iterator();
          while(it.hasNext())
          {
            response = new IecApdu();
            response.format = EIecApduFormat.I_FORMAT;
            //response.asdu.typeId = EIecTypeId.M_SP_NA_1;
            response.asdu.typeId = EIecTypeId.M_SP_TB_1;  //with CP56Time2a
            response.asdu.commonAddress = request.asdu.commonAddress;
            response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
            response.asdu.causeOfTransmission = cot;
            //create the information objects
            while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
                && it.hasNext())
            {
              dp = it.next();
              try
              {
                IecInfoObject infoObject = new IecInfoObject();
                infoObject.objectAddress = dp.getAddress();
                infoObject.timeTag = dp.getTimestamp();
                infoObject.infoElements = new IecInfoElement[1];
                infoObject.infoElements[0] = new IecInfoElement();
                infoObject.infoElements[0].setQuality(dp.getQuality());
                infoObject.infoElements[0].setBooleanValue(dp.getBooleanValue());
                //add the info object into the ASDU
                response.asdu.infoObjects.add(infoObject);
              }
              catch (Exception ex)
              {
                logger_.error("Can not create IEC message for datapoint: " + dp.getName());
                logger_.error("Exception: " + ex.getMessage());
              }
            }
            //add APDU into the list of responses
            responses.add(response);
          }
        }
       
        //return all ddi (COT=20)
        if (interroGroup == 0 || interroGroup == 2)
        {
          dpList = parent_.data_.getAllDDIPoints();
          logger_.info("Sending all DDI datapoints (as double value). Number of datapoints: " + dpList.size());
 
          it = dpList.iterator();
          while(it.hasNext())
          {
            response = new IecApdu();
            response.format = EIecApduFormat.I_FORMAT;
            //response.asdu.typeId = EIecTypeId.M_DP_NA_1;
            response.asdu.typeId = EIecTypeId.M_DP_TB_1;  //with CP56Time2a
            response.asdu.commonAddress = request.asdu.commonAddress;
            response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
            response.asdu.causeOfTransmission = cot;
            //create the information objects
            while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
                && it.hasNext())
            {
              dp = it.next();
              try
              {
                IecInfoObject infoObject = new IecInfoObject();
                infoObject.objectAddress = dp.getAddress();
                infoObject.timeTag = dp.getTimestamp();
                infoObject.infoElements = new IecInfoElement[1];
                infoObject.infoElements[0] = new IecInfoElement();
                infoObject.infoElements[0].setQuality(dp.getQuality());
                infoObject.infoElements[0].setIntValue(dp.getIntValue());
                //add the info object into the ASDU
                response.asdu.infoObjects.add(infoObject);
              }
              catch (Exception ex)
              {
                logger_.error("Can not create IEC message for datapoint: " + dp.getName());
                logger_.error("Exception: " + ex.getMessage());
              }
            }
            //add APDU into the list of responses
              responses.add(response);
          }
        }
       
        //return all ai including tdi and utc
        if (interroGroup == 0 || interroGroup == 3)
        {
          //return all tdi (COT=20)
          //Note: we transfer TDI value as a measured scaled value (ie. as AI)
          dpList = parent_.data_.getAllTDIPoints();
          logger_.info("Sending all TDI datapoints (as measured scaled value). Number of datapoints: " + dpList.size());
 
          it = dpList.iterator();
          while(it.hasNext())
          {
            response = new IecApdu();
            response.format = EIecApduFormat.I_FORMAT;
            //response.asdu.typeId = EIecTypeId.M_ME_NB_1;
            response.asdu.typeId = EIecTypeId.M_ME_TE_1;  //with CP56Time2a
            response.asdu.commonAddress = request.asdu.commonAddress;
            response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
            response.asdu.causeOfTransmission = cot;
            //create the information objects
            while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
                && it.hasNext())
            {
              dp = it.next();
              try
              {
                IecInfoObject infoObject = new IecInfoObject();
                infoObject.objectAddress = dp.getAddress();
                infoObject.timeTag = dp.getTimestamp();
                infoObject.infoElements = new IecInfoElement[1];
                infoObject.infoElements[0] = new IecInfoElement();
                infoObject.infoElements[0].setQuality(dp.getQuality());
                infoObject.infoElements[0].setIntValue(dp.getIntValue());
                //add the info object into the ASDU
                response.asdu.infoObjects.add(infoObject);
              }
              catch (Exception ex)
              {
                logger_.error("Can not create IEC message for datapoint: " + dp.getName());
                logger_.error("Exception: " + ex.getMessage());
              }
            }
            //add APDU into the list of responses
            responses.add(response);
          }
 
          //return all ai (COT=20)
          dpList = parent_.data_.getAllAIPoints();
          logger_.info("Sending all AI datapoints (as measured scaled value). Number of datapoints: " + dpList.size());
 
          it = dpList.iterator();
          while(it.hasNext())
          {
            response = new IecApdu();
            response.format = EIecApduFormat.I_FORMAT;
            //response.asdu.typeId = EIecTypeId.M_ME_NB_1;
            response.asdu.typeId = EIecTypeId.M_ME_TE_1;  //with CP56Time2a
            response.asdu.commonAddress = request.asdu.commonAddress;
            response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
            response.asdu.causeOfTransmission = cot;
            //create the information objects
            while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
                && it.hasNext())
            {
              dp = it.next();
              try
              {
                IecInfoObject infoObject = new IecInfoObject();
                infoObject.objectAddress = dp.getAddress();
                infoObject.timeTag = dp.getTimestamp();
                infoObject.infoElements = new IecInfoElement[1];
                infoObject.infoElements[0] = new IecInfoElement();
                infoObject.infoElements[0].setQuality(dp.getQuality());
                infoObject.infoElements[0].setIntValue(dp.getIntValue());
                //add the info object into the ASDU
                response.asdu.infoObjects.add(infoObject);
              }
              catch (Exception ex)
              {
                logger_.error("Can not create IEC message for datapoint: " + dp.getName());
                logger_.error("Exception: " + ex.getMessage());
              }
            }
            //add APDU into the list of responses
            responses.add(response);
          }
 
          //return all utc (COT=20)
          //Note: we transfer UTC value as a measured scaled value (ie. as AI)
          dpList = parent_.data_.getAllUTCPoints();
          logger_.info("Sending all UTC datapoints (as measured scaled value). Number of datapoints: " + dpList.size());
 
          it = dpList.iterator();
          while(it.hasNext())
          {
            response = new IecApdu();
            response.format = EIecApduFormat.I_FORMAT;
            //response.asdu.typeId = EIecTypeId.M_ME_NB_1;
            response.asdu.typeId = EIecTypeId.M_ME_TE_1;  //with CP56Time2a
            response.asdu.commonAddress = request.asdu.commonAddress;
            response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
            response.asdu.causeOfTransmission = cot;
            //create the information objects
            while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
                && it.hasNext())
            {
              dp = it.next();
              try
              {
                IecInfoObject infoObject = new IecInfoObject();
                infoObject.objectAddress = dp.getAddress();
                infoObject.timeTag = dp.getTimestamp();
                infoObject.infoElements = new IecInfoElement[1];
                infoObject.infoElements[0] = new IecInfoElement();
                infoObject.infoElements[0].setQuality(dp.getQuality());
                infoObject.infoElements[0].setIntValue(dp.getIntValue());
                //add the info object into the ASDU
                response.asdu.infoObjects.add(infoObject);
              }
              catch (Exception ex)
              {
                logger_.error("Can not create IEC message for datapoint: " + dp.getName());
                logger_.error("Exception: " + ex.getMessage());
              }
            }
            //add APDU into the list of responses
            responses.add(response);
          }
        }
       
        if (interroGroup == 0 || interroGroup == 4)
        {
          //return all mi (COT=20)
          //Note: in real IEC-104 application (in POW subsystem), MI data will normally be
          //      counter data. (Thus, can be retrieved using counter interrogation command)
          dpList = parent_.data_.getAllMIPoints();
          logger_.info("Sending all MI datapoints (as integrated total). Number of datapoints: " + dpList.size());
 
          it = dpList.iterator();
          while(it.hasNext())
          {
            response = new IecApdu();
            response.format = EIecApduFormat.I_FORMAT;
            //response.asdu.typeId = EIecTypeId.M_IT_NA_1;
            response.asdu.typeId = EIecTypeId.M_IT_TB_1;  //with CP56Time2a
            response.asdu.commonAddress = request.asdu.commonAddress;
            response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
            response.asdu.causeOfTransmission = cot;
            //create the information objects
            while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
                && it.hasNext())
            {
              dp = it.next();
              try
              {
                IecInfoObject infoObject = new IecInfoObject();
                infoObject.objectAddress = dp.getAddress();
                infoObject.timeTag = dp.getTimestamp();
                infoObject.infoElements = new IecInfoElement[1];
                infoObject.infoElements[0] = new IecInfoElement();
                infoObject.infoElements[0].setQuality(dp.getQuality());
                infoObject.infoElements[0].setIntValue(dp.getIntValue());
                //add the info object into the ASDU
                response.asdu.infoObjects.add(infoObject);
              }
              catch (Exception ex)
              {
                logger_.error("Can not create IEC message for datapoint: " + dp.getName());
                logger_.error("Exception: " + ex.getMessage());
              }
            }
            //add APDU into the list of responses
            responses.add(response);
          }
        }
       
//        //return all cx (COT=20)
//        //Note: in real IEC-104 application (in POW subsystem), there will never be CX data.
//        dpList = parent_.data_.getAllCXPoints();
//        logger_.info("Sending all CX datapoints. Number of datapoints: " + dpList.size());
//
//        it = dpList.iterator();
//        while(it.hasNext())
//        {
//          //TODO
//        }
       
        //interrogation command END (COT=10)
        logger_.info("Sending interrogation command end (C_IC_NA_1, COT=10).");
        response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_TERMINATE;       
        responses.add(response);
      }
      else if (request.asdu.typeId == EIecTypeId.C_CI_NA_1)
      {
        //counter interrogation command
        logger_.info("Received counter interrogation command (C_CI_NA_1).");

        //reply to data request only if the data transfer has been enabled
        if (!isDataEnabled_)
        {
          logger_.warn("Data transfer is not enabled. It will be ignored.");
          return false;
        }
       
        //ensure the COT=6
        if (request.asdu.causeOfTransmission.value() != EIecCauseOfTransmission.ACTIVATION.value())
        {
          logger_.warn("Cause of Transmission is not ACTIVATION (6). Maybe we should ignore this message?");
          logger_.warn("For now, we will just process it as if the correct COT is set.");
        }
       
        //counter interro command CON (COT=7)
        logger_.info("Sending counter interrogation command confirm (C_CI_NA_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);

        Iterator<DataPoint>   it = null;
        DataPoint        dp = null;
        Collection<DataPoint>   dpList = null;
       
        //return all mi (COT=20)
        dpList = parent_.data_.getAllMIPoints();
        logger_.info("Sending all MI datapoints. Number of datapoints: " + dpList.size());

        it = dpList.iterator();
        while(it.hasNext())
        {
          response = new IecApdu();
          response.format = EIecApduFormat.I_FORMAT;
          //response.asdu.typeId = EIecTypeId.M_IT_NA_1;
          response.asdu.typeId = EIecTypeId.M_IT_TB_1;  //with CP56Time2a
          response.asdu.commonAddress = request.asdu.commonAddress;
          response.asdu.sequenceType = EIecAsduSequenceType.SEQUENCE_OF_OBJECTS;
          response.asdu.causeOfTransmission = EIecCauseOfTransmission.INTERROGATED_STATION;
          //create the information objects
          while(response.asdu.infoObjects.size() < MAX_INFO_OBJECT_ARRAY
              && it.hasNext())
          {
            dp = it.next();
            try
            {
              IecInfoObject infoObject = new IecInfoObject();
              infoObject.objectAddress = dp.getAddress();
              infoObject.timeTag = dp.getTimestamp();
              infoObject.infoElements = new IecInfoElement[1];
              infoObject.infoElements[0] = new IecInfoElement();
              infoObject.infoElements[0].setQuality(dp.getQuality());
              infoObject.infoElements[0].setIntValue(dp.getIntValue());
              //add the info object into the ASDU
              response.asdu.infoObjects.add(infoObject);
            }
            catch (Exception ex)
            {
              logger_.error("Can not create IEC message for datapoint: " + dp.getName());
              logger_.error("Exception: " + ex.getMessage());
            }
          }
          //add APDU into the list of responses
          responses.add(response);
        }
       
        //counter interro command END (COT=10)
        logger_.info("Sending counter interrogation command end (C_CI_NA_1, COT=10).");
        response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_TERMINATE;       
        responses.add(response);
      }
      else if (request.asdu.typeId == EIecTypeId.C_SC_NA_1)
      {
        //single command
        int address = request.asdu.infoObjects.get(0).objectAddress;
        boolean value = false;
        try
        {
          value = request.asdu.infoObjects.get(0).infoElements[0].getBooleanValue();
          logger_.info("Received single command (C_SC_NA_1). "
                  + "Address: " + address + ". Value: " + value + ".");
        }
        catch (Exception ex)
        {
          logger_.info("Received single command (C_SC_NA_1). "
              + "Address: " + address + ". Invalid value!");
          logger_.error("Invalid single command value. Exception: " + ex.getMessage());
          return false;
        }
        //for completeness
        @SuppressWarnings("unused")
        int qualifier = request.asdu.infoObjects.get(0).infoElements[0].getQualifier();

        //reply to data request only if the data transfer has been enabled
        if (!isDataEnabled_)
        {
          logger_.warn("Data transfer is not enabled. It will be ignored.");
          return false;
        }
       
        //ensure the COT=6
        if (request.asdu.causeOfTransmission.value() != EIecCauseOfTransmission.ACTIVATION.value())
        {
          logger_.warn("Cause of Transmission is not ACTIVATION (6). Maybe we should ignore this message?");
          logger_.warn("For now, we will just process it as if the correct COT is set.");
        }
       
        //single command CON (COT=7)
        logger_.info("Sending single command confirm (C_SC_NA_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);
       
        //pass the control
        //TODO: handle the command qualifier
        parent_.parent_.receiveControl(address, value);
       
        //single command END (COT=10)
        logger_.info("Sending single command end (C_SC_NA_1, COT=10).");
        response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_TERMINATE;       
        responses.add(response);
      }
      else if (request.asdu.typeId == EIecTypeId.C_DC_NA_1)
      {
        //double command
        int address = request.asdu.infoObjects.get(0).objectAddress;
        int value = 0;
        try
        {
          value = request.asdu.infoObjects.get(0).infoElements[0].getIntValue();
          logger_.info("Received double command (C_DC_NA_1). "
                  + "Address: " + address + ". Value: " + value + ".");
        }
        catch (Exception ex)
        {
          logger_.info("Received double command (C_DC_NA_1). "
              + "Address: " + address + ". Invalid value!");
          logger_.error("Invalid double command value. Exception: " + ex.getMessage());
          return false;
        }
        //for completeness
        @SuppressWarnings("unused")
        int qualifier = request.asdu.infoObjects.get(0).infoElements[0].getQualifier();

        //reply to data request only if the data transfer has been enabled
        if (!isDataEnabled_)
        {
          logger_.warn("Data transfer is not enabled. It will be ignored.");
          return false;
        }
       
        //ensure the COT=6
        if (request.asdu.causeOfTransmission.value() != EIecCauseOfTransmission.ACTIVATION.value())
        {
          logger_.warn("Cause of Transmission is not ACTIVATION (6). Maybe we should ignore this message?");
          logger_.warn("For now, we will just process it as if the correct COT is set.");
        }
       
        //double command CON (COT=7)
        logger_.info("Sending double command confirm (C_DC_NA_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);
       
        //pass the control
        //NOTE: we treat a double command as two pulse commands
        //TODO: handle the command qualifier
        //pass the control (first pulse command)
        boolean boolVal = ((value & 0x01) > 0);
        parent_.parent_.receiveControl(address, boolVal);
        //pass the control (second pulse command)
        boolVal = ((value & 0x02) > 0);
        parent_.parent_.receiveControl(address, boolVal);
       
        //double command END (COT=10)
        logger_.info("Sending double command end (C_DC_NA_1, COT=10).");
        response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_TERMINATE;       
        responses.add(response);
      }
      else if (request.asdu.typeId == EIecTypeId.C_SE_NA_1)
      {
        //set point (normalized)
        int address = request.asdu.infoObjects.get(0).objectAddress;
        int value = 0;
        try
        {
          value = request.asdu.infoObjects.get(0).infoElements[0].getIntValue();
          logger_.info("Received set point - normalized (C_SE_NA_1). "
                  + "Address: " + address + ". Value: " + value + ".");
        }
        catch (Exception ex)
        {
          logger_.info("Received set point - normalized (C_SE_NA_1). "
              + "Address: " + address + ". Invalid value!");
          logger_.error("Invalid set point (normalized) value. Exception: " + ex.getMessage());
          return false;
        }
        //for completeness
        @SuppressWarnings("unused")
        int qualifier = request.asdu.infoObjects.get(0).infoElements[0].getQualifier();

        //reply to data request only if the data transfer has been enabled
        if (!isDataEnabled_)
        {
          logger_.warn("Data transfer is not enabled. It will be ignored.");
          return false;
        }
       
        //ensure the COT=6
        if (request.asdu.causeOfTransmission.value() != EIecCauseOfTransmission.ACTIVATION.value())
        {
          logger_.warn("Cause of Transmission is not ACTIVATION (6). Maybe we should ignore this message?");
          logger_.warn("For now, we will just process it as if the correct COT is set.");
        }
       
        //set point CON (COT=7)
        logger_.info("Sending set point, normalized, command confirm (C_SE_NA_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);
       
        //pass the control
        //TODO
       
        //set point END (COT=10)
        logger_.info("Sending set point, normalized, command end (C_SE_NA_1, COT=10).");
        response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_TERMINATE;       
        responses.add(response);
      }
      else if (request.asdu.typeId == EIecTypeId.C_SE_NB_1)
      {
        //set point (scaled)
        int address = request.asdu.infoObjects.get(0).objectAddress;
        int value = 0;
        try
        {
          value = request.asdu.infoObjects.get(0).infoElements[0].getIntValue();
          logger_.info("Received set point - scaled (C_SE_NB_1). "
                  + "Address: " + address + ". Value: " + value + ".");
        }
        catch (Exception ex)
        {
          logger_.info("Received set point - scaled (C_SE_NB_1). "
              + "Address: " + address + ". Invalid value!");
          logger_.error("Invalid set point (scaled) value. Exception: " + ex.getMessage());
          return false;
        }
        //for completeness
        @SuppressWarnings("unused")
        int qualifier = request.asdu.infoObjects.get(0).infoElements[0].getQualifier();
       
        //reply to data request only if the data transfer has been enabled
        if (!isDataEnabled_)
        {
          logger_.warn("Data transfer is not enabled. It will be ignored.");
          return false;
        }
       
        //ensure the COT=6
        if (request.asdu.causeOfTransmission.value() != EIecCauseOfTransmission.ACTIVATION.value())
        {
          logger_.warn("Cause of Transmission is not ACTIVATION (6). Maybe we should ignore this message?");
          logger_.warn("For now, we will just process it as if the correct COT is set.");
        }
       
        //set point CON (COT=7)
        logger_.info("Sending set point, scaled, command confirm (C_SE_NB_1, COT=7).");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_CONFIRM;       
        responses.add(response);
       
        //pass the control
        //TODO: handle the command qualifier
        parent_.parent_.receiveControl(address, value);
       
        //set point END (COT=10)
        logger_.info("Sending set point, scaled, command end (C_SE_NB_1, COT=10).");
        response = new IecApdu();
        response.format = EIecApduFormat.I_FORMAT;
        response.asdu = new IecAsdu(request.asdu);
        response.asdu.causeOfTransmission = EIecCauseOfTransmission.ACTIVATION_TERMINATE;       
        responses.add(response);
      }
      else
      {
        //unsupported asdu type
        logger_.warn("Received I_FORMAT with ASDU type " + request.asdu.typeId.toString()
              + ". Ignored!");
      }
    }
    else if (request.format == EIecApduFormat.U_FORMAT)
    {
      if (request.uFormatType == EIecApduUFormatType.STARTDT_ACT)
      {
        logger_.info("Received STARTDT_ACT. Data transfer will be enabled.");

        //enable data transfer
        isDataEnabled_ = true;
       
        //create STARTDT_CON response
        logger_.info("Sending STARTDT_CON. Data transfer has been enabled.");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.U_FORMAT;
        response.uFormatType = EIecApduUFormatType.STARTDT_CON;
        responses.add(response);
      }
      else if (request.uFormatType == EIecApduUFormatType.STARTDT_CON)
      {
        //ignore. should never receive this
        logger_.warn("Received STARTDT_CON message. Ignored!");
      }
      else if (request.uFormatType == EIecApduUFormatType.STOPDT_ACT)
      {
        logger_.info("Received STOPDT_ACT. Data transfer will be disabled.");

        //disable data transfer
        isDataEnabled_ = false;
       
        //create STOPDT_CON response
        logger_.info("Received STOPDT_CON. Data transfer has been disabled.");
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.U_FORMAT;
        response.uFormatType = EIecApduUFormatType.STOPDT_CON;
        responses.add(response);
      }
      else if (request.uFormatType == EIecApduUFormatType.STOPDT_CON)
      {
        //ignore. should never receive this
        logger_.warn("Received STOPDT_CON message. Ignored!");
      }
      else if (request.uFormatType == EIecApduUFormatType.TESTFR_ACT)
      {
        logger_.info("Received TESTFR_ACT. Sending TESTFR_CON response.");
        //create TESTFR_CON response
        IecApdu response = new IecApdu();
        response.format = EIecApduFormat.U_FORMAT;
        response.uFormatType = EIecApduUFormatType.TESTFR_CON;
        responses.add(response);
      }
      else if (request.uFormatType == EIecApduUFormatType.TESTFR_CON)
View Full Code Here

      NDC.push("RECV");

      logger_.info("Starting receiving thread...");       

      //reusable iec message structure
      IecApdu request = new IecApdu();
     
      int   errorCounter = 0;
      boolean status = false;     
      long  timestamp = Calendar.getInstance().getTimeInMillis();
      //Date  curtime = new Date();
     
      while (keepReceiving_ && errorCounter < ERROR_THRESHOLD &&
          parent_.isRunning() && sock_.isConnected() )
      { 
        //check for idle timeout
        if((Calendar.getInstance().getTimeInMillis() - timestamp)
            > IecServer.DEF_IDLE_TIMEOUT)
        {
          //timeout
          logger_.info("Receiving thread has timed out. Stopping...")
          keepReceiving_ = false;
          break;
        }
       
        //clear the request structure
        request.reset();
       
        //get the request
        status = false;
        try
        {
View Full Code Here

TOP

Related Classes of tcg.scada.iec.IecApdu

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.