Exemplo n.º 1
0
        public SpinelIPv6Address ReadIp6Address()
        {
            SpinelIPv6Address ipAddress = new SpinelIPv6Address();

            ipAddress.bytes = ReadItems(ipAddress.bytes.Length);
            return(ipAddress);
        }
Exemplo n.º 2
0
        public static IPv6Address SpinelIPtoSystemIP(SpinelIPv6Address ipAddress)
        {
            if (ipAddress == null)
            {
                return(null);
            }

            return(new IPv6Address(ipAddress.bytes));
        }
Exemplo n.º 3
0
        private void ParseRX(byte[] frameIn)
        {
            SpinelDecoder mDecoder    = new SpinelDecoder();
            object        ncpResponse = null;

            mDecoder.Init(frameIn);

            byte header = mDecoder.FrameHeader;

            if ((SpinelHeaderFlag & header) != SpinelHeaderFlag)
            {
                throw new SpinelFormatException("Header parsing error.");
            }

            uint command   = mDecoder.FrameCommand;
            uint properyId = mDecoder.FramePropertyId;

            if (properyId == SpinelProperties.SPINEL_PROP_THREAD_CHILD_TABLE)
            {
                if (command == SpinelCommands.RSP_PROP_VALUE_INSERTED || command == SpinelCommands.RSP_PROP_VALUE_REMOVED)
                {
                    return;
                }
            }

            object tempObj = null;

            switch (properyId)
            {
            case SpinelProperties.PROP_NCP_VERSION:
                ncpResponse = mDecoder.ReadUtf8();
                break;

            case SpinelProperties.PROP_LAST_STATUS:
                ncpResponse = mDecoder.ReadUintPacked();
                break;

            case SpinelProperties.PROP_INTERFACE_TYPE:
                ncpResponse = mDecoder.ReadUintPacked();
                break;

            case SpinelProperties.PROP_VENDOR_ID:
                ncpResponse = mDecoder.ReadUintPacked();
                break;

            case SpinelProperties.SPINEL_PROP_NET_NETWORK_NAME:

                ncpResponse = mDecoder.ReadUtf8();
                break;

            case SpinelProperties.SPINEL_PROP_MAC_SCAN_STATE:
                ncpResponse = mDecoder.ReadUint8();
                break;

            case SpinelProperties.SPINEL_PROP_MAC_SCAN_MASK:
                tempObj = mDecoder.ReadFields("A(C)");

                if (tempObj != null)
                {
                    ArrayList channels = (ArrayList)tempObj;
                    ncpResponse = (byte[])channels.ToArray(typeof(byte));
                }

                break;

            case SpinelProperties.SPINEL_PROP_MAC_SCAN_PERIOD:
                ncpResponse = mDecoder.ReadUint16();
                break;

            case SpinelProperties.SPINEL_PROP_MAC_SCAN_BEACON:
                ncpResponse = mDecoder.ReadFields("Cct(ESSC)t(iCUdd)");
                break;

            case SpinelProperties.SPINEL_PROP_MAC_ENERGY_SCAN_RESULT:
                ncpResponse = mDecoder.ReadFields("Cc");
                break;

            case SpinelProperties.PROP_PROTOCOL_VERSION:

                tempObj = mDecoder.ReadFields("ii");

                if (tempObj != null)
                {
                    ArrayList protocol = (ArrayList)tempObj;
                    ncpResponse = (uint[])protocol.ToArray(typeof(uint));
                }

                break;

            case SpinelProperties.PROP_CAPS:

                tempObj = mDecoder.ReadFields("A(i)");

                if (tempObj != null)
                {
                    ArrayList      caps      = (ArrayList)tempObj;
                    Capabilities[] capsArray = new Capabilities[caps.Count];
                    int            index     = 0;

                    foreach (var capsValue in caps)
                    {
                        capsArray[index] = (Capabilities)Convert.ToUInt32(capsValue);
                        index++;
                    }

                    ncpResponse = capsArray;
                }

                break;

            case SpinelProperties.SPINEL_PROP_MSG_BUFFER_COUNTERS:

                tempObj = mDecoder.ReadFields("SSSSSSSSSSSSSSSS");

                if (tempObj != null)
                {
                    ArrayList counters = (ArrayList)tempObj;
                    ncpResponse = (ushort[])counters.ToArray(typeof(ushort));
                }

                break;

            case SpinelProperties.PROP_PHY_CHAN:
                ncpResponse = mDecoder.ReadUint8();
                break;

            case SpinelProperties.PROP_PHY_CHAN_SUPPORTED:
                tempObj = mDecoder.ReadFields("A(C)");

                if (tempObj != null)
                {
                    ArrayList channels = (ArrayList)tempObj;
                    ncpResponse = (byte[])channels.ToArray(typeof(byte));
                }

                break;

            case SpinelProperties.SPINEL_PROP_IPV6_ADDRESS_TABLE:

                tempObj = mDecoder.ReadFields("A(t(6CLL))");
                ArrayList ipAddresses = new ArrayList();

                if (tempObj != null)
                {
                    ArrayList addressArray = tempObj as ArrayList;

                    foreach (ArrayList addrInfo in addressArray)
                    {
                        object[]          ipProps = addrInfo.ToArray();
                        SpinelIPv6Address ipaddr  = ipProps[0] as SpinelIPv6Address;
                        ipAddresses.Add(ipaddr);
                    }
                }

                if (ipAddresses.Count > 0)
                {
                    ncpResponse = ipAddresses.ToArray(typeof(SpinelIPv6Address));
                }

                break;

            case SpinelProperties.SPINEL_PROP_NET_IF_UP:
                ncpResponse = mDecoder.ReadBool();
                break;

            case SpinelProperties.SPINEL_PROP_NET_STACK_UP:
                ncpResponse = mDecoder.ReadBool();
                break;

            case SpinelProperties.SPINEL_PROP_NET_REQUIRE_JOIN_EXISTING:
                ncpResponse = mDecoder.ReadBool();
                break;

            case SpinelProperties.SPINEL_PROP_MAC_15_4_PANID:
                ncpResponse = mDecoder.ReadUint16();
                break;

            case SpinelProperties.SPINEL_PROP_NET_XPANID:
                ncpResponse = mDecoder.ReadData();
                break;

            case SpinelProperties.SPINEL_PROP_NET_ROLE:
                ncpResponse = mDecoder.ReadUint8();
                break;

            case SpinelProperties.SPINEL_PROP_NET_MASTER_KEY:
                ncpResponse = mDecoder.ReadData();
                break;

            case SpinelProperties.PROP_STREAM_NET:
                tempObj = mDecoder.ReadFields("dD");
                if (tempObj != null)
                {
                    ArrayList responseArray = tempObj as ArrayList;
                    ncpResponse = responseArray[0];
                }
                break;

            case SpinelProperties.SPINEL_PROP_IPV6_LL_ADDR:
                SpinelIPv6Address ipaddrLL = mDecoder.ReadIp6Address();
                ncpResponse = ipaddrLL;
                break;

            case SpinelProperties.SPINEL_PROP_IPV6_ML_ADDR:
                SpinelIPv6Address ipaddrML = mDecoder.ReadIp6Address();
                ncpResponse = ipaddrML;
                break;

            case SpinelProperties.SPINEL_PROP_MAC_15_4_LADDR:
                SpinelEUI64 spinelEUI64 = mDecoder.ReadEui64();
                ncpResponse = spinelEUI64;
                break;

            case SpinelProperties.PROP_HWADDR:
                SpinelEUI64 hwaddr = mDecoder.ReadEui64();
                ncpResponse = hwaddr;
                break;

                //case SpinelProperties.SPINEL_PROP_IPV6_ML_PREFIX:
                //    ncpResponse = mDecoder.ReadFields("6C");
                //    break;
            }

            FrameData frameData = new FrameData(mDecoder.FramePropertyId, mDecoder.FrameHeader, mDecoder.GetFrameLoad(), ncpResponse);

            waitingQueue.Enqueue(frameData);
        }
Exemplo n.º 4
0
        private void FrameDataReceived(FrameData frameData)
        {
            uint properyId = frameData.PropertyId;

            if (properyId == SpinelProperties.PROP_LAST_STATUS)
            {
                LastStatus lastStatus = (LastStatus)Convert.ToInt32(frameData.Response);
                OnLastStatusHandler(lastStatus);
                return;
            }
            else if (properyId == SpinelProperties.PROP_STREAM_NET)
            {
                byte[] ipv6frame = (byte[])frameData.Response;

                if (OnPacketReceived != null)
                {
                    OnPacketReceived(this, ipv6frame);
                }

                return;
            }
            else if (properyId == SpinelProperties.SPINEL_PROP_MAC_SCAN_STATE)
            {
                byte scanState = Convert.ToByte(frameData.Response);

                if (scanState == 0)
                {
                    scanThread.Set();
                }
            }
            else if (properyId == SpinelProperties.SPINEL_PROP_MAC_SCAN_BEACON)
            {
                ArrayList scanInfo = (ArrayList)frameData.Response;

                LowpanBeaconInfo lowpanBeaconInfo = new LowpanBeaconInfo();

                lowpanBeaconInfo.Channel = (byte)scanInfo[0];
                lowpanBeaconInfo.Rssi    = (sbyte)scanInfo[1];

                ArrayList   tempObj = scanInfo[2] as ArrayList;
                SpinelEUI64 mac     = (SpinelEUI64)tempObj[0];

                lowpanBeaconInfo.HardwareAddress = new HardwareAddress(mac.bytes);
                lowpanBeaconInfo.ShortAddress    = (ushort)tempObj[1];
                lowpanBeaconInfo.PanId           = (ushort)tempObj[2];
                lowpanBeaconInfo.LQI             = (byte)tempObj[3];

                tempObj = scanInfo[3] as ArrayList;

                lowpanBeaconInfo.Protocol    = (uint)tempObj[0];
                lowpanBeaconInfo.Flags       = (byte)tempObj[1];
                lowpanBeaconInfo.NetworkName = (string)tempObj[2];
                lowpanBeaconInfo.XpanId      = (byte[])tempObj[3];

                scanMacResult.Add(lowpanBeaconInfo);


                return;
            }
            else if (properyId == SpinelProperties.SPINEL_PROP_MAC_ENERGY_SCAN_RESULT)
            {
                ArrayList energyScan = (ArrayList)frameData.Response;

                LowpanChannelInfo lowpanChannelInfo = new LowpanChannelInfo();

                lowpanChannelInfo.Channel = (byte)energyScan[0];
                lowpanChannelInfo.Rssi    = (sbyte)energyScan[1];
                scanEnergyResult.Add(lowpanChannelInfo);

                return;
            }

            if (frameData.TID == 0x80)
            {
                switch (properyId)
                {
                case SpinelProperties.SPINEL_PROP_NET_ROLE:
                    State newRole = (State)Convert.ToByte(frameData.Response);
                    if (state != newRole)
                    {
                        state = newRole;
                        OnLowpanStateChanged();
                    }
                    break;

                case SpinelProperties.SPINEL_PROP_IPV6_LL_ADDR:

                    if (frameData.Response == null)
                    {
                        ipLinkLocal = null;
                        return;
                    }

                    SpinelIPv6Address ipaddrLL = (SpinelIPv6Address)frameData.Response;
                    ipLinkLocal = new IPv6Address(ipaddrLL.bytes);

                    if (OnIpChanged != null)
                    {
                        OnIpChanged();
                    }

                    break;

                case SpinelProperties.SPINEL_PROP_IPV6_ML_ADDR:

                    if (frameData.Response == null)
                    {
                        ipMeshLocal = null;
                        return;
                    }

                    SpinelIPv6Address ipaddrML = (SpinelIPv6Address)frameData.Response;
                    ipMeshLocal = new IPv6Address(ipaddrML.bytes);
                    break;

                case SpinelProperties.SPINEL_PROP_IPV6_ADDRESS_TABLE:
                    ipAddresses = NetUtilities.SpinelIPtoSystemIP((SpinelIPv6Address[])frameData.Response);
                    break;

                case SpinelProperties.SPINEL_PROP_IPV6_MULTICAST_ADDRESS_TABLE:
                    ipMulticastAddresses = NetUtilities.SpinelIPtoSystemIP((SpinelIPv6Address[])frameData.Response);
                    break;

                //case SpinelProperties.PROP_NET_SAVED:
                //case SpinelProperties.PROP_NET_IF_UP:
                //    break;
                //case SpinelProperties.PROP_NET_STACK_UP:
                //    break;

                //case SpinelProperties.PROP_NET_NETWORK_NAME:
                //case SpinelProperties.PROP_NET_XPANID:
                //case SpinelProperties.PROP_NET_MASTER_KEY:
                //case SpinelProperties.PROP_NET_KEY_SEQUENCE_COUNTER:
                //case SpinelProperties.PROP_NET_PARTITION_ID:
                //case SpinelProperties.PROP_NET_KEY_SWITCH_GUARDTIME:
                //    break;

                //case SpinelProperties.SPINEL_PROP_THREAD_LEADER_ADDR:
                //case SpinelProperties.SPINEL_PROP_THREAD_PARENT:
                //case SpinelProperties.SPINEL_PROP_THREAD_CHILD_TABLE:
                //case SpinelProperties.SPINEL_PROP_THREAD_LEADER_RID:
                //case SpinelProperties.SPINEL_PROP_THREAD_LEADER_WEIGHT:
                //case SpinelProperties.SPINEL_PROP_THREAD_LOCAL_LEADER_WEIGHT:
                //case SpinelProperties.SPINEL_PROP_THREAD_NETWORK_DATA:
                //case SpinelProperties.SPINEL_PROP_THREAD_NETWORK_DATA_VERSION:
                //case SpinelProperties.SPINEL_PROP_THREAD_STABLE_NETWORK_DATA:
                //case SpinelProperties.SPINEL_PROP_THREAD_STABLE_NETWORK_DATA_VERSION:
                //case SpinelProperties.SPINEL_PROP_THREAD_ASSISTING_PORTS:
                //case SpinelProperties.SPINEL_PROP_THREAD_ALLOW_LOCAL_NET_DATA_CHANGE:
                //case SpinelProperties.SPINEL_PROP_THREAD_MODE:
                //    break;
                //case SpinelProperties.SPINEL_PROP_THREAD_ON_MESH_NETS:
                //    break;
                //case SpinelProperties.SPINEL_PROP_THREAD_OFF_MESH_ROUTES:
                //    break;



                //case SpinelProperties.SPINEL_PROP_IPV6_ML_PREFIX:
                //    break;

                //case SpinelProperties.SPINEL_PROP_IPV6_ROUTE_TABLE:
                //case SpinelProperties.SPINEL_PROP_IPV6_ICMP_PING_OFFLOAD:
                //case SpinelProperties.SPINEL_PROP_IPV6_ICMP_PING_OFFLOAD_MODE:
                //    break;



                //case SpinelProperties.SPINEL_PROP_THREAD_CHILD_TIMEOUT:
                //case SpinelProperties.SPINEL_PROP_THREAD_RLOC16:
                //case SpinelProperties.SPINEL_PROP_THREAD_ROUTER_UPGRADE_THRESHOLD:
                //case SpinelProperties.SPINEL_PROP_THREAD_CONTEXT_REUSE_DELAY:
                //case SpinelProperties.SPINEL_PROP_THREAD_NETWORK_ID_TIMEOUT:
                //case SpinelProperties.SPINEL_PROP_THREAD_ACTIVE_ROUTER_IDS:
                //case SpinelProperties.SPINEL_PROP_THREAD_RLOC16_DEBUG_PASSTHRU:
                //case SpinelProperties.SPINEL_PROP_THREAD_ROUTER_ROLE_ENABLED:
                //case SpinelProperties.SPINEL_PROP_THREAD_ROUTER_DOWNGRADE_THRESHOLD:
                //case SpinelProperties.SPINEL_PROP_THREAD_ROUTER_SELECTION_JITTER:
                //case SpinelProperties.SPINEL_PROP_THREAD_PREFERRED_ROUTER_ID:
                //case SpinelProperties.SPINEL_PROP_THREAD_CHILD_COUNT_MAX:
                //    break;
                //case SpinelProperties.SPINEL_PROP_THREAD_NEIGHBOR_TABLE:
                //    break;
                //case SpinelProperties.SPINEL_PROP_THREAD_LEADER_NETWORK_DATA:
                //    break;

                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_NEW_CHANNEL:
                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_DELAY:
                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_SUPPORTED_CHANNELS:
                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_FAVORED_CHANNELS:
                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_CHANNEL_SELECT:
                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_AUTO_SELECT_ENABLED:
                //case SpinelProperties.SPINEL_PROP_CHANNEL_MANAGER_AUTO_SELECT_INTERVAL:
                //case SpinelProperties.SPINEL_PROP_THREAD_NETWORK_TIME:
                //case SpinelProperties.SPINEL_PROP_TIME_SYNC_PERIOD:
                //case SpinelProperties.SPINEL_PROP_TIME_SYNC_XTAL_THRESHOLD:
                //case SpinelProperties.SPINEL_PROP_CHILD_SUPERVISION_INTERVAL:
                //case SpinelProperties.SPINEL_PROP_CHILD_SUPERVISION_CHECK_TIMEOUT:
                //case SpinelProperties.SPINEL_PROP_RCP_VERSION:
                //case SpinelProperties.SPINEL_PROP_SLAAC_ENABLED:
                //    break;
                //case SpinelProperties.SPINEL_PROP_PARENT_RESPONSE_INFO:
                //    break;
                //case SpinelProperties.PROP_LAST_STATUS:

                //    LastStatus lastStatus = (LastStatus)Convert.ToInt32(frameData.Response);
                //    OnLastStatusHandler(lastStatus);
                //    break;
                default:
                    break;
                }
            }
        }