Ejemplo n.º 1
0
        /// <summary>
        /// Executes the GET method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public void GET(CoapMessage Request, CoapResponse Response)
        {
            bool FromBootstrapServer = this.client.IsFromBootstrapServer(Request);

            if (this.id == 0 && !FromBootstrapServer)
            {
                Response.RST(CoapCode.Unauthorized);
                return;
            }

            if (!string.IsNullOrEmpty(Request.SubPath))
            {
                Response.RST(CoapCode.NotFound);
                return;
            }

            if (!Request.IsAcceptable(CoreLinkFormat.ContentFormatCode))
            {
                Response.RST(CoapCode.NotAcceptable);
                return;
            }

            StringBuilder Output = new StringBuilder();

            this.EncodeObjectLinks(this.client.State == Lwm2mState.Bootstrap && FromBootstrapServer, Output);

            Response.Respond(CoapCode.Content, Encoding.UTF8.GetBytes(Output.ToString()), 64,
                             new CoapOptionContentFormat(CoreLinkFormat.ContentFormatCode));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Executes the PUT method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public void PUT(CoapMessage Request, CoapResponse Response)
        {
            if (this.Client.State == Lwm2mState.Bootstrap &&
                this.Client.IsFromBootstrapServer(Request))
            {
                if (!string.IsNullOrEmpty(Request.SubPath) &&
                    ushort.TryParse(Request.SubPath.Substring(1), out ushort InstanceId))
                {
                    Lwm2mSecurityObjectInstance Instance = new Lwm2mSecurityObjectInstance(InstanceId);
                    this.Add(Instance);
                    this.Client.Endpoint.Register(Instance);
                    Instance.AfterRegister(this.Client);

                    Request.Path   += Request.SubPath;
                    Request.SubPath = string.Empty;

                    Instance.PUT(Request, Response);
                }
                else
                {
                    Response.RST(CoapCode.BadRequest);
                }
            }
            else
            {
                Response.RST(CoapCode.Unauthorized);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes the GET method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public virtual void GET(CoapMessage Request, CoapResponse Response)
        {
            ILwm2mWriter Writer;
            bool         FromBootstrapServer = this.objInstance.Object.Client.IsFromBootstrapServer(Request);

            if (this.id == 0 && !FromBootstrapServer)
            {
                Response.RST(CoapCode.Unauthorized);
                return;
            }

            if (Request.Accept is null)
            {
                Writer = new TextWriter();
            }
            else if (Request.IsAcceptable(Tlv.ContentFormatCode))
            {
                Writer = new TlvWriter();
            }
            else if (Request.IsAcceptable(Json.ContentFormatCode))
            {
                Writer = new JsonWriter(this.objInstance.Path + "/");
            }
            else if (Request.IsAcceptable(CoAP.ContentFormats.PlainText.ContentFormatCode))
            {
                Writer = new TextWriter();
            }
            else if (Request.IsAcceptable(CoAP.ContentFormats.Binary.ContentFormatCode))
            {
                Writer = new OpaqueWriter();
            }
            else
            {
                Response.RST(CoapCode.NotAcceptable);
                return;
            }

            if (this.OnBeforeGet != null)
            {
                try
                {
                    this.OnBeforeGet.Invoke(this, new CoapRequestEventArgs(Request));
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }

            this.Write(Writer);

            byte[] Payload = Writer.ToArray();

            Response.Respond(CoapCode.Content, Payload, 64,
                             new CoapOptionContentFormat(Writer.ContentFormat));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Executes the GET method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public virtual void GET(CoapMessage Request, CoapResponse Response)
        {
            ILwm2mWriter Writer;
            bool         FromBootstrapServer = this.obj.Client.IsFromBootstrapServer(Request);

            if (this.id == 0 && !FromBootstrapServer)
            {
                Response.RST(CoapCode.Unauthorized);
                return;
            }

            if (!string.IsNullOrEmpty(Request.SubPath))
            {
                Response.RST(CoapCode.NotFound);
                return;
            }

            if (Request.IsAcceptable(Tlv.ContentFormatCode))
            {
                Writer = new TlvWriter();
            }
            else if (Request.IsAcceptable(Json.ContentFormatCode))
            {
                Writer = new JsonWriter(this.Path + "/");
            }
            else if (Request.IsAcceptable(CoAP.ContentFormats.CoreLinkFormat.ContentFormatCode))
            {
                StringBuilder Output = new StringBuilder();
                this.EncodeObjectLinks(FromBootstrapServer, Output);

                Response.Respond(CoapCode.Content, Encoding.UTF8.GetBytes(Output.ToString()), 64,
                                 new CoapOptionContentFormat(CoAP.ContentFormats.CoreLinkFormat.ContentFormatCode));

                return;
            }
            else
            {
                Response.RST(CoapCode.NotAcceptable);
                return;
            }

            this.Export(Writer);

            byte[] Payload = Writer.ToArray();

            if (Payload.Length == 0)
            {
                Response.RST(CoapCode.NotFound);
            }
            else
            {
                Response.Respond(CoapCode.Content, Payload, 64,
                                 new CoapOptionContentFormat(Writer.ContentFormat));
            }
        }
 /// <summary>
 /// Executes the GET method on the resource.
 /// </summary>
 /// <param name="Request">CoAP Request</param>
 /// <param name="Response">CoAP Response</param>
 /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
 public override void GET(CoapMessage Request, CoapResponse Response)
 {
     if (this.Object.Client.IsFromBootstrapServer(Request))
     {
         base.GET(Request, Response);
     }
     else
     {
         Response.RST(CoapCode.Unauthorized);
     }
 }
Ejemplo n.º 6
0
 public void POST(CoapMessage Request, CoapResponse Response)
 {
     if (this.client.IsFromBootstrapServer(Request))
     {
         Task T = this.client.BootstrapCompleted();
         Response.Respond(CoapCode.Changed);
     }
     else
     {
         Response.RST(CoapCode.Unauthorized);
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Executes the DELETE method on the resource.
 /// </summary>
 /// <param name="Request">CoAP Request</param>
 /// <param name="Response">CoAP Response</param>
 /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
 public void DELETE(CoapMessage Request, CoapResponse Response)
 {
     if (this.Object.Client.State == Lwm2mState.Bootstrap &&
         this.Object.Client.IsFromBootstrapServer(Request))
     {
         Task T = this.DeleteBootstrapInfo();
         Response.ACK(CoapCode.Deleted);
     }
     else
     {
         Response.RST(CoapCode.Unauthorized);
     }
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Executes the DELETE method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public void DELETE(CoapMessage Request, CoapResponse Response)
        {
            if (this.state != Lwm2mState.Bootstrap)
            {
                if (this.IsFromBootstrapServer(Request))
                {
                    this.State = Lwm2mState.Bootstrap;
                }
                else
                {
                    Response.RST(CoapCode.Unauthorized);
                    return;
                }
            }

            Task T = this.DeleteBootstrapInfo();

            Response.ACK(CoapCode.Deleted);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Executes the PUT method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public virtual void PUT(CoapMessage Request, CoapResponse Response)
        {
            bool FromBootstrapServer = this.objInstance.Object.Client.IsFromBootstrapServer(Request);

            if (this.id == 0 && !FromBootstrapServer)
            {
                Response.RST(CoapCode.Unauthorized);
                return;
            }

            if (Request.UriQuery != null && Request.UriQuery.Count > 0)                // Write attributes
            {
                if (!FromBootstrapServer)
                {
                    Response.RST(CoapCode.Unauthorized);
                    return;
                }

                foreach (KeyValuePair <string, string> P in Request.UriQuery)
                {
                    switch (P.Key)
                    {
                    case "pmin":
                    case "pmax":
                    case "gt":
                    case "lt":
                    case "st":
                        // TODO: Implement support
                        break;

                    default:
                        Response.RST(CoapCode.BadRequest);
                        return;
                    }
                }
            }

            if (Request.ContentFormat != null)                  // Write operation
            {
                if (!this.canWrite && !FromBootstrapServer)
                {
                    Response.Respond(CoapCode.BadRequest);
                    return;
                }

                object Decoded = Request.Decode();

                if (Decoded is TlvRecord[] Records)
                {
                    foreach (TlvRecord Rec in Records)
                    {
                        this.Read(Rec);
                    }
                }
                else
                {
                    Response.Respond(CoapCode.NotAcceptable);
                    return;
                }

                this.RemoteUpdate(Request);
            }
            else
            {
                this.Execute(Request);
            }

            Response.Respond(CoapCode.Changed);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Executes the PUT method on the resource.
        /// </summary>
        /// <param name="Request">CoAP Request</param>
        /// <param name="Response">CoAP Response</param>
        /// <exception cref="CoapException">If an error occurred when processing the method.</exception>
        public virtual void PUT(CoapMessage Request, CoapResponse Response)
        {
            bool FromBootstrapServer = this.obj.Client.IsFromBootstrapServer(Request);

            if (this.id == 0 && !FromBootstrapServer)
            {
                Response.RST(CoapCode.Unauthorized);
                return;
            }

            if (Request.UriQuery != null && Request.UriQuery.Count > 0)                // Write attributes
            {
                if (!FromBootstrapServer)
                {
                    Response.RST(CoapCode.Unauthorized);
                    return;
                }

                foreach (KeyValuePair <string, string> P in Request.UriQuery)
                {
                    switch (P.Key)
                    {
                    case "pmin":
                    case "pmax":
                    case "gt":
                    case "lt":
                    case "st":
                        // TODO: Implement support
                        break;

                    default:
                        Response.RST(CoapCode.BadRequest);
                        return;
                    }
                }
            }

            if (Request.ContentFormat != null)                  // Write operation
            {
                object Decoded = Request.Decode();

                if (Decoded is TlvRecord[] Records)
                {
                    LinkedList <KeyValuePair <Lwm2mResource, TlvRecord> > ToWrite =
                        new LinkedList <KeyValuePair <Lwm2mResource, TlvRecord> >();

                    lock (this.resources)
                    {
                        foreach (TlvRecord Rec in Records)
                        {
                            if (!this.resources.TryGetValue(Rec.Identifier, out Lwm2mResource Resource) ||
                                (!Resource.CanWrite && !FromBootstrapServer))
                            {
                                ToWrite = null;
                                break;
                            }

                            ToWrite.AddLast(new KeyValuePair <Lwm2mResource, TlvRecord>(Resource, Rec));
                        }
                    }

                    if (ToWrite is null)
                    {
                        Response.Respond(CoapCode.BadRequest);
                        return;
                    }

                    if (Request.Code == CoapCode.PUT)                       // POST updates, PUT recreates
                    {
                        foreach (Lwm2mResource Resource in this.Resources)
                        {
                            Resource.Reset();
                        }
                    }

                    foreach (KeyValuePair <Lwm2mResource, TlvRecord> Rec in ToWrite)
                    {
                        Rec.Key.Read(Rec.Value);
                        Rec.Key.RemoteUpdate(Request);
                    }
                }
                else
                {
                    Response.Respond(CoapCode.NotAcceptable);
                    return;
                }
            }
            else
            {
                Response.Respond(CoapCode.BadRequest);
                return;
            }

            Response.Respond(CoapCode.Changed);
        }