Пример #1
0
        internal ZclCommand(ZclCluster cluster, byte id, String name, bool specificResponseRequired)
        {
            m_cluster      = cluster;
            m_Id           = id;
            m_zigBeeStatus = ZclHelper.ZCL_ERROR_SUCCESS;

            // as far as ZigBeeCommand class is concerned a response will always be required
            // ZCL will use the Default Response if ZclCommand require no specific response
            m_responseRequired = true;
            if (specificResponseRequired)
            {
                m_useDefaultResponse = false;
            }
            else
            {
                m_useDefaultResponse = true;
            }

            this.Name        = m_cluster.Name + "_" + name;
            this.Description = null;
            this.HResult     = ZclHelper.ZigBeeStatusToHResult(m_zigBeeStatus);

            m_isZdoCommand = false;
            ZclHelper.SetClusterSpecificHeader(ref m_zclHeader, 0);
            int offset = ZclHelper.GetCommandIdOffset(ref m_zclHeader, 0);

            m_zclHeader[offset] = m_Id;

            m_zclInParamList = new List <ZclValue>();
            InputParams      = new List <IAdapterValue>();
            OutputParams     = new List <IAdapterValue>();
        }
Пример #2
0
        protected int GetZclPayloadOffset(ref byte[] buffer)
        {
            int offset = GetZigBeePayloadOffset();

            offset += ZclHelper.GetPayloadOffset(ref buffer, offset);
            return(offset);
        }
Пример #3
0
        public ZclDiscoverAttributes()
        {
            // ZigBee command specific
            m_isZdoCommand = false;
            int offset = ZclHelper.GetCommandIdOffset(ref m_zclHeader, 0);

            m_zclHeader[offset] = COMMAND_ID_DISCOVER_ATTRIBUTES;
        }
Пример #4
0
        internal ZclValue(byte zigBeeType, string name)
        {
            Name       = name;
            ZigBeeType = zigBeeType;

            object tempObject = null;

            ZclHelper.CreateDefaultValue(zigBeeType, out tempObject);
            Data = tempObject;
        }
Пример #5
0
        public override bool ParseResponse(byte[] buffer)
        {
            m_status = ZclHelper.ZCL_ERROR_FAILURE;

            if (!IsResponseOK(buffer))
            {
                return(false);
            }

            // verify command Id
            int offset = GetZclCommandIdOffset(ref buffer);

            if (COMMAND_ID_READ_ATTRIBUTE_RESPONSE != buffer[offset])
            {
                return(false);
            }

            // parse ZCL payload
            offset = GetZclPayloadOffset(ref buffer);

            // check attribute Id
            UInt16 id = AdapterHelper.UInt16FromZigBeeFrame(buffer, offset);

            if (id != m_Id)
            {
                return(false);
            }

            offset += sizeof(UInt16);

            // check status
            m_status = buffer[offset];
            if (m_status != ZclHelper.ZCL_ERROR_SUCCESS)
            {
                return(false);
            }

            offset += ZIGBEE_STATUS_LENGTH;

            // set data
            bool   retValue = false;
            object value    = null;

            // from ZCL command payload
            // - 1st byte indicates the type
            // - following byte(s) contain the value
            byte type = buffer[offset];

            offset    += sizeof(byte);
            retValue   = ZclHelper.GetValue(type, ref buffer, ref offset, out value);
            Value.Data = value;

            return(retValue);
        }
Пример #6
0
        internal void Send()
        {
            // set cluster Id
            m_clusterId         = m_cluster.Id;
            m_responseClusterId = m_cluster.Id;

            // add header to payload
            m_payload = new byte[m_zclHeader.Length];
            Array.Copy(m_zclHeader, m_payload, m_zclHeader.Length);

            // add command parameters to payload
            foreach (var item in m_zclInParamList)
            {
                // copy in parameter data from "BridgeRT" in parameter to
                // "internal" ZCL parameter
                IAdapterValue value = GetInputParamByName(item.Name);
                item.Data = value.Data;

                // add parameter in ZCL payload
                byte[] tempBuffer = item.ToByteBuffer();
                if (tempBuffer == null)
                {
                    // can't set current parameter => give up with sending
                    m_zigBeeStatus = ZclHelper.ZCL_ERROR_INVALID_VALUE;
                    HResult        = ZclHelper.ZigBeeStatusToHResult(m_zigBeeStatus);
                    return;
                }

                int previousLength = m_payload.Length;
                Array.Resize(ref m_payload, previousLength + tempBuffer.Length);
                Array.Copy(tempBuffer, 0, m_payload, previousLength, tempBuffer.Length);
            }

            // send command
            m_zigBeeStatus = ZclHelper.ZCL_ERROR_SUCCESS;
            if (!SendCommand(m_cluster.EndPoint.Device.Module, m_cluster.EndPoint.Device, m_cluster.EndPoint))
            {
                m_zigBeeStatus = ZclHelper.ZCL_ERROR_TIMEOUT;
            }
            HResult = ZclHelper.ZigBeeStatusToHResult(m_zigBeeStatus);

            return;
        }
Пример #7
0
        public static byte[] ToByteBufferWithType(object value, byte zigBeeType)
        {
            byte[] buffer      = null;
            byte[] valueBuffer = null;

            // get buffer that correspond to the vale
            valueBuffer = ZclHelper.ToByteBuffer(value);
            if (valueBuffer == null)
            {
                return(null);
            }

            // add ZigBee type to returned buffer
            buffer    = new byte[ZclHelper.ZCL_TYPE_CODE_LENGTH + valueBuffer.Length];
            buffer[0] = zigBeeType;
            Array.Copy(valueBuffer, 0, buffer, ZclHelper.ZCL_TYPE_CODE_LENGTH, valueBuffer.Length);

            return(buffer);
        }
Пример #8
0
        internal bool Write(object value)
        {
            // sanity check
            if (value == null)
            {
                return(false);
            }

            // get byte buffer from value to set
            byte[] attributeValueBuffer = ZclHelper.ToByteBufferWithType(value, m_zigBeeType);
            if (attributeValueBuffer == null)
            {
                return(false);
            }

            // set cluster Id
            m_clusterId         = m_cluster.Id;
            m_responseClusterId = m_cluster.Id;

            // set write command Id
            int offset = ZclHelper.GetCommandIdOffset(ref m_zclHeader, 0);

            m_zclHeader[offset] = COMMAND_ID_WRITE_ATTRIBUTE;

            // set payload
            byte[] tempBuffer = AdapterHelper.ToZigBeeFrame(m_Id);
            m_payload = new byte[m_zclHeader.Length + tempBuffer.Length + attributeValueBuffer.Length];
            Array.Copy(m_zclHeader, m_payload, m_zclHeader.Length);
            Array.Copy(tempBuffer, 0, m_payload, m_zclHeader.Length, tempBuffer.Length);
            Array.Copy(attributeValueBuffer, 0, m_payload, m_zclHeader.Length + tempBuffer.Length, attributeValueBuffer.Length);

            // send command
            m_responseRequired = false;
            if (!SendCommand(m_cluster.EndPoint.Device.Module, m_cluster.EndPoint.Device, m_cluster.EndPoint))
            {
                return(false);
            }

            return(true);
        }
Пример #9
0
        internal bool Read(out object value)
        {
            // set value(s) to null
            Value.Data = null;
            value      = null;
            m_status   = ZclHelper.ZCL_ERROR_SUCCESS;

            // set cluster Id
            m_clusterId         = m_cluster.Id;
            m_responseClusterId = m_cluster.Id;

            // set read command Id
            int offset = ZclHelper.GetCommandIdOffset(ref m_zclHeader, 0);

            m_zclHeader[offset] = COMMAND_ID_READ_ATTRIBUTE;

            // set payload
            byte[] tempBuffer = AdapterHelper.ToZigBeeFrame(m_Id);
            m_payload = new byte[m_zclHeader.Length + tempBuffer.Length];
            Array.Copy(m_zclHeader, m_payload, m_zclHeader.Length);
            Array.Copy(tempBuffer, 0, m_payload, m_zclHeader.Length, tempBuffer.Length);

            // send command
            m_responseRequired = true;
            if (!SendCommand(m_cluster.EndPoint.Device.Module, m_cluster.EndPoint.Device, m_cluster.EndPoint))
            {
                m_status = ZclHelper.ZCL_ERROR_TIMEOUT;
                return(false);
            }

            // set out value
            if (Value.Data == null)
            {
                return(false);
            }

            value = Value.Data;

            return(true);
        }
Пример #10
0
        internal ZclAttribute(ZclCluster cluster, UInt16 id, String name, byte zigBeeType, bool isReadOnly = false, bool isOptional = false, bool isReportable = false)
        {
            // save parent cluster
            m_cluster = cluster;

            // attribute specific
            m_Id         = id;
            m_isReadOnly = isReadOnly;
            m_isOptional = isOptional;
            m_zigBeeType = zigBeeType;

            // ZigBee command specific
            m_isZdoCommand = false;
            int offset = ZclHelper.GetCommandIdOffset(ref m_zclHeader, 0);

            m_zclHeader[offset] = COMMAND_ID_READ_ATTRIBUTE;

            // BridgeRT specific
            Value       = new ZclValue(zigBeeType, name);
            Annotations = new Dictionary <string, string>();
            if (isReadOnly)
            {
                Access = E_ACCESS_TYPE.ACCESS_READ;
            }
            else
            {
                Access = E_ACCESS_TYPE.ACCESS_READWRITE;
            }
            if (isReportable)
            {
                COVBehavior = SignalBehavior.Always;
            }
            else
            {
                COVBehavior = SignalBehavior.Unspecified;
            }
        }
Пример #11
0
        public override bool ParseResponse(byte[] buffer)
        {
            m_zigBeeStatus = ZclHelper.ZCL_ERROR_SUCCESS;

            if (!m_responseRequired)
            {
                // no response require => leave with success
                return(true);
            }

            // verify response status
            if (!IsResponseOK(buffer))
            {
                m_zigBeeStatus = ZclHelper.ZCL_ERROR_FAILURE;
                return(false);
            }

            // parse response
            int offset = GetZclPayloadOffset(ref buffer);

            if (m_useDefaultResponse)
            {
                // Default response to command

                // verify response command Id and sent command Id
                if (COMMAND_DEFAULT_RESPONSE != buffer[offset - 1] ||
                    m_Id != buffer[offset])
                {
                    m_zigBeeStatus = ZclHelper.ZCL_ERROR_FAILURE;
                    return(false);
                }

                // save away status
                offset++;
                m_zigBeeStatus = buffer[offset];
            }
            else
            {
                // specific response to command

                // verify response command Id
                if (m_Id != buffer[offset - 1])
                {
                    m_zigBeeStatus = ZclHelper.ZCL_ERROR_FAILURE;
                    return(false);
                }

                // fill in out parameters
                foreach (var item in OutputParams)
                {
                    // cast back out param
                    var outParam = (ZclValue)item;

                    object value;
                    if (!ZclHelper.GetValue(outParam.ZigBeeType, ref buffer, ref offset, out value))
                    {
                        // can't get one of the out parameters => give up
                        m_zigBeeStatus = ZclHelper.ZCL_ERROR_FAILURE;
                        return(false);
                    }

                    outParam.Data = value;
                }
            }

            return(true);
        }
Пример #12
0
 internal byte[] ToByteBuffer()
 {
     return(ZclHelper.ToByteBuffer(Data));
 }
Пример #13
0
        public override bool ParseResponse(byte[] buffer)
        {
            SOURCE_INFO sourceInfo = new SOURCE_INFO();

            // verify command Id
            int offset = GetZclCommandIdOffset(ref buffer);

            if (COMMAND_ID_REPORT_ATTRIBUTES != buffer[offset])
            {
                return(false);
            }

            if (OnReception == null)
            {
                // can't signal anything
                // however this is not an error => return true
                return(true);
            }

            // get Mac address, network address, endpoint Id and cluster Id of source
            offset = 0;
            sourceInfo.macAddress = AdapterHelper.UInt64FromXbeeFrame(buffer, offset);
            offset = AdapterHelper.MAC_ADDR_LENGTH;

            sourceInfo.networkAddress = AdapterHelper.UInt16FromXbeeFrame(buffer, offset);
            offset += AdapterHelper.NETWORK_ADDRESS_LENGTH;

            sourceInfo.endpointId = buffer[offset];
            offset++;

            // skip destination end point
            offset++;

            sourceInfo.clusterId = AdapterHelper.UInt16FromXbeeFrame(buffer, offset);

            string strBuffer = "";

            foreach (byte b in buffer)
            {
                strBuffer = strBuffer + b.ToString() + " ";
            }

            loggingServices.WriteLine <ZclReportAttributes>("strBuffer = [" + strBuffer + "]");
            loggingServices.WriteLine <ZclReportAttributes>("macAddress = [" + sourceInfo.macAddress + "]");
            loggingServices.WriteLine <ZclReportAttributes>("networkAddress = [" + sourceInfo.networkAddress + "]");
            loggingServices.WriteLine <ZclReportAttributes>("endpointId = [" + sourceInfo.endpointId + "]");
            loggingServices.WriteLine <ZclReportAttributes>("clusterId = [" + sourceInfo.clusterId + "]");

            // parse ZCL payload
            offset = GetZclPayloadOffset(ref buffer);
            loggingServices.WriteLine <ZclReportAttributes>("offset of addributeId = [" + offset + "]");
            while (offset < buffer.Length)
            {
                object value = null;

                // from ZCL report attribute payload
                // - 1st byte is the attribute Id
                // - 2nd byte indicates the type
                // - following byte(s) contain the value
                UInt16 attributeId = AdapterHelper.UInt16FromZigBeeFrame(buffer, offset);
                loggingServices.WriteLine <ZclReportAttributes>("attributeId = [" + attributeId + "]");
                offset += sizeof(UInt16);
                byte type = buffer[offset];
                offset += sizeof(byte);
                loggingServices.WriteLine <ZclReportAttributes>("offset of value = [" + offset + "]");
                if (!ZclHelper.GetValue(type, ref buffer, ref offset, out value))
                {
                    // give up if attribute can't be retrieved
                    break;
                }

                // execute notification callback asynchronously
                Task.Run(() => { OnReception(sourceInfo, attributeId, value); });
            }
            return(true);
        }
Пример #14
0
        public override bool ParseResponse(byte[] buffer)
        {
            if (m_commandList.Count == 0)
            {
                // no server command registered
                return(false);
            }

            // get Mac address, endpoint Id, cluster Id and command Id of source
            int    offset     = 0;
            UInt64 macAddress = AdapterHelper.UInt64FromXbeeFrame(buffer, offset);

            offset = AdapterHelper.MAC_ADDR_LENGTH;

            // skip network address (mac address is enough)
            offset += AdapterHelper.NETWORK_ADDRESS_LENGTH;

            byte endpointId = buffer[offset];

            offset++;

            // skip destination end point
            offset++;

            UInt16 clusterId = AdapterHelper.UInt16FromXbeeFrame(buffer, offset);

            offset = GetZclCommandIdOffset(ref buffer);
            byte commandId = buffer[offset];

            // find element that matches Mac address, network address, endpoint Id and cluster Id
            var element = Find(macAddress, endpointId, clusterId, commandId);

            if (element == null)
            {
                // no corresponding element for that Zcl server command
                return(false);
            }

            if (element.OnReception == null)
            {
                // no notification hence nothing else to do
                return(true);
            }

            // get parameters
            offset = GetZclPayloadOffset(ref buffer);
            // fill in out parameters
            foreach (var parameter in element.ParamList)
            {
                object value;
                if (!ZclHelper.GetValue(parameter.ZigBeeType, ref buffer, ref offset, out value))
                {
                    // can't get one of the out parameters => nothing else to do
                    // note that return value is true because server command has been found but can't be parsed
                    return(true);
                }

                parameter.Data = value;
            }

            // execute notification callback asynchronously
            Task.Run(() => { element.OnReception(element); });

            return(true);
        }