Example #1
0
        public void DataRequest(
            UInt16 targetAddr,
            ref Frame sdu,
            Byte sduHandle,
            DataConfirmHandler handler)
        {
            if (targetAddr == _addrShort)
            {
                // deliver frame to ourself
                if (handler != null)
                    handler.Invoke(_net, sduHandle, Status.Success);

                _data.HandleDataIndication(targetAddr, targetAddr, ref sdu);
            }
            else
            {
                // encapsulate and sent as 6LoWPAN
                Message.Data data = new Message.Data();
                if (data.WriteToFrameHeader(sdu))
                {
                    DataRequest6LoWPAN(targetAddr, ref sdu, sduHandle, handler);
                }
                else
                {
                    if (handler != null)
                        handler.Invoke(_net, sduHandle, Status.InvalidFrame);
                }
            }

            Frame.Release(ref sdu);
        }
Example #2
0
        private void HandleData(
            UInt16 srcAddr,
            UInt16 dstAddr,
            ref Frame sdu)
        {
            Messages6LoWPAN.Dispatch dispatch;
            if (!Messages6LoWPAN.GetType(sdu, out dispatch))
                return;

            bool consume = false; // receive locally
            bool forward = false; // forward to another node

            UInt16 origAddr = srcAddr; // originator
            UInt16 targetAddr = dstAddr; // target
            bool haveMeshHeader = false;
            Messages6LoWPAN.MeshHeader mh = new Messages6LoWPAN.MeshHeader();
            if (dispatch == Messages6LoWPAN.Dispatch.Mesh)
            {
                if (!mh.ReadFromFrameHeader(sdu) || !Messages6LoWPAN.GetType(sdu, out dispatch))
                    return;
                haveMeshHeader = true;
                origAddr = mh.originatorAddress;
                targetAddr = mh.finalAddress;
            }

            bool haveBroadcastHeader = false;
            Messages6LoWPAN.BroadcastHeader bh = new Messages6LoWPAN.BroadcastHeader();
            if (dispatch == Messages6LoWPAN.Dispatch.IpPacket && bh.ReadFromFrameHeader(sdu))
            {
                haveBroadcastHeader = true;
                bool usefull = _routingTable.CheckBroadcastSeqNo(origAddr, bh.seqNo) && (origAddr != _addrShort);
                if (!usefull || !haveMeshHeader || !Messages6LoWPAN.GetType(sdu, out dispatch))
                    return;
            }

            bool multicast = IsMulticast(targetAddr);
            consume = (targetAddr == _addrShort || multicast);
            forward = (targetAddr != _addrShort || multicast) && haveMeshHeader && (mh.HopsLeft > 0);

            if (forward)
            {
                mh.HopsLeft--;
                // currently we always need to reallocate a frame to be forwarded, since mac header space is worst case assumption
                Frame frame = Frame.GetFrame(_macHeader + Messages6LoWPAN.MeshHeader.cLengthMax + Messages6LoWPAN.BroadcastHeader.cLength,
                    sdu.LengthDataUsed + _macTailer);
                frame.WriteToBack(sdu);

                bool ok = true;
                UInt16 nextHop;
                if (multicast)
                {
                    nextHop = cBroadcastShortAddr;
                    ok = haveBroadcastHeader && bh.WriteToFrameHeader(frame);
                }
                else
                {
                    bool routeHasError;
                    ok = _routingTable.GetRoute(targetAddr, out nextHop, out routeHasError);
                    if (!ok || routeHasError)
                    {
                        SendRouteError(origAddr, targetAddr, !routeHasError);
                    }
                }

                ok &= mh.WriteToFrameHeader(frame);
                if (ok)
                {
                    MacDataRequest(nextHop, ref frame);
                }

                Frame.Release(ref frame);
            }

            if (consume)
            {
                if (dispatch == Messages6LoWPAN.Dispatch.NonLowPan)
                {
                    Message.Type type;
                    if (Message.GetType(sdu, out type))
                    {
                        switch (type)
                        {
                            case Message.Type.Data:
                                {
                                    Message.Data data = new Message.Data();
                                    if (data.ReadFromFrameHeader(sdu))
                                    {
                                        _data.HandleDataIndication(origAddr, targetAddr, ref sdu);
                                    }
                                    break;
                                }
                            case Message.Type.DiscoveryReply:
                                {
                                    Message.DiscoveryReply drep;
                                    if (_isAddrServer && drep.ReadFromFrame(sdu))
                                    {
                                        _addrServer.HandleDiscoveryReply(origAddr);
                                    }
                                    break;
                                }
                        }
                    }
                }
                else
                { // must be 6LoWPAN frame
                    _data.HandleDataIndication6Low(origAddr, targetAddr, ref sdu);
                }
            }

            Frame.Release(ref sdu);
        }