Example usage for java.lang Short reverseBytes

List of usage examples for java.lang Short reverseBytes

Introduction

In this page you can find the example usage for java.lang Short reverseBytes.

Prototype

@HotSpotIntrinsicCandidate
public static short reverseBytes(short i) 

Source Link

Document

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value.

Usage

From source file:org.apache.kylin.common.util.Bytes.java

/**
 * Put a short value out to the specified byte array position (Unsafe).
 *
 * @param bytes  the byte array/* w  ww  . j av  a 2 s  .  c o  m*/
 * @param offset position in the array
 * @param val    short to write out
 * @return incremented offset
 */
public static int putShortUnsafe(byte[] bytes, int offset, short val) {
    if (org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.littleEndian) {
        val = Short.reverseBytes(val);
    }
    org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.theUnsafe.putShort(bytes,
            (long) offset
                    + org.apache.kylin.common.util.Bytes.LexicographicalComparerHolder.UnsafeComparer.BYTE_ARRAY_BASE_OFFSET,
            val);
    return offset + SIZEOF_SHORT;
}

From source file:com.zpci.firstsignhairclipdemo.MainActivity.java

public void savewavefile(byte[] ra) {
    //prepend 44 byte wave header to data

    int sampleRate = 8000; // audio sample rate is 8000 SPS
    int numSecs = ra.length / sampleRate; // number of seconds of audio to record
    int samples = sampleRate * numSecs; // number of samples in file
    short bitsPerSample = 8; // one byte per sample
    int filesize = samples + 44; // check this?
    int fmtChunkSize = 16; // size of 'fmt' chunk
    short channels = 1; // mono
    int byteRate = sampleRate * channels * bitsPerSample / 8; // will be 8K for us
    short format = 1; // 1 == uncompressed pcm
    short blockalign = (short) (channels * bitsPerSample / 8); // bytes per sample
    int audiolen = samples * channels * bitsPerSample / 8; // length of audio in bytes

    try {/*from w w  w.  j  a v a 2 s .c o  m*/
        //OutputStream os = openFileOutput("diagaudio.wav", Context.MODE_PRIVATE);
        String state = Environment.getExternalStorageState();
        Log.d(TAG, "External storage state: " + state);
        if (Environment.MEDIA_MOUNTED.equals(state)) {

            //create firstsign directory
            File rootPath = new File(Environment.getExternalStorageDirectory(), "firstsign");
            if (!rootPath.exists()) {
                rootPath.mkdirs();
                Log.d(TAG, "mkdirs");
            }
            File file = new File(rootPath, "hairclipaudio.wav");
            file.createNewFile();
            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream wf = new DataOutputStream(bos);

            wf.write("RIFF".getBytes());
            wf.writeInt(Integer.reverseBytes(filesize - 8));
            wf.write("WAVE".getBytes());
            wf.write("fmt ".getBytes());
            wf.writeInt(Integer.reverseBytes(fmtChunkSize));
            wf.writeShort(Short.reverseBytes(format));
            wf.writeShort(Short.reverseBytes(channels));
            wf.writeInt(Integer.reverseBytes(sampleRate));
            wf.writeInt(Integer.reverseBytes(byteRate));
            wf.writeShort(Short.reverseBytes(blockalign));
            wf.writeShort(Short.reverseBytes(bitsPerSample));
            wf.write("data".getBytes());
            wf.writeInt(Integer.reverseBytes(audiolen));
            wf.write(ra);

            wf.close();
            bos.close();
            os.close();

            Log.d(TAG, "wavefile write complete");
        } else {
            Toast.makeText(this, "SDCard not mounted", Toast.LENGTH_LONG).show();
        } //what do i do?

    } catch (Exception e) {
        Log.e(TAG, "exception in savewavefile");
        e.printStackTrace();
    }

}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

@Override
public short configureEndPointSync(long timeout, SimpleDescriptor desc)
        throws IOException, Exception, GatewayException {
    ByteArrayObject _res = new ByteArrayObject();
    _res.addByte(desc.getEndPoint().byteValue());/* End Point */
    _res.addBytesShort(Short.reverseBytes(desc.getApplicationProfileIdentifier().shortValue()), 2);
    _res.addBytesShort(Short.reverseBytes(desc.getApplicationDeviceIdentifier().shortValue()), 2);
    _res.addByte(desc.getApplicationDeviceVersion().byteValue());/* DeviceVersion */
    _res.addByte((byte) desc.getApplicationInputCluster().size());/* ClusterInputSize */
    if (desc.getApplicationInputCluster().size() > 0) {
        for (Integer x : desc.getApplicationInputCluster())
            _res.addBytesShort(Short.reverseBytes(x.shortValue()), 2);
    }//from  w w w . ja  v  a  2s. co m
    _res.addByte((byte) desc.getApplicationOutputCluster().size());/* ClusterOutputSize */
    if (desc.getApplicationOutputCluster().size() > 0) {
        for (Integer x : desc.getApplicationOutputCluster())
            _res.addBytesShort(Short.reverseBytes(x.shortValue()), 2);
    }
    _res.addByte((byte) 0x01);/* Maximum Window Size */

    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.APSRegisterEndPointRequest);/*
                                                                                          * StartSequence
                                                                                          * +
                                                                                          * Control
                                                                                          */
    /* APS-RegisterEndPoint.Request */
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("Configure EndPoint command:" + _res.ToHexString());
    }
    short _endPoint = 0;

    ParserLocker lock = new ParserLocker();
    lock.setType(TypeMessage.CONFIGURE_END_POINT);
    Status status = null;
    try {
        synchronized (listLocker) {
            listLocker.add(lock);
        }
        addToSendDataQueue(_res);
        synchronized (lock) {
            try {
                lock.wait(timeout);
            } catch (InterruptedException e) {

            }
        }
        status = lock.getStatus();
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    } catch (Exception e) {
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    }
    if (status.getCode() == ParserLocker.INVALID_ID) {

        if (gal.getPropertiesManager().getDebugEnabled()) {
            logger.error("Timeout expired in Configure End Point");
        }
        throw new GatewayException("Timeout expired in Configure End Point");
    } else {
        if (status.getCode() != 0) {
            if (gal.getPropertiesManager().getDebugEnabled()) {
                logger.info("Returned Status: " + status.getCode());
            }
            throw new GatewayException("Error on  APS-RegisterEndPoint.Request. Status code:" + status.getCode()
                    + " Status Message: " + status.getMessage());
        } else
            _endPoint = desc.getEndPoint();
    }
    return _endPoint;
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

public ByteArrayObject makeByteArrayFromApsMessage(APSMessage apsMessage) throws Exception {
    byte[] data = apsMessage.getData();

    ByteArrayObject _res = new ByteArrayObject();
    byte dam = apsMessage.getDestinationAddressMode().byteValue();
    _res.addByte(dam);/*from   ww  w. ja va 2s.  com*/
    Address address = apsMessage.getDestinationAddress();
    byte[] _reversed = null;
    switch (dam) {
    // TODO Control those address modes!
    case GatewayConstants.ADDRESS_MODE_SHORT:
        byte[] networkAddress = DataManipulation.toByteVect(address.getNetworkAddress(), 8);
        _reversed = DataManipulation.reverseBytes(networkAddress);
        for (byte b : _reversed)
            _res.addByte(b);
        break;
    case GatewayConstants.EXTENDED_ADDRESS_MODE:
        byte[] ieeeAddress = DataManipulation.toByteVect(address.getIeeeAddress(), 8);
        _reversed = DataManipulation.reverseBytes(ieeeAddress);
        for (byte b : _reversed)
            _res.addByte(b);
        break;
    case GatewayConstants.ADDRESS_MODE_ALIAS:
        // TODO
        throw new UnsupportedOperationException("Address Mode Alias");
    default:
        throw new Exception("Address Mode undefined!");

    }

    _res.addByte((byte) apsMessage.getDestinationEndpoint());

    _res.addBytesShort(Short.reverseBytes(apsMessage.getProfileID().shortValue()), 2);

    _res.addBytesShort(Short.reverseBytes((short) apsMessage.getClusterID()), 2);

    _res.addByte((byte) apsMessage.getSourceEndpoint());

    if (data.length > 0x64) {
        throw new Exception("ASDU length must 0x64 or less in length");
    } else {
        _res.addByte((byte) data.length);

    }

    for (Byte b : data)
        _res.addByte(b);

    TxOptions txo = apsMessage.getTxOptions();
    int bitmap = 0x00;
    if (txo.isSecurityEnabled()) {
        bitmap |= 0x01;
    }
    if (txo.isUseNetworkKey()) {
        bitmap |= 0x02;
    }
    if (txo.isAcknowledged()) {
        bitmap |= 0x04;
    }
    if (txo.isPermitFragmentation()) {
        bitmap |= 0x08;
    }
    _res.addByte((byte) bitmap);

    _res.addByte((byte) apsMessage.getRadius());

    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.APSDEDataRequest);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("Write APS on: " + System.currentTimeMillis() + " Message:" + _res.ToHexString());
    }
    return _res;
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

private Status WriteSasSync(long timeout, StartupAttributeInfo sai) throws InterruptedException, Exception {
    // TODO CHECK
    if (sai.getChannelMask() == null)
        sai = gal.getPropertiesManager().getSturtupAttributeInfo();

    LogicalType devType = sai.getDeviceType();

    ByteArrayObject res = new ByteArrayObject();
    res.addBytesShort(Short.reverseBytes(sai.getShortAddress().shortValue()), 2);

    /* Extended PanID */
    byte[] ExtendedPaniId = DataManipulation.toByteVect(sai.getExtendedPANId(), 8);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("Extended PanID:" + DataManipulation.convertBytesToString(ExtendedPaniId));
    }/* w w  w.j a v  a  2  s  .  c om*/

    for (byte b : DataManipulation.reverseBytes(ExtendedPaniId))
        res.addByte(b);

    /* Extended APS Use Extended PAN Id */
    byte[] APSUseExtendedPANId = DataManipulation.toByteVect(BigInteger.ZERO, 8);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("APS Use Extended PAN Id:" + DataManipulation.convertBytesToString(APSUseExtendedPANId));
    }
    for (byte b : DataManipulation.reverseBytes(APSUseExtendedPANId))
        res.addByte(b);
    res.addBytesShort(Short.reverseBytes(sai.getPANId().shortValue()), 2);
    byte[] _channel = Utils.buildChannelMask(sai.getChannelMask().shortValue());

    if (gal.getPropertiesManager().getDebugEnabled())
        logger.info("Channel readed from PropertiesManager:" + sai.getChannelMask());

    if (gal.getPropertiesManager().getDebugEnabled())
        DataManipulation.logArrayHexRadix("Channel after conversion", _channel);

    for (byte x : DataManipulation.reverseBytes(_channel))
        res.addByte(x);

    res.addByte(sai.getProtocolVersion().byteValue());
    res.addByte(sai.getStackProfile().byteValue());
    res.addByte(sai.getStartupControl().byteValue());

    /* TrustCenterAddress */
    byte[] TrustCenterAddress = DataManipulation.toByteVect(sai.getTrustCenterAddress(), 8);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("TrustCenterAddress:" + DataManipulation.convertBytesToString(TrustCenterAddress));
    }
    for (byte b : DataManipulation.reverseBytes(TrustCenterAddress))
        res.addByte(b);

    /* TrustCenterMasterKey */
    byte[] TrustCenterMasterKey = (devType == LogicalType.COORDINATOR) ? sai.getTrustCenterMasterKey()
            : DataManipulation.toByteVect(BigInteger.ZERO, 16);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("TrustCenterMasterKey:" + DataManipulation.convertBytesToString(TrustCenterMasterKey));
    }
    for (byte b : DataManipulation.reverseBytes(TrustCenterMasterKey))
        res.addByte(b);

    /* NetworKey */
    byte[] NetworKey = (devType == LogicalType.COORDINATOR) ? sai.getNetworkKey()
            : DataManipulation.toByteVect(BigInteger.ZERO, 16);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("NetworKey:" + DataManipulation.convertBytesToString(NetworKey));
    }
    for (byte b : DataManipulation.reverseBytes(NetworKey))
        res.addByte(b);

    res.addByte((sai.isUseInsecureJoin()) ? ((byte) 0x01) : ((byte) 0x00));

    /* PreconfiguredLinkKey */
    byte[] PreconfiguredLinkKey = sai.getPreconfiguredLinkKey();
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("PreconfiguredLinkKey:" + DataManipulation.convertBytesToString(PreconfiguredLinkKey));
    }
    for (byte b : PreconfiguredLinkKey)
        res.addByte(b);

    res.addByte(sai.getNetworkKeySeqNum().byteValue());
    res.addByte((byte) 0x01);

    res.addBytesShort(Short.reverseBytes(sai.getNetworkManagerAddress().shortValue()), 2);
    res.addByte(sai.getScanAttempts().byteValue());

    res.addBytesShort(sai.getTimeBetweenScans().shortValue(), 2);

    res.addBytesShort(Short.reverseBytes(sai.getRejoinInterval().shortValue()), 2);

    res.addBytesShort(Short.reverseBytes(sai.getMaxRejoinInterval().shortValue()), 2);

    res.addBytesShort(Short.reverseBytes(sai.getIndirectPollRate().shortValue()), 2);

    res.addByte(sai.getParentRetryThreshold().byteValue());

    res.addByte((sai.isConcentratorFlag()) ? ((byte) 0x01) : ((byte) 0x00));

    res.addByte(sai.getConcentratorRadius().byteValue());

    res.addByte(sai.getConcentratorDiscoveryTime().byteValue());

    res = Set_SequenceStart_And_FSC(res, FreescaleConstants.BlackBoxWriteSAS);
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("WriteSas Command:" + res.ToHexString());
    }

    ParserLocker lock = new ParserLocker();
    lock.setType(TypeMessage.WRITE_SAS);
    Status status = null;
    try {

        synchronized (listLocker) {
            listLocker.add(lock);
        }
        addToSendDataQueue(res);
        synchronized (lock) {
            try {
                lock.wait(timeout);
            } catch (InterruptedException e) {

            }
        }
        status = lock.getStatus();
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    } catch (Exception e) {
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    }
    if (status.getCode() == ParserLocker.INVALID_ID) {
        if (gal.getPropertiesManager().getDebugEnabled()) {
            logger.error("Timeout expired in write sas");
        }
        throw new GatewayException("Timeout expired in write sas");
    } else {
        if (status.getCode() != 0) {
            if (gal.getPropertiesManager().getDebugEnabled()) {
                logger.info("Returned Status: " + status.getCode());
            }
            throw new GatewayException("Error on BlackBox.WriteSAS. Status code:" + status.getCode()
                    + " Status Message: " + status.getMessage());
        }
        return status;
    }
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

@Override
public Status permitJoinSync(long timeout, Address addrOfInterest, short duration, byte TCSignificance)
        throws IOException, Exception, GatewayException {
    ByteArrayObject _res = new ByteArrayObject();
    _res.addBytesShort(Short.reverseBytes(addrOfInterest.getNetworkAddress().shortValue()), 2);/*
                                                                                               * Short
                                                                                               * Network
                                                                                               * Address
                                                                                               */
    _res.addByte((byte) duration);/* Duration */
    _res.addByte(TCSignificance);/* TCSignificant */
    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.NLMEPermitJoiningRequest);/*
                                                                                        * StartSequence
                                                                                        * +
                                                                                        * Control
                                                                                        */
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("Permit Join command:" + _res.ToHexString());
    }//from   www  .j av  a2  s .c  o m
    ParserLocker lock = new ParserLocker();
    lock.setType(TypeMessage.PERMIT_JOIN);
    Status status = null;
    try {
        synchronized (listLocker) {
            listLocker.add(lock);
        }
        addToSendDataQueue(_res);
        synchronized (lock) {
            try {
                lock.wait(timeout);
            } catch (InterruptedException e) {

            }
        }
        status = lock.getStatus();
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    } catch (Exception e) {
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }

    }
    if (status.getCode() == ParserLocker.INVALID_ID)
        throw new GatewayException("Timeout expired in Permit Join");
    else {
        if (status.getCode() != 0)
            throw new GatewayException("Error on ZDP-Mgmt_Permit_Join.Request. Status code:" + status.getCode()
                    + " Status Message: " + status.getMessage());

    }
    return status;
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

@Override
public Status permitJoinAllSync(long timeout, Address addrOfInterest, short duration, byte TCSignificance)
        throws IOException, Exception {
    ByteArrayObject _res = new ByteArrayObject();
    _res.addBytesShort(Short.reverseBytes(addrOfInterest.getNetworkAddress().shortValue()), 2);/*
                                                                                               * Short
                                                                                               * Network
                                                                                               * Address
                                                                                               */
    _res.addByte((byte) duration);/* Duration */
    _res.addByte(TCSignificance);/* TCSignificant */
    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.NLMEPermitJoiningRequest);/*
                                                                                        * StartSequence
                                                                                        * +
                                                                                        * Control
                                                                                        */
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("Permit Join command:" + _res.ToHexString());
    }/*from  w  w  w . ja  va 2  s .c om*/

    addToSendDataQueue(_res);
    Status status = new Status();
    status.setCode((short) GatewayConstants.SUCCESS);

    return status;
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

public BigInteger readExtAddress(long timeout, short shortAddress) throws GatewayException, Exception {
    ByteArrayObject _res = new ByteArrayObject();
    _res.addBytesShort(Short.reverseBytes(shortAddress), 2);/*
                                                            * Short Network
                                                            * Address
                                                            */
    _res.addBytesShort(Short.reverseBytes(shortAddress), 2);/*
                                                            * Short Network
                                                            * Address
                                                            */
    _res.addByte((byte) 0x01);/* Request Type */
    _res.addByte((byte) 0x00);/* StartIndex */

    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.ZDPIeeeAddrRequest);// StartSequence
    // +/*ww w .j  av  a2  s.com*/
    // Control
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("ZDP-IEEE_addr.Request.Request:" + _res.ToHexString());
        System.out.println("ZDP-IEEE_addr.Request.Request:" + _res.ToHexString());
    }

    ParserLocker lock = new ParserLocker();
    lock.setType(TypeMessage.READ_IEEE_ADDRESS);
    Status status = null;
    try {
        synchronized (listLocker) {
            listLocker.add(lock);
        }
        addToSendDataQueue(_res);
        synchronized (lock) {
            try {
                lock.wait(timeout);
            } catch (InterruptedException e) {

            }
        }
        status = lock.getStatus();
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    } catch (Exception e) {
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    }
    if (status.getCode() == ParserLocker.INVALID_ID) {
        if (gal.getPropertiesManager().getDebugEnabled()) {
            logger.error("Timeout expired in ZDP-IEEE_addr.Request");
        }
        throw new GatewayException("Timeout expired in ZDP-IEEE_addr.Request");
    } else {
        if (status.getCode() != 0) {
            if (gal.getPropertiesManager().getDebugEnabled()) {
                logger.info("Returned Status: " + status.getCode());
            }
            throw new GatewayException("Error on ZDP-IEEE_addr.Request. Status code:" + status.getCode()
                    + " Status Message: " + status.getMessage());
        } else
            return ((BigInteger) lock.get_objectOfResponse());
    }
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

@Override
public NodeDescriptor getNodeDescriptorSync(long timeout, Address addrOfInterest)
        throws IOException, Exception, GatewayException {

    ByteArrayObject _res = new ByteArrayObject();

    _res.addBytesShort(Short.reverseBytes(addrOfInterest.getNetworkAddress().shortValue()), 2);/*
                                                                                               * Short
                                                                                               * Network
                                                                                               * Address
                                                                                               */
    _res.addBytesShort(Short.reverseBytes(addrOfInterest.getNetworkAddress().shortValue()), 2);/*
                                                                                               * Short
                                                                                               * Network
                                                                                               * Address
                                                                                               */
    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.ZDPNodeDescriptorRequest);/*
                                                                                        * StartSequence
                                                                                        * +
                                                                                        * Control
                                                                                        */
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("ZDP-NodeDescriptor.Request:" + _res.ToHexString());
    }/*from www.  j a  v a2s . co m*/

    ParserLocker lock = new ParserLocker();
    lock.setType(TypeMessage.NODE_DESCRIPTOR);
    String __Key = String.format("%04X", addrOfInterest.getNetworkAddress());
    lock.set_Key(__Key);
    Status status = null;
    try {
        synchronized (listLocker) {
            listLocker.add(lock);
        }
        addToSendDataQueue(_res);
        synchronized (lock) {
            try {
                lock.wait(timeout);
            } catch (InterruptedException e) {

            }
        }
        status = lock.getStatus();
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    } catch (Exception e) {
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    }

    if (status.getCode() == ParserLocker.INVALID_ID) {

        throw new GatewayException(
                "Timeout expired in ZDP-NodeDescriptor.Request:" + addrOfInterest.getNetworkAddress());

    } else {
        if (status.getCode() != 0) {
            if (gal.getPropertiesManager().getDebugEnabled()) {
                logger.info("Returned Status: " + status.getCode());
            }
            throw new GatewayException("Error on ZDP-NodeDescriptor.Request. Status code:" + status.getCode()
                    + " Status Message: " + status.getMessage());

        } else
            return (NodeDescriptor) lock.get_objectOfResponse();
    }
}

From source file:org.energy_home.jemma.javagal.layers.data.implementations.IDataLayerImplementation.DataFreescale.java

@Override
public List<Short> startServiceDiscoverySync(long timeout, Address aoi) throws Exception {
    if (gal.getPropertiesManager().getDebugEnabled())
        logger.info("startServiceDiscoverySync Timeout:" + timeout);
    ByteArrayObject _res = new ByteArrayObject();
    _res.addBytesShort(Short.reverseBytes(aoi.getNetworkAddress().shortValue()), 2);/*
                                                                                    * Short
                                                                                    * Network
                                                                                    * Address
                                                                                    */
    _res.addBytesShort(Short.reverseBytes(aoi.getNetworkAddress().shortValue()), 2);/*
                                                                                    * Short
                                                                                    * Network
                                                                                    * Address
                                                                                    */
    _res = Set_SequenceStart_And_FSC(_res, FreescaleConstants.ZDPActiveEpRequest);/*
                                                                                  * StartSequence
                                                                                  * +
                                                                                  * Control
                                                                                  */
    if (gal.getPropertiesManager().getDebugEnabled()) {
        logger.info("ZDP-Active_EP_req.Request:" + _res.ToHexString());
    }/*  ww  w .j  ava 2 s . c  o m*/

    ParserLocker lock = new ParserLocker();
    lock.setType(TypeMessage.ACTIVE_EP);
    lock.set_Key(String.format("%04X", aoi.getNetworkAddress()));
    Status status = null;
    try {
        synchronized (listLocker) {
            listLocker.add(lock);
        }
        addToSendDataQueue(_res);
        synchronized (lock) {
            try {
                lock.wait(timeout);
            } catch (InterruptedException e) {

            }
        }
        status = lock.getStatus();
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    } catch (Exception e) {
        synchronized (listLocker) {
            if (listLocker.contains(lock))
                listLocker.remove(lock);
        }
    }
    if (status.getCode() == ParserLocker.INVALID_ID) {
        if (gal.getPropertiesManager().getDebugEnabled()) {
            logger.error("Timeout expired in ZDP-Active_EP_req.Request");
        }

        throw new GatewayException("Timeout expired in ZDP-Active_EP_req.Request");
    } else {
        if (status.getCode() != 0) {
            throw new GatewayException("Error on ZDP-Active_EP_req.Request. Status code:" + status.getCode()
                    + " Status Message: " + status.getMessage());

        } else {
            return (List<Short>) lock.get_objectOfResponse();
        }
    }

}