Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }