Example #1
0
        public void GetNeighbors(XBeeModule xbeeModule, ZigBeeDevice device)
        {
            // clear list
            m_neighborList.Clear();

            // get 1st part of neighbor table
            m_payload[0] = 0;
            if (!SendCommand(xbeeModule, device))
            {
                m_neighborList.Clear();
                return;
            }

            // get rest of neighbor table
            // note that by Zigbee standard nb of entry in neighbor table can't exceed 1 byte
            m_payload[0] = Convert.ToByte(m_neighborList.Count);
            while (m_neighborList.Count < m_nbOfNeighbors)
            {
                if (!SendCommand(xbeeModule, device))
                {
                    m_neighborList.Clear();
                    return;
                }
            }
        }
Example #2
0
        internal ZigBeeEndPoint(ZigBeeEndPoint Other)
        {
            this.m_device = Other.m_device;

            this.Id                  = Other.Id;
            this.DeviceId            = Other.DeviceId;
            this.m_originalProfileId = Other.m_originalProfileId;
            this.m_commandProfileId  = Other.m_commandProfileId;

            this.Name            = Other.Name;
            this.Vendor          = Other.Vendor;
            this.Model           = Other.Model;
            this.Version         = Other.Version;
            this.FirmwareVersion = Other.FirmwareVersion;
            this.SerialNumber    = Other.SerialNumber;
            this.Description     = Other.Description;

            this.m_basicCluster = Other.m_basicCluster;
            this.m_inClusters   = Other.m_inClusters;
            this.m_outClusters  = Other.m_outClusters;

            try
            {
                this.Signals = new List <IAdapterSignal>(Other.Signals);
            }
            catch (OutOfMemoryException ex)
            {
                Debug.WriteLine(ex);
                throw;
            }
        }
Example #3
0
        protected bool SendCommand(XBeeModule xbeeModule, ZigBeeDevice destination, ZigBeeEndPoint endPoint = null)
        {
            bool retVal = false;

            // send ZigBee command then wait for response if required
            m_destination = destination;
            m_endPoint    = endPoint;
            if (m_responseRequired)
            {
                byte sequenceNumber = xbeeModule.SendZigBeeCommand(this);
                retVal = m_responseParsedEv.WaitOne(AdapterHelper.MAX_WAIT_TIMEOUT);
                if (!retVal)
                {
                    // no response received in time
                    xbeeModule.RemovePendingZigBeeCmd(sequenceNumber);
                }
            }
            else
            {
                xbeeModule.SendZigBeeCommandNoResponse(this);
                retVal = true;
            }

            return(retVal);
        }
Example #4
0
        public void RemoveCommands(ZigBeeDevice device)
        {
            // sanity check
            if (device == null)
            {
                return;
            }

            // remove all server command associated with a specific device
            lock (m_commandList)
            {
                // 1st build list of elements to remove then remove them from list
                List <ZclServerCommand> elementsToRemove = new List <ZclServerCommand>();
                foreach (var element in m_commandList)
                {
                    if (element.MatchDevice(device))
                    {
                        elementsToRemove.Add(element);
                    }
                }

                foreach (var element in elementsToRemove)
                {
                    m_commandList.Remove(element);
                }
            }
        }
Example #5
0
 public bool MatchDevice(ZigBeeDevice device)
 {
     if (m_cluster.EndPoint.Device.MacAddress == device.MacAddress)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #6
0
        internal ManagementLeave(ZigBeeDevice device)
        {
            m_isZdoCommand      = true;
            m_clusterId         = MANAGEMENT_LEAVE_CLUSTER_ID;
            m_responseClusterId = MANAGEMENT_LEAVE_RESPONSE_CLUSTER_ID;
            m_device            = device;

            Name         = EXPOSED_NAME;
            Description  = COMMAND_DESCRIPTION;
            InputParams  = new List <IAdapterValue>();
            OutputParams = new List <IAdapterValue>();
        }
Example #7
0
        private void ZclReportAttributeReception(ZclReportAttributes.SOURCE_INFO deviceInfo, UInt16 attributeId, object newValue)
        {
            ZigBeeDevice   device    = null;
            ZigBeeEndPoint endPoint  = null;
            ZclCluster     cluster   = null;
            ZclAttribute   attribute = null;

            // look for corresponding ZigBee device
            lock (m_deviceMap)
            {
                if (!m_deviceMap.TryGetValue(deviceInfo.macAddress, out device))
                {
                    // unknown device => do nothing
                    return;
                }
            }

            // look for corresponding end point
            if (!device.EndPointList.TryGetValue(deviceInfo.endpointId, out endPoint))
            {
                // unknown end point => do nothing
                return;
            }

            // look for corresponding cluster
            cluster = endPoint.GetCluster(deviceInfo.clusterId);
            if (cluster == null)
            {
                // unknown cluster => do nothing
                return;
            }

            // look for the corresponding attribute
            if (cluster.InternalAttributeList.TryGetValue(attributeId, out attribute))
            {
                // unknown attribute => do nothing
                return;
            }

            // update value
            attribute.Value.Data = newValue;

            // signal value of attribute has changed
            SignalChangeOfAttributeValue(endPoint, cluster, attribute);
        }
Example #8
0
        private void RemoveDevice(ZigBeeDevice zigBeeDevice)
        {
            // remove device from list (it will be garbaged collect later)
            lock (m_deviceMap)
            {
                m_deviceMap.Remove(zigBeeDevice.MacAddress);
            }

            // remove server commands that belong to that device
            m_zclServerCommandHandler.RemoveCommands(zigBeeDevice);

            // notify AllJoyn/BridgeRT
            //
            // note that a ZigBee endpoint is a exposed as a device on AllJoyn
            // => BridgeRT should be notified of removal of each enpoint of the removed ZigBee device
            foreach (var endPoint in zigBeeDevice.EndPointList)
            {
                NotifyBridgeRT(endPoint.Value, Constants.DEVICE_REMOVAL_SIGNAL, Constants.DEVICE_REMOVAL__DEVICE_HANDLE);
            }
        }
Example #9
0
        public bool GetListOfAttributeIds(XBeeModule xbeeModule, ZigBeeDevice device, ZigBeeEndPoint endPoint, UInt16 clusterId, out List <UInt16> attributeIdList)
        {
            m_status = ZclHelper.ZCL_ERROR_SUCCESS;

            // reset list of attribute Ids
            attributeIdList = null;
            m_attributeIdList.Clear();
            m_discoveryCompleted = false;

            // set cluster Id
            m_clusterId         = clusterId;
            m_responseClusterId = clusterId;

            // add header and ZCL payload to command payload
            while (!m_discoveryCompleted)
            {
                m_payload = new byte[m_zclHeader.Length + m_zclPayload.Length];
                Array.Copy(m_zclHeader, m_payload, m_zclHeader.Length);
                // update start Id in Zcl payload if necessary
                if (m_attributeIdList.Count != 0)
                {
                    UInt16 lastId = m_attributeIdList.Last();
                    lastId++;
                    byte[] newStartId = AdapterHelper.ToZigBeeFrame(lastId);
                    Array.Copy(newStartId, 0, m_zclPayload, 0, newStartId.Length);
                }
                Array.Copy(m_zclPayload, 0, m_payload, m_zclHeader.Length, m_zclPayload.Length);

                // send command
                if (!SendCommand(xbeeModule, device, endPoint) ||
                    m_status != ZclHelper.ZCL_ERROR_SUCCESS)
                {
                    return(false);
                }
            }

            // set out value
            attributeIdList = m_attributeIdList;

            return(true);
        }
Example #10
0
        public void GetEndPoints(XBeeModule xbeeModule, ZigBeeDevice device)
        {
            // sanity check
            if (null == device)
            {
                return;
            }

            // reset end point list
            m_endPointList.Clear();

            // set payload
            m_payload = AdapterHelper.ToZigBeeFrame(device.NetworkAddress);

            // send command
            if (!SendCommand(xbeeModule, device))
            {
                m_endPointList.Clear();
                return;
            }
        }
Example #11
0
        public void GetDescriptor(XBeeModule xbeeModule, ZigBeeDevice device, byte endPointId)
        {
            // sanity check
            if (null == device)
            {
                return;
            }

            // reset (in case descriptor has already been set by previous GetDescriptor call)
            Reset();

            // set payload
            // (network address of device then end point Id)
            byte[] tempBytes = AdapterHelper.ToZigBeeFrame(device.NetworkAddress);
            Array.Copy(tempBytes, m_payload, tempBytes.Length);
            m_payload[tempBytes.Length] = endPointId;

            // send command
            if (!SendCommand(xbeeModule, device))
            {
                Reset();
            }
        }
Example #12
0
        internal void Initialize(ZigBeeDevice device)
        {
            ZclAttribute attribute = null;

            // save away parent device
            m_device          = device;
            m_managementLeave = new ManagementLeave(m_device);

            ZigBeeProfileLibrary profileLibrary = ZigBeeProfileLibrary.Instance;
            string profileName;
            string deviceType;

            profileLibrary.GetProfileAndDeviceNames(m_originalProfileId, DeviceId, out profileName, out deviceType, out m_commandProfileId);

            // set name and description
            this.Description = profileName + " - " + deviceType;
            this.Name        = deviceType;

            // get some information from Basic cluster, e.g.: manufacturer name, model name, HW version, application version...
            if (m_basicCluster.InternalAttributeList.TryGetValue(BasicCluster.ATTRIBUTE_MANUFACTURER_NAME, out attribute))
            {
                object value;
                if (attribute.Read(out value))
                {
                    this.Vendor = (String)value;
                }
            }
            if (m_basicCluster.InternalAttributeList.TryGetValue(BasicCluster.ATTRIBUTE_MODEL_IDENTIFIER, out attribute))
            {
                object value;
                if (attribute.Read(out value))
                {
                    this.Model = (String)value;
                }
            }
            if (m_basicCluster.InternalAttributeList.TryGetValue(BasicCluster.ATTRIBUTE_HW_VERSION, out attribute))
            {
                object value;
                if (attribute.Read(out value))
                {
                    this.FirmwareVersion = Convert.ToUInt32((byte)value).ToString();
                }
            }
            if (m_basicCluster.InternalAttributeList.TryGetValue(BasicCluster.ATTRIBUTE_APPLICATION_VERSION, out attribute))
            {
                object value;
                if (attribute.Read(out value))
                {
                    this.Version = Convert.ToUInt32((byte)value).ToString();
                }
            }

            // create AllJoyn LSF if this device is a light
            ZigBeeProfileLibrary.DeviceType zigBeeDeviceType;
            if (profileLibrary.IsLight(m_originalProfileId, DeviceId, out zigBeeDeviceType))
            {
                m_lsfHandler = new LSFHandler(this, zigBeeDeviceType);
            }

            // create signals
            CreateSignals();
        }
Example #13
0
        public void Initialize(out ZigBeeDevice adapter)
        {
            // initialize communication with XBee module
            try
            {
                m_serialController.InitializeAsync().Wait();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("{0}.{1}: An exception occurred:\n    {2}", this.GetType().Name, nameof(this.Initialize), ex);
                throw;
            }
            m_serialController.OnByteReception += GetBytesFromModule;

            // make sure there is a valid command Id
            NextCommandId();

            // get information about XBee module
            //-----------------------------------

            // get hardware version
            HV_Command hvCommand = new HV_Command();

            if (hvCommand.SendAndWaitResponse(this))
            {
                m_HWVersion = hvCommand.HWVersion;
            }

            // get software version
            VR_Command vrCommand = new VR_Command();

            if (vrCommand.SendAndWaitResponse(this))
            {
                m_SWVersion = vrCommand.SWVersion;
            }

            // get MAC address
            byte[]     macAddress = null;
            SL_Command slCommand  = new SL_Command();
            SH_Command shCommand  = new SH_Command();

            if (slCommand.SendAndWaitResponse(this) &&
                shCommand.SendAndWaitResponse(this))
            {
                macAddress = new byte[shCommand.MacAddressHightPart.Length + slCommand.MacAddressLowerPart.Length];
                Array.Copy(shCommand.MacAddressHightPart, 0, macAddress, 0, shCommand.MacAddressHightPart.Length);
                Array.Copy(slCommand.MacAddressLowerPart, 0, macAddress, shCommand.MacAddressHightPart.Length, slCommand.MacAddressLowerPart.Length);
            }

            MY_Command myCommand = new MY_Command();

            myCommand.SendAndWaitResponse(this);

            // set RX indicator mode
            // note this API mode is necessary to get response to ZDO and ZCL commands
            AO_Command aoCommand = new AO_Command();

            aoCommand.SetRxIndicatorMode(this);

            adapter = new ZigBeeDevice(myCommand.NetworkAddress, AdapterHelper.UInt64FromXbeeFrame(macAddress, 0), false);
        }
Example #14
0
        private void AddDeviceInDeviceList(UInt16 networkAddress, UInt64 macAddress, bool isEndDevice)
        {
            bool         deviceFound     = false;
            bool         addDeviceInList = false;
            ZigBeeDevice device          = null;

            Logger.TraceDevice(networkAddress, macAddress);

            lock (m_deviceMap)
            {
                deviceFound = m_deviceMap.TryGetValue(macAddress, out device);
            }

            if (!deviceFound)
            {
                // the device isn't in the list yet
                device = new ZigBeeDevice(networkAddress, macAddress, isEndDevice);

                // get end points and supported clusters
                ActiveEndPoints activeEndPointsCommand = new ActiveEndPoints();
                activeEndPointsCommand.GetEndPoints(m_xBeeModule, device);

                foreach (var endPointId in activeEndPointsCommand.EndPointList)
                {
                    SimpleDescriptor descriptor = new SimpleDescriptor();
                    descriptor.GetDescriptor(m_xBeeModule, device, endPointId);
                    Logger.TraceDeviceDetailed(device.MacAddress, endPointId, descriptor);
                    foreach (var clusterId in descriptor.InClusterList)
                    {
                        if (m_zclClusterFactory.IsClusterSupported(clusterId) &&
                            device.AddClusterToEndPoint(true, endPointId, descriptor.ProfileId, descriptor.DeviceId, clusterId, this))
                        {
                            // only add device in list if at least 1 cluster is supported
                            addDeviceInList = true;
                        }
                    }
                    foreach (var clusterId in descriptor.OutClusterList)
                    {
                        if (m_zclClusterFactory.IsClusterSupported(clusterId) &&
                            device.AddClusterToEndPoint(false, endPointId, descriptor.ProfileId, descriptor.DeviceId, clusterId, this))
                        {
                            // only add device in list if at least 1 cluster is supported
                            addDeviceInList = true;
                        }
                    }
                }
            }
            else
            {
                // the device is already in list so just refresh its network address.
                // note that mac address will never change but network address can, e.g.: if end device connects to another router
                device.NetworkAddress = networkAddress;
            }

            // add device in list if necessary
            if (addDeviceInList)
            {
                lock (m_deviceMap)
                {
                    // add device in list if it has not been added between beginning of this routine and now
                    ZigBeeDevice tempDevice = null;
                    if (!m_deviceMap.TryGetValue(device.MacAddress, out tempDevice))
                    {
                        m_deviceMap.Add(device.MacAddress, device);
                    }
                    else
                    {
                        // device has been added => update network address of already existing device
                        // give up with device that has been created and use the already existing device instead
                        tempDevice.NetworkAddress = device.NetworkAddress;
                        device = tempDevice;
                    }
                }
            }

            // notify devices to bridgeRT
            // note end points are devices as far as BridgeRT is concerned
            foreach (var endpoint in device.EndPointList)
            {
                NotifyBridgeRT(endpoint.Value, Constants.DEVICE_ARRIVAL_SIGNAL, Constants.DEVICE_ARRIVAL__DEVICE_HANDLE);
            }
        }