Example #1
0
        public void AppendZigBeeType(object data, ZclDataType type)
        {
            if (data == null)
            {
                throw new ArgumentException("You can not append null data to a stream");
            }

            switch (type.DataType)
            {
            case DataType.BOOLEAN:
                _buffer[_length++] = (bool)data ? (byte)0x01 : (byte)0x00;
                break;

            case DataType.NWK_ADDRESS:
            case DataType.BITMAP_16_BIT:
            case DataType.SIGNED_16_BIT_INTEGER:
            case DataType.UNSIGNED_16_BIT_INTEGER:
            case DataType.ENUMERATION_16_BIT:
            case DataType.CLUSTERID:
                ushort shortValue = ((ushort)data);
                _buffer[_length++] = (byte)(shortValue & 0xFF);
                _buffer[_length++] = (byte)((shortValue >> 8) & 0xFF);
                break;

            case DataType.ENDPOINT:
            case DataType.DATA_8_BIT:
            case DataType.BITMAP_8_BIT:
            case DataType.SIGNED_8_BIT_INTEGER:
            case DataType.UNSIGNED_8_BIT_INTEGER:
            case DataType.ENUMERATION_8_BIT:
                byte byteValue = (byte)data;
                _buffer[_length++] = (byte)(byteValue & 0xFF);
                break;

            case DataType.EXTENDED_PANID:
                byte[] panId = BitConverter.GetBytes(((ExtendedPanId)data).Value);
                _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 DataType.IEEE_ADDRESS:
                byte[] address = BitConverter.GetBytes(((IeeeAddress)data).Value);
                _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 DataType.N_X_ATTRIBUTE_INFORMATION:
                break;

            case DataType.N_X_ATTRIBUTE_RECORD:
                break;

            case DataType.N_X_ATTRIBUTE_REPORT:
                break;

            case DataType.N_X_ATTRIBUTE_REPORTING_CONFIGURATION_RECORD:
                break;

            case DataType.N_X_ATTRIBUTE_SELECTOR:
                break;

            case DataType.N_X_ATTRIBUTE_STATUS_RECORD:
                break;

            case DataType.N_X_EXTENSION_FIELD_SET:
                break;

            case DataType.N_X_NEIGHBORS_INFORMATION:
                break;

            case DataType.N_X_READ_ATTRIBUTE_STATUS_RECORD:
                break;

            case DataType.N_X_UNSIGNED_16_BIT_INTEGER:
                List <ushort> intArray16 = (List <ushort>)data;
                _buffer[_length++] = (byte)intArray16.Count;
                foreach (ushort value in intArray16)
                {
                    _buffer[_length++] = (byte)(value & 0xFF);
                    _buffer[_length++] = (byte)((value >> 8) & 0xFF);
                }
                break;

            case DataType.N_X_UNSIGNED_8_BIT_INTEGER:
                List <byte> intArrayNX8 = (List <byte>)data;
                _buffer[_length++] = (byte)intArrayNX8.Count;
                foreach (byte value in intArrayNX8)
                {
                    _buffer[_length++] = (byte)(value & 0xFF);
                }
                break;

            case DataType.UNSIGNED_8_BIT_INTEGER_ARRAY:
                byte[] intArrayN8 = (byte[])data;
                foreach (byte value in intArrayN8)
                {
                    _buffer[_length++] = (byte)(value & 0xFF);
                }
                break;

            case DataType.X_UNSIGNED_8_BIT_INTEGER:
                List <byte> intArrayX8 = (List <byte>)data;
                foreach (byte value in intArrayX8)
                {
                    _buffer[_length++] = (byte)(value & 0xFF);
                }
                break;

            case DataType.N_X_ATTRIBUTE_IDENTIFIER:
                List <ushort> intArrayX16 = (List <ushort>)data;
                foreach (ushort value in intArrayX16)
                {
                    _buffer[_length++] = (byte)(value & 0xFF);
                    _buffer[_length++] = (byte)((value >> 8) & 0xFF);
                }
                break;

            case DataType.N_X_WRITE_ATTRIBUTE_RECORD:
                break;

            case DataType.N_X_WRITE_ATTRIBUTE_STATUS_RECORD:
                break;

            case DataType.OCTET_STRING:
                ByteArray array = (ByteArray)data;
                _buffer[_length++] = ((byte)(array.Size() & 0xFF));
                foreach (byte arrayByte in array.Get())
                {
                    _buffer[_length++] = arrayByte;
                }
                break;

            case DataType.CHARACTER_STRING:
                string str = (string)data;
                _buffer[_length++] = ((byte)(str.Length & 0xFF));
                foreach (byte strByte in Encoding.ASCII.GetBytes((str)))
                {
                    _buffer[_length++] = strByte;
                }
                break;

            case DataType.UNSIGNED_24_BIT_INTEGER:
                byte[] uint24Value = (byte[])data;
                _buffer[_length++] = uint24Value[0];     //uint24Value & 0xFF;
                _buffer[_length++] = uint24Value[1];     //(uint24Value >> 8) & 0xFF;
                _buffer[_length++] = uint24Value[2];     //(uint24Value >> 16) & 0xFF;
                break;

            case DataType.SIGNED_32_BIT_INTEGER:
                int intValue = (int)data;
                _buffer[_length++] = (byte)(intValue & 0xFF);
                _buffer[_length++] = (byte)((intValue >> 8) & 0xFF);
                _buffer[_length++] = (byte)((intValue >> 16) & 0xFF);
                _buffer[_length++] = (byte)((intValue >> 24) & 0xFF);
                break;

            case DataType.BITMAP_32_BIT:
            case DataType.UNSIGNED_32_BIT_INTEGER:
                UInt32 uint32Value = (UInt32)data;
                _buffer[_length++] = (byte)(uint32Value & 0xFF);
                _buffer[_length++] = (byte)((uint32Value >> 8) & 0xFF);
                _buffer[_length++] = (byte)((uint32Value >> 16) & 0xFF);
                _buffer[_length++] = (byte)((uint32Value >> 24) & 0xFF);
                break;

            case DataType.UNSIGNED_48_BIT_INTEGER:
                long uint48Value = (long)data;
                _buffer[_length++] = (byte)(uint48Value & 0xFF);
                _buffer[_length++] = (byte)((uint48Value >> 8) & 0xFF);
                _buffer[_length++] = (byte)((uint48Value >> 16) & 0xFF);
                _buffer[_length++] = (byte)((uint48Value >> 24) & 0xFF);
                _buffer[_length++] = (byte)((uint48Value >> 32) & 0xFF);
                _buffer[_length++] = (byte)((uint48Value >> 40) & 0xFF);
                break;

            case DataType.UTCTIME:
                break;

            case DataType.ZDO_STATUS:
                _buffer[_length++] = (byte)((ZdoStatus)data);
                break;

            case DataType.ZCL_STATUS:
                _buffer[_length++] = (byte)((ZclStatus)data);
                break;

            case DataType.BYTE_ARRAY:
                ByteArray byteArray = (ByteArray)data;
                _buffer[_length++] = (byte)(byteArray.Size());
                foreach (byte valByte in byteArray.Get())
                {
                    _buffer[_length++] = (byte)(valByte & 0xff);
                }
                break;

            case DataType.ZIGBEE_DATA_TYPE:
                _buffer[_length++] = (byte)((ZclDataType)data).Id;
                break;

            default:
                throw new ArgumentException("No writer defined in " + this.GetType().Name
                                            + " for " + type.ToString() + " (" + type.Id + ")");
            }
        }
Example #2
0
        /**
         * {@inheritDoc}
         */
        public object ReadZigBeeType(ZclDataType type)
        {
            if (index == payload.Length)
            {
                return(null);
            }

            object[] value = new object[1];
            switch (type.DataType)
            {
            case DataType.BOOLEAN:
                value[0] = payload[index++] == 0 ? false : true;
                break;

            case DataType.OCTET_STRING:
                int octetSize = payload[index++];
                value[0] = new ByteArray(payload, index, index + octetSize);
                index   += octetSize;
                break;

            case DataType.CHARACTER_STRING:
                int stringSize = payload[index++];
                if (stringSize == 255)
                {
                    value[0] = null;
                    break;
                }
                byte[] bytes  = new byte[stringSize];
                int    length = stringSize;
                for (int cnt = 0; cnt < stringSize; cnt++)
                {
                    bytes[cnt] = (byte)payload[index + cnt];
                    if (payload[index + cnt] == 0)
                    {
                        length = cnt;
                        break;
                    }
                }
                try
                {
                    int    len  = length - 0;
                    byte[] dest = new byte[len];
                    // note i is always from 0
                    for (int i = 0; i < len; i++)
                    {
                        dest[i] = bytes[0 + i];     // so 0..n = 0+x..n+x
                    }

                    value[0] = Encoding.Default.GetString(dest);
                }
                catch (Exception e)
                {
                    value[0] = null;
                    break;
                }
                index += stringSize;
                break;

            case DataType.ENDPOINT:
            case DataType.BITMAP_8_BIT:
            case DataType.DATA_8_BIT:
            case DataType.ENUMERATION_8_BIT:
                value[0] = (byte)((byte)payload[index++] & 0xFF);
                break;

            case DataType.EXTENDED_PANID:
                byte[] panId = new byte[8];
                for (int iCnt = 7; iCnt >= 0; iCnt--)
                {
                    panId[iCnt] = payload[index + iCnt];
                }
                index   += 8;
                value[0] = new ExtendedPanId(panId);
                break;

            case DataType.IEEE_ADDRESS:
                byte[] address = new byte[8];
                for (int iCnt = 7; iCnt >= 0; iCnt--)
                {
                    address[iCnt] = payload[index + iCnt];
                }
                index   += 8;
                value[0] = new IeeeAddress(address);
                break;

            case DataType.N_X_ATTRIBUTE_INFORMATION:
                break;

            case DataType.N_X_ATTRIBUTE_RECORD:
                break;

            case DataType.N_X_ATTRIBUTE_REPORT:
                break;

            case DataType.N_X_ATTRIBUTE_REPORTING_CONFIGURATION_RECORD:
                break;

            case DataType.N_X_ATTRIBUTE_SELECTOR:
                break;

            case DataType.N_X_ATTRIBUTE_STATUS_RECORD:
                break;

            case DataType.N_X_EXTENSION_FIELD_SET:
                break;

            case DataType.N_X_NEIGHBORS_INFORMATION:
                break;

            case DataType.N_X_READ_ATTRIBUTE_STATUS_RECORD:
                break;

            case DataType.N_X_UNSIGNED_16_BIT_INTEGER:
                int         cntN16   = (byte)((byte)payload[index++] & 0xFF);
                List <byte> arrayN16 = new List <byte>(cntN16);
                for (int arrayIndex = 0; arrayIndex < cntN16; arrayIndex++)
                {
                    arrayN16.Add((byte)(payload[index++] | ((payload[index++] << 8) & 0xffff)));
                }
                value[0] = arrayN16;
                break;

            case DataType.N_X_UNSIGNED_8_BIT_INTEGER:
                int         cntN8   = (byte)((byte)payload[index++] & 0xFF);
                List <byte> arrayN8 = new List <byte>(cntN8);
                for (int arrayIndex = 0; arrayIndex < cntN8; arrayIndex++)
                {
                    arrayN8.Add((byte)(payload[index++]));
                }
                value[0] = arrayN8;
                break;

            case DataType.X_UNSIGNED_8_BIT_INTEGER:
                int         cntX8   = payload.Length - index;
                List <byte> arrayX8 = new List <byte>(cntX8);
                for (int arrayIndex = 0; arrayIndex < cntX8; arrayIndex++)
                {
                    arrayX8.Add((byte)(payload[index++]));
                }
                value[0] = arrayX8;
                break;

            case DataType.N_X_ATTRIBUTE_IDENTIFIER:
                int         cntX16   = (payload.Length - index) / 2;
                List <byte> arrayX16 = new List <byte>(cntX16);
                for (int arrayIndex = 0; arrayIndex < cntX16; arrayIndex++)
                {
                    arrayX16.Add((byte)(payload[index++]));
                }
                value[0] = arrayX16;
                break;

            case DataType.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 DataType.N_X_WRITE_ATTRIBUTE_RECORD:
                break;

            case DataType.N_X_WRITE_ATTRIBUTE_STATUS_RECORD:
                break;

            case DataType.SIGNED_16_BIT_INTEGER:
                short s = (short)(payload[index++] | (payload[index++] << 8));

                value[0] = s;
                break;

            case DataType.CLUSTERID:
            case DataType.NWK_ADDRESS:
            case DataType.BITMAP_16_BIT:
            case DataType.ENUMERATION_16_BIT:
            case DataType.UNSIGNED_16_BIT_INTEGER:
                ushort us = (ushort)(payload[index++] | (payload[index++] << 8));

                value[0] = us;
                break;

            case DataType.UNSIGNED_24_BIT_INTEGER:
                value[0] = payload[index++] + (payload[index++] << 8) | (payload[index++] << 16);
                break;

            case DataType.BITMAP_32_BIT:
            case DataType.SIGNED_32_BIT_INTEGER:
            case DataType.UNSIGNED_32_BIT_INTEGER:
                value[0] = payload[index++] + (payload[index++] << 8) | (payload[index++] << 16)
                           + (payload[index++] << 24);
                break;

            case DataType.UNSIGNED_48_BIT_INTEGER:
                value[0] = (payload[index++]) + ((long)(payload[index++]) << 8) | ((long)(payload[index++]) << 16)
                           + ((long)(payload[index++]) << 24) | ((long)(payload[index++]) << 32)
                           + ((long)(payload[index++]) << 40);
                break;

            case DataType.SIGNED_8_BIT_INTEGER:
                value[0] = (byte)((byte)payload[index++]);
                break;

            case DataType.UNSIGNED_8_BIT_INTEGER:
                value[0] = (byte)((byte)payload[index++] & 0xFF);
                break;

            case DataType.UTCTIME:
                break;

            case DataType.ROUTING_TABLE:
                RoutingTable routingTable = new RoutingTable();
                routingTable.Deserialize(this);
                value[0] = routingTable;
                break;

            case DataType.NEIGHBOR_TABLE:
                NeighborTable neighborTable = new NeighborTable();
                neighborTable.Deserialize(this);
                value[0] = neighborTable;
                break;

            case DataType.NODE_DESCRIPTOR:
                NodeDescriptor nodeDescriptor = new NodeDescriptor();
                nodeDescriptor.Deserialize(this);
                value[0] = nodeDescriptor;
                break;

            case DataType.POWER_DESCRIPTOR:
                PowerDescriptor powerDescriptor = new PowerDescriptor();
                powerDescriptor.Deserialize(this);
                value[0] = powerDescriptor;
                break;

            case DataType.BINDING_TABLE:
                BindingTable bindingTable = new BindingTable();
                bindingTable.Deserialize(this);
                value[0] = bindingTable;
                break;

            case DataType.SIMPLE_DESCRIPTOR:
                SimpleDescriptor simpleDescriptor = new SimpleDescriptor();
                simpleDescriptor.Deserialize(this);
                value[0] = simpleDescriptor;
                break;

            case DataType.ZCL_STATUS:
                value[0] = (ZclStatus)(payload[index++]);
                break;

            case DataType.ZDO_STATUS:
                value[0] = (ZdoStatus)(payload[index++]);
                break;

            case DataType.ZIGBEE_DATA_TYPE:
                value[0] = ZclDataType.Get(payload[index++]);
                break;

            case DataType.BYTE_ARRAY:
                int    cntB8   = (byte)((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 ArgumentException("No reader defined in " + this.GetType().Name + " for " + type.ToString() + " (" + type.Id + ")");
            }
            return(value[0]);
        }