Search in sources :

Example 1 with ByteArray

use of com.zsmartsystems.zigbee.zcl.field.ByteArray in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeOtaServer method sendImageBlock.

/**
 * Sends an image block to the client
 *
 * @param fileOffset the offset into the {@link ZigBeeOtaFile} to send the block
 * @param maximumDataSize the maximum data size the client can accept
 * @return the number of bytes sent
 */
private int sendImageBlock(int fileOffset, int maximumDataSize) {
    ByteArray imageData = otaFile.getImageData(fileOffset, Math.min(dataSize, maximumDataSize));
    logger.debug("{} OTA Data: Sending {} bytes at offset {}", cluster.getZigBeeAddress(), imageData.size(), fileOffset);
    cluster.imageBlockResponse(ZclStatus.SUCCESS, otaFile.getManufacturerCode(), otaFile.getImageType(), otaFile.getFileVersion(), fileOffset, imageData);
    return imageData.size();
}
Also used : ByteArray(com.zsmartsystems.zigbee.zcl.field.ByteArray)

Example 2 with ByteArray

use of com.zsmartsystems.zigbee.zcl.field.ByteArray in project com.zsmartsystems.zigbee by zsmartsystems.

the class DefaultDeserializer method readZigBeeType.

@Override
public /**
 * {@inheritDoc}
 */
Object readZigBeeType(ZclDataType type) {
    if (index == payload.length) {
        return null;
    }
    Object[] value = new Object[1];
    switch(type) {
        case BOOLEAN:
            value[0] = payload[index++] == 0 ? false : true;
            break;
        case CHARACTER_STRING:
        case OCTET_STRING:
            int size = payload[index++];
            if (size == 255) {
                value[0] = null;
                break;
            }
            int length = size;
            for (int cnt = 0; cnt < size; cnt++) {
                if (payload[index + cnt] == 0) {
                    length = cnt;
                }
            }
            value[0] = new String(payload, index, length);
            index += size;
            break;
        case ENDPOINT:
        case BITMAP_8_BIT:
        case DATA_8_BIT:
        case ENUMERATION_8_BIT:
            value[0] = Integer.valueOf((byte) payload[index++] & 0xFF);
            break;
        case EXTENDED_PANID:
            int[] panId = new int[8];
            for (int iCnt = 7; iCnt >= 0; iCnt--) {
                panId[iCnt] = payload[index + iCnt];
            }
            index += 8;
            value[0] = new ExtendedPanId(panId);
            break;
        case IEEE_ADDRESS:
            int[] address = new int[8];
            for (int iCnt = 7; iCnt >= 0; iCnt--) {
                address[iCnt] = payload[index + iCnt];
            }
            index += 8;
            value[0] = new IeeeAddress(address);
            break;
        case N_X_ATTRIBUTE_INFORMATION:
            break;
        case N_X_ATTRIBUTE_RECORD:
            break;
        case N_X_ATTRIBUTE_REPORT:
            break;
        case N_X_ATTRIBUTE_REPORTING_CONFIGURATION_RECORD:
            break;
        case N_X_ATTRIBUTE_SELECTOR:
            break;
        case N_X_ATTRIBUTE_STATUS_RECORD:
            break;
        case N_X_EXTENSION_FIELD_SET:
            break;
        case N_X_NEIGHBORS_INFORMATION:
            break;
        case N_X_READ_ATTRIBUTE_STATUS_RECORD:
            break;
        case N_X_UNSIGNED_16_BIT_INTEGER:
            int cntN16 = Integer.valueOf((byte) payload[index++] & 0xFF);
            List<Integer> arrayN16 = new ArrayList<Integer>(cntN16);
            for (int arrayIndex = 0; arrayIndex < cntN16; arrayIndex++) {
                arrayN16.add(Integer.valueOf(payload[index++] + ((payload[index++] << 8) & 0xffff)));
            }
            value[0] = arrayN16;
            break;
        case N_X_UNSIGNED_8_BIT_INTEGER:
            int cntN8 = Integer.valueOf((byte) payload[index++] & 0xFF);
            List<Integer> arrayN8 = new ArrayList<Integer>(cntN8);
            for (int arrayIndex = 0; arrayIndex < cntN8; arrayIndex++) {
                arrayN8.add(Integer.valueOf(payload[index++]));
            }
            value[0] = arrayN8;
            break;
        case X_UNSIGNED_8_BIT_INTEGER:
            int cntX8 = payload.length - index;
            List<Integer> arrayX8 = new ArrayList<Integer>(cntX8);
            for (int arrayIndex = 0; arrayIndex < cntX8; arrayIndex++) {
                arrayX8.add(Integer.valueOf(payload[index++]));
            }
            value[0] = arrayX8;
            break;
        case N_X_ATTRIBUTE_IDENTIFIER:
            int cntX16 = (payload.length - index) / 2;
            List<Integer> arrayX16 = new ArrayList<Integer>(cntX16);
            for (int arrayIndex = 0; arrayIndex < cntX16; arrayIndex++) {
                arrayX16.add(Integer.valueOf(payload[index++]));
            }
            value[0] = arrayX16;
            break;
        case UNSIGNED_8_BIT_INTEGER_ARRAY:
            int cnt8Array = payload.length - index;
            int[] intarray8 = new int[cnt8Array];
            for (int arrayIndex = 0; arrayIndex < cnt8Array; arrayIndex++) {
                intarray8[arrayIndex] = payload[index++];
            }
            value[0] = intarray8;
            break;
        case N_X_WRITE_ATTRIBUTE_RECORD:
            break;
        case N_X_WRITE_ATTRIBUTE_STATUS_RECORD:
            break;
        case CLUSTERID:
        case NWK_ADDRESS:
        case BITMAP_16_BIT:
        case ENUMERATION_16_BIT:
        case SIGNED_16_BIT_INTEGER:
        case UNSIGNED_16_BIT_INTEGER:
            short s = (short) (payload[index++] + (payload[index++] << 8));
            if (type == ZclDataType.SIGNED_16_BIT_INTEGER) {
                value[0] = Integer.valueOf(s);
            } else {
                value[0] = Integer.valueOf(s & 0xFFFF);
            }
            break;
        case BITMAP_32_BIT:
        case SIGNED_32_BIT_INTEGER:
        case UNSIGNED_32_BIT_INTEGER:
            value[0] = payload[index++] + (payload[index++] << 8) + (payload[index++] << 16) + (payload[index++] << 24);
            break;
        case SIGNED_8_BIT_INTEGER:
            value[0] = Integer.valueOf((byte) payload[index++]);
            break;
        case UNSIGNED_8_BIT_INTEGER:
            value[0] = Integer.valueOf((byte) payload[index++] & 0xFF);
            break;
        case UTCTIME:
            break;
        case ROUTING_TABLE:
            RoutingTable routingTable = new RoutingTable();
            routingTable.deserialize(this);
            value[0] = routingTable;
            break;
        case NEIGHBOR_TABLE:
            NeighborTable neighborTable = new NeighborTable();
            neighborTable.deserialize(this);
            value[0] = neighborTable;
            break;
        case NODE_DESCRIPTOR:
            NodeDescriptor nodeDescriptor = new NodeDescriptor();
            nodeDescriptor.deserialize(this);
            value[0] = nodeDescriptor;
            break;
        case POWER_DESCRIPTOR:
            PowerDescriptor powerDescriptor = new PowerDescriptor();
            powerDescriptor.deserialize(this);
            value[0] = powerDescriptor;
            break;
        case BINDING_TABLE:
            BindingTable bindingTable = new BindingTable();
            bindingTable.deserialize(this);
            value[0] = bindingTable;
            break;
        case SIMPLE_DESCRIPTOR:
            SimpleDescriptor simpleDescriptor = new SimpleDescriptor();
            simpleDescriptor.deserialize(this);
            value[0] = simpleDescriptor;
            break;
        case ZCL_STATUS:
            value[0] = ZclStatus.getStatus(payload[index++]);
            break;
        case ZDO_STATUS:
            value[0] = ZdoStatus.getStatus(payload[index++]);
            break;
        case ZIGBEE_DATA_TYPE:
            value[0] = ZclDataType.getType(payload[index++]);
            break;
        case BYTE_ARRAY:
            int cntB8 = Integer.valueOf((byte) payload[index++] & 0xFF);
            byte[] arrayB8 = new byte[cntB8];
            for (int arrayIndex = 0; arrayIndex < cntB8; arrayIndex++) {
                arrayB8[arrayIndex] = (byte) (payload[index++] & 0xff);
            }
            value[0] = new ByteArray(arrayB8);
            break;
        default:
            throw new IllegalArgumentException("No reader defined in " + ZigBeeDeserializer.class.getSimpleName() + " for " + type.toString() + " (" + type.getId() + ")");
    }
    return value[0];
}
Also used : ExtendedPanId(com.zsmartsystems.zigbee.ExtendedPanId) ArrayList(java.util.ArrayList) NodeDescriptor(com.zsmartsystems.zigbee.zdo.field.NodeDescriptor) IeeeAddress(com.zsmartsystems.zigbee.IeeeAddress) SimpleDescriptor(com.zsmartsystems.zigbee.zdo.field.SimpleDescriptor) RoutingTable(com.zsmartsystems.zigbee.zdo.field.RoutingTable) BindingTable(com.zsmartsystems.zigbee.zdo.field.BindingTable) NeighborTable(com.zsmartsystems.zigbee.zdo.field.NeighborTable) ByteArray(com.zsmartsystems.zigbee.zcl.field.ByteArray) PowerDescriptor(com.zsmartsystems.zigbee.zdo.field.PowerDescriptor)

Example 3 with ByteArray

use of com.zsmartsystems.zigbee.zcl.field.ByteArray in project com.zsmartsystems.zigbee by zsmartsystems.

the class ZigBeeOtaFileTest method testOpenFile.

@Test
public void testOpenFile() throws IOException {
    Path file = FileSystems.getDefault().getPath("./src/test/resource/", "test_ota_file.test");
    byte[] fileData = Files.readAllBytes(file);
    ZigBeeOtaFile otaFile = new ZigBeeOtaFile(fileData);
    System.out.println(otaFile);
    assertEquals(Integer.valueOf(0x1234), otaFile.getManufacturerCode());
    assertEquals(Integer.valueOf(0x0006), otaFile.getImageType());
    assertEquals(Integer.valueOf(0x12345678), otaFile.getFileVersion());
    assertEquals(ZigBeeStackType.ZIGBEE_PRO, otaFile.getStackVersion());
    assertEquals("A.String", otaFile.getHeaderString());
    assertEquals(Integer.valueOf(78), otaFile.getImageSize());
    assertEquals(new ByteArray(new byte[] { 0x1E, (byte) 0xF1, (byte) 0xEE, 0x0B }), otaFile.getImageData(0, 4));
    assertEquals(new ByteArray(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 }), otaFile.getImageData(62, 5));
}
Also used : Path(java.nio.file.Path) ZigBeeOtaFile(com.zsmartsystems.zigbee.app.otaserver.ZigBeeOtaFile) ByteArray(com.zsmartsystems.zigbee.zcl.field.ByteArray) Test(org.junit.Test)

Example 4 with ByteArray

use of com.zsmartsystems.zigbee.zcl.field.ByteArray in project com.zsmartsystems.zigbee by zsmartsystems.

the class SerializerIntegrationTest method testDeserialize_BYTE_ARRAY.

@Test
public void testDeserialize_BYTE_ARRAY() {
    ByteArray valIn = new ByteArray(new byte[] { 1, 2, 3, 4, 5 });
    testSerializer(valIn, ZclDataType.BYTE_ARRAY);
}
Also used : ByteArray(com.zsmartsystems.zigbee.zcl.field.ByteArray) Test(org.junit.Test)

Example 5 with ByteArray

use of com.zsmartsystems.zigbee.zcl.field.ByteArray in project com.zsmartsystems.zigbee by zsmartsystems.

the class DefaultSerializer method appendZigBeeType.

@Override
public void appendZigBeeType(Object data, ZclDataType type) throws IllegalArgumentException {
    if (data == null) {
        throw new IllegalArgumentException("You can not append null data to a stream");
    }
    switch(type) {
        case BOOLEAN:
            buffer[length++] = (Boolean) data ? 1 : 0;
            break;
        case NWK_ADDRESS:
        case BITMAP_16_BIT:
        case SIGNED_16_BIT_INTEGER:
        case UNSIGNED_16_BIT_INTEGER:
        case ENUMERATION_16_BIT:
        case CLUSTERID:
            final short shortValue = ((Number) data).shortValue();
            buffer[length++] = shortValue & 0xFF;
            buffer[length++] = (shortValue >> 8) & 0xFF;
            break;
        case ENDPOINT:
        case DATA_8_BIT:
        case BITMAP_8_BIT:
        case SIGNED_8_BIT_INTEGER:
        case UNSIGNED_8_BIT_INTEGER:
        case ENUMERATION_8_BIT:
            final byte byteValue = ((Number) data).byteValue();
            buffer[length++] = byteValue & 0xFF;
            break;
        case EXTENDED_PANID:
            int[] panId = ((ExtendedPanId) data).getValue();
            buffer[length++] = panId[0];
            buffer[length++] = panId[1];
            buffer[length++] = panId[2];
            buffer[length++] = panId[3];
            buffer[length++] = panId[4];
            buffer[length++] = panId[5];
            buffer[length++] = panId[6];
            buffer[length++] = panId[7];
            break;
        case IEEE_ADDRESS:
            int[] address = ((IeeeAddress) data).getValue();
            buffer[length++] = address[0];
            buffer[length++] = address[1];
            buffer[length++] = address[2];
            buffer[length++] = address[3];
            buffer[length++] = address[4];
            buffer[length++] = address[5];
            buffer[length++] = address[6];
            buffer[length++] = address[7];
            break;
        case N_X_ATTRIBUTE_INFORMATION:
            break;
        case N_X_ATTRIBUTE_RECORD:
            break;
        case N_X_ATTRIBUTE_REPORT:
            break;
        case N_X_ATTRIBUTE_REPORTING_CONFIGURATION_RECORD:
            break;
        case N_X_ATTRIBUTE_SELECTOR:
            break;
        case N_X_ATTRIBUTE_STATUS_RECORD:
            break;
        case N_X_EXTENSION_FIELD_SET:
            break;
        case N_X_NEIGHBORS_INFORMATION:
            break;
        case N_X_READ_ATTRIBUTE_STATUS_RECORD:
            break;
        case N_X_UNSIGNED_16_BIT_INTEGER:
            List<Integer> intArray16 = (List<Integer>) data;
            buffer[length++] = intArray16.size();
            for (int value : intArray16) {
                buffer[length++] = value & 0xFF;
                buffer[length++] = (value >> 8) & 0xFF;
            }
            break;
        case N_X_UNSIGNED_8_BIT_INTEGER:
            List<Integer> intArrayNX8 = (List<Integer>) data;
            buffer[length++] = intArrayNX8.size();
            for (int value : intArrayNX8) {
                buffer[length++] = value & 0xFF;
            }
            break;
        case UNSIGNED_8_BIT_INTEGER_ARRAY:
            int[] intArrayN8 = (int[]) data;
            for (int value : intArrayN8) {
                buffer[length++] = value & 0xFF;
            }
            break;
        case X_UNSIGNED_8_BIT_INTEGER:
            List<Integer> intArrayX8 = (List<Integer>) data;
            for (int value : intArrayX8) {
                buffer[length++] = value & 0xFF;
            }
            break;
        case N_X_ATTRIBUTE_IDENTIFIER:
            List<Integer> intArrayX16 = (List<Integer>) data;
            for (int value : intArrayX16) {
                buffer[length++] = value & 0xFF;
                buffer[length++] = (value >> 8) & 0xFF;
            }
            break;
        case N_X_WRITE_ATTRIBUTE_RECORD:
            break;
        case N_X_WRITE_ATTRIBUTE_STATUS_RECORD:
            break;
        case CHARACTER_STRING:
        case OCTET_STRING:
            final String str = (String) data;
            buffer[length++] = ((byte) (str.length() & (0xFF)));
            for (int strByte : str.getBytes()) {
                buffer[length++] = strByte;
            }
            break;
        case SIGNED_32_BIT_INTEGER:
            final int intValue = (Integer) data;
            buffer[length++] = intValue & 0xFF;
            buffer[length++] = (intValue >> 8) & 0xFF;
            buffer[length++] = (intValue >> 16) & 0xFF;
            buffer[length++] = (intValue >> 24) & 0xFF;
            break;
        case BITMAP_32_BIT:
        case UNSIGNED_32_BIT_INTEGER:
            final int uintValue = (Integer) data;
            buffer[length++] = uintValue & 0xFF;
            buffer[length++] = (uintValue >> 8) & 0xFF;
            buffer[length++] = (uintValue >> 16) & 0xFF;
            buffer[length++] = (uintValue >> 24) & 0xFF;
            break;
        case UTCTIME:
            break;
        case ZDO_STATUS:
            buffer[length++] = ((ZdoStatus) data).getId();
            break;
        case ZCL_STATUS:
            buffer[length++] = ((ZclStatus) data).getId();
            break;
        case BYTE_ARRAY:
            final ByteArray byteArray = (ByteArray) data;
            buffer[length++] = byteArray.size();
            for (byte valByte : byteArray.get()) {
                buffer[length++] = valByte & 0xff;
            }
            break;
        case ZIGBEE_DATA_TYPE:
            buffer[length++] = ((ZclDataType) data).getId();
            break;
        default:
            throw new IllegalArgumentException("No writer defined in " + ZigBeeDeserializer.class.getSimpleName() + " for " + type.toString() + " (" + type.getId() + ")");
    }
}
Also used : ExtendedPanId(com.zsmartsystems.zigbee.ExtendedPanId) IeeeAddress(com.zsmartsystems.zigbee.IeeeAddress) ByteArray(com.zsmartsystems.zigbee.zcl.field.ByteArray) List(java.util.List)

Aggregations

ByteArray (com.zsmartsystems.zigbee.zcl.field.ByteArray)5 ExtendedPanId (com.zsmartsystems.zigbee.ExtendedPanId)2 IeeeAddress (com.zsmartsystems.zigbee.IeeeAddress)2 Test (org.junit.Test)2 ZigBeeOtaFile (com.zsmartsystems.zigbee.app.otaserver.ZigBeeOtaFile)1 BindingTable (com.zsmartsystems.zigbee.zdo.field.BindingTable)1 NeighborTable (com.zsmartsystems.zigbee.zdo.field.NeighborTable)1 NodeDescriptor (com.zsmartsystems.zigbee.zdo.field.NodeDescriptor)1 PowerDescriptor (com.zsmartsystems.zigbee.zdo.field.PowerDescriptor)1 RoutingTable (com.zsmartsystems.zigbee.zdo.field.RoutingTable)1 SimpleDescriptor (com.zsmartsystems.zigbee.zdo.field.SimpleDescriptor)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1