public override void OnInformation(LLCP_Link link, byte[] ServiceDataUnit)
        {
            SNEP_ClientInstanceData instance = (SNEP_ClientInstanceData)link.InstanceData;

            Trace.WriteLine("SNEP Client: I...");
            instance.SomethingSent = false;

            if (ServiceDataUnit.Length < 2)
            {
                /* Frame is too short to have the RESPONSE header */
                Trace.WriteLine("SNEP Client: I.Bad length");
                goto close;
            }

            /* Get version */
            byte version = ServiceDataUnit[0];

            if ((version < 0x10) || (version >= 0x20))
            {
                /* Version not supported */
                Trace.WriteLine("SNEP Client: I.Bad version");
                goto close;
            }

            /* Response code */
            byte response = ServiceDataUnit[1];

            switch (response)
            {
            case SNEP.RSP_CONTINUE:
                /* Message successfully transmited, let's continue */
                instance.MayContinue = true;
                Trace.WriteLine("SNEP Client: I.Continue -> send RR, then second I");
                link.Send_RR();
                ClientNEXT(link, instance);
                break;

            case SNEP.RSP_SUCCESS:
                /* Message successfully transmited, let's disconnect */
                Trace.WriteLine("SNEP Client: I.Success -> send RR");
                /* Acknowledge */
                link.Send_RR();
                if ((OnMessageSent != null) && (!instance.MessageSentSent))
                {
                    OnMessageSent();
                    instance.MessageSentSent = true;
                }
                break;

            default:
                /* An error has occured */
                Trace.WriteLine("SNEP Client: I.Bad Opcode");
                goto close;
            }

            /*  */
            return;

close:

            Trace.WriteLine("SNEP Client: Force exit");
            link.Send_Disc();
        }
        public override bool ProcessPDU(LLCP_Link link, LLCP_PDU recv_pdu)
        {
            /* Server action */
            /* ------------- */

            link.RemotePort = recv_pdu.SSAP;
            link.LocalPort  = (byte)(_server_port & 0x3F);

            switch (recv_pdu.PTYPE)
            {
            case LLCP_PDU.PTYPE_CONNECT:
                Trace.WriteLine("> CONNECT");

                /* The client wants to connect us */
                link.State = LLCP_Link.StateActive;

                if (!OnConnect(link, recv_pdu.Payload))
                {
                    /* Failed */
                    link.Send_DM(LLCP_DM_PDU.REASON_REJECTED);                             /* 03 */
                    /* Drop the link */
                    return(false);
                }
                else
                if (!link.Answered)
                {
                    /* Confirm we agree (CC) */
                    link.Send_CC();
                }
                break;

            case LLCP_PDU.PTYPE_DISC:
                Trace.WriteLine("> DISC");

                /* The client is leaving us */
                if (link.State == LLCP_Link.StateActive)
                {
                    OnDisconnect(link);
                    link.Send_DM(LLCP_DM_PDU.REASON_DISC_OK);                             /* 00 */
                }
                else
                {
                    link.Send_DM(LLCP_DM_PDU.REASON_NO_CONNECTION);                             /* 01 */
                }
                /* Drop the link */
                return(false);

            case LLCP_PDU.PTYPE_I:
                Trace.WriteLine("> I");

                /* The client is talking to us */
                if (link.State == LLCP_Link.StateActive)
                {
                    byte nr = (byte)(recv_pdu.Sequence & 0x0F);
                    byte ns = (byte)((recv_pdu.Sequence >> 4) & 0x0F);

                    link.SetRemoteSequence((byte)((ns + 1) & 0x0F));

                    OnInformation(link, recv_pdu.Payload);

                    if (!link.Answered)
                    {
                        /* No frame -> ACK */
                        link.Send_RR();
                    }
                }
                else
                {
                    link.Send_DM(LLCP_DM_PDU.REASON_NO_CONNECTION);                             /* 01 */
                }
                break;

            case LLCP_PDU.PTYPE_RR:
                Trace.WriteLine("> RR");

                /* The client is acknowledging */
                if (link.State == LLCP_Link.StateActive)
                {
                    byte nr = (byte)(recv_pdu.Sequence & 0x0F);

                    OnAcknowledge(link);
                }
                else
                {
                    link.Send_DM(LLCP_DM_PDU.REASON_NO_CONNECTION);                             /* 01 */
                }
                break;

            default:
                Trace.WriteLine("Unsupported LLCP frame client->server");
                link.Send_FRMR(0x80, recv_pdu.PTYPE, 0x00);
                /* Drop the link */
                return(false);
            }

            /* Keep the link OK */
            return(true);
        }
Exemple #3
0
        public override void OnInformation(LLCP_Link link, byte[] ServiceDataUnit)
        {
            if (Fragments)
            {
                /* Is the length acceptable ? */
                if (Offset + ServiceDataUnit.Length > Buffer.Length)
                {
                    /* Overflow */
                    Trace.WriteLine("SNEP Server: bad request");
                    ServerResponse(link, SNEP.RSP_BAD_REQUEST);
                    return;
                }

                /* Get the NFC_DATA */
                for (int i = 0; i < ServiceDataUnit.Length; i++)
                {
                    Buffer[Offset++] = ServiceDataUnit[i];
                }

                /* Is the message complete ? */
                if (Offset >= Buffer.Length)
                {
                    /* Yes ! Process the message */
                    Trace.WriteLine("SNEP Server: got last fragment (" + ServiceDataUnit.Length + "B)");
                    Fragments = false;
                }
                else
                {
                    /* Do nothing */
                    Trace.WriteLine("SNEP Server: got a fragment (" + ServiceDataUnit.Length + "B), more to come");
                    link.Send_RR();
                    return;
                }
            }
            else
            {
                if (ServiceDataUnit.Length < 6)
                {
                    /* Frame is too short to have the REQUEST header */
                    Trace.WriteLine("SNEP Server: bad request");
                    ServerResponse(link, SNEP.RSP_BAD_REQUEST);
                    return;
                }

                /* Get version */
                byte version = ServiceDataUnit[0];
                if ((version < 0x10) || (version >= 0x20))
                {
                    /* Version not supported */
                    Trace.WriteLine("SNEP Server: unsupported version");
                    ServerResponse(link, SNEP.RSP_UNSUPPORTED_VERSION);
                    return;
                }

                /* Request code */
                byte request = ServiceDataUnit[1];
                if (request != SNEP.REQ_PUT)
                {
                    /* Only PUT REQUEST implemented */
                    Trace.WriteLine("SNEP Server: not implemented");
                    ServerResponse(link, SNEP.RSP_NOT_IMPLEMENTED);
                    return;
                }

                /* Message length */
                uint total_length;
                total_length  = ServiceDataUnit[2]; total_length <<= 8;
                total_length |= ServiceDataUnit[3]; total_length <<= 8;
                total_length |= ServiceDataUnit[4]; total_length <<= 8;
                total_length |= ServiceDataUnit[5];

                /* Is the length acceptable ? */
                if (total_length > SNEP.MAX_BUFFER_SIZE)
                {
                    /* Overflow */
                    Trace.WriteLine("SNEP Server: reject");
                    ServerResponse(link, SNEP.RSP_REJECT);
                    return;
                }

                /* Allocate the buffer */
                Buffer = new byte[total_length];
                Offset = 0;

                /* Get the NFC_DATA */
                for (int i = 6; i < ServiceDataUnit.Length; i++)
                {
                    Buffer[Offset++] = ServiceDataUnit[i];
                }

                /* Is the message complete ? */
                if (Offset >= Buffer.Length)
                {
                    /* Yes ! Process the message */
                    Trace.WriteLine("SNEP Server: got single fragment (" + ServiceDataUnit.Length + "B)");
                }
                else
                {
                    /* No ! The client may continue */
                    Fragments = true;
                    Trace.WriteLine("SNEP Server: got first fragment (" + ServiceDataUnit.Length + "B), more to come");
                    Trace.WriteLine("SNEP Server: continue");
                    ServerResponse(link, SNEP.RSP_CONTINUE);
                    return;
                }
            }

            /* Say thank you */
            Trace.WriteLine("SNEP Server: thank you");
            ServerResponse(link, SNEP.RSP_SUCCESS);

            /* Process the message */
            if (OnMessageReceived != null)
            {
                OnMessageReceived(Buffer);
            }
        }