Пример #1
0
            public RemoteXBeeDevice Connect(String remoteNodeID)
            {
                if (Monitor.TryEnter(Coordinator, COORDINATOR_LOCK_TIMEOUT))
                {
                    RemoteXBeeDevice rxb = null;
                    try
                    {
                        if (!Coordinator.IsOpen)
                        {
                            //Console.WriteLine("Opening coordinator while opening connection to {0}", remoteNodeID);
                            Coordinator.Open();
                        }

                        //always perform a network discovery
                        XBeeNetwork network = Coordinator.GetNetwork();
                        if (network == null)
                        {
                            throw new NetworkNotFoundException(String.Format("Open: Cannot find coordinator network when looking for NodeID {0}", remoteNodeID));
                        }
                        rxb = network.DiscoverDevice(remoteNodeID);
                        if (rxb == null)
                        {
                            throw new BoardNotFoundException(String.Format("Open: Cannot find XBee with NodeID {0}", remoteNodeID));
                        }

                        //delay for a while ...
                        System.Threading.Thread.Sleep(DELAY_AFTER_CONNECT);

                        //return the remote xb device to be used for calls to send data
                        return(rxb);
                    }
                    finally
                    {
                        Monitor.Exit(Coordinator);
                    }
                }
                else
                {
                    throw new TimeoutException(String.Format("TryConnect: could not obtain lock when trying to connect to NodeID {0}", remoteNodeID));
                }
            }
Пример #2
0
        /**
         * Returns the remote XBee device from where the given package was sent
         * from.
         *
         * <p><b>This is for internal use only.</b></p>
         *
         * <p>If the package does not contain information about the source, this
         * method returns {@code null} (for example, {@code ModemStatusPacket}).</p>
         *
         * <p>First the device that sent the provided package is looked in the
         * network of the local XBee device. If the remote device is not in the
         * network, it is automatically added only if the packet contains
         * information about the origin of the package.</p>
         *
         * @param packet The packet sent from the remote device.
         *
         * @return The remote XBee device that sends the given packet. It may be
         *         {@code null} if the packet is not a known frame (see
         *         {@link APIFrameType}) or if it does not contain information of
         *         the source device.
         *
         * @throws ArgumentNullException if {@code packet == null}
         * @throws XBeeException if any error occur while adding the device to the
         *                       network.
         */
        public RemoteXBeeDevice GetRemoteXBeeDeviceFromPacket(XBeeAPIPacket packet)         /*throws XBeeException*/
        {
            if (packet == null)
            {
                throw new ArgumentNullException("XBee API packet cannot be null.");
            }

            XBeeAPIPacket apiPacket = (XBeeAPIPacket)packet;
            APIFrameType  apiType   = apiPacket.FrameType;

            if (apiType == APIFrameType.UNKNOWN)
            {
                return(null);
            }

            RemoteXBeeDevice remoteDevice = null;
            XBee64BitAddress addr64       = null;
            XBee16BitAddress addr16       = null;

            XBeeNetwork network = xbeeDevice.GetNetwork();

            switch (apiType)
            {
            case APIFrameType.RECEIVE_PACKET:
                ReceivePacket receivePacket = (ReceivePacket)apiPacket;
                addr64       = receivePacket.get64bitSourceAddress();
                addr16       = receivePacket.get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_64:
                RX64Packet rx64Packet = (RX64Packet)apiPacket;
                addr64       = rx64Packet.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_16:
                RX16Packet rx16Packet = (RX16Packet)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16Packet.Get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr16);
                break;

            case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket;
                addr64       = ioSamplePacket.get64bitSourceAddress();
                addr16       = ioSamplePacket.get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_64:
                RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket;
                addr64       = rx64IOPacket.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_16:
                RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16IOPacket.get16bitSourceAddress();
                remoteDevice = network.GetDevice(addr16);
                break;

            default:
                // Rest of the types are considered not to contain information
                // about the origin of the packet.
                return(remoteDevice);
            }

            // If the origin is not in the network, add it.
            if (remoteDevice == null)
            {
                remoteDevice = createRemoteXBeeDevice(addr64, addr16, null);
                network.addRemoteDevice(remoteDevice);
            }

            return(remoteDevice);
        }
Пример #3
0
        /// <summary>
        /// Returns the remote XBee device from where the given package was sent from.
        /// </summary>
        /// <remarks>This is for internal use only.
        ///
        /// If the package does not contain information about the source, this method returns <c>null</c>
        /// (for example, <see cref="ModemStatusPacket"/>).
        ///
        /// First the device that sent the provided package is looked in the network of the local
        /// XBee device. If the remote device is not in the network, it is automatically added only
        /// if the packet contains information about the origin of the package.</remarks>
        /// <param name="apiPacket">The packet sent from the remote device.</param>
        /// <returns>The remote XBee device that sends the given packet. It may be <c>null</c> if the
        /// packet is not a known frame (<see cref="APIFrameType"/>) or if it does not contain information
        /// of the source device.</returns>
        /// <exception cref="ArgumentNullException">If <c><paramref name="apiPacket"/> == null</c>.</exception>
        /// <exception cref="XBeeException">If any error occurred while adding the device to the
        /// network.</exception>
        public RemoteXBeeDevice GetRemoteXBeeDeviceFromPacket(XBeeAPIPacket apiPacket)
        {
            if (apiPacket == null)
            {
                throw new ArgumentNullException("XBee API packet cannot be null.");
            }

            APIFrameType apiType = apiPacket.FrameType;

            RemoteXBeeDevice remoteDevice = null;
            XBee64BitAddress addr64       = null;
            XBee16BitAddress addr16       = null;

            XBeeNetwork network = xbeeDevice.GetNetwork();

            switch (apiType)
            {
            case APIFrameType.RECEIVE_PACKET:
                ReceivePacket receivePacket = (ReceivePacket)apiPacket;
                addr64 = receivePacket.SourceAddress64;
                addr16 = receivePacket.SourceAddress16;
                if (!addr64.Equals(XBee64BitAddress.UNKNOWN_ADDRESS))
                {
                    remoteDevice = network.GetDevice(addr64);
                }
                else if (!addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS))
                {
                    remoteDevice = network.GetDevice(addr16);
                }
                break;

            case APIFrameType.RX_64:
                RX64Packet rx64Packet = (RX64Packet)apiPacket;
                addr64       = rx64Packet.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_16:
                RX16Packet rx16Packet = (RX16Packet)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16Packet.SourceAddress16;
                remoteDevice = network.GetDevice(addr16);
                break;

            case APIFrameType.IO_DATA_SAMPLE_RX_INDICATOR:
                IODataSampleRxIndicatorPacket ioSamplePacket = (IODataSampleRxIndicatorPacket)apiPacket;
                addr64       = ioSamplePacket.SourceAddress64;
                addr16       = ioSamplePacket.SourceAddress16;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_64:
                RX64IOPacket rx64IOPacket = (RX64IOPacket)apiPacket;
                addr64       = rx64IOPacket.SourceAddress64;
                remoteDevice = network.GetDevice(addr64);
                break;

            case APIFrameType.RX_IO_16:
                RX16IOPacket rx16IOPacket = (RX16IOPacket)apiPacket;
                addr64       = XBee64BitAddress.UNKNOWN_ADDRESS;
                addr16       = rx16IOPacket.SourceAddress16;
                remoteDevice = network.GetDevice(addr16);
                break;

            case APIFrameType.EXPLICIT_RX_INDICATOR:
                ExplicitRxIndicatorPacket explicitDataPacket = (ExplicitRxIndicatorPacket)apiPacket;
                addr64       = explicitDataPacket.SourceAddress64;
                addr16       = explicitDataPacket.SourceAddress16;
                remoteDevice = network.GetDevice(addr64);
                break;

            default:
                // Rest of the types are considered not to contain information
                // about the origin of the packet.
                return(remoteDevice);
            }

            // If the origin is not in the network, add it.
            if (remoteDevice == null)
            {
                remoteDevice = CreateRemoteXBeeDevice(addr64, addr16, null);
                if (!addr64.Equals(XBee64BitAddress.UNKNOWN_ADDRESS) || !addr16.Equals(XBee16BitAddress.UNKNOWN_ADDRESS))
                {
                    network.AddRemoteDevice(remoteDevice);
                }
            }

            return(remoteDevice);
        }